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