Changeset 8563
- Timestamp:
- May 30, 2009, 9:45:40 PM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/libtransmission/handshake.c
r8483 r8563 108 108 tr_crypto * crypto; 109 109 tr_session * session; 110 uint8_t myPublicKey[KEY_LEN];111 110 uint8_t mySecret[KEY_LEN]; 112 111 uint8_t state; … … 118 117 uint32_t crypto_provide; 119 118 uint8_t myReq1[SHA_DIGEST_LENGTH]; 120 uint8_t peer_id[PEER_ID_LEN];121 119 handshakeDoneCB doneCB; 122 120 void * doneUserData; … … 215 213 } 216 214 217 static uint8_t * 218 buildHandshakeMessage( tr_handshake * handshake, 219 int * setme_len ) 220 { 221 uint8_t * buf = tr_new0( uint8_t, HANDSHAKE_SIZE ); 215 static void 216 buildHandshakeMessage( tr_handshake * handshake, uint8_t * buf ) 217 { 222 218 uint8_t * walk = buf; 223 219 const uint8_t * torrentHash = tr_cryptoGetTorrentHash( handshake->crypto ); … … 245 241 assert( strlen( ( const char* )peer_id ) == PEER_ID_LEN ); 246 242 assert( walk - buf == HANDSHAKE_SIZE ); 247 *setme_len = walk - buf;248 return buf;249 243 } 250 244 … … 268 262 uint8_t hash[SHA_DIGEST_LENGTH]; 269 263 const tr_torrent * tor; 270 const uint8_t * peer_id; 264 const uint8_t * tor_peer_id; 265 uint8_t peer_id[PEER_ID_LEN]; 271 266 272 267 dbgmsg( handshake, "payload: need %d, got %zu", … … 296 291 297 292 /* peer_id */ 298 tr_peerIoReadBytes( handshake->io, inbuf, handshake->peer_id, 299 sizeof( handshake->peer_id ) ); 300 tr_peerIoSetPeersId( handshake->io, handshake->peer_id ); 293 tr_peerIoReadBytes( handshake->io, inbuf, peer_id, sizeof( peer_id ) ); 294 tr_peerIoSetPeersId( handshake->io, peer_id ); 301 295 302 296 /* peer id */ 303 297 handshake->havePeerID = TRUE; 304 dbgmsg( handshake, "peer-id is [%*.*s]", PEER_ID_LEN, PEER_ID_LEN, 305 handshake->peer_id ); 298 dbgmsg( handshake, "peer-id is [%*.*s]", PEER_ID_LEN, PEER_ID_LEN, peer_id ); 306 299 307 300 tor = tr_torrentFindFromHash( handshake->session, hash ); 308 peer_id = tor && tor->peer_id ? tor->peer_id : tr_getPeerId( );309 if( !memcmp( handshake->peer_id,peer_id, PEER_ID_LEN ) )301 tor_peer_id = tor && tor->peer_id ? tor->peer_id : tr_getPeerId( ); 302 if( !memcmp( peer_id, tor_peer_id, PEER_ID_LEN ) ) 310 303 { 311 304 dbgmsg( handshake, "streuth! we've connected to ourselves." ); … … 494 487 /* ENCRYPT len(IA)), ENCRYPT(IA) */ 495 488 { 496 int msglen;497 uint8_t * msg = buildHandshakeMessage( handshake, &msglen);498 499 tr_peerIoWriteUint16( handshake->io, outbuf, msglen);500 tr_peerIoWriteBytes( handshake->io, outbuf, msg, msglen);489 uint8_t msg[HANDSHAKE_SIZE]; 490 buildHandshakeMessage( handshake, msg ); 491 492 tr_peerIoWriteUint16( handshake->io, outbuf, sizeof( msg ) ); 493 tr_peerIoWriteBytes( handshake->io, outbuf, msg, sizeof( msg ) ); 501 494 502 495 handshake->haveSentBitTorrentHandshake = 1; 503 tr_free( msg );504 496 } 505 497 … … 719 711 if( !handshake->haveSentBitTorrentHandshake ) 720 712 { 721 int msgSize; 722 uint8_t * msg = buildHandshakeMessage( handshake, &msgSize ); 723 tr_peerIoWrite( handshake->io, msg, msgSize, FALSE ); 724 tr_free( msg ); 713 uint8_t msg[HANDSHAKE_SIZE]; 714 buildHandshakeMessage( handshake, msg ); 715 tr_peerIoWrite( handshake->io, msg, sizeof( msg ), FALSE ); 725 716 handshake->haveSentBitTorrentHandshake = 1; 726 717 } … … 737 728 char client[128]; 738 729 tr_torrent * tor; 739 const uint8_t * peer_id; 730 const uint8_t * tor_peer_id; 731 uint8_t peer_id[PEER_ID_LEN]; 740 732 741 733 if( EVBUFFER_LENGTH( inbuf ) < PEER_ID_LEN ) … … 743 735 744 736 /* peer id */ 745 tr_peerIoReadBytes( handshake->io, inbuf, handshake->peer_id, PEER_ID_LEN );746 tr_peerIoSetPeersId( handshake->io, handshake->peer_id );737 tr_peerIoReadBytes( handshake->io, inbuf, peer_id, PEER_ID_LEN ); 738 tr_peerIoSetPeersId( handshake->io, peer_id ); 747 739 handshake->havePeerID = TRUE; 748 tr_clientForId( client, sizeof( client ), handshake->peer_id );740 tr_clientForId( client, sizeof( client ), peer_id ); 749 741 dbgmsg( handshake, "peer-id is [%s] ... isIncoming is %d", client, 750 742 tr_peerIoIsIncoming( handshake->io ) ); … … 752 744 /* if we've somehow connected to ourselves, don't keep the connection */ 753 745 tor = tr_torrentFindFromHash( handshake->session, tr_peerIoGetTorrentHash( handshake->io ) ); 754 peer_id = tor && tor->peer_id ? tor->peer_id : tr_getPeerId( );755 peerIsGood = memcmp( handshake->peer_id,peer_id, PEER_ID_LEN ) != 0;746 tor_peer_id = tor && tor->peer_id ? tor->peer_id : tr_getPeerId( ); 747 peerIsGood = memcmp( peer_id, tor->peer_id, PEER_ID_LEN ) != 0; 756 748 dbgmsg( handshake, "isPeerGood == %d", peerIsGood ); 757 749 return tr_handshakeDone( handshake, peerIsGood ); … … 987 979 /* send our handshake */ 988 980 { 989 int msgSize; 990 uint8_t * msg = buildHandshakeMessage( handshake, &msgSize ); 991 tr_peerIoWriteBytes( handshake->io, outbuf, msg, msgSize ); 981 uint8_t msg[HANDSHAKE_SIZE]; 982 buildHandshakeMessage( handshake, msg ); 983 984 tr_peerIoWriteBytes( handshake->io, outbuf, msg, sizeof( msg ) ); 992 985 handshake->haveSentBitTorrentHandshake = 1; 993 tr_free( msg );994 986 } 995 987 … … 1107 1099 { 1108 1100 const uint8_t * peer_id = isConnected && handshake->havePeerID 1109 ? handshake->peer_id 1110 : NULL; 1111 const int success = ( *handshake->doneCB )( handshake, 1112 handshake->io, 1113 isConnected, 1114 peer_id, 1115 handshake-> 1116 doneUserData ); 1101 ? tr_peerIoGetPeersId( handshake->io ) 1102 : NULL; 1103 const int success = ( *handshake->doneCB )( handshake, 1104 handshake->io, 1105 isConnected, 1106 peer_id, 1107 handshake->doneUserData ); 1117 1108 1118 1109 return success; … … 1167 1158 && ( !tr_peerIoReconnect( handshake->io ) ) ) 1168 1159 { 1169 int msgSize;1170 uint8_t * msg; 1160 uint8_t msg[HANDSHAKE_SIZE]; 1161 1171 1162 dbgmsg( handshake, "handshake failed, trying plaintext..." ); 1172 msg = buildHandshakeMessage( handshake, &msgSize);1163 buildHandshakeMessage( handshake, msg ); 1173 1164 handshake->haveSentBitTorrentHandshake = 1; 1174 1165 setReadState( handshake, AWAITING_HANDSHAKE ); 1175 tr_peerIoWrite( handshake->io, msg, msgSize, FALSE ); 1176 tr_free( msg ); 1166 tr_peerIoWrite( handshake->io, msg, sizeof( msg ), FALSE ); 1177 1167 } 1178 1168 else … … 1222 1212 else 1223 1213 { 1224 int msgSize; 1225 uint8_t * msg = buildHandshakeMessage( handshake, &msgSize ); 1214 uint8_t msg[HANDSHAKE_SIZE]; 1215 buildHandshakeMessage( handshake, msg ); 1216 1226 1217 handshake->haveSentBitTorrentHandshake = 1; 1227 1218 setReadState( handshake, AWAITING_HANDSHAKE ); 1228 tr_peerIoWrite( handshake->io, msg, msgSize, FALSE ); 1229 tr_free( msg ); 1219 tr_peerIoWrite( handshake->io, msg, sizeof( msg ), FALSE ); 1230 1220 } 1231 1221
Note: See TracChangeset
for help on using the changeset viewer.