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.12 2002/04/18 20:09:05 zarq Exp $
28 #include <netinet/in.h>
30 #include <netinet/ip.h>
31 #include <netinet/tcp.h>
38 #include <sys/types.h>
41 #include <sys/ioctl.h>
42 /* SunOS really wants sys/socket.h BEFORE net/if.h,
43 and FreeBSD wants these lines below the rest. */
44 #include <arpa/inet.h>
45 #include <sys/socket.h>
54 #include "connection.h"
69 #ifndef HAVE_RAND_PSEUDO_BYTES
70 #define RAND_pseudo_bytes RAND_bytes
73 int addressfamily = AF_INET;
75 int seconds_till_retry = 5;
77 listen_socket_t listen_socket[MAXSOCKETS];
78 int listen_sockets = 0;
82 int setup_listen_socket(sockaddr_t *sa)
87 #if defined(SOL_SOCKET) && defined(SO_BINDTODEVICE)
92 if((nfd = socket(sa->sa.sa_family, SOCK_STREAM, IPPROTO_TCP)) < 0)
94 syslog(LOG_ERR, _("Creating metasocket failed: %s"), strerror(errno));
98 flags = fcntl(nfd, F_GETFL);
99 if(fcntl(nfd, F_SETFL, flags | O_NONBLOCK) < 0)
102 syslog(LOG_ERR, _("System call `%s' failed: %s"), "fcntl", strerror(errno));
106 /* Optimize TCP settings */
109 setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, &option, sizeof(option));
111 #if defined(SOL_TCP) && defined(TCP_NODELAY)
112 setsockopt(nfd, SOL_TCP, TCP_NODELAY, &option, sizeof(option));
115 #if defined(SOL_IP) && defined(IP_TOS) && defined(IPTOS_LOWDELAY)
116 option = IPTOS_LOWDELAY;
117 setsockopt(nfd, SOL_IP, IP_TOS, &option, sizeof(option));
120 if(get_config_string(lookup_config(config_tree, "BindToInterface"), &interface))
122 #if defined(SOL_SOCKET) && defined(SO_BINDTODEVICE)
123 memset(&ifr, 0, sizeof(ifr));
124 strncpy(ifr.ifr_ifrn.ifrn_name, interface, IFNAMSIZ);
125 if(setsockopt(nfd, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof(ifr)))
128 syslog(LOG_ERR, _("Can't bind to interface %s: %s"), interface, strerror(errno));
132 syslog(LOG_WARNING, _("BindToDevice not supported on this platform"));
136 if(bind(nfd, &sa->sa, SALEN(sa->sa)))
139 addrstr = sockaddr2hostname(sa);
140 syslog(LOG_ERR, _("Can't bind to %s/tcp: %s"), addrstr, strerror(errno));
148 syslog(LOG_ERR, _("System call `%s' failed: %s"), "listen", strerror(errno));
155 int setup_vpn_in_socket(sockaddr_t *sa)
160 #if defined(SOL_SOCKET) && defined(SO_BINDTODEVICE)
165 if((nfd = socket(sa->sa.sa_family, SOCK_DGRAM, IPPROTO_UDP)) < 0)
167 syslog(LOG_ERR, _("Creating UDP socket failed: %s"), strerror(errno));
171 flags = fcntl(nfd, F_GETFL);
172 if(fcntl(nfd, F_SETFL, flags | O_NONBLOCK) < 0)
175 syslog(LOG_ERR, _("System call `%s' failed: %s"), "fcntl", strerror(errno));
180 setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, &option, sizeof(option));
182 #if defined(SOL_SOCKET) && defined(SO_BINDTODEVICE)
183 if(get_config_string(lookup_config(config_tree, "BindToInterface"), &interface))
185 memset(&ifr, 0, sizeof(ifr));
186 strncpy(ifr.ifr_ifrn.ifrn_name, interface, IFNAMSIZ);
187 if(setsockopt(nfd, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof(ifr)))
190 syslog(LOG_ERR, _("Can't bind to interface %s: %s"), interface, strerror(errno));
196 if(bind(nfd, &sa->sa, SALEN(sa->sa)))
199 addrstr = sockaddr2hostname(sa);
200 syslog(LOG_ERR, _("Can't bind to %s/udp: %s"), addrstr, strerror(errno));
208 void retry_outgoing(outgoing_t *outgoing)
212 outgoing->timeout += 5;
213 if(outgoing->timeout > maxtimeout)
214 outgoing->timeout = maxtimeout;
217 event->handler = (event_handler_t)setup_outgoing_connection;
218 event->time = now + outgoing->timeout;
219 event->data = outgoing;
222 if(debug_lvl >= DEBUG_CONNECTIONS)
223 syslog(LOG_NOTICE, _("Trying to re-establish outgoing connection in %d seconds"), outgoing->timeout);
227 int setup_outgoing_socket(connection_t *c)
231 if(debug_lvl >= DEBUG_CONNECTIONS)
232 syslog(LOG_INFO, _("Trying to connect to %s (%s)"), c->name, c->hostname);
234 c->socket = socket(c->address.sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
238 syslog(LOG_ERR, _("Creating socket for %s failed: %s"), c->hostname, strerror(errno));
242 /* Optimize TCP settings */
246 setsockopt(c->socket, SOL_TCP, TCP_NODELAY, &option, sizeof(option));
248 option = IPTOS_LOWDELAY;
249 setsockopt(c->socket, SOL_IP, IP_TOS, &option, sizeof(option));
254 if(connect(c->socket, &c->address.sa, SALEN(c->address.sa)) == -1)
257 syslog(LOG_ERR, _("Error while connecting to %s (%s): %s"), c->name, c->hostname, strerror(errno));
261 if(debug_lvl >= DEBUG_CONNECTIONS)
262 syslog(LOG_INFO, _("Connected to %s (%s)"), c->name, c->hostname);
268 void finish_connecting(connection_t *c)
271 if(debug_lvl >= DEBUG_CONNECTIONS)
272 syslog(LOG_INFO, _("Connected to %s (%s)"), c->name, c->hostname);
274 c->last_ping_time = now;
280 void do_outgoing_connection(connection_t *c)
282 char *address, *port;
283 int option, result, flags;
288 if(!c->outgoing->cfg)
290 if(debug_lvl >= DEBUG_CONNECTIONS)
291 syslog(LOG_ERR, _("Could not set up a meta connection to %s"), c->name);
292 c->status.remove = 1;
293 retry_outgoing(c->outgoing);
297 get_config_string(c->outgoing->cfg, &address);
299 if(!get_config_string(lookup_config(c->config_tree, "Port"), &port))
300 asprintf(&port, "655");
302 c->outgoing->ai = str2addrinfo(address, port, SOCK_STREAM);
306 c->outgoing->aip = c->outgoing->ai;
307 c->outgoing->cfg = lookup_config_next(c->config_tree, c->outgoing->cfg);
310 if(!c->outgoing->aip)
312 freeaddrinfo(c->outgoing->ai);
313 c->outgoing->ai = NULL;
317 memcpy(&c->address, c->outgoing->aip->ai_addr, c->outgoing->aip->ai_addrlen);
318 c->outgoing->aip = c->outgoing->aip->ai_next;
323 c->hostname = sockaddr2hostname(&c->address);
325 if(debug_lvl >= DEBUG_CONNECTIONS)
326 syslog(LOG_INFO, _("Trying to connect to %s (%s)"), c->name, c->hostname);
328 c->socket = socket(c->address.sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
332 if(debug_lvl >= DEBUG_CONNECTIONS)
333 syslog(LOG_ERR, _("Creating socket for %s failed: %s"), c->hostname, strerror(errno));
338 /* Optimize TCP settings */
342 setsockopt(c->socket, SOL_TCP, TCP_NODELAY, &option, sizeof(option));
344 option = IPTOS_LOWDELAY;
345 setsockopt(c->socket, SOL_IP, IP_TOS, &option, sizeof(option));
350 flags = fcntl(c->socket, F_GETFL);
352 if(fcntl(c->socket, F_SETFL, flags | O_NONBLOCK) < 0)
354 syslog(LOG_ERR, _("fcntl for %s: %s"), c->hostname, strerror(errno));
359 result = connect(c->socket, &c->address.sa, SALEN(c->address.sa));
363 if(errno == EINPROGRESS)
365 c->status.connecting = 1;
371 if(debug_lvl >= DEBUG_CONNECTIONS)
372 syslog(LOG_ERR, _("%s: %s"), c->hostname, strerror(errno));
377 finish_connecting(c);
382 void setup_outgoing_connection(outgoing_t *outgoing)
387 n = lookup_node(outgoing->name);
392 if(debug_lvl >= DEBUG_CONNECTIONS)
393 syslog(LOG_INFO, _("Already connected to %s"), outgoing->name);
394 n->connection->outgoing = outgoing;
398 c = new_connection();
399 c->name = xstrdup(outgoing->name);
400 c->outcipher = myself->connection->outcipher;
401 c->outdigest = myself->connection->outdigest;
402 c->outmaclength = myself->connection->outmaclength;
403 c->outcompression = myself->connection->outcompression;
405 init_configuration(&c->config_tree);
406 read_connection_config(c);
408 outgoing->cfg = lookup_config(c->config_tree, "Address");
412 syslog(LOG_ERR, _("No address specified for %s"), c->name);
414 free(outgoing->name);
419 c->outgoing = outgoing;
420 c->last_ping_time = now;
424 do_outgoing_connection(c);
428 accept a new tcp connect and create a
431 int handle_new_meta_connection(int sock)
435 int fd, len = sizeof(sa);
437 if((fd = accept(sock, &sa.sa, &len)) < 0)
439 syslog(LOG_ERR, _("Accepting a new connection failed: %s"), strerror(errno));
445 c = new_connection();
446 c->outcipher = myself->connection->outcipher;
447 c->outdigest = myself->connection->outdigest;
448 c->outmaclength = myself->connection->outmaclength;
449 c->outcompression = myself->connection->outcompression;
452 c->hostname = sockaddr2hostname(&sa);
454 c->last_ping_time = now;
456 if(debug_lvl >= DEBUG_CONNECTIONS)
457 syslog(LOG_NOTICE, _("Connection from %s"), c->hostname);
461 c->allow_request = ID;
467 void try_outgoing_connections(void)
469 static config_t *cfg = NULL;
471 outgoing_t *outgoing;
473 for(cfg = lookup_config(config_tree, "ConnectTo"); cfg; cfg = lookup_config_next(config_tree, cfg))
475 get_config_string(cfg, &name);
479 syslog(LOG_ERR, _("Invalid name for outgoing connection in %s line %d"), cfg->file, cfg->line);
484 outgoing = xmalloc_and_zero(sizeof(*outgoing));
485 outgoing->name = name;
486 setup_outgoing_connection(outgoing);