From 9bbead0ac73edc9b991f9facc853e6dde9e49410 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anton=20Luka=20=C5=A0ijanec?= Date: Thu, 8 Dec 2022 11:40:56 +0100 Subject: untested for compilation, dht.c -- TODO: DNS+bootstrap --- makefile | 2 +- src/dht.c | 80 ++++++++++++++++++++++++++++++++++++++++++++++---------------- src/main.c | 2 ++ 3 files changed, 63 insertions(+), 21 deletions(-) diff --git a/makefile b/makefile index 5de5868..9aac5dc 100644 --- a/makefile +++ b/makefile @@ -3,7 +3,7 @@ CC=cc default: mkdir -p tmp - $(CC) -Wall -Wextra -Wformat -pedantic -g -Isrc -Itmp $(CFLAGS) src/main.c -otravnik -lm $(LDFLAGS) + $(CC) -Wall -Wextra -Wformat -pedantic -g -Isrc -Itmp -Itiny-AES-c $(CFLAGS) src/main.c -otravnik -lm $(LDFLAGS) install: mkdir -p $(DESTDIR)/usr/bin/ diff --git a/src/dht.c b/src/dht.c index 05403e4..18d23b4 100644 --- a/src/dht.c +++ b/src/dht.c @@ -722,8 +722,8 @@ int bucket_good (const struct dht * d, const struct bucket * b) { if (in_bucket(d->id, b)) { struct node * n = b->nodes; while (n->next) { - if (distance(n->id, n->next->id) != 1) - return 0; + if (distance(n->id, n->next->id) <= 2) // we allow holes of 1, since we don't actually store our ID in the bucket + return 0; // it's really impossible that a non-malicious node would by chance get so close } return 1; } @@ -1392,26 +1392,33 @@ void handle (struct dht * d, char * pkt, int len, struct sockaddr_in6 addr) { * do periodic housekeeping on the routing table LL, making sure no nodes are bad. removes bad nodes and does not ping questionable nodes. see NOTE03 * * @param b [in] first bucket in LL - basically either d->buckets or d->buckets6 + * @return number of good nodes */ -void refresh (struct bucket * b) { - struct node ** n = &b->nodes; - while (*n) { - switch (node_grade(*n)) { - case bad: - ; - struct node * old = *n; - *n = (*n)->next; - node_free(old); - break; - case questionable: - // ping_node(d, *n); // NOTE03 about not pinging questionable nodes: this ensures a constant regeneration of the routing table. this is just an idea, if the client frequently gets in a situation without any nodes in the routing table, remove the comment before ping_node call. - break; - case good: - break; +int refresh (struct bucket * b) { + int good = 0; + while (b) { + struct node ** n = &b->nodes; + while (*n) { + switch (node_grade(*n)) { + case bad: + ; + struct node * old = *n; + *n = (*n)->next; + node_free(old); + break; + case questionable: + // ping_node(d, *n); // NOTE03 about not pinging questionable nodes: this ensures a constant regeneration of the routing table. this is just an idea, if the client frequently gets in a situation without any nodes in the routing table, remove the comment before ping_node call. + break; + case good: + good++; + break; + } + n = &(*n)->next; } - n = &(*n)->next; + b = b->next; } + return good; } /** @@ -1474,16 +1481,49 @@ void get_peers (struct dht * d, const struct sockaddr_in6 * addr, const unsigned */ void periodic (struct dht * d) { - refresh(d->buckets); - refresh(d->buckets6); + int dns = 0; + if (!refresh(d->buckets)) + dns++; + if (!refresh(d->buckets6)) + dns++; + if (dns) { + + // perform a query to SRV travnik.sijanec.eu, continue with parsing in handle() + } struct torrent * t = d->torrents; while (t) { if (t->type & (peers | announce)) { struct node * n = t->nodes; + int sent = 0; while (n) { + sent++; get_peers(d, &n->addr, t->hash); n = n->next; } + if (sent < K) { +#define RTGP(buckets) struct bucket * b = d->buckets; \ + find(t->hash, &b, NULL); \ + struct node * n = b->nodes; \ + while (sent < K && n) { \ + sent++; \ + get_peers(d, &n->addr, t->hash); \ + n = n->next; + } + RTGP(buckets); + RTGP(buckets6); + } + if (sent < K) { + struct bucket * b = d->buckets; + while (sent < K && b) { + n = b->nodes; + while (sent < K && n) { + sent++; + get_peers(d, &n->addr, t->hash); + n = n->next; + } + b = b->next; + } + } } t = t->next; } diff --git a/src/main.c b/src/main.c index e8debe6..70d12fb 100644 --- a/src/main.c +++ b/src/main.c @@ -1,5 +1,7 @@ #include #include #include +#include int main (void) { + struct dht * dht = dht_init(); } -- cgit v1.2.3