From 4ea57586f53dad8e567d2072c5567b92e8902d8c Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Thu, 24 Apr 2014 22:19:50 +0200 Subject: Add a sequential number to identify peers --- src/peer.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'src/peer.c') diff --git a/src/peer.c b/src/peer.c index 80a91ab..22b25a0 100644 --- a/src/peer.c +++ b/src/peer.c @@ -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); -- cgit v1.2.3