-
Notifications
You must be signed in to change notification settings - Fork 14
/
udpsession.c
157 lines (124 loc) · 2.47 KB
/
udpsession.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
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include <time.h>
#include <unistd.h>
#include "obfstunnel.h"
#include "udpsession.h"
udp_session_t* udps_head = NULL;
udp_session_t* udps_search_byfd(int fd) {
udp_session_t* p;
for (p = udps_head; p != NULL; p = p->next) {
if (p->fd == fd) {
return p;
}
}
return NULL;
}
udp_session_t* udps_search_byladdr(struct sockaddr_in addr) {
udp_session_t* p;
for (p = udps_head; p != NULL; p = p->next) {
if (memcmp(&p->laddr, &addr, sizeof(p->laddr)) == 0) {
return p;
}
}
return NULL;
}
/**
* Add a new sesion
*
* @return udp_session_t* Pointer to the node new added
*/
udp_session_t* udps_add(udp_session_t sess) {
udp_session_t* n;
n = (udp_session_t*)malloc(sizeof(udp_session_t));
if (n == NULL) {
perror("malloc");
return NULL;
}
memcpy(n, &sess, sizeof(*n));
n->next = udps_head;
udps_head = n;
return n;
}
/**
* Delete an UDP session node
*
* @return int 0 for success, negative for fail
*/
/*
int udps_delete(udp_session_t* sess) {
udp_session_t* p;
if (sess == udps_head) {
free(sess);
udps_head = NULL;
}
else {
/// Search the prev node of *sess
for (p = udps_head; p->next != sess; p = p->next) {
if (p == NULL) {
OT_LOGD("No udp session %08X found\n", (unsigned int)sess);
return -1;
}
}
p->next = sess->next;
free(sess);
}
return 0;
}
*/
/// Search timed out UDP sessions and delete them
int udps_cleanup(int ttl) {
udp_session_t* p;
udp_session_t* prev;
time_t curtime;
int n = 0;
curtime = time(NULL);
p = udps_head;
prev = NULL;
while (p != NULL) {
if (curtime - p->atime > ttl) {
n++;
OT_LOGD("curtime - p->atime = %d - %d = %d, s->fd=%d, ttl=%d\n", (int)curtime, (int)p->atime, (int)(curtime - p->atime), p->fd, ttl);
// <!> Shutdown fds
shutdown(p->fd, SHUT_RDWR);
close(p->fd);
if (prev == NULL) {
udps_head = p->next;
free(p);
p = udps_head;
}
else {
prev->next = p->next;
free(p);
p = prev->next;
}
}
else {
prev = p;
p = p->next;
}
}
OT_LOGD("Deleted %d timeout nodes\n", n);
return 0;
}
/**
* Rebuild fdset corresponding to current UDP sessions
*
* @return int Max number of fd
*/
int udps_fdset(fd_set* fds) {
int n = -1;
udp_session_t* p;
if (fds == NULL) {
return -1;
}
FD_ZERO(fds);
for (p = udps_head; p != NULL; p = p->next) {
if (p->fd > n) {
n = p->fd;
}
FD_SET(p->fd, fds);
}
return n;
}