Changeset 2971
- Timestamp:
- Sep 1, 2007, 8:31:58 PM (15 years ago)
- Location:
- branches/encryption/libtransmission
- Files:
-
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/encryption/libtransmission/handshake.c
r2954 r2971 19 19 #include <openssl/bn.h> 20 20 #include <openssl/dh.h> 21 //#include <openssl/rc4.h>22 //#include <openssl/sha.h>23 21 24 22 #include <event.h> … … 85 83 uint8_t state; 86 84 uint8_t encryptionPreference; 87 uint8_t * resync; 88 uint16_t PadC_len; 89 uint16_t PadD_len; 90 uint8_t pad_b_buf[512 + 8]; 91 int pad_b_len; 85 uint16_t pad_c_len; 86 uint16_t pad_d_len; 92 87 int ia_len; 93 int resync_len;94 int pad_d_len;95 88 int crypto_select; 96 89 DH * dh; … … 113 106 AWAITING_HANDSHAKE, 114 107 AWAITING_YA, 115 SENDING_YB,116 108 AWAITING_PAD_A, 117 109 AWAITING_CRYPTO_PROVIDE, … … 120 112 121 113 /* outgoing */ 122 SENDING_YA,123 114 AWAITING_YB, 124 SENDING_CRYPTO_PROVIDE,125 115 AWAITING_VC, 126 116 AWAITING_CRYPTO_SELECT, 127 117 AWAITING_PAD_D, 128 //AWAITING_PLAINTEXT_RESPONSE,129 130 SENDING_PLAINTEXT_HANDSHAKE,131 SENDING_LTEP_HANDHAKE132 118 }; 133 119 … … 142 128 case AWAITING_HANDSHAKE: str = "awaiting handshake"; break; 143 129 case AWAITING_YA: str = "awaiting ya"; break; 144 case SENDING_YB: str = "sending yb"; break;130 //case SENDING_YB: str = "sending yb"; break; 145 131 case AWAITING_PAD_A: str = "awaiting pad a"; break; 146 132 case AWAITING_CRYPTO_PROVIDE: str = "awaiting crypto_provide"; break; 147 133 case AWAITING_PAD_C: str = "awaiting pad c"; break; 148 134 case AWAITING_IA: str = "awaiting ia"; break; 149 case SENDING_YA: str = "sending ya"; break;135 //case SENDING_YA: str = "sending ya"; break; 150 136 case AWAITING_YB: str = "awaiting yb"; break; 151 case SENDING_CRYPTO_PROVIDE: str = "sending crypto provide"; break;137 //case SENDING_CRYPTO_PROVIDE: str = "sending crypto provide"; break; 152 138 case AWAITING_VC: str = "awaiting vc"; break; 153 139 case AWAITING_CRYPTO_SELECT: str = "awaiting crypto select"; break; 154 140 case AWAITING_PAD_D: str = "awaiting pad d"; break; 155 case SENDING_PLAINTEXT_HANDSHAKE: str = "sending plaintext handshake"; break;141 //case SENDING_PLAINTEXT_HANDSHAKE: str = "sending plaintext handshake"; break; 156 142 } 157 143 return str; … … 161 147 setState( tr_handshake * handshake, short state ) 162 148 { 163 fprintf( stderr, " setting handshake %pto state [%s]\n", handshake, getStateName(state) );149 fprintf( stderr, "handshake %p: setting to state [%s]\n", handshake, getStateName(state) ); 164 150 handshake->state = state; 165 151 } … … 169 155 { 170 156 setState( handshake, state ); 171 tr_peerIoSetIOMode( handshake->io, EV_READ, EV_WRITE );157 //tr_peerIoSetIOMode( handshake->io, EV_READ, EV_WRITE ); 172 158 } 173 159 … … 194 180 195 181 /* send it */ 196 setState( handshake, SENDING_YA ); 182 setReadState( handshake, AWAITING_YB ); 183 //setState( handshake, SENDING_YA ); 197 184 tr_peerIoWriteBuf( handshake->io, outbuf ); 198 tr_peerIoSetIOMode( handshake->io, EV_WRITE, EV_READ );199 185 200 186 /* cleanup */ … … 227 213 uint8_t buf[HANDSHAKE_SIZE]; 228 214 buildHandshakeMessage( handshake, buf ); 229 setState( handshake, SENDING_PLAINTEXT_HANDSHAKE ); 215 216 setReadState( handshake, AWAITING_HANDSHAKE ); 230 217 tr_peerIoWrite( handshake->io, buf, HANDSHAKE_SIZE ); 231 tr_peerIoSetIOMode( handshake->io, EV_WRITE, EV_READ );232 218 } 233 219 … … 269 255 tr_bencInitStr( tr_bencDictAdd( &val, "v" ), v, 0, 1 ); 270 256 271 fprintf( stderr, " sending ltep handshake...\n");257 fprintf( stderr, "handshake %p: sending ltep handshake...\n", handshake ); 272 258 buf = tr_bencSaveMalloc( &val, &len ); 273 259 tr_bencPrint( &val ); … … 279 265 tr_peerIoWriteBytes ( handshake->io, outbuf, buf, len ); 280 266 281 handshake->state = SENDING_LTEP_HANDHAKE;282 267 tr_peerIoWriteBuf( handshake->io, outbuf ); 283 tr_peerIoSetIOMode( handshake->io, EV_WRITE, EV_READ);268 fireDoneCB( handshake, TRUE ); 284 269 285 270 /* cleanup */ … … 318 303 while( len-- ) 319 304 *walk++ = tr_rand( UCHAR_MAX ); 320 setState( handshake, SENDING_YB ); 305 306 setReadState( handshake, AWAITING_PAD_A ); 321 307 tr_peerIoWrite( handshake->io, outbuf, walk-outbuf ); 322 tr_peerIoSetIOMode( handshake->io, EV_WRITE, EV_READ );323 308 324 309 return READ_DONE; … … 398 383 tr_peerIoReadUint16( handshake->io, inbuf, &padc_len ); 399 384 fprintf( stderr, "padc is %d\n", (int)padc_len ); 400 handshake-> PadC_len = padc_len;385 handshake->pad_c_len = padc_len; 401 386 setState( handshake, AWAITING_PAD_C ); 402 387 return READ_AGAIN; … … 407 392 { 408 393 uint16_t ia_len; 409 const size_t needlen = handshake-> PadC_len + sizeof(uint16_t);394 const size_t needlen = handshake->pad_c_len + sizeof(uint16_t); 410 395 411 396 if( EVBUFFER_LENGTH(inbuf) < needlen ) … … 545 530 /* send it */ 546 531 tr_cryptoDecryptInit( handshake->crypto ); 547 set State( handshake, SENDING_CRYPTO_PROVIDE);532 setReadState( handshake, AWAITING_VC ); 548 533 tr_peerIoWriteBuf( handshake->io, outbuf ); 549 tr_peerIoSetIOMode( handshake->io, EV_WRITE, EV_READ );550 534 551 535 /* cleanup */ … … 744 728 struct evbuffer * inbuf = EVBUFFER_INPUT ( evin ); 745 729 ReadState ret; 746 fprintf( stderr, "hand ling canRead; state is [%s]\n", getStateName(handshake->state) );730 fprintf( stderr, "handshake %p handling canRead; state is [%s]\n", handshake, getStateName(handshake->state) ); 747 731 748 732 switch( handshake->state ) … … 771 755 tr_handshake * handshake = (tr_handshake *) arg; 772 756 fprintf( stderr, "handshake %p, with a state of %s, got a didWrite event\n", handshake, getStateName(handshake->state) ); 757 #if 0 758 abort ( ); 773 759 774 760 if( handshake->state == SENDING_LTEP_HANDHAKE ) … … 778 764 else 779 765 { 766 cccccccccccccccccccccccccccccccccc 780 767 int state = -1; 781 768 switch( handshake->state ) 782 769 { 783 case SENDING_YA: state = AWAITING_YB; break; 784 case SENDING_YB: state = AWAITING_PAD_A; break; 785 case SENDING_CRYPTO_PROVIDE: state = AWAITING_VC; break; 770 //case SENDING_YA: state = AWAITING_YB; break; 771 //case SENDING_YB: state = AWAITING_PAD_A; break; 772 //case SENDING_CRYPTO_PROVIDE: state = AWAITING_VC; break; 773 //case SENDING_PLAINTEXT_HANDSHAKE: state = AWAITING_HANDSHAKE; break; 786 774 } 787 775 assert( state != -1 ); … … 789 777 tr_peerIoReadOrWait( handshake->io ); 790 778 } 779 #endif 791 780 } 792 781 … … 801 790 fireDoneCB( tr_handshake * handshake, int isConnected ) 802 791 { 792 fprintf( stderr, "handshake %p: firing done. connected==%d\n", handshake, isConnected ); 803 793 (*handshake->doneCB)(handshake->io, isConnected, handshake->doneUserData); 804 794 tr_handshakeFree( handshake ); … … 806 796 807 797 static void 808 gotError( struct bufferevent * evbuf UNUSED, short what UNUSED, void * arg )798 gotError( struct bufferevent * evbuf UNUSED, short what, void * arg ) 809 799 { 810 800 tr_handshake * handshake = (tr_handshake *) arg; 801 fprintf( stderr, "handshake %p: got error; what==%hd\n", handshake, what ); 811 802 812 803 … … 814 805 * have encountered a peer that doesn't do encryption... reconnect and 815 806 * try a plaintext handshake */ 816 if( ( handshake->state == SENDING_YA)807 if( ( handshake->state == AWAITING_YB ) 817 808 && ( handshake->encryptionPreference != HANDSHAKE_ENCRYPTION_REQUIRED ) 818 809 && ( !tr_peerIoReconnect( handshake->io ) ) ) … … 836 827 tr_handshake * handshake; 837 828 829 //w00t 838 830 //static int count = 0; 839 831 //if( count++ ) return NULL; … … 847 839 handshake->handle = tr_peerIoGetHandle( io ); 848 840 841 tr_peerIoSetIOMode( io, EV_READ|EV_WRITE, 0 ); 849 842 tr_peerIoSetIOFuncs( io, canRead, didWrite, gotError, handshake ); 843 844 fprintf( stderr, "handshake %p: new handshake for io %p\n", handshake, io ); 850 845 851 846 if( tr_peerIoIsIncoming( io ) ) -
branches/encryption/libtransmission/peer-io.c
r2965 r2971 113 113 c->rateToPeer = tr_rcInit( ); 114 114 c->rateToClient = tr_rcInit( ); 115 fprintf( stderr, "io %p rates: peer %p client %p\n", c, c->rateToPeer, c->rateToClient ); 115 116 c->bufev = bufferevent_new( c->socket, 116 117 canReadWrapper, … … 118 119 gotErrorWrapper, 119 120 c ); 121 bufferevent_enable( c->bufev, EV_READ|EV_WRITE ); 120 122 return c; 121 123 } … … 126 128 int socket ) 127 129 { 128 tr_peerIo * c = 129 tr_peerIoNew( handle, in_addr, NULL, 1, socket ); 130 tr_peerIo * c; 131 132 assert( handle != NULL ); 133 assert( in_addr != NULL ); 134 assert( socket >= 0 ); 135 136 c = tr_peerIoNew( handle, in_addr, NULL, 1, socket ); 130 137 c->port = -1; 131 138 return c; … … 157 164 { 158 165 bufferevent_free( c->bufev ); 166 fprintf( stderr, "io %p destroying rate to client %p to peer %p\n", c, c->rateToClient, c->rateToPeer ); 159 167 tr_rcClose( c->rateToClient ); 160 168 tr_rcClose( c->rateToPeer ); … … 205 213 { 206 214 tr_setBufferEventMode( c->handle, c->bufev, enable, disable ); 207 }208 209 void210 tr_peerIoReadOrWait( tr_peerIo * c )211 {212 if( EVBUFFER_LENGTH( c->bufev->input ) )213 canReadWrapper( c->bufev, c );214 else215 tr_peerIoSetIOMode( c, EV_READ, EV_WRITE );216 215 } 217 216 -
branches/encryption/libtransmission/peer-io.h
r2963 r2971 107 107 short disable_mode ); 108 108 109 void tr_peerIoReadOrWait( tr_peerIo * io );110 111 109 void tr_peerIoWrite( tr_peerIo * io, 112 110 const void * writeme, -
branches/encryption/libtransmission/peer-mgr.c
r2969 r2971 14 14 #include <string.h> /* memcpy, memcmp */ 15 15 #include <stdlib.h> /* qsort */ 16 #include <stdio.h> /* printf */ 16 17 17 18 #include "transmission.h" … … 118 119 memcpy( &peer->in_addr, in_addr, sizeof(struct in_addr) ); 119 120 tr_ptrArrayInsertSorted( torrent->peers, peer, peerCompare ); 121 fprintf( stderr, "getPeer: torrent %p now has %d peers\n", torrent, tr_ptrArraySize(torrent->peers) ); 120 122 } 121 123 return peer; … … 186 188 tr_peerMgr * manager = (tr_peerMgr*) vmanager; 187 189 Torrent * t = getExistingTorrent( manager, hash ); 188 190 uint16_t port; 191 const struct in_addr * in_addr; 192 193 fprintf( stderr, "peer-mgr: torrent [%s] finished a handshake; isConnected is %d\n", t->tor->info.name, isConnected ); 194 195 assert( io != NULL ); 196 197 in_addr = tr_peerIoGetAddress( io, &port ); 198 199 /* if we couldn't connect or were snubbed, 200 * the peer's probably not worth remembering. */ 201 if( !ok ) { 202 tr_peer * peer = getExistingPeer( t, in_addr ); 203 fprintf( stderr, "peer-mgr: torrent [%s] got a bad one, and you know what? fuck them.\n", t->tor->info.name ); 204 if( peer ) { 205 tr_ptrArrayRemoveSorted( t->peers, peer, peerCompare ); 206 freePeer( peer ); 207 } else { 208 tr_peerIoFree( io ); 209 } 210 --manager->connectionCount; 211 return; 212 } 213 214 #if 0 215 /* ONLY DO THIS TEST FOR INCOMING CONNECTIONS */ 189 216 /* check for duplicates */ 190 if( ok ) { 191 if ( getExistingPeer( t, tr_peerIoGetAddress(io,NULL) ) ) { 192 tr_dbg( "dropping a duplicate connection... dropping." ); 193 ok = FALSE; 194 } 195 } 196 197 /* do something with this connection */ 198 if( !ok ) { 217 if( getExistingPeer( t, in_addr ) ) { 218 tr_dbg( "dropping a duplicate connection... dropping." ); 199 219 tr_peerIoFree( io ); 200 --manager->connectionCount; 201 } else { 202 uint16_t port; 203 tr_peer * peer = getPeer( t, tr_peerIoGetAddress(io,&port) ); 220 return; 221 } 222 #endif 223 224 if( 1 ) { 225 tr_peer * peer = getPeer( t, in_addr ); 204 226 peer->port = port; 205 peer->io = io;206 227 peer->msgs = tr_peerMsgsNew( t->tor, peer ); 207 228 } … … 244 265 peer->port = port; 245 266 peer->from = from; 267 268 if( tr_peerMgrIsAcceptingConnections( manager ) ) 269 { 270 fprintf( stderr, "peer-mgr: torrent [%s] is handshaking with a new peer...\n", t->tor->info.name ); 271 272 peer->io = tr_peerIoNewOutgoing( manager->handle, &addr, port, t->hash ); 273 274 tr_handshakeAdd( peer->io, HANDSHAKE_ENCRYPTION_PREFERRED, 275 myHandshakeDoneCB, manager ); 276 } 246 277 } 247 278 } … … 357 388 const tr_peer ** peers = (const tr_peer **) tr_ptrArrayPeek( t->peers, &peerCount ); 358 389 for( j=0; j<peerCount; ++j ) 359 if( tr_bitfieldHas( peers[ i]->have, j) )390 if( tr_bitfieldHas( peers[j]->have, i ) ) 360 391 ++tab[i]; 361 392 } … … 502 533 ChokeData * data; 503 534 535 fprintf( stderr, "rechoking torrent %p, with %d peers\n", t, size ); 536 504 537 if( size < 1 ) 505 538 return TRUE; -
branches/encryption/libtransmission/peer-msgs.c
r2969 r2971 245 245 tr_free( peer->info->client ); 246 246 fprintf( stderr, "dictionary says client is [%s]\n", sub->val.s.s ); 247 for( i=0; i<sub->val.s.i; ++i ) fprintf( stderr, "[%c] (%d)\n", sub->val.s.s[i], (int)sub->val.s.s[i] );248 247 peer->info->client = tr_strndup( sub->val.s.s, sub->val.s.i ); 248 for( i=0; i<sub->val.s.i; ++i ) { fprintf( stderr, "[%c] (%d)\n", sub->val.s.s[i], (int)sub->val.s.s[i] ); 249 if( (int)peer->info->client[i]==-75 ) peer->info->client[i]='u'; } 249 250 fprintf( stderr, "peer->client is now [%s]\n", peer->info->client ); 250 251 } -
branches/encryption/libtransmission/ratecontrol.c
r2942 r2971 101 101 { 102 102 int ret; 103 tr_lockLock( (tr_lock_t*)r->lock );104 103 105 ret = rateForInterval( r, SHORT_INTERVAL_MSEC ) < r->limit; 104 if( r == NULL ) 105 ret = 0; 106 else { 107 tr_lockLock( (tr_lock_t*)r->lock ); 108 ret = rateForInterval( r, SHORT_INTERVAL_MSEC ) < r->limit; 109 tr_lockUnlock( (tr_lock_t*)r->lock ); 110 } 106 111 107 tr_lockUnlock( (tr_lock_t*)r->lock );108 112 return ret; 109 113 } … … 113 117 { 114 118 float ret; 115 tr_lockLock( (tr_lock_t*)r->lock );116 119 117 ret = rateForInterval( r, LONG_INTERVAL_MSEC ); 120 if( r == NULL ) 121 ret = 0.0f; 122 else { 123 tr_lockLock( (tr_lock_t*)r->lock ); 124 ret = rateForInterval( r, LONG_INTERVAL_MSEC ); 125 tr_lockUnlock( (tr_lock_t*)r->lock ); 126 } 118 127 119 tr_lockUnlock( (tr_lock_t*)r->lock );120 128 return ret; 121 129 } -
branches/encryption/libtransmission/trevent.c
r2954 r2971 121 121 read( fd, &mode, sizeof(short) ); 122 122 bufferevent_disable( bufev, mode ); 123 fprintf( stderr, "after enable/disable, the mode is %hd\n", bufev->enabled ); 123 124 break; 124 125
Note: See TracChangeset
for help on using the changeset viewer.