summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorAnton Luka Šijanec <anton@sijanec.eu>2022-12-08 11:40:56 +0100
committerAnton Luka Šijanec <anton@sijanec.eu>2022-12-08 11:40:56 +0100
commit9bbead0ac73edc9b991f9facc853e6dde9e49410 (patch)
treef5599ded78609904e2a9a197b9bb2d6425e7d476 /src
parentadding submodule correctly (diff)
downloadtravnik-9bbead0ac73edc9b991f9facc853e6dde9e49410.tar
travnik-9bbead0ac73edc9b991f9facc853e6dde9e49410.tar.gz
travnik-9bbead0ac73edc9b991f9facc853e6dde9e49410.tar.bz2
travnik-9bbead0ac73edc9b991f9facc853e6dde9e49410.tar.lz
travnik-9bbead0ac73edc9b991f9facc853e6dde9e49410.tar.xz
travnik-9bbead0ac73edc9b991f9facc853e6dde9e49410.tar.zst
travnik-9bbead0ac73edc9b991f9facc853e6dde9e49410.zip
Diffstat (limited to 'src')
-rw-r--r--src/dht.c80
-rw-r--r--src/main.c2
2 files changed, 62 insertions, 20 deletions
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 <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <dht.c>
int main (void) {
+ struct dht * dht = dht_init();
}