forked from guilhem/sslh
/
ip-map.c
304 lines (276 loc) · 5.51 KB
/
ip-map.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
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/*
# ip-map.c: Code for port <-> ip association map
#
# Copyright (C) 2013 Romain Labolle
#
# 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 2 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.
#
# The full text for the General Public License is here:
# http://www.gnu.org/licenses/gpl.html
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/sem.h>
#include "ip-map.h"
typedef struct map {
uint16_t port;
uint32_t ip;
int next;
} map_t;
int ip_map_id = -1;
int semid = -1;
int new_shared_map()
{
int id = shmget(IPC_PRIVATE, sizeof(map_t), IPC_CREAT | IPC_EXCL | 0600);
CHECK_RES_DIE(id, "shmget");
map_t *m = shmat(id, NULL, 0);
m->ip = 0;
m->port = 0;
m->next = -1;
shmdt(m);
return id;
}
void ip_map_init()
{
ip_map_id = new_shared_map();
semid = semget(IPC_PRIVATE, 1, IPC_CREAT | IPC_EXCL | 0666);
CHECK_RES_DIE(semid, "semget");
int res = semctl(semid, 0, SETVAL, 1);
CHECK_RES_DIE(res, "semctl");
if (verbose) fprintf(stderr, "Port<->IP map initialized.\n");
}
int sem_op(int op)
{
struct sembuf sb;
sb.sem_num = 0;
sb.sem_op = op;
sb.sem_flg = SEM_UNDO;
if(semid == -1)
return semid;
return semop(semid, &sb, 1);
}
int sem_lock()
{
return sem_op(-1);
}
int sem_unlock()
{
return sem_op(1);
}
void ip_map_close()
{
int next = ip_map_id;
int delid;
map_t *m;
sem_lock();
while(next >= 0)
{
m = shmat(next, NULL, 0);
delid = next;
next = m->next;
shmdt(m);
shmctl(delid, IPC_RMID, NULL);
}
sem_unlock();
semctl(semid, 0, IPC_RMID);
if (verbose) fprintf(stderr, "Port<->IP map closed.\n");
}
uint32_t get_ip(const uint16_t port)
{
int next = ip_map_id;
uint32_t ip;
map_t *m;
if(sem_lock() == -1)
return 0;
while(next >= 0)
{
m = shmat(next, NULL, 0);
if(m->port > port)
{
shmdt(m);
sem_unlock();
return 0;
}
if(m->port == port)
{
if (verbose) fprintf(stderr, "got %u->%u from ip map\n", port, m->ip);
ip = m->ip;
shmdt(m);
sem_unlock();
return ip;
}
next = m->next;
shmdt(m);
}
sem_unlock();
return 0;
}
void add_ip(uint16_t port, uint32_t ip)
{
int next = ip_map_id;
int prev = -1;
int n;
map_t *m;
if(ip == 0xFFFFFFFF || sem_lock() == -1)
return;
while(next >= 0)
{
m = shmat(next, NULL, 0);
if(m->port > port)
{
shmdt(m);
break;
}
if(m->port == port)
{
m->ip = ip;
if (verbose) fprintf(stderr, "updated %u->%u to ip map\n", port, ip);
shmdt(m);
sem_unlock();
return;
}
prev = next;
next = m->next;
shmdt(m);
}
n = new_shared_map();
m = shmat(n, NULL, 0);
m->port = port;
m->ip = ip;
m->next = next;
shmdt(m);
m = shmat(prev, NULL, 0);
m->next = n;
shmdt(m);
sem_unlock();
if (verbose) fprintf(stderr, "added %u->%u to ip map\n", port, ip);
}
void remove_ip(uint16_t port)
{
int next = ip_map_id;
int delid;
int prev = -1;
map_t *m;
if(sem_lock() == -1)
return;
while(next >= 0)
{
m = shmat(next, NULL, 0);
if(m->port > port)
{
shmdt(m);
sem_unlock();
return;
}
if(m->port == port)
{
if(prev) {
delid = next;
next = m->next;
shmdt(m);
shmctl(delid, IPC_RMID, NULL);
m = shmat(prev, NULL, 0);
m->next = next;
}
shmdt(m);
sem_unlock();
if (verbose) fprintf(stderr, "removed %u from ip map\n", port);
return;
}
prev = next;
next = m->next;
shmdt(m);
}
sem_unlock();
}
uint32_t fd2ip(int fd)
{
socklen_t len;
struct sockaddr_storage addr;
len = sizeof addr;
getpeername(fd, (struct sockaddr*)&addr, &len);
return ntohl(((struct sockaddr_in *)&addr)->sin_addr.s_addr);
}
uint16_t fd2port(int fd)
{
socklen_t len;
struct sockaddr_storage addr;
len = sizeof addr;
getsockname(fd, (struct sockaddr*)&addr, &len);
return ntohs(((struct sockaddr_in *)&addr)->sin_port);
}
void add_ip_fd(int portfd, int ipfd)
{
add_ip(fd2port(portfd), fd2ip(ipfd));
}
void remove_ip_fd(int portfd)
{
remove_ip(fd2port(portfd));
}
int handle_connection(struct map_queue *q)
{
char *buf = (char*)&q->port;
ssize_t size_r, size_w;
while(q->size_r < sizeof(q->port))
{
size_r = read(q->fd, buf + q->size_r, sizeof(q->port) - q->size_r);
if (size_r == -1) {
switch (errno) {
case EAGAIN:
return FD_NODATA;
case ECONNRESET:
case EPIPE:
return FD_CNXCLOSED;
}
}
CHECK_RES_RETURN(size_r, "read");
if (size_r == 0)
return FD_CNXCLOSED;
q->size_r += size_r;
}
if (verbose) fprintf(stderr, "request fd %d: %d\n", q->fd, ntohs(q->port));
if(q->ip == 0xFFFFFFFF)
q->ip = htonl(get_ip(ntohs(q->port)));
if (verbose) fprintf(stderr, "got ip %u associated with port %u\n", ntohl(q->ip), ntohs(q->port));
buf = (char*)&q->ip;
while(q->size_w < sizeof(q->ip))
{
size_w = write(q->fd, buf + q->size_w, sizeof(q->ip) - q->size_w);
if (size_w == -1) {
switch (errno) {
case EAGAIN:
return FD_STALLED;
case ECONNRESET:
case EPIPE:
return FD_CNXCLOSED;
}
}
CHECK_RES_RETURN(size_w, "write");
q->size_w += size_w;
}
q->size_r = 0;
q->size_w = 0;
q->ip = 0xFFFFFFFF;
return 1;
}
struct map_queue new_map_queue(int fd)
{
struct map_queue q;
memset(&q, 0, sizeof(struct map_queue));
q.ip = 0xFFFFFFFF;
q.fd = fd;
return q;
}