1
0
mirror of git://erdgeist.org/opentracker synced 2025-01-12 16:00:06 +00:00
opentracker/ot_udp.c

237 lines
7.1 KiB
C
Raw Normal View History

2007-11-06 17:51:48 +00:00
/* This software was written by Dirk Engling <erdgeist@erdgeist.org>
2007-12-20 05:59:34 +00:00
It is considered beerware. Prost. Skol. Cheers or whatever.
2008-10-28 01:27:22 +00:00
2007-12-20 05:59:34 +00:00
$id$ */
2007-11-06 17:51:48 +00:00
/* System */
#include <arpa/inet.h>
2024-04-14 22:39:02 +00:00
#include <pthread.h>
#include <stdio.h>
2024-04-14 22:39:02 +00:00
#include <stdlib.h>
#include <string.h>
2007-11-06 17:51:48 +00:00
/* Libowfat */
#include "io.h"
#include "ip6.h"
2024-04-14 22:39:02 +00:00
#include "socket.h"
2007-11-06 17:51:48 +00:00
/* Opentracker */
#include "ot_rijndael.h"
2024-04-14 22:39:02 +00:00
#include "ot_stats.h"
#include "ot_udp.h"
#include "trackerlogic.h"
2007-11-06 17:51:48 +00:00
#if 0
static const uint8_t g_static_connid[8] = { 0x23, 0x42, 0x05, 0x17, 0xde, 0x41, 0x50, 0xff };
#endif
static uint32_t g_rijndael_round_key[44] = {0};
2024-04-14 22:39:02 +00:00
static uint32_t g_key_of_the_hour[2] = {0};
static ot_time g_hour_of_the_key;
2024-04-14 22:39:02 +00:00
static void udp_generate_rijndael_round_key() {
2014-10-06 22:01:30 +00:00
uint32_t key[16];
#ifdef WANT_ARC4RANDOM
arc4random_buf(&key[0], sizeof(key));
#else
2014-10-06 22:01:30 +00:00
key[0] = random();
key[1] = random();
key[2] = random();
key[3] = random();
#endif
2024-04-14 22:39:02 +00:00
rijndaelKeySetupEnc128(g_rijndael_round_key, (uint8_t *)key);
#ifdef WANT_ARC4RANDOM
g_key_of_the_hour[0] = arc4random();
#else
g_key_of_the_hour[0] = random();
#endif
g_hour_of_the_key = g_now_minutes;
}
/* Generate current and previous connection id for ip */
2024-04-14 22:39:02 +00:00
static void udp_make_connectionid(uint32_t connid[2], const ot_ip6 remoteip, int age) {
uint32_t plain[4], crypt[4];
2024-04-14 22:39:02 +00:00
int i;
if (g_now_minutes + 60 > g_hour_of_the_key) {
g_hour_of_the_key = g_now_minutes;
g_key_of_the_hour[1] = g_key_of_the_hour[0];
#ifdef WANT_ARC4RANDOM
2024-04-14 22:39:02 +00:00
g_key_of_the_hour[0] = arc4random();
#else
2024-04-14 22:39:02 +00:00
g_key_of_the_hour[0] = random();
#endif
}
2024-04-14 22:39:02 +00:00
memcpy(plain, remoteip, sizeof(plain));
for (i = 0; i < 4; ++i)
plain[i] ^= g_key_of_the_hour[age];
rijndaelEncrypt128(g_rijndael_round_key, (uint8_t *)remoteip, (uint8_t *)crypt);
connid[0] = crypt[0] ^ crypt[1];
connid[1] = crypt[2] ^ crypt[3];
}
2007-11-06 17:51:48 +00:00
/* UDP implementation according to http://xbtt.sourceforge.net/udp_tracker_protocol.html */
2024-04-14 22:39:02 +00:00
int handle_udp6(int64 serversocket, struct ot_workstruct *ws) {
ot_ip6 remoteip;
uint32_t *inpacket = (uint32_t *)ws->inbuf;
uint32_t *outpacket = (uint32_t *)ws->outbuf;
uint32_t left, event, scopeid;
uint32_t connid[2];
uint32_t action;
uint16_t port, remoteport;
size_t byte_count, scrape_count;
byte_count = socket_recv6(serversocket, ws->inbuf, G_INBUF_SIZE, remoteip, &remoteport, &scopeid);
if (!byte_count)
return 0;
stats_issue_event(EVENT_ACCEPT, FLAG_UDP, (uintptr_t)remoteip);
stats_issue_event(EVENT_READ, FLAG_UDP, byte_count);
2007-11-06 17:51:48 +00:00
/* Minimum udp tracker packet size, also catches error */
2024-04-14 22:39:02 +00:00
if (byte_count < 16)
return 1;
/* Get action to take. Ignore error messages and broken packets */
2024-04-14 22:39:02 +00:00
action = ntohl(inpacket[2]);
if (action > 2)
return 1;
/* Generate the connection id we give out and expect to and from
the requesting ip address, this prevents udp spoofing */
2024-04-14 22:39:02 +00:00
udp_make_connectionid(connid, remoteip, 0);
** struct ot_workstruct gets ritcher (and will become even ritcher soon). This is where we encapsulate all per-request data from peer to hash to peer_id, so that it is available everywhere without passing hundreds of pointers down the stack. Most functions that do work down the stack now accept an ot_workstruct and some flags. So it can end up in the stats/event-handler where it will be the default parameter in the future. ** peer_id is now being copied by default and moved to ot_workstruct So it is available in stats and subsequent functions. ** sync scrape madness is gone SYNC_SCRAPE was intended to sync tracker state that would normally be lost on restarts i.e. downloaded counts per torrent. The way was to push it in the tracker cloud after finding all neighbouring trackers. This is madness. It never was tested and can be done per tracker by fetching stats/mode=statedump from time to time and starting opentracker with the -l option later. ** livesync thread has its own ot_workstruct now So it can behave like ot_udp and ot_http against trackerlogic.c and get rid of the first half of the embarrassing global variables. The sending half will be fixed soon [tm]. ** stats can log completed events The author recognizes the needs of original content distributors to keep track of the amount of times a work has been downloaded. While not feasible and used on openbittorrent and other open and anonymous tracker installations, a tracker user can now choose to send those events to syslog.
2010-04-22 22:08:42 +00:00
/* Initialise hash pointer */
2024-04-14 22:39:02 +00:00
ws->hash = NULL;
** struct ot_workstruct gets ritcher (and will become even ritcher soon). This is where we encapsulate all per-request data from peer to hash to peer_id, so that it is available everywhere without passing hundreds of pointers down the stack. Most functions that do work down the stack now accept an ot_workstruct and some flags. So it can end up in the stats/event-handler where it will be the default parameter in the future. ** peer_id is now being copied by default and moved to ot_workstruct So it is available in stats and subsequent functions. ** sync scrape madness is gone SYNC_SCRAPE was intended to sync tracker state that would normally be lost on restarts i.e. downloaded counts per torrent. The way was to push it in the tracker cloud after finding all neighbouring trackers. This is madness. It never was tested and can be done per tracker by fetching stats/mode=statedump from time to time and starting opentracker with the -l option later. ** livesync thread has its own ot_workstruct now So it can behave like ot_udp and ot_http against trackerlogic.c and get rid of the first half of the embarrassing global variables. The sending half will be fixed soon [tm]. ** stats can log completed events The author recognizes the needs of original content distributors to keep track of the amount of times a work has been downloaded. While not feasible and used on openbittorrent and other open and anonymous tracker installations, a tracker user can now choose to send those events to syslog.
2010-04-22 22:08:42 +00:00
ws->peer_id = NULL;
/* If action is not 0 (connect), then we expect the derived
connection id in first 64 bit */
2024-04-14 22:39:02 +00:00
if ((action > 0) && (inpacket[0] != connid[0] || inpacket[1] != connid[1])) {
/* If connection id does not match, try the one that was
valid in the previous hour. Only if this also does not
match, return an error packet */
2024-04-14 22:39:02 +00:00
udp_make_connectionid(connid, remoteip, 1);
if (inpacket[0] != connid[0] || inpacket[1] != connid[1]) {
const size_t s = sizeof("Connection ID missmatch.");
outpacket[0] = htonl(3);
outpacket[1] = inpacket[3];
memcpy(&outpacket[2], "Connection ID missmatch.", s);
socket_send6(serversocket, ws->outbuf, 8 + s, remoteip, remoteport, 0);
stats_issue_event(EVENT_CONNID_MISSMATCH, FLAG_UDP, 8 + s);
return 1;
}
}
2007-11-06 17:51:48 +00:00
2024-04-14 22:39:02 +00:00
switch (action) {
case 0: /* This is a connect action */
/* look for udp bittorrent magic id */
if ((ntohl(inpacket[0]) != 0x00000417) || (ntohl(inpacket[1]) != 0x27101980))
return 1;
outpacket[0] = 0;
outpacket[1] = inpacket[3];
outpacket[2] = connid[0];
outpacket[3] = connid[1];
socket_send6(serversocket, ws->outbuf, 16, remoteip, remoteport, 0);
stats_issue_event(EVENT_CONNECT, FLAG_UDP, 16);
break;
case 1: /* This is an announce action */
/* Minimum udp announce packet size */
if (byte_count < 98)
return 1;
/* We do only want to know, if it is zero */
left = inpacket[64 / 4] | inpacket[68 / 4];
event = ntohl(inpacket[80 / 4]);
port = *(uint16_t *)(((char *)inpacket) + 96);
ws->hash = (ot_hash *)(((char *)inpacket) + 16);
2007-12-15 17:32:44 +00:00
2024-04-14 22:39:02 +00:00
OT_SETIP(ws->peer, remoteip);
OT_SETPORT(ws->peer, &port);
OT_PEERFLAG(ws->peer) = 0;
2007-12-15 17:32:44 +00:00
2024-04-14 22:39:02 +00:00
switch (event) {
case 1:
OT_PEERFLAG(ws->peer) |= PEER_FLAG_COMPLETED;
2007-11-06 17:51:48 +00:00
break;
2024-04-14 22:39:02 +00:00
case 3:
OT_PEERFLAG(ws->peer) |= PEER_FLAG_STOPPED;
2007-11-06 17:51:48 +00:00
break;
2024-04-14 22:39:02 +00:00
default:
break;
}
2007-11-06 17:51:48 +00:00
2024-04-14 22:39:02 +00:00
if (!left)
OT_PEERFLAG(ws->peer) |= PEER_FLAG_SEEDING;
2007-11-06 17:51:48 +00:00
2024-04-14 22:39:02 +00:00
outpacket[0] = htonl(1); /* announce action */
outpacket[1] = inpacket[12 / 4];
2007-11-06 17:51:48 +00:00
2024-04-14 22:39:02 +00:00
if (OT_PEERFLAG(ws->peer) & PEER_FLAG_STOPPED) { /* Peer is gone. */
ws->reply = ws->outbuf;
ws->reply_size = remove_peer_from_torrent(FLAG_UDP, ws);
} else {
/* Limit amount of peers to OT_MAX_PEERS_UDP */
uint32_t numwant = ntohl(inpacket[92 / 4]);
size_t max_peers = ip6_isv4mapped(remoteip) ? OT_MAX_PEERS_UDP4 : OT_MAX_PEERS_UDP6;
if (numwant > max_peers)
numwant = max_peers;
ws->reply = ws->outbuf + 8;
ws->reply_size = 8 + add_peer_to_torrent_and_return_peers(FLAG_UDP, ws, numwant);
}
socket_send6(serversocket, ws->outbuf, ws->reply_size, remoteip, remoteport, 0);
stats_issue_event(EVENT_ANNOUNCE, FLAG_UDP, ws->reply_size);
break;
case 2: /* This is a scrape action */
outpacket[0] = htonl(2); /* scrape action */
outpacket[1] = inpacket[12 / 4];
for (scrape_count = 0; (scrape_count * 20 < byte_count - 16) && (scrape_count <= 74); scrape_count++)
return_udp_scrape_for_torrent(*(ot_hash *)(((char *)inpacket) + 16 + 20 * scrape_count), ((char *)outpacket) + 8 + 12 * scrape_count);
socket_send6(serversocket, ws->outbuf, 8 + 12 * scrape_count, remoteip, remoteport, 0);
stats_issue_event(EVENT_SCRAPE, FLAG_UDP, scrape_count);
break;
2007-11-06 17:51:48 +00:00
}
return 1;
2007-11-06 17:51:48 +00:00
}
2007-12-20 05:59:34 +00:00
2024-04-14 22:39:02 +00:00
static void *udp_worker(void *args) {
int64 sock = (int64)args;
struct ot_workstruct ws;
2024-04-14 22:39:02 +00:00
memset(&ws, 0, sizeof(ws));
2024-04-14 22:39:02 +00:00
ws.inbuf = malloc(G_INBUF_SIZE);
ws.outbuf = malloc(G_OUTBUF_SIZE);
#ifdef _DEBUG_HTTPERROR
ws.debugbuf = malloc(G_DEBUGBUF_SIZE);
#endif
2024-04-14 22:39:02 +00:00
while (g_opentracker_running)
handle_udp6(sock, &ws);
2024-04-14 22:39:02 +00:00
free(ws.inbuf);
free(ws.outbuf);
#ifdef _DEBUG_HTTPERROR
free(ws.debugbuf);
#endif
return NULL;
}
2024-04-14 22:39:02 +00:00
void udp_init(int64 sock, unsigned int worker_count) {
pthread_t thread_id;
2024-04-14 22:39:02 +00:00
if (!g_rijndael_round_key[0])
udp_generate_rijndael_round_key();
#ifdef _DEBUG
2024-04-14 22:39:02 +00:00
fprintf(stderr, " installing %d workers on udp socket %ld\n", worker_count, (unsigned long)sock);
#endif
2024-04-14 22:39:02 +00:00
while (worker_count--)
pthread_create(&thread_id, NULL, udp_worker, (void *)sock);
}