summaryrefslogtreecommitdiffstats
path: root/src/peer.c
diff options
context:
space:
mode:
authorMatthias Schiffer <mschiffer@universe-factory.net>2012-11-05 22:24:42 +0100
committerMatthias Schiffer <mschiffer@universe-factory.net>2012-11-05 22:24:42 +0100
commit8f60a96b84b1f952b0f3073e5df52f024162ef4b (patch)
tree7f4374bf7e0f1d10bc238093b1de9c264692a3f6 /src/peer.c
parentf631c10660c7c0bcdbf793bb1588670fb9ea82e6 (diff)
downloadfastd-8f60a96b84b1f952b0f3073e5df52f024162ef4b.tar
fastd-8f60a96b84b1f952b0f3073e5df52f024162ef4b.zip
Implement peer limit constraints
Diffstat (limited to 'src/peer.c')
-rw-r--r--src/peer.c45
1 files changed, 45 insertions, 0 deletions
diff --git a/src/peer.c b/src/peer.c
index 117d51a..a735f5e 100644
--- a/src/peer.c
+++ b/src/peer.c
@@ -404,6 +404,49 @@ static inline fastd_peer_group* find_peer_group(fastd_peer_group *group, const f
return NULL;
}
+static inline bool is_group_in(fastd_peer_group *group1, fastd_peer_group *group2) {
+ while (group1) {
+ if (group1 == group2)
+ return true;
+
+ group1 = group1->parent;
+ }
+
+ return false;
+}
+
+static bool is_peer_in_group(fastd_peer *peer, fastd_peer_group *group) {
+ return is_group_in(peer->group, group);
+}
+
+static inline unsigned count_established_group_peers(fastd_context *ctx, fastd_peer_group *group) {
+ unsigned ret = 0;
+ fastd_peer *peer;
+ for (peer = ctx->peers; peer; peer = peer->next) {
+ if (fastd_peer_is_established(peer) && is_peer_in_group(peer, group))
+ ret++;
+ }
+
+ return ret;
+}
+
+bool fastd_peer_may_connect(fastd_context *ctx, fastd_peer *peer) {
+ if (fastd_peer_is_established(peer))
+ return true;
+
+ fastd_peer_group *group;
+
+ for (group = peer->group; group; group = group->parent) {
+ if (!group->conf->max_connections)
+ continue;
+
+ if (count_established_group_peers(ctx, group) >= group->conf->max_connections)
+ return false;
+ }
+
+ return true;
+}
+
fastd_peer* fastd_peer_add(fastd_context *ctx, fastd_peer_config *peer_conf) {
fastd_peer *peer = malloc(sizeof(fastd_peer));
@@ -427,6 +470,8 @@ void fastd_peer_set_established(fastd_context *ctx, fastd_peer *peer) {
on_establish(ctx, peer);
pr_info(ctx, "connection with %P established.", peer);
}
+
+ return;
}
const fastd_eth_addr* fastd_get_source_address(const fastd_context *ctx, fastd_buffer buffer) {