comparison src/protocols/oscar/oft.c @ 13592:6519aeb66b31

[gaim-migrate @ 15978] Holy cow this is crazy. 34 files changed, 5760 insertions(+), 8517 deletions(-) * Non-blocking I/O for all of oscar. That includes normal FLAP connections as well as file transfers and direct IM. * Kick-ass file transfer and direct IM. Either party can request the connection. Gaim will try both the "public" IP and the "client" IP. It'll fall back to transferring through a proxy if that fails. Should be relatively few memleaks (I didn't have a lot of confidence in the non-memleakiness of the old code). And the code is reasonably generic, so it shouldn't be too much work to add voice chat. This might still be a LITTLE buggy, but it shouldn't be too bad. If anything, file transfer will be more buggy than direct IM. And sending a file will be more buggy than receiving a file. Bug reports with a series of steps to reproduce are welcome. * I merged OscarData and aim_session_t * Somewhere between 50 and 100 hours of work. committer: Tailor Script <tailor@pidgin.im>
author Mark Doliner <mark@kingant.net>
date Fri, 07 Apr 2006 05:10:56 +0000
parents
children a79d422bfe62
comparison
equal deleted inserted replaced
13591:dcfda39ad547 13592:6519aeb66b31
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 * I feel like this is a good place to explain OFT, so I'm going to
23 * do just that. Each OFT packet has a header type. I guess this
24 * is pretty similar to the subtype of a SNAC packet. The type
25 * basically tells the other client the meaning of the OFT packet.
26 * There are two distinct types of file transfer, which I usually
27 * call "sendfile" and "getfile." Sendfile is when you send a file
28 * to another AIM user. Getfile is when you share a group of files,
29 * and other users request that you send them the files.
30 *
31 * A typical sendfile file transfer goes like this:
32 * 1) Sender sends a channel 2 ICBM telling the other user that
33 * we want to send them a file. At the same time, we open a
34 * listener socket (this should be done before sending the
35 * ICBM) on some port, and wait for them to connect to us.
36 * The ICBM we sent should contain our IP address and the port
37 * number that we're listening on.
38 * 2) The receiver connects to the sender on the given IP address
39 * and port. After the connection is established, the receiver
40 * sends an ICBM signifying that we are ready and waiting.
41 * 3) The sender sends an OFT PROMPT message over the OFT
42 * connection.
43 * 4) The receiver of the file sends back an exact copy of this
44 * OFT packet, except the cookie is filled in with the cookie
45 * from the ICBM. I think this might be an attempt to verify
46 * that the user that is connected is actually the guy that
47 * we sent the ICBM to. Oh, I've been calling this the ACK.
48 * 5) The sender starts sending raw data across the connection
49 * until the entire file has been sent.
50 * 6) The receiver knows the file is finished because the sender
51 * sent the file size in an earlier OFT packet. So then the
52 * receiver sends the DONE thingy (after filling in the
53 * "received" checksum and size) and closes the connection.
54 */
55
56 #include "oscar.h"
57 #include "peer.h"
58
59 /**
60 * Calculate oft checksum of buffer
61 *
62 * Prevcheck should be 0xFFFF0000 when starting a checksum of a file. The
63 * checksum is kind of a rolling checksum thing, so each time you get bytes
64 * of a file you just call this puppy and it updates the checksum. You can
65 * calculate the checksum of an entire file by calling this in a while or a
66 * for loop, or something.
67 *
68 * Thanks to Graham Booker for providing this improved checksum routine,
69 * which is simpler and should be more accurate than Josh Myer's original
70 * code. -- wtm
71 *
72 * This algorithm works every time I have tried it. The other fails
73 * sometimes. So, AOL who thought this up? It has got to be the weirdest
74 * checksum I have ever seen.
75 *
76 * @param buffer Buffer of data to checksum. Man I'd like to buff her...
77 * @param bufsize Size of buffer.
78 * @param prevchecksum Previous checksum.
79 */
80 static guint32
81 peer_oft_checksum_chunk(const guint8 *buffer, int bufferlen, guint32 prevchecksum)
82 {
83 guint32 checksum, oldchecksum;
84 int i;
85 unsigned short val;
86
87 checksum = (prevchecksum >> 16) & 0xffff;
88 for (i = 0; i < bufferlen; i++)
89 {
90 oldchecksum = checksum;
91 if (i & 1)
92 val = buffer[i];
93 else
94 val = buffer[i] << 8;
95 checksum -= val;
96 /*
97 * The following appears to be necessary.... It happens
98 * every once in a while and the checksum doesn't fail.
99 */
100 if (checksum > oldchecksum)
101 checksum--;
102 }
103 checksum = ((checksum & 0x0000ffff) + (checksum >> 16));
104 checksum = ((checksum & 0x0000ffff) + (checksum >> 16));
105 return checksum << 16;
106 }
107
108 static guint32
109 peer_oft_checksum_file(char *filename)
110 {
111 FILE *fd;
112 guint32 checksum = 0xffff0000;
113
114 if ((fd = fopen(filename, "rb")))
115 {
116 int bytes;
117 guint8 buffer[1024];
118
119 while ((bytes = fread(buffer, 1, 1024, fd)) != 0)
120 checksum = peer_oft_checksum_chunk(buffer, bytes, checksum);
121 fclose(fd);
122 }
123
124 return checksum;
125 }
126
127 /**
128 * Free any OFT related data.
129 */
130 void
131 peer_oft_close(PeerConnection *conn)
132 {
133 /*
134 * If canceled by local user, and we're receiving a file, and
135 * we're not connected/ready then send an ICBM cancel message.
136 */
137 if ((gaim_xfer_get_status(conn->xfer) == GAIM_XFER_STATUS_CANCEL_LOCAL) &&
138 !conn->ready)
139 {
140 aim_im_sendch2_cancel(conn);
141 }
142
143 if (conn->sending_data_timer != 0)
144 {
145 gaim_timeout_remove(conn->sending_data_timer);
146 conn->sending_data_timer = 0;
147 }
148 }
149
150 /**
151 * Write the given OftFrame to a ByteStream and send it out
152 * on the established PeerConnection.
153 */
154 static void
155 peer_oft_send(PeerConnection *conn, OftFrame *frame)
156 {
157 size_t length;
158 ByteStream bs;
159
160 length = 192 + MAX(64, frame->name_length + 1);
161 byte_stream_init(&bs, malloc(length), length);
162 byte_stream_putraw(&bs, conn->magic, 4);
163 byte_stream_put16(&bs, length);
164 byte_stream_put16(&bs, frame->type);
165 byte_stream_putraw(&bs, frame->cookie, 8);
166 byte_stream_put16(&bs, frame->encrypt);
167 byte_stream_put16(&bs, frame->compress);
168 byte_stream_put16(&bs, frame->totfiles);
169 byte_stream_put16(&bs, frame->filesleft);
170 byte_stream_put16(&bs, frame->totparts);
171 byte_stream_put16(&bs, frame->partsleft);
172 byte_stream_put32(&bs, frame->totsize);
173 byte_stream_put32(&bs, frame->size);
174 byte_stream_put32(&bs, frame->modtime);
175 byte_stream_put32(&bs, frame->checksum);
176 byte_stream_put32(&bs, frame->rfrcsum);
177 byte_stream_put32(&bs, frame->rfsize);
178 byte_stream_put32(&bs, frame->cretime);
179 byte_stream_put32(&bs, frame->rfcsum);
180 byte_stream_put32(&bs, frame->nrecvd);
181 byte_stream_put32(&bs, frame->recvcsum);
182 byte_stream_putraw(&bs, frame->idstring, 32);
183 byte_stream_put8(&bs, frame->flags);
184 byte_stream_put8(&bs, frame->lnameoffset);
185 byte_stream_put8(&bs, frame->lsizeoffset);
186 byte_stream_putraw(&bs, frame->dummy, 69);
187 byte_stream_putraw(&bs, frame->macfileinfo, 16);
188 byte_stream_put16(&bs, frame->nencode);
189 byte_stream_put16(&bs, frame->nlanguage);
190 /*
191 * The name can be more than 64 characters, but if it is less than
192 * 64 characters it is padded with NULLs.
193 */
194 byte_stream_putraw(&bs, frame->name, MAX(64, frame->name_length + 1));
195
196 peer_connection_send(conn, &bs);
197
198 free(bs.data);
199 }
200
201 void
202 peer_oft_send_prompt(PeerConnection *conn)
203 {
204 conn->xferdata.type = PEER_TYPE_PROMPT;
205 peer_oft_send(conn, &conn->xferdata);
206 }
207
208 static void
209 peer_oft_send_ack(PeerConnection *conn)
210 {
211 conn->xferdata.type = PEER_TYPE_ACK;
212
213 /* Fill in the cookie */
214 memcpy(conn->xferdata.cookie, conn->cookie, 8);
215
216 peer_oft_send(conn, &conn->xferdata);
217 }
218
219 static void
220 peer_oft_send_done(PeerConnection *conn)
221 {
222 conn->xferdata.type = PEER_TYPE_DONE;
223 conn->xferdata.filesleft = 0;
224 conn->xferdata.partsleft = 0;
225 conn->xferdata.nrecvd = gaim_xfer_get_bytes_sent(conn->xfer);
226 peer_oft_send(conn, &conn->xferdata);
227 }
228
229 /**
230 * This function exists so that we don't remove the outgoing
231 * data watcher while we're still sending data. In most cases
232 * any data we're sending will be instantly wisked away to a TCP
233 * buffer maintained by our operating system... but we want to
234 * make sure the core doesn't start sending file data while
235 * we're still sending OFT frame data. That would be bad.
236 */
237 static gboolean
238 start_transfer_when_done_sending_data(gpointer data)
239 {
240 PeerConnection *conn;
241
242 conn = data;
243
244 if (gaim_circ_buffer_get_max_read(conn->buffer_outgoing) == 0)
245 {
246 conn->sending_data_timer = 0;
247 gaim_input_remove(conn->watcher_incoming);
248 conn->watcher_incoming = 0;
249 conn->xfer->fd = conn->fd;
250 conn->fd = -1;
251 gaim_xfer_start(conn->xfer, conn->xfer->fd, NULL, 0);
252 return FALSE;
253 }
254
255 return TRUE;
256 }
257
258 /**
259 * This function is similar to the above function, except instead
260 * of starting the xfer it will destroy the connection. This is
261 * used when you want to send one final message across the peer
262 * connection, and then close everything.
263 */
264 static gboolean
265 destroy_connection_when_done_sending_data(gpointer data)
266 {
267 PeerConnection *conn;
268
269 conn = data;
270
271 if (gaim_circ_buffer_get_max_read(conn->buffer_outgoing) == 0)
272 {
273 conn->sending_data_timer = 0;
274 peer_connection_destroy(conn, conn->disconnect_reason);
275 return FALSE;
276 }
277
278 return TRUE;
279 }
280
281 /*
282 * This is called when a buddy sends us some file info. This happens when they
283 * are sending a file to you, and you have just established a connection to them.
284 * You should send them the exact same info except use the real cookie. We also
285 * get like totally ready to like, receive the file, kay?
286 */
287 static void
288 peer_oft_recv_frame_prompt(PeerConnection *conn, OftFrame *frame)
289 {
290 /* Record the file information and send an ack */
291 memcpy(&conn->xferdata, frame, sizeof(OftFrame));
292 peer_oft_send_ack(conn);
293
294 /* Remove our watchers and use the file transfer watchers in the core */
295 gaim_input_remove(conn->watcher_incoming);
296 conn->watcher_incoming = 0;
297 conn->sending_data_timer = gaim_timeout_add(100,
298 start_transfer_when_done_sending_data, conn);
299 }
300
301 /**
302 * We are sending a file to someone else. They have just acknowledged our
303 * prompt, so we want to start sending data like there's no tomorrow.
304 */
305 static void
306 peer_oft_recv_frame_ack(PeerConnection *conn, OftFrame *frame)
307 {
308 if (memcmp(conn->cookie, frame->cookie, 8))
309 {
310 gaim_debug_info("oscar", "Received an incorrect cookie. "
311 "Closing connection.\n");
312 peer_connection_destroy(conn, PEER_DISCONNECT_INVALID_DATA);
313 return;
314 }
315
316 /* Remove our watchers and use the file transfer watchers in the core */
317 gaim_input_remove(conn->watcher_incoming);
318 conn->watcher_incoming = 0;
319 conn->sending_data_timer = gaim_timeout_add(100,
320 start_transfer_when_done_sending_data, conn);
321 }
322
323 /*
324 * We just sent a file to someone. They said they got it and everything,
325 * so we can close our direct connection and what not.
326 */
327 static void
328 peer_oft_recv_frame_done(PeerConnection *conn, OftFrame *frame)
329 {
330 if (frame->nrecvd == frame->size)
331 gaim_xfer_set_completed(conn->xfer, TRUE);
332
333 gaim_input_remove(conn->watcher_incoming);
334 conn->watcher_incoming = 0;
335 conn->xfer->fd = conn->fd;
336 conn->fd = -1;
337 gaim_xfer_end(conn->xfer);
338 }
339
340 /**
341 * Handle an incoming OftFrame. If there is a payload associated
342 * with this frame, then we remove the old watcher and add the
343 * OFT watcher to read in the payload.
344 */
345 void
346 peer_oft_recv_frame(PeerConnection *conn, ByteStream *bs)
347 {
348 OftFrame frame;
349
350 frame.type = byte_stream_get16(bs);
351 byte_stream_getrawbuf(bs, frame.cookie, 8);
352 frame.encrypt = byte_stream_get16(bs);
353 frame.compress = byte_stream_get16(bs);
354 frame.totfiles = byte_stream_get16(bs);
355 frame.filesleft = byte_stream_get16(bs);
356 frame.totparts = byte_stream_get16(bs);
357 frame.partsleft = byte_stream_get16(bs);
358 frame.totsize = byte_stream_get32(bs);
359 frame.size = byte_stream_get32(bs);
360 frame.modtime = byte_stream_get32(bs);
361 frame.checksum = byte_stream_get32(bs);
362 frame.rfrcsum = byte_stream_get32(bs);
363 frame.rfsize = byte_stream_get32(bs);
364 frame.cretime = byte_stream_get32(bs);
365 frame.rfcsum = byte_stream_get32(bs);
366 frame.nrecvd = byte_stream_get32(bs);
367 frame.recvcsum = byte_stream_get32(bs);
368 byte_stream_getrawbuf(bs, frame.idstring, 32);
369 frame.flags = byte_stream_get8(bs);
370 frame.lnameoffset = byte_stream_get8(bs);
371 frame.lsizeoffset = byte_stream_get8(bs);
372 byte_stream_getrawbuf(bs, frame.dummy, 69);
373 byte_stream_getrawbuf(bs, frame.macfileinfo, 16);
374 frame.nencode = byte_stream_get16(bs);
375 frame.nlanguage = byte_stream_get16(bs);
376 frame.name_length = bs->len - 186;
377 frame.name = byte_stream_getraw(bs, frame.name_length);
378
379 gaim_debug_info("oscar", "Incoming OFT frame from %s with "
380 "type=0x%04x\n", conn->sn, frame.type);
381
382 /* TODOFT: peer_oft_dirconvert_fromstupid(frame->name); */
383
384 if (frame.type == PEER_TYPE_PROMPT)
385 peer_oft_recv_frame_prompt(conn, &frame);
386 else if (frame.type == PEER_TYPE_ACK)
387 peer_oft_recv_frame_ack(conn, &frame);
388 else if (frame.type == PEER_TYPE_DONE)
389 peer_oft_recv_frame_done(conn, &frame);
390
391 free(frame.name);
392 }
393
394 /*******************************************************************/
395 /* Begin GaimXfer callbacks for use when receiving a file */
396 /*******************************************************************/
397
398 void
399 peer_oft_recvcb_init(GaimXfer *xfer)
400 {
401 PeerConnection *conn;
402
403 conn = xfer->data;
404 conn->flags |= PEER_CONNECTION_FLAG_APPROVED;
405 peer_connection_trynext(conn);
406 }
407
408 void
409 peer_oft_recvcb_end(GaimXfer *xfer)
410 {
411 PeerConnection *conn;
412
413 conn = xfer->data;
414
415 /* Tell the other person that we've received everything */
416 conn->fd = conn->xfer->fd;
417 conn->xfer->fd = -1;
418 peer_oft_send_done(conn);
419
420 conn->disconnect_reason = PEER_DISCONNECT_DONE;
421 conn->sending_data_timer = gaim_timeout_add(100,
422 destroy_connection_when_done_sending_data, conn);
423 }
424
425 void
426 peer_oft_recvcb_ack_recv(GaimXfer *xfer, const guchar *buffer, size_t size)
427 {
428 PeerConnection *conn;
429
430 /* Update our rolling checksum. Like Walmart, yo. */
431 conn = xfer->data;
432 conn->xferdata.recvcsum = peer_oft_checksum_chunk(buffer,
433 size, conn->xferdata.recvcsum);
434 }
435
436 /*******************************************************************/
437 /* End GaimXfer callbacks for use when receiving a file */
438 /*******************************************************************/
439
440 /*******************************************************************/
441 /* Begin GaimXfer callbacks for use when sending a file */
442 /*******************************************************************/
443
444 void
445 peer_oft_sendcb_init(GaimXfer *xfer)
446 {
447 PeerConnection *conn;
448
449 conn = xfer->data;
450 conn->flags |= PEER_CONNECTION_FLAG_APPROVED;
451
452 /* Keep track of file transfer info */
453 conn->xferdata.totfiles = 1;
454 conn->xferdata.filesleft = 1;
455 conn->xferdata.totparts = 1;
456 conn->xferdata.partsleft = 1;
457 conn->xferdata.totsize = gaim_xfer_get_size(xfer);
458 conn->xferdata.size = gaim_xfer_get_size(xfer);
459 conn->xferdata.checksum = 0xffff0000;
460 conn->xferdata.rfrcsum = 0xffff0000;
461 conn->xferdata.rfcsum = 0xffff0000;
462 conn->xferdata.recvcsum = 0xffff0000;
463 strncpy((gchar *)conn->xferdata.idstring, "OFT_Windows ICBMFT V1.1 32", 31);
464 conn->xferdata.modtime = 0;
465 conn->xferdata.cretime = 0;
466 xfer->filename = g_path_get_basename(xfer->local_filename);
467 conn->xferdata.name = (guchar *)g_strdup(xfer->filename);
468 conn->xferdata.name_length = strlen(xfer->filename);
469
470 /* Calculating the checksum can take a very long time for large files */
471 gaim_debug_info("oscar","calculating file checksum\n");
472 conn->xferdata.checksum = peer_oft_checksum_file(xfer->local_filename);
473 gaim_debug_info("oscar","checksum calculated\n");
474
475 /* Start the connection process */
476 peer_connection_trynext(conn);
477 }
478
479 /*
480 * AIM file transfers aren't really meant to be thought
481 * of as a transferring just a single file. The rendezvous
482 * establishes a connection between two computers, and then
483 * those computers can use the same connection for transferring
484 * multiple files. So we don't want the Gaim core up and closing
485 * the socket all willy-nilly. We want to do that in the oscar
486 * prpl, whenever one side or the other says they're finished
487 * using the connection. There might be a better way to intercept
488 * the socket from the core...
489 */
490 void
491 peer_oft_sendcb_ack(GaimXfer *xfer, const guchar *buffer, size_t size)
492 {
493 PeerConnection *conn;
494
495 conn = xfer->data;
496
497 /*
498 * If we're done sending, intercept the socket from the core ft code
499 * and wait for the other guy to send the "done" OFT packet.
500 */
501 if (gaim_xfer_get_bytes_remaining(xfer) <= 0)
502 {
503 gaim_input_remove(xfer->watcher);
504 conn->fd = xfer->fd;
505 xfer->fd = -1;
506 conn->watcher_incoming = gaim_input_add(conn->fd,
507 GAIM_INPUT_READ, peer_connection_recv_cb, conn);
508 }
509 }
510
511 /*******************************************************************/
512 /* End GaimXfer callbacks for use when sending a file */
513 /*******************************************************************/
514
515 /*******************************************************************/
516 /* Begin GaimXfer callbacks for use when sending and receiving */
517 /*******************************************************************/
518
519 void
520 peer_oft_cb_generic_cancel(GaimXfer *xfer)
521 {
522 PeerConnection *conn;
523
524 conn = xfer->data;
525
526 if (conn == NULL)
527 return;
528
529 peer_connection_destroy(conn, PEER_DISCONNECT_LOCAL_CLOSED);
530 }
531
532 /*******************************************************************/
533 /* End GaimXfer callbacks for use when sending and receiving */
534 /*******************************************************************/
535
536 #if 0
537 /*
538 * This little area in oscar.c is the nexus of file transfer code,
539 * so I wrote a little explanation of what happens. I am such a
540 * ninja.
541 *
542 * The series of events for a file send is:
543 * -Create xfer and call gaim_xfer_request (this happens in oscar_ask_sendfile)
544 * -User chooses a file and oscar_xfer_init is called. It establishes a
545 * listening socket, then asks the remote user to connect to us (and
546 * gives them the file name, port, IP, etc.)
547 * -They connect to us and we send them an PEER_TYPE_PROMPT (this happens
548 * in peer_oft_recv_frame_established)
549 * -They send us an PEER_TYPE_ACK and then we start sending data
550 * -When we finish, they send us an PEER_TYPE_DONE and they close the
551 * connection.
552 * -We get drunk because file transfer kicks ass.
553 *
554 * The series of events for a file receive is:
555 * -Create xfer and call gaim_xfer request (this happens in incomingim_chan2)
556 * -Gaim user selects file to name and location to save file to and
557 * oscar_xfer_init is called
558 * -It connects to the remote user using the IP they gave us earlier
559 * -After connecting, they send us an PEER_TYPE_PROMPT. In reply, we send
560 * them an PEER_TYPE_ACK.
561 * -They begin to send us lots of raw data.
562 * -When they finish sending data we send an PEER_TYPE_DONE and then close
563 * the connection.
564 *
565 * Update August 2005:
566 * The series of events for transfers has been seriously complicated by the addition
567 * of transfer redirects and proxied connections. I could throw a whole lot of words
568 * at trying to explain things here, but it probably wouldn't do much good. To get
569 * a better idea of what happens, take a look at the diagrams and documentation
570 * from my Summer of Code project. -- Jonathan Clark
571 */
572
573 /**
574 * Convert the directory separator from / (0x2f) to ^A (0x01)
575 *
576 * @param name The filename to convert.
577 */
578 static void
579 peer_oft_dirconvert_tostupid(char *name)
580 {
581 while (name[0]) {
582 if (name[0] == 0x01)
583 name[0] = G_DIR_SEPARATOR;
584 name++;
585 }
586 }
587
588 /**
589 * Convert the directory separator from ^A (0x01) to / (0x2f)
590 *
591 * @param name The filename to convert.
592 */
593 static void
594 peer_oft_dirconvert_fromstupid(char *name)
595 {
596 while (name[0]) {
597 if (name[0] == G_DIR_SEPARATOR)
598 name[0] = 0x01;
599 name++;
600 }
601 }
602 #endif