summaryrefslogtreecommitdiff
path: root/ot_udp.c
diff options
context:
space:
mode:
Diffstat (limited to 'ot_udp.c')
-rw-r--r--ot_udp.c39
1 files changed, 22 insertions, 17 deletions
diff --git a/ot_udp.c b/ot_udp.c
index a95a4fa..bc2d6cc 100644
--- a/ot_udp.c
+++ b/ot_udp.c
@@ -29,8 +29,6 @@ static void udp_make_connectionid( uint32_t * connid, const ot_ip6 remoteip ) {
29 29
30/* UDP implementation according to http://xbtt.sourceforge.net/udp_tracker_protocol.html */ 30/* UDP implementation according to http://xbtt.sourceforge.net/udp_tracker_protocol.html */
31void handle_udp6( int64 serversocket, struct ot_workstruct *ws ) { 31void handle_udp6( int64 serversocket, struct ot_workstruct *ws ) {
32 ot_peer peer;
33 ot_hash *hash = NULL;
34 ot_ip6 remoteip; 32 ot_ip6 remoteip;
35 uint32_t *inpacket = (uint32_t*)ws->inbuf; 33 uint32_t *inpacket = (uint32_t*)ws->inbuf;
36 uint32_t *outpacket = (uint32_t*)ws->outbuf; 34 uint32_t *outpacket = (uint32_t*)ws->outbuf;
@@ -43,6 +41,10 @@ void handle_udp6( int64 serversocket, struct ot_workstruct *ws ) {
43 stats_issue_event( EVENT_ACCEPT, FLAG_UDP, (uintptr_t)remoteip ); 41 stats_issue_event( EVENT_ACCEPT, FLAG_UDP, (uintptr_t)remoteip );
44 stats_issue_event( EVENT_READ, FLAG_UDP, byte_count ); 42 stats_issue_event( EVENT_READ, FLAG_UDP, byte_count );
45 43
44 /* Initialise hash pointer */
45 ws->hash = NULL;
46 ws->peer_id = NULL;
47
46 /* Minimum udp tracker packet size, also catches error */ 48 /* Minimum udp tracker packet size, also catches error */
47 if( byte_count < 16 ) 49 if( byte_count < 16 )
48 return; 50 return;
@@ -71,33 +73,36 @@ void handle_udp6( int64 serversocket, struct ot_workstruct *ws ) {
71 numwant = ntohl( inpacket[92/4] ); 73 numwant = ntohl( inpacket[92/4] );
72 if (numwant > 200) numwant = 200; 74 if (numwant > 200) numwant = 200;
73 75
74 event = ntohl( inpacket[80/4] ); 76 event = ntohl( inpacket[80/4] );
75 port = *(uint16_t*)( ((char*)inpacket) + 96 ); 77 port = *(uint16_t*)( ((char*)inpacket) + 96 );
76 hash = (ot_hash*)( ((char*)inpacket) + 16 ); 78 ws->hash = (ot_hash*)( ((char*)inpacket) + 16 );
77 79
78 OT_SETIP( &peer, remoteip ); 80 OT_SETIP( &ws->peer, remoteip );
79 OT_SETPORT( &peer, &port ); 81 OT_SETPORT( &ws->peer, &port );
80 OT_PEERFLAG( &peer ) = 0; 82 OT_PEERFLAG( &ws->peer ) = 0;
81 83
82 switch( event ) { 84 switch( event ) {
83 case 1: OT_PEERFLAG( &peer ) |= PEER_FLAG_COMPLETED; break; 85 case 1: OT_PEERFLAG( &ws->peer ) |= PEER_FLAG_COMPLETED; break;
84 case 3: OT_PEERFLAG( &peer ) |= PEER_FLAG_STOPPED; break; 86 case 3: OT_PEERFLAG( &ws->peer ) |= PEER_FLAG_STOPPED; break;
85 default: break; 87 default: break;
86 } 88 }
87 89
88 if( !left ) 90 if( !left )
89 OT_PEERFLAG( &peer ) |= PEER_FLAG_SEEDING; 91 OT_PEERFLAG( &ws->peer ) |= PEER_FLAG_SEEDING;
90 92
91 outpacket[0] = htonl( 1 ); /* announce action */ 93 outpacket[0] = htonl( 1 ); /* announce action */
92 outpacket[1] = inpacket[12/4]; 94 outpacket[1] = inpacket[12/4];
93 95
94 if( OT_PEERFLAG( &peer ) & PEER_FLAG_STOPPED ) /* Peer is gone. */ 96 if( OT_PEERFLAG( &ws->peer ) & PEER_FLAG_STOPPED ) { /* Peer is gone. */
95 byte_count = remove_peer_from_torrent( *hash, &peer, ws->outbuf, FLAG_UDP ); 97 ws->reply = ws->outbuf;
96 else 98 ws->reply_size = remove_peer_from_torrent( FLAG_UDP, ws );
97 byte_count = 8 + add_peer_to_torrent_and_return_peers( *hash, &peer, FLAG_UDP, numwant, ((char*)outpacket) + 8 ); 99 } else {
100 ws->reply = ws->outbuf + 8;
101 ws->reply_size = 8 + add_peer_to_torrent_and_return_peers( FLAG_UDP, ws, numwant );
102 }
98 103
99 socket_send6( serversocket, ws->outbuf, byte_count, remoteip, remoteport, 0 ); 104 socket_send6( serversocket, ws->outbuf, ws->reply_size, remoteip, remoteport, 0 );
100 stats_issue_event( EVENT_ANNOUNCE, FLAG_UDP, byte_count ); 105 stats_issue_event( EVENT_ANNOUNCE, FLAG_UDP, ws->reply_size );
101 break; 106 break;
102 107
103 case 2: /* This is a scrape action */ 108 case 2: /* This is a scrape action */