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"
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, _("%s not supported on this platform"), "BindToInterface");
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)
196 if(myself->options & OPTION_PMTU_DISCOVERY) {
197 option = IP_PMTUDISC_DO;
198 setsockopt(nfd, SOL_IP, IP_MTU_DISCOVER, &option, sizeof(option));
202 #if defined(SOL_IPV6) && defined(IPV6_MTU_DISCOVER) && defined(IPV6_PMTUDISC_DO)
203 if(myself->options & OPTION_PMTU_DISCOVERY) {
204 option = IPV6_PMTUDISC_DO;
205 setsockopt(nfd, SOL_IPV6, IPV6_MTU_DISCOVER, &option, sizeof(option));
209 #if defined(SOL_SOCKET) && defined(SO_BINDTODEVICE)
214 if(get_config_string(lookup_config(config_tree, "BindToInterface"), &iface)) {
215 memset(&ifr, 0, sizeof(ifr));
216 strncpy(ifr.ifr_ifrn.ifrn_name, iface, IFNAMSIZ);
218 if(setsockopt(nfd, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof(ifr))) {
220 logger(LOG_ERR, _("Can't bind to interface %s: %s"), iface,
228 if(bind(nfd, &sa->sa, SALEN(sa->sa))) {
230 addrstr = sockaddr2hostname(sa);
231 logger(LOG_ERR, _("Can't bind to %s/udp: %s"), addrstr,
240 void retry_outgoing(outgoing_t *outgoing)
246 outgoing->timeout += 5;
248 if(outgoing->timeout > maxtimeout)
249 outgoing->timeout = maxtimeout;
252 event->handler = (event_handler_t) setup_outgoing_connection;
253 event->time = now + outgoing->timeout;
254 event->data = outgoing;
257 ifdebug(CONNECTIONS) logger(LOG_NOTICE,
258 _("Trying to re-establish outgoing connection in %d seconds"),
262 void finish_connecting(connection_t *c)
266 ifdebug(CONNECTIONS) logger(LOG_INFO, _("Connected to %s (%s)"), c->name, c->hostname);
270 c->last_ping_time = now;
275 void do_outgoing_connection(connection_t *c)
277 char *address, *port;
283 if(!c->outgoing->ai) {
284 if(!c->outgoing->cfg) {
285 ifdebug(CONNECTIONS) logger(LOG_ERR, _("Could not set up a meta connection to %s"),
287 c->status.remove = true;
288 retry_outgoing(c->outgoing);
292 get_config_string(c->outgoing->cfg, &address);
294 if(!get_config_string(lookup_config(c->config_tree, "Port"), &port))
295 asprintf(&port, "655");
297 c->outgoing->ai = str2addrinfo(address, port, SOCK_STREAM);
301 c->outgoing->aip = c->outgoing->ai;
302 c->outgoing->cfg = lookup_config_next(c->config_tree, c->outgoing->cfg);
305 if(!c->outgoing->aip) {
307 freeaddrinfo(c->outgoing->ai);
308 c->outgoing->ai = NULL;
312 memcpy(&c->address, c->outgoing->aip->ai_addr, c->outgoing->aip->ai_addrlen);
313 c->outgoing->aip = c->outgoing->aip->ai_next;
318 c->hostname = sockaddr2hostname(&c->address);
320 ifdebug(CONNECTIONS) logger(LOG_INFO, _("Trying to connect to %s (%s)"), c->name,
323 c->socket = socket(c->address.sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
325 if(c->socket == -1) {
326 ifdebug(CONNECTIONS) logger(LOG_ERR, _("Creating socket for %s failed: %s"), c->hostname,
332 #if defined(SOL_IPV6) && defined(IPV6_V6ONLY)
334 if(c->address.sa.sa_family == AF_INET6)
335 setsockopt(c->socket, SOL_IPV6, IPV6_V6ONLY, &option, sizeof option);
338 /* Optimize TCP settings */
344 result = connect(c->socket, &c->address.sa, SALEN(c->address.sa));
347 if(errno == EINPROGRESS
348 #if defined(WIN32) && !defined(O_NONBLOCK)
349 || WSAGetLastError() == WSAEWOULDBLOCK
352 c->status.connecting = true;
356 closesocket(c->socket);
358 ifdebug(CONNECTIONS) logger(LOG_ERR, _("%s: %s"), c->hostname, strerror(errno));
363 finish_connecting(c);
368 void setup_outgoing_connection(outgoing_t *outgoing)
375 n = lookup_node(outgoing->name);
379 ifdebug(CONNECTIONS) logger(LOG_INFO, _("Already connected to %s"), outgoing->name);
381 n->connection->outgoing = outgoing;
385 c = new_connection();
386 c->name = xstrdup(outgoing->name);
387 c->outcipher = myself->connection->outcipher;
388 c->outdigest = myself->connection->outdigest;
389 c->outmaclength = myself->connection->outmaclength;
390 c->outcompression = myself->connection->outcompression;
392 init_configuration(&c->config_tree);
393 read_connection_config(c);
395 outgoing->cfg = lookup_config(c->config_tree, "Address");
398 logger(LOG_ERR, _("No address specified for %s"), c->name);
403 c->outgoing = outgoing;
404 c->last_ping_time = now;
408 do_outgoing_connection(c);
412 accept a new tcp connect and create a
415 bool handle_new_meta_connection(int sock)
420 socklen_t len = sizeof(sa);
424 fd = accept(sock, &sa.sa, &len);
427 logger(LOG_ERR, _("Accepting a new connection failed: %s"),
434 c = new_connection();
435 c->name = xstrdup("<unknown>");
436 c->outcipher = myself->connection->outcipher;
437 c->outdigest = myself->connection->outdigest;
438 c->outmaclength = myself->connection->outmaclength;
439 c->outcompression = myself->connection->outcompression;
442 c->hostname = sockaddr2hostname(&sa);
444 c->last_ping_time = now;
446 ifdebug(CONNECTIONS) logger(LOG_NOTICE, _("Connection from %s"), c->hostname);
452 c->allow_request = ID;
458 void free_outgoing(outgoing_t *outgoing) {
460 freeaddrinfo(outgoing->ai);
463 free(outgoing->name);
468 void try_outgoing_connections(void)
470 static config_t *cfg = NULL;
472 outgoing_t *outgoing;
479 for(node = connection_tree->head; node; node = node->next) {
484 list_delete_list(outgoing_list);
487 outgoing_list = list_alloc((list_action_t)free_outgoing);
489 for(cfg = lookup_config(config_tree, "ConnectTo"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
490 get_config_string(cfg, &name);
492 if(!check_id(name)) {
494 _("Invalid name for outgoing connection in %s line %d"),
495 cfg->file, cfg->line);
500 outgoing = xmalloc_and_zero(sizeof(*outgoing));
501 outgoing->name = name;
502 list_insert_tail(outgoing_list, outgoing);
503 setup_outgoing_connection(outgoing);