diff --git a/server/include/node.h b/server/include/node.h index bfa0d5774..4ce8cdb16 100644 --- a/server/include/node.h +++ b/server/include/node.h @@ -35,6 +35,10 @@ struct node { /** The socket descriptor */ int sd; + /** How many paths are sending / receiving from this node? */ + int refcnt; + /** Socket mark for netem, routing and filtering */ + int mark; /** A short identifier of the node, only used for configuration and logging */ const char *name; @@ -46,13 +50,9 @@ struct node /** The egress interface */ struct interface *interface; - /** Network emulator settings */ struct netem *netem; - /** Socket mark for netem, routing and filtering */ - int mark; - /** A pointer to the libconfig object which instantiated this node */ config_setting_t *cfg; diff --git a/server/src/cfg.c b/server/src/cfg.c index 872c6c0de..aa595c1ec 100644 --- a/server/src/cfg.c +++ b/server/src/cfg.c @@ -119,19 +119,25 @@ int config_parse_path(config_setting_t *cfg, p->cfg = cfg; if (enabled) { + p->in->refcnt++; + p->out->refcnt++; + list_add(*paths, p); if (reverse) { - struct path *prev = (struct path *) malloc(sizeof(struct path)); - if (!prev) + struct path *rev = (struct path *) malloc(sizeof(struct path)); + if (!rev) error("Failed to allocate memory for path"); else - memcpy(prev, path, sizeof(struct path)); + memcpy(rev, p, sizeof(struct path)); - prev->in = p->out; /* Swap in/out */ - prev->out = p->in; + rev->in = p->out; /* Swap in/out */ + rev->out = p->in; - list_add(*paths, prev); + rev->in->refcnt++; + rev->out->refcnt++; + + list_add(*paths, rev); } } else { diff --git a/server/src/server.c b/server/src/server.c index b9b603ab7..20fb0e53f 100644 --- a/server/src/server.c +++ b/server/src/server.c @@ -36,6 +36,8 @@ static void start() { /* Connect and bind nodes to their sockets, set socket options */ for (struct node *n = nodes; n; n = n->next) { + if (!n->refcnt) continue; + /* Determine outgoing interface */ int index = if_getegress(&n->remote); if (index < 0) @@ -97,8 +99,9 @@ static void start() /* Setup network emulation */ for (struct interface *i = interfaces; i; i = i->next) { - if (i->refcnt) - tc_prio(i, TC_HDL(4000, 0), i->refcnt); + if (!i->refcnt) continue; + + tc_prio(i, TC_HDL(4000, 0), i->refcnt); } for (struct node *n = nodes; n; n = n->next) {