2 net_socket.c -- Handle various kinds of sockets.
3 Copyright (C) 1998-2002 Ivo Timmermans <ivo@o2w.nl>,
4 2000-2002 Guus Sliepen <guus@sliepen.eu.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.
20 $Id: net_socket.c,v 1.1.2.17 2002/09/03 20:43:25 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"
73 #ifndef HAVE_RAND_PSEUDO_BYTES
74 #define RAND_pseudo_bytes RAND_bytes
77 int addressfamily = AF_INET;
79 int seconds_till_retry = 5;
81 listen_socket_t listen_socket[MAXSOCKETS];
86 int setup_listen_socket(sockaddr_t *sa)
91 #if defined(SOL_SOCKET) && defined(SO_BINDTODEVICE)
96 if((nfd = socket(sa->sa.sa_family, SOCK_STREAM, IPPROTO_TCP)) < 0)
98 syslog(LOG_ERR, _("Creating metasocket failed: %s"), strerror(errno));
102 flags = fcntl(nfd, F_GETFL);
103 if(fcntl(nfd, F_SETFL, flags | O_NONBLOCK) < 0)
106 syslog(LOG_ERR, _("System call `%s' failed: %s"), "fcntl", strerror(errno));
110 /* Optimize TCP settings */
113 setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, &option, sizeof(option));
115 #if defined(SOL_TCP) && defined(TCP_NODELAY)
116 setsockopt(nfd, SOL_TCP, TCP_NODELAY, &option, sizeof(option));
119 #if defined(SOL_IP) && defined(IP_TOS) && defined(IPTOS_LOWDELAY)
120 option = IPTOS_LOWDELAY;
121 setsockopt(nfd, SOL_IP, IP_TOS, &option, sizeof(option));
124 if(get_config_string(lookup_config(config_tree, "BindToInterface"), &interface))
126 #if defined(SOL_SOCKET) && defined(SO_BINDTODEVICE)
127 memset(&ifr, 0, sizeof(ifr));
128 strncpy(ifr.ifr_ifrn.ifrn_name, interface, IFNAMSIZ);
129 if(setsockopt(nfd, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof(ifr)))
132 syslog(LOG_ERR, _("Can't bind to interface %s: %s"), interface, strerror(errno));
136 syslog(LOG_WARNING, _("BindToDevice not supported on this platform"));
140 if(bind(nfd, &sa->sa, SALEN(sa->sa)))
143 addrstr = sockaddr2hostname(sa);
144 syslog(LOG_ERR, _("Can't bind to %s/tcp: %s"), addrstr, strerror(errno));
152 syslog(LOG_ERR, _("System call `%s' failed: %s"), "listen", strerror(errno));
159 int setup_vpn_in_socket(sockaddr_t *sa)
164 #if defined(SOL_SOCKET) && defined(SO_BINDTODEVICE)
169 if((nfd = socket(sa->sa.sa_family, SOCK_DGRAM, IPPROTO_UDP)) < 0)
171 syslog(LOG_ERR, _("Creating UDP socket failed: %s"), strerror(errno));
175 flags = fcntl(nfd, F_GETFL);
176 if(fcntl(nfd, F_SETFL, flags | O_NONBLOCK) < 0)
179 syslog(LOG_ERR, _("System call `%s' failed: %s"), "fcntl", strerror(errno));
184 setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, &option, sizeof(option));
186 #if defined(SOL_SOCKET) && defined(SO_BINDTODEVICE)
187 if(get_config_string(lookup_config(config_tree, "BindToInterface"), &interface))
189 memset(&ifr, 0, sizeof(ifr));
190 strncpy(ifr.ifr_ifrn.ifrn_name, interface, IFNAMSIZ);
191 if(setsockopt(nfd, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof(ifr)))
194 syslog(LOG_ERR, _("Can't bind to interface %s: %s"), interface, strerror(errno));
200 if(bind(nfd, &sa->sa, SALEN(sa->sa)))
203 addrstr = sockaddr2hostname(sa);
204 syslog(LOG_ERR, _("Can't bind to %s/udp: %s"), addrstr, strerror(errno));
212 void retry_outgoing(outgoing_t *outgoing)
216 outgoing->timeout += 5;
217 if(outgoing->timeout > maxtimeout)
218 outgoing->timeout = maxtimeout;
221 event->handler = (event_handler_t)setup_outgoing_connection;
222 event->time = now + outgoing->timeout;
223 event->data = outgoing;
226 if(debug_lvl >= DEBUG_CONNECTIONS)
227 syslog(LOG_NOTICE, _("Trying to re-establish outgoing connection in %d seconds"), outgoing->timeout);
231 int setup_outgoing_socket(connection_t *c)
235 if(debug_lvl >= DEBUG_CONNECTIONS)
236 syslog(LOG_INFO, _("Trying to connect to %s (%s)"), c->name, c->hostname);
238 c->socket = socket(c->address.sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
242 syslog(LOG_ERR, _("Creating socket for %s failed: %s"), c->hostname, strerror(errno));
246 /* Optimize TCP settings */
248 #if defined(SOL_TCP) && defined(TCP_NODELAY)
250 setsockopt(c->socket, SOL_TCP, TCP_NODELAY, &option, sizeof(option));
253 #if defined(SOL_IP) && defined(IP_TOS)
254 option = IPTOS_LOWDELAY;
255 setsockopt(c->socket, SOL_IP, IP_TOS, &option, sizeof(option));
260 if(connect(c->socket, &c->address.sa, SALEN(c->address.sa)) == -1)
263 syslog(LOG_ERR, _("Error while connecting to %s (%s): %s"), c->name, c->hostname, strerror(errno));
267 if(debug_lvl >= DEBUG_CONNECTIONS)
268 syslog(LOG_INFO, _("Connected to %s (%s)"), c->name, c->hostname);
274 void finish_connecting(connection_t *c)
277 if(debug_lvl >= DEBUG_CONNECTIONS)
278 syslog(LOG_INFO, _("Connected to %s (%s)"), c->name, c->hostname);
280 c->last_ping_time = now;
286 void do_outgoing_connection(connection_t *c)
288 char *address, *port;
289 int option, result, flags;
294 if(!c->outgoing->cfg)
296 if(debug_lvl >= DEBUG_CONNECTIONS)
297 syslog(LOG_ERR, _("Could not set up a meta connection to %s"), c->name);
298 c->status.remove = 1;
299 retry_outgoing(c->outgoing);
303 get_config_string(c->outgoing->cfg, &address);
305 if(!get_config_string(lookup_config(c->config_tree, "Port"), &port))
306 asprintf(&port, "655");
308 c->outgoing->ai = str2addrinfo(address, port, SOCK_STREAM);
312 c->outgoing->aip = c->outgoing->ai;
313 c->outgoing->cfg = lookup_config_next(c->config_tree, c->outgoing->cfg);
316 if(!c->outgoing->aip)
318 freeaddrinfo(c->outgoing->ai);
319 c->outgoing->ai = NULL;
323 memcpy(&c->address, c->outgoing->aip->ai_addr, c->outgoing->aip->ai_addrlen);
324 c->outgoing->aip = c->outgoing->aip->ai_next;
329 c->hostname = sockaddr2hostname(&c->address);
331 if(debug_lvl >= DEBUG_CONNECTIONS)
332 syslog(LOG_INFO, _("Trying to connect to %s (%s)"), c->name, c->hostname);
334 c->socket = socket(c->address.sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
338 if(debug_lvl >= DEBUG_CONNECTIONS)
339 syslog(LOG_ERR, _("Creating socket for %s failed: %s"), c->hostname, strerror(errno));
344 /* Optimize TCP settings */
346 #if defined(SOL_TCP) && defined(TCP_NODELAY)
348 setsockopt(c->socket, SOL_TCP, TCP_NODELAY, &option, sizeof(option));
351 #if defined(SOL_IP) && defined(IP_TOS)
352 option = IPTOS_LOWDELAY;
353 setsockopt(c->socket, SOL_IP, IP_TOS, &option, sizeof(option));
358 flags = fcntl(c->socket, F_GETFL);
360 if(fcntl(c->socket, F_SETFL, flags | O_NONBLOCK) < 0)
362 syslog(LOG_ERR, _("fcntl for %s: %s"), c->hostname, strerror(errno));
367 result = connect(c->socket, &c->address.sa, SALEN(c->address.sa));
371 if(errno == EINPROGRESS)
373 c->status.connecting = 1;
379 if(debug_lvl >= DEBUG_CONNECTIONS)
380 syslog(LOG_ERR, _("%s: %s"), c->hostname, strerror(errno));
385 finish_connecting(c);
390 void setup_outgoing_connection(outgoing_t *outgoing)
395 n = lookup_node(outgoing->name);
400 if(debug_lvl >= DEBUG_CONNECTIONS)
401 syslog(LOG_INFO, _("Already connected to %s"), outgoing->name);
402 n->connection->outgoing = outgoing;
406 c = new_connection();
407 c->name = xstrdup(outgoing->name);
408 c->outcipher = myself->connection->outcipher;
409 c->outdigest = myself->connection->outdigest;
410 c->outmaclength = myself->connection->outmaclength;
411 c->outcompression = myself->connection->outcompression;
413 init_configuration(&c->config_tree);
414 read_connection_config(c);
416 outgoing->cfg = lookup_config(c->config_tree, "Address");
420 syslog(LOG_ERR, _("No address specified for %s"), c->name);
422 free(outgoing->name);
427 c->outgoing = outgoing;
428 c->last_ping_time = now;
432 do_outgoing_connection(c);
436 accept a new tcp connect and create a
439 int handle_new_meta_connection(int sock)
443 int fd, len = sizeof(sa);
445 if((fd = accept(sock, &sa.sa, &len)) < 0)
447 syslog(LOG_ERR, _("Accepting a new connection failed: %s"), strerror(errno));
453 c = new_connection();
454 c->outcipher = myself->connection->outcipher;
455 c->outdigest = myself->connection->outdigest;
456 c->outmaclength = myself->connection->outmaclength;
457 c->outcompression = myself->connection->outcompression;
460 c->hostname = sockaddr2hostname(&sa);
462 c->last_ping_time = now;
464 if(debug_lvl >= DEBUG_CONNECTIONS)
465 syslog(LOG_NOTICE, _("Connection from %s"), c->hostname);
469 c->allow_request = ID;
475 void try_outgoing_connections(void)
477 static config_t *cfg = NULL;
479 outgoing_t *outgoing;
481 for(cfg = lookup_config(config_tree, "ConnectTo"); cfg; cfg = lookup_config_next(config_tree, cfg))
483 get_config_string(cfg, &name);
487 syslog(LOG_ERR, _("Invalid name for outgoing connection in %s line %d"), cfg->file, cfg->line);
492 outgoing = xmalloc_and_zero(sizeof(*outgoing));
493 outgoing->name = name;
494 setup_outgoing_connection(outgoing);