summaryrefslogtreecommitdiff
path: root/ot_udp.c
diff options
context:
space:
mode:
Diffstat (limited to 'ot_udp.c')
-rw-r--r--ot_udp.c26
1 files changed, 12 insertions, 14 deletions
diff --git a/ot_udp.c b/ot_udp.c
index d17f3ff..dc8267c 100644
--- a/ot_udp.c
+++ b/ot_udp.c
@@ -12,19 +12,16 @@
12 12
13/* Libowfat */ 13/* Libowfat */
14#include "io.h" 14#include "io.h"
15#include "socket.h"
16 15
17/* Opentracker */ 16/* Opentracker */
18#include "ot_ip.h" 17#include "ot_ip.h"
19#include "ot_random.h" 18#include "ot_random.h"
20#include "ot_rijndael.h" 19#include "ot_rijndael.h"
20#include "ot_socket.h"
21#include "ot_stats.h" 21#include "ot_stats.h"
22#include "ot_udp.h" 22#include "ot_udp.h"
23#include "trackerlogic.h" 23#include "trackerlogic.h"
24 24
25#if 0
26static const uint8_t g_static_connid[8] = { 0x23, 0x42, 0x05, 0x17, 0xde, 0x41, 0x50, 0xff };
27#endif
28static uint32_t g_rijndael_round_key[44] = {0}; 25static uint32_t g_rijndael_round_key[44] = {0};
29static volatile uint32_t g_key_of_the_hour[2] = {0}; 26static volatile uint32_t g_key_of_the_hour[2] = {0};
30static volatile ot_time g_hour_of_the_key; 27static volatile ot_time g_hour_of_the_key;
@@ -71,19 +68,20 @@ static void udp_make_connectionid(uint32_t connid[2], const ot_ip6 remoteip, int
71} 68}
72 69
73/* UDP implementation according to http://xbtt.sourceforge.net/udp_tracker_protocol.html */ 70/* UDP implementation according to http://xbtt.sourceforge.net/udp_tracker_protocol.html */
74int handle_udp6(int64 serversocket, struct ot_workstruct *ws) { 71int handle_udp6(int64_t serversocket, struct ot_workstruct *ws) {
75 ot_ip6 remoteip; 72 ot_ip6 remoteip;
76 uint32_t *inpacket = (uint32_t *)ws->inbuf; 73 uint32_t *inpacket = (uint32_t *)ws->inbuf;
77 uint32_t *outpacket = (uint32_t *)ws->outbuf; 74 uint32_t *outpacket = (uint32_t *)ws->outbuf;
78 uint32_t left, event, scopeid; 75 uint32_t left, event;
79 uint32_t connid[2]; 76 uint32_t connid[2];
80 uint32_t action; 77 uint32_t action;
81 uint16_t port, remoteport; 78 uint16_t port, remoteport;
82 size_t byte_count, scrape_count; 79 size_t byte_count, scrape_count;
83 80
84 byte_count = socket_recv6(serversocket, ws->inbuf, G_INBUF_SIZE, remoteip, &remoteport, &scopeid); 81 ssize_t result = socket_recv(serversocket, ws->inbuf, G_INBUF_SIZE, remoteip, &remoteport);
85 if (!byte_count) 82 if (result <= 0)
86 return 0; 83 return 0;
84 byte_count = (size_t)result;
87 85
88 stats_issue_event(EVENT_ACCEPT, FLAG_UDP, (uintptr_t)remoteip); 86 stats_issue_event(EVENT_ACCEPT, FLAG_UDP, (uintptr_t)remoteip);
89 stats_issue_event(EVENT_READ, FLAG_UDP, byte_count); 87 stats_issue_event(EVENT_READ, FLAG_UDP, byte_count);
@@ -117,7 +115,7 @@ int handle_udp6(int64 serversocket, struct ot_workstruct *ws) {
117 outpacket[0] = htonl(3); 115 outpacket[0] = htonl(3);
118 outpacket[1] = inpacket[3]; 116 outpacket[1] = inpacket[3];
119 memcpy(&outpacket[2], "Connection ID missmatch.", s); 117 memcpy(&outpacket[2], "Connection ID missmatch.", s);
120 socket_send6(serversocket, ws->outbuf, 8 + s, remoteip, remoteport, 0); 118 socket_send(serversocket, ws->outbuf, 8 + s, remoteip, remoteport);
121 stats_issue_event(EVENT_CONNID_MISSMATCH, FLAG_UDP, 8 + s); 119 stats_issue_event(EVENT_CONNID_MISSMATCH, FLAG_UDP, 8 + s);
122 return 1; 120 return 1;
123 } 121 }
@@ -134,7 +132,7 @@ int handle_udp6(int64 serversocket, struct ot_workstruct *ws) {
134 outpacket[2] = connid[0]; 132 outpacket[2] = connid[0];
135 outpacket[3] = connid[1]; 133 outpacket[3] = connid[1];
136 134
137 socket_send6(serversocket, ws->outbuf, 16, remoteip, remoteport, 0); 135 socket_send(serversocket, ws->outbuf, 16, remoteip, remoteport);
138 stats_issue_event(EVENT_CONNECT, FLAG_UDP, 16); 136 stats_issue_event(EVENT_CONNECT, FLAG_UDP, 16);
139 break; 137 break;
140 case 1: /* This is an announce action */ 138 case 1: /* This is an announce action */
@@ -184,7 +182,7 @@ int handle_udp6(int64 serversocket, struct ot_workstruct *ws) {
184 ws->reply_size = 8 + add_peer_to_torrent_and_return_peers(FLAG_UDP, ws, numwant); 182 ws->reply_size = 8 + add_peer_to_torrent_and_return_peers(FLAG_UDP, ws, numwant);
185 } 183 }
186 184
187 socket_send6(serversocket, ws->outbuf, ws->reply_size, remoteip, remoteport, 0); 185 socket_send(serversocket, ws->outbuf, ws->reply_size, remoteip, remoteport);
188 stats_issue_event(EVENT_ANNOUNCE, FLAG_UDP, ws->reply_size); 186 stats_issue_event(EVENT_ANNOUNCE, FLAG_UDP, ws->reply_size);
189 break; 187 break;
190 188
@@ -195,7 +193,7 @@ int handle_udp6(int64 serversocket, struct ot_workstruct *ws) {
195 for (scrape_count = 0; (scrape_count * 20 < byte_count - 16) && (scrape_count <= 74); scrape_count++) 193 for (scrape_count = 0; (scrape_count * 20 < byte_count - 16) && (scrape_count <= 74); scrape_count++)
196 return_udp_scrape_for_torrent(*(ot_hash *)(((char *)inpacket) + 16 + 20 * scrape_count), ((char *)outpacket) + 8 + 12 * scrape_count); 194 return_udp_scrape_for_torrent(*(ot_hash *)(((char *)inpacket) + 16 + 20 * scrape_count), ((char *)outpacket) + 8 + 12 * scrape_count);
197 195
198 socket_send6(serversocket, ws->outbuf, 8 + 12 * scrape_count, remoteip, remoteport, 0); 196 socket_send(serversocket, ws->outbuf, 8 + 12 * scrape_count, remoteip, remoteport);
199 stats_issue_event(EVENT_SCRAPE, FLAG_UDP, scrape_count); 197 stats_issue_event(EVENT_SCRAPE, FLAG_UDP, scrape_count);
200 break; 198 break;
201 } 199 }
@@ -203,7 +201,7 @@ int handle_udp6(int64 serversocket, struct ot_workstruct *ws) {
203} 201}
204 202
205static void *udp_worker(void *args) { 203static void *udp_worker(void *args) {
206 int64 sock = (int64)args; 204 int64_t sock = (int64_t)args;
207 struct ot_workstruct ws; 205 struct ot_workstruct ws;
208 memset(&ws, 0, sizeof(ws)); 206 memset(&ws, 0, sizeof(ws));
209 207
@@ -229,7 +227,7 @@ static void *udp_worker(void *args) {
229 return NULL; 227 return NULL;
230} 228}
231 229
232void udp_init(int64 sock, unsigned int worker_count) { 230void udp_init(int64_t sock, unsigned int worker_count) {
233 pthread_t thread_id; 231 pthread_t thread_id;
234 if (!g_rijndael_round_key[0]) 232 if (!g_rijndael_round_key[0])
235 udp_generate_rijndael_round_key(); 233 udp_generate_rijndael_round_key();