[2c2df7d] | 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> |
---|
[2ff2076] | 29 | #include <regex.h> |
---|
[a02f34f] | 30 | #include "lib/ftutil.h" |
---|
[4358b10] | 31 | |
---|
[2c2df7d] | 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 ); |
---|
[2ff2076] | 67 | gboolean dccs_recv_start( file_transfer_t *ft ); |
---|
| 68 | gboolean dccs_recv_proto( gpointer data, gint fd, b_input_condition cond); |
---|
[dce3903] | 69 | gboolean dccs_recv_write_request( file_transfer_t *ft ); |
---|
[d56ee38] | 70 | gboolean dcc_progress( gpointer data, gint fd, b_input_condition cond ); |
---|
[a02f34f] | 71 | gboolean dcc_abort( dcc_file_transfer_t *df, char *reason, ... ); |
---|
[2c2df7d] | 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 | |
---|
[2ff2076] | 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 | |
---|
[4ac647d] | 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 | |
---|
[2ff2076] | 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 ); |
---|
[1c3008a] | 114 | dcc_file_transfer_t *df = file->priv = g_new0( dcc_file_transfer_t, 1 ); |
---|
| 115 | |
---|
[2ff2076] | 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 | |
---|
[2c2df7d] | 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; |
---|
[a02f34f] | 130 | struct sockaddr_storage saddr; |
---|
| 131 | char *errmsg; |
---|
[60e4df3] | 132 | char host[HOST_NAME_MAX]; |
---|
[a02f34f] | 133 | char port[6]; |
---|
[2c2df7d] | 134 | |
---|
[a02f34f] | 135 | if( file_size > global.conf->ft_max_size ) |
---|
[2c2df7d] | 136 | return NULL; |
---|
| 137 | |
---|
[2ff2076] | 138 | df = dcc_alloc_transfer( file_name, file_size, ic ); |
---|
| 139 | file = df->ft; |
---|
[dce3903] | 140 | file->write = dccs_send_write; |
---|
[2ff2076] | 141 | |
---|
[2c2df7d] | 142 | /* listen and request */ |
---|
[a02f34f] | 143 | |
---|
[1c3008a] | 144 | if( ( df->fd = ft_listen( &saddr, host, port, TRUE, &errmsg ) ) == -1 ) |
---|
| 145 | { |
---|
[a02f34f] | 146 | dcc_abort( df, "Failed to listen locally, check your ft_listen setting in bitlbee.conf: %s", errmsg ); |
---|
[2c2df7d] | 147 | return NULL; |
---|
[a02f34f] | 148 | } |
---|
[2c2df7d] | 149 | |
---|
[a02f34f] | 150 | file->status = FT_STATUS_LISTENING; |
---|
| 151 | |
---|
| 152 | if( !dccs_send_request( df, user_nick, &saddr ) ) |
---|
| 153 | return NULL; |
---|
[2c2df7d] | 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 | |
---|
[d56ee38] | 160 | df->progress_timeout = b_timeout_add( DCC_MAX_STALL * 1000, dcc_progress, df ); |
---|
| 161 | |
---|
[1c3008a] | 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 ); |
---|
[4ac647d] | 166 | |
---|
[2c2df7d] | 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 ); |
---|
[d56ee38] | 183 | |
---|
| 184 | imcb_log( df->ic, "File %s: DCC transfer aborted: %s", file->file_name, msg ); |
---|
[2c2df7d] | 185 | |
---|
| 186 | g_free( msg ); |
---|
| 187 | |
---|
| 188 | dcc_close( df->ft ); |
---|
| 189 | |
---|
| 190 | return FALSE; |
---|
| 191 | } |
---|
| 192 | |
---|
[d56ee38] | 193 | gboolean dcc_progress( gpointer data, gint fd, b_input_condition cond ) |
---|
| 194 | { |
---|
| 195 | struct dcc_file_transfer *df = data; |
---|
| 196 | |
---|
[b043ad5] | 197 | if( df->bytes_sent == df->progress_bytes_last ) |
---|
[d56ee38] | 198 | { |
---|
| 199 | /* no progress. cancel */ |
---|
| 200 | if( df->bytes_sent == 0 ) |
---|
[1c3008a] | 201 | return dcc_abort( df, "Couldn't establish transfer within %d seconds", DCC_MAX_STALL ); |
---|
[d56ee38] | 202 | else |
---|
[b043ad5] | 203 | return dcc_abort( df, "Transfer stalled for %d seconds at %d kb", DCC_MAX_STALL, df->bytes_sent / 1024 ); |
---|
[d56ee38] | 204 | |
---|
| 205 | } |
---|
| 206 | |
---|
[b043ad5] | 207 | df->progress_bytes_last = df->bytes_sent; |
---|
[d56ee38] | 208 | |
---|
| 209 | return TRUE; |
---|
| 210 | } |
---|
| 211 | |
---|
[2c2df7d] | 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", |
---|
[dce3903] | 230 | ntohl( saddr_ipv4->sin_addr.s_addr ) ); |
---|
[2c2df7d] | 231 | port = saddr_ipv4->sin_port; |
---|
[1c3008a] | 232 | } |
---|
| 233 | else |
---|
[2c2df7d] | 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 ) ) |
---|
[1c3008a] | 253 | return dcc_abort( df, "Couldn't send `DCC SEND' message to %s.", user_nick ); |
---|
[2c2df7d] | 254 | |
---|
| 255 | g_free( cmd ); |
---|
| 256 | |
---|
| 257 | return TRUE; |
---|
| 258 | } |
---|
| 259 | |
---|
[2ff2076] | 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 | |
---|
[2e89256] | 269 | if( ( cond & GAIM_INPUT_READ ) && |
---|
[2c2df7d] | 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; |
---|
[2ff2076] | 281 | file->status = FT_STATUS_TRANSFERRING; |
---|
[2c2df7d] | 282 | sock_make_nonblocking( fd ); |
---|
| 283 | |
---|
| 284 | /* IM protocol callback */ |
---|
| 285 | if( file->accept ) |
---|
| 286 | file->accept( file ); |
---|
[dce3903] | 287 | |
---|
[2c2df7d] | 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 | |
---|
[2e89256] | 294 | if( cond & GAIM_INPUT_READ ) |
---|
[2c2df7d] | 295 | { |
---|
| 296 | int ret; |
---|
| 297 | |
---|
[4ed9c8c] | 298 | ASSERTSOCKOP( ret = recv( fd, ( (char*) &df->acked ) + df->acked_len, |
---|
| 299 | sizeof( df->acked ) - df->acked_len, 0 ), "Receiving" ); |
---|
[2c2df7d] | 300 | |
---|
| 301 | if( ret == 0 ) |
---|
| 302 | return dcc_abort( df, "Remote end closed connection" ); |
---|
[4ed9c8c] | 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 ) |
---|
[2c2df7d] | 307 | return TRUE; |
---|
| 308 | |
---|
[4ed9c8c] | 309 | df->acked = ntohl( df->acked ); |
---|
[2c2df7d] | 310 | |
---|
| 311 | /* If any of this is actually happening, the receiver should buy a new IRC client */ |
---|
| 312 | |
---|
[4ed9c8c] | 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 ); |
---|
[2c2df7d] | 315 | |
---|
[4ed9c8c] | 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 ); |
---|
[2c2df7d] | 318 | |
---|
[4ed9c8c] | 319 | file->bytes_transferred = df->acked; |
---|
[2c2df7d] | 320 | |
---|
| 321 | if( file->bytes_transferred >= file->file_size ) { |
---|
[4ac647d] | 322 | if( df->proto_finished ) |
---|
| 323 | dcc_finish( file ); |
---|
[2c2df7d] | 324 | return FALSE; |
---|
| 325 | } |
---|
| 326 | |
---|
| 327 | return TRUE; |
---|
| 328 | } |
---|
| 329 | |
---|
| 330 | return TRUE; |
---|
| 331 | } |
---|
| 332 | |
---|
[2ff2076] | 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; |
---|
[0cab388] | 338 | char ipaddr[INET6_ADDRSTRLEN]; |
---|
[2ff2076] | 339 | socklen_t sa_len = saddr->ss_family == AF_INET ? |
---|
| 340 | sizeof( struct sockaddr_in ) : sizeof( struct sockaddr_in6 ); |
---|
| 341 | |
---|
| 342 | if( !ft->write ) |
---|
[0cab388] | 343 | return dcc_abort( df, "BUG: protocol didn't register write()" ); |
---|
[2ff2076] | 344 | |
---|
[1c3008a] | 345 | ASSERTSOCKOP( fd = df->fd = socket( saddr->ss_family, SOCK_STREAM, 0 ), "Opening Socket" ); |
---|
[2ff2076] | 346 | |
---|
| 347 | sock_make_nonblocking( fd ); |
---|
| 348 | |
---|
| 349 | if( ( connect( fd, (struct sockaddr *)saddr, sa_len ) == -1 ) && |
---|
| 350 | ( errno != EINPROGRESS ) ) |
---|
[0cab388] | 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 ) ); |
---|
[2ff2076] | 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 ); |
---|
[dce3903] | 367 | ft->write_request = dccs_recv_write_request; |
---|
[2ff2076] | 368 | |
---|
[d56ee38] | 369 | df->progress_timeout = b_timeout_add( DCC_MAX_STALL * 1000, dcc_progress, df ); |
---|
| 370 | |
---|
[2ff2076] | 371 | return TRUE; |
---|
| 372 | } |
---|
| 373 | |
---|
[1c3008a] | 374 | gboolean dccs_recv_proto( gpointer data, gint fd, b_input_condition cond ) |
---|
[2ff2076] | 375 | { |
---|
| 376 | dcc_file_transfer_t *df = data; |
---|
| 377 | file_transfer_t *ft = df->ft; |
---|
| 378 | |
---|
[2e89256] | 379 | if( ( cond & GAIM_INPUT_WRITE ) && |
---|
[2ff2076] | 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 | |
---|
[2e89256] | 390 | if( cond & GAIM_INPUT_READ ) |
---|
[2ff2076] | 391 | { |
---|
| 392 | int ret, done; |
---|
| 393 | |
---|
[dce3903] | 394 | ASSERTSOCKOP( ret = recv( fd, ft->buffer, sizeof( ft->buffer ), 0 ), "Receiving" ); |
---|
[2ff2076] | 395 | |
---|
| 396 | if( ret == 0 ) |
---|
| 397 | return dcc_abort( df, "Remote end closed connection" ); |
---|
| 398 | |
---|
[4ac647d] | 399 | if( !ft->write( df->ft, ft->buffer, ret ) ) |
---|
| 400 | return FALSE; |
---|
| 401 | |
---|
[2ff2076] | 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 | { |
---|
[4ed9c8c] | 409 | guint32 ack = htonl( ft->bytes_transferred = df->bytes_sent ); |
---|
| 410 | int ackret; |
---|
[2ff2076] | 411 | |
---|
| 412 | ASSERTSOCKOP( ackret = send( fd, &ack, 4, 0 ), "Sending DCC ACK" ); |
---|
| 413 | |
---|
| 414 | if ( ackret != 4 ) |
---|
[dce3903] | 415 | return dcc_abort( df, "Error sending DCC ACK, sent %d instead of 4 bytes", ackret ); |
---|
[2ff2076] | 416 | } |
---|
| 417 | |
---|
[4ac647d] | 418 | if( df->bytes_sent == ret ) |
---|
| 419 | ft->started = time( NULL ); |
---|
[2ff2076] | 420 | |
---|
| 421 | if( done ) |
---|
| 422 | { |
---|
[4ac647d] | 423 | if( df->watch_out ) |
---|
| 424 | b_event_remove( df->watch_out ); |
---|
| 425 | |
---|
| 426 | if( df->proto_finished ) |
---|
| 427 | dcc_finish( ft ); |
---|
[2ff2076] | 428 | |
---|
| 429 | df->watch_in = 0; |
---|
| 430 | return FALSE; |
---|
| 431 | } |
---|
| 432 | |
---|
[dce3903] | 433 | df->watch_in = 0; |
---|
| 434 | return FALSE; |
---|
[2ff2076] | 435 | } |
---|
| 436 | |
---|
| 437 | return TRUE; |
---|
| 438 | } |
---|
| 439 | |
---|
[dce3903] | 440 | gboolean dccs_recv_write_request( file_transfer_t *ft ) |
---|
[2ff2076] | 441 | { |
---|
| 442 | dcc_file_transfer_t *df = ft->priv; |
---|
| 443 | |
---|
[dce3903] | 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; |
---|
[2ff2076] | 459 | } |
---|
| 460 | |
---|
[2c2df7d] | 461 | /* |
---|
[dce3903] | 462 | * Incoming data. |
---|
[2c2df7d] | 463 | * |
---|
[dce3903] | 464 | */ |
---|
| 465 | gboolean dccs_send_write( file_transfer_t *file, char *data, unsigned int data_len ) |
---|
[2c2df7d] | 466 | { |
---|
| 467 | dcc_file_transfer_t *df = file->priv; |
---|
[dce3903] | 468 | int ret; |
---|
| 469 | |
---|
| 470 | receivedchunks++; receiveddata += data_len; |
---|
[2c2df7d] | 471 | |
---|
[dce3903] | 472 | if( df->watch_out ) |
---|
| 473 | return dcc_abort( df, "BUG: write() called while watching" ); |
---|
[2c2df7d] | 474 | |
---|
[dce3903] | 475 | ASSERTSOCKOP( ret = send( df->fd, data, data_len, 0 ), "Sending data" ); |
---|
[2c2df7d] | 476 | |
---|
[dce3903] | 477 | if( ret == 0 ) |
---|
| 478 | return dcc_abort( df, "Remote end closed connection" ); |
---|
[2c2df7d] | 479 | |
---|
[dce3903] | 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 ); |
---|
[2c2df7d] | 483 | |
---|
[4ac647d] | 484 | if( df->bytes_sent == 0 ) |
---|
| 485 | file->started = time( NULL ); |
---|
| 486 | |
---|
[dce3903] | 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; |
---|
[2c2df7d] | 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 | |
---|
[d56ee38] | 513 | if( df->progress_timeout ) |
---|
| 514 | b_event_remove( df->progress_timeout ); |
---|
| 515 | |
---|
[2c2df7d] | 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 | { |
---|
[4ac647d] | 525 | dcc_file_transfer_t *df = file->priv; |
---|
| 526 | time_t diff = time( NULL ) - file->started ? : 1; |
---|
| 527 | |
---|
[2c2df7d] | 528 | file->status |= FT_STATUS_FINISHED; |
---|
| 529 | |
---|
| 530 | if( file->finished ) |
---|
| 531 | file->finished( file ); |
---|
| 532 | |
---|
[4ac647d] | 533 | imcb_log( df->ic, "File %s transferred successfully at %d kb/s!" , file->file_name, (int) ( file->bytes_transferred / 1024 / diff ) ); |
---|
[2c2df7d] | 534 | dcc_close( file ); |
---|
| 535 | } |
---|
[2ff2076] | 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" |
---|
[9afeefa] | 547 | " (([^\"][^ ]*)|\"(([^\"]|\\\")*)\")" |
---|
[2ff2076] | 548 | " (([0-9]*)|([^ ]*))" |
---|
| 549 | " ([0-9]*)" |
---|
| 550 | " ([0-9]*)\001"; |
---|
[9afeefa] | 551 | regmatch_t pmatch[10]; |
---|
[2ff2076] | 552 | regex_t re; |
---|
| 553 | file_transfer_t *ft; |
---|
| 554 | dcc_file_transfer_t *df; |
---|
[6cac643] | 555 | char errbuf[256]; |
---|
| 556 | int regerrcode, gret; |
---|
| 557 | |
---|
| 558 | if( ( regerrcode = regcomp( &re, pattern, REG_EXTENDED ) ) || |
---|
[9afeefa] | 559 | ( regerrcode = regexec( &re, line, 10, pmatch, 0 ) ) ) { |
---|
[6cac643] | 560 | regerror( regerrcode,&re,errbuf,sizeof( errbuf ) ); |
---|
| 561 | imcb_log( ic, |
---|
| 562 | "DCC: error parsing 'DCC SEND': %s, line: %s", |
---|
| 563 | errbuf, line ); |
---|
[2ff2076] | 564 | return NULL; |
---|
[6cac643] | 565 | } |
---|
[2ff2076] | 566 | |
---|
| 567 | if( ( pmatch[1].rm_so > 0 ) && |
---|
[9afeefa] | 568 | ( pmatch[5].rm_so > 0 ) && |
---|
| 569 | ( pmatch[8].rm_so > 0 ) && |
---|
| 570 | ( pmatch[9].rm_so > 0 ) ) |
---|
[2ff2076] | 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 | |
---|
[9afeefa] | 588 | input[pmatch[5].rm_eo] = '\0'; |
---|
[2ff2076] | 589 | |
---|
| 590 | /* number means ipv4, something else means ipv6 */ |
---|
[9afeefa] | 591 | if ( pmatch[6].rm_so > 0 ) |
---|
[2ff2076] | 592 | { |
---|
[92f4ec5] | 593 | struct in_addr ipaddr = { .s_addr = htonl( strtoul( input + pmatch[5].rm_so, NULL, 10 ) ) }; |
---|
[2ff2076] | 594 | host = inet_ntoa( ipaddr ); |
---|
| 595 | } else |
---|
| 596 | { |
---|
| 597 | /* Contains non-numbers, hopefully an IPV6 address */ |
---|
[9afeefa] | 598 | host = input + pmatch[7].rm_so; |
---|
[2ff2076] | 599 | } |
---|
| 600 | |
---|
| 601 | input[pmatch[8].rm_eo] = '\0'; |
---|
[9afeefa] | 602 | input[pmatch[9].rm_eo] = '\0'; |
---|
[2ff2076] | 603 | |
---|
[9afeefa] | 604 | port = input + pmatch[8].rm_so; |
---|
| 605 | filesize = atoll( input + pmatch[9].rm_so ); |
---|
[2ff2076] | 606 | |
---|
| 607 | memset( &hints, 0, sizeof ( struct addrinfo ) ); |
---|
[aac4017] | 608 | hints.ai_socktype = SOCK_STREAM; |
---|
| 609 | hints.ai_flags = AI_NUMERICSERV; |
---|
| 610 | |
---|
[6cac643] | 611 | if ( ( gret = getaddrinfo( host, port, &hints, &rp ) ) ) |
---|
[2ff2076] | 612 | { |
---|
| 613 | g_free( input ); |
---|
[6cac643] | 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 ); |
---|
[2ff2076] | 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 | |
---|
[6cac643] | 634 | imcb_log( ic, "DCC: couldnt parse 'DCC SEND' line: %s", line ); |
---|
| 635 | |
---|
[2ff2076] | 636 | return NULL; |
---|
| 637 | } |
---|
| 638 | |
---|