2 net_socket.c -- Handle various kinds of sockets.
3 Copyright (C) 1998-2005 Ivo Timmermans,
4 2000-2009 Guus Sliepen <guus@tinc-vpn.org>
5 2009 Florian Forster <octo@verplant.org>
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #include "connection.h"
41 #define EINPROGRESS WSAEINPROGRESS
44 /* Needed on Mac OS/X */
46 #define SOL_TCP IPPROTO_TCP
49 int addressfamily = AF_UNSPEC;
51 int seconds_till_retry = 5;
53 listen_socket_t listen_socket[MAXSOCKETS];
55 list_t *outgoing_list = NULL;
59 static void configure_tcp(connection_t *c)
64 int flags = fcntl(c->socket, F_GETFL);
66 if(fcntl(c->socket, F_SETFL, flags | O_NONBLOCK) < 0) {
67 logger(LOG_ERR, _("fcntl for %s: %s"), c->hostname, strerror(errno));
70 unsigned long arg = 1;
72 if(ioctlsocket(c->socket, FIONBIO, &arg) != 0) {
73 logger(LOG_ERR, _("ioctlsocket for %s: WSA error %d"), c->hostname, WSAGetLastError());
77 #if defined(SOL_TCP) && defined(TCP_NODELAY)
79 setsockopt(c->socket, SOL_TCP, TCP_NODELAY, &option, sizeof(option));
82 #if defined(SOL_IP) && defined(IP_TOS) && defined(IPTOS_LOWDELAY)
83 option = IPTOS_LOWDELAY;
84 setsockopt(c->socket, SOL_IP, IP_TOS, &option, sizeof(option));
88 static bool bind_to_interface(int sd) { /* {{{ */
91 #if defined(SOL_SOCKET) && defined(SO_BINDTODEVICE)
94 #endif /* defined(SOL_SOCKET) && defined(SO_BINDTODEVICE) */
96 if(!get_config_string (lookup_config (config_tree, "BindToInterface"), &iface))
99 #if defined(SOL_SOCKET) && defined(SO_BINDTODEVICE)
100 memset(&ifr, 0, sizeof(ifr));
101 strncpy(ifr.ifr_ifrn.ifrn_name, iface, IFNAMSIZ);
102 ifr.ifr_ifrn.ifrn_name[IFNAMSIZ - 1] = 0;
104 status = setsockopt(sd, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof(ifr));
106 logger(LOG_ERR, _("Can't bind to interface %s: %s"), iface,
110 #else /* if !defined(SOL_SOCKET) || !defined(SO_BINDTODEVICE) */
111 logger(LOG_WARNING, _("%s not supported on this platform"), "BindToInterface");
115 } /* }}} bool bind_to_interface */
117 static bool bind_to_address(connection_t *c) { /* {{{ */
119 struct addrinfo *ai_list;
120 struct addrinfo *ai_ptr;
121 struct addrinfo ai_hints;
125 assert(c->socket >= 0);
128 if(!get_config_string(lookup_config(config_tree, "BindToAddress"),
132 assert(node != NULL);
134 memset(&ai_hints, 0, sizeof(ai_hints));
135 ai_hints.ai_family = c->address.sa.sa_family;
136 /* We're called from `do_outgoing_connection' only. */
137 ai_hints.ai_socktype = SOCK_STREAM;
138 ai_hints.ai_protocol = IPPROTO_TCP;
142 status = getaddrinfo(node, /* service = */ NULL,
143 &ai_hints, &ai_list);
146 logger(LOG_WARNING, _("Error looking up %s port %s: %s"),
147 node, _("any"), gai_strerror(status));
150 assert(ai_list != NULL);
153 for(ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next) {
154 status = bind(c->socket,
155 ai_list->ai_addr, ai_list->ai_addrlen);
162 logger(LOG_ERR, _("Can't bind to %s/tcp: %s"), node,
164 } else ifdebug(CONNECTIONS) {
165 logger(LOG_DEBUG, "Successfully bound outgoing "
166 "TCP socket to %s", node);
170 freeaddrinfo(ai_list);
172 return status ? false : true;
173 } /* }}} bool bind_to_address */
175 int setup_listen_socket(const sockaddr_t *sa)
184 nfd = socket(sa->sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
187 ifdebug(STATUS) logger(LOG_ERR, _("Creating metasocket failed: %s"), strerror(errno));
191 /* Optimize TCP settings */
194 setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, &option, sizeof(option));
196 #if defined(SOL_IPV6) && defined(IPV6_V6ONLY)
197 if(sa->sa.sa_family == AF_INET6)
198 setsockopt(nfd, SOL_IPV6, IPV6_V6ONLY, &option, sizeof option);
202 (lookup_config(config_tree, "BindToInterface"), &iface)) {
203 #if defined(SOL_SOCKET) && defined(SO_BINDTODEVICE)
206 memset(&ifr, 0, sizeof(ifr));
207 strncpy(ifr.ifr_ifrn.ifrn_name, iface, IFNAMSIZ);
209 if(setsockopt(nfd, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof(ifr))) {
211 logger(LOG_ERR, _("Can't bind to interface %s: %s"), iface,
216 logger(LOG_WARNING, _("%s not supported on this platform"), "BindToInterface");
220 if(bind(nfd, &sa->sa, SALEN(sa->sa))) {
222 addrstr = sockaddr2hostname(sa);
223 logger(LOG_ERR, _("Can't bind to %s/tcp: %s"), addrstr,
231 logger(LOG_ERR, _("System call `%s' failed: %s"), "listen",
239 int setup_vpn_in_socket(const sockaddr_t *sa)
247 nfd = socket(sa->sa.sa_family, SOCK_DGRAM, IPPROTO_UDP);
250 logger(LOG_ERR, _("Creating UDP socket failed: %s"), strerror(errno));
256 int flags = fcntl(nfd, F_GETFL);
258 if(fcntl(nfd, F_SETFL, flags | O_NONBLOCK) < 0) {
260 logger(LOG_ERR, _("System call `%s' failed: %s"), "fcntl",
267 unsigned long arg = 1;
268 if(ioctlsocket(nfd, FIONBIO, &arg) != 0) {
270 logger(LOG_ERR, _("Call to `%s' failed: WSA error %d"), "ioctlsocket",
278 setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, &option, sizeof(option));
280 #if defined(SOL_IPV6) && defined(IPV6_V6ONLY)
281 if(sa->sa.sa_family == AF_INET6)
282 setsockopt(nfd, SOL_IPV6, IPV6_V6ONLY, &option, sizeof option);
285 #if defined(SOL_IP) && defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DO)
286 if(myself->options & OPTION_PMTU_DISCOVERY) {
287 option = IP_PMTUDISC_DO;
288 setsockopt(nfd, SOL_IP, IP_MTU_DISCOVER, &option, sizeof(option));
292 #if defined(SOL_IPV6) && defined(IPV6_MTU_DISCOVER) && defined(IPV6_PMTUDISC_DO)
293 if(myself->options & OPTION_PMTU_DISCOVERY) {
294 option = IPV6_PMTUDISC_DO;
295 setsockopt(nfd, SOL_IPV6, IPV6_MTU_DISCOVER, &option, sizeof(option));
299 if (!bind_to_interface(nfd)) {
304 if(bind(nfd, &sa->sa, SALEN(sa->sa))) {
306 addrstr = sockaddr2hostname(sa);
307 logger(LOG_ERR, _("Can't bind to %s/udp: %s"), addrstr,
314 } /* int setup_vpn_in_socket */
316 void retry_outgoing(outgoing_t *outgoing)
322 outgoing->timeout += 5;
324 if(outgoing->timeout > maxtimeout)
325 outgoing->timeout = maxtimeout;
328 event->handler = (event_handler_t) setup_outgoing_connection;
329 event->time = now + outgoing->timeout;
330 event->data = outgoing;
333 ifdebug(CONNECTIONS) logger(LOG_NOTICE,
334 _("Trying to re-establish outgoing connection in %d seconds"),
338 void finish_connecting(connection_t *c)
342 ifdebug(CONNECTIONS) logger(LOG_INFO, _("Connected to %s (%s)"), c->name, c->hostname);
346 c->last_ping_time = now;
351 void do_outgoing_connection(connection_t *c)
353 char *address, *port;
359 if(!c->outgoing->ai) {
360 if(!c->outgoing->cfg) {
361 ifdebug(CONNECTIONS) logger(LOG_ERR, _("Could not set up a meta connection to %s"),
363 c->status.remove = true;
364 retry_outgoing(c->outgoing);
368 get_config_string(c->outgoing->cfg, &address);
370 if(!get_config_string(lookup_config(c->config_tree, "Port"), &port))
371 xasprintf(&port, "655");
373 c->outgoing->ai = str2addrinfo(address, port, SOCK_STREAM);
377 c->outgoing->aip = c->outgoing->ai;
378 c->outgoing->cfg = lookup_config_next(c->config_tree, c->outgoing->cfg);
381 if(!c->outgoing->aip) {
383 freeaddrinfo(c->outgoing->ai);
384 c->outgoing->ai = NULL;
388 memcpy(&c->address, c->outgoing->aip->ai_addr, c->outgoing->aip->ai_addrlen);
389 c->outgoing->aip = c->outgoing->aip->ai_next;
394 c->hostname = sockaddr2hostname(&c->address);
396 ifdebug(CONNECTIONS) logger(LOG_INFO, _("Trying to connect to %s (%s)"), c->name,
399 c->socket = socket(c->address.sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
401 if(c->socket == -1) {
402 ifdebug(CONNECTIONS) logger(LOG_ERR, _("Creating socket for %s failed: %s"), c->hostname,
408 #if defined(SOL_IPV6) && defined(IPV6_V6ONLY)
410 if(c->address.sa.sa_family == AF_INET6)
411 setsockopt(c->socket, SOL_IPV6, IPV6_V6ONLY, &option, sizeof option);
414 bind_to_interface(c->socket);
417 /* Optimize TCP settings */
423 result = connect(c->socket, &c->address.sa, SALEN(c->address.sa));
426 if(errno == EINPROGRESS
427 #if defined(WIN32) && !defined(O_NONBLOCK)
428 || WSAGetLastError() == WSAEWOULDBLOCK
431 c->status.connecting = true;
435 closesocket(c->socket);
437 ifdebug(CONNECTIONS) logger(LOG_ERR, _("%s: %s"), c->hostname, strerror(errno));
442 finish_connecting(c);
447 void setup_outgoing_connection(outgoing_t *outgoing)
454 n = lookup_node(outgoing->name);
458 ifdebug(CONNECTIONS) logger(LOG_INFO, _("Already connected to %s"), outgoing->name);
460 n->connection->outgoing = outgoing;
464 c = new_connection();
465 c->name = xstrdup(outgoing->name);
466 c->outcipher = myself->connection->outcipher;
467 c->outdigest = myself->connection->outdigest;
468 c->outmaclength = myself->connection->outmaclength;
469 c->outcompression = myself->connection->outcompression;
471 init_configuration(&c->config_tree);
472 read_connection_config(c);
474 outgoing->cfg = lookup_config(c->config_tree, "Address");
477 logger(LOG_ERR, _("No address specified for %s"), c->name);
482 c->outgoing = outgoing;
483 c->last_ping_time = now;
487 do_outgoing_connection(c);
491 accept a new tcp connect and create a
494 bool handle_new_meta_connection(int sock)
499 socklen_t len = sizeof(sa);
503 fd = accept(sock, &sa.sa, &len);
506 logger(LOG_ERR, _("Accepting a new connection failed: %s"),
513 c = new_connection();
514 c->name = xstrdup("<unknown>");
515 c->outcipher = myself->connection->outcipher;
516 c->outdigest = myself->connection->outdigest;
517 c->outmaclength = myself->connection->outmaclength;
518 c->outcompression = myself->connection->outcompression;
521 c->hostname = sockaddr2hostname(&sa);
523 c->last_ping_time = now;
525 ifdebug(CONNECTIONS) logger(LOG_NOTICE, _("Connection from %s"), c->hostname);
531 c->allow_request = ID;
537 void free_outgoing(outgoing_t *outgoing) {
539 freeaddrinfo(outgoing->ai);
542 free(outgoing->name);
547 void try_outgoing_connections(void)
549 static config_t *cfg = NULL;
551 outgoing_t *outgoing;
558 for(node = connection_tree->head; node; node = node->next) {
563 list_delete_list(outgoing_list);
566 outgoing_list = list_alloc((list_action_t)free_outgoing);
568 for(cfg = lookup_config(config_tree, "ConnectTo"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
569 get_config_string(cfg, &name);
571 if(!check_id(name)) {
573 _("Invalid name for outgoing connection in %s line %d"),
574 cfg->file, cfg->line);
579 outgoing = xmalloc_and_zero(sizeof(*outgoing));
580 outgoing->name = name;
581 list_insert_tail(outgoing_list, outgoing);
582 setup_outgoing_connection(outgoing);