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.21 2002/09/09 22:32:44 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];
87 int setup_listen_socket(sockaddr_t *sa)
92 #if defined(SOL_SOCKET) && defined(SO_BINDTODEVICE)
99 nfd = socket(sa->sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
102 syslog(LOG_ERR, _("Creating metasocket failed: %s"), strerror(errno));
106 flags = fcntl(nfd, F_GETFL);
108 if(fcntl(nfd, F_SETFL, flags | O_NONBLOCK) < 0) {
110 syslog(LOG_ERR, _("System call `%s' failed: %s"), "fcntl",
115 /* Optimize TCP settings */
118 setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, &option, sizeof(option));
120 #if defined(SOL_TCP) && defined(TCP_NODELAY)
121 setsockopt(nfd, SOL_TCP, TCP_NODELAY, &option, sizeof(option));
124 #if defined(SOL_IP) && defined(IP_TOS) && defined(IPTOS_LOWDELAY)
125 option = IPTOS_LOWDELAY;
126 setsockopt(nfd, SOL_IP, IP_TOS, &option, sizeof(option));
130 (lookup_config(config_tree, "BindToInterface"), &interface)) {
131 #if defined(SOL_SOCKET) && defined(SO_BINDTODEVICE)
132 memset(&ifr, 0, sizeof(ifr));
133 strncpy(ifr.ifr_ifrn.ifrn_name, interface, IFNAMSIZ);
135 if(setsockopt(nfd, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof(ifr))) {
137 syslog(LOG_ERR, _("Can't bind to interface %s: %s"), interface,
142 syslog(LOG_WARNING, _("BindToDevice not supported on this platform"));
146 if(bind(nfd, &sa->sa, SALEN(sa->sa))) {
148 addrstr = sockaddr2hostname(sa);
149 syslog(LOG_ERR, _("Can't bind to %s/tcp: %s"), addrstr,
157 syslog(LOG_ERR, _("System call `%s' failed: %s"), "listen",
165 int setup_vpn_in_socket(sockaddr_t *sa)
170 #if defined(SOL_SOCKET) && defined(SO_BINDTODEVICE)
177 nfd = socket(sa->sa.sa_family, SOCK_DGRAM, IPPROTO_UDP);
180 syslog(LOG_ERR, _("Creating UDP socket failed: %s"), strerror(errno));
184 flags = fcntl(nfd, F_GETFL);
185 if(fcntl(nfd, F_SETFL, flags | O_NONBLOCK) < 0) {
187 syslog(LOG_ERR, _("System call `%s' failed: %s"), "fcntl",
193 setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, &option, sizeof(option));
195 #if defined(SOL_SOCKET) && defined(SO_BINDTODEVICE)
197 (lookup_config(config_tree, "BindToInterface"), &interface)) {
198 memset(&ifr, 0, sizeof(ifr));
199 strncpy(ifr.ifr_ifrn.ifrn_name, interface, IFNAMSIZ);
201 if(setsockopt(nfd, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof(ifr))) {
203 syslog(LOG_ERR, _("Can't bind to interface %s: %s"), interface,
210 if(bind(nfd, &sa->sa, SALEN(sa->sa))) {
212 addrstr = sockaddr2hostname(sa);
213 syslog(LOG_ERR, _("Can't bind to %s/udp: %s"), addrstr,
222 void retry_outgoing(outgoing_t *outgoing)
228 outgoing->timeout += 5;
230 if(outgoing->timeout > maxtimeout)
231 outgoing->timeout = maxtimeout;
234 event->handler = (event_handler_t) setup_outgoing_connection;
235 event->time = now + outgoing->timeout;
236 event->data = outgoing;
239 if(debug_lvl >= DEBUG_CONNECTIONS)
241 _("Trying to re-establish outgoing connection in %d seconds"),
245 int setup_outgoing_socket(connection_t *c)
251 if(debug_lvl >= DEBUG_CONNECTIONS)
252 syslog(LOG_INFO, _("Trying to connect to %s (%s)"), c->name,
255 c->socket = socket(c->address.sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
257 if(c->socket == -1) {
258 syslog(LOG_ERR, _("Creating socket for %s failed: %s"), c->hostname,
263 /* Optimize TCP settings */
265 #if defined(SOL_TCP) && defined(TCP_NODELAY)
267 setsockopt(c->socket, SOL_TCP, TCP_NODELAY, &option, sizeof(option));
270 #if defined(SOL_IP) && defined(IP_TOS)
271 option = IPTOS_LOWDELAY;
272 setsockopt(c->socket, SOL_IP, IP_TOS, &option, sizeof(option));
277 if(connect(c->socket, &c->address.sa, SALEN(c->address.sa)) == -1) {
279 syslog(LOG_ERR, _("Error while connecting to %s (%s): %s"), c->name,
280 c->hostname, strerror(errno));
284 if(debug_lvl >= DEBUG_CONNECTIONS)
285 syslog(LOG_INFO, _("Connected to %s (%s)"), c->name, c->hostname);
291 void finish_connecting(connection_t *c)
295 if(debug_lvl >= DEBUG_CONNECTIONS)
296 syslog(LOG_INFO, _("Connected to %s (%s)"), c->name, c->hostname);
298 c->last_ping_time = now;
303 void do_outgoing_connection(connection_t *c)
305 char *address, *port;
306 int option, result, flags;
311 if(!c->outgoing->ai) {
312 if(!c->outgoing->cfg) {
313 if(debug_lvl >= DEBUG_CONNECTIONS)
314 syslog(LOG_ERR, _("Could not set up a meta connection to %s"),
316 c->status.remove = 1;
317 retry_outgoing(c->outgoing);
321 get_config_string(c->outgoing->cfg, &address);
323 if(!get_config_string(lookup_config(c->config_tree, "Port"), &port))
324 asprintf(&port, "655");
326 c->outgoing->ai = str2addrinfo(address, port, SOCK_STREAM);
330 c->outgoing->aip = c->outgoing->ai;
331 c->outgoing->cfg = lookup_config_next(c->config_tree, c->outgoing->cfg);
334 if(!c->outgoing->aip) {
335 freeaddrinfo(c->outgoing->ai);
336 c->outgoing->ai = NULL;
340 memcpy(&c->address, c->outgoing->aip->ai_addr,
341 c->outgoing->aip->ai_addrlen);
342 c->outgoing->aip = c->outgoing->aip->ai_next;
347 c->hostname = sockaddr2hostname(&c->address);
349 if(debug_lvl >= DEBUG_CONNECTIONS)
350 syslog(LOG_INFO, _("Trying to connect to %s (%s)"), c->name,
353 c->socket = socket(c->address.sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
355 if(c->socket == -1) {
356 if(debug_lvl >= DEBUG_CONNECTIONS)
357 syslog(LOG_ERR, _("Creating socket for %s failed: %s"), c->hostname,
363 /* Optimize TCP settings */
365 #if defined(SOL_TCP) && defined(TCP_NODELAY)
367 setsockopt(c->socket, SOL_TCP, TCP_NODELAY, &option, sizeof(option));
370 #if defined(SOL_IP) && defined(IP_TOS)
371 option = IPTOS_LOWDELAY;
372 setsockopt(c->socket, SOL_IP, IP_TOS, &option, sizeof(option));
377 flags = fcntl(c->socket, F_GETFL);
379 if(fcntl(c->socket, F_SETFL, flags | O_NONBLOCK) < 0) {
380 syslog(LOG_ERR, _("fcntl for %s: %s"), c->hostname, strerror(errno));
385 result = connect(c->socket, &c->address.sa, SALEN(c->address.sa));
388 if(errno == EINPROGRESS) {
389 c->status.connecting = 1;
395 if(debug_lvl >= DEBUG_CONNECTIONS)
396 syslog(LOG_ERR, _("%s: %s"), c->hostname, strerror(errno));
401 finish_connecting(c);
406 void setup_outgoing_connection(outgoing_t *outgoing)
413 n = lookup_node(outgoing->name);
417 if(debug_lvl >= DEBUG_CONNECTIONS)
418 syslog(LOG_INFO, _("Already connected to %s"), outgoing->name);
420 n->connection->outgoing = outgoing;
424 c = new_connection();
425 c->name = xstrdup(outgoing->name);
426 c->outcipher = myself->connection->outcipher;
427 c->outdigest = myself->connection->outdigest;
428 c->outmaclength = myself->connection->outmaclength;
429 c->outcompression = myself->connection->outcompression;
431 init_configuration(&c->config_tree);
432 read_connection_config(c);
434 outgoing->cfg = lookup_config(c->config_tree, "Address");
437 syslog(LOG_ERR, _("No address specified for %s"), c->name);
439 free(outgoing->name);
444 c->outgoing = outgoing;
445 c->last_ping_time = now;
449 do_outgoing_connection(c);
453 accept a new tcp connect and create a
456 int handle_new_meta_connection(int sock)
460 int fd, len = sizeof(sa);
464 fd = accept(sock, &sa.sa, &len);
467 syslog(LOG_ERR, _("Accepting a new connection failed: %s"),
474 c = new_connection();
475 c->outcipher = myself->connection->outcipher;
476 c->outdigest = myself->connection->outdigest;
477 c->outmaclength = myself->connection->outmaclength;
478 c->outcompression = myself->connection->outcompression;
481 c->hostname = sockaddr2hostname(&sa);
483 c->last_ping_time = now;
485 if(debug_lvl >= DEBUG_CONNECTIONS)
486 syslog(LOG_NOTICE, _("Connection from %s"), c->hostname);
490 c->allow_request = ID;
496 void try_outgoing_connections(void)
498 static config_t *cfg = NULL;
500 outgoing_t *outgoing;
504 for(cfg = lookup_config(config_tree, "ConnectTo"); cfg;
505 cfg = lookup_config_next(config_tree, cfg)) {
506 get_config_string(cfg, &name);
510 _("Invalid name for outgoing connection in %s line %d"),
511 cfg->file, cfg->line);
516 outgoing = xmalloc_and_zero(sizeof(*outgoing));
517 outgoing->name = name;
518 setup_outgoing_connection(outgoing);