diff options
-rw-r--r-- | libglouglou/libggnet.c | 99 | ||||
-rw-r--r-- | libglouglou/libggnet.h | 19 |
2 files changed, 103 insertions, 15 deletions
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; i<GGNET_CONN_FREEIDS_COUNT-1; i++) + net->conn_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) { diff --git a/libglouglou/libggnet.h b/libglouglou/libggnet.h index 10116f2..a27e4e1 100644 --- a/libglouglou/libggnet.h +++ b/libglouglou/libggnet.h @@ -13,6 +13,9 @@ #define GGNET_DNSNAME_MAX 30 #define GGNET_CONN_FREEIDS_COUNT 65536 /* 2^16 as freeids are u_int16_t */ +#define GGNET_MANAGE_CONNID_TRUE 1 +#define GGNET_MANAGE_CONNID_FALSE 0 + struct ggnet_node { LIST_ENTRY(ggnet_node) entry; struct in_addr addr; @@ -22,6 +25,7 @@ struct ggnet_node { #define GGNET_NODENAME_WAITING -1 #define GGNET_NODENAME_FAILED -2 char name[GGNET_DNSNAME_MAX]; + void *usrdata; }; enum ggnet_connstate { @@ -42,6 +46,7 @@ struct ggnet_conn { u_int size; u_int size_response; time_t lastseen; + void *usrdata; }; struct ggnet { @@ -51,23 +56,33 @@ struct ggnet { int node_count; u_int16_t conn_freeids[GGNET_CONN_FREEIDS_COUNT]; int conn_freeids_ptr; + int manage_connid; time_t time; }; -struct ggnet *ggnet_new(void); +struct ggnet *ggnet_new(int); void ggnet_free(struct ggnet *); struct ggnet_node *ggnet_node_add(struct ggnet *, struct in_addr *); void ggnet_node_del(struct ggnet *, struct ggnet_node *); struct ggnet_node *ggnet_node_find(struct ggnet *, struct in_addr *); void ggnet_node_update_name(struct ggnet *, struct ggnet_node *, char *, int); +void *ggnet_node_usrdata_get(struct ggnet_node *); +void ggnet_node_usrdata_set(struct ggnet_node *, void *); struct ggnet_conn *ggnet_conn_add(struct ggnet *, struct in_addr *, int, - struct in_addr *, int, int, int); + struct in_addr *, int, int, int, int); void ggnet_conn_data(struct ggnet *, struct ggnet_conn *, int, int); void ggnet_conn_del(struct ggnet *, struct ggnet_conn *); struct ggnet_conn *ggnet_conn_find(struct ggnet *, struct in_addr *, int, struct in_addr *, int, int, int *); +struct ggnet_conn *ggnet_conn_find_by_id(struct ggnet *, int); +struct ggnet_conn *ggnet_conn_find_by_node(struct ggnet *, struct ggnet_node *, + struct ggnet_node *); +void *ggnet_conn_usrdata_get(struct ggnet_conn *); +void ggnet_conn_usrdata_set(struct ggnet_conn *, void *); +void *ggnet_conn_src_get(struct ggnet_conn *); +void *ggnet_conn_dst_get(struct ggnet_conn *); void ggnet_time_update(struct ggnet *, time_t); #endif /* _LIBGGNET_H_ */ |