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