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.
25 #include "splay_tree.h"
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) {
58 int flags = fcntl(c->socket, F_GETFL);
60 if(fcntl(c->socket, F_SETFL, flags | O_NONBLOCK) < 0) {
61 logger(LOG_ERR, _("fcntl for %s: %s"), c->hostname, strerror(errno));
64 unsigned long arg = 1;
66 if(ioctlsocket(c->socket, FIONBIO, &arg) != 0) {
67 logger(LOG_ERR, _("ioctlsocket for %s: WSA error %d"), c->hostname, WSAGetLastError());
71 #if defined(SOL_TCP) && defined(TCP_NODELAY)
73 setsockopt(c->socket, SOL_TCP, TCP_NODELAY, &option, sizeof(option));
76 #if defined(SOL_IP) && defined(IP_TOS) && defined(IPTOS_LOWDELAY)
77 option = IPTOS_LOWDELAY;
78 setsockopt(c->socket, SOL_IP, IP_TOS, &option, sizeof(option));
82 int setup_listen_socket(const sockaddr_t *sa) {
90 nfd = socket(sa->sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
93 ifdebug(STATUS) logger(LOG_ERR, _("Creating metasocket failed: %s"), strerror(errno));
97 /* Optimize TCP settings */
100 setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, &option, sizeof(option));
103 (lookup_config(config_tree, "BindToInterface"), &iface)) {
104 #if defined(SOL_SOCKET) && defined(SO_BINDTODEVICE)
107 memset(&ifr, 0, sizeof(ifr));
108 strncpy(ifr.ifr_ifrn.ifrn_name, iface, IFNAMSIZ);
110 if(setsockopt(nfd, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof(ifr))) {
112 logger(LOG_ERR, _("Can't bind to interface %s: %s"), iface,
117 logger(LOG_WARNING, _("BindToInterface not supported on this platform"));
121 if(bind(nfd, &sa->sa, SALEN(sa->sa))) {
123 addrstr = sockaddr2hostname(sa);
124 logger(LOG_ERR, _("Can't bind to %s/tcp: %s"), addrstr,
132 logger(LOG_ERR, _("System call `%s' failed: %s"), "listen",
140 int setup_vpn_in_socket(const sockaddr_t *sa) {
147 nfd = socket(sa->sa.sa_family, SOCK_DGRAM, IPPROTO_UDP);
150 logger(LOG_ERR, _("Creating UDP socket failed: %s"), strerror(errno));
156 int flags = fcntl(nfd, F_GETFL);
158 if(fcntl(nfd, F_SETFL, flags | O_NONBLOCK) < 0) {
160 logger(LOG_ERR, _("System call `%s' failed: %s"), "fcntl",
167 unsigned long arg = 1;
168 if(ioctlsocket(nfd, FIONBIO, &arg) != 0) {
170 logger(LOG_ERR, _("Call to `%s' failed: WSA error %d"), "ioctlsocket",
178 setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, &option, sizeof(option));
180 #if defined(SOL_IP) && defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DO)
184 if(get_config_bool(lookup_config(myself->connection->config_tree, "PMTUDiscovery"), &choice) && choice) {
185 option = IP_PMTUDISC_DO;
186 setsockopt(nfd, SOL_IP, IP_MTU_DISCOVER, &option, sizeof(option));
191 #if defined(SOL_IPV6) && defined(IPV6_MTU_DISCOVER) && defined(IPV6_PMTUDISC_DO)
195 if(get_config_bool(lookup_config(myself->connection->config_tree, "PMTUDiscovery"), &choice) && choice) {
196 option = IPV6_PMTUDISC_DO;
197 setsockopt(nfd, SOL_IPV6, IPV6_MTU_DISCOVER, &option, sizeof(option));
202 #if defined(SOL_SOCKET) && defined(SO_BINDTODEVICE)
207 if(get_config_string(lookup_config(config_tree, "BindToInterface"), &iface)) {
208 memset(&ifr, 0, sizeof(ifr));
209 strncpy(ifr.ifr_ifrn.ifrn_name, iface, IFNAMSIZ);
211 if(setsockopt(nfd, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof(ifr))) {
213 logger(LOG_ERR, _("Can't bind to interface %s: %s"), iface,
221 if(bind(nfd, &sa->sa, SALEN(sa->sa))) {
223 addrstr = sockaddr2hostname(sa);
224 logger(LOG_ERR, _("Can't bind to %s/udp: %s"), addrstr,
233 static void retry_outgoing_handler(int fd, short events, void *data) {
234 do_outgoing_connection(data);
237 void retry_outgoing(outgoing_t *outgoing) {
240 outgoing->timeout += 5;
242 if(outgoing->timeout > maxtimeout)
243 outgoing->timeout = maxtimeout;
245 timeout_set(&outgoing->ev, retry_outgoing_handler, outgoing);
246 event_add(&outgoing->ev, &(struct timeval){outgoing->timeout, 0});
248 ifdebug(CONNECTIONS) logger(LOG_NOTICE,
249 _("Trying to re-establish outgoing connection in %d seconds"),
253 void finish_connecting(connection_t *c) {
256 ifdebug(CONNECTIONS) logger(LOG_INFO, _("Connected to %s (%s)"), c->name, c->hostname);
260 c->last_ping_time = time(NULL);
261 c->status.connecting = false;
266 void do_outgoing_connection(connection_t *c) {
267 char *address, *port;
273 if(!c->outgoing->ai) {
274 if(!c->outgoing->cfg) {
275 ifdebug(CONNECTIONS) logger(LOG_ERR, _("Could not set up a meta connection to %s"),
277 c->status.remove = true;
278 retry_outgoing(c->outgoing);
282 get_config_string(c->outgoing->cfg, &address);
284 if(!get_config_string(lookup_config(c->config_tree, "Port"), &port))
285 asprintf(&port, "655");
287 c->outgoing->ai = str2addrinfo(address, port, SOCK_STREAM);
291 c->outgoing->aip = c->outgoing->ai;
292 c->outgoing->cfg = lookup_config_next(c->config_tree, c->outgoing->cfg);
295 if(!c->outgoing->aip) {
296 freeaddrinfo(c->outgoing->ai);
297 c->outgoing->ai = NULL;
301 memcpy(&c->address, c->outgoing->aip->ai_addr, c->outgoing->aip->ai_addrlen);
302 c->outgoing->aip = c->outgoing->aip->ai_next;
307 c->hostname = sockaddr2hostname(&c->address);
309 ifdebug(CONNECTIONS) logger(LOG_INFO, _("Trying to connect to %s (%s)"), c->name,
312 c->socket = socket(c->address.sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
314 if(c->socket == -1) {
315 ifdebug(CONNECTIONS) logger(LOG_ERR, _("Creating socket for %s failed: %s"), c->hostname,
321 /* Optimize TCP settings */
327 result = connect(c->socket, &c->address.sa, SALEN(c->address.sa));
330 if(errno == EINPROGRESS
331 #if defined(WIN32) && !defined(O_NONBLOCK)
332 || WSAGetLastError() == WSAEWOULDBLOCK
335 c->status.connecting = true;
339 closesocket(c->socket);
341 ifdebug(CONNECTIONS) logger(LOG_ERR, _("%s: %s"), c->hostname, strerror(errno));
346 finish_connecting(c);
351 void setup_outgoing_connection(outgoing_t *outgoing) {
357 n = lookup_node(outgoing->name);
361 ifdebug(CONNECTIONS) logger(LOG_INFO, _("Already connected to %s"), outgoing->name);
363 n->connection->outgoing = outgoing;
367 c = new_connection();
368 c->name = xstrdup(outgoing->name);
369 c->outcipher = myself->connection->outcipher;
370 c->outdigest = myself->connection->outdigest;
371 c->outmaclength = myself->connection->outmaclength;
372 c->outcompression = myself->connection->outcompression;
374 init_configuration(&c->config_tree);
375 read_connection_config(c);
377 outgoing->cfg = lookup_config(c->config_tree, "Address");
380 logger(LOG_ERR, _("No address specified for %s"), c->name);
382 free(outgoing->name);
387 c->outgoing = outgoing;
388 c->last_ping_time = time(NULL);
392 do_outgoing_connection(c);
394 event_set(&c->ev, c->socket, EV_READ | EV_PERSIST, handle_meta_connection_data, c);
395 event_set(&c->outev, c->socket, EV_WRITE | EV_PERSIST, flush_meta, c);
396 if(event_add(&c->ev, NULL) < 0) {
397 logger(LOG_EMERG, _("event_add failed: %s"), strerror(errno));
403 accept a new tcp connect and create a
406 void handle_new_meta_connection(int sock, short events, void *data) {
410 socklen_t len = sizeof(sa);
414 fd = accept(sock, &sa.sa, &len);
417 logger(LOG_ERR, _("Accepting a new connection failed: %s"), strerror(errno));
423 c = new_connection();
424 c->name = xstrdup("<unknown>");
425 c->outcipher = myself->connection->outcipher;
426 c->outdigest = myself->connection->outdigest;
427 c->outmaclength = myself->connection->outmaclength;
428 c->outcompression = myself->connection->outcompression;
431 c->hostname = sockaddr2hostname(&sa);
433 c->last_ping_time = time(NULL);
435 ifdebug(CONNECTIONS) logger(LOG_NOTICE, _("Connection from %s"), c->hostname);
437 event_set(&c->ev, c->socket, EV_READ | EV_PERSIST, handle_meta_connection_data, c);
438 event_set(&c->outev, c->socket, EV_WRITE | EV_PERSIST, flush_meta, c);
439 if(event_add(&c->ev, NULL) < 0) {
440 logger(LOG_ERR, _("event_add failed: %s"), strerror(errno));
449 c->allow_request = ID;
453 void try_outgoing_connections(void) {
454 static config_t *cfg = NULL;
456 outgoing_t *outgoing;
460 for(cfg = lookup_config(config_tree, "ConnectTo"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
461 get_config_string(cfg, &name);
463 if(!check_id(name)) {
465 _("Invalid name for outgoing connection in %s line %d"),
466 cfg->file, cfg->line);
471 outgoing = xmalloc_and_zero(sizeof(*outgoing));
472 outgoing->name = name;
473 setup_outgoing_connection(outgoing);