From cc7482269feb12c275a156efd42dd6a3d6cb189d Mon Sep 17 00:00:00 2001 From: Dirk Engling Date: Sat, 28 Nov 2026 19:41:05 +0100 Subject: Add helpers to parse and generate udp packets and give them named sizes and offsets --- src/ot_udp.c | 169 +++++++++++++++++++++++++++++++++++------------------------ 1 file changed, 100 insertions(+), 69 deletions(-) (limited to 'src/ot_udp.c') diff --git a/src/ot_udp.c b/src/ot_udp.c index 2755766..2918c25 100644 --- a/src/ot_udp.c +++ b/src/ot_udp.c @@ -64,20 +64,71 @@ static void udp_make_connectionid(uint32_t connid[2], const ot_ip6 remoteip, int connid[1] = crypt[2] ^ crypt[3]; } +/* BEP 15 packet layouts, as byte offsets into the datagram */ +enum { + /* Every request starts alike */ + UDP_REQUEST_CONNECTION_ID_OFFSET = 0, + UDP_REQUEST_ACTION_OFFSET = 8, + UDP_REQUEST_TRANSACTION_ID_OFFSET = 12, + UDP_REQUEST_HEADER_SIZE = 16, + + UDP_ANNOUNCE_INFO_HASH_OFFSET = 16, + UDP_ANNOUNCE_LEFT_OFFSET = 64, /* 64 bit */ + UDP_ANNOUNCE_EVENT_OFFSET = 80, + UDP_ANNOUNCE_NUM_WANT_OFFSET = 92, + UDP_ANNOUNCE_PORT_OFFSET = 96, /* 16 bit */ + UDP_ANNOUNCE_SIZE = 98, + + UDP_SCRAPE_INFO_HASH_OFFSET = 16, + + /* And so does every reply */ + UDP_REPLY_ACTION_OFFSET = 0, + UDP_REPLY_TRANSACTION_ID_OFFSET = 4, + UDP_REPLY_HEADER_SIZE = 8 +}; + +enum { UDP_ACTION_CONNECT = 0, UDP_ACTION_ANNOUNCE = 1, UDP_ACTION_SCRAPE = 2, UDP_ACTION_ERROR = 3 }; +enum { UDP_EVENT_COMPLETED = 1, UDP_EVENT_STOPPED = 3 }; + +/* What a client puts where the connection id goes, when it asks for one */ +static const uint8_t g_udp_protocol_id[8] = {0x00, 0x00, 0x04, 0x17, 0x27, 0x10, 0x19, 0x80}; + +static uint32_t udp_read32(const char *packet, size_t offset) { + uint32_t value; + memcpy(&value, packet + offset, sizeof(value)); + return ntohl(value); +} + +/* Returns where the rest of the reply goes */ +static char *udp_reply_header(char *reply, uint32_t action, const char *request) { + action = htonl(action); + memcpy(reply + UDP_REPLY_ACTION_OFFSET, &action, sizeof(action)); + memcpy(reply + UDP_REPLY_TRANSACTION_ID_OFFSET, request + UDP_REQUEST_TRANSACTION_ID_OFFSET, sizeof(uint32_t)); + return reply + UDP_REPLY_HEADER_SIZE; +} + +static void udp_send_error(int64_t serversocket, struct ot_workstruct *ws, const char *message, const ot_ip6 remoteip, uint16_t remoteport) { + size_t size = strlen(message) + 1; + char *reply = udp_reply_header(ws->outbuf, UDP_ACTION_ERROR, ws->inbuf); + + memcpy(reply, message, size); + ws->reply_size = UDP_REPLY_HEADER_SIZE + size; + socket_send(serversocket, ws->outbuf, ws->reply_size, remoteip, remoteport); +} + /* UDP implementation according to http://xbtt.sourceforge.net/udp_tracker_protocol.html */ int handle_udp6(int64_t 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; - uint32_t connid[2]; - uint32_t action; - uint16_t port, remoteport; - size_t byte_count, scrape_count, i; - ot_hash *hashes; - char *reply; - - ssize_t result = socket_recv(serversocket, ws->inbuf, G_INBUF_SIZE, remoteip, &remoteport); + ot_ip6 remoteip; + const char *request = ws->inbuf; + char *reply; + uint32_t left, event, numwant; + uint32_t connid[2]; + uint32_t action; + uint16_t port, remoteport; + size_t byte_count, scrape_count, max_peers, i; + ot_hash *hashes; + + ssize_t result = socket_recv(serversocket, ws->inbuf, G_INBUF_SIZE, remoteip, &remoteport); if (result <= 0) return 0; byte_count = (size_t)result; @@ -86,12 +137,12 @@ int handle_udp6(int64_t serversocket, struct ot_workstruct *ws) { stats_issue_event(ws, EVENT_READ, FLAG_UDP, byte_count); /* Minimum udp tracker packet size, also catches error */ - if (byte_count < 16) + if (byte_count < UDP_REQUEST_HEADER_SIZE) return 1; /* Get action to take. Ignore error messages and broken packets */ - action = ntohl(inpacket[2]); - if (action > 2) + action = udp_read32(request, UDP_REQUEST_ACTION_OFFSET); + if (action > UDP_ACTION_SCRAPE) return 1; /* Generate the connection id we give out and expect to and from @@ -102,112 +153,92 @@ int handle_udp6(int64_t serversocket, struct ot_workstruct *ws) { ws->hash = NULL; ws->peer_id = NULL; - /* If action is not 0 (connect), then we expect the derived + /* If action is not connect, then we expect the derived connection id in first 64 bit */ - if ((action > 0) && (inpacket[0] != connid[0] || inpacket[1] != connid[1])) { + if (action != UDP_ACTION_CONNECT && memcmp(request + UDP_REQUEST_CONNECTION_ID_OFFSET, connid, sizeof(connid))) { /* 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 */ udp_make_connectionid(connid, remoteip, 1); - if (inpacket[0] != connid[0] || inpacket[1] != connid[1]) { - const char err_idmiss[] = "Connection ID missmatch."; - size_t s = sizeof(err_idmiss); - outpacket[0] = htonl(3); - outpacket[1] = inpacket[3]; - memcpy(&outpacket[2], &err_idmiss[0], s); - socket_send(serversocket, ws->outbuf, 8 + s, remoteip, remoteport); - stats_issue_event(ws, EVENT_CONNID_MISSMATCH, FLAG_UDP, 8 + s); + if (memcmp(request + UDP_REQUEST_CONNECTION_ID_OFFSET, connid, sizeof(connid))) { + udp_send_error(serversocket, ws, "Connection ID missmatch.", remoteip, remoteport); + stats_issue_event(ws, EVENT_CONNID_MISSMATCH, FLAG_UDP, ws->reply_size); return 1; } } switch (action) { - case 0: /* This is a connect action */ - /* look for udp bittorrent magic id */ - if ((ntohl(inpacket[0]) != 0x00000417) || (ntohl(inpacket[1]) != 0x27101980)) + case UDP_ACTION_CONNECT: + if (memcmp(request + UDP_REQUEST_CONNECTION_ID_OFFSET, g_udp_protocol_id, sizeof(g_udp_protocol_id))) return 1; - outpacket[0] = 0; - outpacket[1] = inpacket[3]; - outpacket[2] = connid[0]; - outpacket[3] = connid[1]; + reply = udp_reply_header(ws->outbuf, UDP_ACTION_CONNECT, request); + memcpy(reply, connid, sizeof(connid)); + reply += sizeof(connid); - socket_send(serversocket, ws->outbuf, 16, remoteip, remoteport); - stats_issue_event(ws, EVENT_CONNECT, FLAG_UDP, 16); + socket_send(serversocket, ws->outbuf, reply - ws->outbuf, remoteip, remoteport); + stats_issue_event(ws, EVENT_CONNECT, FLAG_UDP, reply - ws->outbuf); break; - case 1: /* This is an announce action */ - /* Minimum udp announce packet size */ - if (byte_count < 98) + + case UDP_ACTION_ANNOUNCE: + if (byte_count < UDP_ANNOUNCE_SIZE) return 1; /* We do only want to know, if it is zero */ - left = inpacket[64 / 4] | inpacket[68 / 4]; + left = udp_read32(request, UDP_ANNOUNCE_LEFT_OFFSET) | udp_read32(request, UDP_ANNOUNCE_LEFT_OFFSET + sizeof(uint32_t)); + event = udp_read32(request, UDP_ANNOUNCE_EVENT_OFFSET); + numwant = udp_read32(request, UDP_ANNOUNCE_NUM_WANT_OFFSET); + ws->hash = (ot_hash *)(ws->inbuf + UDP_ANNOUNCE_INFO_HASH_OFFSET); - event = ntohl(inpacket[80 / 4]); - port = *(uint16_t *)(((char *)inpacket) + 96); - ws->hash = (ot_hash *)(((char *)inpacket) + 16); + /* The peer keeps its port the way it came over the wire */ + memcpy(&port, request + UDP_ANNOUNCE_PORT_OFFSET, sizeof(port)); OT_SETIP(ws->peer, remoteip); OT_SETPORT(ws->peer, &port); OT_PEERFLAG(ws->peer) = 0; - switch (event) { - case 1: + if (event == UDP_EVENT_COMPLETED) OT_PEERFLAG(ws->peer) |= PEER_FLAG_COMPLETED; - break; - case 3: + if (event == UDP_EVENT_STOPPED) OT_PEERFLAG(ws->peer) |= PEER_FLAG_STOPPED; - break; - default: - break; - } - if (!left) OT_PEERFLAG(ws->peer) |= PEER_FLAG_SEEDING; - outpacket[0] = htonl(1); /* announce action */ - outpacket[1] = inpacket[12 / 4]; + reply = udp_reply_header(ws->outbuf, UDP_ACTION_ANNOUNCE, request); if (OT_PEERFLAG(ws->peer) & PEER_FLAG_STOPPED) { /* Peer is gone. */ + /* This one writes the whole reply, and leaves the header alone */ 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 = ip_is_v4mapped(remoteip) ? OT_MAX_PEERS_UDP4 : OT_MAX_PEERS_UDP6; + /* List of peers should fit in a single UDP packet */ + max_peers = ip_is_v4mapped(remoteip) ? OT_MAX_PEERS_UDP4 : OT_MAX_PEERS_UDP6; if (numwant > max_peers) numwant = max_peers; - ws->reply = ws->outbuf + 8; + ws->reply = reply; ws->reply_size = add_peer_to_torrent_and_return_peers(FLAG_UDP, ws, numwant); /* The announce path answers a refusal itself, so nothing is left here but a tracker that could not get the memory to track the torrent */ if (!ws->reply_size) { - const char err_resources[] = "Tracker is out of resources, try again later."; - size_t s = sizeof(err_resources); - outpacket[0] = htonl(3); - memcpy(&outpacket[2], &err_resources[0], s); - socket_send(serversocket, ws->outbuf, 8 + s, remoteip, remoteport); + udp_send_error(serversocket, ws, "Tracker is out of resources, try again later.", remoteip, remoteport); return 1; } - ws->reply_size += 8; + ws->reply_size += UDP_REPLY_HEADER_SIZE; } socket_send(serversocket, ws->outbuf, ws->reply_size, remoteip, remoteport); stats_issue_event(ws, EVENT_ANNOUNCE, FLAG_UDP, ws->reply_size); break; - case 2: /* This is a scrape action */ - scrape_count = (byte_count - 16) / sizeof(ot_hash); + case UDP_ACTION_SCRAPE: + scrape_count = (byte_count - UDP_SCRAPE_INFO_HASH_OFFSET) / sizeof(ot_hash); if (scrape_count > OT_MAX_SCRAPE_UDP) scrape_count = OT_MAX_SCRAPE_UDP; - outpacket[0] = htonl(2); /* scrape action */ - outpacket[1] = inpacket[12 / 4]; - - hashes = (ot_hash *)(ws->inbuf + 16); - reply = ws->outbuf + 8; + hashes = (ot_hash *)(ws->inbuf + UDP_SCRAPE_INFO_HASH_OFFSET); + reply = udp_reply_header(ws->outbuf, UDP_ACTION_SCRAPE, request); for (i = 0; i < scrape_count; ++i) reply += return_udp_scrape_for_torrent(hashes[i], reply); -- cgit v1.3