2 net_socket.c -- Handle various kinds of sockets.
3 Copyright (C) 1998-2004 Ivo Timmermans <ivo@tinc-vpn.org>,
4 2000-2004 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 int addressfamily = AF_UNSPEC;
43 int seconds_till_retry = 5;
44 bool blockingtcp = false;
46 listen_socket_t listen_socket[MAXSOCKETS];
51 int setup_listen_socket(const sockaddr_t *sa)
60 nfd = socket(sa->sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
63 ifdebug(STATUS) logger(LOG_ERR, _("Creating metasocket failed: %s"), strerror(errno));
69 int flags = fcntl(nfd, F_GETFL);
71 if(fcntl(nfd, F_SETFL, flags | O_NONBLOCK) < 0) {
73 logger(LOG_ERR, _("System call `%s' failed: %s"), "fcntl",
80 /* Optimize TCP settings */
83 setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, &option, sizeof(option));
85 #if defined(SOL_TCP) && defined(TCP_NODELAY)
86 setsockopt(nfd, SOL_TCP, TCP_NODELAY, &option, sizeof(option));
89 #if defined(SOL_IP) && defined(IP_TOS) && defined(IPTOS_LOWDELAY)
90 option = IPTOS_LOWDELAY;
91 setsockopt(nfd, SOL_IP, IP_TOS, &option, sizeof(option));
95 (lookup_config(config_tree, "BindToInterface"), &iface)) {
96 #if defined(SOL_SOCKET) && defined(SO_BINDTODEVICE)
99 memset(&ifr, 0, sizeof(ifr));
100 strncpy(ifr.ifr_ifrn.ifrn_name, iface, IFNAMSIZ);
102 if(setsockopt(nfd, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof(ifr))) {
104 logger(LOG_ERR, _("Can't bind to interface %s: %s"), iface,
109 logger(LOG_WARNING, _("BindToInterface not supported on this platform"));
113 if(bind(nfd, &sa->sa, SALEN(sa->sa))) {
115 addrstr = sockaddr2hostname(sa);
116 logger(LOG_ERR, _("Can't bind to %s/tcp: %s"), addrstr,
124 logger(LOG_ERR, _("System call `%s' failed: %s"), "listen",
132 int setup_vpn_in_socket(const sockaddr_t *sa)
140 nfd = socket(sa->sa.sa_family, SOCK_DGRAM, IPPROTO_UDP);
143 logger(LOG_ERR, _("Creating UDP socket failed: %s"), strerror(errno));
149 int flags = fcntl(nfd, F_GETFL);
151 if(fcntl(nfd, F_SETFL, flags | O_NONBLOCK) < 0) {
153 logger(LOG_ERR, _("System call `%s' failed: %s"), "fcntl",
161 setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, &option, sizeof(option));
163 #if defined(SOL_IP) && defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DO)
167 if(get_config_bool(lookup_config(myself->connection->config_tree, "PMTUDiscovery"), &choice) && choice) {
168 option = IP_PMTUDISC_DO;
169 setsockopt(nfd, SOL_IP, IP_MTU_DISCOVER, &option, sizeof(option));
174 #if defined(SOL_IPV6) && defined(IPV6_MTU_DISCOVER) && defined(IPV6_PMTUDISC_DO)
178 if(get_config_bool(lookup_config(myself->connection->config_tree, "PMTUDiscovery"), &choice) && choice) {
179 option = IPV6_PMTUDISC_DO;
180 setsockopt(nfd, SOL_IPV6, IPV6_MTU_DISCOVER, &option, sizeof(option));
185 #if defined(SOL_SOCKET) && defined(SO_BINDTODEVICE)
190 if(get_config_string(lookup_config(config_tree, "BindToInterface"), &iface)) {
191 memset(&ifr, 0, sizeof(ifr));
192 strncpy(ifr.ifr_ifrn.ifrn_name, iface, IFNAMSIZ);
194 if(setsockopt(nfd, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof(ifr))) {
196 logger(LOG_ERR, _("Can't bind to interface %s: %s"), iface,
204 if(bind(nfd, &sa->sa, SALEN(sa->sa))) {
206 addrstr = sockaddr2hostname(sa);
207 logger(LOG_ERR, _("Can't bind to %s/udp: %s"), addrstr,
216 void retry_outgoing(outgoing_t *outgoing)
222 outgoing->timeout += 5;
224 if(outgoing->timeout > maxtimeout)
225 outgoing->timeout = maxtimeout;
228 event->handler = (event_handler_t) setup_outgoing_connection;
229 event->time = now + outgoing->timeout;
230 event->data = outgoing;
233 ifdebug(CONNECTIONS) logger(LOG_NOTICE,
234 _("Trying to re-establish outgoing connection in %d seconds"),
238 void finish_connecting(connection_t *c)
242 ifdebug(CONNECTIONS) logger(LOG_INFO, _("Connected to %s (%s)"), c->name, c->hostname);
246 int flags = fcntl(c->socket, F_GETFL);
248 if(fcntl(c->socket, F_SETFL, flags & ~O_NONBLOCK) < 0) {
249 logger(LOG_ERR, _("fcntl for %s: %s"), c->hostname, strerror(errno));
254 c->last_ping_time = now;
259 void do_outgoing_connection(connection_t *c)
261 char *address, *port;
262 int option, result, flags;
267 if(!c->outgoing->ai) {
268 if(!c->outgoing->cfg) {
269 ifdebug(CONNECTIONS) logger(LOG_ERR, _("Could not set up a meta connection to %s"),
271 c->status.remove = true;
272 retry_outgoing(c->outgoing);
276 get_config_string(c->outgoing->cfg, &address);
278 if(!get_config_string(lookup_config(c->config_tree, "Port"), &port))
279 asprintf(&port, "655");
281 c->outgoing->ai = str2addrinfo(address, port, SOCK_STREAM);
285 c->outgoing->aip = c->outgoing->ai;
286 c->outgoing->cfg = lookup_config_next(c->config_tree, c->outgoing->cfg);
289 if(!c->outgoing->aip) {
290 freeaddrinfo(c->outgoing->ai);
291 c->outgoing->ai = NULL;
295 memcpy(&c->address, c->outgoing->aip->ai_addr, c->outgoing->aip->ai_addrlen);
296 c->outgoing->aip = c->outgoing->aip->ai_next;
301 c->hostname = sockaddr2hostname(&c->address);
303 ifdebug(CONNECTIONS) logger(LOG_INFO, _("Trying to connect to %s (%s)"), c->name,
306 c->socket = socket(c->address.sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
308 if(c->socket == -1) {
309 ifdebug(CONNECTIONS) logger(LOG_ERR, _("Creating socket for %s failed: %s"), c->hostname,
315 /* Optimize TCP settings */
317 #if defined(SOL_TCP) && defined(TCP_NODELAY)
319 setsockopt(c->socket, SOL_TCP, TCP_NODELAY, &option, sizeof(option));
322 #if defined(SOL_IP) && defined(IP_TOS)
323 option = IPTOS_LOWDELAY;
324 setsockopt(c->socket, SOL_IP, IP_TOS, &option, sizeof(option));
330 flags = fcntl(c->socket, F_GETFL);
332 if(fcntl(c->socket, F_SETFL, flags | O_NONBLOCK) < 0) {
333 logger(LOG_ERR, _("fcntl for %s: %s"), c->hostname, strerror(errno));
339 result = connect(c->socket, &c->address.sa, SALEN(c->address.sa));
342 if(errno == EINPROGRESS) {
343 c->status.connecting = true;
347 closesocket(c->socket);
349 ifdebug(CONNECTIONS) logger(LOG_ERR, _("%s: %s"), c->hostname, strerror(errno));
354 finish_connecting(c);
359 void setup_outgoing_connection(outgoing_t *outgoing)
366 n = lookup_node(outgoing->name);
370 ifdebug(CONNECTIONS) logger(LOG_INFO, _("Already connected to %s"), outgoing->name);
372 n->connection->outgoing = outgoing;
376 c = new_connection();
377 c->name = xstrdup(outgoing->name);
378 c->outcipher = myself->connection->outcipher;
379 c->outdigest = myself->connection->outdigest;
380 c->outmaclength = myself->connection->outmaclength;
381 c->outcompression = myself->connection->outcompression;
383 init_configuration(&c->config_tree);
384 read_connection_config(c);
386 outgoing->cfg = lookup_config(c->config_tree, "Address");
389 logger(LOG_ERR, _("No address specified for %s"), c->name);
391 free(outgoing->name);
396 c->outgoing = outgoing;
397 c->last_ping_time = now;
401 do_outgoing_connection(c);
405 accept a new tcp connect and create a
408 bool handle_new_meta_connection(int sock)
412 int fd, 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->outcipher = myself->connection->outcipher;
428 c->outdigest = myself->connection->outdigest;
429 c->outmaclength = myself->connection->outmaclength;
430 c->outcompression = myself->connection->outcompression;
433 c->hostname = sockaddr2hostname(&sa);
435 c->last_ping_time = now;
437 ifdebug(CONNECTIONS) logger(LOG_NOTICE, _("Connection from %s"), c->hostname);
441 int flags = fcntl(c->socket, F_GETFL);
443 if(fcntl(c->socket, F_SETFL, flags & ~O_NONBLOCK) < 0) {
444 logger(LOG_ERR, _("fcntl for %s: %s"), c->hostname, strerror(errno));
451 c->allow_request = ID;
457 void try_outgoing_connections(void)
459 static config_t *cfg = NULL;
461 outgoing_t *outgoing;
465 for(cfg = lookup_config(config_tree, "ConnectTo"); cfg;
466 cfg = lookup_config_next(config_tree, cfg)) {
467 get_config_string(cfg, &name);
469 if(!check_id(name)) {
471 _("Invalid name for outgoing connection in %s line %d"),
472 cfg->file, cfg->line);
477 outgoing = xmalloc_and_zero(sizeof(*outgoing));
478 outgoing->name = name;
479 setup_outgoing_connection(outgoing);