2 net_socket.c -- Handle various kinds of sockets.
3 Copyright (C) 1998-2005 Ivo Timmermans,
4 2000-2007 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];
55 static void configure_tcp(connection_t *c)
60 int flags = fcntl(c->socket, F_GETFL);
62 if(fcntl(c->socket, F_SETFL, flags | O_NONBLOCK) < 0) {
63 logger(LOG_ERR, _("fcntl for %s: %s"), c->hostname, strerror(errno));
66 unsigned long arg = 1;
68 if(ioctlsocket(c->socket, FIONBIO, &arg) != 0) {
69 logger(LOG_ERR, _("ioctlsocket for %s: WSA error %d"), c->hostname, WSAGetLastError());
73 #if defined(SOL_TCP) && defined(TCP_NODELAY)
75 setsockopt(c->socket, SOL_TCP, TCP_NODELAY, &option, sizeof(option));
78 #if defined(SOL_IP) && defined(IP_TOS) && defined(IPTOS_LOWDELAY)
79 option = IPTOS_LOWDELAY;
80 setsockopt(c->socket, SOL_IP, IP_TOS, &option, sizeof(option));
84 int setup_listen_socket(const sockaddr_t *sa)
93 nfd = socket(sa->sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
96 ifdebug(STATUS) logger(LOG_ERR, _("Creating metasocket failed: %s"), strerror(errno));
100 /* Optimize TCP settings */
103 setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, &option, sizeof(option));
106 (lookup_config(config_tree, "BindToInterface"), &iface)) {
107 #if defined(SOL_SOCKET) && defined(SO_BINDTODEVICE)
110 memset(&ifr, 0, sizeof(ifr));
111 strncpy(ifr.ifr_ifrn.ifrn_name, iface, IFNAMSIZ);
113 if(setsockopt(nfd, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof(ifr))) {
115 logger(LOG_ERR, _("Can't bind to interface %s: %s"), iface,
120 logger(LOG_WARNING, _("BindToInterface not supported on this platform"));
124 if(bind(nfd, &sa->sa, SALEN(sa->sa))) {
126 addrstr = sockaddr2hostname(sa);
127 logger(LOG_ERR, _("Can't bind to %s/tcp: %s"), addrstr,
135 logger(LOG_ERR, _("System call `%s' failed: %s"), "listen",
143 int setup_vpn_in_socket(const sockaddr_t *sa)
151 nfd = socket(sa->sa.sa_family, SOCK_DGRAM, IPPROTO_UDP);
154 logger(LOG_ERR, _("Creating UDP socket failed: %s"), strerror(errno));
160 int flags = fcntl(nfd, F_GETFL);
162 if(fcntl(nfd, F_SETFL, flags | O_NONBLOCK) < 0) {
164 logger(LOG_ERR, _("System call `%s' failed: %s"), "fcntl",
171 unsigned long arg = 1;
172 if(ioctlsocket(nfd, FIONBIO, &arg) != 0) {
174 logger(LOG_ERR, _("Call to `%s' failed: WSA error %d"), "ioctlsocket",
182 setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, &option, sizeof(option));
184 #if defined(SOL_IP) && defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DO)
188 if(get_config_bool(lookup_config(myself->connection->config_tree, "PMTUDiscovery"), &choice) && choice) {
189 option = IP_PMTUDISC_DO;
190 setsockopt(nfd, SOL_IP, IP_MTU_DISCOVER, &option, sizeof(option));
195 #if defined(SOL_IPV6) && defined(IPV6_MTU_DISCOVER) && defined(IPV6_PMTUDISC_DO)
199 if(get_config_bool(lookup_config(myself->connection->config_tree, "PMTUDiscovery"), &choice) && choice) {
200 option = IPV6_PMTUDISC_DO;
201 setsockopt(nfd, SOL_IPV6, IPV6_MTU_DISCOVER, &option, sizeof(option));
206 #if defined(SOL_SOCKET) && defined(SO_BINDTODEVICE)
211 if(get_config_string(lookup_config(config_tree, "BindToInterface"), &iface)) {
212 memset(&ifr, 0, sizeof(ifr));
213 strncpy(ifr.ifr_ifrn.ifrn_name, iface, IFNAMSIZ);
215 if(setsockopt(nfd, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof(ifr))) {
217 logger(LOG_ERR, _("Can't bind to interface %s: %s"), iface,
225 if(bind(nfd, &sa->sa, SALEN(sa->sa))) {
227 addrstr = sockaddr2hostname(sa);
228 logger(LOG_ERR, _("Can't bind to %s/udp: %s"), addrstr,
237 void retry_outgoing(outgoing_t *outgoing)
243 outgoing->timeout += 5;
245 if(outgoing->timeout > maxtimeout)
246 outgoing->timeout = maxtimeout;
249 event->handler = (event_handler_t) setup_outgoing_connection;
250 event->time = now + outgoing->timeout;
251 event->data = outgoing;
254 ifdebug(CONNECTIONS) logger(LOG_NOTICE,
255 _("Trying to re-establish outgoing connection in %d seconds"),
259 void finish_connecting(connection_t *c)
263 ifdebug(CONNECTIONS) logger(LOG_INFO, _("Connected to %s (%s)"), c->name, c->hostname);
267 c->last_ping_time = now;
272 void do_outgoing_connection(connection_t *c)
274 char *address, *port;
280 if(!c->outgoing->ai) {
281 if(!c->outgoing->cfg) {
282 ifdebug(CONNECTIONS) logger(LOG_ERR, _("Could not set up a meta connection to %s"),
284 c->status.remove = true;
285 retry_outgoing(c->outgoing);
289 get_config_string(c->outgoing->cfg, &address);
291 if(!get_config_string(lookup_config(c->config_tree, "Port"), &port))
292 asprintf(&port, "655");
294 c->outgoing->ai = str2addrinfo(address, port, SOCK_STREAM);
298 c->outgoing->aip = c->outgoing->ai;
299 c->outgoing->cfg = lookup_config_next(c->config_tree, c->outgoing->cfg);
302 if(!c->outgoing->aip) {
303 freeaddrinfo(c->outgoing->ai);
304 c->outgoing->ai = NULL;
308 memcpy(&c->address, c->outgoing->aip->ai_addr, c->outgoing->aip->ai_addrlen);
309 c->outgoing->aip = c->outgoing->aip->ai_next;
314 c->hostname = sockaddr2hostname(&c->address);
316 ifdebug(CONNECTIONS) logger(LOG_INFO, _("Trying to connect to %s (%s)"), c->name,
319 c->socket = socket(c->address.sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
321 if(c->socket == -1) {
322 ifdebug(CONNECTIONS) logger(LOG_ERR, _("Creating socket for %s failed: %s"), c->hostname,
328 /* Optimize TCP settings */
334 result = connect(c->socket, &c->address.sa, SALEN(c->address.sa));
337 if(errno == EINPROGRESS
338 #if defined(WIN32) && !defined(O_NONBLOCK)
339 || WSAGetLastError() == WSAEWOULDBLOCK
342 c->status.connecting = true;
346 closesocket(c->socket);
348 ifdebug(CONNECTIONS) logger(LOG_ERR, _("%s: %s"), c->hostname, strerror(errno));
353 finish_connecting(c);
358 void setup_outgoing_connection(outgoing_t *outgoing)
365 n = lookup_node(outgoing->name);
369 ifdebug(CONNECTIONS) logger(LOG_INFO, _("Already connected to %s"), outgoing->name);
371 n->connection->outgoing = outgoing;
375 c = new_connection();
376 c->name = xstrdup(outgoing->name);
377 c->outcipher = myself->connection->outcipher;
378 c->outdigest = myself->connection->outdigest;
379 c->outmaclength = myself->connection->outmaclength;
380 c->outcompression = myself->connection->outcompression;
382 init_configuration(&c->config_tree);
383 read_connection_config(c);
385 outgoing->cfg = lookup_config(c->config_tree, "Address");
388 logger(LOG_ERR, _("No address specified for %s"), c->name);
390 free(outgoing->name);
395 c->outgoing = outgoing;
396 c->last_ping_time = now;
400 do_outgoing_connection(c);
404 accept a new tcp connect and create a
407 bool handle_new_meta_connection(int sock)
412 socklen_t len = sizeof(sa);
416 fd = accept(sock, &sa.sa, &len);
419 logger(LOG_ERR, _("Accepting a new connection failed: %s"),
426 c = new_connection();
427 c->name = xstrdup("<unknown>");
428 c->outcipher = myself->connection->outcipher;
429 c->outdigest = myself->connection->outdigest;
430 c->outmaclength = myself->connection->outmaclength;
431 c->outcompression = myself->connection->outcompression;
434 c->hostname = sockaddr2hostname(&sa);
436 c->last_ping_time = now;
438 ifdebug(CONNECTIONS) logger(LOG_NOTICE, _("Connection from %s"), c->hostname);
444 c->allow_request = ID;
450 void try_outgoing_connections(void)
452 static config_t *cfg = NULL;
454 outgoing_t *outgoing;
458 for(cfg = lookup_config(config_tree, "ConnectTo"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
459 get_config_string(cfg, &name);
461 if(!check_id(name)) {
463 _("Invalid name for outgoing connection in %s line %d"),
464 cfg->file, cfg->line);
469 outgoing = xmalloc_and_zero(sizeof(*outgoing));
470 outgoing->name = name;
471 setup_outgoing_connection(outgoing);