aboutsummaryrefslogtreecommitdiffstats
path: root/libglouglou/libggnet.c
blob: cfed99e66976799b2eac759bd010eb1bf27685b1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
#include <stdlib.h>
#include <string.h>

#include "libglouglou.h"
#include "libggnet.h"

static struct ggnet_nodegroup *nodegroup_add(struct ggnet *,
                                             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);
static void nodegroup_set(struct ggnet *, struct ggnet_node *);
static void nodegroup_unset(struct ggnet *, struct ggnet_node *);

struct ggnet *
ggnet_new(int manage_connid)
{
  struct ggnet *net;
  int i;

  net = xcalloc(1, sizeof(struct ggnet));
  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;
}

/**
 * Sets grouping option
 *
 * Note: Must be set before any use of ggnet
 */
void
ggnet_grouping_set(struct ggnet *net, int set,
                   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;
}

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_node *
ggnet_node_add(struct ggnet *net, struct in_addr *addr)
{
	struct ggnet_node *n;

	gg_log_debug("ggnet_node_add");

	n = xcalloc(1, sizeof(struct ggnet_node));
	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);

	return n;
}

void
ggnet_node_del(struct ggnet *net, struct ggnet_node *n)
{
	if (n->used)
		gg_log_fatal("ggnet_node_del: trying to remove a used node !");
	gg_log_debug("ggnet_node_del: ggnet_node_del");

	if (net->use_grouping)
    nodegroup_unset(net, n);

	LIST_REMOVE(n, entry);
	free(n);
	net->node_count--;
}

struct ggnet_node *
ggnet_node_find(struct ggnet *net, struct in_addr *remote)
{
	struct ggnet_node *n;

	LIST_FOREACH(n, &net->node_list, entry)
		if (n->addr.s_addr == remote->s_addr)
			return n;
	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)
{
  return n->usrdata;
}

void
ggnet_node_usrdata_set(struct ggnet_node *n, void *usrdata)
{
  n->usrdata = usrdata;
}

struct ggnet_nodegroup *
ggnet_node_group_get(struct ggnet_node *n)
{
  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 ggnet_conn *c;
	struct ggnet_node *srcnode;
	struct ggnet_node *dstnode;
	int id;

	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->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)
		srcnode = ggnet_node_add(net, src);
	srcnode->used++;
	dstnode = ggnet_node_find(net, dst);
	if (!dstnode)
		dstnode = ggnet_node_add(net, dst);
	dstnode->used++;

	c = xmalloc(sizeof(struct ggnet_conn));
	c->id = id;
	c->state = CONNSTATE_ESTABLISHED;
	c->src = srcnode;
	c->src_port = src_port;
	c->dst = dstnode;
	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;
}

void
ggnet_conn_data(struct ggnet *net, struct ggnet_conn *c, int size, int response)
{
	gg_log_debug("ggnet_conn_data");

  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;
}

void
ggnet_conn_del(struct ggnet *net, struct ggnet_conn *c)
{
	gg_log_debug("ggnet_conn_del");

  /* 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)
      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--;

	LIST_REMOVE(c, entry);
	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)
{
  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) {
			if (c->src->addr.s_addr == src->s_addr)
				*response = 0;
			else
				*response = 1;
			return c;
		}
	}
	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_nodegroup_usrdata_get(struct ggnet_nodegroup *g)
{
  return g->usrdata;
}

void
ggnet_nodegroup_usrdata_set(struct ggnet_nodegroup *g, void *usrdata)
{
  g->usrdata = usrdata;
}

void
ggnet_time_update(struct ggnet *net, time_t 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 *group;

  group = xcalloc(1, sizeof(struct ggnet_nodegroup));
  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)
{
  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 *
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;
}

static void
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++;
}

static void
nodegroup_unset(struct ggnet *net, struct ggnet_node *n)
{
  if (!n->group)
    return;

  n->group->node_count--;
  if (n->group->node_count == 0)
    nodegroup_del(net, n->group);
}