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>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 #include "connection.h"
40 #define EINPROGRESS WSAEINPROGRESS
43 /* Needed on Mac OS/X */
45 #define SOL_TCP IPPROTO_TCP
48 int addressfamily = AF_UNSPEC;
50 int seconds_till_retry = 5;
52 listen_socket_t listen_socket[MAXSOCKETS];
54 list_t *outgoing_list = NULL;
58 static void configure_tcp(connection_t *c)
63 int flags = fcntl(c->socket, F_GETFL);
65 if(fcntl(c->socket, F_SETFL, flags | O_NONBLOCK) < 0) {
66 logger(LOG_ERR, _("fcntl for %s: %s"), c->hostname, strerror(errno));
69 unsigned long arg = 1;
71 if(ioctlsocket(c->socket, FIONBIO, &arg) != 0) {
72 logger(LOG_ERR, _("ioctlsocket for %s: WSA error %d"), c->hostname, WSAGetLastError());
76 #if defined(SOL_TCP) && defined(TCP_NODELAY)
78 setsockopt(c->socket, SOL_TCP, TCP_NODELAY, &option, sizeof(option));
81 #if defined(SOL_IP) && defined(IP_TOS) && defined(IPTOS_LOWDELAY)
82 option = IPTOS_LOWDELAY;
83 setsockopt(c->socket, SOL_IP, IP_TOS, &option, sizeof(option));
87 static bool bind_to_interface(int sd) { /* {{{ */
90 #if defined(SOL_SOCKET) && defined(SO_BINDTODEVICE)
93 #endif /* defined(SOL_SOCKET) && defined(SO_BINDTODEVICE) */
95 if(!get_config_string (lookup_config (config_tree, "BindToInterface"), &iface))
98 #if defined(SOL_SOCKET) && defined(SO_BINDTODEVICE)
99 memset(&ifr, 0, sizeof(ifr));
100 strncpy(ifr.ifr_ifrn.ifrn_name, iface, IFNAMSIZ);
101 ifr.ifr_ifrn.ifrn_name[IFNAMSIZ - 1] = 0;
103 status = setsockopt(sd, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof(ifr));
105 logger(LOG_ERR, _("Can't bind to interface %s: %s"), iface,
109 #else /* if !defined(SOL_SOCKET) || !defined(SO_BINDTODEVICE) */
110 logger(LOG_WARNING, _("%s not supported on this platform"), "BindToInterface");
114 } /* }}} bool bind_to_interface */
116 static bool bind_to_address(connection_t *c) { /* {{{ */
118 struct addrinfo *ai_list;
119 struct addrinfo *ai_ptr;
120 struct addrinfo ai_hints;
124 assert(c->socket >= 0);
127 if(!get_config_string(lookup_config(config_tree, "BindToAddress"),
131 assert(node != NULL);
133 memset(&ai_hints, 0, sizeof(ai_hints));
134 ai_hints.ai_family = c->address.sa.sa_family;
135 /* We're called from `do_outgoing_connection' only. */
136 ai_hints.ai_socktype = SOCK_STREAM;
137 ai_hints.ai_protocol = IPPROTO_TCP;
141 status = getaddrinfo(node, /* service = */ NULL,
142 &ai_hints, &ai_list);
145 logger(LOG_WARNING, _("Error looking up %s port %s: %s"),
146 node, _("any"), gai_strerror(status));
149 assert(ai_list != NULL);
152 for(ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next) {
153 status = bind(c->socket,
154 ai_list->ai_addr, ai_list->ai_addrlen);
161 logger(LOG_ERR, _("Can't bind to %s/tcp: %s"), node,
163 } else ifdebug(CONNECTIONS) {
164 logger(LOG_DEBUG, "Successfully bound outgoing "
165 "TCP socket to %s", node);
169 freeaddrinfo(ai_list);
171 return status ? false : true;
172 } /* }}} bool bind_to_address */
174 int setup_listen_socket(const sockaddr_t *sa)
183 nfd = socket(sa->sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
186 ifdebug(STATUS) logger(LOG_ERR, _("Creating metasocket failed: %s"), strerror(errno));
190 /* Optimize TCP settings */
193 setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, &option, sizeof(option));
195 #if defined(SOL_IPV6) && defined(IPV6_V6ONLY)
196 if(sa->sa.sa_family == AF_INET6)
197 setsockopt(nfd, SOL_IPV6, IPV6_V6ONLY, &option, sizeof option);
201 (lookup_config(config_tree, "BindToInterface"), &iface)) {
202 #if defined(SOL_SOCKET) && defined(SO_BINDTODEVICE)
205 memset(&ifr, 0, sizeof(ifr));
206 strncpy(ifr.ifr_ifrn.ifrn_name, iface, IFNAMSIZ);
208 if(setsockopt(nfd, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof(ifr))) {
210 logger(LOG_ERR, _("Can't bind to interface %s: %s"), iface,
215 logger(LOG_WARNING, _("%s not supported on this platform"), "BindToInterface");
219 if(bind(nfd, &sa->sa, SALEN(sa->sa))) {
221 addrstr = sockaddr2hostname(sa);
222 logger(LOG_ERR, _("Can't bind to %s/tcp: %s"), addrstr,
230 logger(LOG_ERR, _("System call `%s' failed: %s"), "listen",
238 int setup_vpn_in_socket(const sockaddr_t *sa)
246 nfd = socket(sa->sa.sa_family, SOCK_DGRAM, IPPROTO_UDP);
249 logger(LOG_ERR, _("Creating UDP socket failed: %s"), strerror(errno));
255 int flags = fcntl(nfd, F_GETFL);
257 if(fcntl(nfd, F_SETFL, flags | O_NONBLOCK) < 0) {
259 logger(LOG_ERR, _("System call `%s' failed: %s"), "fcntl",
266 unsigned long arg = 1;
267 if(ioctlsocket(nfd, FIONBIO, &arg) != 0) {
269 logger(LOG_ERR, _("Call to `%s' failed: WSA error %d"), "ioctlsocket",
277 setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, &option, sizeof(option));
279 #if defined(SOL_IPV6) && defined(IPV6_V6ONLY)
280 if(sa->sa.sa_family == AF_INET6)
281 setsockopt(nfd, SOL_IPV6, IPV6_V6ONLY, &option, sizeof option);
284 #if defined(SOL_IP) && defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DO)
285 if(myself->options & OPTION_PMTU_DISCOVERY) {
286 option = IP_PMTUDISC_DO;
287 setsockopt(nfd, SOL_IP, IP_MTU_DISCOVER, &option, sizeof(option));
291 #if defined(SOL_IPV6) && defined(IPV6_MTU_DISCOVER) && defined(IPV6_PMTUDISC_DO)
292 if(myself->options & OPTION_PMTU_DISCOVERY) {
293 option = IPV6_PMTUDISC_DO;
294 setsockopt(nfd, SOL_IPV6, IPV6_MTU_DISCOVER, &option, sizeof(option));
298 if (!bind_to_interface(nfd)) {
303 if(bind(nfd, &sa->sa, SALEN(sa->sa))) {
305 addrstr = sockaddr2hostname(sa);
306 logger(LOG_ERR, _("Can't bind to %s/udp: %s"), addrstr,
313 } /* int setup_vpn_in_socket */
315 void retry_outgoing(outgoing_t *outgoing)
321 outgoing->timeout += 5;
323 if(outgoing->timeout > maxtimeout)
324 outgoing->timeout = maxtimeout;
327 event->handler = (event_handler_t) setup_outgoing_connection;
328 event->time = now + outgoing->timeout;
329 event->data = outgoing;
332 ifdebug(CONNECTIONS) logger(LOG_NOTICE,
333 _("Trying to re-establish outgoing connection in %d seconds"),
337 void finish_connecting(connection_t *c)
341 ifdebug(CONNECTIONS) logger(LOG_INFO, _("Connected to %s (%s)"), c->name, c->hostname);
345 c->last_ping_time = now;
350 void do_outgoing_connection(connection_t *c)
352 char *address, *port;
358 if(!c->outgoing->ai) {
359 if(!c->outgoing->cfg) {
360 ifdebug(CONNECTIONS) logger(LOG_ERR, _("Could not set up a meta connection to %s"),
362 c->status.remove = true;
363 retry_outgoing(c->outgoing);
367 get_config_string(c->outgoing->cfg, &address);
369 if(!get_config_string(lookup_config(c->config_tree, "Port"), &port))
370 xasprintf(&port, "655");
372 c->outgoing->ai = str2addrinfo(address, port, SOCK_STREAM);
376 c->outgoing->aip = c->outgoing->ai;
377 c->outgoing->cfg = lookup_config_next(c->config_tree, c->outgoing->cfg);
380 if(!c->outgoing->aip) {
382 freeaddrinfo(c->outgoing->ai);
383 c->outgoing->ai = NULL;
387 memcpy(&c->address, c->outgoing->aip->ai_addr, c->outgoing->aip->ai_addrlen);
388 c->outgoing->aip = c->outgoing->aip->ai_next;
393 c->hostname = sockaddr2hostname(&c->address);
395 ifdebug(CONNECTIONS) logger(LOG_INFO, _("Trying to connect to %s (%s)"), c->name,
398 c->socket = socket(c->address.sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
400 if(c->socket == -1) {
401 ifdebug(CONNECTIONS) logger(LOG_ERR, _("Creating socket for %s failed: %s"), c->hostname,
407 #if defined(SOL_IPV6) && defined(IPV6_V6ONLY)
409 if(c->address.sa.sa_family == AF_INET6)
410 setsockopt(c->socket, SOL_IPV6, IPV6_V6ONLY, &option, sizeof option);
413 bind_to_interface(c->socket);
416 /* Optimize TCP settings */
422 result = connect(c->socket, &c->address.sa, SALEN(c->address.sa));
425 if(errno == EINPROGRESS
426 #if defined(WIN32) && !defined(O_NONBLOCK)
427 || WSAGetLastError() == WSAEWOULDBLOCK
430 c->status.connecting = true;
434 closesocket(c->socket);
436 ifdebug(CONNECTIONS) logger(LOG_ERR, _("%s: %s"), c->hostname, strerror(errno));
441 finish_connecting(c);
446 void setup_outgoing_connection(outgoing_t *outgoing)
453 n = lookup_node(outgoing->name);
457 ifdebug(CONNECTIONS) logger(LOG_INFO, _("Already connected to %s"), outgoing->name);
459 n->connection->outgoing = outgoing;
463 c = new_connection();
464 c->name = xstrdup(outgoing->name);
465 c->outcipher = myself->connection->outcipher;
466 c->outdigest = myself->connection->outdigest;
467 c->outmaclength = myself->connection->outmaclength;
468 c->outcompression = myself->connection->outcompression;
470 init_configuration(&c->config_tree);
471 read_connection_config(c);
473 outgoing->cfg = lookup_config(c->config_tree, "Address");
476 logger(LOG_ERR, _("No address specified for %s"), c->name);
481 c->outgoing = outgoing;
482 c->last_ping_time = now;
486 do_outgoing_connection(c);
490 accept a new tcp connect and create a
493 bool handle_new_meta_connection(int sock)
498 socklen_t len = sizeof(sa);
502 fd = accept(sock, &sa.sa, &len);
505 logger(LOG_ERR, _("Accepting a new connection failed: %s"),
512 c = new_connection();
513 c->name = xstrdup("<unknown>");
514 c->outcipher = myself->connection->outcipher;
515 c->outdigest = myself->connection->outdigest;
516 c->outmaclength = myself->connection->outmaclength;
517 c->outcompression = myself->connection->outcompression;
520 c->hostname = sockaddr2hostname(&sa);
522 c->last_ping_time = now;
524 ifdebug(CONNECTIONS) logger(LOG_NOTICE, _("Connection from %s"), c->hostname);
530 c->allow_request = ID;
536 void free_outgoing(outgoing_t *outgoing) {
538 freeaddrinfo(outgoing->ai);
541 free(outgoing->name);
546 void try_outgoing_connections(void)
548 static config_t *cfg = NULL;
550 outgoing_t *outgoing;
557 for(node = connection_tree->head; node; node = node->next) {
562 list_delete_list(outgoing_list);
565 outgoing_list = list_alloc((list_action_t)free_outgoing);
567 for(cfg = lookup_config(config_tree, "ConnectTo"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
568 get_config_string(cfg, &name);
570 if(!check_id(name)) {
572 _("Invalid name for outgoing connection in %s line %d"),
573 cfg->file, cfg->line);
578 outgoing = xmalloc_and_zero(sizeof(*outgoing));
579 outgoing->name = name;
580 list_insert_tail(outgoing_list, outgoing);
581 setup_outgoing_connection(outgoing);