aboutsummaryrefslogtreecommitdiffstats
path: root/libglouglou
diff options
context:
space:
mode:
Diffstat (limited to 'libglouglou')
-rw-r--r--libglouglou/Makefile2
-rw-r--r--libglouglou/examples/dnsreverse.c48
-rw-r--r--libglouglou/libggnet.c452
-rw-r--r--libglouglou/libggnet.h132
-rw-r--r--libglouglou/libggnet_dns.c150
-rw-r--r--libglouglou/libggnet_dns.h36
-rw-r--r--libglouglou/libglouglou.c945
-rw-r--r--libglouglou/libglouglou.h175
-rw-r--r--libglouglou/sendbuf.c140
-rw-r--r--libglouglou/sendbuf.h36
-rw-r--r--libglouglou/tests/connect.c52
-rw-r--r--libglouglou/tests/sendrecv.c81
-rw-r--r--libglouglou/utils.c114
13 files changed, 1187 insertions, 1176 deletions
diff --git a/libglouglou/Makefile b/libglouglou/Makefile
index 0343e6f..aae8f0b 100644
--- a/libglouglou/Makefile
+++ b/libglouglou/Makefile
@@ -13,7 +13,7 @@ OBJECTS = $(SOURCES:.c=.o)
all: $(TARGET)
$(TARGET): $(OBJECTS)
- $(CC) $(CFLAGS) -o $(TARGET) $(OBJECTS)
+ $(CC) $(CFLAGS) -o $(TARGET) $(OBJECTS)
install:
@echo "installation of $(LIBNAME)"
diff --git a/libglouglou/examples/dnsreverse.c b/libglouglou/examples/dnsreverse.c
index 5111391..e6504b0 100644
--- a/libglouglou/examples/dnsreverse.c
+++ b/libglouglou/examples/dnsreverse.c
@@ -15,39 +15,39 @@ int _answer = 0;
int
main(int argc, char *argv[])
{
- struct ggnet_dns *ggdns;
- struct in_addr ip;
- struct timeval tv;
+ struct ggnet_dns *ggdns;
+ struct in_addr ip;
+ struct timeval tv;
- if (argc < 2) {
- printf("usage: dnsreverse <fqdn>\n");
- exit(1);
- }
- inet_aton(argv[1], &ip);
- ip.s_addr = ntohl(ip.s_addr);
+ if (argc < 2) {
+ printf("usage: dnsreverse <fqdn>\n");
+ exit(1);
+ }
+ inet_aton(argv[1], &ip);
+ ip.s_addr = ntohl(ip.s_addr);
- bzero(&tv, sizeof(struct timeval));
- tv.tv_sec = 5;
+ bzero(&tv, sizeof(struct timeval));
+ tv.tv_sec = 5;
- _ev_base = event_base_new();
+ _ev_base = event_base_new();
- ggdns = ggnet_dns_new(_ev_base);
- ggnet_dns_reverse(ggdns, &ip, _cb_dns, NULL);
+ ggdns = ggnet_dns_new(_ev_base);
+ ggnet_dns_reverse(ggdns, &ip, _cb_dns, NULL);
- event_base_loopexit(_ev_base, &tv);
- event_base_dispatch(_ev_base);
+ event_base_loopexit(_ev_base, &tv);
+ event_base_dispatch(_ev_base);
- if (_answer)
- printf("%x: %s\n", ip.s_addr, _name);
- else
- printf("no answer\n");
- return 0;
+ if (_answer)
+ printf("%x: %s\n", ip.s_addr, _name);
+ else
+ printf("no answer\n");
+ return 0;
}
static void
_cb_dns(struct in_addr *ip, char *name, void *data)
{
- _name = name;
- _answer = 1;
- event_base_loopexit(_ev_base, NULL);
+ _name = name;
+ _answer = 1;
+ event_base_loopexit(_ev_base, NULL);
}
diff --git a/libglouglou/libggnet.c b/libglouglou/libggnet.c
index 10c2cf5..ab44d9f 100644
--- a/libglouglou/libggnet.c
+++ b/libglouglou/libggnet.c
@@ -6,12 +6,12 @@
#include "libggnet_dns.h"
static struct ggnet_nodegroup *nodegroup_add(struct ggnet *,
- enum ggnet_grouptype, void *,
- struct ggnet_nodegroup *);
+ enum ggnet_grouptype, void *,
+ struct ggnet_nodegroup *);
static void nodegroup_del(struct ggnet *, struct ggnet_nodegroup *);
static struct ggnet_nodegroup *nodegroup_find(struct ggnet *,
- enum ggnet_grouptype type,
- void *param);
+ enum ggnet_grouptype type,
+ 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 *);
@@ -19,20 +19,20 @@ static void _cb_dns_reverse(struct in_addr *, char *, void *);
struct ggnet *
ggnet_new(int manage_connid)
{
- struct ggnet *net;
- int i;
-
- net = calloc(1, sizeof(struct ggnet));
- if (!net) {
- printf("could not allocate ggnet\n");
- exit(1);
- }
- net->manage_connid = manage_connid;
- if (manage_connid)
- for (i=0; i<GGNET_CONN_FREEIDS_COUNT-1; i++)
- net->conn_freeids[i] = i;
-
- return net;
+ struct ggnet *net;
+ int i;
+
+ net = calloc(1, sizeof(struct ggnet));
+ if (!net) {
+ printf("could not allocate ggnet\n");
+ exit(1);
+ }
+ net->manage_connid = manage_connid;
+ if (manage_connid)
+ for (i=0; i<GGNET_CONN_FREEIDS_COUNT-1; i++)
+ net->conn_freeids[i] = i;
+
+ return net;
}
/**
@@ -42,48 +42,48 @@ ggnet_new(int manage_connid)
*/
void
ggnet_set_grouping(struct ggnet *net, int set,
- void (*cb_addgroup)(struct ggnet *, struct ggnet_nodegroup *, struct ggnet_nodegroup *),
- void (*cb_delgroup)(struct ggnet *, struct ggnet_nodegroup *))
+ void (*cb_addgroup)(struct ggnet *, struct ggnet_nodegroup *, struct ggnet_nodegroup *),
+ void (*cb_delgroup)(struct ggnet *, struct ggnet_nodegroup *))
{
- net->use_grouping = set;
- net->cb_addgroup = cb_addgroup;
- net->cb_delgroup = cb_delgroup;
+ net->use_grouping = set;
+ net->cb_addgroup = cb_addgroup;
+ net->cb_delgroup = cb_delgroup;
}
void
ggnet_set_dns(struct ggnet *net, int set,
- struct event_base *ev_base,
- void (*cb_nodename)(struct ggnet *, struct ggnet_node *))
+ struct event_base *ev_base,
+ void (*cb_nodename)(struct ggnet *, struct ggnet_node *))
{
- net->use_dns = set;
- if (set) {
- net->ggdns = ggnet_dns_new(ev_base);
- net->cb_nodename = cb_nodename;
- }
- else if (net->use_dns)
- ggnet_dns_free(net->ggdns);
+ net->use_dns = set;
+ if (set) {
+ net->ggdns = ggnet_dns_new(ev_base);
+ net->cb_nodename = cb_nodename;
+ }
+ else if (net->use_dns)
+ ggnet_dns_free(net->ggdns);
}
void
ggnet_debug_set(struct ggnet *net, int set)
{
- net->debug = set;
+ net->debug = set;
}
void
ggnet_free(struct ggnet *net)
{
- struct ggnet_conn *c, *ctmp;
- struct ggnet_node *n, *ntmp;
- struct ggnet_nodegroup *g, *gtmp;
-
- LIST_FOREACH_SAFE(c, &net->conn_list, entry, ctmp)
- ggnet_conn_del(net, c);
- LIST_FOREACH_SAFE(n, &net->node_list, entry, ntmp)
- ggnet_node_del(net, n);
- LIST_FOREACH_SAFE(g, &net->group_list, entry, gtmp)
- nodegroup_del(net, g);
- free(net);
+ struct ggnet_conn *c, *ctmp;
+ struct ggnet_node *n, *ntmp;
+ struct ggnet_nodegroup *g, *gtmp;
+
+ LIST_FOREACH_SAFE(c, &net->conn_list, entry, ctmp)
+ ggnet_conn_del(net, c);
+ LIST_FOREACH_SAFE(n, &net->node_list, entry, ntmp)
+ ggnet_node_del(net, n);
+ LIST_FOREACH_SAFE(g, &net->group_list, entry, gtmp)
+ nodegroup_del(net, g);
+ free(net);
}
struct ggnet_node *
@@ -92,22 +92,22 @@ ggnet_node_add(struct ggnet *net, struct in_addr *addr)
struct ggnet_node *n;
if (net->debug)
- printf("ggnet_node_add\n");
+ printf("ggnet_node_add\n");
n = calloc(1, sizeof(struct ggnet_node));
- if (!n) {
- printf("could not allocate node\n");
- exit(1);
- }
- n->net = net;
+ if (!n) {
+ printf("could not allocate node\n");
+ exit(1);
+ }
+ n->net = net;
n->addr.s_addr = addr->s_addr;
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);
+ nodegroup_set(net, n);
+ if (net->use_dns)
+ n->dns_req = ggnet_dns_reverse(net->ggdns, &n->addr, _cb_dns_reverse, n);
return n;
}
@@ -118,14 +118,14 @@ ggnet_node_del(struct ggnet *net, struct ggnet_node *n)
if (n->used) {
printf("FATAL: ggnet_node_del: trying to remove a used node !\n");
exit(1);
- }
+ }
if (net->debug)
- printf("ggnet_node_del: ggnet_node_del\n");
+ printf("ggnet_node_del: ggnet_node_del\n");
if (net->use_grouping)
- nodegroup_unset(net, n);
- if (n->dns_req)
- ggnet_dns_cancel(net->ggdns, n->dns_req);
+ nodegroup_unset(net, n);
+ if (n->dns_req)
+ ggnet_dns_cancel(net->ggdns, n->dns_req);
LIST_REMOVE(n, entry);
free(n);
@@ -146,25 +146,25 @@ ggnet_node_find(struct ggnet *net, struct in_addr *remote)
void *
ggnet_node_usrdata_get(struct ggnet_node *n)
{
- return n->usrdata;
+ return n->usrdata;
}
void
ggnet_node_usrdata_set(struct ggnet_node *n, void *usrdata)
{
- n->usrdata = usrdata;
+ n->usrdata = usrdata;
}
struct ggnet_nodegroup *
ggnet_node_group_get(struct ggnet_node *n)
{
- return n->group;
+ return n->group;
}
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,
- int given_id)
+ struct in_addr *dst, int dst_port, int proto, int size,
+ int given_id)
{
struct ggnet_conn *c;
struct ggnet_node *srcnode;
@@ -172,38 +172,38 @@ ggnet_conn_add(struct ggnet *net, struct in_addr *src, int src_port,
int id;
if (net->debug)
- printf("ggnet_conn_add, %x:%d->%x:%d %d [%d]\n",
- src->s_addr, src_port, dst->s_addr, dst_port, proto, size);
-
- if (net->manage_connid) {
- if (net->conn_freeids_ptr == GGNET_CONN_FREEIDS_COUNT) {
- printf("ggnet_conn_add: ERROR: out of connection identifiers !");
- return NULL;
- }
- id = net->conn_freeids[net->conn_freeids_ptr];
- net->conn_freeids_ptr++;
- } else {
- id = given_id;
- }
+ printf("ggnet_conn_add, %x:%d->%x:%d %d [%d]\n",
+ src->s_addr, src_port, dst->s_addr, dst_port, proto, size);
+
+ if (net->manage_connid) {
+ if (net->conn_freeids_ptr == GGNET_CONN_FREEIDS_COUNT) {
+ printf("ggnet_conn_add: ERROR: 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)
srcnode = ggnet_node_add(net, src);
srcnode->used++;
if (srcnode->group)
- srcnode->group->conn_count++;
+ srcnode->group->conn_count++;
dstnode = ggnet_node_find(net, dst);
if (!dstnode)
dstnode = ggnet_node_add(net, dst);
dstnode->used++;
if (dstnode->group)
- dstnode->group->conn_count++;
+ dstnode->group->conn_count++;
c = malloc(sizeof(struct ggnet_conn));
- if (!c) {
- printf("could not allocate conn\n");
- exit(1);
- }
+ if (!c) {
+ printf("could not allocate conn\n");
+ exit(1);
+ }
c->id = id;
c->state = CONNSTATE_ESTABLISHED;
c->src = srcnode;
@@ -224,12 +224,12 @@ void
ggnet_conn_data(struct ggnet *net, struct ggnet_conn *c, int size, int response)
{
if (net->debug)
- printf("ggnet_conn_data\n");
+ printf("ggnet_conn_data\n");
- if (!response)
- c->size = c->size + size;
- else
- c->size_response = c->size_response + size;
+ if (!response)
+ c->size = c->size + size;
+ else
+ c->size_response = c->size_response + size;
c->lastseen = net->time;
c->src->lastseen = net->time;
c->dst->lastseen = net->time;
@@ -239,38 +239,38 @@ void
ggnet_conn_del(struct ggnet *net, struct ggnet_conn *c)
{
if (net->debug)
- printf("ggnet_conn_del\n");
-
- /* UNSUPPORTED
- if (c->proto == IPPROTO_TCP) {
- switch (c->state) {
- case CONNSTATE_ESTABLISHED:
- c->state = CONNSTATE_TCPFIN;
- return;
- case CONNSTATE_TCPFIN:
- c->state = CONNSTATE_TCPFIN2;
- return;
- case CONNSTATE_TCPFIN2:
- break;
+ printf("ggnet_conn_del\n");
+
+ /* UNSUPPORTED
+ if (c->proto == IPPROTO_TCP) {
+ switch (c->state) {
+ case CONNSTATE_ESTABLISHED:
+ c->state = CONNSTATE_TCPFIN;
+ return;
+ case CONNSTATE_TCPFIN:
+ c->state = CONNSTATE_TCPFIN2;
+ return;
+ case CONNSTATE_TCPFIN2:
+ break;
+ }
+ }
+ */
+
+ if (net->manage_connid) {
+ if (net->conn_freeids_ptr == 0) {
+ printf("FATAL: net->conn_freeids_ptr == 0\n");
+ exit(1);
}
+ net->conn_freeids_ptr--;
+ net->conn_freeids[net->conn_freeids_ptr] = c->id;
}
- */
-
- if (net->manage_connid) {
- if (net->conn_freeids_ptr == 0) {
- printf("FATAL: net->conn_freeids_ptr == 0\n");
- exit(1);
- }
- net->conn_freeids_ptr--;
- net->conn_freeids[net->conn_freeids_ptr] = c->id;
- }
c->src->used--;
if (c->src->group)
- c->src->group->conn_count--;
+ c->src->group->conn_count--;
c->dst->used--;
if (c->dst->group)
- c->dst->group->conn_count--;
+ c->dst->group->conn_count--;
LIST_REMOVE(c, entry);
free(c);
@@ -278,20 +278,20 @@ ggnet_conn_del(struct ggnet *net, struct ggnet_conn *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)
+ struct in_addr *dst, int dst_port, int proto, int *response)
{
- struct ggnet_conn *c;
+ struct ggnet_conn *c;
LIST_FOREACH(c, &net->conn_list, entry) {
if (((c->src->addr.s_addr == src->s_addr &&
- c->src_port == src_port &&
- c->dst->addr.s_addr == dst->s_addr &&
- c->dst_port == dst_port) ||
- (c->src->addr.s_addr == dst->s_addr &&
- c->src_port == dst_port &&
- c->dst->addr.s_addr == src->s_addr &&
- c->dst_port == src_port)) &&
- c->proto == proto) {
+ c->src_port == src_port &&
+ c->dst->addr.s_addr == dst->s_addr &&
+ c->dst_port == dst_port) ||
+ (c->src->addr.s_addr == dst->s_addr &&
+ c->src_port == dst_port &&
+ c->dst->addr.s_addr == src->s_addr &&
+ c->dst_port == src_port)) &&
+ c->proto == proto) {
if (c->src->addr.s_addr == src->s_addr)
*response = 0;
else
@@ -305,122 +305,122 @@ ggnet_conn_find(struct ggnet *net, struct in_addr *src, int src_port,
struct ggnet_conn *
ggnet_conn_find_by_id(struct ggnet *net, int id)
{
- struct ggnet_conn *c;
+ struct ggnet_conn *c;
LIST_FOREACH(c, &net->conn_list, entry)
- if (c->id == id)
- return c;
- return NULL;
+ 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_node *a, struct ggnet_node *b)
{
- struct ggnet_conn *c;
+ 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;
+ 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;
+ return c->usrdata;
}
void
ggnet_conn_usrdata_set(struct ggnet_conn *c, void *usrdata)
{
- c->usrdata = usrdata;
+ c->usrdata = usrdata;
}
void *
ggnet_conn_src_get(struct ggnet_conn *c)
{
- return c->src;
+ return c->src;
}
void *
ggnet_conn_dst_get(struct ggnet_conn *c)
{
- return c->dst;
+ return c->dst;
}
void *
ggnet_nodegroup_usrdata_get(struct ggnet_nodegroup *g)
{
- return g->usrdata;
+ return g->usrdata;
}
void
ggnet_nodegroup_usrdata_set(struct ggnet_nodegroup *g, void *usrdata)
{
- g->usrdata = usrdata;
+ g->usrdata = usrdata;
}
void
ggnet_time_update(struct ggnet *net, time_t time)
{
- net->time = time;
+ net->time = time;
}
static struct ggnet_nodegroup *
nodegroup_add(struct ggnet *net, enum ggnet_grouptype type, void *param,
- struct ggnet_nodegroup *parent)
+ struct ggnet_nodegroup *parent)
{
- struct ggnet_nodegroup *group;
-
- group = calloc(1, sizeof(struct ggnet_nodegroup));
- if (!group) {
- printf("could not allocate nodegroup\n");
- exit(1);
- }
- group->type = type;
- if (parent) {
- group->parent = parent;
- parent->child_groups_count++;
- }
-
- switch (type) {
- case GROUP_ADDRESS:
- group->addr.s_addr = *(u_int *)param;
- break;
- case GROUP_WHOIS:
- case GROUP_DNS:
- case GROUP_ROUTE:
- /* UNSUPPORTED */
- free(group);
- return NULL;
- }
-
- LIST_INSERT_HEAD(&net->group_list, group, entry);
- net->cb_addgroup(net, group, parent);
-
- return group;
+ struct ggnet_nodegroup *group;
+
+ group = calloc(1, sizeof(struct ggnet_nodegroup));
+ if (!group) {
+ printf("could not allocate nodegroup\n");
+ exit(1);
+ }
+ group->type = type;
+ if (parent) {
+ group->parent = parent;
+ parent->child_groups_count++;
+ }
+
+ switch (type) {
+ case GROUP_ADDRESS:
+ group->addr.s_addr = *(u_int *)param;
+ break;
+ case GROUP_WHOIS:
+ case GROUP_DNS:
+ case GROUP_ROUTE:
+ /* UNSUPPORTED */
+ free(group);
+ return NULL;
+ }
+
+ LIST_INSERT_HEAD(&net->group_list, group, entry);
+ net->cb_addgroup(net, group, parent);
+
+ return group;
}
static void
nodegroup_del(struct ggnet *net, struct ggnet_nodegroup *group)
{
- if (group->node_count || group->conn_count) {
+ if (group->node_count || group->conn_count) {
printf("FATAL: ggnet_nodegroup_del: trying to remove a used group !\n");
exit(1);
- }
+ }
if (net->debug)
- printf("ggnet_nodegroup_del\n");
-
- net->cb_delgroup(net, group);
- if (group->parent) {
- group->parent->child_groups_count--;
- if (group->parent->child_groups_count == 0)
- nodegroup_del(net, group->parent);
- }
- LIST_REMOVE(group, entry);
- free(group);
+ printf("ggnet_nodegroup_del\n");
+
+ net->cb_delgroup(net, group);
+ if (group->parent) {
+ group->parent->child_groups_count--;
+ if (group->parent->child_groups_count == 0)
+ nodegroup_del(net, group->parent);
+ }
+ LIST_REMOVE(group, entry);
+ free(group);
}
static struct ggnet_nodegroup *
@@ -428,19 +428,19 @@ nodegroup_find(struct ggnet *net, enum ggnet_grouptype type, void *param)
{
struct ggnet_nodegroup *g;
- LIST_FOREACH(g, &net->group_list, entry) {
- switch (g->type) {
- case GROUP_ADDRESS:
- if (g->addr.s_addr == *(u_int *)param)
- return g;
- case GROUP_WHOIS:
- case GROUP_DNS:
- case GROUP_ROUTE:
- /* UNSUPPORTED */
- break;
- }
- }
- return NULL;
+ LIST_FOREACH(g, &net->group_list, entry) {
+ switch (g->type) {
+ case GROUP_ADDRESS:
+ if (g->addr.s_addr == *(u_int *)param)
+ return g;
+ case GROUP_WHOIS:
+ case GROUP_DNS:
+ case GROUP_ROUTE:
+ /* UNSUPPORTED */
+ break;
+ }
+ }
+ return NULL;
}
static void
@@ -449,48 +449,48 @@ nodegroup_set(struct ggnet *net, struct ggnet_node *n)
struct ggnet_nodegroup *group, *groot, *gclassb;
u_int addr, addr2;
- addr = n->addr.s_addr & 0xffff0000;
-
- group = nodegroup_find(net, GROUP_ADDRESS, &addr);
- if (!group) {
- addr2 = 0x00000000;
- groot = nodegroup_find(net, GROUP_ADDRESS, &addr2);
- if (!groot)
- groot = nodegroup_add(net, GROUP_ADDRESS, &addr2, NULL);
- addr2 = addr & 0xff000000;
- gclassb = nodegroup_find(net, GROUP_ADDRESS, &addr2);
- if (!gclassb)
- gclassb = nodegroup_add(net, GROUP_ADDRESS, &addr2, groot);
- group = nodegroup_add(net, GROUP_ADDRESS, &addr, gclassb);
- }
-
- n->group = group;
- group->node_count++;
+ addr = n->addr.s_addr & 0xffff0000;
+
+ group = nodegroup_find(net, GROUP_ADDRESS, &addr);
+ if (!group) {
+ addr2 = 0x00000000;
+ groot = nodegroup_find(net, GROUP_ADDRESS, &addr2);
+ if (!groot)
+ groot = nodegroup_add(net, GROUP_ADDRESS, &addr2, NULL);
+ addr2 = addr & 0xff000000;
+ gclassb = nodegroup_find(net, GROUP_ADDRESS, &addr2);
+ if (!gclassb)
+ gclassb = nodegroup_add(net, GROUP_ADDRESS, &addr2, groot);
+ group = nodegroup_add(net, GROUP_ADDRESS, &addr, gclassb);
+ }
+
+ n->group = group;
+ group->node_count++;
}
static void
nodegroup_unset(struct ggnet *net, struct ggnet_node *n)
{
- if (!n->group)
- return;
+ if (!n->group)
+ return;
- n->group->node_count--;
- if (n->group->node_count == 0)
- nodegroup_del(net, n->group);
+ n->group->node_count--;
+ if (n->group->node_count == 0)
+ 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);
- }
+ 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 1b17a32..3caea7c 100644
--- a/libglouglou/libggnet.h
+++ b/libglouglou/libggnet.h
@@ -23,10 +23,10 @@
#define GGNET_GROUPING_FALSE 1
enum ggnet_grouptype {
- GROUP_ADDRESS,
- GROUP_WHOIS,
- GROUP_DNS,
- GROUP_ROUTE
+ GROUP_ADDRESS,
+ GROUP_WHOIS,
+ GROUP_DNS,
+ GROUP_ROUTE
};
struct ggnet_nodegroup {
@@ -34,11 +34,11 @@ struct ggnet_nodegroup {
struct ggnet_nodegroup *parent;
enum ggnet_grouptype type;
struct in_addr addr;
- char name[GGNET_DNSNAME_MAX];
- int node_count;
- int conn_count;
- int child_groups_count;
- void *usrdata;
+ char name[GGNET_DNSNAME_MAX];
+ int node_count;
+ int conn_count;
+ int child_groups_count;
+ void *usrdata;
};
struct ggnet_node {
@@ -61,72 +61,76 @@ enum ggnet_connstate {
struct ggnet_conn {
LIST_ENTRY(ggnet_conn) entry;
- u_int id;
+ u_int id;
enum ggnet_connstate state;
- struct ggnet_node *src;
- u_int src_port;
- struct ggnet_node *dst;
- u_int dst_port;
- u_int proto;
- u_int size;
- u_int size_response;
- time_t lastseen;
- void *usrdata;
+ struct ggnet_node *src;
+ u_int src_port;
+ struct ggnet_node *dst;
+ u_int dst_port;
+ u_int proto;
+ u_int size;
+ u_int size_response;
+ time_t lastseen;
+ void *usrdata;
};
struct ggnet {
LIST_HEAD(, ggnet_conn) conn_list;
LIST_HEAD(, ggnet_node) node_list;
LIST_HEAD(, ggnet_nodegroup) group_list;
- int conn_count;
- int node_count;
- u_int16_t conn_freeids[GGNET_CONN_FREEIDS_COUNT];
- int conn_freeids_ptr;
- int manage_connid;
- time_t time;
- int debug;
+ int conn_count;
+ int node_count;
+ u_int16_t conn_freeids[GGNET_CONN_FREEIDS_COUNT];
+ int conn_freeids_ptr;
+ int manage_connid;
+ time_t time;
+ int debug;
/* grouping */
- int use_grouping;
- void (*cb_addgroup)(struct ggnet *, struct ggnet_nodegroup *, struct ggnet_nodegroup *);
- void (*cb_delgroup)(struct ggnet *, struct ggnet_nodegroup *);
- /* dns */
- int use_dns;
+ int use_grouping;
+ void (*cb_addgroup)(struct ggnet *, struct ggnet_nodegroup *, struct ggnet_nodegroup *);
+ void (*cb_delgroup)(struct ggnet *, struct ggnet_nodegroup *);
+ /* dns */
+ int use_dns;
struct ggnet_dns *ggdns;
- void (*cb_nodename)(struct ggnet *, struct ggnet_node *);
+ void (*cb_nodename)(struct ggnet *, struct ggnet_node *);
};
-struct ggnet *ggnet_new(int);
-void ggnet_set_grouping(struct ggnet *, int,
- void (*cb_addgroup)(struct ggnet *, struct ggnet_nodegroup *, struct ggnet_nodegroup *),
- void (*cb_delgroup)(struct ggnet *, struct ggnet_nodegroup *));
-void ggnet_set_dns(struct ggnet *net, int set,
- struct event_base *ev_base,
- void (*cb_nodename)(struct ggnet *, struct ggnet_node *));
-void ggnet_debug_set(struct ggnet *, 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_usrdata_get(struct ggnet_node *);
-void ggnet_node_usrdata_set(struct ggnet_node *, void *);
+struct ggnet *ggnet_new(int);
+void ggnet_set_grouping(struct ggnet *, int,
+ void (*cb_addgroup)(struct ggnet *,
+ struct ggnet_nodegroup *,
+ struct ggnet_nodegroup *),
+ void (*cb_delgroup)(struct ggnet *,
+ struct ggnet_nodegroup *));
+void ggnet_set_dns(struct ggnet *net, int set,
+ struct event_base *ev_base,
+ void (*cb_nodename)(struct ggnet *,
+ struct ggnet_node *));
+void ggnet_debug_set(struct ggnet *, 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_usrdata_get(struct ggnet_node *);
+void ggnet_node_usrdata_set(struct ggnet_node *, void *);
struct ggnet_nodegroup *ggnet_node_group_get(struct ggnet_node *);
-struct ggnet_conn *ggnet_conn_add(struct ggnet *, struct in_addr *, 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_nodegroup_usrdata_get(struct ggnet_nodegroup *);
-void ggnet_nodegroup_usrdata_set(struct ggnet_nodegroup *,
- void *);
-void ggnet_time_update(struct ggnet *, time_t);
+struct ggnet_conn *ggnet_conn_add(struct ggnet *, struct in_addr *, 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_nodegroup_usrdata_get(struct ggnet_nodegroup *);
+void ggnet_nodegroup_usrdata_set(struct ggnet_nodegroup *,
+ void *);
+void ggnet_time_update(struct ggnet *, time_t);
#endif /* _LIBGGNET_H_ */
diff --git a/libglouglou/libggnet_dns.c b/libglouglou/libggnet_dns.c
index 25bada3..c59c8a7 100644
--- a/libglouglou/libggnet_dns.c
+++ b/libglouglou/libggnet_dns.c
@@ -16,101 +16,101 @@ static void _cb_evdns_reverse(int, char, int, int, void *, void *);
struct ggnet_dns *
ggnet_dns_new(struct event_base *ev_base)
{
- struct ggnet_dns *ggdns = NULL;
-
- ggdns = calloc(1, sizeof(struct ggnet_dns));
- if (!ggdns) {
- printf("could not allocate ggnet_dns\n");
- exit(1);
- }
- ggdns->ev_base = ev_base;
- ggdns->evdns_base = evdns_base_new(ev_base, 1);
- if (!ggdns->evdns_base)
- goto err;
- return ggdns;
+ struct ggnet_dns *ggdns = NULL;
+
+ ggdns = calloc(1, sizeof(struct ggnet_dns));
+ if (!ggdns) {
+ printf("could not allocate ggnet_dns\n");
+ exit(1);
+ }
+ ggdns->ev_base = ev_base;
+ ggdns->evdns_base = evdns_base_new(ev_base, 1);
+ if (!ggdns->evdns_base)
+ goto err;
+ return ggdns;
err:
- if (ggdns)
- free(ggdns);
- return NULL;
+ if (ggdns)
+ free(ggdns);
+ return NULL;
}
void
ggnet_dns_free(struct ggnet_dns *ggdns)
{
- evdns_base_free(ggdns->evdns_base, 1);
- free(ggdns);
+ evdns_base_free(ggdns->evdns_base, 1);
+ free(ggdns);
}
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 in_addr *ip,
+ void (*cb_usr)(struct in_addr *, char *, void *),
+ void *data)
{
- struct evutil_addrinfo hints;
- struct ggnet_dns_req *req;
- struct evdns_request *ereq;
- struct in_addr nip;
-
- memset(&hints, 0, sizeof(hints));
- hints.ai_family = AF_UNSPEC;
- hints.ai_flags = EVUTIL_AI_CANONNAME;
- hints.ai_socktype = SOCK_STREAM;
- hints.ai_protocol = IPPROTO_TCP;
-
- req = malloc(sizeof(struct ggnet_dns_req));
- if (!req) {
- printf("could not allocate ggnet_dns_req\n");
- exit(1);
- }
- req->ggdns = ggdns;
- memcpy(&req->ip, ip, sizeof(struct in_addr));
- req->cb_usr = cb_usr;
- req->data = data;
-
- nip.s_addr = htonl(ip->s_addr); /* evdns eats network byte order */
-
- LIST_INSERT_HEAD(&ggdns->req_list, req, entry);
- ggdns->req_pending++;
- ereq = evdns_base_resolve_reverse(ggdns->evdns_base, &nip, 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 NULL;
- }
- req->ereq = ereq;
-
- return req;
+ struct evutil_addrinfo hints;
+ struct ggnet_dns_req *req;
+ struct evdns_request *ereq;
+ struct in_addr nip;
+
+ memset(&hints, 0, sizeof(hints));
+ hints.ai_family = AF_UNSPEC;
+ hints.ai_flags = EVUTIL_AI_CANONNAME;
+ hints.ai_socktype = SOCK_STREAM;
+ hints.ai_protocol = IPPROTO_TCP;
+
+ req = malloc(sizeof(struct ggnet_dns_req));
+ if (!req) {
+ printf("could not allocate ggnet_dns_req\n");
+ exit(1);
+ }
+ req->ggdns = ggdns;
+ memcpy(&req->ip, ip, sizeof(struct in_addr));
+ req->cb_usr = cb_usr;
+ req->data = data;
+
+ nip.s_addr = htonl(ip->s_addr); /* evdns eats network byte order */
+
+ LIST_INSERT_HEAD(&ggdns->req_list, req, entry);
+ ggdns->req_pending++;
+ ereq = evdns_base_resolve_reverse(ggdns->evdns_base, &nip, 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 NULL;
+ }
+ req->ereq = ereq;
+
+ 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. */
+ 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)
+ int ttl, void *addresses, void *arg)
{
- struct ggnet_dns_req *req;
- char **name;
-
- if (count > 1)
- printf("libggnet_dns XXX: has %d PTR records !\n", count); // XXX
-
- req = arg;
- name = addresses;
- if (result != DNS_ERR_NONE || count == 0)
- req->cb_usr(&req->ip, NULL, req->data);
- else
- req->cb_usr(&req->ip, *name, req->data);
-
- LIST_REMOVE(req, entry);
- req->ggdns->req_pending--;
- free(req);
+ struct ggnet_dns_req *req;
+ char **name;
+
+ if (count > 1)
+ printf("libggnet_dns XXX: has %d PTR records !\n", count); // XXX
+
+ req = arg;
+ name = addresses;
+ if (result != DNS_ERR_NONE || count == 0)
+ req->cb_usr(&req->ip, NULL, req->data);
+ else
+ req->cb_usr(&req->ip, *name, req->data);
+
+ LIST_REMOVE(req, entry);
+ req->ggdns->req_pending--;
+ free(req);
}
diff --git a/libglouglou/libggnet_dns.h b/libglouglou/libggnet_dns.h
index ae7b634..9fc716d 100644
--- a/libglouglou/libggnet_dns.h
+++ b/libglouglou/libggnet_dns.h
@@ -9,26 +9,26 @@
#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;
+ 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_pending;
- LIST_HEAD(, ggnet_dns_req) req_list; /* XXX for now unused. remove ? */
+ struct event_base *ev_base;
+ struct evdns_base *evdns_base;
+ 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 *);
-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 *);
+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 *);
diff --git a/libglouglou/libglouglou.c b/libglouglou/libglouglou.c
index 79f57c6..972bb07 100644
--- a/libglouglou/libglouglou.c
+++ b/libglouglou/libglouglou.c
@@ -16,46 +16,46 @@
#include "sendbuf.h"
#define error(fmt, ...) \
- if (_verbosity >= 0) \
- printf("libgg: %s: ERROR: " fmt "\n", __func__, ##__VA_ARGS__)
+ if (_verbosity >= 0) \
+printf("libgg: %s: ERROR: " fmt "\n", __func__, ##__VA_ARGS__)
#define verbose(fmt, ...) \
- if (_verbosity >= 1) \
- printf("libgg: %s: " fmt "\n", __func__, ##__VA_ARGS__)
+ if (_verbosity >= 1) \
+printf("libgg: %s: " fmt "\n", __func__, ##__VA_ARGS__)
#define debug(fmt, ...) \
- if (_verbosity >= 2) \
- printf("libgg: %s: XXX: " fmt "\n", __func__, ##__VA_ARGS__)
-
-void cb_srv_receive(evutil_socket_t, short, void *);
-struct gg_user *user_add(struct gg_server *, struct sockaddr_in *);
-void user_del(struct gg_server *, struct gg_user *);
-struct gg_user * user_find(struct gg_server *, struct sockaddr_in *);
-int user_send(struct gg_user *, void *, int);
-int cb_usr_send(void *, int, void *);
-int client_send(struct gg_client *, void *, int);
-void cb_cli_receive(evutil_socket_t, short, void *);
-void cb_cli_timer(evutil_socket_t, short, void *);
-int cb_cli_send(void *, int, void *);
+ if (_verbosity >= 2) \
+printf("libgg: %s: XXX: " fmt "\n", __func__, ##__VA_ARGS__)
+
+void cb_srv_receive(evutil_socket_t, short, void *);
+struct gg_user *user_add(struct gg_server *, struct sockaddr_in *);
+void user_del(struct gg_server *, struct gg_user *);
+struct gg_user *user_find(struct gg_server *, struct sockaddr_in *);
+int user_send(struct gg_user *, void *, int);
+int cb_usr_send(void *, int, void *);
+int client_send(struct gg_client *, void *, int);
+void cb_cli_receive(evutil_socket_t, short, void *);
+void cb_cli_timer(evutil_socket_t, short, void *);
+int cb_cli_send(void *, int, void *);
struct gg_packet *pkt_decode(char **, int *);
-int pkt_getsize(struct gg_packet *);
-int pkt_encode(struct gg_packet *, struct gg_packet *);
+int pkt_getsize(struct gg_packet *);
+int pkt_encode(struct gg_packet *, struct gg_packet *);
int _verbosity = 0;
gg_packet_props_t gg_packet_props[] = {
- [PACKET_NEWCONN] = \
- { (PACKET_HEADER_SIZE + sizeof((struct gg_packet *)0)->pdat.newconn) },
- [PACKET_DELCONN] = \
- { (PACKET_HEADER_SIZE + sizeof((struct gg_packet *)0)->pdat.delconn) },
- [PACKET_DATA] = \
- { (PACKET_HEADER_SIZE + sizeof((struct gg_packet *)0)->pdat.data) },
- [PACKET_NAME] = \
- { ((PACKET_HEADER_SIZE + sizeof((struct gg_packet *)0)->pdat.name) - GG_PKTARG_MAX) },
- [PACKET_FORK] = \
- { (PACKET_HEADER_SIZE + sizeof((struct gg_packet *)0)->pdat.fork) },
- [PACKET_EXEC] = \
- { ((PACKET_HEADER_SIZE + sizeof((struct gg_packet *)0)->pdat.exec) - GG_PKTARG_MAX) },
- [PACKET_EXIT] = \
- { (PACKET_HEADER_SIZE + sizeof((struct gg_packet *)0)->pdat.exit) },
+ [PACKET_NEWCONN] = \
+ { (PACKET_HEADER_SIZE + sizeof((struct gg_packet *)0)->pdat.newconn) },
+ [PACKET_DELCONN] = \
+ { (PACKET_HEADER_SIZE + sizeof((struct gg_packet *)0)->pdat.delconn) },
+ [PACKET_DATA] = \
+ { (PACKET_HEADER_SIZE + sizeof((struct gg_packet *)0)->pdat.data) },
+ [PACKET_NAME] = \
+ { ((PACKET_HEADER_SIZE + sizeof((struct gg_packet *)0)->pdat.name) - GG_PKTARG_MAX) },
+ [PACKET_FORK] = \
+ { (PACKET_HEADER_SIZE + sizeof((struct gg_packet *)0)->pdat.fork) },
+ [PACKET_EXEC] = \
+ { ((PACKET_HEADER_SIZE + sizeof((struct gg_packet *)0)->pdat.exec) - GG_PKTARG_MAX) },
+ [PACKET_EXIT] = \
+ { (PACKET_HEADER_SIZE + sizeof((struct gg_packet *)0)->pdat.exit) },
};
/*
@@ -70,30 +70,30 @@ gg_packet_props_t gg_packet_props[] = {
*/
struct gg_server *
gg_server_start(struct event_base *ev_base, char *ip, int port,
- int (*handle_conn)(struct gg_server *, struct gg_user *),
- int (*handle_packet)(struct gg_server *, struct gg_user *, struct gg_packet *),
- void *usrdata)
+ int (*handle_conn)(struct gg_server *, struct gg_user *),
+ int (*handle_packet)(struct gg_server *, struct gg_user *, struct gg_packet *),
+ void *usrdata)
{
- struct gg_server *srv;
- struct sockaddr_in sock_addr;
- struct event *ev;
- int s;
- int sock_on = 1;
-
- srv = xcalloc(1, sizeof(struct gg_server));
- srv->ev_base = ev_base;
- srv->ip = ip;
- srv->port = port;
- srv->handle_conn = handle_conn;
- srv->handle_packet = handle_packet;
- srv->usrdata = usrdata;
-
- s = socket(AF_INET, SOCK_DGRAM, 0);
- if (s < 0)
- goto err;
- srv->sock = s;
+ struct gg_server *srv;
+ struct sockaddr_in sock_addr;
+ struct event *ev;
+ int s;
+ int sock_on = 1;
+
+ srv = xcalloc(1, sizeof(struct gg_server));
+ srv->ev_base = ev_base;
+ srv->ip = ip;
+ srv->port = port;
+ srv->handle_conn = handle_conn;
+ srv->handle_packet = handle_packet;
+ srv->usrdata = usrdata;
+
+ s = socket(AF_INET, SOCK_DGRAM, 0);
+ if (s < 0)
+ goto err;
+ srv->sock = s;
setsockopt(s, SOL_SOCKET, SO_REUSEADDR,
- &sock_on, sizeof(sock_on));
+ &sock_on, sizeof(sock_on));
fd_nonblock(s);
bzero(&sock_addr, sizeof(sock_addr));
@@ -103,97 +103,98 @@ gg_server_start(struct event_base *ev_base, char *ip, int port,
addrcpy(&srv->addr, &sock_addr);
if (bind(s, (struct sockaddr *)&sock_addr,
- sizeof(sock_addr)) != 0)
+ sizeof(sock_addr)) != 0)
goto err;
- ev = event_new(ev_base, s, EV_READ|EV_PERSIST, cb_srv_receive, srv);
- event_add(ev, NULL);
- srv->ev = ev;
+ ev = event_new(ev_base, s, EV_READ|EV_PERSIST, cb_srv_receive, srv);
+ event_add(ev, NULL);
+ srv->ev = ev;
- return srv;
+ return srv;
err:
- error("%s", strerror(errno));
- gg_server_stop(srv);
- return NULL;
+ error("%s", strerror(errno));
+ gg_server_stop(srv);
+ return NULL;
}
void
gg_server_stop(struct gg_server *srv)
{
- struct gg_user *usr;
-
- while ((usr = LIST_FIRST(&srv->user_list))) {
- user_del(srv, usr);
- }
- if (srv->sock)
- close(srv->sock);
- if (srv->ev)
- event_del(srv->ev);
- free(srv);
+ struct gg_user *usr;
+
+ while ((usr = LIST_FIRST(&srv->user_list))) {
+ user_del(srv, usr);
+ }
+ if (srv->sock)
+ close(srv->sock);
+ if (srv->ev)
+ event_del(srv->ev);
+ free(srv);
}
int
gg_server_send(struct gg_server *srv, struct gg_packet *pkt, struct gg_user *usr)
{
- static struct gg_packet *newpkt;
- static struct gg_packet pktbuf;
- struct gg_user *u;
- int size;
- int res = 0;
-
- /* optimisation to use sendbuf_gettoken */
- if (!usr && srv->user_count == 1)
- usr = LIST_FIRST(&srv->user_list);
-
- if (usr) {
- size = pkt_getsize(pkt);
- newpkt = sendbuf_gettoken(usr->sbuf, size);
- if (!newpkt)
- return -1;
- return pkt_encode(pkt, newpkt);
- } else {
- size = pkt_encode(pkt, &pktbuf);
- LIST_FOREACH(u, &srv->user_list, entry) {
- res = res + sendbuf_append(u->sbuf, &pktbuf, size);
- }
- }
-
- return res;
+ static struct gg_packet *newpkt;
+ static struct gg_packet pktbuf;
+ struct gg_user *u;
+ int size;
+ int res = 0;
+
+ /* optimisation to use sendbuf_gettoken */
+ if (!usr && srv->user_count == 1)
+ usr = LIST_FIRST(&srv->user_list);
+
+ if (usr) {
+ size = pkt_getsize(pkt);
+ newpkt = sendbuf_gettoken(usr->sbuf, size);
+ if (!newpkt)
+ return -1;
+ return pkt_encode(pkt, newpkt);
+ } else {
+ size = pkt_encode(pkt, &pktbuf);
+ LIST_FOREACH(u, &srv->user_list, entry) {
+ res = res + sendbuf_append(u->sbuf, &pktbuf, size);
+ }
+ }
+
+ return res;
}
void
gg_server_send_flush(struct gg_server *srv, struct gg_user *usr)
{
- struct gg_user *u;
+ struct gg_user *u;
- if (usr)
- sendbuf_flush(usr->sbuf);
- else
- LIST_FOREACH(u, &srv->user_list, entry)
- sendbuf_flush(u->sbuf);
+ if (usr)
+ sendbuf_flush(usr->sbuf);
+ else
+ LIST_FOREACH(u, &srv->user_list, entry)
+ sendbuf_flush(u->sbuf);
}
/*
* Server - private
*/
-void cb_srv_receive(evutil_socket_t fd, short what, void *arg)
+void
+cb_srv_receive(evutil_socket_t fd, short what, void *arg)
{
- struct gg_server *srv;
- struct gg_user *usr;
- struct gg_packet *pkt;
+ struct gg_server *srv;
+ struct gg_user *usr;
+ struct gg_packet *pkt;
struct sockaddr_in remote;
- socklen_t remote_len;
- char buf[PACKET_BUFFER_SIZE];
- char *buf_p;
+ socklen_t remote_len;
+ char buf[PACKET_BUFFER_SIZE];
+ char *buf_p;
int buf_len;
int len;
- srv = arg;
- remote_len = sizeof(struct sockaddr_in);
+ srv = arg;
+ remote_len = sizeof(struct sockaddr_in);
len = recvfrom(fd, buf, sizeof(buf), 0,
- (struct sockaddr *)&remote, &remote_len);
+ (struct sockaddr *)&remote, &remote_len);
if (len < 0) {
error("recvfrom failed");
return;
@@ -201,67 +202,67 @@ void cb_srv_receive(evutil_socket_t fd, short what, void *arg)
usr = user_find(srv, &remote);
if (!usr) {
- usr = user_add(srv, &remote);
- if (srv->handle_conn)
- srv->handle_conn(srv, usr);
- user_send(usr, "", 0);
- } else {
- debug("Incoming data from existing user !");
- if (len == 0) {
- user_del(srv, usr);
- return;
- }
- if (srv->handle_packet) {
- buf_p = buf;
- buf_len = len;
- while (buf_len > 0 && (pkt = pkt_decode(&buf_p, &buf_len)))
- srv->handle_packet(srv, usr, pkt);
- if (buf_len > 0) {
- /* XXX store incomplete packet for next recv */
- error("incomplete packet, dropped %d bytes !", buf_len);
- }
- }
- }
+ usr = user_add(srv, &remote);
+ if (srv->handle_conn)
+ srv->handle_conn(srv, usr);
+ user_send(usr, "", 0);
+ } else {
+ debug("Incoming data from existing user !");
+ if (len == 0) {
+ user_del(srv, usr);
+ return;
+ }
+ if (srv->handle_packet) {
+ buf_p = buf;
+ buf_len = len;
+ while (buf_len > 0 && (pkt = pkt_decode(&buf_p, &buf_len)))
+ srv->handle_packet(srv, usr, pkt);
+ if (buf_len > 0) {
+ /* XXX store incomplete packet for next recv */
+ error("incomplete packet, dropped %d bytes !", buf_len);
+ }
+ }
+ }
}
struct gg_user *
user_add(struct gg_server *srv, struct sockaddr_in *remote)
{
- struct gg_user *usr;
- struct sendbuf *sbuf;
+ struct gg_user *usr;
+ struct sendbuf *sbuf;
- usr = xcalloc(1, sizeof(struct gg_user));
- usr->id = srv->user_id_count;
- srv->user_id_count++;
- srv->user_count++;
- usr->sock = srv->sock;
- addrcpy(&usr->addr, remote);
+ usr = xcalloc(1, sizeof(struct gg_user));
+ usr->id = srv->user_id_count;
+ srv->user_id_count++;
+ srv->user_count++;
+ usr->sock = srv->sock;
+ addrcpy(&usr->addr, remote);
- sbuf = sendbuf_new(srv->ev_base, PACKET_SNDBUF_MAX, 200, cb_usr_send, usr);
- if (!sbuf)
- goto err;
- usr->sbuf = sbuf;
+ sbuf = sendbuf_new(srv->ev_base, PACKET_SNDBUF_MAX, 200, cb_usr_send, usr);
+ if (!sbuf)
+ goto err;
+ usr->sbuf = sbuf;
- LIST_INSERT_HEAD(&srv->user_list, usr, entry);
- verbose("Add user %d !", usr->id);
+ LIST_INSERT_HEAD(&srv->user_list, usr, entry);
+ verbose("Add user %d !", usr->id);
- return usr;
+ return usr;
err:
- user_del(srv, usr);
- return NULL;
+ user_del(srv, usr);
+ return NULL;
}
void
user_del(struct gg_server *srv, struct gg_user *usr)
{
- verbose("Del user %d !", usr->id);
- if (usr->sbuf)
- sendbuf_free(usr->sbuf);
- user_send(usr, "", 0);
- LIST_REMOVE(usr, entry);
- srv->user_count--;
- free(usr);
+ verbose("Del user %d !", usr->id);
+ if (usr->sbuf)
+ sendbuf_free(usr->sbuf);
+ user_send(usr, "", 0);
+ LIST_REMOVE(usr, entry);
+ srv->user_count--;
+ free(usr);
}
struct gg_user *
@@ -273,7 +274,7 @@ user_find(struct gg_server *srv, struct sockaddr_in *remote)
LIST_FOREACH(usr, &srv->user_list, entry) {
u = &usr->addr;
if (u->sin_addr.s_addr == remote->sin_addr.s_addr &&
- u->sin_port == remote->sin_port)
+ u->sin_port == remote->sin_port)
return usr;
}
return NULL;
@@ -282,22 +283,22 @@ user_find(struct gg_server *srv, struct sockaddr_in *remote)
int
user_send(struct gg_user *usr, void *data, int size)
{
- int sent;
+ int sent;
- sent = sendto(usr->sock, data, size, 0, (struct sockaddr *)&usr->addr,
- sizeof(struct sockaddr_in));
- if (sent == -1)
- error("failed: %s", strerror(errno));
- return sent;
+ sent = sendto(usr->sock, data, size, 0, (struct sockaddr *)&usr->addr,
+ sizeof(struct sockaddr_in));
+ if (sent == -1)
+ error("failed: %s", strerror(errno));
+ return sent;
}
int
cb_usr_send(void *data, int size, void *usrdata)
{
- struct gg_user *usr;
+ struct gg_user *usr;
- usr = usrdata;
- return user_send(usr, data, size);
+ usr = usrdata;
+ return user_send(usr, data, size);
}
/*
@@ -312,96 +313,96 @@ cb_usr_send(void *data, int size, void *usrdata)
*/
struct gg_client *
gg_client_connect(struct event_base *ev_base, char *ip, int port,
- int (*handle_conn)(struct gg_client *),
- int (*handle_packet)(struct gg_client *, struct gg_packet *),
- void *usrdata)
+ int (*handle_conn)(struct gg_client *),
+ int (*handle_packet)(struct gg_client *, struct gg_packet *),
+ void *usrdata)
{
- struct gg_client *cli;
- struct sockaddr_in sock_addr;
- struct sendbuf *sbuf;
- struct event *ev;
- struct timeval tv;
- int s;
- int sock_on = 1;
-
- cli = xcalloc(1, sizeof(struct gg_client));
- cli->ev_base = ev_base;
- cli->ip = ip;
- cli->port = port;
- cli->handle_conn = handle_conn;
- cli->handle_packet = handle_packet;
- cli->usrdata = usrdata;
- cli->status = GG_CLIENT_STATUS_CONNECTING;
- s = socket(AF_INET, SOCK_DGRAM, 0);
- if (s < 0)
- goto err;
- cli->sock = s;
+ struct gg_client *cli;
+ struct sockaddr_in sock_addr;
+ struct sendbuf *sbuf;
+ struct event *ev;
+ struct timeval tv;
+ int s;
+ int sock_on = 1;
+
+ cli = xcalloc(1, sizeof(struct gg_client));
+ cli->ev_base = ev_base;
+ cli->ip = ip;
+ cli->port = port;
+ cli->handle_conn = handle_conn;
+ cli->handle_packet = handle_packet;
+ cli->usrdata = usrdata;
+ cli->status = GG_CLIENT_STATUS_CONNECTING;
+ s = socket(AF_INET, SOCK_DGRAM, 0);
+ if (s < 0)
+ goto err;
+ cli->sock = s;
setsockopt(s, SOL_SOCKET, SO_REUSEADDR,
- &sock_on, sizeof(sock_on));
+ &sock_on, sizeof(sock_on));
fd_nonblock(s);
bzero(&sock_addr, sizeof(sock_addr));
- sock_addr.sin_family = AF_INET;
- sock_addr.sin_addr.s_addr=inet_addr(ip);
- sock_addr.sin_port=htons(port);
- addrcpy(&cli->addr, &sock_addr);
-
- ev = event_new(ev_base, s, EV_READ|EV_PERSIST, cb_cli_receive, cli);
- event_add(ev, NULL);
- cli->ev = ev;
-
- ev = evtimer_new(ev_base, cb_cli_timer, cli);
- cli->ev_timer = ev;
- bzero(&tv, sizeof(struct timeval));
- tv.tv_sec = 0;
- evtimer_add(ev, &tv);
-
- sbuf = sendbuf_new(cli->ev_base, PACKET_SNDBUF_MAX, 200, cb_cli_send, cli);
- if (!sbuf)
- goto err;
- cli->sbuf = sbuf;
+ sock_addr.sin_family = AF_INET;
+ sock_addr.sin_addr.s_addr=inet_addr(ip);
+ sock_addr.sin_port=htons(port);
+ addrcpy(&cli->addr, &sock_addr);
+
+ ev = event_new(ev_base, s, EV_READ|EV_PERSIST, cb_cli_receive, cli);
+ event_add(ev, NULL);
+ cli->ev = ev;
+
+ ev = evtimer_new(ev_base, cb_cli_timer, cli);
+ cli->ev_timer = ev;
+ bzero(&tv, sizeof(struct timeval));
+ tv.tv_sec = 0;
+ evtimer_add(ev, &tv);
+
+ sbuf = sendbuf_new(cli->ev_base, PACKET_SNDBUF_MAX, 200, cb_cli_send, cli);
+ if (!sbuf)
+ goto err;
+ cli->sbuf = sbuf;
- return cli;
+ return cli;
err:
- error("Error ! %s", strerror(errno));
- gg_client_disconnect(cli);
- return NULL;
+ error("Error ! %s", strerror(errno));
+ gg_client_disconnect(cli);
+ return NULL;
}
void
gg_client_disconnect(struct gg_client *cli)
{
- if (cli->sbuf)
- sendbuf_free(cli->sbuf);
- if (cli->sock) {
- client_send(cli, "", 0);
- close(cli->sock);
- }
- if (cli->ev)
- event_del(cli->ev);
- if (cli->ev_timer)
- event_del(cli->ev_timer);
- free(cli);
+ if (cli->sbuf)
+ sendbuf_free(cli->sbuf);
+ if (cli->sock) {
+ client_send(cli, "", 0);
+ close(cli->sock);
+ }
+ if (cli->ev)
+ event_del(cli->ev);
+ if (cli->ev_timer)
+ event_del(cli->ev_timer);
+ free(cli);
}
int
gg_client_send(struct gg_client *cli, struct gg_packet *pkt)
{
- static struct gg_packet *newpkt;
- int size;
-
- size = pkt_getsize(pkt);
- newpkt = sendbuf_gettoken(cli->sbuf, size);
- if (!newpkt)
- return -1;
- return pkt_encode(pkt, newpkt);
+ static struct gg_packet *newpkt;
+ int size;
+
+ size = pkt_getsize(pkt);
+ newpkt = sendbuf_gettoken(cli->sbuf, size);
+ if (!newpkt)
+ return -1;
+ return pkt_encode(pkt, newpkt);
}
void
gg_client_send_flush(struct gg_client *cli)
{
- sendbuf_flush(cli->sbuf);
+ sendbuf_flush(cli->sbuf);
}
/*
@@ -411,95 +412,97 @@ gg_client_send_flush(struct gg_client *cli)
int
client_send(struct gg_client *cli, void *data, int size)
{
- int sent;
+ int sent;
- sent = sendto(cli->sock, data, size, 0, (struct sockaddr *)&cli->addr,
- sizeof(struct sockaddr_in));
- if (sent == -1)
- error("failed: %s", strerror(errno));
- return sent;
+ sent = sendto(cli->sock, data, size, 0, (struct sockaddr *)&cli->addr,
+ sizeof(struct sockaddr_in));
+ if (sent == -1)
+ error("failed: %s", strerror(errno));
+ return sent;
}
-void cb_cli_receive(evutil_socket_t fd, short what, void *arg)
+void
+cb_cli_receive(evutil_socket_t fd, short what, void *arg)
{
- struct gg_client *cli;
- struct gg_packet *pkt;
- struct sockaddr_in remote;
+ struct gg_client *cli;
+ struct gg_packet *pkt;
+ struct sockaddr_in remote;
socklen_t remote_len;
- char buf[PACKET_BUFFER_SIZE];
- char *buf_p;
- int buf_len;
- int len;
+ char buf[PACKET_BUFFER_SIZE];
+ char *buf_p;
+ int buf_len;
+ int len;
- cli = arg;
- remote_len = sizeof(struct sockaddr_in);
+ cli = arg;
+ remote_len = sizeof(struct sockaddr_in);
len = recvfrom(fd, buf, sizeof(buf), 0,
- (struct sockaddr *)&remote, &remote_len);
+ (struct sockaddr *)&remote, &remote_len);
if (addrcmp(&cli->addr, &remote)) {
- error("receiving from stranger !");
- return;
- }
+ error("receiving from stranger !");
+ return;
+ }
if (len < 0) {
error("recvfrom failed");
return;
}
- switch (cli->status) {
- case GG_CLIENT_STATUS_CONNECTING:
- verbose("Connected !");
- cli->status = GG_CLIENT_STATUS_CONNECTED;
- if (cli->handle_conn)
- cli->handle_conn(cli);
- break;
- case GG_CLIENT_STATUS_CONNECTED:
- if (len == 0) {
- verbose("libglouglou: cb_cli_receive: recvfrom = 0");
- cli->status = GG_CLIENT_STATUS_CONNECTING;
- return;
- }
- debug("Incoming data !");
- if (cli->handle_packet) {
- buf_p = buf;
- buf_len = len;
- while (buf_len > 0 && (pkt = pkt_decode(&buf_p, &buf_len)))
- cli->handle_packet(cli, pkt);
- if (buf_len > 0) {
- /* XXX store incomplete packet for next recv */
- error("incomplete packet, dropped %d bytes !", buf_len);
- }
- }
- break;
- }
+ switch (cli->status) {
+ case GG_CLIENT_STATUS_CONNECTING:
+ verbose("Connected !");
+ cli->status = GG_CLIENT_STATUS_CONNECTED;
+ if (cli->handle_conn)
+ cli->handle_conn(cli);
+ break;
+ case GG_CLIENT_STATUS_CONNECTED:
+ if (len == 0) {
+ verbose("libglouglou: cb_cli_receive: recvfrom = 0");
+ cli->status = GG_CLIENT_STATUS_CONNECTING;
+ return;
+ }
+ debug("Incoming data !");
+ if (cli->handle_packet) {
+ buf_p = buf;
+ buf_len = len;
+ while (buf_len > 0 && (pkt = pkt_decode(&buf_p, &buf_len)))
+ cli->handle_packet(cli, pkt);
+ if (buf_len > 0) {
+ /* XXX store incomplete packet for next recv */
+ error("incomplete packet, dropped %d bytes !", buf_len);
+ }
+ }
+ break;
+ }
}
-void cb_cli_timer(evutil_socket_t fd, short what, void *arg)
+void
+cb_cli_timer(evutil_socket_t fd, short what, void *arg)
{
- struct timeval tv;
- struct gg_client *cli;
-
- cli = arg;
-
- switch (cli->status) {
- case GG_CLIENT_STATUS_CONNECTING:
- client_send(cli, "", 0);
- break;
- case GG_CLIENT_STATUS_CONNECTED:
- // XXX send keepalive
- break;
- }
-
- bzero(&tv, sizeof(struct timeval));
- tv.tv_sec = 2;
- event_add(cli->ev_timer, &tv);
+ struct timeval tv;
+ struct gg_client *cli;
+
+ cli = arg;
+
+ switch (cli->status) {
+ case GG_CLIENT_STATUS_CONNECTING:
+ client_send(cli, "", 0);
+ break;
+ case GG_CLIENT_STATUS_CONNECTED:
+ // XXX send keepalive
+ break;
+ }
+
+ bzero(&tv, sizeof(struct timeval));
+ tv.tv_sec = 2;
+ event_add(cli->ev_timer, &tv);
}
int
cb_cli_send(void *data, int size, void *usrdata)
{
- struct gg_client *cli;
+ struct gg_client *cli;
- cli = usrdata;
- return client_send(cli, data, size);
+ cli = usrdata;
+ return client_send(cli, data, size);
}
/*
@@ -509,100 +512,100 @@ cb_cli_send(void *data, int size, void *usrdata)
struct gg_packet *
pkt_decode(char **buf, int *buf_len)
{
- static struct gg_packet newpkt;
- struct gg_packet *pkt;
- int len;
- int packet_len;
+ static struct gg_packet newpkt;
+ struct gg_packet *pkt;
+ int len;
+ int packet_len;
- len = *buf_len;
+ len = *buf_len;
#define invalid(msg) \
- { verbose(msg); goto invalid; }
+ { verbose(msg); goto invalid; }
#define incomplete(msg) \
- { verbose(msg); goto incomplete; }
-
- if (len < PACKET_HEADER_SIZE)
- invalid("len");
- if (len > PACKET_BUFFER_SIZE)
- invalid("len");
- pkt = (struct gg_packet *)*buf;
- if (pkt->ver != PACKET_VERSION)
- invalid("ver");
- if (pkt->type < PACKET_TYPE_MIN || pkt->type > PACKET_TYPE_MAX)
- invalid("type");
- packet_len = gg_packet_props[pkt->type].size; // XXX never overflow ?
- debug("type %d: %d %d", pkt->type, len, packet_len);
- if (len < packet_len)
- invalid("type len");
-
- newpkt.ver = pkt->ver;
- newpkt.type = pkt->type;
- switch(pkt->type) {
- case PACKET_NEWCONN:
- newpkt.newconn_id = ntohs(pkt->newconn_id);
- newpkt.newconn_src = ntohl(pkt->newconn_src);
- newpkt.newconn_dst = ntohl(pkt->newconn_dst);
- newpkt.newconn_proto = pkt->newconn_proto;
- newpkt.newconn_size = pkt->newconn_size;
- break;
- case PACKET_DELCONN:
- newpkt.delconn_id = ntohs(pkt->delconn_id);
- break;
- case PACKET_DATA:
- newpkt.data_connid = ntohs(pkt->data_connid);
- newpkt.data_size = pkt->data_size;
- break;
- case PACKET_NAME:
- newpkt.name_addr = ntohl(pkt->name_addr);
- newpkt.name_len = pkt->name_len;
- if (newpkt.name_len > GG_PKTARG_MAX)
- invalid("type name name_len");
- if (len < packet_len + newpkt.name_len)
- goto incomplete;
- packet_len = packet_len + newpkt.name_len;
- strncpy((char *)newpkt.name_fqdn, (char *)pkt->name_fqdn,
- newpkt.name_len);
- break;
- case PACKET_FORK:
- newpkt.fork_pid = ntohl(pkt->fork_pid);
- newpkt.fork_ppid = ntohl(pkt->fork_ppid);
- newpkt.fork_cpid = ntohl(pkt->fork_cpid);
- newpkt.fork_tgid = ntohl(pkt->fork_tgid);
- break;
- case PACKET_EXEC:
- newpkt.exec_pid = ntohl(pkt->exec_pid);
- newpkt.exec_cmdlen = pkt->exec_cmdlen;
- if (newpkt.exec_cmdlen > GG_PKTARG_MAX)
- invalid("type exec cmdlen");
- if (len < packet_len + newpkt.exec_cmdlen)
- goto incomplete;
- packet_len = packet_len + newpkt.exec_cmdlen;
- strncpy((char *)newpkt.exec_cmd, (char *)pkt->exec_cmd,
- newpkt.exec_cmdlen);
- break;
- case PACKET_EXIT:
- newpkt.exit_pid = ntohl(pkt->exit_pid);
- newpkt.exit_tgid = ntohl(pkt->exit_tgid);
- newpkt.exit_ecode = pkt->exit_ecode;
- break;
- default:
- goto invalid;
- invalid("type switch");
- }
-
- *buf = *buf + packet_len;
- *buf_len = len - packet_len;
- return &newpkt;
+ { verbose(msg); goto incomplete; }
+
+ if (len < PACKET_HEADER_SIZE)
+ invalid("len");
+ if (len > PACKET_BUFFER_SIZE)
+ invalid("len");
+ pkt = (struct gg_packet *)*buf;
+ if (pkt->ver != PACKET_VERSION)
+ invalid("ver");
+ if (pkt->type < PACKET_TYPE_MIN || pkt->type > PACKET_TYPE_MAX)
+ invalid("type");
+ packet_len = gg_packet_props[pkt->type].size; // XXX never overflow ?
+ debug("type %d: %d %d", pkt->type, len, packet_len);
+ if (len < packet_len)
+ invalid("type len");
+
+ newpkt.ver = pkt->ver;
+ newpkt.type = pkt->type;
+ switch(pkt->type) {
+ case PACKET_NEWCONN:
+ newpkt.newconn_id = ntohs(pkt->newconn_id);
+ newpkt.newconn_src = ntohl(pkt->newconn_src);
+ newpkt.newconn_dst = ntohl(pkt->newconn_dst);
+ newpkt.newconn_proto = pkt->newconn_proto;
+ newpkt.newconn_size = pkt->newconn_size;
+ break;
+ case PACKET_DELCONN:
+ newpkt.delconn_id = ntohs(pkt->delconn_id);
+ break;
+ case PACKET_DATA:
+ newpkt.data_connid = ntohs(pkt->data_connid);
+ newpkt.data_size = pkt->data_size;
+ break;
+ case PACKET_NAME:
+ newpkt.name_addr = ntohl(pkt->name_addr);
+ newpkt.name_len = pkt->name_len;
+ if (newpkt.name_len > GG_PKTARG_MAX)
+ invalid("type name name_len");
+ if (len < packet_len + newpkt.name_len)
+ goto incomplete;
+ packet_len = packet_len + newpkt.name_len;
+ strncpy((char *)newpkt.name_fqdn, (char *)pkt->name_fqdn,
+ newpkt.name_len);
+ break;
+ case PACKET_FORK:
+ newpkt.fork_pid = ntohl(pkt->fork_pid);
+ newpkt.fork_ppid = ntohl(pkt->fork_ppid);
+ newpkt.fork_cpid = ntohl(pkt->fork_cpid);
+ newpkt.fork_tgid = ntohl(pkt->fork_tgid);
+ break;
+ case PACKET_EXEC:
+ newpkt.exec_pid = ntohl(pkt->exec_pid);
+ newpkt.exec_cmdlen = pkt->exec_cmdlen;
+ if (newpkt.exec_cmdlen > GG_PKTARG_MAX)
+ invalid("type exec cmdlen");
+ if (len < packet_len + newpkt.exec_cmdlen)
+ goto incomplete;
+ packet_len = packet_len + newpkt.exec_cmdlen;
+ strncpy((char *)newpkt.exec_cmd, (char *)pkt->exec_cmd,
+ newpkt.exec_cmdlen);
+ break;
+ case PACKET_EXIT:
+ newpkt.exit_pid = ntohl(pkt->exit_pid);
+ newpkt.exit_tgid = ntohl(pkt->exit_tgid);
+ newpkt.exit_ecode = pkt->exit_ecode;
+ break;
+ default:
+ goto invalid;
+ invalid("type switch");
+ }
+
+ *buf = *buf + packet_len;
+ *buf_len = len - packet_len;
+ return &newpkt;
incomplete:
- error("incomplete packet");
- *buf_len = len;
- return NULL;
+ error("incomplete packet");
+ *buf_len = len;
+ return NULL;
invalid:
- error("invalid packet");
- *buf_len = 0;
- return NULL;
+ error("invalid packet");
+ *buf_len = 0;
+ return NULL;
}
/*
@@ -612,96 +615,96 @@ invalid:
int
pkt_encode(struct gg_packet *pkt, struct gg_packet *newpkt)
{
- if (pkt->type < PACKET_TYPE_MIN || pkt->type > PACKET_TYPE_MAX)
- invalid("type");
-
- newpkt->ver = pkt->ver;
- newpkt->type = pkt->type;
- switch(pkt->type) {
- case PACKET_NEWCONN:
- newpkt->newconn_id = htons(pkt->newconn_id);
- newpkt->newconn_src = htonl(pkt->newconn_src);
- newpkt->newconn_dst = htonl(pkt->newconn_dst);
- newpkt->newconn_proto = pkt->newconn_proto;
- newpkt->newconn_size = pkt->newconn_size;
- break;
- case PACKET_DELCONN:
- newpkt->delconn_id = htons(pkt->delconn_id);
- break;
- case PACKET_DATA:
- newpkt->data_connid = htons(pkt->data_connid);
- newpkt->data_size = pkt->data_size;
- break;
- case PACKET_NAME:
- if (pkt->name_len > GG_PKTARG_MAX)
- goto invalid;
- newpkt->name_addr = htonl(pkt->name_addr);
- newpkt->name_len = pkt->name_len;
- strncpy((char *)newpkt->name_fqdn, (char *)pkt->name_fqdn,
- pkt->name_len);
- break;
- case PACKET_FORK:
- newpkt->fork_pid = htonl(pkt->fork_pid);
- newpkt->fork_ppid = htonl(pkt->fork_ppid);
- newpkt->fork_cpid = htonl(pkt->fork_cpid);
- newpkt->fork_tgid = htonl(pkt->fork_tgid);
- break;
- case PACKET_EXEC:
- if (pkt->exec_cmdlen > GG_PKTARG_MAX)
- goto invalid;
- newpkt->exec_pid = htonl(pkt->exec_pid);
- newpkt->exec_cmdlen = pkt->exec_cmdlen;
- strncpy((char *)newpkt->exec_cmd, (char *)pkt->exec_cmd,
- pkt->exec_cmdlen);
- break;
- case PACKET_EXIT:
- newpkt->exit_pid = htonl(pkt->exit_pid);
- newpkt->exit_tgid = htonl(pkt->exit_tgid);
- newpkt->exit_ecode = pkt->exit_ecode;
- break;
- default:
- error("Unsupported packet type");
- return -1;
- }
- return pkt_getsize(newpkt);
+ if (pkt->type < PACKET_TYPE_MIN || pkt->type > PACKET_TYPE_MAX)
+ invalid("type");
+
+ newpkt->ver = pkt->ver;
+ newpkt->type = pkt->type;
+ switch(pkt->type) {
+ case PACKET_NEWCONN:
+ newpkt->newconn_id = htons(pkt->newconn_id);
+ newpkt->newconn_src = htonl(pkt->newconn_src);
+ newpkt->newconn_dst = htonl(pkt->newconn_dst);
+ newpkt->newconn_proto = pkt->newconn_proto;
+ newpkt->newconn_size = pkt->newconn_size;
+ break;
+ case PACKET_DELCONN:
+ newpkt->delconn_id = htons(pkt->delconn_id);
+ break;
+ case PACKET_DATA:
+ newpkt->data_connid = htons(pkt->data_connid);
+ newpkt->data_size = pkt->data_size;
+ break;
+ case PACKET_NAME:
+ if (pkt->name_len > GG_PKTARG_MAX)
+ goto invalid;
+ newpkt->name_addr = htonl(pkt->name_addr);
+ newpkt->name_len = pkt->name_len;
+ strncpy((char *)newpkt->name_fqdn, (char *)pkt->name_fqdn,
+ pkt->name_len);
+ break;
+ case PACKET_FORK:
+ newpkt->fork_pid = htonl(pkt->fork_pid);
+ newpkt->fork_ppid = htonl(pkt->fork_ppid);
+ newpkt->fork_cpid = htonl(pkt->fork_cpid);
+ newpkt->fork_tgid = htonl(pkt->fork_tgid);
+ break;
+ case PACKET_EXEC:
+ if (pkt->exec_cmdlen > GG_PKTARG_MAX)
+ goto invalid;
+ newpkt->exec_pid = htonl(pkt->exec_pid);
+ newpkt->exec_cmdlen = pkt->exec_cmdlen;
+ strncpy((char *)newpkt->exec_cmd, (char *)pkt->exec_cmd,
+ pkt->exec_cmdlen);
+ break;
+ case PACKET_EXIT:
+ newpkt->exit_pid = htonl(pkt->exit_pid);
+ newpkt->exit_tgid = htonl(pkt->exit_tgid);
+ newpkt->exit_ecode = pkt->exit_ecode;
+ break;
+ default:
+ error("Unsupported packet type");
+ return -1;
+ }
+ return pkt_getsize(newpkt);
invalid:
- error("invalid packet");
- return -1;
+ error("invalid packet");
+ return -1;
}
int
pkt_getsize(struct gg_packet *pkt)
{
- int packet_len;
-
- packet_len = gg_packet_props[pkt->type].size; // XXX never overflow ?
- switch(pkt->type) {
- case PACKET_NEWCONN:
- case PACKET_DELCONN:
- case PACKET_DATA:
- case PACKET_FORK:
- case PACKET_EXIT:
- break;
- case PACKET_NAME:
- if (pkt->name_len > GG_PKTARG_MAX)
- goto invalid;
- packet_len = packet_len + pkt->name_len;
- break;
- case PACKET_EXEC:
- if (pkt->exec_cmdlen > GG_PKTARG_MAX)
- goto invalid;
- packet_len = packet_len + pkt->exec_cmdlen;
- break;
- default:
- error("Unsupported packet type");
- return -1;
- }
- return packet_len;
+ int packet_len;
+
+ packet_len = gg_packet_props[pkt->type].size; // XXX never overflow ?
+ switch(pkt->type) {
+ case PACKET_NEWCONN:
+ case PACKET_DELCONN:
+ case PACKET_DATA:
+ case PACKET_FORK:
+ case PACKET_EXIT:
+ break;
+ case PACKET_NAME:
+ if (pkt->name_len > GG_PKTARG_MAX)
+ goto invalid;
+ packet_len = packet_len + pkt->name_len;
+ break;
+ case PACKET_EXEC:
+ if (pkt->exec_cmdlen > GG_PKTARG_MAX)
+ goto invalid;
+ packet_len = packet_len + pkt->exec_cmdlen;
+ break;
+ default:
+ error("Unsupported packet type");
+ return -1;
+ }
+ return packet_len;
invalid:
- error("invalid packet");
- return -1;
+ error("invalid packet");
+ return -1;
}
/*
@@ -711,11 +714,11 @@ invalid:
int
gg_verbosity_get(void)
{
- return _verbosity;
+ return _verbosity;
}
void
gg_verbosity_set(int verb)
{
- _verbosity = verb;
+ _verbosity = verb;
}
diff --git a/libglouglou/libglouglou.h b/libglouglou/libglouglou.h
index b081685..cdb29e7 100644
--- a/libglouglou/libglouglou.h
+++ b/libglouglou/libglouglou.h
@@ -22,22 +22,22 @@
#define GG_PKTDATA_SIZE_FACTOR 20
#define GG_PKTDATA_SIZE_ENCODE(pktsize, size, response) \
- (pktsize = ((size / GG_PKTDATA_SIZE_FACTOR) & 0x7f) | (response << 7))
+ (pktsize = ((size / GG_PKTDATA_SIZE_FACTOR) & 0x7f) | (response << 7))
#define GG_PKTDATA_SIZE_DECODE(pktsize, size, response) { \
- response = pktsize >> 7; size = (pktsize & 0x7f) * GG_PKTDATA_SIZE_FACTOR; \
+ response = pktsize >> 7; size = (pktsize & 0x7f) * GG_PKTDATA_SIZE_FACTOR; \
}
#define PACKET_HEADER_SIZE 2
#define PACKET_TYPE_MIN 0x00
#define PACKET_TYPE_MAX 0x12
enum gg_packet_type { /* u_int8_t */
- PACKET_NEWCONN = 0x00,
- PACKET_DELCONN = 0x01,
- PACKET_DATA = 0x02,
- PACKET_NAME = 0x03,
- PACKET_FORK = 0x10,
- PACKET_EXEC = 0x11,
- PACKET_EXIT = 0x12,
+ PACKET_NEWCONN = 0x00,
+ PACKET_DELCONN = 0x01,
+ PACKET_DATA = 0x02,
+ PACKET_NAME = 0x03,
+ PACKET_FORK = 0x10,
+ PACKET_EXEC = 0x11,
+ PACKET_EXIT = 0x12,
};
/* XXX is packed needed everywhere ? */
@@ -64,22 +64,22 @@ struct __attribute__((packed)) gg_packet {
u_int8_t len;
u_char fqdn[GG_PKTARG_MAX];
} name;
- struct __attribute__((packed)) fork {
- u_int32_t pid;
- u_int32_t ppid;
- u_int32_t cpid;
- u_int32_t tgid;
- } fork;
- struct __attribute__((packed)) exec {
- u_int32_t pid;
- u_int8_t cmdlen;
- u_char cmd[GG_PKTARG_MAX];
- } exec;
- struct __attribute__((packed)) exit {
- u_int32_t pid;
- u_int32_t tgid;
- u_int8_t ecode;
- } exit;
+ struct __attribute__((packed)) fork {
+ u_int32_t pid;
+ u_int32_t ppid;
+ u_int32_t cpid;
+ u_int32_t tgid;
+ } fork;
+ struct __attribute__((packed)) exec {
+ u_int32_t pid;
+ u_int8_t cmdlen;
+ u_char cmd[GG_PKTARG_MAX];
+ } exec;
+ struct __attribute__((packed)) exit {
+ u_int32_t pid;
+ u_int32_t tgid;
+ u_int8_t ecode;
+ } exit;
} pdat;
#define newconn_id pdat.newconn.id
#define newconn_src pdat.newconn.src
@@ -105,72 +105,73 @@ struct __attribute__((packed)) gg_packet {
};
typedef struct gg_packet_props_t {
- int size;
+ int size;
} gg_packet_props_t;
extern gg_packet_props_t gg_packet_props[];
struct gg_user {
LIST_ENTRY(gg_user) entry;
- int id;
- int sock;
- struct sockaddr_in addr;
- struct sendbuf *sbuf;
+ int id;
+ int sock;
+ struct sockaddr_in addr;
+ struct sendbuf *sbuf;
};
struct gg_server {
- struct event_base *ev_base;
- const char *ip;
- int port;
- struct sockaddr_in addr;
- struct event *ev;
- int sock;
- int (*handle_conn)(struct gg_server *, struct gg_user *);
- int (*handle_packet)(struct gg_server *, struct gg_user *, struct gg_packet *);
- void *usrdata;
- LIST_HEAD(, gg_user) user_list;
- int user_count;
- int user_id_count;
+ struct event_base *ev_base;
+ const char *ip;
+ int port;
+ struct sockaddr_in addr;
+ struct event *ev;
+ int sock;
+ int (*handle_conn)(struct gg_server *, struct gg_user *);
+ int (*handle_packet)(struct gg_server *,
+ struct gg_user *, struct gg_packet *);
+ void *usrdata;
+ LIST_HEAD(, gg_user) user_list;
+ int user_count;
+ int user_id_count;
};
enum client_status {
- GG_CLIENT_STATUS_CONNECTING = 0,
- GG_CLIENT_STATUS_CONNECTED = 1
+ GG_CLIENT_STATUS_CONNECTING = 0,
+ GG_CLIENT_STATUS_CONNECTED = 1
};
struct gg_client {
- struct event_base *ev_base;
- const char *ip;
- int port;
- struct sockaddr_in addr;
- struct event *ev;
- struct event *ev_timer;
- int sock;
- enum client_status status;
- int (*handle_conn)(struct gg_client *);
- int (*handle_packet)(struct gg_client *, struct gg_packet *);
- void *usrdata;
- struct sendbuf *sbuf;
+ struct event_base *ev_base;
+ const char *ip;
+ int port;
+ struct sockaddr_in addr;
+ struct event *ev;
+ struct event *ev_timer;
+ int sock;
+ enum client_status status;
+ int (*handle_conn)(struct gg_client *);
+ int (*handle_packet)(struct gg_client *, struct gg_packet *);
+ void *usrdata;
+ struct sendbuf *sbuf;
};
-struct gg_server *gg_server_start(struct event_base *, char *, int,
- int (*handle_conn)(struct gg_server *, struct gg_user *),
- int (*handle_packet)(struct gg_server *, struct gg_user *, struct gg_packet *),
- void *);
-void gg_server_stop(struct gg_server *);
-int gg_server_send(struct gg_server *, struct gg_packet *, struct gg_user *);
-void gg_server_send_flush(struct gg_server *, struct gg_user *);
-
-struct gg_client *gg_client_connect(struct event_base *, char *, int,
- int (*handle_conn)(struct gg_client *),
- int (*handle_packet)(struct gg_client *, struct gg_packet *),
- void *);
-void gg_client_disconnect(struct gg_client *);
-int gg_client_send(struct gg_client *, struct gg_packet *);
-void gg_client_send_flush(struct gg_client *);
-
-int gg_verbosity_get(void);
-void gg_verbosity_set(int);
+struct gg_server *gg_server_start(struct event_base *, char *, int,
+ int (*handle_conn)(struct gg_server *, struct gg_user *),
+ int (*handle_packet)(struct gg_server *, struct gg_user *, struct gg_packet *),
+ void *);
+void gg_server_stop(struct gg_server *);
+int gg_server_send(struct gg_server *, struct gg_packet *, struct gg_user *);
+void gg_server_send_flush(struct gg_server *, struct gg_user *);
+
+struct gg_client *gg_client_connect(struct event_base *, char *, int,
+ int (*handle_conn)(struct gg_client *),
+ int (*handle_packet)(struct gg_client *, struct gg_packet *),
+ void *);
+void gg_client_disconnect(struct gg_client *);
+int gg_client_send(struct gg_client *, struct gg_packet *);
+void gg_client_send_flush(struct gg_client *);
+
+int gg_verbosity_get(void);
+void gg_verbosity_set(int);
/* utils.c */
@@ -180,23 +181,23 @@ void gg_verbosity_set(int);
#define GGLOG_INFO 1
#define GGLOG_DEBUG 2
-int gg_log_init(char *, int);
-void gg_log_shutdown(void);
-void gg_log_tmp(const char *, ...);
-void gg_log_debug(const char *, ...);
-void gg_log_info(const char *, ...);
-void gg_log_warn(const char *, ...);
+int gg_log_init(char *, int);
+void gg_log_shutdown(void);
+void gg_log_tmp(const char *, ...);
+void gg_log_debug(const char *, ...);
+void gg_log_info(const char *, ...);
+void gg_log_warn(const char *, ...);
#if defined(__OpenBSD__)
void __dead gg_log_fatal(const char *, ...);
#else
-void gg_log_fatal(const char *, ...);
+void gg_log_fatal(const char *, ...);
#endif
-void *xmalloc(size_t);
-void *xcalloc(size_t, size_t);
-void fd_nonblock(int);
-void addrcpy(struct sockaddr_in *, struct sockaddr_in *);
-int addrcmp(struct sockaddr_in *, struct sockaddr_in *);
-void droppriv(char *, int, char *);
+void *xmalloc(size_t);
+void *xcalloc(size_t, size_t);
+void fd_nonblock(int);
+void addrcpy(struct sockaddr_in *, struct sockaddr_in *);
+int addrcmp(struct sockaddr_in *, struct sockaddr_in *);
+void droppriv(char *, int, char *);
#endif /* _LIBGLOUGLOU_H_ */
diff --git a/libglouglou/sendbuf.c b/libglouglou/sendbuf.c
index 4e83294..47c4fd1 100644
--- a/libglouglou/sendbuf.c
+++ b/libglouglou/sendbuf.c
@@ -14,45 +14,45 @@ static void cb_timer(evutil_socket_t, short, void *);
*/
struct sendbuf *
sendbuf_new(struct event_base *ev_base, int buffer_size, int msec_max,
- int (*send_func)(void *, int, void *), void *usrdata)
+ int (*send_func)(void *, int, void *), void *usrdata)
{
- struct sendbuf *sbuf = NULL;
- struct event *ev_timer;
-
- sbuf = calloc(1, sizeof(struct sendbuf));
- if (!sbuf)
- return NULL;
- sbuf->ev_base = ev_base;
- sbuf->msec_max = msec_max;
- sbuf->buffer_size = buffer_size;
- sbuf->send_func = send_func;
- sbuf->usrdata = usrdata;
- sbuf->buffer = malloc(sbuf->buffer_size);
- if (!sbuf->buffer)
- goto err;
-
- ev_timer = evtimer_new(ev_base, cb_timer, sbuf);
- sbuf->ev_timer = ev_timer;
- sbuf->ev_timer_tv.tv_usec = msec_max * 1000;
- evtimer_add(ev_timer, &sbuf->ev_timer_tv);
-
- return sbuf;
+ struct sendbuf *sbuf = NULL;
+ struct event *ev_timer;
+
+ sbuf = calloc(1, sizeof(struct sendbuf));
+ if (!sbuf)
+ return NULL;
+ sbuf->ev_base = ev_base;
+ sbuf->msec_max = msec_max;
+ sbuf->buffer_size = buffer_size;
+ sbuf->send_func = send_func;
+ sbuf->usrdata = usrdata;
+ sbuf->buffer = malloc(sbuf->buffer_size);
+ if (!sbuf->buffer)
+ goto err;
+
+ ev_timer = evtimer_new(ev_base, cb_timer, sbuf);
+ sbuf->ev_timer = ev_timer;
+ sbuf->ev_timer_tv.tv_usec = msec_max * 1000;
+ evtimer_add(ev_timer, &sbuf->ev_timer_tv);
+
+ return sbuf;
err:
- sendbuf_free(sbuf);
- return NULL;
+ sendbuf_free(sbuf);
+ return NULL;
}
void
sendbuf_free(struct sendbuf *sbuf)
{
- if (sbuf->ev_timer)
- event_del(sbuf->ev_timer);
- if (sbuf->buffer && sbuf->send_func)
- sendbuf_flush(sbuf);
- if (sbuf->buffer)
- free(sbuf->buffer);
- free(sbuf);
+ if (sbuf->ev_timer)
+ event_del(sbuf->ev_timer);
+ if (sbuf->buffer && sbuf->send_func)
+ sendbuf_flush(sbuf);
+ if (sbuf->buffer)
+ free(sbuf->buffer);
+ free(sbuf);
}
/*
@@ -63,14 +63,14 @@ sendbuf_free(struct sendbuf *sbuf)
int
sendbuf_append(struct sendbuf *sbuf, void *token, int size)
{
- if (sbuf->buffer_pos + size >= sbuf->buffer_size)
- if (sendbuf_flush(sbuf) == -1)
- return -1;
+ if (sbuf->buffer_pos + size >= sbuf->buffer_size)
+ if (sendbuf_flush(sbuf) == -1)
+ return -1;
- memcpy(sbuf->buffer + sbuf->buffer_pos, token, size);
- sbuf->buffer_pos = sbuf->buffer_pos + size;
+ memcpy(sbuf->buffer + sbuf->buffer_pos, token, size);
+ sbuf->buffer_pos = sbuf->buffer_pos + size;
- return size;
+ return size;
}
/*
@@ -81,16 +81,16 @@ sendbuf_append(struct sendbuf *sbuf, void *token, int size)
void *
sendbuf_gettoken(struct sendbuf *sbuf, int size)
{
- void *token;
+ void *token;
- if (sbuf->buffer_pos + size >= sbuf->buffer_size)
- if (sendbuf_flush(sbuf) == -1)
- return NULL;
+ if (sbuf->buffer_pos + size >= sbuf->buffer_size)
+ if (sendbuf_flush(sbuf) == -1)
+ return NULL;
- token = sbuf->buffer + sbuf->buffer_pos;
- sbuf->buffer_pos = sbuf->buffer_pos + size;
+ token = sbuf->buffer + sbuf->buffer_pos;
+ sbuf->buffer_pos = sbuf->buffer_pos + size;
- return token;
+ return token;
}
/*
@@ -102,28 +102,28 @@ sendbuf_gettoken(struct sendbuf *sbuf, int size)
int
sendbuf_flush(struct sendbuf *sbuf)
{
- int tosend, sent;
-
- if (sbuf->buffer_pos == 0)
- return 0;
-
- sbuf->flushing = 1;
-
- tosend = sbuf->buffer_pos - sbuf->flushing_pos;
- sent = sbuf->send_func(sbuf->buffer + sbuf->flushing_pos,
- tosend, sbuf->usrdata);
- if (sent == -1) {
- // XXX handle error
- return -1;
- } else if (sent < tosend) {
- sbuf->flushing_pos = sbuf->flushing_pos + sent;
- return -1;
- }
- sbuf->buffer_pos = 0;
-
- sbuf->flushing = 0;
- sbuf->flushing_pos = 0;
- return 0;
+ int tosend, sent;
+
+ if (sbuf->buffer_pos == 0)
+ return 0;
+
+ sbuf->flushing = 1;
+
+ tosend = sbuf->buffer_pos - sbuf->flushing_pos;
+ sent = sbuf->send_func(sbuf->buffer + sbuf->flushing_pos,
+ tosend, sbuf->usrdata);
+ if (sent == -1) {
+ // XXX handle error
+ return -1;
+ } else if (sent < tosend) {
+ sbuf->flushing_pos = sbuf->flushing_pos + sent;
+ return -1;
+ }
+ sbuf->buffer_pos = 0;
+
+ sbuf->flushing = 0;
+ sbuf->flushing_pos = 0;
+ return 0;
}
@@ -134,9 +134,9 @@ sendbuf_flush(struct sendbuf *sbuf)
static void
cb_timer(evutil_socket_t fd, short what, void *arg)
{
- struct sendbuf *sbuf;
+ struct sendbuf *sbuf;
- sbuf = arg;
- sendbuf_flush(sbuf);
- evtimer_add(sbuf->ev_timer, &sbuf->ev_timer_tv);
+ sbuf = arg;
+ sendbuf_flush(sbuf);
+ evtimer_add(sbuf->ev_timer, &sbuf->ev_timer_tv);
}
diff --git a/libglouglou/sendbuf.h b/libglouglou/sendbuf.h
index 6d163d4..7a7bd05 100644
--- a/libglouglou/sendbuf.h
+++ b/libglouglou/sendbuf.h
@@ -2,23 +2,23 @@
#include <sys/queue.h>
struct sendbuf {
- struct event_base *ev_base;
- struct event *ev_timer;
- struct timeval ev_timer_tv;
- int msec_max;
- int buffer_size;
- void *buffer;
- int buffer_pos; /* next to use in buffer */
- int flushing;
- int flushing_pos; /* next to send in buffer */
- int (*send_func)(void *, int, void *);
- void *usrdata;
+ struct event_base *ev_base;
+ struct event *ev_timer;
+ struct timeval ev_timer_tv;
+ int msec_max;
+ int buffer_size;
+ void *buffer;
+ int buffer_pos; /* next to use in buffer */
+ int flushing;
+ int flushing_pos; /* next to send in buffer */
+ int (*send_func)(void *, int, void *);
+ void *usrdata;
};
-struct sendbuf *sendbuf_new(struct event_base *, int, int,
- int (*send_func)(void *, int, void *),
- void *);
-void sendbuf_free(struct sendbuf *);
-int sendbuf_append(struct sendbuf *, void *, int);
-void *sendbuf_gettoken(struct sendbuf *, int);
-int sendbuf_flush(struct sendbuf *);
+struct sendbuf *sendbuf_new(struct event_base *, int, int,
+ int (*send_func)(void *, int, void *),
+ void *);
+void sendbuf_free(struct sendbuf *);
+int sendbuf_append(struct sendbuf *, void *, int);
+void *sendbuf_gettoken(struct sendbuf *, int);
+int sendbuf_flush(struct sendbuf *);
diff --git a/libglouglou/tests/connect.c b/libglouglou/tests/connect.c
index 3e46667..2a203c8 100644
--- a/libglouglou/tests/connect.c
+++ b/libglouglou/tests/connect.c
@@ -8,39 +8,41 @@ int cli_connect_ok = 0;
int
srv_handle_conn(struct gg_server *srv, struct gg_user *usr)
{
- if (usr->id == 0)
- srv_connect_ok = 1;
- return 0;
+ if (usr->id == 0)
+ srv_connect_ok = 1;
+ return 0;
}
int
cli_handle_conn(struct gg_client *cli)
{
- cli_connect_ok = 1;
- return 0;
+ cli_connect_ok = 1;
+ return 0;
}
int
main(void)
{
- struct event_base *ev_base;
-
- ev_base = event_base_new();
- if (!gg_server_start(ev_base, "127.0.0.1", 12345, srv_handle_conn, NULL, NULL)) {
- printf("error: gg_server_start returned NULL\n");
- return 1;
- }
- if (!gg_client_connect(ev_base, "127.0.0.1", 12345, cli_handle_conn, NULL, NULL)) {
- printf("error: gg_client_connect returned NULL\n");
- return 1;
- }
- event_base_loop(ev_base, EVLOOP_NONBLOCK);
- event_base_loop(ev_base, EVLOOP_NONBLOCK);
-
- if (srv_connect_ok == 0)
- printf("error: srv_connect_ok == 0\n");
- if (cli_connect_ok == 0)
- printf("error: cli_connect_ok == 0\n");
-
- return (!srv_connect_ok || !cli_connect_ok);
+ struct event_base *ev_base;
+
+ ev_base = event_base_new();
+ if (!gg_server_start(ev_base, "127.0.0.1", 12345,
+ srv_handle_conn,NULL, NULL)) {
+ printf("error: gg_server_start returned NULL\n");
+ return 1;
+ }
+ if (!gg_client_connect(ev_base, "127.0.0.1", 12345,
+ cli_handle_conn, NULL, NULL)) {
+ printf("error: gg_client_connect returned NULL\n");
+ return 1;
+ }
+ event_base_loop(ev_base, EVLOOP_NONBLOCK);
+ event_base_loop(ev_base, EVLOOP_NONBLOCK);
+
+ if (srv_connect_ok == 0)
+ printf("error: srv_connect_ok == 0\n");
+ if (cli_connect_ok == 0)
+ printf("error: cli_connect_ok == 0\n");
+
+ return (!srv_connect_ok || !cli_connect_ok);
}
diff --git a/libglouglou/tests/sendrecv.c b/libglouglou/tests/sendrecv.c
index cbfb402..2df9e01 100644
--- a/libglouglou/tests/sendrecv.c
+++ b/libglouglou/tests/sendrecv.c
@@ -6,62 +6,63 @@ int srv_recv_ok = 0;
int cli_recv_ok = 0;
int
-srv_handle_packet(struct gg_server *srv, struct gg_user *usr, struct gg_packet *pkt)
+srv_handle_packet(struct gg_server *srv,
+ struct gg_user *usr, struct gg_packet *pkt)
{
- if (pkt->type == PACKET_NEWCONN)
- srv_recv_ok = 1;
- return 0;
+ if (pkt->type == PACKET_NEWCONN)
+ srv_recv_ok = 1;
+ return 0;
}
int
cli_handle_packet(struct gg_client *cli, struct gg_packet *pkt)
{
- if (pkt->type == PACKET_NEWCONN)
- cli_recv_ok = 1;
- return 0;
+ if (pkt->type == PACKET_NEWCONN)
+ cli_recv_ok = 1;
+ return 0;
}
int
main(void)
{
- struct event_base *ev_base;
- struct gg_server *srv;
- struct gg_client *cli;
- struct gg_packet pkt;
+ struct event_base *ev_base;
+ struct gg_server *srv;
+ struct gg_client *cli;
+ struct gg_packet pkt;
- ev_base = event_base_new();
- srv = gg_server_start(ev_base, "127.0.0.1", GLOUGLOU_PROBE_DEFAULT_PORT,
- NULL, srv_handle_packet, NULL);
- if (!srv) {
- printf("error: gg_server_start returned NULL\n");
- return 1;
- }
- cli = gg_client_connect(ev_base, "127.0.0.1", GLOUGLOU_PROBE_DEFAULT_PORT,
- NULL, cli_handle_packet, NULL);
- if (!cli) {
- printf("error: gg_client_connect returned NULL\n");
- return 1;
- }
- event_base_loop(ev_base, EVLOOP_NONBLOCK);
- event_base_loop(ev_base, EVLOOP_NONBLOCK);
+ ev_base = event_base_new();
+ srv = gg_server_start(ev_base, "127.0.0.1", GLOUGLOU_PROBE_DEFAULT_PORT,
+ NULL, srv_handle_packet, NULL);
+ if (!srv) {
+ printf("error: gg_server_start returned NULL\n");
+ return 1;
+ }
+ cli = gg_client_connect(ev_base, "127.0.0.1", GLOUGLOU_PROBE_DEFAULT_PORT,
+ NULL, cli_handle_packet, NULL);
+ if (!cli) {
+ printf("error: gg_client_connect returned NULL\n");
+ return 1;
+ }
+ event_base_loop(ev_base, EVLOOP_NONBLOCK);
+ event_base_loop(ev_base, EVLOOP_NONBLOCK);
- pkt.ver = PACKET_VERSION;
- pkt.type = PACKET_NEWCONN;
+ pkt.ver = PACKET_VERSION;
+ pkt.type = PACKET_NEWCONN;
- gg_server_send(srv, &pkt, NULL);
- gg_server_send_flush(srv, NULL);
- gg_client_send(cli, &pkt);
- gg_client_send_flush(cli);
+ gg_server_send(srv, &pkt, NULL);
+ gg_server_send_flush(srv, NULL);
+ gg_client_send(cli, &pkt);
+ gg_client_send_flush(cli);
- event_base_loop(ev_base, EVLOOP_NONBLOCK);
+ event_base_loop(ev_base, EVLOOP_NONBLOCK);
- gg_client_disconnect(cli);
- gg_server_stop(srv);
+ gg_client_disconnect(cli);
+ gg_server_stop(srv);
- if (srv_recv_ok == 0)
- printf("error: srv_recv_ok == 0\n");
- if (cli_recv_ok == 0)
- printf("error: cli_recv_ok == 0\n");
+ if (srv_recv_ok == 0)
+ printf("error: srv_recv_ok == 0\n");
+ if (cli_recv_ok == 0)
+ printf("error: cli_recv_ok == 0\n");
- return (!srv_recv_ok || !cli_recv_ok);
+ return (!srv_recv_ok || !cli_recv_ok);
}
diff --git a/libglouglou/utils.c b/libglouglou/utils.c
index 8ca3660..2c17eb0 100644
--- a/libglouglou/utils.c
+++ b/libglouglou/utils.c
@@ -43,47 +43,47 @@ gg_log_init(char *filename, int level)
void
gg_log_shutdown(void)
{
- fclose(_logfile);
+ fclose(_logfile);
}
void
gg_log_tmp(const char *msg, ...)
{
- va_list ap;
+ va_list ap;
- va_start(ap, msg);
- logit(GGLOG_FORCED, "XXX ", msg, ap);
- va_end(ap);
+ va_start(ap, msg);
+ logit(GGLOG_FORCED, "XXX ", msg, ap);
+ va_end(ap);
}
void
gg_log_debug(const char *msg, ...)
{
- va_list ap;
+ va_list ap;
- va_start(ap, msg);
- logit(GGLOG_DEBUG, "", msg, ap);
- va_end(ap);
+ va_start(ap, msg);
+ logit(GGLOG_DEBUG, "", msg, ap);
+ va_end(ap);
}
void
gg_log_info(const char *msg, ...)
{
- va_list ap;
+ va_list ap;
- va_start(ap, msg);
- logit(GGLOG_INFO, "", msg, ap);
- va_end(ap);
+ va_start(ap, msg);
+ logit(GGLOG_INFO, "", msg, ap);
+ va_end(ap);
}
void
gg_log_warn(const char *msg, ...)
{
- va_list ap;
+ va_list ap;
- va_start(ap, msg);
- logit(GGLOG_WARN, "", msg, ap);
- va_end(ap);
+ va_start(ap, msg);
+ logit(GGLOG_WARN, "", msg, ap);
+ va_end(ap);
}
#if defined(__OpenBSD__)
@@ -93,29 +93,29 @@ void
#endif
gg_log_fatal(const char *msg, ...)
{
- va_list ap;
+ va_list ap;
- va_start(ap, msg);
- logit(GGLOG_FATAL, "fatal: ", msg, ap);
- va_end(ap);
+ va_start(ap, msg);
+ logit(GGLOG_FATAL, "fatal: ", msg, ap);
+ va_end(ap);
- exit(1);
+ exit(1);
}
/* XXX mpsafe */
static void
logit(int level, const char *prefix, const char *msg, va_list ap)
{
- time_t clock;
-
- if (level <= _loglevel) {
- time(&clock);
- fprintf(_logfile, "%d ", (int)clock);
- vfprintf(_logfile, prefix, ap);
- vfprintf(_logfile, msg, ap);
- fprintf(_logfile, "\n");
- fflush(_logfile);
- }
+ time_t clock;
+
+ if (level <= _loglevel) {
+ time(&clock);
+ fprintf(_logfile, "%d ", (int)clock);
+ vfprintf(_logfile, prefix, ap);
+ vfprintf(_logfile, msg, ap);
+ fprintf(_logfile, "\n");
+ fflush(_logfile);
+ }
}
/*
@@ -125,23 +125,23 @@ logit(int level, const char *prefix, const char *msg, va_list ap)
void *
xmalloc(size_t size)
{
- void *data;
+ void *data;
- data = malloc(size);
- if (!data)
- err(1, "could not malloc %d", (int)size);
- return data;
+ data = malloc(size);
+ if (!data)
+ err(1, "could not malloc %d", (int)size);
+ return data;
}
void *
xcalloc(size_t nmemb, size_t size)
{
- void *data;
+ void *data;
- data = calloc(nmemb, size);
- if (!data)
- err(1, "could not calloc %d", (int)size);
- return data;
+ data = calloc(nmemb, size);
+ if (!data)
+ err(1, "could not calloc %d", (int)size);
+ return data;
}
void
@@ -156,21 +156,21 @@ fd_nonblock(int fd)
void
addrcpy(struct sockaddr_in *dst, struct sockaddr_in *src)
{
- dst->sin_addr.s_addr = src->sin_addr.s_addr;
- dst->sin_port = src->sin_port;
- dst->sin_family = src->sin_family;
+ dst->sin_addr.s_addr = src->sin_addr.s_addr;
+ dst->sin_port = src->sin_port;
+ dst->sin_family = src->sin_family;
}
int
addrcmp(struct sockaddr_in *a, struct sockaddr_in *b)
{
- if (a->sin_addr.s_addr != b->sin_addr.s_addr)
- return -1;
- if (a->sin_port != b->sin_port)
- return -2;
- if (a->sin_family != b->sin_family)
- return -3;
- return 0;
+ if (a->sin_addr.s_addr != b->sin_addr.s_addr)
+ return -1;
+ if (a->sin_port != b->sin_port)
+ return -2;
+ if (a->sin_family != b->sin_family)
+ return -3;
+ return 0;
}
void
@@ -182,11 +182,11 @@ droppriv(char *user, int do_chroot, char *chroot_path)
if (!pw)
err(1, "unknown user %s", user);
if (do_chroot) {
- if (!chroot_path)
- chroot_path = pw->pw_dir;
- if (chroot(chroot_path) != 0)
- err(1, "unable to chroot");
- }
+ if (!chroot_path)
+ chroot_path = pw->pw_dir;
+ if (chroot(chroot_path) != 0)
+ err(1, "unable to chroot");
+ }
if (chdir("/") != 0)
err(1, "unable to chdir");
if (setgroups(1, &pw->pw_gid) == -1)