forked from cooldavid/tsps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
keepalive.c
222 lines (197 loc) · 5.25 KB
/
keepalive.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
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
/*
* TSP Server
*
* A TSP Server implementation that follows RFC5572 as much as possible.
* It is designed to be compatible with FreeNET6 service.
*
* Copyright (C) 2011 Guo-Fu Tseng <cooldavid@cooldavid.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "tsps.h"
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
enum {
HASH_SIZE = 1024,
};
static struct keepalive_info *kahash[HASH_SIZE];
static pthread_mutex_t lock_keepalive = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t lock_hook = PTHREAD_MUTEX_INITIALIZER;
static int hashkey(time_t seed)
{
return seed % HASH_SIZE;
}
static void insert_hash(struct keepalive_info *newkai)
{
int key = hashkey(newkai->expire);
struct keepalive_info *kai;
pthread_mutex_lock(&lock_keepalive);
kai = kahash[key];
if (!kai) {
kahash[key] = newkai;
} else {
while (kai->next)
kai = kai->next;
kai->next = newkai;
newkai->priv = kai;
}
pthread_mutex_unlock(&lock_keepalive);
}
void insert_keepalive(struct client_session *session)
{
struct keepalive_info *kai;
kai = calloc(1, sizeof(struct keepalive_info));
if (!kai) {
tspslog(LOG_ERR, "Create keepalive info: Out of memory");
exit(EXIT_FAILURE);
}
time(&session->lastrcv);
kai->expire = time(NULL) + session->keepalive;
kai->session = session;
session->kai = kai;
build_icmp6(kai->kapkt, &kai->chksum, &session->v6addr);
insert_hash(kai);
dbg_keepalive("Hooked keepalive info for session: %s:%u (%p)",
inet_ntoa(session->v4addr),
session->v4port,
session->kai);
}
void remove_keepalive(struct client_session *session)
{
pthread_mutex_lock(&lock_hook);
if (session->kai)
session->kai->session = NULL;
dbg_keepalive("Unhook keepalive info for killed session: %s:%u (%p)",
inet_ntoa(session->v4addr),
session->v4port,
session->kai);
pthread_mutex_unlock(&lock_hook);
}
static void unhash_keepalive(struct keepalive_info *kai, int key)
{
if (kai->priv) {
kai->priv->next = kai->next;
if (kai->next)
kai->next->priv = kai->priv;
} else {
kahash[key] = kai->next;
if (kai->next)
kai->next->priv = NULL;
}
kai->priv = NULL;
kai->next = NULL;
}
static struct keepalive_info *pop_keepalive(time_t exp)
{
int key = hashkey(exp);
struct keepalive_info *kai;
pthread_mutex_lock(&lock_keepalive);
kai = kahash[key];
while (kai) {
if (kai->expire <= exp) {
unhash_keepalive(kai, key);
pthread_mutex_unlock(&lock_keepalive);
return kai;
}
kai = kai->next;
}
pthread_mutex_unlock(&lock_keepalive);
return kai;
}
static void _do_keepalive(time_t expire)
{
struct keepalive_info *kai;
struct client_session *session;
time_t last;
struct in_addr ip;
in_port_t port;
uint32_t keepalive;
while ((kai = pop_keepalive(expire)) != NULL) {
pthread_mutex_lock(&lock_hook);
/*
* Check if session is still exist
*/
session = get_session(kai->session);
if (!session) {
dbg_keepalive("Free keepalive info for killed session: %p",
kai);
free(kai);
pthread_mutex_unlock(&lock_hook);
continue;
}
/*
* Skip keepalive if recent activity is within
* keepalive window
*/
last = (session->lastrcv < session->lastsnd) ?
session->lastrcv :
session->lastsnd;
if ((last + session->keepalive) > expire) {
kai->expire = last + session->keepalive;
insert_hash(kai);
dbg_keepalive("Skip keepalive for active channel: "
"scheduled %u seconds later (%p)",
kai->expire - time(NULL), kai);
put_session(session);
pthread_mutex_unlock(&lock_hook);
continue;
}
/*
* Remove session if no action after 5 * keepalive
*/
last = (session->lastrcv > session->lastsnd) ?
session->lastrcv :
session->lastsnd;
if ((last + (session->keepalive * 5)) < time(NULL)) {
dbg_keepalive("Timeout inactive channel: %s:%u (%p)",
inet_ntoa(session->v4addr),
session->v4port, kai);
timeout_session(session);
free(kai);
pthread_mutex_unlock(&lock_hook);
continue;
}
/*
* Copy IP/PORT from session before exiting hook lock
*/
ip = session->v4addr;
port = session->v4port;
keepalive = session->keepalive;
put_session(session);
pthread_mutex_unlock(&lock_hook);
/*
* Send ICMPv6 ping over UDPv4 socket
*/
dbg_keepalive("Sending keepalive at %u (%p)",
time(NULL), kai);
socket_ping(&ip, port, kai->kapkt, kai->chksum);
kai->expire = time(NULL) + keepalive;
insert_hash(kai);
dbg_keepalive("Keepalive scheduled %u seconds later (%p)",
kai->expire - time(NULL), kai);
}
}
void do_keepalive(void)
{
time_t expire;
for (expire = time(NULL) - (SLEEP_GAP * 2);
expire <= time(NULL);
++expire) {
_do_keepalive(expire);
}
}