[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 <poll.h> |
---|
| 29 | #include <netinet/tcp.h> |
---|
[2ff2076] | 30 | #include <regex.h> |
---|
[2c2df7d] | 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 | int max_packet_size = 0; |
---|
| 64 | |
---|
| 65 | static void dcc_finish( file_transfer_t *file ); |
---|
| 66 | static void dcc_close( file_transfer_t *file ); |
---|
| 67 | gboolean dccs_send_proto( gpointer data, gint fd, b_input_condition cond ); |
---|
| 68 | gboolean dcc_listen( dcc_file_transfer_t *df, struct sockaddr_storage **saddr_ptr ); |
---|
| 69 | int dccs_send_request( struct dcc_file_transfer *df, char *user_nick, struct sockaddr_storage *saddr ); |
---|
[2ff2076] | 70 | gboolean dccs_recv_start( file_transfer_t *ft ); |
---|
| 71 | gboolean dccs_recv_proto( gpointer data, gint fd, b_input_condition cond); |
---|
[dce3903] | 72 | gboolean dccs_recv_write_request( file_transfer_t *ft ); |
---|
[2c2df7d] | 73 | |
---|
| 74 | /* As defined in ft.h */ |
---|
| 75 | file_transfer_t *imcb_file_send_start( struct im_connection *ic, char *handle, char *file_name, size_t file_size ) |
---|
| 76 | { |
---|
| 77 | user_t *u = user_findhandle( ic, handle ); |
---|
| 78 | /* one could handle this more intelligent like imcb_buddy_msg. |
---|
| 79 | * can't call it directly though cause it does some wrapping. |
---|
| 80 | * Maybe give imcb_buddy_msg a parameter NO_WRAPPING? */ |
---|
| 81 | if (!u) return NULL; |
---|
| 82 | |
---|
| 83 | return dccs_send_start( ic, u->nick, file_name, file_size ); |
---|
| 84 | }; |
---|
| 85 | |
---|
| 86 | /* As defined in ft.h */ |
---|
| 87 | void imcb_file_canceled( file_transfer_t *file, char *reason ) |
---|
| 88 | { |
---|
| 89 | if( file->canceled ) |
---|
| 90 | file->canceled( file, reason ); |
---|
| 91 | |
---|
| 92 | dcc_close( file ); |
---|
| 93 | } |
---|
| 94 | |
---|
[2ff2076] | 95 | /* As defined in ft.h */ |
---|
| 96 | gboolean imcb_file_recv_start( file_transfer_t *ft ) |
---|
| 97 | { |
---|
| 98 | return dccs_recv_start( ft ); |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | dcc_file_transfer_t *dcc_alloc_transfer( char *file_name, size_t file_size, struct im_connection *ic ) |
---|
| 102 | { |
---|
| 103 | file_transfer_t *file = g_new0( file_transfer_t, 1 ); |
---|
| 104 | dcc_file_transfer_t *df = file->priv = g_new0( dcc_file_transfer_t, 1); |
---|
| 105 | file->file_size = file_size; |
---|
| 106 | file->file_name = g_strdup( file_name ); |
---|
| 107 | file->local_id = local_transfer_id++; |
---|
| 108 | df->ic = ic; |
---|
| 109 | df->ft = file; |
---|
| 110 | |
---|
| 111 | return df; |
---|
| 112 | } |
---|
| 113 | |
---|
[2c2df7d] | 114 | /* This is where the sending magic starts... */ |
---|
| 115 | file_transfer_t *dccs_send_start( struct im_connection *ic, char *user_nick, char *file_name, size_t file_size ) |
---|
| 116 | { |
---|
| 117 | file_transfer_t *file; |
---|
| 118 | dcc_file_transfer_t *df; |
---|
[2ff2076] | 119 | struct sockaddr_storage *saddr; |
---|
[2c2df7d] | 120 | |
---|
| 121 | if( file_size > global.conf->max_filetransfer_size ) |
---|
| 122 | return NULL; |
---|
| 123 | |
---|
[2ff2076] | 124 | df = dcc_alloc_transfer( file_name, file_size, ic ); |
---|
| 125 | file = df->ft; |
---|
[dce3903] | 126 | file->write = dccs_send_write; |
---|
| 127 | file->sending = TRUE; |
---|
[2ff2076] | 128 | |
---|
[2c2df7d] | 129 | /* listen and request */ |
---|
[2ff2076] | 130 | if( !dcc_listen( df, &saddr ) || |
---|
| 131 | !dccs_send_request( df, user_nick, saddr ) ) |
---|
[2c2df7d] | 132 | return NULL; |
---|
| 133 | |
---|
[2ff2076] | 134 | g_free( saddr ); |
---|
[2c2df7d] | 135 | |
---|
| 136 | /* watch */ |
---|
| 137 | df->watch_in = b_input_add( df->fd, GAIM_INPUT_READ, dccs_send_proto, df ); |
---|
| 138 | |
---|
| 139 | df->ic->irc->file_transfers = g_slist_prepend( df->ic->irc->file_transfers, file ); |
---|
| 140 | |
---|
| 141 | return file; |
---|
| 142 | } |
---|
| 143 | |
---|
| 144 | /* Used pretty much everywhere in the code to abort a transfer */ |
---|
| 145 | gboolean dcc_abort( dcc_file_transfer_t *df, char *reason, ... ) |
---|
| 146 | { |
---|
| 147 | file_transfer_t *file = df->ft; |
---|
| 148 | va_list params; |
---|
| 149 | va_start( params, reason ); |
---|
| 150 | char *msg = g_strdup_vprintf( reason, params ); |
---|
| 151 | va_end( params ); |
---|
| 152 | |
---|
| 153 | file->status |= FT_STATUS_CANCELED; |
---|
| 154 | |
---|
| 155 | if( file->canceled ) |
---|
| 156 | file->canceled( file, msg ); |
---|
| 157 | else |
---|
| 158 | imcb_log( df->ic, "DCC transfer aborted: %s", msg ); |
---|
| 159 | |
---|
| 160 | g_free( msg ); |
---|
| 161 | |
---|
| 162 | dcc_close( df->ft ); |
---|
| 163 | |
---|
| 164 | return FALSE; |
---|
| 165 | } |
---|
| 166 | |
---|
| 167 | /* used extensively for socket operations */ |
---|
| 168 | #define ASSERTSOCKOP(op, msg) \ |
---|
| 169 | if( (op) == -1 ) \ |
---|
| 170 | return dcc_abort( df , msg ": %s", strerror( errno ) ); |
---|
| 171 | |
---|
| 172 | /* Creates the "DCC SEND" line and sends it to the server */ |
---|
| 173 | int dccs_send_request( struct dcc_file_transfer *df, char *user_nick, struct sockaddr_storage *saddr ) |
---|
| 174 | { |
---|
| 175 | char ipaddr[INET6_ADDRSTRLEN]; |
---|
| 176 | const void *netaddr; |
---|
| 177 | int port; |
---|
| 178 | char *cmd; |
---|
| 179 | |
---|
| 180 | if( saddr->ss_family == AF_INET ) |
---|
| 181 | { |
---|
| 182 | struct sockaddr_in *saddr_ipv4 = ( struct sockaddr_in *) saddr; |
---|
| 183 | |
---|
| 184 | sprintf( ipaddr, "%d", |
---|
[dce3903] | 185 | ntohl( saddr_ipv4->sin_addr.s_addr ) ); |
---|
[2c2df7d] | 186 | port = saddr_ipv4->sin_port; |
---|
| 187 | } else |
---|
| 188 | { |
---|
| 189 | struct sockaddr_in6 *saddr_ipv6 = ( struct sockaddr_in6 *) saddr; |
---|
| 190 | |
---|
| 191 | netaddr = &saddr_ipv6->sin6_addr.s6_addr; |
---|
| 192 | port = saddr_ipv6->sin6_port; |
---|
| 193 | |
---|
| 194 | /* |
---|
| 195 | * Didn't find docs about this, but it seems that's the way irssi does it |
---|
| 196 | */ |
---|
| 197 | if( !inet_ntop( saddr->ss_family, netaddr, ipaddr, sizeof( ipaddr ) ) ) |
---|
| 198 | return dcc_abort( df, "inet_ntop failed: %s", strerror( errno ) ); |
---|
| 199 | } |
---|
| 200 | |
---|
| 201 | port = ntohs( port ); |
---|
| 202 | |
---|
| 203 | cmd = g_strdup_printf( "\001DCC SEND %s %s %u %zu\001", |
---|
| 204 | df->ft->file_name, ipaddr, port, df->ft->file_size ); |
---|
| 205 | |
---|
| 206 | if ( !irc_msgfrom( df->ic->irc, user_nick, cmd ) ) |
---|
| 207 | return dcc_abort( df, "couldn't send 'DCC SEND' message to %s", user_nick ); |
---|
| 208 | |
---|
| 209 | g_free( cmd ); |
---|
| 210 | |
---|
| 211 | return TRUE; |
---|
| 212 | } |
---|
| 213 | |
---|
| 214 | /* |
---|
| 215 | * Creates a listening socket and returns it in saddr_ptr. |
---|
| 216 | */ |
---|
| 217 | gboolean dcc_listen( dcc_file_transfer_t *df, struct sockaddr_storage **saddr_ptr ) |
---|
| 218 | { |
---|
| 219 | file_transfer_t *file = df->ft; |
---|
| 220 | struct sockaddr_storage *saddr; |
---|
| 221 | int fd; |
---|
| 222 | char hostname[ HOST_NAME_MAX + 1 ]; |
---|
| 223 | struct addrinfo hints, *rp; |
---|
| 224 | socklen_t ssize = sizeof( struct sockaddr_storage ); |
---|
| 225 | |
---|
| 226 | /* won't be long till someone asks for this to be configurable :) */ |
---|
| 227 | |
---|
| 228 | ASSERTSOCKOP( gethostname( hostname, sizeof( hostname ) ), "gethostname()" ); |
---|
| 229 | |
---|
| 230 | memset( &hints, 0, sizeof( struct addrinfo ) ); |
---|
| 231 | hints.ai_socktype = SOCK_STREAM; |
---|
| 232 | hints.ai_flags = AI_NUMERICSERV; |
---|
| 233 | |
---|
| 234 | if ( getaddrinfo( hostname, "0", &hints, &rp ) != 0 ) |
---|
| 235 | return dcc_abort( df, "getaddrinfo()" ); |
---|
| 236 | |
---|
| 237 | saddr = g_new( struct sockaddr_storage, 1 ); |
---|
| 238 | |
---|
| 239 | *saddr_ptr = saddr; |
---|
| 240 | |
---|
| 241 | memcpy( saddr, rp->ai_addr, rp->ai_addrlen ); |
---|
| 242 | |
---|
| 243 | ASSERTSOCKOP( fd = df->fd = socket( saddr->ss_family, SOCK_STREAM, 0 ), "Opening socket" ); |
---|
| 244 | |
---|
| 245 | ASSERTSOCKOP( bind( fd, ( struct sockaddr *)saddr, rp->ai_addrlen ), "Binding socket" ); |
---|
| 246 | |
---|
| 247 | freeaddrinfo( rp ); |
---|
| 248 | |
---|
| 249 | ASSERTSOCKOP( getsockname( fd, ( struct sockaddr *)saddr, &ssize ), "Getting socket name" ); |
---|
| 250 | |
---|
| 251 | ASSERTSOCKOP( listen( fd, 1 ), "Making socket listen" ); |
---|
| 252 | |
---|
| 253 | file->status = FT_STATUS_LISTENING; |
---|
| 254 | |
---|
| 255 | return TRUE; |
---|
| 256 | } |
---|
| 257 | |
---|
| 258 | /* |
---|
[2ff2076] | 259 | * Checks poll(), same for receiving and sending |
---|
[2c2df7d] | 260 | */ |
---|
[2ff2076] | 261 | gboolean dcc_poll( dcc_file_transfer_t *df, int fd, short *revents ) |
---|
[2c2df7d] | 262 | { |
---|
| 263 | struct pollfd pfd = { .fd = fd, .events = POLLHUP|POLLERR|POLLIN|POLLOUT }; |
---|
| 264 | |
---|
[2ff2076] | 265 | ASSERTSOCKOP( poll( &pfd, 1, 0 ), "poll()" ) |
---|
[2c2df7d] | 266 | |
---|
[2ff2076] | 267 | if( pfd.revents & POLLERR ) |
---|
[2c2df7d] | 268 | { |
---|
| 269 | int sockerror; |
---|
| 270 | socklen_t errlen = sizeof( sockerror ); |
---|
| 271 | |
---|
| 272 | if ( getsockopt( fd, SOL_SOCKET, SO_ERROR, &sockerror, &errlen ) ) |
---|
| 273 | return dcc_abort( df, "getsockopt() failed, unknown socket error (weird!)" ); |
---|
| 274 | |
---|
| 275 | return dcc_abort( df, "Socket error: %s", strerror( sockerror ) ); |
---|
| 276 | } |
---|
| 277 | |
---|
[2ff2076] | 278 | if( pfd.revents & POLLHUP ) |
---|
[2c2df7d] | 279 | return dcc_abort( df, "Remote end closed connection" ); |
---|
| 280 | |
---|
[2ff2076] | 281 | *revents = pfd.revents; |
---|
| 282 | |
---|
| 283 | return TRUE; |
---|
| 284 | } |
---|
| 285 | |
---|
[dce3903] | 286 | /* |
---|
| 287 | * fills max_packet_size with twice the TCP maximum segment size |
---|
| 288 | */ |
---|
[2ff2076] | 289 | gboolean dcc_check_maxseg( dcc_file_transfer_t *df, int fd ) |
---|
| 290 | { |
---|
| 291 | /* |
---|
| 292 | * use twice the maximum segment size as a maximum for calls to send(). |
---|
| 293 | */ |
---|
| 294 | if( max_packet_size == 0 ) |
---|
| 295 | { |
---|
| 296 | unsigned int mpslen = sizeof( max_packet_size ); |
---|
| 297 | if( getsockopt( fd, IPPROTO_TCP, TCP_MAXSEG, &max_packet_size, &mpslen ) ) |
---|
| 298 | return dcc_abort( df, "getsockopt() failed" ); |
---|
| 299 | max_packet_size *= 2; |
---|
| 300 | } |
---|
| 301 | return TRUE; |
---|
| 302 | } |
---|
| 303 | |
---|
| 304 | /* |
---|
| 305 | * After setup, the transfer itself is handled entirely by this function. |
---|
| 306 | * There are basically four things to handle: connect, receive, send, and error. |
---|
| 307 | */ |
---|
| 308 | gboolean dccs_send_proto( gpointer data, gint fd, b_input_condition cond ) |
---|
| 309 | { |
---|
| 310 | dcc_file_transfer_t *df = data; |
---|
| 311 | file_transfer_t *file = df->ft; |
---|
| 312 | short revents; |
---|
| 313 | |
---|
| 314 | if( !dcc_poll( df, fd, &revents) ) |
---|
| 315 | return FALSE; |
---|
| 316 | |
---|
[2c2df7d] | 317 | if( ( revents & POLLIN ) && |
---|
| 318 | ( file->status & FT_STATUS_LISTENING ) ) |
---|
| 319 | { |
---|
| 320 | struct sockaddr *clt_addr; |
---|
| 321 | socklen_t ssize = sizeof( clt_addr ); |
---|
| 322 | |
---|
| 323 | /* Connect */ |
---|
| 324 | |
---|
| 325 | ASSERTSOCKOP( df->fd = accept( fd, (struct sockaddr *) &clt_addr, &ssize ), "Accepting connection" ); |
---|
| 326 | |
---|
| 327 | closesocket( fd ); |
---|
| 328 | fd = df->fd; |
---|
[2ff2076] | 329 | file->status = FT_STATUS_TRANSFERRING; |
---|
[2c2df7d] | 330 | sock_make_nonblocking( fd ); |
---|
| 331 | |
---|
[2ff2076] | 332 | if ( !dcc_check_maxseg( df, fd ) ) |
---|
| 333 | return FALSE; |
---|
| 334 | |
---|
[2c2df7d] | 335 | /* IM protocol callback */ |
---|
| 336 | if( file->accept ) |
---|
| 337 | file->accept( file ); |
---|
[dce3903] | 338 | |
---|
[2c2df7d] | 339 | /* reschedule for reading on new fd */ |
---|
| 340 | df->watch_in = b_input_add( fd, GAIM_INPUT_READ, dccs_send_proto, df ); |
---|
| 341 | |
---|
| 342 | return FALSE; |
---|
| 343 | } |
---|
| 344 | |
---|
| 345 | if( revents & POLLIN ) |
---|
| 346 | { |
---|
| 347 | int bytes_received; |
---|
| 348 | int ret; |
---|
| 349 | |
---|
| 350 | ASSERTSOCKOP( ret = recv( fd, &bytes_received, sizeof( bytes_received ), MSG_PEEK ), "Receiving" ); |
---|
| 351 | |
---|
| 352 | if( ret == 0 ) |
---|
| 353 | return dcc_abort( df, "Remote end closed connection" ); |
---|
| 354 | |
---|
| 355 | if( ret < 4 ) |
---|
| 356 | { |
---|
| 357 | imcb_log( df->ic, "WARNING: DCC SEND: receiver sent only 2 bytes instead of 4, shouldn't happen too often!" ); |
---|
| 358 | return TRUE; |
---|
| 359 | } |
---|
| 360 | |
---|
| 361 | ASSERTSOCKOP( ret = recv( fd, &bytes_received, sizeof( bytes_received ), 0 ), "Receiving" ); |
---|
| 362 | if( ret != 4 ) |
---|
| 363 | return dcc_abort( df, "MSG_PEEK'ed 4, but can only dequeue %d bytes", ret ); |
---|
| 364 | |
---|
| 365 | bytes_received = ntohl( bytes_received ); |
---|
| 366 | |
---|
| 367 | /* If any of this is actually happening, the receiver should buy a new IRC client */ |
---|
| 368 | |
---|
| 369 | if ( bytes_received > df->bytes_sent ) |
---|
| 370 | return dcc_abort( df, "Receiver magically received more bytes than sent ( %d > %d ) (BUG at receiver?)", bytes_received, df->bytes_sent ); |
---|
| 371 | |
---|
| 372 | if ( bytes_received < file->bytes_transferred ) |
---|
| 373 | return dcc_abort( df, "Receiver lost bytes? ( has %d, had %d ) (BUG at receiver?)", bytes_received, file->bytes_transferred ); |
---|
| 374 | |
---|
| 375 | file->bytes_transferred = bytes_received; |
---|
| 376 | |
---|
| 377 | if( file->bytes_transferred >= file->file_size ) { |
---|
| 378 | dcc_finish( file ); |
---|
| 379 | return FALSE; |
---|
| 380 | } |
---|
| 381 | |
---|
| 382 | return TRUE; |
---|
| 383 | } |
---|
| 384 | |
---|
| 385 | return TRUE; |
---|
| 386 | } |
---|
| 387 | |
---|
[2ff2076] | 388 | gboolean dccs_recv_start( file_transfer_t *ft ) |
---|
| 389 | { |
---|
| 390 | dcc_file_transfer_t *df = ft->priv; |
---|
| 391 | struct sockaddr_storage *saddr = &df->saddr; |
---|
| 392 | int fd; |
---|
| 393 | socklen_t sa_len = saddr->ss_family == AF_INET ? |
---|
| 394 | sizeof( struct sockaddr_in ) : sizeof( struct sockaddr_in6 ); |
---|
| 395 | |
---|
| 396 | if( !ft->write ) |
---|
| 397 | return dcc_abort( df, "Protocol didn't register write()" ); |
---|
| 398 | |
---|
| 399 | ASSERTSOCKOP( fd = df->fd = socket( saddr->ss_family, SOCK_STREAM, 0 ) , "Opening Socket" ); |
---|
| 400 | |
---|
| 401 | sock_make_nonblocking( fd ); |
---|
| 402 | |
---|
| 403 | if( ( connect( fd, (struct sockaddr *)saddr, sa_len ) == -1 ) && |
---|
| 404 | ( errno != EINPROGRESS ) ) |
---|
| 405 | return dcc_abort( df, "Connecting" ); |
---|
| 406 | |
---|
| 407 | ft->status = FT_STATUS_CONNECTING; |
---|
| 408 | |
---|
| 409 | /* watch */ |
---|
| 410 | df->watch_out = b_input_add( df->fd, GAIM_INPUT_WRITE, dccs_recv_proto, df ); |
---|
[dce3903] | 411 | ft->write_request = dccs_recv_write_request; |
---|
[2ff2076] | 412 | |
---|
| 413 | return TRUE; |
---|
| 414 | } |
---|
| 415 | |
---|
| 416 | gboolean dccs_recv_proto( gpointer data, gint fd, b_input_condition cond) |
---|
| 417 | { |
---|
| 418 | dcc_file_transfer_t *df = data; |
---|
| 419 | file_transfer_t *ft = df->ft; |
---|
| 420 | short revents; |
---|
| 421 | |
---|
| 422 | if( !dcc_poll( df, fd, &revents ) ) |
---|
| 423 | return FALSE; |
---|
| 424 | |
---|
| 425 | if( ( revents & POLLOUT ) && |
---|
| 426 | ( ft->status & FT_STATUS_CONNECTING ) ) |
---|
| 427 | { |
---|
| 428 | ft->status = FT_STATUS_TRANSFERRING; |
---|
| 429 | if ( !dcc_check_maxseg( df, fd ) ) |
---|
| 430 | return FALSE; |
---|
| 431 | |
---|
| 432 | //df->watch_in = b_input_add( df->fd, GAIM_INPUT_READ, dccs_recv_proto, df ); |
---|
| 433 | |
---|
| 434 | df->watch_out = 0; |
---|
| 435 | return FALSE; |
---|
| 436 | } |
---|
| 437 | |
---|
| 438 | if( revents & POLLIN ) |
---|
| 439 | { |
---|
| 440 | int ret, done; |
---|
| 441 | |
---|
[dce3903] | 442 | ASSERTSOCKOP( ret = recv( fd, ft->buffer, sizeof( ft->buffer ), 0 ), "Receiving" ); |
---|
[2ff2076] | 443 | |
---|
| 444 | if( ret == 0 ) |
---|
| 445 | return dcc_abort( df, "Remote end closed connection" ); |
---|
| 446 | |
---|
| 447 | df->bytes_sent += ret; |
---|
| 448 | |
---|
| 449 | done = df->bytes_sent >= ft->file_size; |
---|
| 450 | |
---|
| 451 | if( ( ( df->bytes_sent - ft->bytes_transferred ) > DCC_PACKET_SIZE ) || |
---|
| 452 | done ) |
---|
| 453 | { |
---|
| 454 | int ack, ackret; |
---|
| 455 | ack = htonl( ft->bytes_transferred = df->bytes_sent ); |
---|
| 456 | |
---|
| 457 | ASSERTSOCKOP( ackret = send( fd, &ack, 4, 0 ), "Sending DCC ACK" ); |
---|
| 458 | |
---|
| 459 | if ( ackret != 4 ) |
---|
[dce3903] | 460 | return dcc_abort( df, "Error sending DCC ACK, sent %d instead of 4 bytes", ackret ); |
---|
[2ff2076] | 461 | } |
---|
| 462 | |
---|
[dce3903] | 463 | if( !ft->write( df->ft, ft->buffer, ret ) ) |
---|
[2ff2076] | 464 | return FALSE; |
---|
| 465 | |
---|
| 466 | if( done ) |
---|
| 467 | { |
---|
| 468 | closesocket( fd ); |
---|
| 469 | dcc_finish( ft ); |
---|
| 470 | |
---|
| 471 | df->watch_in = 0; |
---|
| 472 | return FALSE; |
---|
| 473 | } |
---|
| 474 | |
---|
[dce3903] | 475 | df->watch_in = 0; |
---|
| 476 | return FALSE; |
---|
[2ff2076] | 477 | } |
---|
| 478 | |
---|
| 479 | return TRUE; |
---|
| 480 | } |
---|
| 481 | |
---|
[dce3903] | 482 | gboolean dccs_recv_write_request( file_transfer_t *ft ) |
---|
[2ff2076] | 483 | { |
---|
| 484 | dcc_file_transfer_t *df = ft->priv; |
---|
| 485 | |
---|
[dce3903] | 486 | if( df->watch_in ) |
---|
| 487 | return dcc_abort( df, "BUG: write_request() called while watching" ); |
---|
| 488 | |
---|
| 489 | df->watch_in = b_input_add( df->fd, GAIM_INPUT_READ, dccs_recv_proto, df ); |
---|
| 490 | |
---|
| 491 | return TRUE; |
---|
| 492 | } |
---|
| 493 | |
---|
| 494 | gboolean dccs_send_can_write( gpointer data, gint fd, b_input_condition cond ) |
---|
| 495 | { |
---|
| 496 | struct dcc_file_transfer *df = data; |
---|
| 497 | df->watch_out = 0; |
---|
| 498 | |
---|
| 499 | df->ft->write_request( df->ft ); |
---|
| 500 | return FALSE; |
---|
[2ff2076] | 501 | } |
---|
| 502 | |
---|
[2c2df7d] | 503 | /* |
---|
[dce3903] | 504 | * Incoming data. |
---|
[2c2df7d] | 505 | * |
---|
[dce3903] | 506 | */ |
---|
| 507 | gboolean dccs_send_write( file_transfer_t *file, char *data, unsigned int data_len ) |
---|
[2c2df7d] | 508 | { |
---|
| 509 | dcc_file_transfer_t *df = file->priv; |
---|
[dce3903] | 510 | int ret; |
---|
| 511 | |
---|
| 512 | receivedchunks++; receiveddata += data_len; |
---|
[2c2df7d] | 513 | |
---|
[dce3903] | 514 | if( df->watch_out ) |
---|
| 515 | return dcc_abort( df, "BUG: write() called while watching" ); |
---|
[2c2df7d] | 516 | |
---|
[dce3903] | 517 | ASSERTSOCKOP( ret = send( df->fd, data, data_len, 0 ), "Sending data" ); |
---|
[2c2df7d] | 518 | |
---|
[dce3903] | 519 | if( ret == 0 ) |
---|
| 520 | return dcc_abort( df, "Remote end closed connection" ); |
---|
[2c2df7d] | 521 | |
---|
[dce3903] | 522 | /* TODO: this should really not be fatal */ |
---|
| 523 | if( ret < data_len ) |
---|
| 524 | return dcc_abort( df, "send() sent %d instead of %d", ret, data_len ); |
---|
[2c2df7d] | 525 | |
---|
[dce3903] | 526 | df->bytes_sent += ret; |
---|
| 527 | |
---|
| 528 | if( df->bytes_sent < df->ft->file_size ) |
---|
| 529 | df->watch_out = b_input_add( df->fd, GAIM_INPUT_WRITE, dccs_send_can_write, df ); |
---|
| 530 | |
---|
| 531 | return TRUE; |
---|
[2c2df7d] | 532 | } |
---|
| 533 | |
---|
| 534 | /* |
---|
| 535 | * Cleans up after a transfer. |
---|
| 536 | */ |
---|
| 537 | static void dcc_close( file_transfer_t *file ) |
---|
| 538 | { |
---|
| 539 | dcc_file_transfer_t *df = file->priv; |
---|
| 540 | |
---|
| 541 | if( file->free ) |
---|
| 542 | file->free( file ); |
---|
| 543 | |
---|
| 544 | closesocket( df->fd ); |
---|
| 545 | |
---|
| 546 | if( df->watch_in ) |
---|
| 547 | b_event_remove( df->watch_in ); |
---|
| 548 | |
---|
| 549 | if( df->watch_out ) |
---|
| 550 | b_event_remove( df->watch_out ); |
---|
| 551 | |
---|
| 552 | df->ic->irc->file_transfers = g_slist_remove( df->ic->irc->file_transfers, file ); |
---|
| 553 | |
---|
| 554 | g_free( df ); |
---|
| 555 | g_free( file->file_name ); |
---|
| 556 | g_free( file ); |
---|
| 557 | } |
---|
| 558 | |
---|
| 559 | void dcc_finish( file_transfer_t *file ) |
---|
| 560 | { |
---|
| 561 | file->status |= FT_STATUS_FINISHED; |
---|
| 562 | |
---|
| 563 | if( file->finished ) |
---|
| 564 | file->finished( file ); |
---|
| 565 | |
---|
| 566 | dcc_close( file ); |
---|
| 567 | } |
---|
[2ff2076] | 568 | |
---|
| 569 | /* |
---|
| 570 | * DCC SEND <filename> <IP> <port> <filesize> |
---|
| 571 | * |
---|
| 572 | * filename can be in "" or not. If it is, " can probably be escaped... |
---|
| 573 | * IP can be an unsigned int (IPV4) or something else (IPV6) |
---|
| 574 | * |
---|
| 575 | */ |
---|
| 576 | file_transfer_t *dcc_request( struct im_connection *ic, char *line ) |
---|
| 577 | { |
---|
| 578 | char *pattern = "SEND" |
---|
| 579 | " (([^\"][^ ]*)|\"([^\"]|\\\")*\")" |
---|
| 580 | " (([0-9]*)|([^ ]*))" |
---|
| 581 | " ([0-9]*)" |
---|
| 582 | " ([0-9]*)\001"; |
---|
| 583 | regmatch_t pmatch[9]; |
---|
| 584 | regex_t re; |
---|
| 585 | file_transfer_t *ft; |
---|
| 586 | dcc_file_transfer_t *df; |
---|
| 587 | |
---|
| 588 | if( regcomp( &re, pattern, REG_EXTENDED ) ) |
---|
| 589 | return NULL; |
---|
| 590 | if( regexec( &re, line, 9, pmatch, 0 ) ) |
---|
| 591 | return NULL; |
---|
| 592 | |
---|
| 593 | if( ( pmatch[1].rm_so > 0 ) && |
---|
| 594 | ( pmatch[4].rm_so > 0 ) && |
---|
| 595 | ( pmatch[7].rm_so > 0 ) && |
---|
| 596 | ( pmatch[8].rm_so > 0 ) ) |
---|
| 597 | { |
---|
| 598 | char *input = g_strdup( line ); |
---|
| 599 | char *filename, *host, *port; |
---|
| 600 | size_t filesize; |
---|
| 601 | struct addrinfo hints, *rp; |
---|
| 602 | |
---|
| 603 | /* "filename" or filename */ |
---|
| 604 | if ( pmatch[2].rm_so > 0 ) |
---|
| 605 | { |
---|
| 606 | input[pmatch[2].rm_eo] = '\0'; |
---|
| 607 | filename = input + pmatch[2].rm_so; |
---|
| 608 | } else |
---|
| 609 | { |
---|
| 610 | input[pmatch[3].rm_eo] = '\0'; |
---|
| 611 | filename = input + pmatch[3].rm_so; |
---|
| 612 | } |
---|
| 613 | |
---|
| 614 | input[pmatch[4].rm_eo] = '\0'; |
---|
| 615 | |
---|
| 616 | /* number means ipv4, something else means ipv6 */ |
---|
| 617 | if ( pmatch[5].rm_so > 0 ) |
---|
| 618 | { |
---|
| 619 | struct in_addr ipaddr = { htonl( atoi( input + pmatch[5].rm_so ) ) }; |
---|
| 620 | host = inet_ntoa( ipaddr ); |
---|
| 621 | } else |
---|
| 622 | { |
---|
| 623 | /* Contains non-numbers, hopefully an IPV6 address */ |
---|
| 624 | host = input + pmatch[6].rm_so; |
---|
| 625 | } |
---|
| 626 | |
---|
| 627 | input[pmatch[7].rm_eo] = '\0'; |
---|
| 628 | input[pmatch[8].rm_eo] = '\0'; |
---|
| 629 | |
---|
| 630 | port = input + pmatch[7].rm_so; |
---|
| 631 | filesize = atoll( input + pmatch[8].rm_so ); |
---|
| 632 | |
---|
| 633 | memset( &hints, 0, sizeof ( struct addrinfo ) ); |
---|
| 634 | if ( getaddrinfo( host, port, &hints, &rp ) ) |
---|
| 635 | { |
---|
| 636 | g_free( input ); |
---|
| 637 | return NULL; |
---|
| 638 | } |
---|
| 639 | |
---|
| 640 | df = dcc_alloc_transfer( filename, filesize, ic ); |
---|
| 641 | ft = df->ft; |
---|
| 642 | ft->sending = TRUE; |
---|
| 643 | memcpy( &df->saddr, rp->ai_addr, rp->ai_addrlen ); |
---|
| 644 | |
---|
| 645 | freeaddrinfo( rp ); |
---|
| 646 | g_free( input ); |
---|
| 647 | |
---|
| 648 | df->ic->irc->file_transfers = g_slist_prepend( df->ic->irc->file_transfers, ft ); |
---|
| 649 | |
---|
| 650 | return ft; |
---|
| 651 | } |
---|
| 652 | |
---|
| 653 | return NULL; |
---|
| 654 | } |
---|
| 655 | |
---|