Mercurial > pidgin
annotate src/protocols/oscar/ft.c @ 11369:ab0fa7cd61cc
[gaim-migrate @ 13593]
Bring all the changes to OSCAR file transfers from oldstatus to HEAD.
Add documentation resulting from my Summer of Code project to CVS including
the original editable files (in OpenOffice.org 2.0 format).
committer: Tailor Script <tailor@pidgin.im>
author | Jonathan Clark <ardentlygnarly> |
---|---|
date | Tue, 30 Aug 2005 05:21:58 +0000 |
parents | 7d31d61e6438 |
children | 48244c196228 |
rev | line source |
---|---|
2086 | 1 /* |
4617 | 2 * Oscar File transfer (OFT) and Oscar Direct Connect (ODC). |
3 * (ODC is also referred to as DirectIM and IM Image.) | |
4 * | |
11369 | 5 * There are a few static helper functions at the top, then |
4617 | 6 * ODC stuff, then ft stuff. |
7 * | |
11369 | 8 * I feel like this is a good place to explain OFT, so I'm going to |
9 * do just that. Each OFT packet has a header type. I guess this | |
10 * is pretty similar to the subtype of a SNAC packet. The type | |
11 * basically tells the other client the meaning of the OFT packet. | |
12 * There are two distinct types of file transfer, which I usually | |
13 * call "sendfile" and "getfile." Sendfile is when you send a file | |
14 * to another AIM user. Getfile is when you share a group of files, | |
4617 | 15 * and other users request that you send them the files. |
16 * | |
17 * A typical sendfile file transfer goes like this: | |
11369 | 18 * 1) Sender sends a channel 2 ICBM telling the other user that |
19 * we want to send them a file. At the same time, we open a | |
20 * listener socket (this should be done before sending the | |
21 * ICBM) on some port, and wait for them to connect to us. | |
22 * The ICBM we sent should contain our IP address and the port | |
4617 | 23 * number that we're listening on. |
11369 | 24 * 2) The receiver connects to the sender on the given IP address |
25 * and port. After the connection is established, the receiver | |
5146 | 26 * sends an ICBM signifying that we are ready and waiting. |
11369 | 27 * 3) The sender sends an OFT PROMPT message over the OFT |
4617 | 28 * connection. |
11369 | 29 * 4) The receiver of the file sends back an exact copy of this |
30 * OFT packet, except the cookie is filled in with the cookie | |
31 * from the ICBM. I think this might be an attempt to verify | |
32 * that the user that is connected is actually the guy that | |
4617 | 33 * we sent the ICBM to. Oh, I've been calling this the ACK. |
11369 | 34 * 5) The sender starts sending raw data across the connection |
4617 | 35 * until the entire file has been sent. |
11369 | 36 * 6) The receiver knows the file is finished because the sender |
37 * sent the file size in an earlier OFT packet. So then the | |
38 * receiver sends the DONE thingy (after filling in the | |
5146 | 39 * "received" checksum and size) and closes the connection. |
2086 | 40 */ |
41 | |
42 #define FAIM_INTERNAL | |
5415 | 43 #ifdef HAVE_CONFIG_H |
44 #include <config.h> | |
45 #endif | |
46 | |
2086 | 47 #include <aim.h> |
48 | |
49 #ifndef _WIN32 | |
5927 | 50 #include <stdio.h> |
2086 | 51 #include <netdb.h> |
52 #include <sys/socket.h> | |
53 #include <netinet/in.h> | |
4617 | 54 #include <sys/utsname.h> /* for aim_odc_initiate */ |
3630 | 55 #include <arpa/inet.h> /* for inet_ntoa */ |
8080 | 56 #include <limits.h> /* for UINT_MAX */ |
3960 | 57 #define G_DIR_SEPARATOR '/' |
3646
bfd8df165f32
[gaim-migrate @ 3770]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
3630
diff
changeset
|
58 #endif |
bfd8df165f32
[gaim-migrate @ 3770]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
3630
diff
changeset
|
59 |
bfd8df165f32
[gaim-migrate @ 3770]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
3630
diff
changeset
|
60 #ifdef _WIN32 |
3630 | 61 #include "win32dep.h" |
62 #endif | |
2086 | 63 |
9430 | 64 /* |
65 * I really want to switch all our networking code to using IPv6 only, | |
66 * but that really isn't a good idea at all. Evan S. of Adium says | |
67 * OS X sets all connections as "AF_INET6/PF_INET6," even if there is | |
68 * nothing inherently IPv6 about them. And I feel like Linux kernel | |
69 * 2.6.5 is doing the same thing. So we REALLY should accept | |
70 * connections if they're showing up as IPv6. Old OSes (Solaris?) | |
71 * that might not have full IPv6 support yet will fail if we try | |
72 * to use PF_INET6 but it isn't defined. --Mark Doliner | |
73 */ | |
74 #ifndef PF_INET6 | |
75 #define PF_INET6 PF_INET | |
76 #endif | |
77 | |
4617 | 78 struct aim_odc_intdata { |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
79 fu8_t cookie[8]; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
80 char sn[MAXSNLEN+1]; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
81 char ip[22]; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
82 }; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
83 |
4617 | 84 /** |
85 * Convert the directory separator from / (0x2f) to ^A (0x01) | |
86 * | |
87 * @param name The filename to convert. | |
88 */ | |
89 static void aim_oft_dirconvert_tostupid(char *name) | |
90 { | |
91 while (name[0]) { | |
92 if (name[0] == 0x01) | |
93 name[0] = G_DIR_SEPARATOR; | |
94 name++; | |
95 } | |
96 } | |
97 | |
98 /** | |
99 * Convert the directory separator from ^A (0x01) to / (0x2f) | |
100 * | |
101 * @param name The filename to convert. | |
102 */ | |
103 static void aim_oft_dirconvert_fromstupid(char *name) | |
104 { | |
105 while (name[0]) { | |
106 if (name[0] == G_DIR_SEPARATOR) | |
107 name[0] = 0x01; | |
108 name++; | |
109 } | |
110 } | |
111 | |
112 /** | |
113 * Calculate oft checksum of buffer | |
114 * | |
11369 | 115 * Prevcheck should be 0xFFFF0000 when starting a checksum of a file. The |
116 * checksum is kind of a rolling checksum thing, so each time you get bytes | |
117 * of a file you just call this puppy and it updates the checksum. You can | |
118 * calculate the checksum of an entire file by calling this in a while or a | |
4617 | 119 * for loop, or something. |
120 * | |
11369 | 121 * Thanks to Graham Booker for providing this improved checksum routine, |
122 * which is simpler and should be more accurate than Josh Myer's original | |
4617 | 123 * code. -- wtm |
124 * | |
11369 | 125 * This algorithm works every time I have tried it. The other fails |
126 * sometimes. So, AOL who thought this up? It has got to be the weirdest | |
4617 | 127 * checksum I have ever seen. |
128 * | |
129 * @param buffer Buffer of data to checksum. Man I'd like to buff her... | |
130 * @param bufsize Size of buffer. | |
131 * @param prevcheck Previous checksum. | |
132 */ | |
4763 | 133 faim_export fu32_t aim_oft_checksum_chunk(const fu8_t *buffer, int bufferlen, fu32_t prevcheck) |
4617 | 134 { |
135 fu32_t check = (prevcheck >> 16) & 0xffff, oldcheck; | |
136 int i; | |
137 unsigned short val; | |
138 | |
139 for (i=0; i<bufferlen; i++) { | |
140 oldcheck = check; | |
141 if (i&1) | |
142 val = buffer[i]; | |
143 else | |
144 val = buffer[i] << 8; | |
145 check -= val; | |
146 /* | |
11369 | 147 * The following appears to be necessary.... It happens |
4617 | 148 * every once in a while and the checksum doesn't fail. |
149 */ | |
150 if (check > oldcheck) | |
151 check--; | |
152 } | |
153 check = ((check & 0x0000ffff) + (check >> 16)); | |
154 check = ((check & 0x0000ffff) + (check >> 16)); | |
155 return check << 16; | |
156 } | |
157 | |
4650 | 158 faim_export fu32_t aim_oft_checksum_file(char *filename) { |
159 FILE *fd; | |
160 fu32_t checksum = 0xffff0000; | |
161 | |
162 if ((fd = fopen(filename, "rb"))) { | |
163 int bytes; | |
4763 | 164 fu8_t buffer[1024]; |
4650 | 165 |
166 while ((bytes = fread(buffer, 1, 1024, fd))) | |
167 checksum = aim_oft_checksum_chunk(buffer, bytes, checksum); | |
168 fclose(fd); | |
169 } | |
170 | |
171 return checksum; | |
172 } | |
173 | |
2086 | 174 /** |
4617 | 175 * After establishing a listening socket, this is called to accept a connection. It |
176 * clones the conn used by the listener, and passes both of these to a signal handler. | |
177 * The signal handler should close the listener conn and keep track of the new conn, | |
178 * since this is what is used for file transfers and what not. | |
179 * | |
180 * @param sess The session. | |
181 * @param cur The conn the incoming connection is on. | |
182 * @return Return 0 if no errors, otherwise return the error number. | |
2086 | 183 */ |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
184 faim_export int aim_handlerendconnect(aim_session_t *sess, aim_conn_t *cur) |
4617 | 185 { |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
186 int acceptfd = 0; |
4617 | 187 struct sockaddr addr; |
188 socklen_t addrlen = sizeof(addr); | |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
189 int ret = 0; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
190 aim_conn_t *newconn; |
4617 | 191 char ip[20]; |
8880 | 192 unsigned short port; |
2086 | 193 |
4617 | 194 if ((acceptfd = accept(cur->fd, &addr, &addrlen)) == -1) |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
195 return 0; /* not an error */ |
2086 | 196 |
9430 | 197 if ((addr.sa_family != PF_INET) && (addr.sa_family != PF_INET6)) { |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
198 close(acceptfd); |
10271 | 199 aim_conn_close(cur); |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
200 return -1; |
4617 | 201 } |
202 | |
203 strncpy(ip, inet_ntoa(((struct sockaddr_in *)&addr)->sin_addr), sizeof(ip)); | |
204 port = ntohs(((struct sockaddr_in *)&addr)->sin_port); | |
2086 | 205 |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
206 if (!(newconn = aim_cloneconn(sess, cur))) { |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
207 close(acceptfd); |
10271 | 208 aim_conn_close(cur); |
4617 | 209 return -ENOMEM; |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
210 } |
2086 | 211 |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
212 newconn->type = AIM_CONN_TYPE_RENDEZVOUS; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
213 newconn->fd = acceptfd; |
2086 | 214 |
4617 | 215 if (newconn->subtype == AIM_CONN_SUBTYPE_OFT_DIRECTIM) { |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
216 aim_rxcallback_t userfunc; |
4617 | 217 struct aim_odc_intdata *priv; |
2086 | 218 |
4617 | 219 priv = (struct aim_odc_intdata *)(newconn->internal = cur->internal); |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
220 cur->internal = NULL; |
8446 | 221 snprintf(priv->ip, sizeof(priv->ip), "%s:%hu", ip, port); |
2086 | 222 |
4617 | 223 if ((userfunc = aim_callhandler(sess, newconn, AIM_CB_FAM_OFT, AIM_CB_OFT_DIRECTIM_ESTABLISHED))) |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
224 ret = userfunc(sess, NULL, newconn, cur); |
2086 | 225 |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
226 } else if (newconn->subtype == AIM_CONN_SUBTYPE_OFT_GETFILE) { |
4617 | 227 } else if (newconn->subtype == AIM_CONN_SUBTYPE_OFT_SENDFILE) { |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
228 aim_rxcallback_t userfunc; |
2086 | 229 |
4617 | 230 if ((userfunc = aim_callhandler(sess, newconn, AIM_CB_FAM_OFT, AIM_CB_OFT_ESTABLISHED))) |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
231 ret = userfunc(sess, NULL, newconn, cur); |
3630 | 232 |
4617 | 233 } else { |
11253 | 234 gaim_debug_warning("oscar", "Got a connection on a listener that's not rendezvous. Closing connection.\n"); |
10271 | 235 aim_conn_close(newconn); |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
236 ret = -1; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
237 } |
2086 | 238 |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
239 return ret; |
2086 | 240 } |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
241 |
2086 | 242 /** |
4617 | 243 * Send client-to-client typing notification over an established direct connection. |
2086 | 244 * |
4617 | 245 * @param sess The session. |
246 * @param conn The already-connected ODC connection. | |
11369 | 247 * @param typing If 0x0002, sends a "typing" message, 0x0001 sends "typed," and |
4870 | 248 * 0x0000 sends "stopped." |
4617 | 249 * @return Return 0 if no errors, otherwise return the error number. |
2086 | 250 */ |
4617 | 251 faim_export int aim_odc_send_typing(aim_session_t *sess, aim_conn_t *conn, int typing) |
2086 | 252 { |
4617 | 253 struct aim_odc_intdata *intdata = (struct aim_odc_intdata *)conn->internal; |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
254 aim_frame_t *fr; |
3952 | 255 aim_bstream_t *hdrbs; |
256 fu8_t *hdr; | |
257 int hdrlen = 0x44; | |
2086 | 258 |
3952 | 259 if (!sess || !conn || (conn->type != AIM_CONN_TYPE_RENDEZVOUS)) |
260 return -EINVAL; | |
2086 | 261 |
4870 | 262 if (!(fr = aim_tx_new(sess, conn, AIM_FRAMETYPE_OFT, 0x0001, 0))) |
3952 | 263 return -ENOMEM; |
264 memcpy(fr->hdr.rend.magic, "ODC2", 4); | |
8934 | 265 fr->hdr.rend.hdrlen = hdrlen + 8; |
2086 | 266 |
3952 | 267 if (!(hdr = calloc(1, hdrlen))) { |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
268 aim_frame_destroy(fr); |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
269 return -ENOMEM; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
270 } |
3952 | 271 |
272 hdrbs = &(fr->data); | |
273 aim_bstream_init(hdrbs, hdr, hdrlen); | |
2086 | 274 |
3952 | 275 aimbs_put16(hdrbs, 0x0006); |
276 aimbs_put16(hdrbs, 0x0000); | |
277 aimbs_putraw(hdrbs, intdata->cookie, 8); | |
278 aimbs_put16(hdrbs, 0x0000); | |
279 aimbs_put16(hdrbs, 0x0000); | |
280 aimbs_put16(hdrbs, 0x0000); | |
281 aimbs_put16(hdrbs, 0x0000); | |
282 aimbs_put32(hdrbs, 0x00000000); | |
283 aimbs_put16(hdrbs, 0x0000); | |
284 aimbs_put16(hdrbs, 0x0000); | |
285 aimbs_put16(hdrbs, 0x0000); | |
2086 | 286 |
4870 | 287 if (typing == 0x0002) |
288 aimbs_put16(hdrbs, 0x0002 | 0x0008); | |
289 else if (typing == 0x0001) | |
290 aimbs_put16(hdrbs, 0x0002 | 0x0004); | |
291 else | |
292 aimbs_put16(hdrbs, 0x0002); | |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
293 |
3952 | 294 aimbs_put16(hdrbs, 0x0000); |
295 aimbs_put16(hdrbs, 0x0000); | |
10990 | 296 aimbs_putstr(hdrbs, sess->sn); |
4617 | 297 |
3952 | 298 aim_bstream_setpos(hdrbs, 52); /* bleeehh */ |
2086 | 299 |
3952 | 300 aimbs_put8(hdrbs, 0x00); |
301 aimbs_put16(hdrbs, 0x0000); | |
302 aimbs_put16(hdrbs, 0x0000); | |
303 aimbs_put16(hdrbs, 0x0000); | |
304 aimbs_put16(hdrbs, 0x0000); | |
305 aimbs_put16(hdrbs, 0x0000); | |
306 aimbs_put16(hdrbs, 0x0000); | |
307 aimbs_put16(hdrbs, 0x0000); | |
308 aimbs_put8(hdrbs, 0x00); | |
2086 | 309 |
3952 | 310 /* end of hdr */ |
2086 | 311 |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
312 aim_tx_enqueue(sess, fr); |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
313 |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
314 return 0; |
4617 | 315 } |
2086 | 316 |
2993 | 317 /** |
4617 | 318 * Send client-to-client IM over an established direct connection. |
319 * Call this just like you would aim_send_im, to send a directim. | |
11369 | 320 * |
4617 | 321 * @param sess The session. |
322 * @param conn The already-connected ODC connection. | |
323 * @param msg Null-terminated string to send. | |
324 * @param len The length of the message to send, including binary data. | |
9826 | 325 * @param encoding See the AIM_CHARSET_* defines in aim.h |
4870 | 326 * @param isawaymsg 0 if this is not an auto-response, 1 if it is. |
4617 | 327 * @return Return 0 if no errors, otherwise return the error number. |
2993 | 328 */ |
4870 | 329 faim_export int aim_odc_send_im(aim_session_t *sess, aim_conn_t *conn, const char *msg, int len, int encoding, int isawaymsg) |
2993 | 330 { |
331 aim_frame_t *fr; | |
3952 | 332 aim_bstream_t *hdrbs; |
4617 | 333 struct aim_odc_intdata *intdata = (struct aim_odc_intdata *)conn->internal; |
3952 | 334 int hdrlen = 0x44; |
335 fu8_t *hdr; | |
2993 | 336 |
4617 | 337 if (!sess || !conn || (conn->type != AIM_CONN_TYPE_RENDEZVOUS) || !msg) |
338 return -EINVAL; | |
2993 | 339 |
4875 | 340 if (!(fr = aim_tx_new(sess, conn, AIM_FRAMETYPE_OFT, 0x01, 0))) |
4617 | 341 return -ENOMEM; |
2993 | 342 |
3952 | 343 memcpy(fr->hdr.rend.magic, "ODC2", 4); |
8934 | 344 fr->hdr.rend.hdrlen = hdrlen + 8; |
4617 | 345 |
3952 | 346 if (!(hdr = calloc(1, hdrlen + len))) { |
2993 | 347 aim_frame_destroy(fr); |
348 return -ENOMEM; | |
349 } | |
3952 | 350 |
351 hdrbs = &(fr->data); | |
352 aim_bstream_init(hdrbs, hdr, hdrlen + len); | |
353 | |
354 aimbs_put16(hdrbs, 0x0006); | |
355 aimbs_put16(hdrbs, 0x0000); | |
356 aimbs_putraw(hdrbs, intdata->cookie, 8); | |
357 aimbs_put16(hdrbs, 0x0000); | |
358 aimbs_put16(hdrbs, 0x0000); | |
359 aimbs_put16(hdrbs, 0x0000); | |
360 aimbs_put16(hdrbs, 0x0000); | |
361 aimbs_put32(hdrbs, len); | |
362 aimbs_put16(hdrbs, encoding); | |
363 aimbs_put16(hdrbs, 0x0000); | |
364 aimbs_put16(hdrbs, 0x0000); | |
4617 | 365 |
4870 | 366 /* flags - used for typing notification and to mark if this is an away message */ |
367 aimbs_put16(hdrbs, 0x0000 | isawaymsg); | |
3952 | 368 |
369 aimbs_put16(hdrbs, 0x0000); | |
370 aimbs_put16(hdrbs, 0x0000); | |
10990 | 371 aimbs_putstr(hdrbs, sess->sn); |
3952 | 372 |
373 aim_bstream_setpos(hdrbs, 52); /* bleeehh */ | |
374 | |
375 aimbs_put8(hdrbs, 0x00); | |
376 aimbs_put16(hdrbs, 0x0000); | |
377 aimbs_put16(hdrbs, 0x0000); | |
378 aimbs_put16(hdrbs, 0x0000); | |
379 aimbs_put16(hdrbs, 0x0000); | |
380 aimbs_put16(hdrbs, 0x0000); | |
381 aimbs_put16(hdrbs, 0x0000); | |
382 aimbs_put16(hdrbs, 0x0000); | |
383 aimbs_put8(hdrbs, 0x00); | |
4617 | 384 |
2993 | 385 /* end of hdr2 */ |
4617 | 386 |
11369 | 387 #if 0 /* XXX - this is how you send buddy icon info... */ |
4617 | 388 aimbs_put16(hdrbs, 0x0008); |
389 aimbs_put16(hdrbs, 0x000c); | |
390 aimbs_put16(hdrbs, 0x0000); | |
391 aimbs_put16(hdrbs, 0x1466); | |
392 aimbs_put16(hdrbs, 0x0001); | |
393 aimbs_put16(hdrbs, 0x2e0f); | |
394 aimbs_put16(hdrbs, 0x393e); | |
395 aimbs_put16(hdrbs, 0xcac8); | |
2993 | 396 #endif |
11159 | 397 aimbs_putraw(hdrbs, (guchar *)msg, len); |
4617 | 398 |
2993 | 399 aim_tx_enqueue(sess, fr); |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
400 |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
401 return 0; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
402 } |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
403 |
2086 | 404 /** |
4617 | 405 * Get the screen name of the peer of a direct connection. |
8983 | 406 * |
4617 | 407 * @param conn The ODC connection. |
408 * @return The screen name of the dude, or NULL if there was an anomaly. | |
2086 | 409 */ |
4617 | 410 faim_export const char *aim_odc_getsn(aim_conn_t *conn) |
411 { | |
412 struct aim_odc_intdata *intdata; | |
3630 | 413 |
4617 | 414 if (!conn || !conn->internal) |
3952 | 415 return NULL; |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
416 |
8983 | 417 if ((conn->type != AIM_CONN_TYPE_RENDEZVOUS) || |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
418 (conn->subtype != AIM_CONN_SUBTYPE_OFT_DIRECTIM)) |
3952 | 419 return NULL; |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
420 |
4617 | 421 intdata = (struct aim_odc_intdata *)conn->internal; |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
422 |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
423 return intdata->sn; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
424 } |
2086 | 425 |
426 /** | |
8983 | 427 * Get the cookie of a direct connection. |
428 * | |
429 * @param conn The ODC connection. | |
430 * @return The cookie, an 8 byte unterminated string, or NULL if there was an anomaly. | |
431 */ | |
11159 | 432 faim_export const guchar *aim_odc_getcookie(aim_conn_t *conn) |
8983 | 433 { |
434 struct aim_odc_intdata *intdata; | |
435 | |
436 if (!conn || !conn->internal) | |
437 return NULL; | |
438 | |
439 intdata = (struct aim_odc_intdata *)conn->internal; | |
440 | |
441 return intdata->cookie; | |
442 } | |
443 | |
444 /** | |
4617 | 445 * Find the conn of a direct connection with the given buddy. |
446 * | |
447 * @param sess The session. | |
448 * @param sn The screen name of the buddy whose direct connection you want to find. | |
11369 | 449 * @return The conn for the direct connection with the given buddy, or NULL if no |
4617 | 450 * connection was found. |
451 */ | |
452 faim_export aim_conn_t *aim_odc_getconn(aim_session_t *sess, const char *sn) | |
453 { | |
454 aim_conn_t *cur; | |
455 struct aim_odc_intdata *intdata; | |
456 | |
457 if (!sess || !sn || !strlen(sn)) | |
458 return NULL; | |
459 | |
460 for (cur = sess->connlist; cur; cur = cur->next) { | |
461 if ((cur->type == AIM_CONN_TYPE_RENDEZVOUS) && (cur->subtype == AIM_CONN_SUBTYPE_OFT_DIRECTIM)) { | |
462 intdata = cur->internal; | |
463 if (!aim_sncmp(intdata->sn, sn)) | |
464 return cur; | |
465 } | |
466 } | |
467 | |
468 return NULL; | |
469 } | |
470 | |
471 /** | |
472 * For those times when we want to open up the direct connection channel ourselves. | |
473 * | |
8982 | 474 * You'll want to set up some kind of watcher on this socket. |
475 * When the state changes, call aim_handlerendconnection with | |
476 * the connection returned by this. aim_handlerendconnection | |
4617 | 477 * will accept the pending connection and stop listening. |
478 * | |
479 * @param sess The session | |
480 * @param sn The screen name to connect to. | |
481 * @return The new connection. | |
482 */ | |
8982 | 483 faim_export aim_conn_t *aim_odc_initiate(aim_session_t *sess, const char *sn, int listenfd, |
484 const fu8_t *localip, fu16_t port, const fu8_t *mycookie) | |
4617 | 485 { |
486 aim_conn_t *newconn; | |
487 aim_msgcookie_t *cookie; | |
488 struct aim_odc_intdata *priv; | |
489 fu8_t ck[8]; | |
490 | |
8982 | 491 if (!localip) |
4617 | 492 return NULL; |
493 | |
8982 | 494 if (mycookie) { |
495 memcpy(ck, mycookie, 8); | |
496 aim_im_sendch2_odcrequest(sess, ck, TRUE, sn, localip, port); | |
497 } else | |
498 aim_im_sendch2_odcrequest(sess, ck, FALSE, sn, localip, port); | |
4617 | 499 |
500 cookie = (aim_msgcookie_t *)calloc(1, sizeof(aim_msgcookie_t)); | |
501 memcpy(cookie->cookie, ck, 8); | |
502 cookie->type = AIM_COOKIETYPE_OFTIM; | |
503 | |
504 /* this one is for the cookie */ | |
505 priv = (struct aim_odc_intdata *)calloc(1, sizeof(struct aim_odc_intdata)); | |
506 | |
507 memcpy(priv->cookie, ck, 8); | |
508 strncpy(priv->sn, sn, sizeof(priv->sn)); | |
509 cookie->data = priv; | |
510 aim_cachecookie(sess, cookie); | |
511 | |
512 /* XXX - switch to aim_cloneconn()? */ | |
11162 | 513 if (!(newconn = aim_newconn(sess, AIM_CONN_TYPE_LISTENER))) { |
4617 | 514 close(listenfd); |
515 return NULL; | |
516 } | |
517 | |
518 /* this one is for the conn */ | |
519 priv = (struct aim_odc_intdata *)calloc(1, sizeof(struct aim_odc_intdata)); | |
520 | |
521 memcpy(priv->cookie, ck, 8); | |
522 strncpy(priv->sn, sn, sizeof(priv->sn)); | |
523 | |
524 newconn->fd = listenfd; | |
525 newconn->subtype = AIM_CONN_SUBTYPE_OFT_DIRECTIM; | |
526 newconn->internal = priv; | |
527 newconn->lastactivity = time(NULL); | |
528 | |
529 return newconn; | |
530 } | |
531 | |
532 /** | |
533 * Connect directly to the given buddy for directim. | |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
534 * |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
535 * This is a wrapper for aim_newconn. |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
536 * |
11369 | 537 * If addr is NULL, the socket is not created, but the connection is |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
538 * allocated and setup to connect. |
2086 | 539 * |
4617 | 540 * @param sess The Godly session. |
541 * @param sn The screen name we're connecting to. I hope it's a girl... | |
542 * @param addr Address to connect to. | |
543 * @return The new connection. | |
2086 | 544 */ |
4617 | 545 faim_export aim_conn_t *aim_odc_connect(aim_session_t *sess, const char *sn, const char *addr, const fu8_t *cookie) |
546 { | |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
547 aim_conn_t *newconn; |
4617 | 548 struct aim_odc_intdata *intdata; |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
549 |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
550 if (!sess || !sn) |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
551 return NULL; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
552 |
5146 | 553 if (!(intdata = calloc(1, sizeof(struct aim_odc_intdata)))) |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
554 return NULL; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
555 memcpy(intdata->cookie, cookie, 8); |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
556 strncpy(intdata->sn, sn, sizeof(intdata->sn)); |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
557 if (addr) |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
558 strncpy(intdata->ip, addr, sizeof(intdata->ip)); |
2086 | 559 |
4617 | 560 /* XXX - verify that non-blocking connects actually work */ |
11162 | 561 if (!(newconn = aim_newconn(sess, AIM_CONN_TYPE_RENDEZVOUS))) { |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
562 free(intdata); |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
563 return NULL; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
564 } |
2086 | 565 |
4617 | 566 newconn->internal = intdata; |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
567 newconn->subtype = AIM_CONN_SUBTYPE_OFT_DIRECTIM; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
568 |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
569 return newconn; |
4617 | 570 } |
2086 | 571 |
572 /** | |
4826 | 573 * Sometimes you just don't know with these kinds of people. |
574 * | |
575 * @param sess The session. | |
576 * @param conn The ODC connection of the incoming data. | |
577 * @param frr The frame allocated for the incoming data. | |
578 * @param bs It stands for "bologna sandwich." | |
579 * @return Return 0 if no errors, otherwise return the error number. | |
580 */ | |
581 static int handlehdr_odc(aim_session_t *sess, aim_conn_t *conn, aim_frame_t *frr, aim_bstream_t *bs) | |
582 { | |
583 aim_frame_t fr; | |
4870 | 584 int ret = 0; |
4826 | 585 aim_rxcallback_t userfunc; |
586 fu32_t payloadlength; | |
587 fu16_t flags, encoding; | |
588 char *snptr = NULL; | |
589 | |
590 fr.conn = conn; | |
591 | |
592 /* AAA - ugly */ | |
593 aim_bstream_setpos(bs, 20); | |
594 payloadlength = aimbs_get32(bs); | |
595 | |
596 aim_bstream_setpos(bs, 24); | |
597 encoding = aimbs_get16(bs); | |
598 | |
599 aim_bstream_setpos(bs, 30); | |
600 flags = aimbs_get16(bs); | |
601 | |
602 aim_bstream_setpos(bs, 36); | |
603 /* XXX - create an aimbs_getnullstr function? */ | |
4913 | 604 snptr = aimbs_getstr(bs, 32); /* Next 32 bytes contain the sn, padded with null chars */ |
4826 | 605 |
11253 | 606 gaim_debug_misc("oscar", "faim: OFT frame: handlehdr_odc: %04x / %04x / %s\n", payloadlength, flags, snptr); |
4826 | 607 |
4870 | 608 if (flags & 0x0008) { |
609 if ((userfunc = aim_callhandler(sess, conn, AIM_CB_FAM_OFT, AIM_CB_OFT_DIRECTIMTYPING))) | |
610 ret = userfunc(sess, &fr, snptr, 2); | |
611 } else if (flags & 0x0004) { | |
612 if ((userfunc = aim_callhandler(sess, conn, AIM_CB_FAM_OFT, AIM_CB_OFT_DIRECTIMTYPING))) | |
613 ret = userfunc(sess, &fr, snptr, 1); | |
614 } else { | |
4826 | 615 if ((userfunc = aim_callhandler(sess, conn, AIM_CB_FAM_OFT, AIM_CB_OFT_DIRECTIMTYPING))) |
616 ret = userfunc(sess, &fr, snptr, 0); | |
4870 | 617 } |
4826 | 618 |
8000 | 619 if ((payloadlength != 0) && (payloadlength != UINT_MAX)) { |
4870 | 620 char *msg; |
4826 | 621 int recvd = 0; |
4870 | 622 int i, isawaymsg; |
623 | |
624 isawaymsg = flags & 0x0001; | |
4826 | 625 |
4895 | 626 if (!(msg = calloc(1, payloadlength+1))) { |
627 free(snptr); | |
4870 | 628 return -ENOMEM; |
4895 | 629 } |
4870 | 630 |
4826 | 631 while (payloadlength - recvd) { |
632 if (payloadlength - recvd >= 1024) | |
4870 | 633 i = aim_recv(conn->fd, &msg[recvd], 1024); |
11369 | 634 else |
4870 | 635 i = aim_recv(conn->fd, &msg[recvd], payloadlength - recvd); |
4826 | 636 if (i <= 0) { |
637 free(msg); | |
4895 | 638 free(snptr); |
4826 | 639 return -1; |
640 } | |
641 recvd = recvd + i; | |
642 if ((userfunc = aim_callhandler(sess, conn, AIM_CB_FAM_SPECIAL, AIM_CB_SPECIAL_IMAGETRANSFER))) | |
4870 | 643 ret = userfunc(sess, &fr, snptr, (double)recvd / payloadlength); |
4826 | 644 } |
11369 | 645 |
4870 | 646 if ((userfunc = aim_callhandler(sess, conn, AIM_CB_FAM_OFT, AIM_CB_OFT_DIRECTIMINCOMING))) |
647 ret = userfunc(sess, &fr, snptr, msg, payloadlength, encoding, isawaymsg); | |
4826 | 648 |
649 free(msg); | |
650 } | |
651 | |
4895 | 652 free(snptr); |
653 | |
4870 | 654 return ret; |
4826 | 655 } |
656 | |
11369 | 657 faim_export struct aim_oft_info *aim_oft_createinfo(aim_session_t *sess, const fu8_t *cookie, const char *sn, const char *ip, fu16_t port, fu32_t size, fu32_t modtime, char *filename, int send_or_recv, int method, int stage) |
5146 | 658 { |
659 struct aim_oft_info *new; | |
660 | |
661 if (!sess) | |
662 return NULL; | |
663 | |
664 if (!(new = (struct aim_oft_info *)calloc(1, sizeof(struct aim_oft_info)))) | |
665 return NULL; | |
666 | |
667 new->sess = sess; | |
668 if (cookie) | |
669 memcpy(new->cookie, cookie, 8); | |
11369 | 670 else |
671 aim_im_makecookie(new->cookie); | |
5146 | 672 if (ip) |
673 new->clientip = strdup(ip); | |
11369 | 674 else |
675 new->clientip = NULL; | |
5146 | 676 if (sn) |
677 new->sn = strdup(sn); | |
11369 | 678 else |
679 new->sn = NULL; | |
680 new->method = method; | |
681 new->send_or_recv = send_or_recv; | |
682 new->stage = stage; | |
5146 | 683 new->port = port; |
11369 | 684 new->xfer_reffed = FALSE; |
11214 | 685 new->success = FALSE; |
5146 | 686 new->fh.totfiles = 1; |
687 new->fh.filesleft = 1; | |
688 new->fh.totparts = 1; | |
689 new->fh.partsleft = 1; | |
690 new->fh.totsize = size; | |
691 new->fh.size = size; | |
692 new->fh.modtime = modtime; | |
693 new->fh.checksum = 0xffff0000; | |
694 new->fh.rfrcsum = 0xffff0000; | |
695 new->fh.rfcsum = 0xffff0000; | |
696 new->fh.recvcsum = 0xffff0000; | |
697 strncpy(new->fh.idstring, "OFT_Windows ICBMFT V1.1 32", 31); | |
8446 | 698 if (filename) { |
5146 | 699 strncpy(new->fh.name, filename, 63); |
8446 | 700 new->fh.name[63] = '\0'; |
701 } | |
5146 | 702 |
703 new->next = sess->oft_info; | |
704 sess->oft_info = new; | |
705 | |
706 return new; | |
707 } | |
708 | |
11369 | 709 faim_export struct aim_rv_proxy_info *aim_rv_proxy_createinfo(aim_session_t *sess, const fu8_t *cookie, |
710 fu16_t port) | |
711 { | |
712 struct aim_rv_proxy_info *proxy_info; | |
713 | |
714 if (!(proxy_info = (struct aim_rv_proxy_info*)calloc(1, sizeof(struct aim_rv_proxy_info)))) | |
715 return NULL; | |
716 | |
717 proxy_info->sess = sess; | |
718 proxy_info->port = port; | |
719 proxy_info->packet_ver = AIM_RV_PROXY_PACKETVER_DFLT; | |
720 proxy_info->unknownA = AIM_RV_PROXY_UNKNOWNA_DFLT; | |
721 | |
722 if (cookie) | |
723 memcpy(proxy_info->cookie, cookie, 8); | |
724 | |
725 return proxy_info; | |
726 } | |
727 | |
5146 | 728 /** |
11369 | 729 * Remove the given oft_info struct from the oft_info linked list, and |
5146 | 730 * then free its memory. |
731 * | |
732 * @param sess The session. | |
733 * @param oft_info The aim_oft_info struct that we're destroying. | |
734 * @return Return 0 if no errors, otherwise return the error number. | |
735 */ | |
736 faim_export int aim_oft_destroyinfo(struct aim_oft_info *oft_info) | |
737 { | |
738 aim_session_t *sess; | |
739 | |
740 if (!oft_info || !(sess = oft_info->sess)) | |
741 return -EINVAL; | |
742 | |
743 if (sess->oft_info && (sess->oft_info == oft_info)) { | |
744 sess->oft_info = sess->oft_info->next; | |
745 } else { | |
746 struct aim_oft_info *cur; | |
747 for (cur=sess->oft_info; (cur->next && (cur->next!=oft_info)); cur=cur->next); | |
748 if (cur->next) | |
749 cur->next = cur->next->next; | |
750 } | |
751 | |
752 free(oft_info->sn); | |
753 free(oft_info->proxyip); | |
754 free(oft_info->clientip); | |
755 free(oft_info->verifiedip); | |
756 free(oft_info); | |
757 | |
758 return 0; | |
759 } | |
760 | |
4826 | 761 /** |
4617 | 762 * Creates a listener socket so the other dude can connect to us. |
2086 | 763 * |
11369 | 764 * You'll want to set up some kind of watcher on this socket. |
765 * When the state changes, call aim_handlerendconnection with | |
766 * the connection returned by this. aim_handlerendconnection | |
4617 | 767 * will accept the pending connection and stop listening. |
2086 | 768 * |
4617 | 769 * @param sess The session. |
11369 | 770 * @param oft_info File transfer information associated with this |
5146 | 771 * connection. |
772 * @return Return 0 if no errors, otherwise return the error number. | |
2086 | 773 */ |
8240 | 774 faim_export int aim_sendfile_listen(aim_session_t *sess, struct aim_oft_info *oft_info, int listenfd) |
2086 | 775 { |
5146 | 776 if (!oft_info) |
777 return -EINVAL; | |
2086 | 778 |
11162 | 779 if (!(oft_info->conn = aim_newconn(sess, AIM_CONN_TYPE_LISTENER))) { |
4617 | 780 close(listenfd); |
5146 | 781 return -ENOMEM; |
3952 | 782 } |
3630 | 783 |
5146 | 784 oft_info->conn->fd = listenfd; |
785 oft_info->conn->subtype = AIM_CONN_SUBTYPE_OFT_SENDFILE; | |
786 oft_info->conn->lastactivity = time(NULL); | |
3952 | 787 |
5146 | 788 return 0; |
4617 | 789 } |
3630 | 790 |
4617 | 791 /** |
792 * Extract an &aim_fileheader_t from the given buffer. | |
793 * | |
794 * @param bs The should be from an incoming rendezvous packet. | |
795 * @return A pointer to new struct on success, or NULL on error. | |
796 */ | |
797 static struct aim_fileheader_t *aim_oft_getheader(aim_bstream_t *bs) | |
798 { | |
799 struct aim_fileheader_t *fh; | |
3630 | 800 |
4617 | 801 if (!(fh = calloc(1, sizeof(struct aim_fileheader_t)))) |
802 return NULL; | |
3630 | 803 |
4617 | 804 /* The bstream should be positioned after the hdrtype. */ |
805 aimbs_getrawbuf(bs, fh->bcookie, 8); | |
806 fh->encrypt = aimbs_get16(bs); | |
807 fh->compress = aimbs_get16(bs); | |
808 fh->totfiles = aimbs_get16(bs); | |
809 fh->filesleft = aimbs_get16(bs); | |
810 fh->totparts = aimbs_get16(bs); | |
811 fh->partsleft = aimbs_get16(bs); | |
812 fh->totsize = aimbs_get32(bs); | |
813 fh->size = aimbs_get32(bs); | |
814 fh->modtime = aimbs_get32(bs); | |
815 fh->checksum = aimbs_get32(bs); | |
816 fh->rfrcsum = aimbs_get32(bs); | |
817 fh->rfsize = aimbs_get32(bs); | |
818 fh->cretime = aimbs_get32(bs); | |
819 fh->rfcsum = aimbs_get32(bs); | |
820 fh->nrecvd = aimbs_get32(bs); | |
821 fh->recvcsum = aimbs_get32(bs); | |
11159 | 822 aimbs_getrawbuf(bs, (guchar *)fh->idstring, 32); |
4617 | 823 fh->flags = aimbs_get8(bs); |
824 fh->lnameoffset = aimbs_get8(bs); | |
825 fh->lsizeoffset = aimbs_get8(bs); | |
826 aimbs_getrawbuf(bs, fh->dummy, 69); | |
827 aimbs_getrawbuf(bs, fh->macfileinfo, 16); | |
828 fh->nencode = aimbs_get16(bs); | |
829 fh->nlanguage = aimbs_get16(bs); | |
11159 | 830 aimbs_getrawbuf(bs, (guchar *)fh->name, 64); /* XXX - filenames longer than 64B */ |
8446 | 831 fh->name[63] = '\0'; |
2086 | 832 |
4617 | 833 return fh; |
11369 | 834 } |
3630 | 835 |
4617 | 836 /** |
837 * Fills a buffer with network-order fh data | |
838 * | |
839 * @param bs A bstream to fill -- automatically initialized | |
840 * @param fh A struct aim_fileheader_t to get data from. | |
841 * @return Return non-zero on error. | |
842 */ | |
843 static int aim_oft_buildheader(aim_bstream_t *bs, struct aim_fileheader_t *fh) | |
11369 | 844 { |
4617 | 845 fu8_t *hdr; |
3630 | 846 |
4617 | 847 if (!bs || !fh) |
848 return -EINVAL; | |
3952 | 849 |
4617 | 850 if (!(hdr = (unsigned char *)calloc(1, 0x100 - 8))) |
851 return -ENOMEM; | |
3630 | 852 |
4617 | 853 aim_bstream_init(bs, hdr, 0x100 - 8); |
854 aimbs_putraw(bs, fh->bcookie, 8); | |
855 aimbs_put16(bs, fh->encrypt); | |
856 aimbs_put16(bs, fh->compress); | |
857 aimbs_put16(bs, fh->totfiles); | |
858 aimbs_put16(bs, fh->filesleft); | |
859 aimbs_put16(bs, fh->totparts); | |
860 aimbs_put16(bs, fh->partsleft); | |
861 aimbs_put32(bs, fh->totsize); | |
862 aimbs_put32(bs, fh->size); | |
863 aimbs_put32(bs, fh->modtime); | |
864 aimbs_put32(bs, fh->checksum); | |
865 aimbs_put32(bs, fh->rfrcsum); | |
866 aimbs_put32(bs, fh->rfsize); | |
867 aimbs_put32(bs, fh->cretime); | |
868 aimbs_put32(bs, fh->rfcsum); | |
869 aimbs_put32(bs, fh->nrecvd); | |
870 aimbs_put32(bs, fh->recvcsum); | |
11159 | 871 aimbs_putraw(bs, (const guchar *)fh->idstring, 32); |
4617 | 872 aimbs_put8(bs, fh->flags); |
873 aimbs_put8(bs, fh->lnameoffset); | |
874 aimbs_put8(bs, fh->lsizeoffset); | |
875 aimbs_putraw(bs, fh->dummy, 69); | |
876 aimbs_putraw(bs, fh->macfileinfo, 16); | |
877 aimbs_put16(bs, fh->nencode); | |
878 aimbs_put16(bs, fh->nlanguage); | |
11159 | 879 aimbs_putraw(bs, (const guchar *)fh->name, 64); /* XXX - filenames longer than 64B */ |
3952 | 880 |
4617 | 881 return 0; |
882 } | |
2086 | 883 |
4617 | 884 /** |
885 * Create an OFT packet based on the given information, and send it on its merry way. | |
886 * | |
887 * @param sess The session. | |
5146 | 888 * @param type The subtype of the OFT packet we're sending. |
11369 | 889 * @param oft_info The aim_oft_info struct with the connection and OFT |
5146 | 890 * info we're sending. |
4617 | 891 * @return Return 0 if no errors, otherwise return the error number. |
892 */ | |
5146 | 893 faim_export int aim_oft_sendheader(aim_session_t *sess, fu16_t type, struct aim_oft_info *oft_info) |
4617 | 894 { |
5146 | 895 aim_frame_t *fr; |
2086 | 896 |
5146 | 897 if (!sess || !oft_info || !oft_info->conn || (oft_info->conn->type != AIM_CONN_TYPE_RENDEZVOUS)) |
4617 | 898 return -EINVAL; |
899 | |
5146 | 900 #if 0 |
4617 | 901 /* |
11369 | 902 * If you are receiving a file, the cookie should be null, if you are sending a |
903 * file, the cookie should be the same as the one used in the ICBM negotiation | |
4617 | 904 * SNACs. |
905 */ | |
3952 | 906 fh->lnameoffset = 0x1a; |
907 fh->lsizeoffset = 0x10; | |
2086 | 908 |
9826 | 909 /* These should be the same as charset and charsubset in ICBMs */ |
3952 | 910 fh->nencode = 0x0000; |
911 fh->nlanguage = 0x0000; | |
5146 | 912 #endif |
4617 | 913 |
5146 | 914 aim_oft_dirconvert_tostupid(oft_info->fh.name); |
2086 | 915 |
5146 | 916 if (!(fr = aim_tx_new(sess, oft_info->conn, AIM_FRAMETYPE_OFT, type, 0))) |
917 return -ENOMEM; | |
918 | |
919 if (aim_oft_buildheader(&fr->data, &oft_info->fh) == -1) { | |
920 aim_frame_destroy(fr); | |
4617 | 921 return -ENOMEM; |
3952 | 922 } |
2086 | 923 |
5146 | 924 memcpy(fr->hdr.rend.magic, "OFT2", 4); |
8426 | 925 fr->hdr.rend.hdrlen = aim_bstream_curpos(&fr->data) + 8; |
2086 | 926 |
5146 | 927 aim_tx_enqueue(sess, fr); |
3952 | 928 |
929 return 0; | |
2086 | 930 } |
931 | |
932 /** | |
11369 | 933 * Create a rendezvous "init recv" packet and send it on its merry way. |
934 * This is the first packet sent to the proxy server by the second client | |
935 * involved in this rendezvous proxy session. | |
936 * | |
937 * @param sess The session. | |
938 * @param proxy_info Changable pieces of data for this packet | |
939 * @return Return 0 if no errors, otherwise return the error number. | |
940 */ | |
941 faim_export int aim_rv_proxy_init_recv(struct aim_rv_proxy_info *proxy_info) | |
942 { | |
943 //aim_tlvlist_t *tlvlist_sendfile; | |
944 aim_bstream_t bs; | |
945 fu8_t *bs_raw; | |
946 fu16_t packet_len; | |
947 fu8_t sn_len; | |
948 int err; | |
949 | |
950 err = 0; | |
951 | |
952 if (!proxy_info) | |
953 return -EINVAL; | |
954 | |
955 sn_len = strlen(proxy_info->sess->sn); | |
956 packet_len = 2 + 2 /* packet_len, packet_ver */ | |
957 + 2 + 4 /* cmd_type, unknownA */ | |
958 + 2 /* flags */ | |
959 + 1 + sn_len /* Length/value pair for screenname */ | |
960 + 8 /* ICBM Cookie */ | |
961 + 2 /* port */ | |
962 + 2 + 2 + 16; /* TLV for Filesend capability block */ | |
963 | |
964 if (!(bs_raw = malloc(packet_len))) | |
965 return -ENOMEM; | |
966 | |
967 aim_bstream_init(&bs, bs_raw, packet_len); | |
968 aimbs_put16(&bs, packet_len - 2); /* Length includes only packets after length marker */ | |
969 aimbs_put16(&bs, proxy_info->packet_ver); | |
970 aimbs_put16(&bs, AIM_RV_PROXY_INIT_RECV); | |
971 aimbs_put32(&bs, proxy_info->unknownA); | |
972 aimbs_put16(&bs, proxy_info->flags); | |
973 aimbs_put8(&bs, sn_len); | |
974 aimbs_putraw(&bs, proxy_info->sess->sn, sn_len); | |
975 aimbs_put16(&bs, proxy_info->port); | |
976 aimbs_putraw(&bs, proxy_info->cookie, 8); | |
977 | |
978 aimbs_put16(&bs, 0x0001); /* Type */ | |
979 aimbs_put16(&bs, 16); /* Length */ | |
980 aimbs_putcaps(&bs, AIM_CAPS_SENDFILE); /* Value */ | |
981 | |
982 // TODO: Use built-in TLV | |
983 //aim_tlvlist_add_caps(&tlvlist_sendfile, 0x0001, AIM_CAPS_SENDFILE); | |
984 //aim_tlvlist_write(&bs, &tlvlist_sendfile); | |
985 | |
986 aim_bstream_rewind(&bs); | |
987 if (aim_bstream_send(&bs, proxy_info->conn, packet_len) != packet_len) | |
988 err = errno; | |
989 proxy_info->conn->lastactivity = time(NULL); | |
990 | |
991 //aim_tlvlist_free(tlvlist_sendfile); | |
992 free(bs_raw); | |
993 | |
994 return err; | |
995 } | |
996 | |
997 | |
998 /** | |
999 * Create a rendezvous "init send" packet and send it on its merry way. | |
1000 * This is the first packet sent to the proxy server by the client | |
1001 * first indicating that this will be a proxied connection | |
1002 * | |
1003 * @param sess The session. | |
1004 * @param proxy_info Changable pieces of data for this packet | |
1005 * @return Return 0 if no errors, otherwise return the error number. | |
1006 */ | |
1007 faim_export int aim_rv_proxy_init_send(struct aim_rv_proxy_info *proxy_info) | |
1008 { | |
1009 //aim_tlvlist_t *tlvlist_sendfile; | |
1010 aim_bstream_t bs; | |
1011 fu8_t *bs_raw; | |
1012 fu16_t packet_len; | |
1013 fu8_t sn_len; | |
1014 int err; | |
1015 | |
1016 err = 0; | |
1017 | |
1018 if (!proxy_info) | |
1019 return -EINVAL; | |
1020 | |
1021 sn_len = strlen(proxy_info->sess->sn); | |
1022 packet_len = 2 + 2 /* packet_len, packet_ver */ | |
1023 + 2 + 4 /* cmd_type, unknownA */ | |
1024 + 2 /* flags */ | |
1025 + 1 + sn_len /* Length/value pair for screenname */ | |
1026 + 8 /* ICBM Cookie */ | |
1027 + 2 + 2 + 16; /* TLV for Filesend capability block */ | |
1028 | |
1029 if (!(bs_raw = malloc(packet_len))) | |
1030 return -ENOMEM; | |
1031 | |
1032 aim_bstream_init(&bs, bs_raw, packet_len); | |
1033 aimbs_put16(&bs, packet_len - 2); /* Length includes only packets after length marker */ | |
1034 aimbs_put16(&bs, proxy_info->packet_ver); | |
1035 aimbs_put16(&bs, AIM_RV_PROXY_INIT_SEND); | |
1036 aimbs_put32(&bs, proxy_info->unknownA); | |
1037 aimbs_put16(&bs, proxy_info->flags); | |
1038 aimbs_put8(&bs, sn_len); | |
1039 aimbs_putraw(&bs, proxy_info->sess->sn, sn_len); | |
1040 aimbs_putraw(&bs, proxy_info->cookie, 8); | |
1041 | |
1042 aimbs_put16(&bs, 0x0001); /* Type */ | |
1043 aimbs_put16(&bs, 16); /* Length */ | |
1044 aimbs_putcaps(&bs, AIM_CAPS_SENDFILE); /* Value */ | |
1045 | |
1046 // TODO: Use built-in TLV | |
1047 //aim_tlvlist_add_caps(&tlvlist_sendfile, 0x0001, AIM_CAPS_SENDFILE); | |
1048 //aim_tlvlist_write(&bs, &tlvlist_sendfile); | |
1049 | |
1050 aim_bstream_rewind(&bs); | |
1051 if (aim_bstream_send(&bs, proxy_info->conn, packet_len) != packet_len) | |
1052 err = errno; | |
1053 proxy_info->conn->lastactivity = time(NULL); | |
1054 | |
1055 //aim_tlvlist_free(tlvlist_sendfile); | |
1056 free(bs_raw); | |
1057 | |
1058 return err; | |
1059 } | |
1060 | |
1061 /** | |
1062 * Handle incoming data on a rendezvous connection. This is analogous to the | |
1063 * consumesnac function in rxhandlers.c, and I really think this should probably | |
4617 | 1064 * be in rxhandlers.c as well, but I haven't finished cleaning everything up yet. |
1065 * | |
1066 * @param sess The session. | |
1067 * @param fr The frame allocated for the incoming data. | |
11369 | 1068 * @return Return 0 if the packet was handled correctly, otherwise return the |
4617 | 1069 * error number. |
3771 | 1070 */ |
3952 | 1071 faim_internal int aim_rxdispatch_rendezvous(aim_session_t *sess, aim_frame_t *fr) |
2086 | 1072 { |
3952 | 1073 aim_conn_t *conn = fr->conn; |
4617 | 1074 int ret = 1; |
2086 | 1075 |
4617 | 1076 if (conn->subtype == AIM_CONN_SUBTYPE_OFT_DIRECTIM) { |
1077 if (fr->hdr.rend.type == 0x0001) | |
1078 ret = handlehdr_odc(sess, conn, fr, &fr->data); | |
1079 else | |
11253 | 1080 gaim_debug_info("oscar", "ODC directim frame unknown, type is %04x\n", fr->hdr.rend.type); |
2086 | 1081 |
4826 | 1082 } else { |
4617 | 1083 aim_rxcallback_t userfunc; |
1084 struct aim_fileheader_t *header = aim_oft_getheader(&fr->data); | |
1085 aim_oft_dirconvert_fromstupid(header->name); /* XXX - This should be client-side */ | |
3771 | 1086 |
4617 | 1087 if ((userfunc = aim_callhandler(sess, conn, AIM_CB_FAM_OFT, fr->hdr.rend.type))) |
1088 ret = userfunc(sess, fr, conn, header->bcookie, header); | |
1089 | |
1090 free(header); | |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
1091 } |
4617 | 1092 |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
1093 if (ret == -1) |
10271 | 1094 aim_conn_close(conn); |
2086 | 1095 |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
1096 return ret; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
1097 } |
11369 | 1098 |
1099 /** | |
1100 * Handle incoming data on a rendezvous proxy connection. This is similar to | |
1101 * aim_rxdispatch_rendezvous above and should probably be kept with that function. | |
1102 * | |
1103 * @param sess The session. | |
1104 * @param fr The frame allocated for the incoming data. | |
1105 * @return Return 0 if the packet was handled correctly, otherwise return the | |
1106 * error number. | |
1107 */ | |
1108 faim_internal struct aim_rv_proxy_info *aim_rv_proxy_read(aim_session_t *sess, aim_conn_t *conn) | |
1109 { | |
1110 aim_bstream_t bs_hdr; | |
1111 fu8_t hdr_buf[AIM_RV_PROXY_HDR_LEN]; | |
1112 aim_bstream_t bs_body; /* The body (everything but the header) of the packet */ | |
1113 fu8_t *body_buf = NULL; | |
1114 fu8_t body_len; | |
1115 | |
1116 char str_ip[30] = {""}; | |
1117 fu8_t ip_temp[4]; | |
1118 | |
1119 fu16_t len; | |
1120 struct aim_rv_proxy_info *proxy_info; | |
1121 | |
1122 if(!(proxy_info = malloc(sizeof(struct aim_rv_proxy_info)))) | |
1123 return NULL; | |
1124 | |
1125 aim_bstream_init(&bs_hdr, hdr_buf, AIM_RV_PROXY_HDR_LEN); | |
1126 if (aim_bstream_recv(&bs_hdr, conn->fd, AIM_RV_PROXY_HDR_LEN) == AIM_RV_PROXY_HDR_LEN) { | |
1127 aim_bstream_rewind(&bs_hdr); | |
1128 len = aimbs_get16(&bs_hdr); | |
1129 proxy_info->packet_ver = aimbs_get16(&bs_hdr); | |
1130 proxy_info->cmd_type = aimbs_get16(&bs_hdr); | |
1131 proxy_info->unknownA = aimbs_get32(&bs_hdr); | |
1132 proxy_info->flags = aimbs_get16(&bs_hdr); | |
1133 if(proxy_info->cmd_type == AIM_RV_PROXY_READY) { | |
1134 /* Do a little victory dance | |
1135 * A ready packet contains no additional information */ | |
1136 } else if(proxy_info->cmd_type == AIM_RV_PROXY_ERROR) { | |
1137 if(len == AIM_RV_PROXY_ERROR_LEN - 2) { | |
1138 body_len = AIM_RV_PROXY_ERROR_LEN - AIM_RV_PROXY_HDR_LEN; | |
1139 body_buf = malloc(body_len); | |
1140 aim_bstream_init(&bs_body, body_buf, body_len); | |
1141 if (aim_bstream_recv(&bs_body, conn->fd, body_len) == body_len) { | |
1142 aim_bstream_rewind(&bs_body); | |
1143 proxy_info->err_code = aimbs_get16(&bs_body); | |
1144 } else { | |
1145 gaim_debug_warning("oscar","error reading rv proxy error packet\n"); | |
1146 aim_conn_close(conn); | |
1147 free(proxy_info); | |
1148 proxy_info = NULL; | |
1149 } | |
1150 } else { | |
1151 gaim_debug_warning("oscar","invalid length for proxy error packet\n"); | |
1152 free(proxy_info); | |
1153 proxy_info = NULL; | |
1154 } | |
1155 } else if(proxy_info->cmd_type == AIM_RV_PROXY_ACK) { | |
1156 if(len == AIM_RV_PROXY_ACK_LEN - 2) { | |
1157 body_len = AIM_RV_PROXY_ACK_LEN - AIM_RV_PROXY_HDR_LEN; | |
1158 body_buf = malloc(body_len); | |
1159 aim_bstream_init(&bs_body, body_buf, body_len); | |
1160 if (aim_bstream_recv(&bs_body, conn->fd, body_len) == body_len) { | |
1161 aim_bstream_rewind(&bs_body); | |
1162 proxy_info->port = aimbs_get16(&bs_body); | |
1163 int i; | |
1164 for(i=0; i<4; i++) | |
1165 ip_temp[i] = aimbs_get8(&bs_body); | |
1166 snprintf(str_ip, sizeof(str_ip), "%hhu.%hhu.%hhu.%hhu", | |
1167 ip_temp[0], ip_temp[1], | |
1168 ip_temp[2], ip_temp[3]); | |
1169 proxy_info->ip = strdup(str_ip); | |
1170 } else { | |
1171 gaim_debug_warning("oscar","error reading rv proxy error packet\n"); | |
1172 aim_conn_close(conn); | |
1173 free(proxy_info); | |
1174 proxy_info = NULL; | |
1175 } | |
1176 } else { | |
1177 gaim_debug_warning("oscar","invalid length for proxy error packet\n"); | |
1178 free(proxy_info); | |
1179 proxy_info = NULL; | |
1180 } | |
1181 } else { | |
1182 gaim_debug_warning("oscar","unknown type for aim rendezvous proxy packet\n"); | |
1183 } | |
1184 } else { | |
1185 gaim_debug_warning("oscar","error reading header of rv proxy packet\n"); | |
1186 aim_conn_close(conn); | |
1187 free(proxy_info); | |
1188 proxy_info = NULL; | |
1189 } | |
1190 if(body_buf) { | |
1191 free(body_buf); | |
1192 body_buf = NULL; | |
1193 } | |
1194 return proxy_info; | |
1195 } |