diff options
author | Laurent Ghigonis <laurent@p1sec.com> | 2012-12-15 04:18:51 +0100 |
---|---|---|
committer | Laurent Ghigonis <laurent@p1sec.com> | 2012-12-15 04:18:51 +0100 |
commit | b6f9d1cb1fc279d0f5ab6e2fbc00b7bd0d2861ae (patch) | |
tree | 21b34d206a25b913ea3c63e9e50b211e62851b2e | |
parent | add (struct ggnet *) in cb_nodename() arguments (diff) | |
download | glouglou-b6f9d1cb1fc279d0f5ab6e2fbc00b7bd0d2861ae.tar.xz glouglou-b6f9d1cb1fc279d0f5ab6e2fbc00b7bd0d2861ae.zip |
add necessary code in libggnet to do resolving of nodes names.
modify libggnet_dns structure to fit with these changes.
-rw-r--r-- | libglouglou/libggnet.c | 36 | ||||
-rw-r--r-- | libglouglou/libggnet.h | 9 | ||||
-rw-r--r-- | libglouglou/libggnet_dns.c | 36 | ||||
-rw-r--r-- | libglouglou/libggnet_dns.h | 26 |
4 files changed, 59 insertions, 48 deletions
diff --git a/libglouglou/libggnet.c b/libglouglou/libggnet.c index eb3d31b..2dfb5d3 100644 --- a/libglouglou/libggnet.c +++ b/libglouglou/libggnet.c @@ -14,6 +14,7 @@ static struct ggnet_nodegroup *nodegroup_find(struct ggnet *, void *param); static void nodegroup_set(struct ggnet *, struct ggnet_node *); static void nodegroup_unset(struct ggnet *, struct ggnet_node *); +static void _cb_dns_reverse(struct in_addr *, char *, void *); struct ggnet * ggnet_new(int manage_connid) @@ -98,13 +99,15 @@ ggnet_node_add(struct ggnet *net, struct in_addr *addr) printf("could not allocate node\n"); exit(1); } + n->net = net; n->addr.s_addr = addr->s_addr; - n->namelen = GGNET_NODENAME_WAITING; n->lastseen = net->time; LIST_INSERT_HEAD(&net->node_list, n, entry); net->node_count++; if (net->use_grouping) nodegroup_set(net, n); + if (net->use_dns) + n->dns_req = ggnet_dns_reverse(net->ggdns, &n->addr, _cb_dns_reverse, n); return n; } @@ -121,6 +124,8 @@ ggnet_node_del(struct ggnet *net, struct ggnet_node *n) if (net->use_grouping) nodegroup_unset(net, n); + if (n->dns_req) + ggnet_dns_cancel(net->ggdns, n->dns_req); LIST_REMOVE(n, entry); free(n); @@ -138,21 +143,6 @@ ggnet_node_find(struct ggnet *net, struct in_addr *remote) return NULL; } -void -ggnet_node_update_name(struct ggnet *net, struct ggnet_node *n, - char *name, int len) -{ - if (!name) { - snprintf(n->name, sizeof(n->name), "%x", n->addr.s_addr); - n->namelen = sizeof(n->addr.s_addr); - } else { - if (len > sizeof(n->name)) - len = sizeof(n->name); - strncpy(n->name, name, len); - n->namelen = len; - } -} - void * ggnet_node_usrdata_get(struct ggnet_node *n) { @@ -489,3 +479,17 @@ nodegroup_unset(struct ggnet *net, struct ggnet_node *n) nodegroup_del(net, n->group); } +static void +_cb_dns_reverse(struct in_addr *ip, char *name, void *data) +{ + struct ggnet *net; + struct ggnet_node *n; + + n = data; + net = n->net; + n->dns_req = NULL; + if (name) + snprintf(n->fqdn, sizeof(n->fqdn), "%s", name); + net->cb_nodename(net, n); +} + diff --git a/libglouglou/libggnet.h b/libglouglou/libggnet.h index 1caa64b..825f8f8 100644 --- a/libglouglou/libggnet.h +++ b/libglouglou/libggnet.h @@ -43,14 +43,13 @@ struct ggnet_nodegroup { struct ggnet_node { LIST_ENTRY(ggnet_node) entry; + struct ggnet *net; struct in_addr addr; time_t lastseen; int used; - short namelen; -#define GGNET_NODENAME_WAITING -1 -#define GGNET_NODENAME_FAILED -2 - char name[GGNET_DNSNAME_MAX]; + char fqdn[GGNET_DNSNAME_MAX]; struct ggnet_nodegroup *group; /* XXX for now we support only one group */ + struct ggnet_dns_req *dns_req; void *usrdata; }; @@ -108,8 +107,6 @@ 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_nodegroup *ggnet_node_group_get(struct ggnet_node *); diff --git a/libglouglou/libggnet_dns.c b/libglouglou/libggnet_dns.c index 252aba5..be4e19b 100644 --- a/libglouglou/libggnet_dns.c +++ b/libglouglou/libggnet_dns.c @@ -13,15 +13,6 @@ static void _cb_evdns_reverse(int, char, int, int, void *, void *); -struct req_reverse { - LIST_ENTRY(req_reverse) entry; - struct ggnet_dns *ggdns; - struct evdns_request *ereq; - struct in_addr ip; - void (*cb_usr)(struct in_addr *, char *, void *); - void *data; -}; - struct ggnet_dns * ggnet_dns_new(struct event_base *ev_base) { @@ -51,14 +42,14 @@ ggnet_dns_free(struct ggnet_dns *ggdns) free(ggdns); } -int +struct ggnet_dns_req * ggnet_dns_reverse(struct ggnet_dns *ggdns, struct in_addr *ip, void (*cb_usr)(struct in_addr *, char *, void *), void *data) { struct evutil_addrinfo hints; - struct req_reverse *req; + struct ggnet_dns_req *req; struct evdns_request *ereq; memset(&hints, 0, sizeof(hints)); @@ -67,9 +58,9 @@ ggnet_dns_reverse(struct ggnet_dns *ggdns, hints.ai_socktype = SOCK_STREAM; hints.ai_protocol = IPPROTO_TCP; - req = malloc(sizeof(struct req_reverse)); + req = malloc(sizeof(struct ggnet_dns_req)); if (!req) { - printf("could not allocate req_reverse\n"); + printf("could not allocate ggnet_dns_req\n"); exit(1); } req->ggdns = ggdns; @@ -77,26 +68,33 @@ ggnet_dns_reverse(struct ggnet_dns *ggdns, req->cb_usr = cb_usr; req->data = data; - LIST_INSERT_HEAD(&ggdns->req_reverse_list, req, entry); - ggdns->req_reverse_pending++; + LIST_INSERT_HEAD(&ggdns->req_list, req, entry); + ggdns->req_pending++; ereq = evdns_base_resolve_reverse(ggdns->evdns_base, ip, 0, _cb_evdns_reverse, req); if (ereq == NULL) { printf("libggnet_dns WARNING: dns request for %d returned immediately\n", ip->s_addr); /* remove req from list and free it happened in the callback. */ - return 0; + return NULL; } req->ereq = ereq; - return 1; + return req; +} + +void +ggnet_dns_cancel(struct ggnet_dns *ggdns, struct ggnet_dns_req *req) +{ + evdns_cancel_request(ggdns->evdns_base, req->ereq); + /* remove req from list and free it happened in the callback. */ } static void _cb_evdns_reverse(int result, char type, int count, int ttl, void *addresses, void *arg) { - struct req_reverse *req; + struct ggnet_dns_req *req; req = arg; if (type != DNS_PTR) { @@ -108,6 +106,6 @@ _cb_evdns_reverse(int result, char type, int count, free: LIST_REMOVE(req, entry); - req->ggdns->req_reverse_pending--; + req->ggdns->req_pending--; free(req); } diff --git a/libglouglou/libggnet_dns.h b/libglouglou/libggnet_dns.h index f74f696..ae7b634 100644 --- a/libglouglou/libggnet_dns.h +++ b/libglouglou/libggnet_dns.h @@ -8,15 +8,27 @@ #include <bsd/sys/queue.h> #endif +struct ggnet_dns_req { + LIST_ENTRY(ggnet_dns_req) entry; + struct ggnet_dns *ggdns; + struct evdns_request *ereq; + struct in_addr ip; + char *name; + void (*cb_usr)(struct in_addr *, char *, void *); + void *data; +}; + struct ggnet_dns { struct event_base *ev_base; struct evdns_base *evdns_base; - int req_reverse_pending; - LIST_HEAD(, req_reverse) req_reverse_list; /* XXX for now unused. remove ? */ + int req_pending; + LIST_HEAD(, ggnet_dns_req) req_list; /* XXX for now unused. remove ? */ }; -struct ggnet_dns *ggnet_dns_new(struct event_base *); -void ggnet_dns_free(struct ggnet_dns *); -int ggnet_dns_reverse(struct ggnet_dns *, struct in_addr *, - void (*cb_usr)(struct in_addr *, char *, void *), - void *); +struct ggnet_dns *ggnet_dns_new(struct event_base *); +void ggnet_dns_free(struct ggnet_dns *); +struct ggnet_dns_req *ggnet_dns_reverse(struct ggnet_dns *, struct in_addr *, + void (*cb_usr)(struct in_addr *, char *, void *), + void *); +void ggnet_dns_cancel(struct ggnet_dns *, + struct ggnet_dns_req *); |