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 2002/04/28 12:46:26 zarq Exp $
28 #include <netinet/in.h>
30 #include <netinet/ip.h>
31 #include <netinet/tcp.h>
38 #include <sys/types.h>
40 #include <sys/ioctl.h>
41 /* SunOS really wants sys/socket.h BEFORE net/if.h,
42 and FreeBSD wants these lines below the rest. */
43 #include <arpa/inet.h>
44 #include <sys/socket.h>
53 #include "interface.h"
54 #include "connection.h"
70 int addressfamily = AF_INET;
72 int seconds_till_retry = 5;
74 listen_socket_t listen_socket[MAXSOCKETS];
75 int listen_sockets = 0;
79 int setup_listen_socket(sockaddr_t *sa)
84 #if defined(SOL_SOCKET) && defined(SO_BINDTODEVICE)
89 if((nfd = socket(sa->sa.sa_family, SOCK_STREAM, IPPROTO_TCP)) < 0)
92 _("Creating metasocket failed: %s"),
97 flags = fcntl(nfd, F_GETFL);
98 if(fcntl(nfd, F_SETFL, flags | O_NONBLOCK) < 0)
101 syslog(LOG_ERR, _("System call `%s' failed: %s"), "fcntl", strerror(errno));
105 /* Optimize TCP settings */
108 setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, &option, sizeof(option));
110 #if defined(SOL_TCP) && defined(TCP_NODELAY)
111 setsockopt(nfd, SOL_TCP, TCP_NODELAY, &option, sizeof(option));
114 #if defined(SOL_IP) && defined(IP_TOS) && defined(IPTOS_LOWDELAY)
115 option = IPTOS_LOWDELAY;
116 setsockopt(nfd, SOL_IP, IP_TOS, &option, sizeof(option));
119 if(get_config_string(lookup_config(config_tree, "BindToInterface"), &interface))
121 #if defined(SOL_SOCKET) && defined(SO_BINDTODEVICE)
122 memset(&ifr, 0, sizeof(ifr));
123 strncpy(ifr.ifr_ifrn.ifrn_name, interface, IFNAMSIZ);
124 if(setsockopt(nfd, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof(ifr)))
127 syslog(LOG_ERR, _("Can't bind to interface %s: %s"), interface, strerror(errno));
131 syslog(LOG_WARNING, _("BindToDevice not supported on this platform"));
135 if(bind(nfd, &sa->sa, SALEN(sa->sa)))
138 addrstr = sockaddr2hostname(sa);
139 syslog(LOG_ERR, _("Can't bind to %s/tcp: %s"), addrstr, strerror(errno));
147 syslog(LOG_ERR, _("System call `%s' failed: %s"), "listen", strerror(errno));
154 int setup_vpn_in_socket(sockaddr_t *sa)
159 #if defined(SOL_SOCKET) && defined(SO_BINDTODEVICE)
164 if((nfd = socket(sa->sa.sa_family, SOCK_DGRAM, IPPROTO_UDP)) < 0)
166 syslog(LOG_ERR, _("Creating UDP socket failed: %s"), strerror(errno));
170 flags = fcntl(nfd, F_GETFL);
171 if(fcntl(nfd, F_SETFL, flags | O_NONBLOCK) < 0)
174 syslog(LOG_ERR, _("System call `%s' failed: %s"), "fcntl", strerror(errno));
179 setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, &option, sizeof(option));
181 #if defined(SOL_SOCKET) && defined(SO_BINDTODEVICE)
182 if(get_config_string(lookup_config(config_tree, "BindToInterface"), &interface))
184 memset(&ifr, 0, sizeof(ifr));
185 strncpy(ifr.ifr_ifrn.ifrn_name, interface, IFNAMSIZ);
186 if(setsockopt(nfd, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof(ifr)))
189 syslog(LOG_ERR, _("Can't bind to interface %s: %s"), interface, strerror(errno));
195 if(bind(nfd, &sa->sa, SALEN(sa->sa)))
198 addrstr = sockaddr2hostname(sa);
199 syslog(LOG_ERR, _("Can't bind to %s/udp: %s"), addrstr, strerror(errno));
207 void retry_outgoing(outgoing_t *outgoing)
211 outgoing->timeout += 5;
212 if(outgoing->timeout > maxtimeout)
213 outgoing->timeout = maxtimeout;
216 event->handler = (event_handler_t)setup_outgoing_connection;
217 event->time = now + outgoing->timeout;
218 event->data = outgoing;
221 if(debug_lvl >= DEBUG_CONNECTIONS)
222 syslog(LOG_NOTICE, _("Trying to re-establish outgoing connection in %d seconds"), outgoing->timeout);
226 int setup_outgoing_socket(connection_t *c)
230 if(debug_lvl >= DEBUG_CONNECTIONS)
231 syslog(LOG_INFO, _("Trying to connect to %s (%s)"), c->name, c->hostname);
233 c->socket = socket(c->address.sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
237 syslog(LOG_ERR, _("Creating socket for %s failed: %s"), c->hostname, strerror(errno));
241 /* Optimize TCP settings */
245 setsockopt(c->socket, SOL_TCP, TCP_NODELAY, &option, sizeof(option));
247 option = IPTOS_LOWDELAY;
248 setsockopt(c->socket, SOL_IP, IP_TOS, &option, sizeof(option));
253 if(connect(c->socket, &c->address.sa, SALEN(c->address.sa)) == -1)
256 syslog(LOG_ERR, _("Error while connecting to %s (%s): %s"), c->name, c->hostname, strerror(errno));
260 if(debug_lvl >= DEBUG_CONNECTIONS)
261 syslog(LOG_INFO, _("Connected to %s (%s)"), c->name, c->hostname);
267 void finish_connecting(connection_t *c)
270 if(debug_lvl >= DEBUG_CONNECTIONS)
271 syslog(LOG_INFO, _("Connected to %s (%s)"), c->name, c->hostname);
273 c->last_ping_time = now;
279 void do_outgoing_connection(connection_t *c)
281 char *address, *port;
282 int option, result, flags;
287 if(!c->outgoing->cfg)
289 log(DEBUG_CONNECTIONS, TLOG_ERROR,
290 _("Could not set up a meta connection to %s"),
292 c->status.remove = 1;
294 retry_outgoing(c->outgoing);
298 get_config_string(c->outgoing->cfg, &address);
300 if(!get_config_string(lookup_config(c->config_tree, "Port"), &port))
301 asprintf(&port, "655");
303 c->outgoing->ai = str2addrinfo(address, port, SOCK_STREAM);
307 c->outgoing->aip = c->outgoing->ai;
308 c->outgoing->cfg = lookup_config_next(c->config_tree, c->outgoing->cfg);
311 if(!c->outgoing->aip)
313 freeaddrinfo(c->outgoing->ai);
314 c->outgoing->ai = NULL;
318 memcpy(&c->address, c->outgoing->aip->ai_addr, c->outgoing->aip->ai_addrlen);
319 c->outgoing->aip = c->outgoing->aip->ai_next;
324 c->hostname = sockaddr2hostname(&c->address);
326 if(debug_lvl >= DEBUG_CONNECTIONS)
327 syslog(LOG_INFO, _("Trying to connect to %s (%s)"), c->name, c->hostname);
329 c->socket = socket(c->address.sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
333 if(debug_lvl >= DEBUG_CONNECTIONS)
334 syslog(LOG_ERR, _("Creating socket for %s failed: %s"), c->hostname, strerror(errno));
339 /* Optimize TCP settings */
343 setsockopt(c->socket, SOL_TCP, TCP_NODELAY, &option, sizeof(option));
345 option = IPTOS_LOWDELAY;
346 setsockopt(c->socket, SOL_IP, IP_TOS, &option, sizeof(option));
351 flags = fcntl(c->socket, F_GETFL);
353 if(fcntl(c->socket, F_SETFL, flags | O_NONBLOCK) < 0)
355 syslog(LOG_ERR, _("fcntl for %s: %s"), c->hostname, strerror(errno));
360 result = connect(c->socket, &c->address.sa, SALEN(c->address.sa));
364 if(errno == EINPROGRESS)
366 c->status.connecting = 1;
372 if(debug_lvl >= DEBUG_CONNECTIONS)
373 syslog(LOG_ERR, _("%s: %s"), c->hostname, strerror(errno));
378 finish_connecting(c);
383 void setup_outgoing_connection(outgoing_t *outgoing)
388 n = lookup_node(outgoing->name);
393 log(DEBUG_CONNECTIONS, TLOG_INFO,
394 _("Already connected to %s"),
396 n->connection->outgoing = outgoing;
400 c = new_connection();
401 c->name = xstrdup(outgoing->name);
402 c->outcipher = myself->connection->outcipher;
403 c->outdigest = myself->connection->outdigest;
404 c->outmaclength = myself->connection->outmaclength;
405 c->outcompression = myself->connection->outcompression;
407 init_configuration(&c->config_tree);
408 read_connection_config(c);
410 outgoing->cfg = lookup_config(c->config_tree, "Address");
414 syslog(LOG_ERR, _("No address specified for %s"), c->name);
416 free(outgoing->name);
421 c->outgoing = outgoing;
422 c->last_ping_time = now;
426 do_outgoing_connection(c);
430 accept a new tcp connect and create a
433 int handle_new_meta_connection(int sock)
437 int fd, len = sizeof(sa);
439 if((fd = accept(sock, &sa.sa, &len)) < 0)
441 syslog(LOG_ERR, _("Accepting a new connection failed: %s"), strerror(errno));
447 c = new_connection();
448 c->outcipher = myself->connection->outcipher;
449 c->outdigest = myself->connection->outdigest;
450 c->outmaclength = myself->connection->outmaclength;
451 c->outcompression = myself->connection->outcompression;
454 c->hostname = sockaddr2hostname(&sa);
456 c->last_ping_time = now;
458 log(DEBUG_CONNECTIONS, TLOG_NOTICE,
459 _("Connection from %s"),
464 c->allow_request = ID;
470 void try_outgoing_connections(void)
472 static config_t *cfg = NULL;
474 outgoing_t *outgoing;
476 for(cfg = lookup_config(config_tree, "ConnectTo"); cfg; cfg = lookup_config_next(config_tree, cfg))
478 get_config_string(cfg, &name);
482 syslog(LOG_ERR, _("Invalid name for outgoing connection in %s line %d"), cfg->file, cfg->line);
487 outgoing = xmalloc_and_zero(sizeof(*outgoing));
488 outgoing->name = name;
489 setup_outgoing_connection(outgoing);