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"
37 #define EINPROGRESS WSAEINPROGRESS
40 /* Needed on Mac OS/X */
42 #define SOL_TCP IPPROTO_TCP
45 int addressfamily = AF_UNSPEC;
47 int seconds_till_retry = 5;
49 listen_socket_t listen_socket[MAXSOCKETS];
54 static void configure_tcp(connection_t *c)
59 int flags = fcntl(c->socket, F_GETFL);
61 if(fcntl(c->socket, F_SETFL, flags | O_NONBLOCK) < 0) {
62 logger(LOG_ERR, _("fcntl for %s: %s"), c->hostname, strerror(errno));
65 unsigned long arg = 1;
67 if(ioctlsocket(c->socket, FIONBIO, &arg) != 0) {
68 logger(LOG_ERR, _("ioctlsocket for %s: WSA error %d"), c->hostname, WSAGetLastError());
72 #if defined(SOL_TCP) && defined(TCP_NODELAY)
74 setsockopt(c->socket, SOL_TCP, TCP_NODELAY, &option, sizeof(option));
77 #if defined(SOL_IP) && defined(IP_TOS) && defined(IPTOS_LOWDELAY)
78 option = IPTOS_LOWDELAY;
79 setsockopt(c->socket, SOL_IP, IP_TOS, &option, sizeof(option));
83 int setup_listen_socket(const sockaddr_t *sa)
92 nfd = socket(sa->sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
95 ifdebug(STATUS) logger(LOG_ERR, _("Creating metasocket failed: %s"), strerror(errno));
99 /* Optimize TCP settings */
102 setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, &option, sizeof(option));
105 (lookup_config(config_tree, "BindToInterface"), &iface)) {
106 #if defined(SOL_SOCKET) && defined(SO_BINDTODEVICE)
109 memset(&ifr, 0, sizeof(ifr));
110 strncpy(ifr.ifr_ifrn.ifrn_name, iface, IFNAMSIZ);
112 if(setsockopt(nfd, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof(ifr))) {
114 logger(LOG_ERR, _("Can't bind to interface %s: %s"), iface,
119 logger(LOG_WARNING, _("BindToInterface not supported on this platform"));
123 if(bind(nfd, &sa->sa, SALEN(sa->sa))) {
125 addrstr = sockaddr2hostname(sa);
126 logger(LOG_ERR, _("Can't bind to %s/tcp: %s"), addrstr,
134 logger(LOG_ERR, _("System call `%s' failed: %s"), "listen",
142 int setup_vpn_in_socket(const sockaddr_t *sa)
150 nfd = socket(sa->sa.sa_family, SOCK_DGRAM, IPPROTO_UDP);
153 logger(LOG_ERR, _("Creating UDP socket failed: %s"), strerror(errno));
159 int flags = fcntl(nfd, F_GETFL);
161 if(fcntl(nfd, F_SETFL, flags | O_NONBLOCK) < 0) {
163 logger(LOG_ERR, _("System call `%s' failed: %s"), "fcntl",
170 unsigned long arg = 1;
171 if(ioctlsocket(nfd, FIONBIO, &arg) != 0) {
173 logger(LOG_ERR, _("Call to `%s' failed: WSA error %d"), "ioctlsocket",
181 setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, &option, sizeof(option));
183 #if defined(SOL_IP) && defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DO)
187 if(get_config_bool(lookup_config(myself->connection->config_tree, "PMTUDiscovery"), &choice) && choice) {
188 option = IP_PMTUDISC_DO;
189 setsockopt(nfd, SOL_IP, IP_MTU_DISCOVER, &option, sizeof(option));
194 #if defined(SOL_IPV6) && defined(IPV6_MTU_DISCOVER) && defined(IPV6_PMTUDISC_DO)
198 if(get_config_bool(lookup_config(myself->connection->config_tree, "PMTUDiscovery"), &choice) && choice) {
199 option = IPV6_PMTUDISC_DO;
200 setsockopt(nfd, SOL_IPV6, IPV6_MTU_DISCOVER, &option, sizeof(option));
205 #if defined(SOL_SOCKET) && defined(SO_BINDTODEVICE)
210 if(get_config_string(lookup_config(config_tree, "BindToInterface"), &iface)) {
211 memset(&ifr, 0, sizeof(ifr));
212 strncpy(ifr.ifr_ifrn.ifrn_name, iface, IFNAMSIZ);
214 if(setsockopt(nfd, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof(ifr))) {
216 logger(LOG_ERR, _("Can't bind to interface %s: %s"), iface,
224 if(bind(nfd, &sa->sa, SALEN(sa->sa))) {
226 addrstr = sockaddr2hostname(sa);
227 logger(LOG_ERR, _("Can't bind to %s/udp: %s"), addrstr,
236 static void retry_outgoing_handler(int fd, short events, void *data) {
237 retry_outgoing(data);
240 void retry_outgoing(outgoing_t *outgoing) {
243 outgoing->timeout += 5;
245 if(outgoing->timeout > maxtimeout)
246 outgoing->timeout = maxtimeout;
248 timeout_set(&outgoing->ev, retry_outgoing_handler, outgoing);
249 event_add(&outgoing->ev, &(struct timeval){outgoing->timeout, 0});
251 ifdebug(CONNECTIONS) logger(LOG_NOTICE,
252 _("Trying to re-establish outgoing connection in %d seconds"),
256 void finish_connecting(connection_t *c)
260 ifdebug(CONNECTIONS) logger(LOG_INFO, _("Connected to %s (%s)"), c->name, c->hostname);
264 c->last_ping_time = now;
269 void do_outgoing_connection(connection_t *c)
271 char *address, *port;
277 if(!c->outgoing->ai) {
278 if(!c->outgoing->cfg) {
279 ifdebug(CONNECTIONS) logger(LOG_ERR, _("Could not set up a meta connection to %s"),
281 c->status.remove = true;
282 retry_outgoing(c->outgoing);
286 get_config_string(c->outgoing->cfg, &address);
288 if(!get_config_string(lookup_config(c->config_tree, "Port"), &port))
289 asprintf(&port, "655");
291 c->outgoing->ai = str2addrinfo(address, port, SOCK_STREAM);
295 c->outgoing->aip = c->outgoing->ai;
296 c->outgoing->cfg = lookup_config_next(c->config_tree, c->outgoing->cfg);
299 if(!c->outgoing->aip) {
300 freeaddrinfo(c->outgoing->ai);
301 c->outgoing->ai = NULL;
305 memcpy(&c->address, c->outgoing->aip->ai_addr, c->outgoing->aip->ai_addrlen);
306 c->outgoing->aip = c->outgoing->aip->ai_next;
311 c->hostname = sockaddr2hostname(&c->address);
313 ifdebug(CONNECTIONS) logger(LOG_INFO, _("Trying to connect to %s (%s)"), c->name,
316 c->socket = socket(c->address.sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
318 if(c->socket == -1) {
319 ifdebug(CONNECTIONS) logger(LOG_ERR, _("Creating socket for %s failed: %s"), c->hostname,
325 /* Optimize TCP settings */
331 result = connect(c->socket, &c->address.sa, SALEN(c->address.sa));
334 if(errno == EINPROGRESS
335 #if defined(WIN32) && !defined(O_NONBLOCK)
336 || WSAGetLastError() == WSAEWOULDBLOCK
339 c->status.connecting = true;
343 closesocket(c->socket);
345 ifdebug(CONNECTIONS) logger(LOG_ERR, _("%s: %s"), c->hostname, strerror(errno));
350 finish_connecting(c);
355 void setup_outgoing_connection(outgoing_t *outgoing)
362 n = lookup_node(outgoing->name);
366 ifdebug(CONNECTIONS) logger(LOG_INFO, _("Already connected to %s"), outgoing->name);
368 n->connection->outgoing = outgoing;
372 c = new_connection();
373 c->name = xstrdup(outgoing->name);
374 c->outcipher = myself->connection->outcipher;
375 c->outdigest = myself->connection->outdigest;
376 c->outmaclength = myself->connection->outmaclength;
377 c->outcompression = myself->connection->outcompression;
379 init_configuration(&c->config_tree);
380 read_connection_config(c);
382 outgoing->cfg = lookup_config(c->config_tree, "Address");
385 logger(LOG_ERR, _("No address specified for %s"), c->name);
387 free(outgoing->name);
392 c->outgoing = outgoing;
393 c->last_ping_time = now;
397 do_outgoing_connection(c);
399 event_set(&c->ev, c->socket, EV_READ | EV_PERSIST, handle_meta_connection_data, c);
400 event_set(&c->outev, c->socket, EV_WRITE | EV_PERSIST, flush_meta, c);
401 if(event_add(&c->ev, NULL) < 0) {
402 logger(LOG_EMERG, _("event_add failed: %s"), strerror(errno));
408 accept a new tcp connect and create a
411 void handle_new_meta_connection(int sock, short events, void *data)
416 socklen_t len = sizeof(sa);
420 fd = accept(sock, &sa.sa, &len);
423 logger(LOG_ERR, _("Accepting a new connection failed: %s"), strerror(errno));
429 c = new_connection();
430 c->name = xstrdup("<unknown>");
431 c->outcipher = myself->connection->outcipher;
432 c->outdigest = myself->connection->outdigest;
433 c->outmaclength = myself->connection->outmaclength;
434 c->outcompression = myself->connection->outcompression;
437 c->hostname = sockaddr2hostname(&sa);
439 c->last_ping_time = now;
441 ifdebug(CONNECTIONS) logger(LOG_NOTICE, _("Connection from %s"), c->hostname);
443 event_set(&c->ev, c->socket, EV_READ | EV_PERSIST, handle_meta_connection_data, c);
444 event_set(&c->outev, c->socket, EV_WRITE | EV_PERSIST, flush_meta, c);
445 if(event_add(&c->ev, NULL) < 0) {
446 logger(LOG_ERR, _("event_add failed: %s"), strerror(errno));
455 c->allow_request = ID;
459 void try_outgoing_connections(void)
461 static config_t *cfg = NULL;
463 outgoing_t *outgoing;
467 for(cfg = lookup_config(config_tree, "ConnectTo"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
468 get_config_string(cfg, &name);
470 if(!check_id(name)) {
472 _("Invalid name for outgoing connection in %s line %d"),
473 cfg->file, cfg->line);
478 outgoing = xmalloc_and_zero(sizeof(*outgoing));
479 outgoing->name = name;
480 setup_outgoing_connection(outgoing);