Mercurial > pidgin
annotate libgaim/protocols/oscar/oft.c @ 15277:623affd9a13e
[gaim-migrate @ 18067]
Patch from Graham Booker which fixes a crash after a successful OSCAR file transfer. gaim_xfer_end() is called by ft.c's transfer_cb() when the transfer completes so does not need to be called here; peer_connection_schedule_destroy() should be called instead.
committer: Tailor Script <tailor@pidgin.im>
author | Evan Schoenberg <evan.s@dreskin.net> |
---|---|
date | Wed, 03 Jan 2007 14:02:44 +0000 |
parents | 5176a9f30ba3 |
children | 0e8f0468a809 |
rev | line source |
---|---|
14192 | 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 | |
14538 | 59 #include "util.h" |
60 | |
14192 | 61 /** |
62 * Calculate oft checksum of buffer | |
63 * | |
64 * Prevcheck should be 0xFFFF0000 when starting a checksum of a file. The | |
65 * checksum is kind of a rolling checksum thing, so each time you get bytes | |
66 * of a file you just call this puppy and it updates the checksum. You can | |
67 * calculate the checksum of an entire file by calling this in a while or a | |
68 * for loop, or something. | |
69 * | |
70 * Thanks to Graham Booker for providing this improved checksum routine, | |
71 * which is simpler and should be more accurate than Josh Myer's original | |
72 * code. -- wtm | |
73 * | |
74 * This algorithm works every time I have tried it. The other fails | |
75 * sometimes. So, AOL who thought this up? It has got to be the weirdest | |
76 * checksum I have ever seen. | |
77 * | |
78 * @param buffer Buffer of data to checksum. Man I'd like to buff her... | |
79 * @param bufsize Size of buffer. | |
80 * @param prevchecksum Previous checksum. | |
81 */ | |
82 static guint32 | |
83 peer_oft_checksum_chunk(const guint8 *buffer, int bufferlen, guint32 prevchecksum) | |
84 { | |
85 guint32 checksum, oldchecksum; | |
86 int i; | |
87 unsigned short val; | |
88 | |
89 checksum = (prevchecksum >> 16) & 0xffff; | |
90 for (i = 0; i < bufferlen; i++) | |
91 { | |
92 oldchecksum = checksum; | |
93 if (i & 1) | |
94 val = buffer[i]; | |
95 else | |
96 val = buffer[i] << 8; | |
97 checksum -= val; | |
98 /* | |
99 * The following appears to be necessary.... It happens | |
100 * every once in a while and the checksum doesn't fail. | |
101 */ | |
102 if (checksum > oldchecksum) | |
103 checksum--; | |
104 } | |
105 checksum = ((checksum & 0x0000ffff) + (checksum >> 16)); | |
106 checksum = ((checksum & 0x0000ffff) + (checksum >> 16)); | |
107 return checksum << 16; | |
108 } | |
109 | |
110 static guint32 | |
111 peer_oft_checksum_file(char *filename) | |
112 { | |
113 FILE *fd; | |
114 guint32 checksum = 0xffff0000; | |
115 | |
116 if ((fd = fopen(filename, "rb"))) | |
117 { | |
118 int bytes; | |
15260 | 119 guint8 *buffer = g_malloc(65536); |
14192 | 120 |
15260 | 121 while ((bytes = fread(buffer, 1, 65536, fd)) != 0) |
14192 | 122 checksum = peer_oft_checksum_chunk(buffer, bytes, checksum); |
15260 | 123 g_free(buffer); |
14192 | 124 fclose(fd); |
125 } | |
126 | |
127 return checksum; | |
128 } | |
129 | |
15260 | 130 static void |
131 peer_oft_copy_xfer_data(PeerConnection *conn, OftFrame *frame) | |
132 { | |
133 g_free(conn->xferdata.name); | |
134 | |
135 memcpy(&(conn->xferdata), frame, sizeof(OftFrame)); | |
136 conn->xferdata.name = g_memdup(frame->name, frame->name_length); | |
137 } | |
138 | |
14192 | 139 /** |
140 * Free any OFT related data. | |
141 */ | |
142 void | |
143 peer_oft_close(PeerConnection *conn) | |
144 { | |
145 /* | |
146 * If canceled by local user, and we're receiving a file, and | |
147 * we're not connected/ready then send an ICBM cancel message. | |
148 */ | |
149 if ((gaim_xfer_get_status(conn->xfer) == GAIM_XFER_STATUS_CANCEL_LOCAL) && | |
150 !conn->ready) | |
151 { | |
152 aim_im_sendch2_cancel(conn); | |
153 } | |
154 | |
155 if (conn->sending_data_timer != 0) | |
156 { | |
157 gaim_timeout_remove(conn->sending_data_timer); | |
158 conn->sending_data_timer = 0; | |
159 } | |
160 } | |
161 | |
162 /** | |
163 * Write the given OftFrame to a ByteStream and send it out | |
164 * on the established PeerConnection. | |
165 */ | |
166 static void | |
167 peer_oft_send(PeerConnection *conn, OftFrame *frame) | |
168 { | |
169 size_t length; | |
170 ByteStream bs; | |
171 | |
172 length = 192 + MAX(64, frame->name_length + 1); | |
15089 | 173 byte_stream_new(&bs, length); |
14192 | 174 byte_stream_putraw(&bs, conn->magic, 4); |
175 byte_stream_put16(&bs, length); | |
176 byte_stream_put16(&bs, frame->type); | |
177 byte_stream_putraw(&bs, frame->cookie, 8); | |
178 byte_stream_put16(&bs, frame->encrypt); | |
179 byte_stream_put16(&bs, frame->compress); | |
180 byte_stream_put16(&bs, frame->totfiles); | |
181 byte_stream_put16(&bs, frame->filesleft); | |
182 byte_stream_put16(&bs, frame->totparts); | |
183 byte_stream_put16(&bs, frame->partsleft); | |
184 byte_stream_put32(&bs, frame->totsize); | |
185 byte_stream_put32(&bs, frame->size); | |
186 byte_stream_put32(&bs, frame->modtime); | |
187 byte_stream_put32(&bs, frame->checksum); | |
188 byte_stream_put32(&bs, frame->rfrcsum); | |
189 byte_stream_put32(&bs, frame->rfsize); | |
190 byte_stream_put32(&bs, frame->cretime); | |
191 byte_stream_put32(&bs, frame->rfcsum); | |
192 byte_stream_put32(&bs, frame->nrecvd); | |
193 byte_stream_put32(&bs, frame->recvcsum); | |
194 byte_stream_putraw(&bs, frame->idstring, 32); | |
195 byte_stream_put8(&bs, frame->flags); | |
196 byte_stream_put8(&bs, frame->lnameoffset); | |
197 byte_stream_put8(&bs, frame->lsizeoffset); | |
198 byte_stream_putraw(&bs, frame->dummy, 69); | |
199 byte_stream_putraw(&bs, frame->macfileinfo, 16); | |
200 byte_stream_put16(&bs, frame->nencode); | |
201 byte_stream_put16(&bs, frame->nlanguage); | |
202 /* | |
203 * The name can be more than 64 characters, but if it is less than | |
204 * 64 characters it is padded with NULLs. | |
205 */ | |
206 byte_stream_putraw(&bs, frame->name, MAX(64, frame->name_length + 1)); | |
207 | |
208 peer_connection_send(conn, &bs); | |
209 | |
15089 | 210 g_free(bs.data); |
14192 | 211 } |
212 | |
213 void | |
214 peer_oft_send_prompt(PeerConnection *conn) | |
215 { | |
216 conn->xferdata.type = PEER_TYPE_PROMPT; | |
217 peer_oft_send(conn, &conn->xferdata); | |
218 } | |
219 | |
220 static void | |
221 peer_oft_send_ack(PeerConnection *conn) | |
222 { | |
223 conn->xferdata.type = PEER_TYPE_ACK; | |
224 | |
225 /* Fill in the cookie */ | |
226 memcpy(conn->xferdata.cookie, conn->cookie, 8); | |
227 | |
228 peer_oft_send(conn, &conn->xferdata); | |
229 } | |
230 | |
231 static void | |
15260 | 232 peer_oft_send_resume_accept(PeerConnection *conn) |
233 { | |
234 conn->xferdata.type = PEER_TYPE_RESUMEACCEPT; | |
235 | |
236 /* Fill in the cookie */ | |
237 memcpy(conn->xferdata.cookie, conn->cookie, 8); | |
238 | |
239 peer_oft_send(conn, &conn->xferdata); | |
240 } | |
241 | |
242 static void | |
14192 | 243 peer_oft_send_done(PeerConnection *conn) |
244 { | |
245 conn->xferdata.type = PEER_TYPE_DONE; | |
246 conn->xferdata.filesleft = 0; | |
247 conn->xferdata.partsleft = 0; | |
248 conn->xferdata.nrecvd = gaim_xfer_get_bytes_sent(conn->xfer); | |
249 peer_oft_send(conn, &conn->xferdata); | |
250 } | |
251 | |
252 /** | |
253 * This function exists so that we don't remove the outgoing | |
254 * data watcher while we're still sending data. In most cases | |
255 * any data we're sending will be instantly wisked away to a TCP | |
256 * buffer maintained by our operating system... but we want to | |
257 * make sure the core doesn't start sending file data while | |
258 * we're still sending OFT frame data. That would be bad. | |
259 */ | |
260 static gboolean | |
261 start_transfer_when_done_sending_data(gpointer data) | |
262 { | |
263 PeerConnection *conn; | |
264 | |
265 conn = data; | |
266 | |
267 if (gaim_circ_buffer_get_max_read(conn->buffer_outgoing) == 0) | |
268 { | |
269 conn->sending_data_timer = 0; | |
270 conn->xfer->fd = conn->fd; | |
271 conn->fd = -1; | |
272 gaim_xfer_start(conn->xfer, conn->xfer->fd, NULL, 0); | |
273 return FALSE; | |
274 } | |
275 | |
276 return TRUE; | |
277 } | |
278 | |
279 /** | |
280 * This function is similar to the above function, except instead | |
281 * of starting the xfer it will destroy the connection. This is | |
282 * used when you want to send one final message across the peer | |
283 * connection, and then close everything. | |
284 */ | |
285 static gboolean | |
286 destroy_connection_when_done_sending_data(gpointer data) | |
287 { | |
288 PeerConnection *conn; | |
289 | |
290 conn = data; | |
291 | |
292 if (gaim_circ_buffer_get_max_read(conn->buffer_outgoing) == 0) | |
293 { | |
294 conn->sending_data_timer = 0; | |
14402 | 295 peer_connection_destroy(conn, conn->disconnect_reason, NULL); |
14192 | 296 return FALSE; |
297 } | |
298 | |
299 return TRUE; | |
300 } | |
301 | |
302 /* | |
303 * This is called when a buddy sends us some file info. This happens when they | |
304 * are sending a file to you, and you have just established a connection to them. | |
305 * You should send them the exact same info except use the real cookie. We also | |
306 * get like totally ready to like, receive the file, kay? | |
307 */ | |
308 static void | |
309 peer_oft_recv_frame_prompt(PeerConnection *conn, OftFrame *frame) | |
310 { | |
311 /* Record the file information and send an ack */ | |
15260 | 312 peer_oft_copy_xfer_data(conn, frame); |
14192 | 313 peer_oft_send_ack(conn); |
314 | |
315 /* Remove our watchers and use the file transfer watchers in the core */ | |
316 gaim_input_remove(conn->watcher_incoming); | |
317 conn->watcher_incoming = 0; | |
318 conn->sending_data_timer = gaim_timeout_add(100, | |
319 start_transfer_when_done_sending_data, conn); | |
320 } | |
321 | |
322 /** | |
323 * We are sending a file to someone else. They have just acknowledged our | |
324 * prompt, so we want to start sending data like there's no tomorrow. | |
325 */ | |
326 static void | |
327 peer_oft_recv_frame_ack(PeerConnection *conn, OftFrame *frame) | |
328 { | |
15260 | 329 if (memcmp(conn->cookie, frame->cookie, 8) != 0) |
14192 | 330 { |
331 gaim_debug_info("oscar", "Received an incorrect cookie. " | |
332 "Closing connection.\n"); | |
14402 | 333 peer_connection_destroy(conn, OSCAR_DISCONNECT_INVALID_DATA, NULL); |
14192 | 334 return; |
335 } | |
336 | |
337 /* Remove our watchers and use the file transfer watchers in the core */ | |
338 gaim_input_remove(conn->watcher_incoming); | |
339 conn->watcher_incoming = 0; | |
340 conn->sending_data_timer = gaim_timeout_add(100, | |
341 start_transfer_when_done_sending_data, conn); | |
342 } | |
343 | |
15260 | 344 /** |
345 * We are sending a file to someone else. They have just acknowledged our | |
346 * prompt and are asking to resume, so we accept their resume and await | |
347 * a resume ack. | |
348 */ | |
349 static void | |
350 peer_oft_recv_frame_resume(PeerConnection *conn, OftFrame *frame) | |
351 { | |
352 if (memcmp(conn->cookie, frame->cookie, 8) != 0) | |
353 { | |
354 gaim_debug_info("oscar", "Received an incorrect cookie. " | |
355 "Closing connection.\n"); | |
356 peer_connection_destroy(conn, OSCAR_DISCONNECT_INVALID_DATA, NULL); | |
357 return; | |
358 } | |
359 | |
360 /* | |
361 * TODO: Check the checksums here. If they don't match then don't | |
362 * copy the data like below. | |
363 */ | |
364 | |
365 /* Copy resume data into internal structure */ | |
366 conn->xferdata.recvcsum = frame->recvcsum; | |
367 conn->xferdata.rfrcsum = frame->rfrcsum; | |
368 conn->xferdata.nrecvd = frame->nrecvd; | |
369 | |
370 gaim_xfer_set_bytes_sent(conn->xfer, frame->nrecvd); | |
371 peer_oft_send_resume_accept(conn); | |
372 } | |
373 | |
14192 | 374 /* |
375 * We just sent a file to someone. They said they got it and everything, | |
376 * so we can close our direct connection and what not. | |
377 */ | |
378 static void | |
379 peer_oft_recv_frame_done(PeerConnection *conn, OftFrame *frame) | |
380 { | |
381 gaim_input_remove(conn->watcher_incoming); | |
382 conn->watcher_incoming = 0; | |
383 conn->xfer->fd = conn->fd; | |
384 conn->fd = -1; | |
15277
623affd9a13e
[gaim-migrate @ 18067]
Evan Schoenberg <evan.s@dreskin.net>
parents:
15260
diff
changeset
|
385 conn->disconnect_reason = OSCAR_DISCONNECT_DONE; |
623affd9a13e
[gaim-migrate @ 18067]
Evan Schoenberg <evan.s@dreskin.net>
parents:
15260
diff
changeset
|
386 peer_connection_schedule_destroy(conn, conn->disconnect_reason, NULL); |
14192 | 387 } |
388 | |
389 /** | |
390 * Handle an incoming OftFrame. If there is a payload associated | |
391 * with this frame, then we remove the old watcher and add the | |
392 * OFT watcher to read in the payload. | |
393 */ | |
394 void | |
395 peer_oft_recv_frame(PeerConnection *conn, ByteStream *bs) | |
396 { | |
397 OftFrame frame; | |
398 | |
399 frame.type = byte_stream_get16(bs); | |
400 byte_stream_getrawbuf(bs, frame.cookie, 8); | |
401 frame.encrypt = byte_stream_get16(bs); | |
402 frame.compress = byte_stream_get16(bs); | |
403 frame.totfiles = byte_stream_get16(bs); | |
404 frame.filesleft = byte_stream_get16(bs); | |
405 frame.totparts = byte_stream_get16(bs); | |
406 frame.partsleft = byte_stream_get16(bs); | |
407 frame.totsize = byte_stream_get32(bs); | |
408 frame.size = byte_stream_get32(bs); | |
409 frame.modtime = byte_stream_get32(bs); | |
410 frame.checksum = byte_stream_get32(bs); | |
411 frame.rfrcsum = byte_stream_get32(bs); | |
412 frame.rfsize = byte_stream_get32(bs); | |
413 frame.cretime = byte_stream_get32(bs); | |
414 frame.rfcsum = byte_stream_get32(bs); | |
415 frame.nrecvd = byte_stream_get32(bs); | |
416 frame.recvcsum = byte_stream_get32(bs); | |
417 byte_stream_getrawbuf(bs, frame.idstring, 32); | |
418 frame.flags = byte_stream_get8(bs); | |
419 frame.lnameoffset = byte_stream_get8(bs); | |
420 frame.lsizeoffset = byte_stream_get8(bs); | |
421 byte_stream_getrawbuf(bs, frame.dummy, 69); | |
422 byte_stream_getrawbuf(bs, frame.macfileinfo, 16); | |
423 frame.nencode = byte_stream_get16(bs); | |
424 frame.nlanguage = byte_stream_get16(bs); | |
425 frame.name_length = bs->len - 186; | |
426 frame.name = byte_stream_getraw(bs, frame.name_length); | |
427 | |
428 gaim_debug_info("oscar", "Incoming OFT frame from %s with " | |
429 "type=0x%04x\n", conn->sn, frame.type); | |
430 | |
431 /* TODOFT: peer_oft_dirconvert_fromstupid(frame->name); */ | |
432 | |
15260 | 433 switch(frame.type) |
434 { | |
435 case PEER_TYPE_PROMPT: | |
436 peer_oft_recv_frame_prompt(conn, &frame); | |
437 break; | |
438 case PEER_TYPE_ACK: | |
439 case PEER_TYPE_RESUMEACK: | |
440 peer_oft_recv_frame_ack(conn, &frame); | |
441 break; | |
442 case PEER_TYPE_RESUME: | |
443 peer_oft_recv_frame_resume(conn, &frame); | |
444 break; | |
445 case PEER_TYPE_DONE: | |
446 peer_oft_recv_frame_done(conn, &frame); | |
447 break; | |
448 default: | |
449 break; | |
450 } | |
14192 | 451 |
452 free(frame.name); | |
453 } | |
454 | |
455 /*******************************************************************/ | |
456 /* Begin GaimXfer callbacks for use when receiving a file */ | |
457 /*******************************************************************/ | |
458 | |
459 void | |
460 peer_oft_recvcb_init(GaimXfer *xfer) | |
461 { | |
462 PeerConnection *conn; | |
463 | |
464 conn = xfer->data; | |
465 conn->flags |= PEER_CONNECTION_FLAG_APPROVED; | |
466 peer_connection_trynext(conn); | |
467 } | |
468 | |
469 void | |
470 peer_oft_recvcb_end(GaimXfer *xfer) | |
471 { | |
472 PeerConnection *conn; | |
473 | |
474 conn = xfer->data; | |
475 | |
476 /* Tell the other person that we've received everything */ | |
477 conn->fd = conn->xfer->fd; | |
478 conn->xfer->fd = -1; | |
479 peer_oft_send_done(conn); | |
480 | |
481 conn->disconnect_reason = OSCAR_DISCONNECT_DONE; | |
482 conn->sending_data_timer = gaim_timeout_add(100, | |
483 destroy_connection_when_done_sending_data, conn); | |
484 } | |
485 | |
486 void | |
487 peer_oft_recvcb_ack_recv(GaimXfer *xfer, const guchar *buffer, size_t size) | |
488 { | |
489 PeerConnection *conn; | |
490 | |
491 /* Update our rolling checksum. Like Walmart, yo. */ | |
492 conn = xfer->data; | |
493 conn->xferdata.recvcsum = peer_oft_checksum_chunk(buffer, | |
494 size, conn->xferdata.recvcsum); | |
495 } | |
496 | |
497 /*******************************************************************/ | |
498 /* End GaimXfer callbacks for use when receiving a file */ | |
499 /*******************************************************************/ | |
500 | |
501 /*******************************************************************/ | |
502 /* Begin GaimXfer callbacks for use when sending a file */ | |
503 /*******************************************************************/ | |
504 | |
505 void | |
506 peer_oft_sendcb_init(GaimXfer *xfer) | |
507 { | |
508 PeerConnection *conn; | |
14538 | 509 size_t size; |
14192 | 510 |
511 conn = xfer->data; | |
512 conn->flags |= PEER_CONNECTION_FLAG_APPROVED; | |
513 | |
14538 | 514 /* Make sure the file size can be represented in 32 bits */ |
515 size = gaim_xfer_get_size(xfer); | |
516 if (size > G_MAXUINT32) | |
517 { | |
518 gchar *tmp, *size1, *size2; | |
519 size1 = gaim_str_size_to_units(size); | |
520 size2 = gaim_str_size_to_units(G_MAXUINT32); | |
521 tmp = g_strdup_printf(_("File %s is %s, which is larger than " | |
522 "the maximum size of %s."), | |
523 xfer->local_filename, size1, size2); | |
524 gaim_xfer_error(gaim_xfer_get_type(xfer), | |
525 gaim_xfer_get_account(xfer), xfer->who, tmp); | |
526 g_free(size1); | |
527 g_free(size2); | |
528 g_free(tmp); | |
529 peer_connection_destroy(conn, OSCAR_DISCONNECT_LOCAL_CLOSED, NULL); | |
530 return; | |
531 } | |
532 | |
14192 | 533 /* Keep track of file transfer info */ |
534 conn->xferdata.totfiles = 1; | |
535 conn->xferdata.filesleft = 1; | |
536 conn->xferdata.totparts = 1; | |
537 conn->xferdata.partsleft = 1; | |
14538 | 538 conn->xferdata.totsize = size; |
539 conn->xferdata.size = size; | |
14192 | 540 conn->xferdata.checksum = 0xffff0000; |
541 conn->xferdata.rfrcsum = 0xffff0000; | |
542 conn->xferdata.rfcsum = 0xffff0000; | |
543 conn->xferdata.recvcsum = 0xffff0000; | |
544 strncpy((gchar *)conn->xferdata.idstring, "OFT_Windows ICBMFT V1.1 32", 31); | |
545 conn->xferdata.modtime = 0; | |
546 conn->xferdata.cretime = 0; | |
547 xfer->filename = g_path_get_basename(xfer->local_filename); | |
548 conn->xferdata.name = (guchar *)g_strdup(xfer->filename); | |
549 conn->xferdata.name_length = strlen(xfer->filename); | |
550 | |
551 /* Calculating the checksum can take a very long time for large files */ | |
552 gaim_debug_info("oscar","calculating file checksum\n"); | |
553 conn->xferdata.checksum = peer_oft_checksum_file(xfer->local_filename); | |
554 gaim_debug_info("oscar","checksum calculated\n"); | |
555 | |
556 /* Start the connection process */ | |
557 peer_connection_trynext(conn); | |
558 } | |
559 | |
560 /* | |
561 * AIM file transfers aren't really meant to be thought | |
562 * of as a transferring just a single file. The rendezvous | |
563 * establishes a connection between two computers, and then | |
564 * those computers can use the same connection for transferring | |
565 * multiple files. So we don't want the Gaim core up and closing | |
566 * the socket all willy-nilly. We want to do that in the oscar | |
567 * prpl, whenever one side or the other says they're finished | |
568 * using the connection. There might be a better way to intercept | |
569 * the socket from the core... | |
570 */ | |
571 void | |
572 peer_oft_sendcb_ack(GaimXfer *xfer, const guchar *buffer, size_t size) | |
573 { | |
574 PeerConnection *conn; | |
575 | |
576 conn = xfer->data; | |
577 | |
578 /* | |
579 * If we're done sending, intercept the socket from the core ft code | |
580 * and wait for the other guy to send the "done" OFT packet. | |
581 */ | |
582 if (gaim_xfer_get_bytes_remaining(xfer) <= 0) | |
583 { | |
584 gaim_input_remove(xfer->watcher); | |
585 conn->fd = xfer->fd; | |
586 xfer->fd = -1; | |
587 conn->watcher_incoming = gaim_input_add(conn->fd, | |
588 GAIM_INPUT_READ, peer_connection_recv_cb, conn); | |
589 } | |
590 } | |
591 | |
592 /*******************************************************************/ | |
593 /* End GaimXfer callbacks for use when sending a file */ | |
594 /*******************************************************************/ | |
595 | |
596 /*******************************************************************/ | |
597 /* Begin GaimXfer callbacks for use when sending and receiving */ | |
598 /*******************************************************************/ | |
599 | |
600 void | |
601 peer_oft_cb_generic_cancel(GaimXfer *xfer) | |
602 { | |
603 PeerConnection *conn; | |
604 | |
605 conn = xfer->data; | |
606 | |
607 if (conn == NULL) | |
608 return; | |
609 | |
14402 | 610 peer_connection_destroy(conn, OSCAR_DISCONNECT_LOCAL_CLOSED, NULL); |
14192 | 611 } |
612 | |
613 /*******************************************************************/ | |
614 /* End GaimXfer callbacks for use when sending and receiving */ | |
615 /*******************************************************************/ | |
616 | |
15107 | 617 #ifdef TODOFT |
14192 | 618 /* |
619 * This little area in oscar.c is the nexus of file transfer code, | |
620 * so I wrote a little explanation of what happens. I am such a | |
621 * ninja. | |
622 * | |
623 * The series of events for a file send is: | |
624 * -Create xfer and call gaim_xfer_request (this happens in oscar_ask_sendfile) | |
625 * -User chooses a file and oscar_xfer_init is called. It establishes a | |
626 * listening socket, then asks the remote user to connect to us (and | |
627 * gives them the file name, port, IP, etc.) | |
628 * -They connect to us and we send them an PEER_TYPE_PROMPT (this happens | |
629 * in peer_oft_recv_frame_established) | |
630 * -They send us an PEER_TYPE_ACK and then we start sending data | |
631 * -When we finish, they send us an PEER_TYPE_DONE and they close the | |
632 * connection. | |
633 * -We get drunk because file transfer kicks ass. | |
634 * | |
635 * The series of events for a file receive is: | |
636 * -Create xfer and call gaim_xfer request (this happens in incomingim_chan2) | |
637 * -Gaim user selects file to name and location to save file to and | |
638 * oscar_xfer_init is called | |
639 * -It connects to the remote user using the IP they gave us earlier | |
640 * -After connecting, they send us an PEER_TYPE_PROMPT. In reply, we send | |
641 * them an PEER_TYPE_ACK. | |
642 * -They begin to send us lots of raw data. | |
643 * -When they finish sending data we send an PEER_TYPE_DONE and then close | |
644 * the connection. | |
645 * | |
646 * Update August 2005: | |
647 * The series of events for transfers has been seriously complicated by the addition | |
648 * of transfer redirects and proxied connections. I could throw a whole lot of words | |
649 * at trying to explain things here, but it probably wouldn't do much good. To get | |
650 * a better idea of what happens, take a look at the diagrams and documentation | |
651 * from my Summer of Code project. -- Jonathan Clark | |
652 */ | |
653 | |
654 /** | |
655 * Convert the directory separator from / (0x2f) to ^A (0x01) | |
656 * | |
657 * @param name The filename to convert. | |
658 */ | |
659 static void | |
660 peer_oft_dirconvert_tostupid(char *name) | |
661 { | |
662 while (name[0]) { | |
663 if (name[0] == 0x01) | |
664 name[0] = G_DIR_SEPARATOR; | |
665 name++; | |
666 } | |
667 } | |
668 | |
669 /** | |
670 * Convert the directory separator from ^A (0x01) to / (0x2f) | |
671 * | |
672 * @param name The filename to convert. | |
673 */ | |
674 static void | |
675 peer_oft_dirconvert_fromstupid(char *name) | |
676 { | |
677 while (name[0]) { | |
678 if (name[0] == G_DIR_SEPARATOR) | |
679 name[0] = 0x01; | |
680 name++; | |
681 } | |
682 } | |
683 #endif |