2 net_socket.c -- Handle various kinds of sockets.
3 Copyright (C) 1998-2002 Ivo Timmermans <itimmermans@bigfoot.com>,
4 2000-2002 Guus Sliepen <guus@sliepen.warande.net>
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.
20 $Id: net_socket.c,v 1.1.2.14 2002/06/08 14:08:57 guus Exp $
28 #include <netinet/in.h>
29 #ifdef HAVE_NETINET_IN_SYSTM_H
30 #include <netinet/in_systm.h>
32 #ifdef HAVE_NETINET_IP_H
33 #include <netinet/ip.h>
35 #ifdef HAVE_NETINET_TCP_H
36 #include <netinet/tcp.h>
43 #include <sys/types.h>
46 #include <sys/ioctl.h>
47 /* SunOS really wants sys/socket.h BEFORE net/if.h,
48 and FreeBSD wants these lines below the rest. */
49 #include <arpa/inet.h>
50 #include <sys/socket.h>
59 #include "connection.h"
74 #ifndef HAVE_RAND_PSEUDO_BYTES
75 #define RAND_pseudo_bytes RAND_bytes
78 int addressfamily = AF_INET;
80 int seconds_till_retry = 5;
82 listen_socket_t listen_socket[MAXSOCKETS];
83 int listen_sockets = 0;
87 int setup_listen_socket(sockaddr_t *sa)
92 #if defined(SOL_SOCKET) && defined(SO_BINDTODEVICE)
97 if((nfd = socket(sa->sa.sa_family, SOCK_STREAM, IPPROTO_TCP)) < 0)
99 syslog(LOG_ERR, _("Creating metasocket failed: %s"), strerror(errno));
103 flags = fcntl(nfd, F_GETFL);
104 if(fcntl(nfd, F_SETFL, flags | O_NONBLOCK) < 0)
107 syslog(LOG_ERR, _("System call `%s' failed: %s"), "fcntl", strerror(errno));
111 /* Optimize TCP settings */
114 setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, &option, sizeof(option));
116 #if defined(SOL_TCP) && defined(TCP_NODELAY)
117 setsockopt(nfd, SOL_TCP, TCP_NODELAY, &option, sizeof(option));
120 #if defined(SOL_IP) && defined(IP_TOS) && defined(IPTOS_LOWDELAY)
121 option = IPTOS_LOWDELAY;
122 setsockopt(nfd, SOL_IP, IP_TOS, &option, sizeof(option));
125 if(get_config_string(lookup_config(config_tree, "BindToInterface"), &interface))
127 #if defined(SOL_SOCKET) && defined(SO_BINDTODEVICE)
128 memset(&ifr, 0, sizeof(ifr));
129 strncpy(ifr.ifr_ifrn.ifrn_name, interface, IFNAMSIZ);
130 if(setsockopt(nfd, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof(ifr)))
133 syslog(LOG_ERR, _("Can't bind to interface %s: %s"), interface, strerror(errno));
137 syslog(LOG_WARNING, _("BindToDevice not supported on this platform"));
141 if(bind(nfd, &sa->sa, SALEN(sa->sa)))
144 addrstr = sockaddr2hostname(sa);
145 syslog(LOG_ERR, _("Can't bind to %s/tcp: %s"), addrstr, strerror(errno));
153 syslog(LOG_ERR, _("System call `%s' failed: %s"), "listen", strerror(errno));
160 int setup_vpn_in_socket(sockaddr_t *sa)
165 #if defined(SOL_SOCKET) && defined(SO_BINDTODEVICE)
170 if((nfd = socket(sa->sa.sa_family, SOCK_DGRAM, IPPROTO_UDP)) < 0)
172 syslog(LOG_ERR, _("Creating UDP socket failed: %s"), strerror(errno));
176 flags = fcntl(nfd, F_GETFL);
177 if(fcntl(nfd, F_SETFL, flags | O_NONBLOCK) < 0)
180 syslog(LOG_ERR, _("System call `%s' failed: %s"), "fcntl", strerror(errno));
185 setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, &option, sizeof(option));
187 #if defined(SOL_SOCKET) && defined(SO_BINDTODEVICE)
188 if(get_config_string(lookup_config(config_tree, "BindToInterface"), &interface))
190 memset(&ifr, 0, sizeof(ifr));
191 strncpy(ifr.ifr_ifrn.ifrn_name, interface, IFNAMSIZ);
192 if(setsockopt(nfd, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof(ifr)))
195 syslog(LOG_ERR, _("Can't bind to interface %s: %s"), interface, strerror(errno));
201 if(bind(nfd, &sa->sa, SALEN(sa->sa)))
204 addrstr = sockaddr2hostname(sa);
205 syslog(LOG_ERR, _("Can't bind to %s/udp: %s"), addrstr, strerror(errno));
213 void retry_outgoing(outgoing_t *outgoing)
217 outgoing->timeout += 5;
218 if(outgoing->timeout > maxtimeout)
219 outgoing->timeout = maxtimeout;
222 event->handler = (event_handler_t)setup_outgoing_connection;
223 event->time = now + outgoing->timeout;
224 event->data = outgoing;
227 if(debug_lvl >= DEBUG_CONNECTIONS)
228 syslog(LOG_NOTICE, _("Trying to re-establish outgoing connection in %d seconds"), outgoing->timeout);
232 int setup_outgoing_socket(connection_t *c)
236 if(debug_lvl >= DEBUG_CONNECTIONS)
237 syslog(LOG_INFO, _("Trying to connect to %s (%s)"), c->name, c->hostname);
239 c->socket = socket(c->address.sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
243 syslog(LOG_ERR, _("Creating socket for %s failed: %s"), c->hostname, strerror(errno));
247 /* Optimize TCP settings */
249 #if defined(SOL_TCP) && defined(TCP_NODELAY)
251 setsockopt(c->socket, SOL_TCP, TCP_NODELAY, &option, sizeof(option));
254 #if defined(SOL_IP) && defined(IP_TOS)
255 option = IPTOS_LOWDELAY;
256 setsockopt(c->socket, SOL_IP, IP_TOS, &option, sizeof(option));
261 if(connect(c->socket, &c->address.sa, SALEN(c->address.sa)) == -1)
264 syslog(LOG_ERR, _("Error while connecting to %s (%s): %s"), c->name, c->hostname, strerror(errno));
268 if(debug_lvl >= DEBUG_CONNECTIONS)
269 syslog(LOG_INFO, _("Connected to %s (%s)"), c->name, c->hostname);
275 void finish_connecting(connection_t *c)
278 if(debug_lvl >= DEBUG_CONNECTIONS)
279 syslog(LOG_INFO, _("Connected to %s (%s)"), c->name, c->hostname);
281 c->last_ping_time = now;
287 void do_outgoing_connection(connection_t *c)
289 char *address, *port;
290 int option, result, flags;
295 if(!c->outgoing->cfg)
297 if(debug_lvl >= DEBUG_CONNECTIONS)
298 syslog(LOG_ERR, _("Could not set up a meta connection to %s"), c->name);
299 c->status.remove = 1;
300 retry_outgoing(c->outgoing);
304 get_config_string(c->outgoing->cfg, &address);
306 if(!get_config_string(lookup_config(c->config_tree, "Port"), &port))
307 asprintf(&port, "655");
309 c->outgoing->ai = str2addrinfo(address, port, SOCK_STREAM);
313 c->outgoing->aip = c->outgoing->ai;
314 c->outgoing->cfg = lookup_config_next(c->config_tree, c->outgoing->cfg);
317 if(!c->outgoing->aip)
319 freeaddrinfo(c->outgoing->ai);
320 c->outgoing->ai = NULL;
324 memcpy(&c->address, c->outgoing->aip->ai_addr, c->outgoing->aip->ai_addrlen);
325 c->outgoing->aip = c->outgoing->aip->ai_next;
330 c->hostname = sockaddr2hostname(&c->address);
332 if(debug_lvl >= DEBUG_CONNECTIONS)
333 syslog(LOG_INFO, _("Trying to connect to %s (%s)"), c->name, c->hostname);
335 c->socket = socket(c->address.sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
339 if(debug_lvl >= DEBUG_CONNECTIONS)
340 syslog(LOG_ERR, _("Creating socket for %s failed: %s"), c->hostname, strerror(errno));
345 /* Optimize TCP settings */
347 #if defined(SOL_TCP) && defined(TCP_NODELAY)
349 setsockopt(c->socket, SOL_TCP, TCP_NODELAY, &option, sizeof(option));
352 #if defined(SOL_IP) && defined(IP_TOS)
353 option = IPTOS_LOWDELAY;
354 setsockopt(c->socket, SOL_IP, IP_TOS, &option, sizeof(option));
359 flags = fcntl(c->socket, F_GETFL);
361 if(fcntl(c->socket, F_SETFL, flags | O_NONBLOCK) < 0)
363 syslog(LOG_ERR, _("fcntl for %s: %s"), c->hostname, strerror(errno));
368 result = connect(c->socket, &c->address.sa, SALEN(c->address.sa));
372 if(errno == EINPROGRESS)
374 c->status.connecting = 1;
380 if(debug_lvl >= DEBUG_CONNECTIONS)
381 syslog(LOG_ERR, _("%s: %s"), c->hostname, strerror(errno));
386 finish_connecting(c);
391 void setup_outgoing_connection(outgoing_t *outgoing)
396 n = lookup_node(outgoing->name);
401 if(debug_lvl >= DEBUG_CONNECTIONS)
402 syslog(LOG_INFO, _("Already connected to %s"), outgoing->name);
403 n->connection->outgoing = outgoing;
407 c = new_connection();
408 c->name = xstrdup(outgoing->name);
409 c->outcipher = myself->connection->outcipher;
410 c->outdigest = myself->connection->outdigest;
411 c->outmaclength = myself->connection->outmaclength;
412 c->outcompression = myself->connection->outcompression;
414 init_configuration(&c->config_tree);
415 read_connection_config(c);
417 outgoing->cfg = lookup_config(c->config_tree, "Address");
421 syslog(LOG_ERR, _("No address specified for %s"), c->name);
423 free(outgoing->name);
428 c->outgoing = outgoing;
429 c->last_ping_time = now;
433 do_outgoing_connection(c);
437 accept a new tcp connect and create a
440 int handle_new_meta_connection(int sock)
444 int fd, len = sizeof(sa);
446 if((fd = accept(sock, &sa.sa, &len)) < 0)
448 syslog(LOG_ERR, _("Accepting a new connection failed: %s"), strerror(errno));
454 c = new_connection();
455 c->outcipher = myself->connection->outcipher;
456 c->outdigest = myself->connection->outdigest;
457 c->outmaclength = myself->connection->outmaclength;
458 c->outcompression = myself->connection->outcompression;
461 c->hostname = sockaddr2hostname(&sa);
463 c->last_ping_time = now;
465 if(debug_lvl >= DEBUG_CONNECTIONS)
466 syslog(LOG_NOTICE, _("Connection from %s"), c->hostname);
470 c->allow_request = ID;
476 void try_outgoing_connections(void)
478 static config_t *cfg = NULL;
480 outgoing_t *outgoing;
482 for(cfg = lookup_config(config_tree, "ConnectTo"); cfg; cfg = lookup_config_next(config_tree, cfg))
484 get_config_string(cfg, &name);
488 syslog(LOG_ERR, _("Invalid name for outgoing connection in %s line %d"), cfg->file, cfg->line);
493 outgoing = xmalloc_and_zero(sizeof(*outgoing));
494 outgoing->name = name;
495 setup_outgoing_connection(outgoing);