diff options
| author | Dirk Engling <erdgeist@erdgeist.org> | 2026-11-28 19:41:05 +0100 |
|---|---|---|
| committer | Dirk Engling <erdgeist@erdgeist.org> | 2026-11-28 19:41:05 +0100 |
| commit | cc7482269feb12c275a156efd42dd6a3d6cb189d (patch) | |
| tree | 77dc76994c71f25b5f6e3752d6f5ba9f8094c4e6 | |
| parent | 2a1b6673ffc96912322974cc85b93d13b6cd8e7c (diff) | |
Add helpers to parse and generate udp packets and give them named sizes and offsets
| -rw-r--r-- | src/ot_udp.c | 167 |
1 files changed, 99 insertions, 68 deletions
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 | |||
| 64 | connid[1] = crypt[2] ^ crypt[3]; | 64 | connid[1] = crypt[2] ^ crypt[3]; |
| 65 | } | 65 | } |
| 66 | 66 | ||
| 67 | /* BEP 15 packet layouts, as byte offsets into the datagram */ | ||
| 68 | enum { | ||
| 69 | /* Every request starts alike */ | ||
| 70 | UDP_REQUEST_CONNECTION_ID_OFFSET = 0, | ||
| 71 | UDP_REQUEST_ACTION_OFFSET = 8, | ||
| 72 | UDP_REQUEST_TRANSACTION_ID_OFFSET = 12, | ||
| 73 | UDP_REQUEST_HEADER_SIZE = 16, | ||
| 74 | |||
| 75 | UDP_ANNOUNCE_INFO_HASH_OFFSET = 16, | ||
| 76 | UDP_ANNOUNCE_LEFT_OFFSET = 64, /* 64 bit */ | ||
| 77 | UDP_ANNOUNCE_EVENT_OFFSET = 80, | ||
| 78 | UDP_ANNOUNCE_NUM_WANT_OFFSET = 92, | ||
| 79 | UDP_ANNOUNCE_PORT_OFFSET = 96, /* 16 bit */ | ||
| 80 | UDP_ANNOUNCE_SIZE = 98, | ||
| 81 | |||
| 82 | UDP_SCRAPE_INFO_HASH_OFFSET = 16, | ||
| 83 | |||
| 84 | /* And so does every reply */ | ||
| 85 | UDP_REPLY_ACTION_OFFSET = 0, | ||
| 86 | UDP_REPLY_TRANSACTION_ID_OFFSET = 4, | ||
| 87 | UDP_REPLY_HEADER_SIZE = 8 | ||
| 88 | }; | ||
| 89 | |||
| 90 | enum { UDP_ACTION_CONNECT = 0, UDP_ACTION_ANNOUNCE = 1, UDP_ACTION_SCRAPE = 2, UDP_ACTION_ERROR = 3 }; | ||
| 91 | enum { UDP_EVENT_COMPLETED = 1, UDP_EVENT_STOPPED = 3 }; | ||
| 92 | |||
| 93 | /* What a client puts where the connection id goes, when it asks for one */ | ||
| 94 | static const uint8_t g_udp_protocol_id[8] = {0x00, 0x00, 0x04, 0x17, 0x27, 0x10, 0x19, 0x80}; | ||
| 95 | |||
| 96 | static uint32_t udp_read32(const char *packet, size_t offset) { | ||
| 97 | uint32_t value; | ||
| 98 | memcpy(&value, packet + offset, sizeof(value)); | ||
| 99 | return ntohl(value); | ||
| 100 | } | ||
| 101 | |||
| 102 | /* Returns where the rest of the reply goes */ | ||
| 103 | static char *udp_reply_header(char *reply, uint32_t action, const char *request) { | ||
| 104 | action = htonl(action); | ||
| 105 | memcpy(reply + UDP_REPLY_ACTION_OFFSET, &action, sizeof(action)); | ||
| 106 | memcpy(reply + UDP_REPLY_TRANSACTION_ID_OFFSET, request + UDP_REQUEST_TRANSACTION_ID_OFFSET, sizeof(uint32_t)); | ||
| 107 | return reply + UDP_REPLY_HEADER_SIZE; | ||
| 108 | } | ||
| 109 | |||
| 110 | static void udp_send_error(int64_t serversocket, struct ot_workstruct *ws, const char *message, const ot_ip6 remoteip, uint16_t remoteport) { | ||
| 111 | size_t size = strlen(message) + 1; | ||
| 112 | char *reply = udp_reply_header(ws->outbuf, UDP_ACTION_ERROR, ws->inbuf); | ||
| 113 | |||
| 114 | memcpy(reply, message, size); | ||
| 115 | ws->reply_size = UDP_REPLY_HEADER_SIZE + size; | ||
| 116 | socket_send(serversocket, ws->outbuf, ws->reply_size, remoteip, remoteport); | ||
| 117 | } | ||
| 118 | |||
| 67 | /* UDP implementation according to http://xbtt.sourceforge.net/udp_tracker_protocol.html */ | 119 | /* UDP implementation according to http://xbtt.sourceforge.net/udp_tracker_protocol.html */ |
| 68 | int handle_udp6(int64_t serversocket, struct ot_workstruct *ws) { | 120 | int handle_udp6(int64_t serversocket, struct ot_workstruct *ws) { |
| 69 | ot_ip6 remoteip; | 121 | ot_ip6 remoteip; |
| 70 | uint32_t *inpacket = (uint32_t *)ws->inbuf; | 122 | const char *request = ws->inbuf; |
| 71 | uint32_t *outpacket = (uint32_t *)ws->outbuf; | 123 | char *reply; |
| 72 | uint32_t left, event; | 124 | uint32_t left, event, numwant; |
| 73 | uint32_t connid[2]; | 125 | uint32_t connid[2]; |
| 74 | uint32_t action; | 126 | uint32_t action; |
| 75 | uint16_t port, remoteport; | 127 | uint16_t port, remoteport; |
| 76 | size_t byte_count, scrape_count, i; | 128 | size_t byte_count, scrape_count, max_peers, i; |
| 77 | ot_hash *hashes; | 129 | ot_hash *hashes; |
| 78 | char *reply; | ||
| 79 | 130 | ||
| 80 | ssize_t result = socket_recv(serversocket, ws->inbuf, G_INBUF_SIZE, remoteip, &remoteport); | 131 | ssize_t result = socket_recv(serversocket, ws->inbuf, G_INBUF_SIZE, remoteip, &remoteport); |
| 81 | if (result <= 0) | 132 | if (result <= 0) |
| 82 | return 0; | 133 | return 0; |
| 83 | byte_count = (size_t)result; | 134 | byte_count = (size_t)result; |
| @@ -86,12 +137,12 @@ int handle_udp6(int64_t serversocket, struct ot_workstruct *ws) { | |||
| 86 | stats_issue_event(ws, EVENT_READ, FLAG_UDP, byte_count); | 137 | stats_issue_event(ws, EVENT_READ, FLAG_UDP, byte_count); |
| 87 | 138 | ||
| 88 | /* Minimum udp tracker packet size, also catches error */ | 139 | /* Minimum udp tracker packet size, also catches error */ |
| 89 | if (byte_count < 16) | 140 | if (byte_count < UDP_REQUEST_HEADER_SIZE) |
| 90 | return 1; | 141 | return 1; |
| 91 | 142 | ||
| 92 | /* Get action to take. Ignore error messages and broken packets */ | 143 | /* Get action to take. Ignore error messages and broken packets */ |
| 93 | action = ntohl(inpacket[2]); | 144 | action = udp_read32(request, UDP_REQUEST_ACTION_OFFSET); |
| 94 | if (action > 2) | 145 | if (action > UDP_ACTION_SCRAPE) |
| 95 | return 1; | 146 | return 1; |
| 96 | 147 | ||
| 97 | /* Generate the connection id we give out and expect to and from | 148 | /* 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) { | |||
| 102 | ws->hash = NULL; | 153 | ws->hash = NULL; |
| 103 | ws->peer_id = NULL; | 154 | ws->peer_id = NULL; |
| 104 | 155 | ||
| 105 | /* If action is not 0 (connect), then we expect the derived | 156 | /* If action is not connect, then we expect the derived |
| 106 | connection id in first 64 bit */ | 157 | connection id in first 64 bit */ |
| 107 | if ((action > 0) && (inpacket[0] != connid[0] || inpacket[1] != connid[1])) { | 158 | if (action != UDP_ACTION_CONNECT && memcmp(request + UDP_REQUEST_CONNECTION_ID_OFFSET, connid, sizeof(connid))) { |
| 108 | /* If connection id does not match, try the one that was | 159 | /* If connection id does not match, try the one that was |
| 109 | valid in the previous hour. Only if this also does not | 160 | valid in the previous hour. Only if this also does not |
| 110 | match, return an error packet */ | 161 | match, return an error packet */ |
| 111 | udp_make_connectionid(connid, remoteip, 1); | 162 | udp_make_connectionid(connid, remoteip, 1); |
| 112 | if (inpacket[0] != connid[0] || inpacket[1] != connid[1]) { | 163 | if (memcmp(request + UDP_REQUEST_CONNECTION_ID_OFFSET, connid, sizeof(connid))) { |
| 113 | const char err_idmiss[] = "Connection ID missmatch."; | 164 | udp_send_error(serversocket, ws, "Connection ID missmatch.", remoteip, remoteport); |
| 114 | size_t s = sizeof(err_idmiss); | 165 | stats_issue_event(ws, EVENT_CONNID_MISSMATCH, FLAG_UDP, ws->reply_size); |
| 115 | outpacket[0] = htonl(3); | ||
| 116 | outpacket[1] = inpacket[3]; | ||
| 117 | memcpy(&outpacket[2], &err_idmiss[0], s); | ||
| 118 | socket_send(serversocket, ws->outbuf, 8 + s, remoteip, remoteport); | ||
| 119 | stats_issue_event(ws, EVENT_CONNID_MISSMATCH, FLAG_UDP, 8 + s); | ||
| 120 | return 1; | 166 | return 1; |
| 121 | } | 167 | } |
| 122 | } | 168 | } |
| 123 | 169 | ||
| 124 | switch (action) { | 170 | switch (action) { |
| 125 | case 0: /* This is a connect action */ | 171 | case UDP_ACTION_CONNECT: |
| 126 | /* look for udp bittorrent magic id */ | 172 | if (memcmp(request + UDP_REQUEST_CONNECTION_ID_OFFSET, g_udp_protocol_id, sizeof(g_udp_protocol_id))) |
| 127 | if ((ntohl(inpacket[0]) != 0x00000417) || (ntohl(inpacket[1]) != 0x27101980)) | ||
| 128 | return 1; | 173 | return 1; |
| 129 | 174 | ||
| 130 | outpacket[0] = 0; | 175 | reply = udp_reply_header(ws->outbuf, UDP_ACTION_CONNECT, request); |
| 131 | outpacket[1] = inpacket[3]; | 176 | memcpy(reply, connid, sizeof(connid)); |
| 132 | outpacket[2] = connid[0]; | 177 | reply += sizeof(connid); |
| 133 | outpacket[3] = connid[1]; | ||
| 134 | 178 | ||
| 135 | socket_send(serversocket, ws->outbuf, 16, remoteip, remoteport); | 179 | socket_send(serversocket, ws->outbuf, reply - ws->outbuf, remoteip, remoteport); |
| 136 | stats_issue_event(ws, EVENT_CONNECT, FLAG_UDP, 16); | 180 | stats_issue_event(ws, EVENT_CONNECT, FLAG_UDP, reply - ws->outbuf); |
| 137 | break; | 181 | break; |
| 138 | case 1: /* This is an announce action */ | 182 | |
| 139 | /* Minimum udp announce packet size */ | 183 | case UDP_ACTION_ANNOUNCE: |
| 140 | if (byte_count < 98) | 184 | if (byte_count < UDP_ANNOUNCE_SIZE) |
| 141 | return 1; | 185 | return 1; |
| 142 | 186 | ||
| 143 | /* We do only want to know, if it is zero */ | 187 | /* We do only want to know, if it is zero */ |
| 144 | left = inpacket[64 / 4] | inpacket[68 / 4]; | 188 | left = udp_read32(request, UDP_ANNOUNCE_LEFT_OFFSET) | udp_read32(request, UDP_ANNOUNCE_LEFT_OFFSET + sizeof(uint32_t)); |
| 189 | event = udp_read32(request, UDP_ANNOUNCE_EVENT_OFFSET); | ||
| 190 | numwant = udp_read32(request, UDP_ANNOUNCE_NUM_WANT_OFFSET); | ||
| 191 | ws->hash = (ot_hash *)(ws->inbuf + UDP_ANNOUNCE_INFO_HASH_OFFSET); | ||
| 145 | 192 | ||
| 146 | event = ntohl(inpacket[80 / 4]); | 193 | /* The peer keeps its port the way it came over the wire */ |
| 147 | port = *(uint16_t *)(((char *)inpacket) + 96); | 194 | memcpy(&port, request + UDP_ANNOUNCE_PORT_OFFSET, sizeof(port)); |
| 148 | ws->hash = (ot_hash *)(((char *)inpacket) + 16); | ||
| 149 | 195 | ||
| 150 | OT_SETIP(ws->peer, remoteip); | 196 | OT_SETIP(ws->peer, remoteip); |
| 151 | OT_SETPORT(ws->peer, &port); | 197 | OT_SETPORT(ws->peer, &port); |
| 152 | OT_PEERFLAG(ws->peer) = 0; | 198 | OT_PEERFLAG(ws->peer) = 0; |
| 153 | 199 | ||
| 154 | switch (event) { | 200 | if (event == UDP_EVENT_COMPLETED) |
| 155 | case 1: | ||
| 156 | OT_PEERFLAG(ws->peer) |= PEER_FLAG_COMPLETED; | 201 | OT_PEERFLAG(ws->peer) |= PEER_FLAG_COMPLETED; |
| 157 | break; | 202 | if (event == UDP_EVENT_STOPPED) |
| 158 | case 3: | ||
| 159 | OT_PEERFLAG(ws->peer) |= PEER_FLAG_STOPPED; | 203 | OT_PEERFLAG(ws->peer) |= PEER_FLAG_STOPPED; |
| 160 | break; | ||
| 161 | default: | ||
| 162 | break; | ||
| 163 | } | ||
| 164 | |||
| 165 | if (!left) | 204 | if (!left) |
| 166 | OT_PEERFLAG(ws->peer) |= PEER_FLAG_SEEDING; | 205 | OT_PEERFLAG(ws->peer) |= PEER_FLAG_SEEDING; |
| 167 | 206 | ||
| 168 | outpacket[0] = htonl(1); /* announce action */ | 207 | reply = udp_reply_header(ws->outbuf, UDP_ACTION_ANNOUNCE, request); |
| 169 | outpacket[1] = inpacket[12 / 4]; | ||
| 170 | 208 | ||
| 171 | if (OT_PEERFLAG(ws->peer) & PEER_FLAG_STOPPED) { /* Peer is gone. */ | 209 | if (OT_PEERFLAG(ws->peer) & PEER_FLAG_STOPPED) { /* Peer is gone. */ |
| 210 | /* This one writes the whole reply, and leaves the header alone */ | ||
| 172 | ws->reply = ws->outbuf; | 211 | ws->reply = ws->outbuf; |
| 173 | ws->reply_size = remove_peer_from_torrent(FLAG_UDP, ws); | 212 | ws->reply_size = remove_peer_from_torrent(FLAG_UDP, ws); |
| 174 | } else { | 213 | } else { |
| 175 | /* Limit amount of peers to OT_MAX_PEERS_UDP */ | 214 | /* List of peers should fit in a single UDP packet */ |
| 176 | uint32_t numwant = ntohl(inpacket[92 / 4]); | 215 | max_peers = ip_is_v4mapped(remoteip) ? OT_MAX_PEERS_UDP4 : OT_MAX_PEERS_UDP6; |
| 177 | size_t max_peers = ip_is_v4mapped(remoteip) ? OT_MAX_PEERS_UDP4 : OT_MAX_PEERS_UDP6; | ||
| 178 | if (numwant > max_peers) | 216 | if (numwant > max_peers) |
| 179 | numwant = max_peers; | 217 | numwant = max_peers; |
| 180 | 218 | ||
| 181 | ws->reply = ws->outbuf + 8; | 219 | ws->reply = reply; |
| 182 | ws->reply_size = add_peer_to_torrent_and_return_peers(FLAG_UDP, ws, numwant); | 220 | ws->reply_size = add_peer_to_torrent_and_return_peers(FLAG_UDP, ws, numwant); |
| 183 | 221 | ||
| 184 | /* The announce path answers a refusal itself, so nothing is left here | 222 | /* The announce path answers a refusal itself, so nothing is left here |
| 185 | but a tracker that could not get the memory to track the torrent */ | 223 | but a tracker that could not get the memory to track the torrent */ |
| 186 | if (!ws->reply_size) { | 224 | if (!ws->reply_size) { |
| 187 | const char err_resources[] = "Tracker is out of resources, try again later."; | 225 | udp_send_error(serversocket, ws, "Tracker is out of resources, try again later.", remoteip, remoteport); |
| 188 | size_t s = sizeof(err_resources); | ||
| 189 | outpacket[0] = htonl(3); | ||
| 190 | memcpy(&outpacket[2], &err_resources[0], s); | ||
| 191 | socket_send(serversocket, ws->outbuf, 8 + s, remoteip, remoteport); | ||
| 192 | return 1; | 226 | return 1; |
| 193 | } | 227 | } |
| 194 | ws->reply_size += 8; | 228 | ws->reply_size += UDP_REPLY_HEADER_SIZE; |
| 195 | } | 229 | } |
| 196 | 230 | ||
| 197 | socket_send(serversocket, ws->outbuf, ws->reply_size, remoteip, remoteport); | 231 | socket_send(serversocket, ws->outbuf, ws->reply_size, remoteip, remoteport); |
| 198 | stats_issue_event(ws, EVENT_ANNOUNCE, FLAG_UDP, ws->reply_size); | 232 | stats_issue_event(ws, EVENT_ANNOUNCE, FLAG_UDP, ws->reply_size); |
| 199 | break; | 233 | break; |
| 200 | 234 | ||
| 201 | case 2: /* This is a scrape action */ | 235 | case UDP_ACTION_SCRAPE: |
| 202 | scrape_count = (byte_count - 16) / sizeof(ot_hash); | 236 | scrape_count = (byte_count - UDP_SCRAPE_INFO_HASH_OFFSET) / sizeof(ot_hash); |
| 203 | if (scrape_count > OT_MAX_SCRAPE_UDP) | 237 | if (scrape_count > OT_MAX_SCRAPE_UDP) |
| 204 | scrape_count = OT_MAX_SCRAPE_UDP; | 238 | scrape_count = OT_MAX_SCRAPE_UDP; |
| 205 | 239 | ||
| 206 | outpacket[0] = htonl(2); /* scrape action */ | 240 | hashes = (ot_hash *)(ws->inbuf + UDP_SCRAPE_INFO_HASH_OFFSET); |
| 207 | outpacket[1] = inpacket[12 / 4]; | 241 | reply = udp_reply_header(ws->outbuf, UDP_ACTION_SCRAPE, request); |
| 208 | |||
| 209 | hashes = (ot_hash *)(ws->inbuf + 16); | ||
| 210 | reply = ws->outbuf + 8; | ||
| 211 | for (i = 0; i < scrape_count; ++i) | 242 | for (i = 0; i < scrape_count; ++i) |
| 212 | reply += return_udp_scrape_for_torrent(hashes[i], reply); | 243 | reply += return_udp_scrape_for_torrent(hashes[i], reply); |
| 213 | 244 | ||
