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.23 2003/01/17 00:37:20 guus Exp $
28 #include <netinet/in.h>
34 #include <sys/types.h>
37 #include <sys/ioctl.h>
38 /* SunOS really wants sys/socket.h BEFORE net/if.h,
39 and FreeBSD wants these lines below the rest. */
40 #include <arpa/inet.h>
41 #include <sys/socket.h>
43 #ifdef HAVE_NETINET_IN_SYSTM_H
44 #include <netinet/in_systm.h>
46 #ifdef HAVE_NETINET_IP_H
47 #include <netinet/ip.h>
49 #ifdef HAVE_NETINET_TCP_H
50 #include <netinet/tcp.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, _("BindToInterface 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 void finish_connecting(connection_t *c)
249 if(debug_lvl >= DEBUG_CONNECTIONS)
250 syslog(LOG_INFO, _("Connected to %s (%s)"), c->name, c->hostname);
252 c->last_ping_time = now;
257 void do_outgoing_connection(connection_t *c)
259 char *address, *port;
260 int option, result, flags;
265 if(!c->outgoing->ai) {
266 if(!c->outgoing->cfg) {
267 if(debug_lvl >= DEBUG_CONNECTIONS)
268 syslog(LOG_ERR, _("Could not set up a meta connection to %s"),
270 c->status.remove = 1;
271 retry_outgoing(c->outgoing);
275 get_config_string(c->outgoing->cfg, &address);
277 if(!get_config_string(lookup_config(c->config_tree, "Port"), &port))
278 asprintf(&port, "655");
280 c->outgoing->ai = str2addrinfo(address, port, SOCK_STREAM);
284 c->outgoing->aip = c->outgoing->ai;
285 c->outgoing->cfg = lookup_config_next(c->config_tree, c->outgoing->cfg);
288 if(!c->outgoing->aip) {
289 freeaddrinfo(c->outgoing->ai);
290 c->outgoing->ai = NULL;
294 memcpy(&c->address, c->outgoing->aip->ai_addr,
295 c->outgoing->aip->ai_addrlen);
296 c->outgoing->aip = c->outgoing->aip->ai_next;
301 c->hostname = sockaddr2hostname(&c->address);
303 if(debug_lvl >= DEBUG_CONNECTIONS)
304 syslog(LOG_INFO, _("Trying to connect to %s (%s)"), c->name,
307 c->socket = socket(c->address.sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
309 if(c->socket == -1) {
310 if(debug_lvl >= DEBUG_CONNECTIONS)
311 syslog(LOG_ERR, _("Creating socket for %s failed: %s"), c->hostname,
317 /* Optimize TCP settings */
319 #if defined(SOL_TCP) && defined(TCP_NODELAY)
321 setsockopt(c->socket, SOL_TCP, TCP_NODELAY, &option, sizeof(option));
324 #if defined(SOL_IP) && defined(IP_TOS)
325 option = IPTOS_LOWDELAY;
326 setsockopt(c->socket, SOL_IP, IP_TOS, &option, sizeof(option));
331 flags = fcntl(c->socket, F_GETFL);
333 if(fcntl(c->socket, F_SETFL, flags | O_NONBLOCK) < 0) {
334 syslog(LOG_ERR, _("fcntl for %s: %s"), c->hostname, strerror(errno));
339 result = connect(c->socket, &c->address.sa, SALEN(c->address.sa));
342 if(errno == EINPROGRESS) {
343 c->status.connecting = 1;
349 if(debug_lvl >= DEBUG_CONNECTIONS)
350 syslog(LOG_ERR, _("%s: %s"), c->hostname, strerror(errno));
355 finish_connecting(c);
360 void setup_outgoing_connection(outgoing_t *outgoing)
367 n = lookup_node(outgoing->name);
371 if(debug_lvl >= DEBUG_CONNECTIONS)
372 syslog(LOG_INFO, _("Already connected to %s"), outgoing->name);
374 n->connection->outgoing = outgoing;
378 c = new_connection();
379 c->name = xstrdup(outgoing->name);
380 c->outcipher = myself->connection->outcipher;
381 c->outdigest = myself->connection->outdigest;
382 c->outmaclength = myself->connection->outmaclength;
383 c->outcompression = myself->connection->outcompression;
385 init_configuration(&c->config_tree);
386 read_connection_config(c);
388 outgoing->cfg = lookup_config(c->config_tree, "Address");
391 syslog(LOG_ERR, _("No address specified for %s"), c->name);
393 free(outgoing->name);
398 c->outgoing = outgoing;
399 c->last_ping_time = now;
403 do_outgoing_connection(c);
407 accept a new tcp connect and create a
410 int handle_new_meta_connection(int sock)
414 int fd, len = sizeof(sa);
418 fd = accept(sock, &sa.sa, &len);
421 syslog(LOG_ERR, _("Accepting a new connection failed: %s"),
428 c = new_connection();
429 c->outcipher = myself->connection->outcipher;
430 c->outdigest = myself->connection->outdigest;
431 c->outmaclength = myself->connection->outmaclength;
432 c->outcompression = myself->connection->outcompression;
435 c->hostname = sockaddr2hostname(&sa);
437 c->last_ping_time = now;
439 if(debug_lvl >= DEBUG_CONNECTIONS)
440 syslog(LOG_NOTICE, _("Connection from %s"), c->hostname);
444 c->allow_request = ID;
450 void try_outgoing_connections(void)
452 static config_t *cfg = NULL;
454 outgoing_t *outgoing;
458 for(cfg = lookup_config(config_tree, "ConnectTo"); cfg;
459 cfg = lookup_config_next(config_tree, cfg)) {
460 get_config_string(cfg, &name);
464 _("Invalid name for outgoing connection in %s line %d"),
465 cfg->file, cfg->line);
470 outgoing = xmalloc_and_zero(sizeof(*outgoing));
471 outgoing->name = name;
472 setup_outgoing_connection(outgoing);