Skip to content

Commit dec3803

Browse files
committed
bep-42: implement enforcement
Do not accept a get_peer search response if the node ID prefix does not match its external IP address. Introduce DHT_BEP42_ENFORCE macro to enable enforcement (disabled by default).
1 parent ab91375 commit dec3803

1 file changed

Lines changed: 18 additions & 0 deletions

File tree

dht.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,10 @@ struct peer {
213213
#define DHT_SEARCH_RETRANSMIT 10
214214
#endif
215215

216+
#ifndef DHT_BEP42_ENFORCE
217+
#define DHT_BEP42_ENFORCE 0
218+
#endif
219+
216220
struct storage {
217221
unsigned char id[20];
218222
int numpeers, maxpeers;
@@ -456,6 +460,16 @@ compute_prefix(const unsigned char *id, const struct sockaddr *sa)
456460
return crc;
457461
}
458462

463+
static int
464+
is_prefix_valid(const unsigned char *id, const struct sockaddr *sa)
465+
{
466+
uint32_t prefix = compute_prefix(id, sa);
467+
468+
return id[0] == (prefix >> 24) &&
469+
id[1] == ((prefix >> 16) & 0xff) &&
470+
(id[2] & 0xf8) == ((prefix >> 8) & 0xf8);
471+
}
472+
459473
FILE *dht_debug = NULL;
460474

461475
#ifdef __GNUC__
@@ -2299,7 +2313,11 @@ dht_periodic(const void *buf, size_t buflen,
22992313
another request. */
23002314
search_send_get_peers(sr, NULL);
23012315
}
2316+
#if DHT_BEP42_ENFORCE > 0
2317+
if(sr && is_prefix_valid(m.id, from)) {
2318+
#else
23022319
if(sr) {
2320+
#endif
23032321
insert_search_node(m.id, from, fromlen, sr,
23042322
1, m.token, m.token_len);
23052323
if(m.values_len > 0 || m.values6_len > 0) {

0 commit comments

Comments
 (0)