From 4a6c79517e22edbf9f5a6531e8ac2912ee25cfe2 Mon Sep 17 00:00:00 2001 From: Laurent Ghigonis Date: Mon, 10 Dec 2012 00:03:00 +0100 Subject: support for client side usage, and fix connection id handling * fix missing connection id initialisation * add user data keeping for nodes and connections * add accessors for src and dst of a connection * add new connection find functions * add option to disable connection id handling --- libglouglou/libggnet.c | 99 +++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 86 insertions(+), 13 deletions(-) (limited to 'libglouglou/libggnet.c') diff --git a/libglouglou/libggnet.c b/libglouglou/libggnet.c index 24937c0..69eee3f 100644 --- a/libglouglou/libggnet.c +++ b/libglouglou/libggnet.c @@ -5,11 +5,16 @@ #include "libggnet.h" struct ggnet * -ggnet_new(void) +ggnet_new(int manage_connid) { struct ggnet *net; + int i; net = xcalloc(1, sizeof(struct ggnet)); + net->manage_connid = manage_connid; + for (i=0; iconn_freeids[i] = i; + return net; } @@ -81,9 +86,22 @@ ggnet_node_update_name(struct ggnet *net, struct ggnet_node *n, } } +void * +ggnet_node_usrdata_get(struct ggnet_node *n) +{ + return n->usrdata; +} + +void +ggnet_node_usrdata_set(struct ggnet_node *n, void *usrdata) +{ + n->usrdata = usrdata; +} + struct ggnet_conn * ggnet_conn_add(struct ggnet *net, struct in_addr *src, int src_port, - struct in_addr *dst, int dst_port, int proto, int size) + struct in_addr *dst, int dst_port, int proto, int size, + int given_id) { struct ggnet_conn *c; struct ggnet_node *srcnode; @@ -92,13 +110,17 @@ ggnet_conn_add(struct ggnet *net, struct in_addr *src, int src_port, gg_log_debug("ggnet_conn_add, %x:%d->%x:%d %d [%d]", src->s_addr, src_port, dst->s_addr, dst_port, proto, size); - if (net->conn_freeids_ptr == GGNET_CONN_FREEIDS_COUNT) { - gg_log_warn("ggnet_conn_add: out of connection identifiers !"); - return NULL; - } - id = net->conn_freeids[net->conn_freeids_ptr]; - net->conn_freeids_ptr++; + if (net->manage_connid) { + if (net->conn_freeids_ptr == GGNET_CONN_FREEIDS_COUNT) { + gg_log_warn("ggnet_conn_add: out of connection identifiers !"); + return NULL; + } + id = net->conn_freeids[net->conn_freeids_ptr]; + net->conn_freeids_ptr++; + } else { + id = given_id; + } srcnode = ggnet_node_find(net, src); if (!srcnode) @@ -118,7 +140,9 @@ ggnet_conn_add(struct ggnet *net, struct in_addr *src, int src_port, c->dst_port = dst_port; c->proto = proto; c->size = size; + c->size_response = 0; c->lastseen = net->time; + c->usrdata = NULL; LIST_INSERT_HEAD(&net->conn_list, c, entry); return c; @@ -154,10 +178,12 @@ ggnet_conn_del(struct ggnet *net, struct ggnet_conn *c) } } - if (net->conn_freeids_ptr == 0) - gg_log_fatal("net->conn_freeids_ptr == 0"); - net->conn_freeids_ptr--; - net->conn_freeids[net->conn_freeids_ptr] = c->id; + if (net->manage_connid) { + if (net->conn_freeids_ptr == 0) + gg_log_fatal("net->conn_freeids_ptr == 0"); + net->conn_freeids_ptr--; + net->conn_freeids[net->conn_freeids_ptr] = c->id; + } c->src->used--; c->dst->used--; @@ -166,7 +192,6 @@ ggnet_conn_del(struct ggnet *net, struct ggnet_conn *c) free(c); } - struct ggnet_conn * ggnet_conn_find(struct ggnet *net, struct in_addr *src, int src_port, struct in_addr *dst, int dst_port, int proto, int *response) @@ -193,6 +218,54 @@ ggnet_conn_find(struct ggnet *net, struct in_addr *src, int src_port, return NULL; } +struct ggnet_conn * +ggnet_conn_find_by_id(struct ggnet *net, int id) +{ + struct ggnet_conn *c; + + LIST_FOREACH(c, &net->conn_list, entry) + if (c->id == id) + return c; + return NULL; +} + +struct ggnet_conn * +ggnet_conn_find_by_node(struct ggnet *net, + struct ggnet_node *a, struct ggnet_node *b) +{ + struct ggnet_conn *c; + + LIST_FOREACH(c, &net->conn_list, entry) + if ((c->src == a && c->dst == b) || + (c->src == b && c->dst == a)) + return c; + return NULL; +} + +void * +ggnet_conn_usrdata_get(struct ggnet_conn *c) +{ + return c->usrdata; +} + +void +ggnet_conn_usrdata_set(struct ggnet_conn *c, void *usrdata) +{ + c->usrdata = usrdata; +} + +void * +ggnet_conn_src_get(struct ggnet_conn *c) +{ + return c->src; +} + +void * +ggnet_conn_dst_get(struct ggnet_conn *c) +{ + return c->dst; +} + void ggnet_time_update(struct ggnet *net, time_t time) { -- cgit v1.2.3-59-g8ed1b