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.
26 #include "connection.h"
39 #define EINPROGRESS WSAEINPROGRESS
42 /* Needed on Mac OS/X */
44 #define SOL_TCP IPPROTO_TCP
47 int addressfamily = AF_UNSPEC;
49 int seconds_till_retry = 5;
51 listen_socket_t listen_socket[MAXSOCKETS];
53 list_t *outgoing_list = NULL;
57 static void configure_tcp(connection_t *c)
62 int flags = fcntl(c->socket, F_GETFL);
64 if(fcntl(c->socket, F_SETFL, flags | O_NONBLOCK) < 0) {
65 logger(LOG_ERR, _("fcntl for %s: %s"), c->hostname, strerror(errno));
68 unsigned long arg = 1;
70 if(ioctlsocket(c->socket, FIONBIO, &arg) != 0) {
71 logger(LOG_ERR, _("ioctlsocket for %s: WSA error %d"), c->hostname, WSAGetLastError());
75 #if defined(SOL_TCP) && defined(TCP_NODELAY)
77 setsockopt(c->socket, SOL_TCP, TCP_NODELAY, &option, sizeof(option));
80 #if defined(SOL_IP) && defined(IP_TOS) && defined(IPTOS_LOWDELAY)
81 option = IPTOS_LOWDELAY;
82 setsockopt(c->socket, SOL_IP, IP_TOS, &option, sizeof(option));
86 static bool bind_to_interface(int sd) { /* {{{ */
89 #if defined(SOL_SOCKET) && defined(SO_BINDTODEVICE)
92 #endif /* defined(SOL_SOCKET) && defined(SO_BINDTODEVICE) */
94 if(!get_config_string (lookup_config (config_tree, "BindToInterface"), &iface))
97 #if defined(SOL_SOCKET) && defined(SO_BINDTODEVICE)
98 memset(&ifr, 0, sizeof(ifr));
99 strncpy(ifr.ifr_ifrn.ifrn_name, iface, IFNAMSIZ);
100 ifr.ifr_ifrn.ifrn_name[IFNAMSIZ - 1] = 0;
102 status = setsockopt(sd, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof(ifr));
104 logger(LOG_ERR, _("Can't bind to interface %s: %s"), iface,
108 #else /* if !defined(SOL_SOCKET) || !defined(SO_BINDTODEVICE) */
109 logger(LOG_WARNING, _("%s not supported on this platform"), "BindToInterface");
113 } /* }}} bool bind_to_interface */
115 static bool bind_to_address(connection_t *c) { /* {{{ */
117 struct addrinfo *ai_list;
118 struct addrinfo *ai_ptr;
119 struct addrinfo ai_hints;
123 assert(c->socket >= 0);
126 if(!get_config_string(lookup_config(config_tree, "BindToAddress"),
130 assert(node != NULL);
132 memset(&ai_hints, 0, sizeof(ai_hints));
133 ai_hints.ai_family = c->address.sa.sa_family;
134 /* We're called from `do_outgoing_connection' only. */
135 ai_hints.ai_socktype = SOCK_STREAM;
136 ai_hints.ai_protocol = IPPROTO_TCP;
140 status = getaddrinfo(node, /* service = */ NULL,
141 &ai_hints, &ai_list);
144 logger(LOG_WARNING, _("Error looking up %s port %s: %s"),
145 node, _("any"), gai_strerror(status));
148 assert(ai_list != NULL);
151 for(ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next) {
152 status = bind(c->socket,
153 ai_list->ai_addr, ai_list->ai_addrlen);
160 logger(LOG_ERR, _("Can't bind to %s/tcp: %s"), node,
162 } else ifdebug(CONNECTIONS) {
163 logger(LOG_DEBUG, "Successfully bound outgoing "
164 "TCP socket to %s", node);
168 freeaddrinfo(ai_list);
170 return status ? false : true;
171 } /* }}} bool bind_to_address */
173 int setup_listen_socket(const sockaddr_t *sa)
182 nfd = socket(sa->sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
185 ifdebug(STATUS) logger(LOG_ERR, _("Creating metasocket failed: %s"), strerror(errno));
189 /* Optimize TCP settings */
192 setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, &option, sizeof(option));
194 #if defined(SOL_IPV6) && defined(IPV6_V6ONLY)
195 if(sa->sa.sa_family == AF_INET6)
196 setsockopt(nfd, SOL_IPV6, IPV6_V6ONLY, &option, sizeof option);
200 (lookup_config(config_tree, "BindToInterface"), &iface)) {
201 #if defined(SOL_SOCKET) && defined(SO_BINDTODEVICE)
204 memset(&ifr, 0, sizeof(ifr));
205 strncpy(ifr.ifr_ifrn.ifrn_name, iface, IFNAMSIZ);
207 if(setsockopt(nfd, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof(ifr))) {
209 logger(LOG_ERR, _("Can't bind to interface %s: %s"), iface,
214 logger(LOG_WARNING, _("%s not supported on this platform"), "BindToInterface");
218 if(bind(nfd, &sa->sa, SALEN(sa->sa))) {
220 addrstr = sockaddr2hostname(sa);
221 logger(LOG_ERR, _("Can't bind to %s/tcp: %s"), addrstr,
229 logger(LOG_ERR, _("System call `%s' failed: %s"), "listen",
237 int setup_vpn_in_socket(const sockaddr_t *sa)
245 nfd = socket(sa->sa.sa_family, SOCK_DGRAM, IPPROTO_UDP);
248 logger(LOG_ERR, _("Creating UDP socket failed: %s"), strerror(errno));
254 int flags = fcntl(nfd, F_GETFL);
256 if(fcntl(nfd, F_SETFL, flags | O_NONBLOCK) < 0) {
258 logger(LOG_ERR, _("System call `%s' failed: %s"), "fcntl",
265 unsigned long arg = 1;
266 if(ioctlsocket(nfd, FIONBIO, &arg) != 0) {
268 logger(LOG_ERR, _("Call to `%s' failed: WSA error %d"), "ioctlsocket",
276 setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, &option, sizeof(option));
278 #if defined(SOL_IPV6) && defined(IPV6_V6ONLY)
279 if(sa->sa.sa_family == AF_INET6)
280 setsockopt(nfd, SOL_IPV6, IPV6_V6ONLY, &option, sizeof option);
283 #if defined(SOL_IP) && defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DO)
284 if(myself->options & OPTION_PMTU_DISCOVERY) {
285 option = IP_PMTUDISC_DO;
286 setsockopt(nfd, SOL_IP, IP_MTU_DISCOVER, &option, sizeof(option));
290 #if defined(SOL_IPV6) && defined(IPV6_MTU_DISCOVER) && defined(IPV6_PMTUDISC_DO)
291 if(myself->options & OPTION_PMTU_DISCOVERY) {
292 option = IPV6_PMTUDISC_DO;
293 setsockopt(nfd, SOL_IPV6, IPV6_MTU_DISCOVER, &option, sizeof(option));
297 if (!bind_to_interface(nfd)) {
302 if(bind(nfd, &sa->sa, SALEN(sa->sa))) {
304 addrstr = sockaddr2hostname(sa);
305 logger(LOG_ERR, _("Can't bind to %s/udp: %s"), addrstr,
312 } /* int setup_vpn_in_socket */
314 void retry_outgoing(outgoing_t *outgoing)
320 outgoing->timeout += 5;
322 if(outgoing->timeout > maxtimeout)
323 outgoing->timeout = maxtimeout;
326 event->handler = (event_handler_t) setup_outgoing_connection;
327 event->time = now + outgoing->timeout;
328 event->data = outgoing;
331 ifdebug(CONNECTIONS) logger(LOG_NOTICE,
332 _("Trying to re-establish outgoing connection in %d seconds"),
336 void finish_connecting(connection_t *c)
340 ifdebug(CONNECTIONS) logger(LOG_INFO, _("Connected to %s (%s)"), c->name, c->hostname);
344 c->last_ping_time = now;
349 void do_outgoing_connection(connection_t *c)
351 char *address, *port;
357 if(!c->outgoing->ai) {
358 if(!c->outgoing->cfg) {
359 ifdebug(CONNECTIONS) logger(LOG_ERR, _("Could not set up a meta connection to %s"),
361 c->status.remove = true;
362 retry_outgoing(c->outgoing);
366 get_config_string(c->outgoing->cfg, &address);
368 if(!get_config_string(lookup_config(c->config_tree, "Port"), &port))
369 xasprintf(&port, "655");
371 c->outgoing->ai = str2addrinfo(address, port, SOCK_STREAM);
375 c->outgoing->aip = c->outgoing->ai;
376 c->outgoing->cfg = lookup_config_next(c->config_tree, c->outgoing->cfg);
379 if(!c->outgoing->aip) {
381 freeaddrinfo(c->outgoing->ai);
382 c->outgoing->ai = NULL;
386 memcpy(&c->address, c->outgoing->aip->ai_addr, c->outgoing->aip->ai_addrlen);
387 c->outgoing->aip = c->outgoing->aip->ai_next;
392 c->hostname = sockaddr2hostname(&c->address);
394 ifdebug(CONNECTIONS) logger(LOG_INFO, _("Trying to connect to %s (%s)"), c->name,
397 c->socket = socket(c->address.sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
399 if(c->socket == -1) {
400 ifdebug(CONNECTIONS) logger(LOG_ERR, _("Creating socket for %s failed: %s"), c->hostname,
406 #if defined(SOL_IPV6) && defined(IPV6_V6ONLY)
408 if(c->address.sa.sa_family == AF_INET6)
409 setsockopt(c->socket, SOL_IPV6, IPV6_V6ONLY, &option, sizeof option);
412 bind_to_interface(c->socket);
415 /* Optimize TCP settings */
421 result = connect(c->socket, &c->address.sa, SALEN(c->address.sa));
424 if(errno == EINPROGRESS
425 #if defined(WIN32) && !defined(O_NONBLOCK)
426 || WSAGetLastError() == WSAEWOULDBLOCK
429 c->status.connecting = true;
433 closesocket(c->socket);
435 ifdebug(CONNECTIONS) logger(LOG_ERR, _("%s: %s"), c->hostname, strerror(errno));
440 finish_connecting(c);
445 void setup_outgoing_connection(outgoing_t *outgoing)
452 n = lookup_node(outgoing->name);
456 ifdebug(CONNECTIONS) logger(LOG_INFO, _("Already connected to %s"), outgoing->name);
458 n->connection->outgoing = outgoing;
462 c = new_connection();
463 c->name = xstrdup(outgoing->name);
464 c->outcipher = myself->connection->outcipher;
465 c->outdigest = myself->connection->outdigest;
466 c->outmaclength = myself->connection->outmaclength;
467 c->outcompression = myself->connection->outcompression;
469 init_configuration(&c->config_tree);
470 read_connection_config(c);
472 outgoing->cfg = lookup_config(c->config_tree, "Address");
475 logger(LOG_ERR, _("No address specified for %s"), c->name);
480 c->outgoing = outgoing;
481 c->last_ping_time = now;
485 do_outgoing_connection(c);
489 accept a new tcp connect and create a
492 bool handle_new_meta_connection(int sock)
497 socklen_t len = sizeof(sa);
501 fd = accept(sock, &sa.sa, &len);
504 logger(LOG_ERR, _("Accepting a new connection failed: %s"),
511 c = new_connection();
512 c->name = xstrdup("<unknown>");
513 c->outcipher = myself->connection->outcipher;
514 c->outdigest = myself->connection->outdigest;
515 c->outmaclength = myself->connection->outmaclength;
516 c->outcompression = myself->connection->outcompression;
519 c->hostname = sockaddr2hostname(&sa);
521 c->last_ping_time = now;
523 ifdebug(CONNECTIONS) logger(LOG_NOTICE, _("Connection from %s"), c->hostname);
529 c->allow_request = ID;
535 void free_outgoing(outgoing_t *outgoing) {
537 freeaddrinfo(outgoing->ai);
540 free(outgoing->name);
545 void try_outgoing_connections(void)
547 static config_t *cfg = NULL;
549 outgoing_t *outgoing;
556 for(node = connection_tree->head; node; node = node->next) {
561 list_delete_list(outgoing_list);
564 outgoing_list = list_alloc((list_action_t)free_outgoing);
566 for(cfg = lookup_config(config_tree, "ConnectTo"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
567 get_config_string(cfg, &name);
569 if(!check_id(name)) {
571 _("Invalid name for outgoing connection in %s line %d"),
572 cfg->file, cfg->line);
577 outgoing = xmalloc_and_zero(sizeof(*outgoing));
578 outgoing->name = name;
579 list_insert_tail(outgoing_list, outgoing);
580 setup_outgoing_connection(outgoing);