aboutsummaryrefslogtreecommitdiffstats
path: root/libglouglou/libggnet.c
blob: 69eee3f90c9c2e6589bfc154cdf1636adf4b3d40 (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
#include <stdlib.h>
#include <string.h>

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

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

  net = xcalloc(1, sizeof(struct ggnet));
  net->manage_connid = manage_connid;
	for (i=0; i<GGNET_CONN_FREEIDS_COUNT-1; i++)
		net->conn_freeids[i] = i;

  return net;
}

void
ggnet_free(struct ggnet *net)
{
  struct ggnet_conn *c, *ctmp;
  struct ggnet_node *n, *ntmp;

  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);
  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++;

	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");

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

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

	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_time_update(struct ggnet *net, time_t time)
{
  net->time = time;
}