summaryrefslogtreecommitdiff
path: root/src/routing.h
blob: aee2c8ef949ddb34493d5fc4c938738eb6f49ad7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#pragma once

#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <time.h>
#include <stdio.h>

#define RT_IDBITS 160
#define RT_BSIZE 8
// The 3 here is log2(BUCKETSIZE), since the final bucket will contain all those combinations
#define RT_BBITS 3
#define RT_SIZE (RT_IDBITS * RT_BSIZE)

struct addr {
	uint32_t ip;
	uint16_t port;
};

struct nodeid {
	union {
		uint32_t inner[5];
		char inner_b[20];
	};
};

struct entry {
	bool set;
	struct nodeid id;
	struct addr addr;

	time_t expire;
};

extern struct nodeid myID;
extern struct entry *table;
extern int table_size;

void routing_init(struct nodeid* myid);
void routing_update_metrics();
void routing_flush();
bool routing_interested(struct nodeid* id);
bool routing_offer(struct nodeid* id, struct entry **dest);
void routing_oldest(struct entry** dest);
size_t routing_closest(struct nodeid* needle, size_t n, struct entry** res);
void routing_reset_expire(time_t expire);
void routing_status(int* filled, int* size, double* load_factor, size_t load_factor_len);

struct entry* routing_get(struct nodeid* id);
void routing_remove(struct nodeid* self);

struct nodeid rand_nodeid_in_bucket(struct nodeid *self, struct nodeid *other);
uint8_t prefix(struct nodeid* a, struct nodeid* b);