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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
|
/* tinyproxy - A fast light-weight HTTP proxy
* Copyright (C) 1998 Steven Young <sdyoung@miranda.org>
* Copyright (C) 1999, 2004 Robert James Kaes <rjkaes@users.sourceforge.net>
* Copyright (C) 2000 Chris Lightfoot <chris@ex-parrot.com>
*
* 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.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
/* Sockets are created and destroyed here. When a new connection comes in from
* a client, we need to copy the socket and the create a second socket to the
* remote server the client is trying to connect to. Also, the listening
* socket is created and destroyed here. Sounds more impressive than it
* actually is.
*/
#include "main.h"
#include <ares.h>
#include "log.h"
#include "heap.h"
#include "network.h"
#include "sock.h"
#include "text.h"
#include "conf.h"
#include "loop.h"
#include "sblist.h"
static struct htab *resolvers;
static ares_channel setup_resolver (const char *bind_to, const char *bind_to_alt)
{
htab_value *val = htab_find(resolvers, bind_to);
htab_value *val_alt = NULL;
htab_value tmp_val;
if (!bind_to)
return NULL;
if (bind_to_alt)
val_alt = htab_find(resolvers, bind_to_alt);
if (!val && !val_alt) {
/* struct ares_options options; */
ares_channel resolver = NULL;
/* memset(&options, 0, sizeof(options)); */
/* int res = ares_init_options(&resolver, options */
/* struct ares_options *options, */
/* int optmask) */
int res = ares_init(&resolver);
tmp_val = HTV_P(resolver);
} else if (val) {
tmp_val = *val;
} else {
tmp_val = *val_alt;
}
if (!val) {
htab_insert(resolvers, strdup(bind_to), tmp_val);
}
if (!val_alt && bind_to_alt) {
htab_insert(resolvers, strdup(bind_to_alt), tmp_val);
}
if (val && val_alt && val->p != val_alt->p) {
log_message(LOG_WARNING, "Different resolvers for %s and $s", bind_to, bind_to_alt);
}
return tmp_val.p;
}
/*
* Return a human readable error for getaddrinfo() and getnameinfo().
*/
static const char * get_gai_error (int n)
{
if (n == EAI_SYSTEM)
return strerror (errno);
else
return gai_strerror (n);
}
/*
* Bind the given socket to the supplied address. The socket is
* returned if the bind succeeded. Otherwise, -1 is returned
* to indicate an error.
*/
static int
bind_socket (int sockfd, const char *addr, int family)
{
struct addrinfo hints, *res, *ressave;
int n;
assert (sockfd >= 0);
assert (addr != NULL && strlen (addr) != 0);
memset (&hints, 0, sizeof (struct addrinfo));
hints.ai_family = family;
hints.ai_socktype = SOCK_STREAM;
/* The local port is not important */
n = getaddrinfo (addr, NULL, &hints, &res);
if (n != 0) {
log_message (LOG_INFO,
"bind_socket: getaddrinfo failed for %s: %s", addr, get_gai_error (n));
return -1;
}
ressave = res;
/* Loop through the addresses and try to bind to each */
do {
if (bind (sockfd, res->ai_addr, res->ai_addrlen) == 0)
break; /* success */
} while ((res = res->ai_next) != NULL);
freeaddrinfo (ressave);
if (res == NULL) /* was not able to bind to any address */
return -1;
return sockfd;
}
/**
* Try binding the given socket to supplied addresses, stopping when one succeeds.
*/
static int
bind_socket_list (int sockfd, sblist *addresses, int family)
{
size_t nb_addresses = sblist_getsize(addresses);
size_t i;
for (i = 0; i < nb_addresses; i++) {
const char *address = *(const char **)sblist_get(addresses, i);
if (bind_socket(sockfd, address, family) >= 0) {
log_message(LOG_INFO, "Bound to %s", address);
return 0;
}
}
return -1;
}
/*
* Open a connection to a remote host. It's been re-written to use
* the getaddrinfo() library function, which allows for a protocol
* independent implementation (mostly for IPv4 and IPv6 addresses.)
*/
int opensock (const char *host, int port, const char *bind_to, const char *bind_to_alt)
{
int sockfd, n;
struct ares_addrinfo *ressave;
struct ares_addrinfo_node *res;
struct ares_addrinfo_hints hints;
char portstr[6];
ares_channel resolver = NULL;
assert (host != NULL);
assert (port > 0);
log_message(LOG_INFO,
"opensock: opening connection to %s:%d", host, port);
if (bind_to) {
log_message(LOG_INFO,
"opensock: bind to %s or %s", bind_to, bind_to_alt);
}
if (bind_to) {
resolver = setup_resolver(bind_to, bind_to_alt);
}
if (!resolver) {
log_message (LOG_ERR, "opensock: no resolver for %s", bind_to);
}
memset (&hints, 0, sizeof (struct addrinfo));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
snprintf (portstr, sizeof (portstr), "%d", port);
n = ares_getaddrinfo (resolver, host, portstr, &hints, &ressave);
if (n != 0) {
log_message (LOG_ERR,
"opensock: Could not retrieve address info for %s:%d: %s", host, port, get_gai_error (n));
return -1;
}
log_message(LOG_INFO,
"opensock: getaddrinfo returned for %s:%d", host, port);
res = ressave->nodes;
do {
sockfd =
socket (res->ai_family, res->ai_socktype, res->ai_protocol);
if (sockfd < 0)
continue; /* ignore this one */
/* Bind to the specified address */
if (bind_to) {
if (bind_socket (sockfd, bind_to,
res->ai_family) < 0 &&
(!bind_to_alt ||
bind_socket (sockfd, bind_to_alt,
res->ai_family) < 0)) {
close (sockfd);
continue; /* can't bind, so try again */
}
} else if (config->bind_addrs) {
if (bind_socket_list (sockfd, config->bind_addrs,
res->ai_family) < 0) {
close (sockfd);
continue; /* can't bind, so try again */
}
}
if (connect (sockfd, res->ai_addr, res->ai_addrlen) == 0) {
union sockaddr_union *p = (void*) res->ai_addr, u;
int af = res->ai_addr->sa_family;
unsigned dport = ntohs(af == AF_INET ? p->v4.sin_port : p->v6.sin6_port);
socklen_t slen = sizeof u;
if (dport == config->port) {
getsockname(sockfd, (void*)&u, &slen);
loop_records_add(&u);
}
break; /* success */
}
close (sockfd);
} while ((res = res->ai_next) != NULL);
ares_freeaddrinfo (ressave);
if (res == NULL) {
log_message (LOG_ERR,
"opensock: Could not establish a connection to %s:%d",
host,
port);
return -1;
}
return sockfd;
}
/*
* Set the socket to non blocking -rjkaes
*/
int socket_nonblocking (int sock)
{
int flags;
assert (sock >= 0);
flags = fcntl (sock, F_GETFL, 0);
return fcntl (sock, F_SETFL, flags | O_NONBLOCK);
}
/*
* Set the socket to blocking -rjkaes
*/
int socket_blocking (int sock)
{
int flags;
assert (sock >= 0);
flags = fcntl (sock, F_GETFL, 0);
return fcntl (sock, F_SETFL, flags & ~O_NONBLOCK);
}
/**
* Try to listen on one socket based on the addrinfo
* as returned from getaddrinfo.
*
* Return the file descriptor upon success, -1 upon error.
*/
static int listen_on_one_socket(struct addrinfo *ad)
{
int listenfd;
int ret;
const int on = 1;
char numerichost[NI_MAXHOST];
int flags = NI_NUMERICHOST;
ret = getnameinfo(ad->ai_addr, ad->ai_addrlen,
numerichost, NI_MAXHOST, NULL, 0, flags);
if (ret != 0) {
log_message(LOG_ERR, "getnameinfo failed: %s", get_gai_error (ret));
return -1;
}
log_message(LOG_INFO, "trying to listen on host[%s], family[%d], "
"socktype[%d], proto[%d]", numerichost,
ad->ai_family, ad->ai_socktype, ad->ai_protocol);
listenfd = socket(ad->ai_family, ad->ai_socktype, ad->ai_protocol);
if (listenfd == -1) {
log_message(LOG_ERR, "socket() failed: %s", strerror(errno));
return -1;
}
ret = setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
if (ret != 0) {
log_message(LOG_ERR,
"setsockopt failed to set SO_REUSEADDR: %s",
strerror(errno));
close(listenfd);
return -1;
}
if (ad->ai_family == AF_INET6) {
ret = setsockopt(listenfd, IPPROTO_IPV6, IPV6_V6ONLY, &on,
sizeof(on));
if (ret != 0) {
log_message(LOG_ERR,
"setsockopt failed to set IPV6_V6ONLY: %s",
strerror(errno));
close(listenfd);
return -1;
}
}
ret = bind(listenfd, ad->ai_addr, ad->ai_addrlen);
if (ret != 0) {
log_message(LOG_ERR, "bind failed: %s", strerror (errno));
close(listenfd);
return -1;
}
ret = listen(listenfd, MAXLISTEN);
if (ret != 0) {
log_message(LOG_ERR, "listen failed: %s", strerror(errno));
close(listenfd);
return -1;
}
log_message(LOG_INFO, "listening on fd [%d]", listenfd);
return listenfd;
}
/*
* Start listening on a socket. Create a socket with the selected port.
* If the provided address is NULL, we may listen on multiple sockets,
* e.g. the wildcard addresse for IPv4 and IPv6, depending on what is
* supported. If the address is not NULL, we only listen on the first
* address reported by getaddrinfo that works.
*
* Upon success, the listen-fds are added to the listen_fds list
* and 0 is returned. Upon error, -1 is returned.
*/
int listen_sock (const char *addr, uint16_t port, sblist* listen_fds)
{
struct addrinfo hints, *result, *rp;
char portstr[6];
int ret = -1;
int n;
assert (port > 0);
assert (listen_fds != NULL);
log_message(LOG_INFO, "listen_sock called with addr = '%s'",
addr == NULL ? "(NULL)" : addr);
memset (&hints, 0, sizeof (struct addrinfo));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
snprintf (portstr, sizeof (portstr), "%d", port);
n = getaddrinfo (addr, portstr, &hints, &result);
if (n != 0) {
log_message (LOG_ERR,
"Unable to getaddrinfo() for %s:%d because of %s",
addr,
port,
get_gai_error (n));
return -1;
}
for (rp = result; rp != NULL; rp = rp->ai_next) {
int listenfd;
listenfd = listen_on_one_socket(rp);
if (listenfd == -1) {
continue;
}
sblist_add (listen_fds, &listenfd);
/* success */
ret = 0;
if (addr != NULL) {
/*
* Unless wildcard is requested, only listen
* on the first result that works.
*/
break;
}
}
if (ret != 0) {
log_message (LOG_ERR, "Unable to listen on any address.");
}
freeaddrinfo (result);
return ret;
}
/*
* Takes a socket descriptor and returns the socket's IP address.
*/
int getsock_ip (int fd, char *ipaddr)
{
struct sockaddr_storage name;
socklen_t namelen = sizeof (name);
assert (fd >= 0);
if (getsockname (fd, (struct sockaddr *) &name, &namelen) != 0) {
log_message (LOG_ERR, "getsock_ip: getsockname() error: %s",
strerror (errno));
return -1;
}
if (get_ip_string ((struct sockaddr *) &name, ipaddr, IP_LENGTH) ==
NULL)
return -1;
return 0;
}
int getmapped_ipv4 (const char *ipv6mapped_conf, const char *ipaddr, char *ipaddr_mapped)
{
struct in6_addr addr6;
struct in_addr addr;
memset(&addr6, 0, sizeof(addr6));
if ( inet_pton(AF_INET6, ipaddr, &addr6) < 0 )
return -1;
memcpy(&addr, addr6.s6_addr + 12, sizeof(addr));
return inet_ntop(AF_INET, &addr, ipaddr_mapped, IP_LENGTH) == NULL ? -1 : 0;
}
int getmapped_ipv6 (const char *ipv4mapped_conf, const char *ipaddr, char *ipaddr_mapped)
{
memset(ipaddr_mapped, 0, IP_LENGTH);
strncpy(ipaddr_mapped, ipv4mapped_conf, IP_LENGTH-1);
strncat(ipaddr_mapped, ipaddr, IP_LENGTH-1-strlen(ipaddr_mapped));
return 0;
}
/*
* Return the peer's socket information.
*/
void getpeer_information (union sockaddr_union* addr, char *ipaddr, size_t ipaddr_len)
{
int af = addr->v4.sin_family;
void *ipdata = af == AF_INET ? (void*)&addr->v4.sin_addr : (void*)&addr->v6.sin6_addr;
inet_ntop(af, ipdata, ipaddr, ipaddr_len);
}
int init_sock (void)
{
if (ares_library_init(ARES_LIB_INIT_ALL))
return -1;
resolvers = htab_create(10);
}
void cleanup_sock (void)
{
htab_destroy(resolvers);
ares_library_cleanup();
}
|