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.25 2003/07/06 22:11:32 guus Exp $
28 #include <netinet/in.h>
34 #include <sys/types.h>
36 #include <sys/ioctl.h>
37 /* SunOS really wants sys/socket.h BEFORE net/if.h,
38 and FreeBSD wants these lines below the rest. */
39 #include <arpa/inet.h>
40 #include <sys/socket.h>
42 #ifdef HAVE_NETINET_IN_SYSTM_H
43 #include <netinet/in_systm.h>
45 #ifdef HAVE_NETINET_IP_H
46 #include <netinet/ip.h>
48 #ifdef HAVE_NETINET_TCP_H
49 #include <netinet/tcp.h>
58 #include "connection.h"
74 #ifndef HAVE_RAND_PSEUDO_BYTES
75 #define RAND_pseudo_bytes RAND_bytes
78 int addressfamily = AF_UNSPEC;
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 logger(DEBUG_ALWAYS, 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 logger(DEBUG_ALWAYS, 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 logger(DEBUG_ALWAYS, LOG_ERR, _("Can't bind to interface %s: %s"), interface,
142 logger(DEBUG_ALWAYS, LOG_WARNING, _("BindToInterface not supported on this platform"));
146 if(bind(nfd, &sa->sa, SALEN(sa->sa))) {
148 addrstr = sockaddr2hostname(sa);
149 logger(DEBUG_ALWAYS, LOG_ERR, _("Can't bind to %s/tcp: %s"), addrstr,
157 logger(DEBUG_ALWAYS, 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 logger(DEBUG_ALWAYS, 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 logger(DEBUG_ALWAYS, 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 logger(DEBUG_ALWAYS, LOG_ERR, _("Can't bind to interface %s: %s"), interface,
210 if(bind(nfd, &sa->sa, SALEN(sa->sa))) {
212 addrstr = sockaddr2hostname(sa);
213 logger(DEBUG_ALWAYS, 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 logger(DEBUG_CONNECTIONS, LOG_NOTICE,
240 _("Trying to re-establish outgoing connection in %d seconds"),
244 void finish_connecting(connection_t *c)
248 logger(DEBUG_CONNECTIONS, LOG_INFO, _("Connected to %s (%s)"), c->name, c->hostname);
250 c->last_ping_time = now;
255 void do_outgoing_connection(connection_t *c)
257 char *address, *port;
258 int option, result, flags;
263 if(!c->outgoing->ai) {
264 if(!c->outgoing->cfg) {
265 logger(DEBUG_CONNECTIONS, LOG_ERR, _("Could not set up a meta connection to %s"),
267 c->status.remove = 1;
268 retry_outgoing(c->outgoing);
272 get_config_string(c->outgoing->cfg, &address);
274 if(!get_config_string(lookup_config(c->config_tree, "Port"), &port))
275 asprintf(&port, "655");
277 c->outgoing->ai = str2addrinfo(address, port, SOCK_STREAM);
281 c->outgoing->aip = c->outgoing->ai;
282 c->outgoing->cfg = lookup_config_next(c->config_tree, c->outgoing->cfg);
285 if(!c->outgoing->aip) {
286 freeaddrinfo(c->outgoing->ai);
287 c->outgoing->ai = NULL;
291 memcpy(&c->address, c->outgoing->aip->ai_addr,
292 c->outgoing->aip->ai_addrlen);
293 c->outgoing->aip = c->outgoing->aip->ai_next;
298 c->hostname = sockaddr2hostname(&c->address);
300 logger(DEBUG_CONNECTIONS, LOG_INFO, _("Trying to connect to %s (%s)"), c->name,
303 c->socket = socket(c->address.sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
305 if(c->socket == -1) {
306 logger(DEBUG_CONNECTIONS, LOG_ERR, _("Creating socket for %s failed: %s"), c->hostname,
312 /* Optimize TCP settings */
314 #if defined(SOL_TCP) && defined(TCP_NODELAY)
316 setsockopt(c->socket, SOL_TCP, TCP_NODELAY, &option, sizeof(option));
319 #if defined(SOL_IP) && defined(IP_TOS)
320 option = IPTOS_LOWDELAY;
321 setsockopt(c->socket, SOL_IP, IP_TOS, &option, sizeof(option));
326 flags = fcntl(c->socket, F_GETFL);
328 if(fcntl(c->socket, F_SETFL, flags | O_NONBLOCK) < 0) {
329 logger(DEBUG_ALWAYS, LOG_ERR, _("fcntl for %s: %s"), c->hostname, strerror(errno));
334 result = connect(c->socket, &c->address.sa, SALEN(c->address.sa));
337 if(errno == EINPROGRESS) {
338 c->status.connecting = 1;
344 logger(DEBUG_CONNECTIONS, LOG_ERR, _("%s: %s"), c->hostname, strerror(errno));
349 finish_connecting(c);
354 void setup_outgoing_connection(outgoing_t *outgoing)
361 n = lookup_node(outgoing->name);
365 logger(DEBUG_CONNECTIONS, LOG_INFO, _("Already connected to %s"), outgoing->name);
367 n->connection->outgoing = outgoing;
371 c = new_connection();
372 c->name = xstrdup(outgoing->name);
373 c->outcipher = myself->connection->outcipher;
374 c->outdigest = myself->connection->outdigest;
375 c->outmaclength = myself->connection->outmaclength;
376 c->outcompression = myself->connection->outcompression;
378 init_configuration(&c->config_tree);
379 read_connection_config(c);
381 outgoing->cfg = lookup_config(c->config_tree, "Address");
384 logger(DEBUG_ALWAYS, LOG_ERR, _("No address specified for %s"), c->name);
386 free(outgoing->name);
391 c->outgoing = outgoing;
392 c->last_ping_time = now;
396 do_outgoing_connection(c);
400 accept a new tcp connect and create a
403 int handle_new_meta_connection(int sock)
407 int fd, len = sizeof(sa);
411 fd = accept(sock, &sa.sa, &len);
414 logger(DEBUG_ALWAYS, LOG_ERR, _("Accepting a new connection failed: %s"),
421 c = new_connection();
422 c->outcipher = myself->connection->outcipher;
423 c->outdigest = myself->connection->outdigest;
424 c->outmaclength = myself->connection->outmaclength;
425 c->outcompression = myself->connection->outcompression;
428 c->hostname = sockaddr2hostname(&sa);
430 c->last_ping_time = now;
432 logger(DEBUG_CONNECTIONS, LOG_NOTICE, _("Connection from %s"), c->hostname);
436 c->allow_request = ID;
442 void try_outgoing_connections(void)
444 static config_t *cfg = NULL;
446 outgoing_t *outgoing;
450 for(cfg = lookup_config(config_tree, "ConnectTo"); cfg;
451 cfg = lookup_config_next(config_tree, cfg)) {
452 get_config_string(cfg, &name);
455 logger(DEBUG_ALWAYS, LOG_ERR,
456 _("Invalid name for outgoing connection in %s line %d"),
457 cfg->file, cfg->line);
462 outgoing = xmalloc_and_zero(sizeof(*outgoing));
463 outgoing->name = name;
464 setup_outgoing_connection(outgoing);