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.2 2002/04/09 15:26:00 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 int addressfamily = AF_INET;
71 int seconds_till_retry = 5;
73 listen_socket_t listen_socket[MAXSOCKETS];
74 int listen_sockets = 0;
78 int setup_listen_socket(sockaddr_t *sa)
83 #if defined(SOL_SOCKET) && defined(SO_BINDTODEVICE)
88 if((nfd = socket(sa->sa.sa_family, SOCK_STREAM, IPPROTO_TCP)) < 0)
90 syslog(LOG_ERR, _("Creating metasocket failed: %s"), strerror(errno));
94 flags = fcntl(nfd, F_GETFL);
95 if(fcntl(nfd, F_SETFL, flags | O_NONBLOCK) < 0)
98 syslog(LOG_ERR, _("System call `%s' failed: %s"), "fcntl", strerror(errno));
102 /* Optimize TCP settings */
105 setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, &option, sizeof(option));
107 #if defined(SOL_TCP) && defined(TCP_NODELAY)
108 setsockopt(nfd, SOL_TCP, TCP_NODELAY, &option, sizeof(option));
111 #if defined(SOL_IP) && defined(IP_TOS) && defined(IPTOS_LOWDELAY)
112 option = IPTOS_LOWDELAY;
113 setsockopt(nfd, SOL_IP, IP_TOS, &option, sizeof(option));
116 if(get_config_string(lookup_config(config_tree, "BindToInterface"), &interface))
118 #if defined(SOL_SOCKET) && defined(SO_BINDTODEVICE)
119 memset(&ifr, 0, sizeof(ifr));
120 strncpy(ifr.ifr_ifrn.ifrn_name, interface, IFNAMSIZ);
121 if(setsockopt(nfd, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof(ifr)))
124 syslog(LOG_ERR, _("Can't bind to interface %s: %s"), interface, strerror(errno));
128 syslog(LOG_WARNING, _("BindToDevice not supported on this platform"));
132 if(bind(nfd, &sa->sa, SALEN(sa->sa)))
135 addrstr = sockaddr2hostname(sa);
136 syslog(LOG_ERR, _("Can't bind to %s/tcp: %s"), addrstr, strerror(errno));
144 syslog(LOG_ERR, _("System call `%s' failed: %s"), "listen", strerror(errno));
151 int setup_vpn_in_socket(sockaddr_t *sa)
156 #if defined(SOL_SOCKET) && defined(SO_BINDTODEVICE)
161 if((nfd = socket(sa->sa.sa_family, SOCK_DGRAM, IPPROTO_UDP)) < 0)
163 syslog(LOG_ERR, _("Creating UDP socket failed: %s"), strerror(errno));
167 flags = fcntl(nfd, F_GETFL);
168 if(fcntl(nfd, F_SETFL, flags | O_NONBLOCK) < 0)
171 syslog(LOG_ERR, _("System call `%s' failed: %s"), "fcntl", strerror(errno));
176 setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, &option, sizeof(option));
178 #if defined(SOL_SOCKET) && defined(SO_BINDTODEVICE)
179 if(get_config_string(lookup_config(config_tree, "BindToInterface"), &interface))
181 memset(&ifr, 0, sizeof(ifr));
182 strncpy(ifr.ifr_ifrn.ifrn_name, interface, IFNAMSIZ);
183 if(setsockopt(nfd, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof(ifr)))
186 syslog(LOG_ERR, _("Can't bind to interface %s: %s"), interface, strerror(errno));
192 if(bind(nfd, &sa->sa, SALEN(sa->sa)))
195 addrstr = sockaddr2hostname(sa);
196 syslog(LOG_ERR, _("Can't bind to %s/udp: %s"), addrstr, strerror(errno));
204 void retry_outgoing(outgoing_t *outgoing)
208 outgoing->timeout += 5;
209 if(outgoing->timeout > maxtimeout)
210 outgoing->timeout = maxtimeout;
213 event->handler = (event_handler_t)setup_outgoing_connection;
214 event->time = now + outgoing->timeout;
215 event->data = outgoing;
218 if(debug_lvl >= DEBUG_CONNECTIONS)
219 syslog(LOG_NOTICE, _("Trying to re-establish outgoing connection in %d seconds"), outgoing->timeout);
223 int setup_outgoing_socket(connection_t *c)
227 if(debug_lvl >= DEBUG_CONNECTIONS)
228 syslog(LOG_INFO, _("Trying to connect to %s (%s)"), c->name, c->hostname);
230 c->socket = socket(c->address.sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
234 syslog(LOG_ERR, _("Creating socket for %s failed: %s"), c->hostname, strerror(errno));
238 /* Optimize TCP settings */
242 setsockopt(c->socket, SOL_TCP, TCP_NODELAY, &option, sizeof(option));
244 option = IPTOS_LOWDELAY;
245 setsockopt(c->socket, SOL_IP, IP_TOS, &option, sizeof(option));
250 if(connect(c->socket, &c->address.sa, SALEN(c->address.sa)) == -1)
253 syslog(LOG_ERR, _("Error while connecting to %s (%s): %s"), c->name, c->hostname, strerror(errno));
257 if(debug_lvl >= DEBUG_CONNECTIONS)
258 syslog(LOG_INFO, _("Connected to %s (%s)"), c->name, c->hostname);
264 void finish_connecting(connection_t *c)
267 if(debug_lvl >= DEBUG_CONNECTIONS)
268 syslog(LOG_INFO, _("Connected to %s (%s)"), c->name, c->hostname);
270 c->last_ping_time = now;
276 void do_outgoing_connection(connection_t *c)
278 char *address, *port;
279 int option, result, flags;
284 if(!c->outgoing->cfg)
286 if(debug_lvl >= DEBUG_CONNECTIONS)
287 syslog(LOG_ERR, _("Could not set up a meta connection to %s"), c->name);
288 c->status.remove = 1;
289 retry_outgoing(c->outgoing);
293 get_config_string(c->outgoing->cfg, &address);
295 if(!get_config_string(lookup_config(c->config_tree, "Port"), &port))
296 asprintf(&port, "655");
298 c->outgoing->ai = str2addrinfo(address, port, SOCK_STREAM);
302 c->outgoing->aip = c->outgoing->ai;
303 c->outgoing->cfg = lookup_config_next(c->config_tree, c->outgoing->cfg);
306 if(!c->outgoing->aip)
308 freeaddrinfo(c->outgoing->ai);
309 c->outgoing->ai = NULL;
313 memcpy(&c->address, c->outgoing->aip->ai_addr, c->outgoing->aip->ai_addrlen);
314 c->outgoing->aip = c->outgoing->aip->ai_next;
319 c->hostname = sockaddr2hostname(&c->address);
321 if(debug_lvl >= DEBUG_CONNECTIONS)
322 syslog(LOG_INFO, _("Trying to connect to %s (%s)"), c->name, c->hostname);
324 c->socket = socket(c->address.sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
328 if(debug_lvl >= DEBUG_CONNECTIONS)
329 syslog(LOG_ERR, _("Creating socket for %s failed: %s"), c->hostname, strerror(errno));
334 /* Optimize TCP settings */
338 setsockopt(c->socket, SOL_TCP, TCP_NODELAY, &option, sizeof(option));
340 option = IPTOS_LOWDELAY;
341 setsockopt(c->socket, SOL_IP, IP_TOS, &option, sizeof(option));
346 flags = fcntl(c->socket, F_GETFL);
348 if(fcntl(c->socket, F_SETFL, flags | O_NONBLOCK) < 0)
350 syslog(LOG_ERR, _("fcntl for %s: %s"), c->hostname, strerror(errno));
355 result = connect(c->socket, &c->address.sa, SALEN(c->address.sa));
359 if(errno == EINPROGRESS)
361 c->status.connecting = 1;
367 if(debug_lvl >= DEBUG_CONNECTIONS)
368 syslog(LOG_ERR, _("%s: %s"), c->hostname, strerror(errno));
373 finish_connecting(c);
378 void setup_outgoing_connection(outgoing_t *outgoing)
383 n = lookup_node(outgoing->name);
388 if(debug_lvl >= DEBUG_CONNECTIONS)
389 syslog(LOG_INFO, _("Already connected to %s"), outgoing->name);
390 n->connection->outgoing = outgoing;
394 c = new_connection();
395 c->name = xstrdup(outgoing->name);
396 c->outcipher = myself->connection->outcipher;
397 c->outdigest = myself->connection->outdigest;
398 c->outmaclength = myself->connection->outmaclength;
399 c->outcompression = myself->connection->outcompression;
401 init_configuration(&c->config_tree);
402 read_connection_config(c);
404 outgoing->cfg = lookup_config(c->config_tree, "Address");
408 syslog(LOG_ERR, _("No address specified for %s"), c->name);
410 free(outgoing->name);
415 c->outgoing = outgoing;
416 c->last_ping_time = now;
420 do_outgoing_connection(c);
424 accept a new tcp connect and create a
427 int handle_new_meta_connection(int sock)
431 int fd, len = sizeof(sa);
433 if((fd = accept(sock, &sa.sa, &len)) < 0)
435 syslog(LOG_ERR, _("Accepting a new connection failed: %s"), strerror(errno));
441 c = new_connection();
442 c->outcipher = myself->connection->outcipher;
443 c->outdigest = myself->connection->outdigest;
444 c->outmaclength = myself->connection->outmaclength;
445 c->outcompression = myself->connection->outcompression;
448 c->hostname = sockaddr2hostname(&sa);
450 c->last_ping_time = now;
452 if(debug_lvl >= DEBUG_CONNECTIONS)
453 syslog(LOG_NOTICE, _("Connection from %s"), c->hostname);
457 c->allow_request = ID;
463 void try_outgoing_connections(void)
465 static config_t *cfg = NULL;
467 outgoing_t *outgoing;
469 for(cfg = lookup_config(config_tree, "ConnectTo"); cfg; cfg = lookup_config_next(config_tree, cfg))
471 get_config_string(cfg, &name);
475 syslog(LOG_ERR, _("Invalid name for outgoing connection in %s line %d"), cfg->file, cfg->line);
480 outgoing = xmalloc_and_zero(sizeof(*outgoing));
481 outgoing->name = name;
482 setup_outgoing_connection(outgoing);