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

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

struct ggnet *
ggnet_new(void)
{
  struct ggnet *net;

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

struct ggnet_conn *
ggnet_conn_add(struct ggnet *net, struct in_addr *src, int src_port,
               struct in_addr *dst, int dst_port, int proto, int size)
{
	struct 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->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++;

	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->lastseen = net->time;
	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->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;
}

void
ggnet_time_update(struct ggnet *net, time_t time)
{
  net->time = time;
}