2 net_socket.c -- Handle various kinds of sockets.
3 Copyright (C) 1998-2005 Ivo Timmermans,
4 2000-2008 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"
38 #define EINPROGRESS WSAEINPROGRESS
41 /* Needed on Mac OS/X */
43 #define SOL_TCP IPPROTO_TCP
46 int addressfamily = AF_UNSPEC;
48 int seconds_till_retry = 5;
50 listen_socket_t listen_socket[MAXSOCKETS];
52 list_t *outgoing_list = NULL;
56 static void configure_tcp(connection_t *c)
61 int flags = fcntl(c->socket, F_GETFL);
63 if(fcntl(c->socket, F_SETFL, flags | O_NONBLOCK) < 0) {
64 logger(LOG_ERR, _("fcntl for %s: %s"), c->hostname, strerror(errno));
67 unsigned long arg = 1;
69 if(ioctlsocket(c->socket, FIONBIO, &arg) != 0) {
70 logger(LOG_ERR, _("ioctlsocket for %s: WSA error %d"), c->hostname, WSAGetLastError());
74 #if defined(SOL_TCP) && defined(TCP_NODELAY)
76 setsockopt(c->socket, SOL_TCP, TCP_NODELAY, &option, sizeof(option));
79 #if defined(SOL_IP) && defined(IP_TOS) && defined(IPTOS_LOWDELAY)
80 option = IPTOS_LOWDELAY;
81 setsockopt(c->socket, SOL_IP, IP_TOS, &option, sizeof(option));
85 int setup_listen_socket(const sockaddr_t *sa)
94 nfd = socket(sa->sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
97 ifdebug(STATUS) logger(LOG_ERR, _("Creating metasocket failed: %s"), strerror(errno));
101 /* Optimize TCP settings */
104 setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, &option, sizeof(option));
106 #if defined(SOL_IPV6) && defined(IPV6_V6ONLY)
107 if(sa->sa.sa_family == AF_INET6)
108 setsockopt(nfd, SOL_IPV6, IPV6_V6ONLY, &option, sizeof option);
112 (lookup_config(config_tree, "BindToInterface"), &iface)) {
113 #if defined(SOL_SOCKET) && defined(SO_BINDTODEVICE)
116 memset(&ifr, 0, sizeof(ifr));
117 strncpy(ifr.ifr_ifrn.ifrn_name, iface, IFNAMSIZ);
119 if(setsockopt(nfd, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof(ifr))) {
121 logger(LOG_ERR, _("Can't bind to interface %s: %s"), iface,
126 logger(LOG_WARNING, _("BindToInterface not supported on this platform"));
130 if(bind(nfd, &sa->sa, SALEN(sa->sa))) {
132 addrstr = sockaddr2hostname(sa);
133 logger(LOG_ERR, _("Can't bind to %s/tcp: %s"), addrstr,
141 logger(LOG_ERR, _("System call `%s' failed: %s"), "listen",
149 int setup_vpn_in_socket(const sockaddr_t *sa)
157 nfd = socket(sa->sa.sa_family, SOCK_DGRAM, IPPROTO_UDP);
160 logger(LOG_ERR, _("Creating UDP socket failed: %s"), strerror(errno));
166 int flags = fcntl(nfd, F_GETFL);
168 if(fcntl(nfd, F_SETFL, flags | O_NONBLOCK) < 0) {
170 logger(LOG_ERR, _("System call `%s' failed: %s"), "fcntl",
177 unsigned long arg = 1;
178 if(ioctlsocket(nfd, FIONBIO, &arg) != 0) {
180 logger(LOG_ERR, _("Call to `%s' failed: WSA error %d"), "ioctlsocket",
188 setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, &option, sizeof(option));
190 #if defined(SOL_IPV6) && defined(IPV6_V6ONLY)
191 if(sa->sa.sa_family == AF_INET6)
192 setsockopt(nfd, SOL_IPV6, IPV6_V6ONLY, &option, sizeof option);
195 #if defined(SOL_IP) && defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DO)
199 if(!get_config_bool(lookup_config(myself->connection->config_tree, "PMTUDiscovery"), &choice) || choice) {
200 option = IP_PMTUDISC_DO;
201 setsockopt(nfd, SOL_IP, IP_MTU_DISCOVER, &option, sizeof(option));
206 #if defined(SOL_IPV6) && defined(IPV6_MTU_DISCOVER) && defined(IPV6_PMTUDISC_DO)
210 if(!get_config_bool(lookup_config(myself->connection->config_tree, "PMTUDiscovery"), &choice) || choice) {
211 option = IPV6_PMTUDISC_DO;
212 setsockopt(nfd, SOL_IPV6, IPV6_MTU_DISCOVER, &option, sizeof(option));
217 #if defined(SOL_SOCKET) && defined(SO_BINDTODEVICE)
222 if(get_config_string(lookup_config(config_tree, "BindToInterface"), &iface)) {
223 memset(&ifr, 0, sizeof(ifr));
224 strncpy(ifr.ifr_ifrn.ifrn_name, iface, IFNAMSIZ);
226 if(setsockopt(nfd, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof(ifr))) {
228 logger(LOG_ERR, _("Can't bind to interface %s: %s"), iface,
236 if(bind(nfd, &sa->sa, SALEN(sa->sa))) {
238 addrstr = sockaddr2hostname(sa);
239 logger(LOG_ERR, _("Can't bind to %s/udp: %s"), addrstr,
248 void retry_outgoing(outgoing_t *outgoing)
254 outgoing->timeout += 5;
256 if(outgoing->timeout > maxtimeout)
257 outgoing->timeout = maxtimeout;
260 event->handler = (event_handler_t) setup_outgoing_connection;
261 event->time = now + outgoing->timeout;
262 event->data = outgoing;
265 ifdebug(CONNECTIONS) logger(LOG_NOTICE,
266 _("Trying to re-establish outgoing connection in %d seconds"),
270 void finish_connecting(connection_t *c)
274 ifdebug(CONNECTIONS) logger(LOG_INFO, _("Connected to %s (%s)"), c->name, c->hostname);
278 c->last_ping_time = now;
283 void do_outgoing_connection(connection_t *c)
285 char *address, *port;
291 if(!c->outgoing->ai) {
292 if(!c->outgoing->cfg) {
293 ifdebug(CONNECTIONS) logger(LOG_ERR, _("Could not set up a meta connection to %s"),
295 c->status.remove = true;
296 retry_outgoing(c->outgoing);
300 get_config_string(c->outgoing->cfg, &address);
302 if(!get_config_string(lookup_config(c->config_tree, "Port"), &port))
303 asprintf(&port, "655");
305 c->outgoing->ai = str2addrinfo(address, port, SOCK_STREAM);
309 c->outgoing->aip = c->outgoing->ai;
310 c->outgoing->cfg = lookup_config_next(c->config_tree, c->outgoing->cfg);
313 if(!c->outgoing->aip) {
315 freeaddrinfo(c->outgoing->ai);
316 c->outgoing->ai = NULL;
320 memcpy(&c->address, c->outgoing->aip->ai_addr, c->outgoing->aip->ai_addrlen);
321 c->outgoing->aip = c->outgoing->aip->ai_next;
326 c->hostname = sockaddr2hostname(&c->address);
328 ifdebug(CONNECTIONS) logger(LOG_INFO, _("Trying to connect to %s (%s)"), c->name,
331 c->socket = socket(c->address.sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
333 if(c->socket == -1) {
334 ifdebug(CONNECTIONS) logger(LOG_ERR, _("Creating socket for %s failed: %s"), c->hostname,
340 #if defined(SOL_IPV6) && defined(IPV6_V6ONLY)
342 if(c->address.sa.sa_family == AF_INET6)
343 setsockopt(c->socket, SOL_IPV6, IPV6_V6ONLY, &option, sizeof option);
346 /* Optimize TCP settings */
352 result = connect(c->socket, &c->address.sa, SALEN(c->address.sa));
355 if(errno == EINPROGRESS
356 #if defined(WIN32) && !defined(O_NONBLOCK)
357 || WSAGetLastError() == WSAEWOULDBLOCK
360 c->status.connecting = true;
364 closesocket(c->socket);
366 ifdebug(CONNECTIONS) logger(LOG_ERR, _("%s: %s"), c->hostname, strerror(errno));
371 finish_connecting(c);
376 void setup_outgoing_connection(outgoing_t *outgoing)
383 n = lookup_node(outgoing->name);
387 ifdebug(CONNECTIONS) logger(LOG_INFO, _("Already connected to %s"), outgoing->name);
389 n->connection->outgoing = outgoing;
393 c = new_connection();
394 c->name = xstrdup(outgoing->name);
395 c->outcipher = myself->connection->outcipher;
396 c->outdigest = myself->connection->outdigest;
397 c->outmaclength = myself->connection->outmaclength;
398 c->outcompression = myself->connection->outcompression;
400 init_configuration(&c->config_tree);
401 read_connection_config(c);
403 outgoing->cfg = lookup_config(c->config_tree, "Address");
406 logger(LOG_ERR, _("No address specified for %s"), c->name);
411 c->outgoing = outgoing;
412 c->last_ping_time = now;
416 do_outgoing_connection(c);
420 accept a new tcp connect and create a
423 bool handle_new_meta_connection(int sock)
428 socklen_t len = sizeof(sa);
432 fd = accept(sock, &sa.sa, &len);
435 logger(LOG_ERR, _("Accepting a new connection failed: %s"),
442 c = new_connection();
443 c->name = xstrdup("<unknown>");
444 c->outcipher = myself->connection->outcipher;
445 c->outdigest = myself->connection->outdigest;
446 c->outmaclength = myself->connection->outmaclength;
447 c->outcompression = myself->connection->outcompression;
450 c->hostname = sockaddr2hostname(&sa);
452 c->last_ping_time = now;
454 ifdebug(CONNECTIONS) logger(LOG_NOTICE, _("Connection from %s"), c->hostname);
460 c->allow_request = ID;
466 void free_outgoing(outgoing_t *outgoing) {
468 freeaddrinfo(outgoing->ai);
471 free(outgoing->name);
476 void try_outgoing_connections(void)
478 static config_t *cfg = NULL;
480 outgoing_t *outgoing;
487 for(node = connection_tree->head; node; node = node->next) {
492 list_delete_list(outgoing_list);
495 outgoing_list = list_alloc((list_action_t)free_outgoing);
497 for(cfg = lookup_config(config_tree, "ConnectTo"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
498 get_config_string(cfg, &name);
500 if(!check_id(name)) {
502 _("Invalid name for outgoing connection in %s line %d"),
503 cfg->file, cfg->line);
508 outgoing = xmalloc_and_zero(sizeof(*outgoing));
509 outgoing->name = name;
510 list_insert_tail(outgoing_list, outgoing);
511 setup_outgoing_connection(outgoing);