Changeset 0fbd3a6d for protocols/jabber/s5bytestream.c
- Timestamp:
- 2007-12-19T00:24:32Z (17 years ago)
- Branches:
- master
- Children:
- 1ba7e8f
- Parents:
- 793cc25 (diff), 2379566 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
protocols/jabber/s5bytestream.c
r793cc25 r0fbd3a6d 30 30 struct jabber_transfer *tf; 31 31 32 /* <query> element and <streamhost> elements */33 struct xt_node *qnode, *shnode;32 jabber_streamhost_t *sh; 33 GSList *streamhosts; 34 34 35 35 enum … … 74 74 gboolean jabber_bs_abort( struct bs_transfer *bt, char *format, ... ); 75 75 void jabber_bs_canceled( file_transfer_t *ft , char *reason ); 76 void jabber_bs_free_transfer( file_transfer_t *ft );76 void jabber_bs_free_transfer( file_transfer_t *ft ); 77 77 gboolean jabber_bs_connect_timeout( gpointer data, gint fd, b_input_condition cond ); 78 78 gboolean jabber_bs_poll( struct bs_transfer *bt, int fd, short *revents ); … … 84 84 gboolean jabber_bs_recv_handshake( gpointer data, gint fd, b_input_condition cond ); 85 85 gboolean jabber_bs_recv_handshake_abort( struct bs_transfer *bt, char *error ); 86 int jabber_bs_recv_request( struct im_connection *ic, struct xt_node *node, struct xt_node *qnode );86 int jabber_bs_recv_request( struct im_connection *ic, struct xt_node *node, struct xt_node *qnode ); 87 87 88 88 gboolean jabber_bs_send_handshake_abort( struct bs_transfer *bt, char *error ); 89 gboolean jabber_bs_send_request( struct jabber_transfer *tf, char *host, char *port);89 gboolean jabber_bs_send_request( struct jabber_transfer *tf, GSList *streamhosts ); 90 90 gboolean jabber_bs_send_handshake( gpointer data, gint fd, b_input_condition cond ); 91 91 gboolean jabber_bs_send_listen( struct bs_transfer *bt, struct sockaddr_storage *saddr, char *host, char *port ); 92 static xt_status jabber_bs_send_handle_activate( struct im_connection *ic, struct xt_node *node, struct xt_node *orig ); 93 void jabber_bs_send_activate( struct bs_transfer *bt ); 92 94 93 95 /* … … 97 99 struct jabber_transfer *tf = ft->data; 98 100 struct bs_transfer *bt = tf->streamhandle; 101 jabber_streamhost_t *sh; 99 102 100 103 if ( tf->watch_in ) … … 105 108 106 109 g_free( bt->pseudoadr ); 107 xt_free_node( bt->qnode ); 110 111 while( bt->streamhosts ) 112 { 113 sh = bt->streamhosts->data; 114 bt->streamhosts = g_slist_remove( bt->streamhosts, sh ); 115 g_free( sh->jid ); 116 g_free( sh->host ); 117 g_free( sh ); 118 } 119 108 120 g_free( bt ); 109 121 … … 111 123 } 112 124 125 /* 126 * Checks if buflen data is available on the socket and 127 * writes it to buffer if that's the case. 128 */ 113 129 gboolean jabber_bs_peek( struct bs_transfer *bt, void *buffer, int buflen ) 114 130 { … … 147 163 } 148 164 165 /* 166 * Polls the socket, checks for errors and removes a connect timer 167 * if there is one. 168 */ 149 169 gboolean jabber_bs_poll( struct bs_transfer *bt, int fd, short *revents ) 150 170 { … … 181 201 } 182 202 203 /* 204 * Used for receive and send path. 205 */ 183 206 gboolean jabber_bs_abort( struct bs_transfer *bt, char *format, ... ) 184 207 { … … 191 214 va_end( params ); 192 215 if( bt->tf->ft->sending ) 216 return jabber_bs_send_handshake_abort( bt, error ); 217 else 193 218 return jabber_bs_recv_handshake_abort( bt, error ); 194 else195 return jabber_bs_send_handshake_abort( bt, error );196 219 } 197 220 … … 214 237 GSList *tflist; 215 238 struct bs_transfer *bt; 239 GSList *shlist=NULL; 240 struct xt_node *shnode; 216 241 217 242 sha1_state_t sha; … … 233 258 { 234 259 imcb_log( ic, "WARNING: Received SI Request for unsupported bytestream mode %s", xt_find_attr( qnode, "mode" ) ); 260 return XT_HANDLED; 261 } 262 263 shnode = qnode->children; 264 while( ( shnode = xt_find_node( shnode, "streamhost" ) ) ) 265 { 266 char *jid, *host; 267 int port; 268 if( ( jid = xt_find_attr( shnode, "jid" ) ) && 269 ( host = xt_find_attr( shnode, "host" ) ) && 270 ( ( port = atoi( xt_find_attr( shnode, "port" ) ) ) ) ) 271 { 272 jabber_streamhost_t *sh = g_new0( jabber_streamhost_t, 1 ); 273 sh->jid = g_strdup(jid); 274 sh->host = g_strdup(host); 275 sprintf( sh->port, "%u", port ); 276 shlist = g_slist_append( shlist, sh ); 277 } 278 shnode = shnode->next; 279 } 280 281 if( !shlist ) 282 { 283 imcb_log( ic, "WARNING: Received incomplete SI bytestream request, no parseable streamhost entries"); 235 284 return XT_HANDLED; 236 285 } … … 274 323 bt = g_new0( struct bs_transfer, 1 ); 275 324 bt->tf = tf; 276 bt-> qnode = xt_dup( qnode );277 bt->sh node = bt->qnode->children;325 bt->streamhosts = shlist; 326 bt->sh = shlist->data; 278 327 bt->phase = BS_PHASE_CONNECT; 279 328 bt->pseudoadr = g_strdup( hash_hex ); … … 285 334 return XT_HANDLED; 286 335 } 336 287 337 /* 288 338 * This is what a protocol handshake can look like in cooperative multitasking :) … … 305 355 case BS_PHASE_CONNECT: 306 356 { 307 struct xt_node *c;308 char *host, *port;309 357 struct addrinfo hints, *rp; 310 358 311 if( ( c = bt->shnode = xt_find_node( bt->shnode, "streamhost" ) ) && 312 ( port = xt_find_attr( c, "port" ) ) && 313 ( host = xt_find_attr( c, "host" ) ) && 314 xt_find_attr( c, "jid" ) ) 315 { 316 memset( &hints, 0, sizeof( struct addrinfo ) ); 317 hints.ai_socktype = SOCK_STREAM; 318 319 if ( getaddrinfo( host, port, &hints, &rp ) != 0 ) 320 return jabber_bs_abort( bt, "getaddrinfo() failed: %s", strerror( errno ) ); 321 322 ASSERTSOCKOP( bt->tf->fd = fd = socket( rp->ai_family, rp->ai_socktype, 0 ), "Opening socket" ); 323 324 sock_make_nonblocking( fd ); 325 326 imcb_log( bt->tf->ic, "File %s: Connecting to streamhost %s:%s", bt->tf->ft->file_name, host, port ); 327 328 if( ( connect( fd, rp->ai_addr, rp->ai_addrlen ) == -1 ) && 329 ( errno != EINPROGRESS ) ) 330 return jabber_bs_abort( bt , "connect() failed: %s", strerror( errno ) ); 331 332 freeaddrinfo( rp ); 333 334 bt->phase = BS_PHASE_CONNECTED; 335 336 bt->tf->watch_out = b_input_add( fd, GAIM_INPUT_WRITE, jabber_bs_recv_handshake, bt ); 337 338 /* since it takes forever(3mins?) till connect() fails on itself we schedule a timeout */ 339 bt->connect_timeout = b_timeout_add( JABBER_BS_CONTIMEOUT * 1000, jabber_bs_connect_timeout, bt ); 340 341 bt->tf->watch_in = 0; 342 return FALSE; 343 } else 344 return jabber_bs_abort( bt, c ? "incomplete streamhost entry: host=%s port=%s jid=%s" : NULL, 345 host, port, xt_find_attr( c, "jid" ) ); 359 memset( &hints, 0, sizeof( struct addrinfo ) ); 360 hints.ai_socktype = SOCK_STREAM; 361 362 if ( getaddrinfo( bt->sh->host, bt->sh->port, &hints, &rp ) != 0 ) 363 return jabber_bs_abort( bt, "getaddrinfo() failed: %s", strerror( errno ) ); 364 365 ASSERTSOCKOP( bt->tf->fd = fd = socket( rp->ai_family, rp->ai_socktype, 0 ), "Opening socket" ); 366 367 sock_make_nonblocking( fd ); 368 369 imcb_log( bt->tf->ic, "File %s: Connecting to streamhost %s:%s", bt->tf->ft->file_name, bt->sh->host, bt->sh->port ); 370 371 if( ( connect( fd, rp->ai_addr, rp->ai_addrlen ) == -1 ) && 372 ( errno != EINPROGRESS ) ) 373 return jabber_bs_abort( bt , "connect() failed: %s", strerror( errno ) ); 374 375 freeaddrinfo( rp ); 376 377 bt->phase = BS_PHASE_CONNECTED; 378 379 bt->tf->watch_out = b_input_add( fd, GAIM_INPUT_WRITE, jabber_bs_recv_handshake, bt ); 380 381 /* since it takes forever(3mins?) till connect() fails on itself we schedule a timeout */ 382 bt->connect_timeout = b_timeout_add( JABBER_BS_CONTIMEOUT * 1000, jabber_bs_connect_timeout, bt ); 383 384 bt->tf->watch_in = 0; 385 return FALSE; 346 386 } 347 387 case BS_PHASE_CONNECTED: … … 422 462 socks5_reply.addrlen); 423 463 424 jabber_bs_recv_answer_request( bt ); 464 if( bt->tf->ft->sending ) 465 jabber_bs_send_activate( bt ); 466 else 467 jabber_bs_recv_answer_request( bt ); 425 468 426 469 return FALSE; … … 447 490 struct jabber_transfer *tf = bt->tf; 448 491 struct xt_node *reply, *iqnode; 449 450 if( bt->shnode ) 451 {452 imcb_log( tf->ic, "Transferring file %s: connection to streamhost %s:%s failed (%s)",453 tf->ft->file_name,454 xt_find_attr( bt->shnode, "host" ),455 xt_find_attr( bt->shnode, "port" ),456 error ); 457 458 /* Alright, this streamhost failed, let's try the next... */459 bt->phase = BS_PHASE_CONNECT;460 bt->shnode = bt->shnode->next;461 462 /* the if is not neccessary but saves us one recursion */463 if( bt->shnode )464 return jabber_bs_recv_handshake( bt, 0, 0 );465 } 492 GSList *shlist; 493 494 imcb_log( tf->ic, "Transferring file %s: connection to streamhost %s:%s failed (%s)", 495 tf->ft->file_name, 496 bt->sh->host, 497 bt->sh->port, 498 error ); 499 500 /* Alright, this streamhost failed, let's try the next... */ 501 bt->phase = BS_PHASE_CONNECT; 502 shlist = g_slist_find( bt->streamhosts, bt->sh ); 503 if( shlist && shlist->next ) 504 { 505 bt->sh = shlist->next->data; 506 return jabber_bs_recv_handshake( bt, 0, 0 ); 507 } 508 466 509 467 510 /* out of stream hosts */ … … 495 538 imcb_log( tf->ic, "File %s: established SOCKS5 connection to %s:%s", 496 539 tf->ft->file_name, 497 xt_find_attr( bt->shnode, "host" ),498 xt_find_attr( bt->shnode, "port" ));540 bt->sh->host, 541 bt->sh->port ); 499 542 500 543 tf->ft->data = tf; 501 tf->ft->started = time( NULL );502 544 tf->watch_in = b_input_add( tf->fd, GAIM_INPUT_READ, jabber_bs_recv_read, bt ); 503 545 tf->ft->write_request = jabber_bs_recv_write_request; 504 546 505 547 reply = xt_new_node( "streamhost-used", NULL, NULL ); 506 xt_add_attr( reply, "jid", xt_find_attr( bt->shnode, "jid" ));548 xt_add_attr( reply, "jid", bt->sh->jid ); 507 549 508 550 reply = xt_new_node( "query", NULL, reply ); … … 551 593 return jabber_bs_abort( bt, "Remote end closed connection" ); 552 594 595 if( tf->bytesread == 0 ) 596 tf->ft->started = time( NULL ); 597 553 598 tf->bytesread += ret; 554 599 … … 603 648 return jabber_bs_abort( bt, "BUG: write() called while watching " ); 604 649 650 /* TODO: catch broken pipe */ 605 651 ASSERTSOCKOP( ret = send( tf->fd, buffer, len, 0 ), "Sending" ); 652 653 if( tf->byteswritten == 0 ) 654 tf->ft->started = time( NULL ); 606 655 607 656 tf->byteswritten += ret; … … 665 714 tf->accepted = TRUE; 666 715 667 if( bt->phase == BS_PHASE_REPLY ) 668 { 669 /* handshake went through, let's start transferring */ 670 tf->ft->started = time( NULL ); 671 tf->ft->write_request( tf->ft ); 716 if( strcmp( jid, tf->ini_jid ) == 0 ) 717 { 718 /* we're streamhost and target */ 719 if( bt->phase == BS_PHASE_REPLY ) 720 { 721 /* handshake went through, let's start transferring */ 722 tf->ft->write_request( tf->ft ); 723 } 724 } else 725 { 726 /* using a proxy */ 727 GSList *shlist; 728 for( shlist = jd->streamhosts ; shlist ; shlist = g_slist_next( shlist ) ) 729 { 730 jabber_streamhost_t *sh = shlist->data; 731 if( strcmp( sh->jid, jid ) == 0 ) 732 { 733 bt->sh = sh; 734 jabber_bs_recv_handshake( bt, 0, 0 ); 735 return XT_HANDLED; 736 } 737 } 738 739 imcb_log( ic, "WARNING: Received SOCKS5 bytestream reply with unknown streamhost %s", jid ); 672 740 } 673 741 … … 675 743 } 676 744 745 /* 746 * Tell the proxy to activate the stream. Looks like this: 747 * 748 * <iq type=set> 749 * <query xmlns=bs sid=sid> 750 * <activate>tgt_jid</activate> 751 * </query> 752 * </iq> 753 */ 754 void jabber_bs_send_activate( struct bs_transfer *bt ) 755 { 756 struct xt_node *node; 757 758 node = xt_new_node( "activate", bt->tf->tgt_jid, NULL ); 759 node = xt_new_node( "query", NULL, node ); 760 xt_add_attr( node, "xmlns", XMLNS_BYTESTREAMS ); 761 xt_add_attr( node, "sid", bt->tf->sid ); 762 node = jabber_make_packet( "iq", "set", bt->sh->jid, node ); 763 764 jabber_cache_add( bt->tf->ic, node, jabber_bs_send_handle_activate ); 765 766 jabber_write_packet( bt->tf->ic, node ); 767 } 768 769 /* 770 * The proxy has activated the bytestream. 771 * We can finally start pushing some data out. 772 */ 773 static xt_status jabber_bs_send_handle_activate( struct im_connection *ic, struct xt_node *node, struct xt_node *orig ) 774 { 775 char *sid; 776 GSList *tflist; 777 struct jabber_transfer *tf; 778 struct xt_node *query; 779 struct jabber_data *jd = ic->proto_data; 780 781 query = xt_find_node( orig->children, "query" ); 782 sid = xt_find_attr( query, "sid" ); 783 784 for( tflist = jd->filetransfers ; tflist; tflist = g_slist_next(tflist) ) 785 { 786 struct jabber_transfer *tft = tflist->data; 787 if( ( strcmp( tft->sid, sid ) == 0 ) ) 788 { 789 tf = tft; 790 break; 791 } 792 } 793 794 if( !tf ) 795 { 796 imcb_log( ic, "WARNING: Received SOCKS5 bytestream activation for unknown stream" ); 797 return XT_HANDLED; 798 } 799 800 /* handshake went through, let's start transferring */ 801 tf->ft->write_request( tf->ft ); 802 803 return XT_HANDLED; 804 } 805 806 /* 807 * Starts a bytestream. 808 */ 677 809 gboolean jabber_bs_send_start( struct jabber_transfer *tf ) 678 810 { 679 char host[INET6_ADDRSTRLEN] , port[6];811 char host[INET6_ADDRSTRLEN]; 680 812 struct bs_transfer *bt; 681 813 sha1_state_t sha; 682 814 char hash_hex[41]; 683 815 unsigned char hash[20]; 684 int i; 816 int i,ret; 817 struct jabber_data *jd = tf->ic->proto_data; 818 jabber_streamhost_t sh; 819 GSList *streamhosts = jd->streamhosts; 685 820 686 821 /* SHA1( SID + Initiator JID + Target JID ) is given to the streamhost which it will match against the initiator's value */ … … 702 837 tf->ft->canceled = jabber_bs_canceled; 703 838 704 if ( !jabber_bs_send_listen( bt, &tf->saddr, host,port ) )839 if ( !jabber_bs_send_listen( bt, &tf->saddr, sh.host = host, sh.port ) ) 705 840 return FALSE; 706 841 707 842 bt->tf->watch_in = b_input_add( tf->fd, GAIM_INPUT_READ, jabber_bs_send_handshake, bt ); 708 843 bt->connect_timeout = b_timeout_add( JABBER_BS_LISTEN_TIMEOUT * 1000, jabber_bs_connect_timeout, bt ); 709 return jabber_bs_send_request( tf, host, port ); 710 } 711 712 gboolean jabber_bs_send_request( struct jabber_transfer *tf, char *host, char *port ) 713 { 714 struct xt_node *sh, *query, *iq; 715 716 sh = xt_new_node( "streamhost", NULL, NULL ); 717 xt_add_attr( sh, "jid", tf->ini_jid ); 718 xt_add_attr( sh, "host", host ); 719 xt_add_attr( sh, "port", port ); 844 845 sh.jid = tf->ini_jid; 846 847 /* temporarily add listen address to streamhosts, send the request and remove it */ 848 streamhosts = g_slist_prepend( streamhosts, &sh ); 849 ret = jabber_bs_send_request( tf, streamhosts); 850 streamhosts = g_slist_remove( streamhosts, &sh ); 851 852 return ret; 853 } 854 855 gboolean jabber_bs_send_request( struct jabber_transfer *tf, GSList *streamhosts ) 856 { 857 struct xt_node *shnode, *query, *iq; 720 858 721 859 query = xt_new_node( "query", NULL, NULL ); … … 723 861 xt_add_attr( query, "sid", tf->sid ); 724 862 xt_add_attr( query, "mode", "tcp" ); 725 xt_add_child( query, sh ); 863 864 while( streamhosts ) { 865 jabber_streamhost_t *sh = streamhosts->data; 866 shnode = xt_new_node( "streamhost", NULL, NULL ); 867 xt_add_attr( shnode, "jid", sh->jid ); 868 xt_add_attr( shnode, "host", sh->host ); 869 xt_add_attr( shnode, "port", sh->port ); 870 871 xt_add_child( query, shnode ); 872 873 streamhosts = g_slist_next( streamhosts ); 874 } 875 726 876 727 877 iq = jabber_make_packet( "iq", "set", tf->tgt_jid, query ); … … 739 889 struct jabber_transfer *tf = bt->tf; 740 890 891 /* TODO: did the receiver get here somehow??? */ 741 892 imcb_log( tf->ic, "Transferring file %s: SOCKS5 handshake failed: %s", 742 893 tf->ft->file_name, … … 901 1052 { 902 1053 /* streamhost-used message came already in(possible?), let's start sending */ 903 tf->ft->started = time( NULL );904 1054 tf->ft->write_request( tf->ft ); 905 1055 }
Note: See TracChangeset
for help on using the changeset viewer.