diff options
author | Matthias Schiffer <mschiffer@universe-factory.net> | 2014-04-24 22:19:50 +0200 |
---|---|---|
committer | Matthias Schiffer <mschiffer@universe-factory.net> | 2014-04-24 22:25:41 +0200 |
commit | 4ea57586f53dad8e567d2072c5567b92e8902d8c (patch) | |
tree | 6f0812a3b61b3b7ebdaa8d2355bf30ca5b13c384 | |
parent | e4e4f007e87b250d327eb222f1e44af2ffe47774 (diff) | |
download | fastd-4ea57586f53dad8e567d2072c5567b92e8902d8c.tar fastd-4ea57586f53dad8e567d2072c5567b92e8902d8c.zip |
Add a sequential number to identify peers
-rw-r--r-- | src/fastd.h | 1 | ||||
-rw-r--r-- | src/peer.c | 25 | ||||
-rw-r--r-- | src/peer.h | 3 |
3 files changed, 29 insertions, 0 deletions
diff --git a/src/fastd.h b/src/fastd.h index 4119a06..328666c 100644 --- a/src/fastd.h +++ b/src/fastd.h @@ -236,6 +236,7 @@ struct fastd_context { fastd_peer_group_t *peer_group; + uint64_t next_peer_id; VECTOR(fastd_peer_t*) peers; #ifdef USE_EPOLL @@ -137,6 +137,29 @@ void fastd_peer_schedule_handshake(fastd_peer_t *peer, int delay) { fastd_dlist_insert(list, &peer->handshake_entry); } +static int peer_id_cmp(fastd_peer_t *const *a, fastd_peer_t *const *b) { + if ((*a)->id == (*b)->id) + return 0; + else if ((*a)->id < (*b)->id) + return -1; + else + return 1; +} + +fastd_peer_t* fastd_peer_find_by_id(uint64_t id) { + fastd_peer_t tmp = {.id = id}; + const fastd_peer_t *tmpp = &tmp; + + fastd_peer_t **ret = bsearch(&tmpp, VECTOR_DATA(ctx.peers), VECTOR_LEN(ctx.peers), sizeof(fastd_peer_t*), + (int (*)(const void *, const void *))peer_id_cmp); + + if (ret) + return *ret; + else + return NULL; + +} + static inline fastd_peer_group_t* find_peer_group(fastd_peer_group_t *group, const fastd_peer_group_config_t *config) { if (group->conf == config) return group; @@ -547,6 +570,8 @@ bool fastd_peer_may_connect(fastd_peer_t *peer) { fastd_peer_t* fastd_peer_add(fastd_peer_config_t *peer_conf) { fastd_peer_t *peer = calloc(1, sizeof(fastd_peer_t)); + peer->id = ctx.next_peer_id++; + if (peer_conf) { peer->config = peer_conf; peer->group = find_peer_group(ctx.peer_group, peer_conf->group); @@ -30,6 +30,8 @@ struct fastd_peer { + uint64_t id; + const fastd_peer_config_t *config; fastd_peer_group_t *group; @@ -140,6 +142,7 @@ bool fastd_peer_matches_address(const fastd_peer_t *peer, const fastd_peer_addre bool fastd_peer_claim_address(fastd_peer_t *peer, fastd_socket_t *sock, const fastd_peer_address_t *local_addr, const fastd_peer_address_t *remote_addr); void fastd_peer_reset_socket(fastd_peer_t *peer); void fastd_peer_schedule_handshake(fastd_peer_t *peer, int delay); +fastd_peer_t* fastd_peer_find_by_id(uint64_t id); static inline void fastd_peer_schedule_handshake_default(fastd_peer_t *peer) { fastd_peer_schedule_handshake(peer, fastd_rand(17500, 22500)); |