-
Notifications
You must be signed in to change notification settings - Fork 13
/
connlist.c
174 lines (160 loc) · 4.23 KB
/
connlist.c
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
#include "connlist.h"
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include "misc.h"
conn_entry_t* conn_table = NULL;
static unsigned count = 0;
/**
* insert an entry to the connection table. It will allocate new memory
* on the heap, insert it into the linked list and return a pointer
* to the newly created entry.
*
* @return pointer to the entry in the list on the heap
*/
conn_entry_t* conn_table_insert(void) {
conn_entry_t* e = malloc(sizeof(conn_entry_t));
memset(e, 0, sizeof(conn_entry_t));
e->next = conn_table;
if (e->next != NULL) {
e->next->prev = e;
}
e->prev = NULL;
conn_table = e;
++count;
return e;
}
/**
* remove an entry from the connection table.
* The entry will also be freed from the heap.
*
* @param entry pointer to the entry to be removed
*/
void conn_table_remove(conn_entry_t* entry) {
if (entry->prev != NULL) {
entry->prev->next = entry->next;
} else {
conn_table = entry->next;
}
if (entry->next != NULL) {
entry->next->prev = entry->prev;
}
if (entry->sock_service > 0) {
close(entry->sock_service);
}
if (entry->sock_tunnel > 0) {
close(entry->sock_tunnel);
}
free(entry);
--count;
}
/**
* find the connection table entry by its client address. It will compare
* the entire sockaddr struct (port and address) to identify the
* entry. If not found it will return NULL.
*
* @param addr pointer to sockaddr struct
* @return pointer to connection table entry or NULL
*/
conn_entry_t* conn_table_find_client_address(struct sockaddr_in* addr) {
conn_entry_t* p = conn_table;
while (p != NULL) {
if (memcmp(&p->addr_client, addr, sizeof(struct sockaddr_in)) == 0) {
return p;
}
p = p->next;
}
return NULL;
}
/**
* find the connection table entry by its tunnel address. It will compare
* the entire sockaddr struct (port and address) to identify the
* entry. If not found it will return NULL.
*
* @param addr pointer to sockaddr struct
* @return pointer to connection table entry or NULL
*/
conn_entry_t* conn_table_find_tunnel_address(struct sockaddr_in* addr) {
conn_entry_t* p = conn_table;
while (p != NULL) {
if (memcmp(&p->addr_tunnel, addr, sizeof(struct sockaddr_in)) == 0) {
return p;
}
p = p->next;
}
return NULL;
}
/**
* return the next best table entry that has the spare flag set,
* return NULL if no such entry exists.
*/
conn_entry_t* conn_table_find_next_spare(void) {
conn_entry_t* p = conn_table;
while (p != NULL) {
if (p->spare) {
return p;
}
p = p->next;
}
return NULL;
}
/**
* check all connection table entries for their last usage time and remove
* all entries that have been inactive for longer than the defined lifetime.
* This function is meant to be called periodically every few seconds.
*
* @param max_age inactivity time in seconds
* @param clean_spares should spare entries also be cleaned
*/
void conn_table_clean(unsigned max_age, bool clean_spares) {
conn_entry_t* e = conn_table;
bool changed = false;
uint64_t time = millisec();
while (e != NULL) {
conn_entry_t* next = e->next;
if ((time - e->last_acticity > max_age * 1000) && (clean_spares || !e->spare)) {
print(LOG_DEBUG, "removing connection");
conn_table_remove(e);
changed = true;
}
e = next;
}
if (changed) {
conn_print_numbers();
}
}
unsigned conn_count() {
return count;
}
unsigned conn_spare_count() {
conn_entry_t* e = conn_table;
unsigned cnt = 0;
while (e) {
if (e->spare) {
++cnt;
}
e = e->next;
}
return cnt;
}
unsigned conn_socket_count() {
conn_entry_t* e = conn_table;
unsigned cnt = 0;
while (e) {
if (e->sock_service) {
++cnt;
}
if (e->sock_tunnel) {
++cnt;
}
e = e->next;
}
return cnt;
}
void conn_print_numbers() {
unsigned spare = conn_spare_count();
print(LOG_DEBUG, "Total: %d, active: %d, spare: %d", count, count - spare, spare);
}