1 | /********************************************************************\ |
---|
2 | * BitlBee -- An IRC to other IM-networks gateway * |
---|
3 | * * |
---|
4 | * Copyright 2007 Uli Meis <a.sporto+bee@gmail.com> * |
---|
5 | \********************************************************************/ |
---|
6 | |
---|
7 | /* |
---|
8 | This program is free software; you can redistribute it and/or modify |
---|
9 | it under the terms of the GNU General Public License as published by |
---|
10 | the Free Software Foundation; either version 2 of the License, or |
---|
11 | (at your option) any later version. |
---|
12 | |
---|
13 | This program is distributed in the hope that it will be useful, |
---|
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
16 | GNU General Public License for more details. |
---|
17 | |
---|
18 | You should have received a copy of the GNU General Public License with |
---|
19 | the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL; |
---|
20 | if not, write to the Free Software Foundation, Inc., 59 Temple Place, |
---|
21 | Suite 330, Boston, MA 02111-1307 USA |
---|
22 | */ |
---|
23 | |
---|
24 | #define BITLBEE_CORE |
---|
25 | #include "bitlbee.h" |
---|
26 | #include "ft.h" |
---|
27 | #include "dcc.h" |
---|
28 | #include <netinet/tcp.h> |
---|
29 | #include <regex.h> |
---|
30 | #include "lib/ftutil.h" |
---|
31 | |
---|
32 | /* |
---|
33 | * Since that might be confusing a note on naming: |
---|
34 | * |
---|
35 | * Generic dcc functions start with |
---|
36 | * |
---|
37 | * dcc_ |
---|
38 | * |
---|
39 | * ,methods specific to DCC SEND start with |
---|
40 | * |
---|
41 | * dccs_ |
---|
42 | * |
---|
43 | * . Since we can be on both ends of a DCC SEND, |
---|
44 | * functions specific to one end are called |
---|
45 | * |
---|
46 | * dccs_send and dccs_recv |
---|
47 | * |
---|
48 | * ,respectively. |
---|
49 | */ |
---|
50 | |
---|
51 | |
---|
52 | /* |
---|
53 | * used to generate a unique local transfer id the user |
---|
54 | * can use to reject/cancel transfers |
---|
55 | */ |
---|
56 | unsigned int local_transfer_id=1; |
---|
57 | |
---|
58 | /* |
---|
59 | * just for debugging the nr. of chunks we received from im-protocols and the total data |
---|
60 | */ |
---|
61 | unsigned int receivedchunks=0, receiveddata=0; |
---|
62 | |
---|
63 | static void dcc_finish( file_transfer_t *file ); |
---|
64 | static void dcc_close( file_transfer_t *file ); |
---|
65 | gboolean dccs_send_proto( gpointer data, gint fd, b_input_condition cond ); |
---|
66 | int dccs_send_request( struct dcc_file_transfer *df, char *user_nick, struct sockaddr_storage *saddr ); |
---|
67 | gboolean dccs_recv_start( file_transfer_t *ft ); |
---|
68 | gboolean dccs_recv_proto( gpointer data, gint fd, b_input_condition cond); |
---|
69 | gboolean dccs_recv_write_request( file_transfer_t *ft ); |
---|
70 | gboolean dcc_progress( gpointer data, gint fd, b_input_condition cond ); |
---|
71 | gboolean dcc_abort( dcc_file_transfer_t *df, char *reason, ... ); |
---|
72 | |
---|
73 | /* As defined in ft.h */ |
---|
74 | file_transfer_t *imcb_file_send_start( struct im_connection *ic, char *handle, char *file_name, size_t file_size ) |
---|
75 | { |
---|
76 | user_t *u = user_findhandle( ic, handle ); |
---|
77 | /* one could handle this more intelligent like imcb_buddy_msg. |
---|
78 | * can't call it directly though cause it does some wrapping. |
---|
79 | * Maybe give imcb_buddy_msg a parameter NO_WRAPPING? */ |
---|
80 | if (!u) return NULL; |
---|
81 | |
---|
82 | return dccs_send_start( ic, u->nick, file_name, file_size ); |
---|
83 | }; |
---|
84 | |
---|
85 | /* As defined in ft.h */ |
---|
86 | void imcb_file_canceled( file_transfer_t *file, char *reason ) |
---|
87 | { |
---|
88 | if( file->canceled ) |
---|
89 | file->canceled( file, reason ); |
---|
90 | |
---|
91 | dcc_close( file ); |
---|
92 | } |
---|
93 | |
---|
94 | /* As defined in ft.h */ |
---|
95 | gboolean imcb_file_recv_start( file_transfer_t *ft ) |
---|
96 | { |
---|
97 | return dccs_recv_start( ft ); |
---|
98 | } |
---|
99 | |
---|
100 | /* As defined in ft.h */ |
---|
101 | void imcb_file_finished( file_transfer_t *file ) |
---|
102 | { |
---|
103 | dcc_file_transfer_t *df = file->priv; |
---|
104 | |
---|
105 | if( file->bytes_transferred >= file->file_size ) |
---|
106 | dcc_finish( file ); |
---|
107 | else |
---|
108 | df->proto_finished = TRUE; |
---|
109 | } |
---|
110 | |
---|
111 | dcc_file_transfer_t *dcc_alloc_transfer( char *file_name, size_t file_size, struct im_connection *ic ) |
---|
112 | { |
---|
113 | file_transfer_t *file = g_new0( file_transfer_t, 1 ); |
---|
114 | dcc_file_transfer_t *df = file->priv = g_new0( dcc_file_transfer_t, 1 ); |
---|
115 | |
---|
116 | file->file_size = file_size; |
---|
117 | file->file_name = g_strdup( file_name ); |
---|
118 | file->local_id = local_transfer_id++; |
---|
119 | df->ic = ic; |
---|
120 | df->ft = file; |
---|
121 | |
---|
122 | return df; |
---|
123 | } |
---|
124 | |
---|
125 | /* This is where the sending magic starts... */ |
---|
126 | file_transfer_t *dccs_send_start( struct im_connection *ic, char *user_nick, char *file_name, size_t file_size ) |
---|
127 | { |
---|
128 | file_transfer_t *file; |
---|
129 | dcc_file_transfer_t *df; |
---|
130 | struct sockaddr_storage saddr; |
---|
131 | char *errmsg; |
---|
132 | char host[HOST_NAME_MAX]; |
---|
133 | char port[6]; |
---|
134 | |
---|
135 | if( file_size > global.conf->ft_max_size ) |
---|
136 | return NULL; |
---|
137 | |
---|
138 | df = dcc_alloc_transfer( file_name, file_size, ic ); |
---|
139 | file = df->ft; |
---|
140 | file->write = dccs_send_write; |
---|
141 | |
---|
142 | /* listen and request */ |
---|
143 | |
---|
144 | if( ( df->fd = ft_listen( &saddr, host, port, TRUE, &errmsg ) ) == -1 ) |
---|
145 | { |
---|
146 | dcc_abort( df, "Failed to listen locally, check your ft_listen setting in bitlbee.conf: %s", errmsg ); |
---|
147 | return NULL; |
---|
148 | } |
---|
149 | |
---|
150 | file->status = FT_STATUS_LISTENING; |
---|
151 | |
---|
152 | if( !dccs_send_request( df, user_nick, &saddr ) ) |
---|
153 | return NULL; |
---|
154 | |
---|
155 | /* watch */ |
---|
156 | df->watch_in = b_input_add( df->fd, GAIM_INPUT_READ, dccs_send_proto, df ); |
---|
157 | |
---|
158 | df->ic->irc->file_transfers = g_slist_prepend( df->ic->irc->file_transfers, file ); |
---|
159 | |
---|
160 | df->progress_timeout = b_timeout_add( DCC_MAX_STALL * 1000, dcc_progress, df ); |
---|
161 | |
---|
162 | imcb_log( ic, "File transfer request from %s for %s (%zd kb).\n" |
---|
163 | "Accept the file transfer if you'd like the file. If you don't, " |
---|
164 | "issue the 'transfers reject' command.", |
---|
165 | user_nick, file_name, file_size / 1024 ); |
---|
166 | |
---|
167 | return file; |
---|
168 | } |
---|
169 | |
---|
170 | /* Used pretty much everywhere in the code to abort a transfer */ |
---|
171 | gboolean dcc_abort( dcc_file_transfer_t *df, char *reason, ... ) |
---|
172 | { |
---|
173 | file_transfer_t *file = df->ft; |
---|
174 | va_list params; |
---|
175 | va_start( params, reason ); |
---|
176 | char *msg = g_strdup_vprintf( reason, params ); |
---|
177 | va_end( params ); |
---|
178 | |
---|
179 | file->status |= FT_STATUS_CANCELED; |
---|
180 | |
---|
181 | if( file->canceled ) |
---|
182 | file->canceled( file, msg ); |
---|
183 | |
---|
184 | imcb_log( df->ic, "File %s: DCC transfer aborted: %s", file->file_name, msg ); |
---|
185 | |
---|
186 | g_free( msg ); |
---|
187 | |
---|
188 | dcc_close( df->ft ); |
---|
189 | |
---|
190 | return FALSE; |
---|
191 | } |
---|
192 | |
---|
193 | gboolean dcc_progress( gpointer data, gint fd, b_input_condition cond ) |
---|
194 | { |
---|
195 | struct dcc_file_transfer *df = data; |
---|
196 | |
---|
197 | if( df->bytes_sent == df->progress_bytes_last ) |
---|
198 | { |
---|
199 | /* no progress. cancel */ |
---|
200 | if( df->bytes_sent == 0 ) |
---|
201 | return dcc_abort( df, "Couldn't establish transfer within %d seconds", DCC_MAX_STALL ); |
---|
202 | else |
---|
203 | return dcc_abort( df, "Transfer stalled for %d seconds at %d kb", DCC_MAX_STALL, df->bytes_sent / 1024 ); |
---|
204 | |
---|
205 | } |
---|
206 | |
---|
207 | df->progress_bytes_last = df->bytes_sent; |
---|
208 | |
---|
209 | return TRUE; |
---|
210 | } |
---|
211 | |
---|
212 | /* used extensively for socket operations */ |
---|
213 | #define ASSERTSOCKOP(op, msg) \ |
---|
214 | if( (op) == -1 ) \ |
---|
215 | return dcc_abort( df , msg ": %s", strerror( errno ) ); |
---|
216 | |
---|
217 | /* Creates the "DCC SEND" line and sends it to the server */ |
---|
218 | int dccs_send_request( struct dcc_file_transfer *df, char *user_nick, struct sockaddr_storage *saddr ) |
---|
219 | { |
---|
220 | char ipaddr[INET6_ADDRSTRLEN]; |
---|
221 | const void *netaddr; |
---|
222 | int port; |
---|
223 | char *cmd; |
---|
224 | |
---|
225 | if( saddr->ss_family == AF_INET ) |
---|
226 | { |
---|
227 | struct sockaddr_in *saddr_ipv4 = ( struct sockaddr_in *) saddr; |
---|
228 | |
---|
229 | sprintf( ipaddr, "%d", |
---|
230 | ntohl( saddr_ipv4->sin_addr.s_addr ) ); |
---|
231 | port = saddr_ipv4->sin_port; |
---|
232 | } |
---|
233 | else |
---|
234 | { |
---|
235 | struct sockaddr_in6 *saddr_ipv6 = ( struct sockaddr_in6 *) saddr; |
---|
236 | |
---|
237 | netaddr = &saddr_ipv6->sin6_addr.s6_addr; |
---|
238 | port = saddr_ipv6->sin6_port; |
---|
239 | |
---|
240 | /* |
---|
241 | * Didn't find docs about this, but it seems that's the way irssi does it |
---|
242 | */ |
---|
243 | if( !inet_ntop( saddr->ss_family, netaddr, ipaddr, sizeof( ipaddr ) ) ) |
---|
244 | return dcc_abort( df, "inet_ntop failed: %s", strerror( errno ) ); |
---|
245 | } |
---|
246 | |
---|
247 | port = ntohs( port ); |
---|
248 | |
---|
249 | cmd = g_strdup_printf( "\001DCC SEND %s %s %u %zu\001", |
---|
250 | df->ft->file_name, ipaddr, port, df->ft->file_size ); |
---|
251 | |
---|
252 | if ( !irc_msgfrom( df->ic->irc, user_nick, cmd ) ) |
---|
253 | return dcc_abort( df, "Couldn't send `DCC SEND' message to %s.", user_nick ); |
---|
254 | |
---|
255 | g_free( cmd ); |
---|
256 | |
---|
257 | return TRUE; |
---|
258 | } |
---|
259 | |
---|
260 | /* |
---|
261 | * After setup, the transfer itself is handled entirely by this function. |
---|
262 | * There are basically four things to handle: connect, receive, send, and error. |
---|
263 | */ |
---|
264 | gboolean dccs_send_proto( gpointer data, gint fd, b_input_condition cond ) |
---|
265 | { |
---|
266 | dcc_file_transfer_t *df = data; |
---|
267 | file_transfer_t *file = df->ft; |
---|
268 | |
---|
269 | if( ( cond & GAIM_INPUT_READ ) && |
---|
270 | ( file->status & FT_STATUS_LISTENING ) ) |
---|
271 | { |
---|
272 | struct sockaddr *clt_addr; |
---|
273 | socklen_t ssize = sizeof( clt_addr ); |
---|
274 | |
---|
275 | /* Connect */ |
---|
276 | |
---|
277 | ASSERTSOCKOP( df->fd = accept( fd, (struct sockaddr *) &clt_addr, &ssize ), "Accepting connection" ); |
---|
278 | |
---|
279 | closesocket( fd ); |
---|
280 | fd = df->fd; |
---|
281 | file->status = FT_STATUS_TRANSFERRING; |
---|
282 | sock_make_nonblocking( fd ); |
---|
283 | |
---|
284 | /* IM protocol callback */ |
---|
285 | if( file->accept ) |
---|
286 | file->accept( file ); |
---|
287 | |
---|
288 | /* reschedule for reading on new fd */ |
---|
289 | df->watch_in = b_input_add( fd, GAIM_INPUT_READ, dccs_send_proto, df ); |
---|
290 | |
---|
291 | return FALSE; |
---|
292 | } |
---|
293 | |
---|
294 | if( cond & GAIM_INPUT_READ ) |
---|
295 | { |
---|
296 | int ret; |
---|
297 | |
---|
298 | ASSERTSOCKOP( ret = recv( fd, ( (char*) &df->acked ) + df->acked_len, |
---|
299 | sizeof( df->acked ) - df->acked_len, 0 ), "Receiving" ); |
---|
300 | |
---|
301 | if( ret == 0 ) |
---|
302 | return dcc_abort( df, "Remote end closed connection" ); |
---|
303 | |
---|
304 | /* How likely is it that a 32-bit integer gets split accross |
---|
305 | packet boundaries? Chances are rarely 0 so let's be sure. */ |
---|
306 | if( ( df->acked_len = ( df->acked_len + ret ) % 4 ) > 0 ) |
---|
307 | return TRUE; |
---|
308 | |
---|
309 | df->acked = ntohl( df->acked ); |
---|
310 | |
---|
311 | /* If any of this is actually happening, the receiver should buy a new IRC client */ |
---|
312 | |
---|
313 | if ( df->acked > df->bytes_sent ) |
---|
314 | return dcc_abort( df, "Receiver magically received more bytes than sent ( %d > %d ) (BUG at receiver?)", df->acked, df->bytes_sent ); |
---|
315 | |
---|
316 | if ( df->acked < file->bytes_transferred ) |
---|
317 | return dcc_abort( df, "Receiver lost bytes? ( has %d, had %d ) (BUG at receiver?)", df->acked, file->bytes_transferred ); |
---|
318 | |
---|
319 | file->bytes_transferred = df->acked; |
---|
320 | |
---|
321 | if( file->bytes_transferred >= file->file_size ) { |
---|
322 | if( df->proto_finished ) |
---|
323 | dcc_finish( file ); |
---|
324 | return FALSE; |
---|
325 | } |
---|
326 | |
---|
327 | return TRUE; |
---|
328 | } |
---|
329 | |
---|
330 | return TRUE; |
---|
331 | } |
---|
332 | |
---|
333 | gboolean dccs_recv_start( file_transfer_t *ft ) |
---|
334 | { |
---|
335 | dcc_file_transfer_t *df = ft->priv; |
---|
336 | struct sockaddr_storage *saddr = &df->saddr; |
---|
337 | int fd; |
---|
338 | char ipaddr[INET6_ADDRSTRLEN]; |
---|
339 | socklen_t sa_len = saddr->ss_family == AF_INET ? |
---|
340 | sizeof( struct sockaddr_in ) : sizeof( struct sockaddr_in6 ); |
---|
341 | |
---|
342 | if( !ft->write ) |
---|
343 | return dcc_abort( df, "BUG: protocol didn't register write()" ); |
---|
344 | |
---|
345 | ASSERTSOCKOP( fd = df->fd = socket( saddr->ss_family, SOCK_STREAM, 0 ), "Opening Socket" ); |
---|
346 | |
---|
347 | sock_make_nonblocking( fd ); |
---|
348 | |
---|
349 | if( ( connect( fd, (struct sockaddr *)saddr, sa_len ) == -1 ) && |
---|
350 | ( errno != EINPROGRESS ) ) |
---|
351 | return dcc_abort( df, "Connecting to %s:%d : %s", |
---|
352 | inet_ntop( saddr->ss_family, |
---|
353 | saddr->ss_family == AF_INET ? |
---|
354 | ( void* ) &( ( struct sockaddr_in *) saddr )->sin_addr.s_addr : |
---|
355 | ( void* ) &( ( struct sockaddr_in6 *) saddr )->sin6_addr.s6_addr, |
---|
356 | ipaddr, |
---|
357 | sizeof( ipaddr ) ), |
---|
358 | ntohs( saddr->ss_family == AF_INET ? |
---|
359 | ( ( struct sockaddr_in *) saddr )->sin_port : |
---|
360 | ( ( struct sockaddr_in6 *) saddr )->sin6_port ), |
---|
361 | strerror( errno ) ); |
---|
362 | |
---|
363 | ft->status = FT_STATUS_CONNECTING; |
---|
364 | |
---|
365 | /* watch */ |
---|
366 | df->watch_out = b_input_add( df->fd, GAIM_INPUT_WRITE, dccs_recv_proto, df ); |
---|
367 | ft->write_request = dccs_recv_write_request; |
---|
368 | |
---|
369 | df->progress_timeout = b_timeout_add( DCC_MAX_STALL * 1000, dcc_progress, df ); |
---|
370 | |
---|
371 | return TRUE; |
---|
372 | } |
---|
373 | |
---|
374 | gboolean dccs_recv_proto( gpointer data, gint fd, b_input_condition cond ) |
---|
375 | { |
---|
376 | dcc_file_transfer_t *df = data; |
---|
377 | file_transfer_t *ft = df->ft; |
---|
378 | |
---|
379 | if( ( cond & GAIM_INPUT_WRITE ) && |
---|
380 | ( ft->status & FT_STATUS_CONNECTING ) ) |
---|
381 | { |
---|
382 | ft->status = FT_STATUS_TRANSFERRING; |
---|
383 | |
---|
384 | //df->watch_in = b_input_add( df->fd, GAIM_INPUT_READ, dccs_recv_proto, df ); |
---|
385 | |
---|
386 | df->watch_out = 0; |
---|
387 | return FALSE; |
---|
388 | } |
---|
389 | |
---|
390 | if( cond & GAIM_INPUT_READ ) |
---|
391 | { |
---|
392 | int ret, done; |
---|
393 | |
---|
394 | ASSERTSOCKOP( ret = recv( fd, ft->buffer, sizeof( ft->buffer ), 0 ), "Receiving" ); |
---|
395 | |
---|
396 | if( ret == 0 ) |
---|
397 | return dcc_abort( df, "Remote end closed connection" ); |
---|
398 | |
---|
399 | if( !ft->write( df->ft, ft->buffer, ret ) ) |
---|
400 | return FALSE; |
---|
401 | |
---|
402 | df->bytes_sent += ret; |
---|
403 | |
---|
404 | done = df->bytes_sent >= ft->file_size; |
---|
405 | |
---|
406 | if( ( ( df->bytes_sent - ft->bytes_transferred ) > DCC_PACKET_SIZE ) || |
---|
407 | done ) |
---|
408 | { |
---|
409 | guint32 ack = htonl( ft->bytes_transferred = df->bytes_sent ); |
---|
410 | int ackret; |
---|
411 | |
---|
412 | ASSERTSOCKOP( ackret = send( fd, &ack, 4, 0 ), "Sending DCC ACK" ); |
---|
413 | |
---|
414 | if ( ackret != 4 ) |
---|
415 | return dcc_abort( df, "Error sending DCC ACK, sent %d instead of 4 bytes", ackret ); |
---|
416 | } |
---|
417 | |
---|
418 | if( df->bytes_sent == ret ) |
---|
419 | ft->started = time( NULL ); |
---|
420 | |
---|
421 | if( done ) |
---|
422 | { |
---|
423 | if( df->watch_out ) |
---|
424 | b_event_remove( df->watch_out ); |
---|
425 | |
---|
426 | if( df->proto_finished ) |
---|
427 | dcc_finish( ft ); |
---|
428 | |
---|
429 | df->watch_in = 0; |
---|
430 | return FALSE; |
---|
431 | } |
---|
432 | |
---|
433 | df->watch_in = 0; |
---|
434 | return FALSE; |
---|
435 | } |
---|
436 | |
---|
437 | return TRUE; |
---|
438 | } |
---|
439 | |
---|
440 | gboolean dccs_recv_write_request( file_transfer_t *ft ) |
---|
441 | { |
---|
442 | dcc_file_transfer_t *df = ft->priv; |
---|
443 | |
---|
444 | if( df->watch_in ) |
---|
445 | return dcc_abort( df, "BUG: write_request() called while watching" ); |
---|
446 | |
---|
447 | df->watch_in = b_input_add( df->fd, GAIM_INPUT_READ, dccs_recv_proto, df ); |
---|
448 | |
---|
449 | return TRUE; |
---|
450 | } |
---|
451 | |
---|
452 | gboolean dccs_send_can_write( gpointer data, gint fd, b_input_condition cond ) |
---|
453 | { |
---|
454 | struct dcc_file_transfer *df = data; |
---|
455 | df->watch_out = 0; |
---|
456 | |
---|
457 | df->ft->write_request( df->ft ); |
---|
458 | return FALSE; |
---|
459 | } |
---|
460 | |
---|
461 | /* |
---|
462 | * Incoming data. |
---|
463 | * |
---|
464 | */ |
---|
465 | gboolean dccs_send_write( file_transfer_t *file, char *data, unsigned int data_len ) |
---|
466 | { |
---|
467 | dcc_file_transfer_t *df = file->priv; |
---|
468 | int ret; |
---|
469 | |
---|
470 | receivedchunks++; receiveddata += data_len; |
---|
471 | |
---|
472 | if( df->watch_out ) |
---|
473 | return dcc_abort( df, "BUG: write() called while watching" ); |
---|
474 | |
---|
475 | ASSERTSOCKOP( ret = send( df->fd, data, data_len, 0 ), "Sending data" ); |
---|
476 | |
---|
477 | if( ret == 0 ) |
---|
478 | return dcc_abort( df, "Remote end closed connection" ); |
---|
479 | |
---|
480 | /* TODO: this should really not be fatal */ |
---|
481 | if( ret < data_len ) |
---|
482 | return dcc_abort( df, "send() sent %d instead of %d", ret, data_len ); |
---|
483 | |
---|
484 | if( df->bytes_sent == 0 ) |
---|
485 | file->started = time( NULL ); |
---|
486 | |
---|
487 | df->bytes_sent += ret; |
---|
488 | |
---|
489 | if( df->bytes_sent < df->ft->file_size ) |
---|
490 | df->watch_out = b_input_add( df->fd, GAIM_INPUT_WRITE, dccs_send_can_write, df ); |
---|
491 | |
---|
492 | return TRUE; |
---|
493 | } |
---|
494 | |
---|
495 | /* |
---|
496 | * Cleans up after a transfer. |
---|
497 | */ |
---|
498 | static void dcc_close( file_transfer_t *file ) |
---|
499 | { |
---|
500 | dcc_file_transfer_t *df = file->priv; |
---|
501 | |
---|
502 | if( file->free ) |
---|
503 | file->free( file ); |
---|
504 | |
---|
505 | closesocket( df->fd ); |
---|
506 | |
---|
507 | if( df->watch_in ) |
---|
508 | b_event_remove( df->watch_in ); |
---|
509 | |
---|
510 | if( df->watch_out ) |
---|
511 | b_event_remove( df->watch_out ); |
---|
512 | |
---|
513 | if( df->progress_timeout ) |
---|
514 | b_event_remove( df->progress_timeout ); |
---|
515 | |
---|
516 | df->ic->irc->file_transfers = g_slist_remove( df->ic->irc->file_transfers, file ); |
---|
517 | |
---|
518 | g_free( df ); |
---|
519 | g_free( file->file_name ); |
---|
520 | g_free( file ); |
---|
521 | } |
---|
522 | |
---|
523 | void dcc_finish( file_transfer_t *file ) |
---|
524 | { |
---|
525 | dcc_file_transfer_t *df = file->priv; |
---|
526 | time_t diff = time( NULL ) - file->started ? : 1; |
---|
527 | |
---|
528 | file->status |= FT_STATUS_FINISHED; |
---|
529 | |
---|
530 | if( file->finished ) |
---|
531 | file->finished( file ); |
---|
532 | |
---|
533 | imcb_log( df->ic, "File %s transferred successfully at %d kb/s!" , file->file_name, (int) ( file->bytes_transferred / 1024 / diff ) ); |
---|
534 | dcc_close( file ); |
---|
535 | } |
---|
536 | |
---|
537 | /* |
---|
538 | * DCC SEND <filename> <IP> <port> <filesize> |
---|
539 | * |
---|
540 | * filename can be in "" or not. If it is, " can probably be escaped... |
---|
541 | * IP can be an unsigned int (IPV4) or something else (IPV6) |
---|
542 | * |
---|
543 | */ |
---|
544 | file_transfer_t *dcc_request( struct im_connection *ic, char *line ) |
---|
545 | { |
---|
546 | char *pattern = "SEND" |
---|
547 | " (([^\"][^ ]*)|\"(([^\"]|\\\")*)\")" |
---|
548 | " (([0-9]*)|([^ ]*))" |
---|
549 | " ([0-9]*)" |
---|
550 | " ([0-9]*)\001"; |
---|
551 | regmatch_t pmatch[10]; |
---|
552 | regex_t re; |
---|
553 | file_transfer_t *ft; |
---|
554 | dcc_file_transfer_t *df; |
---|
555 | char errbuf[256]; |
---|
556 | int regerrcode, gret; |
---|
557 | |
---|
558 | if( ( regerrcode = regcomp( &re, pattern, REG_EXTENDED ) ) || |
---|
559 | ( regerrcode = regexec( &re, line, 10, pmatch, 0 ) ) ) { |
---|
560 | regerror( regerrcode,&re,errbuf,sizeof( errbuf ) ); |
---|
561 | imcb_log( ic, |
---|
562 | "DCC: error parsing 'DCC SEND': %s, line: %s", |
---|
563 | errbuf, line ); |
---|
564 | return NULL; |
---|
565 | } |
---|
566 | |
---|
567 | if( ( pmatch[1].rm_so > 0 ) && |
---|
568 | ( pmatch[5].rm_so > 0 ) && |
---|
569 | ( pmatch[8].rm_so > 0 ) && |
---|
570 | ( pmatch[9].rm_so > 0 ) ) |
---|
571 | { |
---|
572 | char *input = g_strdup( line ); |
---|
573 | char *filename, *host, *port; |
---|
574 | size_t filesize; |
---|
575 | struct addrinfo hints, *rp; |
---|
576 | |
---|
577 | /* "filename" or filename */ |
---|
578 | if ( pmatch[2].rm_so > 0 ) |
---|
579 | { |
---|
580 | input[pmatch[2].rm_eo] = '\0'; |
---|
581 | filename = input + pmatch[2].rm_so; |
---|
582 | } else |
---|
583 | { |
---|
584 | input[pmatch[3].rm_eo] = '\0'; |
---|
585 | filename = input + pmatch[3].rm_so; |
---|
586 | } |
---|
587 | |
---|
588 | input[pmatch[5].rm_eo] = '\0'; |
---|
589 | |
---|
590 | /* number means ipv4, something else means ipv6 */ |
---|
591 | if ( pmatch[6].rm_so > 0 ) |
---|
592 | { |
---|
593 | struct in_addr ipaddr = { .s_addr = htonl( strtoul( input + pmatch[5].rm_so, NULL, 10 ) ) }; |
---|
594 | host = inet_ntoa( ipaddr ); |
---|
595 | } else |
---|
596 | { |
---|
597 | /* Contains non-numbers, hopefully an IPV6 address */ |
---|
598 | host = input + pmatch[7].rm_so; |
---|
599 | } |
---|
600 | |
---|
601 | input[pmatch[8].rm_eo] = '\0'; |
---|
602 | input[pmatch[9].rm_eo] = '\0'; |
---|
603 | |
---|
604 | port = input + pmatch[8].rm_so; |
---|
605 | filesize = atoll( input + pmatch[9].rm_so ); |
---|
606 | |
---|
607 | memset( &hints, 0, sizeof ( struct addrinfo ) ); |
---|
608 | hints.ai_socktype = SOCK_STREAM; |
---|
609 | hints.ai_flags = AI_NUMERICSERV; |
---|
610 | |
---|
611 | if ( ( gret = getaddrinfo( host, port, &hints, &rp ) ) ) |
---|
612 | { |
---|
613 | g_free( input ); |
---|
614 | imcb_log( ic, "DCC: getaddrinfo() failed with %s " |
---|
615 | "when parsing incoming 'DCC SEND': " |
---|
616 | "host %s, port %s", |
---|
617 | gai_strerror( gret ), host, port ); |
---|
618 | return NULL; |
---|
619 | } |
---|
620 | |
---|
621 | df = dcc_alloc_transfer( filename, filesize, ic ); |
---|
622 | ft = df->ft; |
---|
623 | ft->sending = TRUE; |
---|
624 | memcpy( &df->saddr, rp->ai_addr, rp->ai_addrlen ); |
---|
625 | |
---|
626 | freeaddrinfo( rp ); |
---|
627 | g_free( input ); |
---|
628 | |
---|
629 | df->ic->irc->file_transfers = g_slist_prepend( df->ic->irc->file_transfers, ft ); |
---|
630 | |
---|
631 | return ft; |
---|
632 | } |
---|
633 | |
---|
634 | imcb_log( ic, "DCC: couldnt parse 'DCC SEND' line: %s", line ); |
---|
635 | |
---|
636 | return NULL; |
---|
637 | } |
---|
638 | |
---|