From e92174e887bac9b18a6b5d18e04adaefd3bd4bc1 Mon Sep 17 00:00:00 2001 From: Dimitri Staessens Date: Thu, 3 May 2018 19:36:23 +0200 Subject: ipcpd: Limit number of returned query addresses The query returned all addresses where a name was registered, causing some scaling problems. Now it will return at most DHT_RETR_ADDR, which is currently set at 1 (anycast). Signed-off-by: Dimitri Staessens Signed-off-by: Sander Vrijders --- src/ipcpd/normal/dht.c | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/ipcpd/normal/dht.c b/src/ipcpd/normal/dht.c index 5e63f748..faf3a35e 100644 --- a/src/ipcpd/normal/dht.c +++ b/src/ipcpd/normal/dht.c @@ -70,6 +70,7 @@ typedef KadContactMsg kad_contact_msg_t; #define KAD_JOIN_RETR 8 /* Number of retries sending a join. */ #define KAD_JOIN_INTV 1 /* Time (seconds) between join retries. */ #define HANDLE_TIMEO 1000 /* Timeout for dht_handle_sdu tpm check (ms) */ +#define DHT_RETR_ADDR 1 /* Number of addresses to return on retrieve */ enum dht_state { DHT_INIT = 0, @@ -1891,34 +1892,39 @@ static buffer_t dht_retrieve(struct dht * dht, struct list_head * p; buffer_t buf; uint64_t * pos; - - buf.len = 0; + size_t addrs = 0; pthread_rwlock_rdlock(&dht->lock); e = dht_find_entry(dht, key); - if (e == NULL) { - pthread_rwlock_unlock(&dht->lock); - return buf; - } + if (e == NULL) + goto fail; - buf.data = malloc(sizeof(dht->addr) * e->n_vals); - if (buf.data == NULL) { - pthread_rwlock_unlock(&dht->lock); - return buf; - } + buf.len = MIN(DHT_RETR_ADDR, e->n_vals); + if (buf.len == 0) + goto fail; - buf.len = e->n_vals; + buf.data = malloc(sizeof(dht->addr) * buf.len); + if (buf.data == NULL) + goto fail; pos = (uint64_t *) buf.data;; list_for_each(p, &e->vals) { struct val * v = list_entry(p, struct val, next); *pos++ = v->addr; + if (++addrs >= buf.len) + break; } pthread_rwlock_unlock(&dht->lock); + return buf; + + fail: + pthread_rwlock_unlock(&dht->lock); + buf.len = 0; + return buf; } -- cgit v1.2.3