summaryrefslogtreecommitdiff
path: root/ot_udp.c
diff options
context:
space:
mode:
Diffstat (limited to 'ot_udp.c')
-rw-r--r--ot_udp.c29
1 files changed, 12 insertions, 17 deletions
diff --git a/ot_udp.c b/ot_udp.c
index fb171e7..81c4d63 100644
--- a/ot_udp.c
+++ b/ot_udp.c
@@ -17,9 +17,6 @@
17#include "ot_udp.h" 17#include "ot_udp.h"
18#include "ot_stats.h" 18#include "ot_stats.h"
19 19
20static char static_inbuf[8192];
21static char static_outbuf[8192];
22
23static const uint8_t g_static_connid[8] = { 0x23, 0x42, 0x05, 0x17, 0xde, 0x41, 0x50, 0xff }; 20static const uint8_t g_static_connid[8] = { 0x23, 0x42, 0x05, 0x17, 0xde, 0x41, 0x50, 0xff };
24 21
25static void udp_make_connectionid( uint32_t * connid, const char * remoteip ) { 22static void udp_make_connectionid( uint32_t * connid, const char * remoteip ) {
@@ -39,17 +36,17 @@ static int udp_test_connectionid( const uint32_t * const connid, const char * re
39} 36}
40 37
41/* UDP implementation according to http://xbtt.sourceforge.net/udp_tracker_protocol.html */ 38/* UDP implementation according to http://xbtt.sourceforge.net/udp_tracker_protocol.html */
42void handle_udp4( int64 serversocket ) { 39void handle_udp4( int64 serversocket, struct ot_workstruct *ws ) {
43 ot_peer peer; 40 ot_peer peer;
44 ot_hash *hash = NULL; 41 ot_hash *hash = NULL;
45 char remoteip[4]; 42 char remoteip[4];
46 uint32_t *inpacket = (uint32_t*)static_inbuf; 43 uint32_t *inpacket = (uint32_t*)ws->inbuf;
47 uint32_t *outpacket = (uint32_t*)static_outbuf; 44 uint32_t *outpacket = (uint32_t*)ws->outbuf;
48 uint32_t numwant, left, event; 45 uint32_t numwant, left, event;
49 uint16_t port, remoteport; 46 uint16_t port, remoteport;
50 size_t r, r_out; 47 size_t r, r_out;
51 48
52 r = socket_recv4( serversocket, static_inbuf, sizeof( static_inbuf ), remoteip, &remoteport); 49 r = socket_recv4( serversocket, ws->inbuf, ws->inbuf_size, remoteip, &remoteport);
53 50
54 stats_issue_event( EVENT_ACCEPT, FLAG_UDP, ntohl(*(uint32_t*)remoteip) ); 51 stats_issue_event( EVENT_ACCEPT, FLAG_UDP, ntohl(*(uint32_t*)remoteip) );
55 stats_issue_event( EVENT_READ, FLAG_UDP, r ); 52 stats_issue_event( EVENT_READ, FLAG_UDP, r );
@@ -58,8 +55,6 @@ void handle_udp4( int64 serversocket ) {
58 if( r < 16 ) 55 if( r < 16 )
59 return; 56 return;
60 57
61/* fprintf( stderr, "UDP Connection id: %16llX\n", *(uint64_t*)inpacket ); */
62
63 switch( ntohl( inpacket[2] ) ) { 58 switch( ntohl( inpacket[2] ) ) {
64 case 0: /* This is a connect action */ 59 case 0: /* This is a connect action */
65 /* look for udp bittorrent magic id */ 60 /* look for udp bittorrent magic id */
@@ -70,7 +65,7 @@ void handle_udp4( int64 serversocket ) {
70 outpacket[1] = inpacket[3]; 65 outpacket[1] = inpacket[3];
71 udp_make_connectionid( outpacket + 2, remoteip ); 66 udp_make_connectionid( outpacket + 2, remoteip );
72 67
73 socket_send4( serversocket, static_outbuf, 16, remoteip, remoteport ); 68 socket_send4( serversocket, ws->outbuf, 16, remoteip, remoteport );
74 stats_issue_event( EVENT_CONNECT, FLAG_UDP, 16 ); 69 stats_issue_event( EVENT_CONNECT, FLAG_UDP, 16 );
75 break; 70 break;
76 case 1: /* This is an announce action */ 71 case 1: /* This is an announce action */
@@ -88,8 +83,8 @@ void handle_udp4( int64 serversocket ) {
88 if (numwant > 200) numwant = 200; 83 if (numwant > 200) numwant = 200;
89 84
90 event = ntohl( inpacket[80/4] ); 85 event = ntohl( inpacket[80/4] );
91 port = *(uint16_t*)( static_inbuf + 96 ); 86 port = *(uint16_t*)( ((char*)inpacket) + 96 );
92 hash = (ot_hash*)( static_inbuf + 16 ); 87 hash = (ot_hash*)( ((char*)inpacket) + 16 );
93 88
94 OT_SETIP( &peer, remoteip ); 89 OT_SETIP( &peer, remoteip );
95 OT_SETPORT( &peer, &port ); 90 OT_SETPORT( &peer, &port );
@@ -108,11 +103,11 @@ void handle_udp4( int64 serversocket ) {
108 outpacket[1] = inpacket[12/4]; 103 outpacket[1] = inpacket[12/4];
109 104
110 if( OT_PEERFLAG( &peer ) & PEER_FLAG_STOPPED ) /* Peer is gone. */ 105 if( OT_PEERFLAG( &peer ) & PEER_FLAG_STOPPED ) /* Peer is gone. */
111 r = remove_peer_from_torrent( hash, &peer, static_outbuf, FLAG_UDP ); 106 r = remove_peer_from_torrent( hash, &peer, ws->outbuf, FLAG_UDP );
112 else 107 else
113 r = 8 + add_peer_to_torrent_and_return_peers( hash, &peer, FLAG_UDP, numwant, static_outbuf + 8 ); 108 r = 8 + add_peer_to_torrent_and_return_peers( hash, &peer, FLAG_UDP, numwant, ((char*)outpacket) + 8 );
114 109
115 socket_send4( serversocket, static_outbuf, r, remoteip, remoteport ); 110 socket_send4( serversocket, ws->outbuf, r, remoteip, remoteport );
116 stats_issue_event( EVENT_ANNOUNCE, FLAG_UDP, r ); 111 stats_issue_event( EVENT_ANNOUNCE, FLAG_UDP, r );
117 break; 112 break;
118 113
@@ -124,9 +119,9 @@ void handle_udp4( int64 serversocket ) {
124 outpacket[1] = inpacket[12/4]; 119 outpacket[1] = inpacket[12/4];
125 120
126 for( r_out = 0; ( r_out * 20 < r - 16) && ( r_out <= 74 ); r_out++ ) 121 for( r_out = 0; ( r_out * 20 < r - 16) && ( r_out <= 74 ); r_out++ )
127 return_udp_scrape_for_torrent( (ot_hash*)( static_inbuf + 16 + 20 * r_out ), static_outbuf + 8 + 12 * r_out ); 122 return_udp_scrape_for_torrent( (ot_hash*)( ((char*)inpacket) + 16 + 20 * r_out ), ((char*)outpacket) + 8 + 12 * r_out );
128 123
129 socket_send4( serversocket, static_outbuf, 8 + 12 * r_out, remoteip, remoteport ); 124 socket_send4( serversocket, ws->outbuf, 8 + 12 * r_out, remoteip, remoteport );
130 stats_issue_event( EVENT_SCRAPE, FLAG_UDP, r ); 125 stats_issue_event( EVENT_SCRAPE, FLAG_UDP, r );
131 break; 126 break;
132 } 127 }