2 net_socket.c -- Handle various kinds of sockets.
3 Copyright (C) 1998-2005 Ivo Timmermans,
4 2000-2006 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));
67 #if defined(SOL_TCP) && defined(TCP_NODELAY)
69 setsockopt(c->socket, SOL_TCP, TCP_NODELAY, &option, sizeof(option));
72 #if defined(SOL_IP) && defined(IP_TOS) && defined(IPTOS_LOWDELAY)
73 option = IPTOS_LOWDELAY;
74 setsockopt(c->socket, SOL_IP, IP_TOS, &option, sizeof(option));
78 int setup_listen_socket(const sockaddr_t *sa)
87 nfd = socket(sa->sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
90 ifdebug(STATUS) logger(LOG_ERR, _("Creating metasocket failed: %s"), strerror(errno));
94 /* Optimize TCP settings */
97 setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, &option, sizeof(option));
100 (lookup_config(config_tree, "BindToInterface"), &iface)) {
101 #if defined(SOL_SOCKET) && defined(SO_BINDTODEVICE)
104 memset(&ifr, 0, sizeof(ifr));
105 strncpy(ifr.ifr_ifrn.ifrn_name, iface, IFNAMSIZ);
107 if(setsockopt(nfd, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof(ifr))) {
109 logger(LOG_ERR, _("Can't bind to interface %s: %s"), iface,
114 logger(LOG_WARNING, _("BindToInterface not supported on this platform"));
118 if(bind(nfd, &sa->sa, SALEN(sa->sa))) {
120 addrstr = sockaddr2hostname(sa);
121 logger(LOG_ERR, _("Can't bind to %s/tcp: %s"), addrstr,
129 logger(LOG_ERR, _("System call `%s' failed: %s"), "listen",
137 int setup_vpn_in_socket(const sockaddr_t *sa)
145 nfd = socket(sa->sa.sa_family, SOCK_DGRAM, IPPROTO_UDP);
148 logger(LOG_ERR, _("Creating UDP socket failed: %s"), strerror(errno));
154 int flags = fcntl(nfd, F_GETFL);
156 if(fcntl(nfd, F_SETFL, flags | O_NONBLOCK) < 0) {
158 logger(LOG_ERR, _("System call `%s' failed: %s"), "fcntl",
166 setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, &option, sizeof(option));
168 #if defined(SOL_IP) && defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DO)
172 if(get_config_bool(lookup_config(myself->connection->config_tree, "PMTUDiscovery"), &choice) && choice) {
173 option = IP_PMTUDISC_DO;
174 setsockopt(nfd, SOL_IP, IP_MTU_DISCOVER, &option, sizeof(option));
179 #if defined(SOL_IPV6) && defined(IPV6_MTU_DISCOVER) && defined(IPV6_PMTUDISC_DO)
183 if(get_config_bool(lookup_config(myself->connection->config_tree, "PMTUDiscovery"), &choice) && choice) {
184 option = IPV6_PMTUDISC_DO;
185 setsockopt(nfd, SOL_IPV6, IPV6_MTU_DISCOVER, &option, sizeof(option));
190 #if defined(SOL_SOCKET) && defined(SO_BINDTODEVICE)
195 if(get_config_string(lookup_config(config_tree, "BindToInterface"), &iface)) {
196 memset(&ifr, 0, sizeof(ifr));
197 strncpy(ifr.ifr_ifrn.ifrn_name, iface, IFNAMSIZ);
199 if(setsockopt(nfd, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof(ifr))) {
201 logger(LOG_ERR, _("Can't bind to interface %s: %s"), iface,
209 if(bind(nfd, &sa->sa, SALEN(sa->sa))) {
211 addrstr = sockaddr2hostname(sa);
212 logger(LOG_ERR, _("Can't bind to %s/udp: %s"), addrstr,
221 void retry_outgoing(outgoing_t *outgoing)
227 outgoing->timeout += 5;
229 if(outgoing->timeout > maxtimeout)
230 outgoing->timeout = maxtimeout;
233 event->handler = (event_handler_t) setup_outgoing_connection;
234 event->time = now + outgoing->timeout;
235 event->data = outgoing;
238 ifdebug(CONNECTIONS) logger(LOG_NOTICE,
239 _("Trying to re-establish outgoing connection in %d seconds"),
243 void finish_connecting(connection_t *c)
249 ifdebug(CONNECTIONS) logger(LOG_INFO, _("Connected to %s (%s)"), c->name, c->hostname);
253 c->last_ping_time = now;
258 void do_outgoing_connection(connection_t *c)
260 char *address, *port;
261 int option, result, flags;
266 if(!c->outgoing->ai) {
267 if(!c->outgoing->cfg) {
268 ifdebug(CONNECTIONS) logger(LOG_ERR, _("Could not set up a meta connection to %s"),
270 c->status.remove = true;
271 retry_outgoing(c->outgoing);
275 get_config_string(c->outgoing->cfg, &address);
277 if(!get_config_string(lookup_config(c->config_tree, "Port"), &port))
278 asprintf(&port, "655");
280 c->outgoing->ai = str2addrinfo(address, port, SOCK_STREAM);
284 c->outgoing->aip = c->outgoing->ai;
285 c->outgoing->cfg = lookup_config_next(c->config_tree, c->outgoing->cfg);
288 if(!c->outgoing->aip) {
289 freeaddrinfo(c->outgoing->ai);
290 c->outgoing->ai = NULL;
294 memcpy(&c->address, c->outgoing->aip->ai_addr, c->outgoing->aip->ai_addrlen);
295 c->outgoing->aip = c->outgoing->aip->ai_next;
300 c->hostname = sockaddr2hostname(&c->address);
302 ifdebug(CONNECTIONS) logger(LOG_INFO, _("Trying to connect to %s (%s)"), c->name,
305 c->socket = socket(c->address.sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
307 if(c->socket == -1) {
308 ifdebug(CONNECTIONS) logger(LOG_ERR, _("Creating socket for %s failed: %s"), c->hostname,
314 /* Optimize TCP settings */
320 result = connect(c->socket, &c->address.sa, SALEN(c->address.sa));
323 if(errno == EINPROGRESS) {
324 c->status.connecting = true;
328 closesocket(c->socket);
330 ifdebug(CONNECTIONS) logger(LOG_ERR, _("%s: %s"), c->hostname, strerror(errno));
335 finish_connecting(c);
340 void setup_outgoing_connection(outgoing_t *outgoing)
347 n = lookup_node(outgoing->name);
351 ifdebug(CONNECTIONS) logger(LOG_INFO, _("Already connected to %s"), outgoing->name);
353 n->connection->outgoing = outgoing;
357 c = new_connection();
358 c->name = xstrdup(outgoing->name);
359 c->outcipher = myself->connection->outcipher;
360 c->outdigest = myself->connection->outdigest;
361 c->outmaclength = myself->connection->outmaclength;
362 c->outcompression = myself->connection->outcompression;
364 init_configuration(&c->config_tree);
365 read_connection_config(c);
367 outgoing->cfg = lookup_config(c->config_tree, "Address");
370 logger(LOG_ERR, _("No address specified for %s"), c->name);
372 free(outgoing->name);
377 c->outgoing = outgoing;
378 c->last_ping_time = now;
382 do_outgoing_connection(c);
386 accept a new tcp connect and create a
389 bool handle_new_meta_connection(int sock)
395 socklen_t len = sizeof(sa);
399 fd = accept(sock, &sa.sa, &len);
402 logger(LOG_ERR, _("Accepting a new connection failed: %s"),
409 c = new_connection();
411 c->outcipher = myself->connection->outcipher;
412 c->outdigest = myself->connection->outdigest;
413 c->outmaclength = myself->connection->outmaclength;
414 c->outcompression = myself->connection->outcompression;
417 c->hostname = sockaddr2hostname(&sa);
419 c->last_ping_time = now;
421 ifdebug(CONNECTIONS) logger(LOG_NOTICE, _("Connection from %s"), c->hostname);
427 c->allow_request = ID;
433 void try_outgoing_connections(void)
435 static config_t *cfg = NULL;
437 outgoing_t *outgoing;
441 for(cfg = lookup_config(config_tree, "ConnectTo"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
442 get_config_string(cfg, &name);
444 if(!check_id(name)) {
446 _("Invalid name for outgoing connection in %s line %d"),
447 cfg->file, cfg->line);
452 outgoing = xmalloc_and_zero(sizeof(*outgoing));
453 outgoing->name = name;
454 setup_outgoing_connection(outgoing);