1 /*
2     net_socket.c -- Handle various kinds of sockets.
3     Copyright (C) 1998-2005 Ivo Timmermans,
4                   2000-2017 Guus Sliepen <guus@tinc-vpn.org>
5                   2006      Scott Lamb <slamb@slamb.org>
6                   2009      Florian Forster <octo@verplant.org>
7 
8     This program is free software; you can redistribute it and/or modify
9     it under the terms of the GNU General Public License as published by
10     the Free Software Foundation; either version 2 of the License, or
11     (at your option) any later version.
12 
13     This program is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16     GNU General Public License for more details.
17 
18     You should have received a copy of the GNU General Public License along
19     with this program; if not, write to the Free Software Foundation, Inc.,
20     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22 
23 #include "system.h"
24 
25 #include "avl_tree.h"
26 #include "conf.h"
27 #include "connection.h"
28 #include "event.h"
29 #include "logger.h"
30 #include "meta.h"
31 #include "net.h"
32 #include "netutl.h"
33 #include "protocol.h"
34 #include "proxy.h"
35 #include "utils.h"
36 #include "xalloc.h"
37 
38 /* Needed on Mac OS/X */
39 #ifndef SOL_TCP
40 #define SOL_TCP IPPROTO_TCP
41 #endif
42 
43 int addressfamily = AF_UNSPEC;
44 int mintimeout = 0;
45 int maxtimeout = 900;
46 int seconds_till_retry = 5;
47 int udp_rcvbuf = 0;
48 int udp_sndbuf = 0;
49 
50 listen_socket_t listen_socket[MAXSOCKETS];
51 int listen_sockets;
52 list_t *outgoing_list = NULL;
53 
54 /* Setup sockets */
55 
configure_tcp(connection_t * c)56 static void configure_tcp(connection_t *c) {
57 	int option;
58 
59 #ifdef O_NONBLOCK
60 	int flags = fcntl(c->socket, F_GETFL);
61 
62 	if(fcntl(c->socket, F_SETFL, flags | O_NONBLOCK) < 0) {
63 		logger(LOG_ERR, "fcntl for %s: %s", c->hostname, strerror(errno));
64 	}
65 
66 #elif defined(WIN32)
67 	unsigned long arg = 1;
68 
69 	if(ioctlsocket(c->socket, FIONBIO, &arg) != 0) {
70 		logger(LOG_ERR, "ioctlsocket for %s: %s", c->hostname, sockstrerror(sockerrno));
71 	}
72 
73 #endif
74 
75 #if defined(SOL_TCP) && defined(TCP_NODELAY)
76 	option = 1;
77 	setsockopt(c->socket, SOL_TCP, TCP_NODELAY, (void *)&option, sizeof(option));
78 #endif
79 
80 #if defined(IP_TOS) && defined(IPTOS_LOWDELAY)
81 	option = IPTOS_LOWDELAY;
82 	setsockopt(c->socket, IPPROTO_IP, IP_TOS, (void *)&option, sizeof(option));
83 #endif
84 
85 #if defined(IPV6_TCLASS) && defined(IPTOS_LOWDELAY)
86 	option = IPTOS_LOWDELAY;
87 	setsockopt(c->socket, IPPROTO_IPV6, IPV6_TCLASS, (void *)&option, sizeof(option));
88 #endif
89 }
90 
bind_to_interface(int sd)91 static bool bind_to_interface(int sd) {
92 	char *iface;
93 
94 #if defined(SOL_SOCKET) && defined(SO_BINDTODEVICE)
95 	struct ifreq ifr;
96 	int status;
97 #endif /* defined(SOL_SOCKET) && defined(SO_BINDTODEVICE) */
98 
99 	if(!get_config_string(lookup_config(config_tree, "BindToInterface"), &iface)) {
100 		return true;
101 	}
102 
103 #if defined(SOL_SOCKET) && defined(SO_BINDTODEVICE)
104 	memset(&ifr, 0, sizeof(ifr));
105 	strncpy(ifr.ifr_ifrn.ifrn_name, iface, IFNAMSIZ);
106 	ifr.ifr_ifrn.ifrn_name[IFNAMSIZ - 1] = 0;
107 	free(iface);
108 
109 	status = setsockopt(sd, SOL_SOCKET, SO_BINDTODEVICE, (void *)&ifr, sizeof(ifr));
110 
111 	if(status) {
112 		logger(LOG_ERR, "Can't bind to interface %s: %s", ifr.ifr_ifrn.ifrn_name, strerror(errno));
113 		return false;
114 	}
115 
116 #else /* if !defined(SOL_SOCKET) || !defined(SO_BINDTODEVICE) */
117 	logger(LOG_WARNING, "%s not supported on this platform", "BindToInterface");
118 #endif
119 
120 	return true;
121 }
122 
setup_listen_socket(const sockaddr_t * sa)123 int setup_listen_socket(const sockaddr_t *sa) {
124 	int nfd;
125 	char *addrstr;
126 	int option;
127 	char *iface;
128 
129 	nfd = socket(sa->sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
130 
131 	if(nfd < 0) {
132 		ifdebug(STATUS) logger(LOG_ERR, "Creating metasocket failed: %s", sockstrerror(sockerrno));
133 		return -1;
134 	}
135 
136 #ifdef FD_CLOEXEC
137 	fcntl(nfd, F_SETFD, FD_CLOEXEC);
138 #endif
139 
140 	/* Optimize TCP settings */
141 
142 	option = 1;
143 	setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, (void *)&option, sizeof(option));
144 
145 #if defined(IPV6_V6ONLY)
146 
147 	if(sa->sa.sa_family == AF_INET6) {
148 		setsockopt(nfd, IPPROTO_IPV6, IPV6_V6ONLY, (void *)&option, sizeof(option));
149 	}
150 
151 #else
152 #warning IPV6_V6ONLY not defined
153 #endif
154 
155 	if(get_config_string(lookup_config(config_tree, "BindToInterface"), &iface)) {
156 #if defined(SOL_SOCKET) && defined(SO_BINDTODEVICE)
157 		struct ifreq ifr;
158 
159 		memset(&ifr, 0, sizeof(ifr));
160 		strncpy(ifr.ifr_ifrn.ifrn_name, iface, IFNAMSIZ);
161 		ifr.ifr_ifrn.ifrn_name[IFNAMSIZ - 1] = 0;
162 		free(iface);
163 
164 		if(setsockopt(nfd, SOL_SOCKET, SO_BINDTODEVICE, (void *)&ifr, sizeof(ifr))) {
165 			closesocket(nfd);
166 			logger(LOG_ERR, "Can't bind to interface %s: %s", ifr.ifr_ifrn.ifrn_name, strerror(sockerrno));
167 			return -1;
168 		}
169 
170 #else
171 		logger(LOG_WARNING, "%s not supported on this platform", "BindToInterface");
172 #endif
173 	}
174 
175 	if(bind(nfd, &sa->sa, SALEN(sa->sa))) {
176 		closesocket(nfd);
177 		addrstr = sockaddr2hostname(sa);
178 		logger(LOG_ERR, "Can't bind to %s/tcp: %s", addrstr, sockstrerror(sockerrno));
179 		free(addrstr);
180 		return -1;
181 	}
182 
183 	if(listen(nfd, 3)) {
184 		closesocket(nfd);
185 		logger(LOG_ERR, "System call `%s' failed: %s", "listen", sockstrerror(sockerrno));
186 		return -1;
187 	}
188 
189 	return nfd;
190 }
191 
setup_vpn_in_socket(const sockaddr_t * sa)192 int setup_vpn_in_socket(const sockaddr_t *sa) {
193 	int nfd;
194 	char *addrstr;
195 	int option;
196 
197 	nfd = socket(sa->sa.sa_family, SOCK_DGRAM, IPPROTO_UDP);
198 
199 	if(nfd < 0) {
200 		logger(LOG_ERR, "Creating UDP socket failed: %s", sockstrerror(sockerrno));
201 		return -1;
202 	}
203 
204 #ifdef FD_CLOEXEC
205 	fcntl(nfd, F_SETFD, FD_CLOEXEC);
206 #endif
207 
208 #ifdef O_NONBLOCK
209 	{
210 		int flags = fcntl(nfd, F_GETFL);
211 
212 		if(fcntl(nfd, F_SETFL, flags | O_NONBLOCK) < 0) {
213 			closesocket(nfd);
214 			logger(LOG_ERR, "System call `%s' failed: %s", "fcntl",
215 			       strerror(errno));
216 			return -1;
217 		}
218 	}
219 #elif defined(WIN32)
220 	{
221 		unsigned long arg = 1;
222 
223 		if(ioctlsocket(nfd, FIONBIO, &arg) != 0) {
224 			closesocket(nfd);
225 			logger(LOG_ERR, "Call to `%s' failed: %s", "ioctlsocket", sockstrerror(sockerrno));
226 			return -1;
227 		}
228 	}
229 #endif
230 
231 	option = 1;
232 	setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, (void *)&option, sizeof(option));
233 	setsockopt(nfd, SOL_SOCKET, SO_BROADCAST, (void *)&option, sizeof(option));
234 
235 	if(udp_rcvbuf && setsockopt(nfd, SOL_SOCKET, SO_RCVBUF, (void *)&udp_rcvbuf, sizeof(udp_rcvbuf))) {
236 		logger(LOG_WARNING, "Can't set UDP SO_RCVBUF to %i: %s", udp_rcvbuf, strerror(errno));
237 	}
238 
239 	if(udp_sndbuf && setsockopt(nfd, SOL_SOCKET, SO_SNDBUF, (void *)&udp_sndbuf, sizeof(udp_sndbuf))) {
240 		logger(LOG_WARNING, "Can't set UDP SO_SNDBUF to %i: %s", udp_sndbuf, strerror(errno));
241 	}
242 
243 #if defined(IPV6_V6ONLY)
244 
245 	if(sa->sa.sa_family == AF_INET6) {
246 		setsockopt(nfd, IPPROTO_IPV6, IPV6_V6ONLY, (void *)&option, sizeof(option));
247 	}
248 
249 #endif
250 
251 #if defined(IP_DONTFRAG) && !defined(IP_DONTFRAGMENT)
252 #define IP_DONTFRAGMENT IP_DONTFRAG
253 #endif
254 
255 #if defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DO)
256 
257 	if(myself->options & OPTION_PMTU_DISCOVERY) {
258 		option = IP_PMTUDISC_DO;
259 		setsockopt(nfd, IPPROTO_IP, IP_MTU_DISCOVER, (void *)&option, sizeof(option));
260 	}
261 
262 #elif defined(IP_DONTFRAGMENT)
263 
264 	if(myself->options & OPTION_PMTU_DISCOVERY) {
265 		option = 1;
266 		setsockopt(nfd, IPPROTO_IP, IP_DONTFRAGMENT, (void *)&option, sizeof(option));
267 	}
268 
269 #endif
270 
271 #if defined(IPV6_MTU_DISCOVER) && defined(IPV6_PMTUDISC_DO)
272 
273 	if(myself->options & OPTION_PMTU_DISCOVERY) {
274 		option = IPV6_PMTUDISC_DO;
275 		setsockopt(nfd, IPPROTO_IPV6, IPV6_MTU_DISCOVER, (void *)&option, sizeof(option));
276 	}
277 
278 #elif defined(IPV6_DONTFRAG)
279 
280 	if(myself->options & OPTION_PMTU_DISCOVERY) {
281 		option = 1;
282 		setsockopt(nfd, IPPROTO_IPV6, IPV6_DONTFRAG, (void *)&option, sizeof(option));
283 	}
284 
285 #endif
286 
287 	if(!bind_to_interface(nfd)) {
288 		closesocket(nfd);
289 		return -1;
290 	}
291 
292 	if(bind(nfd, &sa->sa, SALEN(sa->sa))) {
293 		closesocket(nfd);
294 		addrstr = sockaddr2hostname(sa);
295 		logger(LOG_ERR, "Can't bind to %s/udp: %s", addrstr, sockstrerror(sockerrno));
296 		free(addrstr);
297 		return -1;
298 	}
299 
300 	return nfd;
301 } /* int setup_vpn_in_socket */
302 
retry_outgoing(outgoing_t * outgoing)303 void retry_outgoing(outgoing_t *outgoing) {
304 	outgoing->timeout += 5;
305 
306 	if(outgoing->timeout < mintimeout) {
307 		outgoing->timeout = mintimeout;
308 	}
309 
310 	if(outgoing->timeout > maxtimeout) {
311 		outgoing->timeout = maxtimeout;
312 	}
313 
314 	if(outgoing->event) {
315 		event_del(outgoing->event);
316 	}
317 
318 	outgoing->event = new_event();
319 	outgoing->event->handler = (event_handler_t) setup_outgoing_connection;
320 	outgoing->event->time = now + outgoing->timeout;
321 	outgoing->event->data = outgoing;
322 	event_add(outgoing->event);
323 
324 	ifdebug(CONNECTIONS) logger(LOG_NOTICE,
325 	                            "Trying to re-establish outgoing connection in %d seconds",
326 	                            outgoing->timeout);
327 }
328 
finish_connecting(connection_t * c)329 void finish_connecting(connection_t *c) {
330 	ifdebug(CONNECTIONS) logger(LOG_INFO, "Connected to %s (%s)", c->name, c->hostname);
331 
332 	c->last_ping_time = now;
333 
334 	send_id(c);
335 }
336 
do_outgoing_pipe(connection_t * c,char * command)337 static void do_outgoing_pipe(connection_t *c, char *command) {
338 #ifndef HAVE_MINGW
339 	int fd[2];
340 
341 	if(socketpair(AF_UNIX, SOCK_STREAM, 0, fd)) {
342 		logger(LOG_ERR, "Could not create socketpair: %s\n", strerror(errno));
343 		return;
344 	}
345 
346 	if(fork()) {
347 		c->socket = fd[0];
348 		close(fd[1]);
349 		ifdebug(CONNECTIONS) logger(LOG_DEBUG, "Using proxy %s", command);
350 		return;
351 	}
352 
353 	close(0);
354 	close(1);
355 	close(fd[0]);
356 	dup2(fd[1], 0);
357 	dup2(fd[1], 1);
358 	close(fd[1]);
359 
360 	// Other filedescriptors should be closed automatically by CLOEXEC
361 
362 	char *host = NULL;
363 	char *port = NULL;
364 
365 	sockaddr2str(&c->address, &host, &port);
366 	setenv("REMOTEADDRESS", host, true);
367 	setenv("REMOTEPORT", port, true);
368 	setenv("NODE", c->name, true);
369 	setenv("NAME", myself->name, true);
370 
371 	if(netname) {
372 		setenv("NETNAME", netname, true);
373 	}
374 
375 	int result = system(command);
376 
377 	if(result < 0) {
378 		logger(LOG_ERR, "Could not execute %s: %s\n", command, strerror(errno));
379 	} else if(result) {
380 		logger(LOG_ERR, "%s exited with non-zero status %d", command, result);
381 	}
382 
383 	exit(result);
384 #else
385 	logger(LOG_ERR, "Proxy type exec not supported on this platform!");
386 	return;
387 #endif
388 }
389 
is_valid_host_port(const char * host,const char * port)390 static bool is_valid_host_port(const char *host, const char *port) {
391 	for(const char *p = host; *p; p++)
392 		if(!isalnum(*p) && *p != '-' && *p != '.') {
393 			return false;
394 		}
395 
396 	for(const char *p = port; *p; p++)
397 		if(!isalnum(*p)) {
398 			return false;
399 		}
400 
401 	return true;
402 }
403 
do_outgoing_connection(connection_t * c)404 void do_outgoing_connection(connection_t *c) {
405 	struct addrinfo *proxyai = NULL;
406 	int result;
407 
408 	if(!c->outgoing) {
409 		logger(LOG_ERR, "do_outgoing_connection() for %s called without c->outgoing", c->name);
410 		abort();
411 	}
412 
413 begin:
414 
415 	if(!c->outgoing->ai) {
416 		if(!c->outgoing->cfg) {
417 			ifdebug(CONNECTIONS) logger(LOG_ERR, "Could not set up a meta connection to %s",
418 			                            c->name);
419 			c->status.remove = true;
420 			retry_outgoing(c->outgoing);
421 			c->outgoing = NULL;
422 			return;
423 		}
424 
425 		char *address, *port, *space;
426 
427 		get_config_string(c->outgoing->cfg, &address);
428 
429 		space = strchr(address, ' ');
430 
431 		if(space) {
432 			port = xstrdup(space + 1);
433 			*space = 0;
434 		} else {
435 			if(!get_config_string(lookup_config(c->config_tree, "Port"), &port)) {
436 				port = xstrdup("655");
437 			}
438 		}
439 
440 		c->outgoing->ai = str2addrinfo(address, port, SOCK_STREAM);
441 
442 		// If we cannot resolve the address, maybe we are using a proxy that can?
443 		if(!c->outgoing->ai && proxytype != PROXY_NONE && is_valid_host_port(address, port)) {
444 			memset(&c->address, 0, sizeof(c->address));
445 			c->address.sa.sa_family = AF_UNKNOWN;
446 			c->address.unknown.address = address;
447 			c->address.unknown.port = port;
448 		} else {
449 			free(address);
450 			free(port);
451 		}
452 
453 		c->outgoing->aip = c->outgoing->ai;
454 		c->outgoing->cfg = lookup_config_next(c->config_tree, c->outgoing->cfg);
455 
456 		if(!c->outgoing->ai && proxytype != PROXY_NONE) {
457 			goto connect;
458 		}
459 	}
460 
461 	if(!c->outgoing->aip) {
462 		if(c->outgoing->ai) {
463 			freeaddrinfo(c->outgoing->ai);
464 		}
465 
466 		c->outgoing->ai = NULL;
467 		goto begin;
468 	}
469 
470 	memcpy(&c->address, c->outgoing->aip->ai_addr, c->outgoing->aip->ai_addrlen);
471 	c->outgoing->aip = c->outgoing->aip->ai_next;
472 
473 connect:
474 
475 	if(c->hostname) {
476 		free(c->hostname);
477 	}
478 
479 	c->hostname = sockaddr2hostname(&c->address);
480 
481 	ifdebug(CONNECTIONS) logger(LOG_INFO, "Trying to connect to %s (%s)", c->name,
482 	                            c->hostname);
483 
484 	if(!proxytype) {
485 		c->socket = socket(c->address.sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
486 	} else if(proxytype == PROXY_EXEC) {
487 		c->status.proxy_passed = true;
488 		do_outgoing_pipe(c, proxyhost);
489 	} else {
490 		proxyai = str2addrinfo(proxyhost, proxyport, SOCK_STREAM);
491 
492 		if(!proxyai) {
493 			goto begin;
494 		}
495 
496 		ifdebug(CONNECTIONS) logger(LOG_INFO, "Using proxy at %s port %s", proxyhost, proxyport);
497 		c->socket = socket(proxyai->ai_family, SOCK_STREAM, IPPROTO_TCP);
498 	}
499 
500 	if(c->socket == -1) {
501 		ifdebug(CONNECTIONS) logger(LOG_ERR, "Creating socket for %s failed: %s", c->hostname, sockstrerror(sockerrno));
502 		goto begin;
503 	}
504 
505 	if(proxytype != PROXY_EXEC) {
506 		configure_tcp(c);
507 	}
508 
509 #ifdef FD_CLOEXEC
510 	fcntl(c->socket, F_SETFD, FD_CLOEXEC);
511 #endif
512 
513 	if(proxytype != PROXY_EXEC) {
514 #if defined(IPV6_V6ONLY)
515 		int option = 1;
516 
517 		if(c->address.sa.sa_family == AF_INET6) {
518 			setsockopt(c->socket, IPPROTO_IPV6, IPV6_V6ONLY, (void *)&option, sizeof(option));
519 		}
520 
521 #endif
522 
523 		bind_to_interface(c->socket);
524 
525 		int b = -1;
526 
527 		for(int i = 0; i < listen_sockets; i++) {
528 			if(listen_socket[i].sa.sa.sa_family == c->address.sa.sa_family) {
529 				if(b == -1) {
530 					b = i;
531 				} else  {
532 					b = -1;
533 					break;
534 				}
535 			}
536 		}
537 
538 		if(b != -1) {
539 			sockaddr_t sa = listen_socket[b].sa;
540 
541 			if(sa.sa.sa_family == AF_INET) {
542 				sa.in.sin_port = 0;
543 			} else if(sa.sa.sa_family == AF_INET6) {
544 				sa.in6.sin6_port = 0;
545 			}
546 
547 			if(bind(c->socket, &sa.sa, SALEN(sa.sa))) {
548 				char *addrstr = sockaddr2hostname(&sa);
549 				logger(LOG_ERR, "Can't bind to %s/tcp: %s", addrstr, sockstrerror(sockerrno));
550 				free(addrstr);
551 			}
552 		}
553 	}
554 
555 	/* Connect */
556 
557 	if(!proxytype) {
558 		result = connect(c->socket, &c->address.sa, SALEN(c->address.sa));
559 	} else if(proxytype == PROXY_EXEC) {
560 		result = 0;
561 	} else {
562 		result = connect(c->socket, proxyai->ai_addr, proxyai->ai_addrlen);
563 		freeaddrinfo(proxyai);
564 	}
565 
566 	now = time(NULL);
567 
568 	if(result == -1) {
569 		if(sockinprogress(sockerrno)) {
570 			c->last_ping_time = now;
571 			c->status.connecting = true;
572 			return;
573 		}
574 
575 		closesocket(c->socket);
576 
577 		ifdebug(CONNECTIONS) logger(LOG_ERR, "%s: %s", c->hostname, sockstrerror(sockerrno));
578 
579 		goto begin;
580 	}
581 
582 	finish_connecting(c);
583 
584 	return;
585 }
586 
setup_outgoing_connection(outgoing_t * outgoing)587 void setup_outgoing_connection(outgoing_t *outgoing) {
588 	connection_t *c;
589 	node_t *n;
590 
591 	outgoing->event = NULL;
592 
593 	n = lookup_node(outgoing->name);
594 
595 	if(n)
596 		if(n->connection) {
597 			ifdebug(CONNECTIONS) logger(LOG_INFO, "Already connected to %s", outgoing->name);
598 
599 			n->connection->outgoing = outgoing;
600 			return;
601 		}
602 
603 	c = new_connection();
604 	c->name = xstrdup(outgoing->name);
605 	c->outcipher = myself->connection->outcipher;
606 	c->outdigest = myself->connection->outdigest;
607 	c->outmaclength = myself->connection->outmaclength;
608 	c->outcompression = myself->connection->outcompression;
609 
610 	init_configuration(&c->config_tree);
611 
612 	if(!read_connection_config(c)) {
613 		free_connection(c);
614 		outgoing->timeout = maxtimeout;
615 		retry_outgoing(outgoing);
616 		return;
617 	}
618 
619 	outgoing->cfg = lookup_config(c->config_tree, "Address");
620 
621 	if(!outgoing->cfg) {
622 		logger(LOG_ERR, "No address specified for %s", c->name);
623 		free_connection(c);
624 		outgoing->timeout = maxtimeout;
625 		retry_outgoing(outgoing);
626 		return;
627 	}
628 
629 	c->outgoing = outgoing;
630 	c->last_ping_time = now;
631 
632 	connection_add(c);
633 
634 	do_outgoing_connection(c);
635 }
636 
637 /*
638   accept a new tcp connect and create a
639   new connection
640 */
handle_new_meta_connection(int sock)641 bool handle_new_meta_connection(int sock) {
642 	static const int max_accept_burst = 10;
643 	static int last_accept_burst;
644 	static int last_accept_time;
645 	connection_t *c;
646 	sockaddr_t sa;
647 	int fd;
648 	socklen_t len = sizeof(sa);
649 
650 	fd = accept(sock, &sa.sa, &len);
651 
652 	if(fd < 0) {
653 		logger(LOG_ERR, "Accepting a new connection failed: %s", sockstrerror(sockerrno));
654 		return false;
655 	}
656 
657 	if(last_accept_time == now) {
658 		last_accept_burst++;
659 
660 		if(last_accept_burst >= max_accept_burst) {
661 			if(last_accept_burst == max_accept_burst) {
662 				ifdebug(CONNECTIONS) logger(LOG_WARNING, "Throttling incoming connections");
663 			}
664 
665 			tarpit(fd);
666 			return false;
667 		}
668 	} else {
669 		last_accept_burst = 0;
670 		last_accept_time = now;
671 	}
672 
673 	sockaddrunmap(&sa);
674 
675 	c = new_connection();
676 	c->name = xstrdup("<unknown>");
677 	c->outcipher = myself->connection->outcipher;
678 	c->outdigest = myself->connection->outdigest;
679 	c->outmaclength = myself->connection->outmaclength;
680 	c->outcompression = myself->connection->outcompression;
681 
682 	c->address = sa;
683 	c->hostname = sockaddr2hostname(&sa);
684 	c->socket = fd;
685 	c->last_ping_time = now;
686 
687 	ifdebug(CONNECTIONS) logger(LOG_NOTICE, "Connection from %s", c->hostname);
688 
689 	configure_tcp(c);
690 
691 	connection_add(c);
692 
693 	c->allow_request = ID;
694 
695 	return true;
696 }
697 
free_outgoing(outgoing_t * outgoing)698 static void free_outgoing(outgoing_t *outgoing) {
699 	if(outgoing->ai) {
700 		freeaddrinfo(outgoing->ai);
701 	}
702 
703 	if(outgoing->name) {
704 		free(outgoing->name);
705 	}
706 
707 	free(outgoing);
708 }
709 
try_outgoing_connections(void)710 void try_outgoing_connections(void) {
711 	static config_t *cfg = NULL;
712 	char *name;
713 	outgoing_t *outgoing;
714 
715 	outgoing_list = list_alloc((list_action_t)free_outgoing);
716 
717 	for(cfg = lookup_config(config_tree, "ConnectTo"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
718 		get_config_string(cfg, &name);
719 
720 		if(!check_id(name)) {
721 			logger(LOG_ERR,
722 			       "Invalid name for outgoing connection in %s line %d",
723 			       cfg->file, cfg->line);
724 			free(name);
725 			continue;
726 		}
727 
728 		outgoing = xmalloc_and_zero(sizeof(*outgoing));
729 		outgoing->name = name;
730 		list_insert_tail(outgoing_list, outgoing);
731 		setup_outgoing_connection(outgoing);
732 	}
733 }
734