summaryrefslogtreecommitdiff
path: root/ot_udp.c
diff options
context:
space:
mode:
Diffstat (limited to 'ot_udp.c')
-rw-r--r--ot_udp.c27
1 files changed, 14 insertions, 13 deletions
diff --git a/ot_udp.c b/ot_udp.c
index 82aa8bd..e80f4e7 100644
--- a/ot_udp.c
+++ b/ot_udp.c
@@ -12,12 +12,12 @@
12 12
13/* Libowfat */ 13/* Libowfat */
14#include "io.h" 14#include "io.h"
15#include "ip6.h"
16#include "socket.h"
17 15
18/* Opentracker */ 16/* Opentracker */
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"
@@ -71,19 +71,20 @@ static void udp_make_connectionid(uint32_t connid[2], const ot_ip6 remoteip, int
71} 71}
72 72
73/* UDP implementation according to http://xbtt.sourceforge.net/udp_tracker_protocol.html */ 73/* UDP implementation according to http://xbtt.sourceforge.net/udp_tracker_protocol.html */
74int handle_udp6(int64 serversocket, struct ot_workstruct *ws) { 74int handle_udp6(int64_t serversocket, struct ot_workstruct *ws) {
75 ot_ip6 remoteip; 75 ot_ip6 remoteip;
76 uint32_t *inpacket = (uint32_t *)ws->inbuf; 76 uint32_t *inpacket = (uint32_t *)ws->inbuf;
77 uint32_t *outpacket = (uint32_t *)ws->outbuf; 77 uint32_t *outpacket = (uint32_t *)ws->outbuf;
78 uint32_t left, event, scopeid; 78 uint32_t left, event;
79 uint32_t connid[2]; 79 uint32_t connid[2];
80 uint32_t action; 80 uint32_t action;
81 uint16_t port, remoteport; 81 uint16_t port, remoteport;
82 size_t byte_count, scrape_count; 82 size_t byte_count, scrape_count;
83 83
84 byte_count = socket_recv6(serversocket, ws->inbuf, G_INBUF_SIZE, remoteip, &remoteport, &scopeid); 84 ssize_t result = socket_recv(serversocket, ws->inbuf, G_INBUF_SIZE, remoteip, &remoteport);
85 if (!byte_count) 85 if (result <= 0)
86 return 0; 86 return 0;
87 byte_count = (size_t)result;
87 88
88 stats_issue_event(EVENT_ACCEPT, FLAG_UDP, (uintptr_t)remoteip); 89 stats_issue_event(EVENT_ACCEPT, FLAG_UDP, (uintptr_t)remoteip);
89 stats_issue_event(EVENT_READ, FLAG_UDP, byte_count); 90 stats_issue_event(EVENT_READ, FLAG_UDP, byte_count);
@@ -117,7 +118,7 @@ int handle_udp6(int64 serversocket, struct ot_workstruct *ws) {
117 outpacket[0] = htonl(3); 118 outpacket[0] = htonl(3);
118 outpacket[1] = inpacket[3]; 119 outpacket[1] = inpacket[3];
119 memcpy(&outpacket[2], "Connection ID missmatch.", s); 120 memcpy(&outpacket[2], "Connection ID missmatch.", s);
120 socket_send6(serversocket, ws->outbuf, 8 + s, remoteip, remoteport, 0); 121 socket_send(serversocket, ws->outbuf, 8 + s, remoteip, remoteport);
121 stats_issue_event(EVENT_CONNID_MISSMATCH, FLAG_UDP, 8 + s); 122 stats_issue_event(EVENT_CONNID_MISSMATCH, FLAG_UDP, 8 + s);
122 return 1; 123 return 1;
123 } 124 }
@@ -134,7 +135,7 @@ int handle_udp6(int64 serversocket, struct ot_workstruct *ws) {
134 outpacket[2] = connid[0]; 135 outpacket[2] = connid[0];
135 outpacket[3] = connid[1]; 136 outpacket[3] = connid[1];
136 137
137 socket_send6(serversocket, ws->outbuf, 16, remoteip, remoteport, 0); 138 socket_send(serversocket, ws->outbuf, 16, remoteip, remoteport);
138 stats_issue_event(EVENT_CONNECT, FLAG_UDP, 16); 139 stats_issue_event(EVENT_CONNECT, FLAG_UDP, 16);
139 break; 140 break;
140 case 1: /* This is an announce action */ 141 case 1: /* This is an announce action */
@@ -176,7 +177,7 @@ int handle_udp6(int64 serversocket, struct ot_workstruct *ws) {
176 } else { 177 } else {
177 /* Limit amount of peers to OT_MAX_PEERS_UDP */ 178 /* Limit amount of peers to OT_MAX_PEERS_UDP */
178 uint32_t numwant = ntohl(inpacket[92 / 4]); 179 uint32_t numwant = ntohl(inpacket[92 / 4]);
179 size_t max_peers = ip6_isv4mapped(remoteip) ? OT_MAX_PEERS_UDP4 : OT_MAX_PEERS_UDP6; 180 size_t max_peers = ip_is_v4mapped(remoteip) ? OT_MAX_PEERS_UDP4 : OT_MAX_PEERS_UDP6;
180 if (numwant > max_peers) 181 if (numwant > max_peers)
181 numwant = max_peers; 182 numwant = max_peers;
182 183
@@ -184,7 +185,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); 185 ws->reply_size = 8 + add_peer_to_torrent_and_return_peers(FLAG_UDP, ws, numwant);
185 } 186 }
186 187
187 socket_send6(serversocket, ws->outbuf, ws->reply_size, remoteip, remoteport, 0); 188 socket_send(serversocket, ws->outbuf, ws->reply_size, remoteip, remoteport);
188 stats_issue_event(EVENT_ANNOUNCE, FLAG_UDP, ws->reply_size); 189 stats_issue_event(EVENT_ANNOUNCE, FLAG_UDP, ws->reply_size);
189 break; 190 break;
190 191
@@ -195,7 +196,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++) 196 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); 197 return_udp_scrape_for_torrent(*(ot_hash *)(((char *)inpacket) + 16 + 20 * scrape_count), ((char *)outpacket) + 8 + 12 * scrape_count);
197 198
198 socket_send6(serversocket, ws->outbuf, 8 + 12 * scrape_count, remoteip, remoteport, 0); 199 socket_send(serversocket, ws->outbuf, 8 + 12 * scrape_count, remoteip, remoteport);
199 stats_issue_event(EVENT_SCRAPE, FLAG_UDP, scrape_count); 200 stats_issue_event(EVENT_SCRAPE, FLAG_UDP, scrape_count);
200 break; 201 break;
201 } 202 }
@@ -203,7 +204,7 @@ int handle_udp6(int64 serversocket, struct ot_workstruct *ws) {
203} 204}
204 205
205static void *udp_worker(void *args) { 206static void *udp_worker(void *args) {
206 int64 sock = (int64)args; 207 int64_t sock = (int64_t)args;
207 struct ot_workstruct ws; 208 struct ot_workstruct ws;
208 memset(&ws, 0, sizeof(ws)); 209 memset(&ws, 0, sizeof(ws));
209 210
@@ -229,7 +230,7 @@ static void *udp_worker(void *args) {
229 return NULL; 230 return NULL;
230} 231}
231 232
232void udp_init(int64 sock, unsigned int worker_count) { 233void udp_init(int64_t sock, unsigned int worker_count) {
233 pthread_t thread_id; 234 pthread_t thread_id;
234 if (!g_rijndael_round_key[0]) 235 if (!g_rijndael_round_key[0])
235 udp_generate_rijndael_round_key(); 236 udp_generate_rijndael_round_key();