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.2.5 2002/03/01 11:18:34 guus 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 int tcp_socket[MAXSOCKETS];
74 int udp_socket[MAXSOCKETS];
80 int setup_listen_socket(sockaddr_t *sa)
90 if((nfd = socket(sa->sa.sa_family, SOCK_STREAM, IPPROTO_TCP)) < 0)
92 syslog(LOG_ERR, _("Creating metasocket failed: %s"), strerror(errno));
96 flags = fcntl(nfd, F_GETFL);
97 if(fcntl(nfd, F_SETFL, flags | O_NONBLOCK) < 0)
100 syslog(LOG_ERR, _("System call `%s' failed: %s"), "fcntl", strerror(errno));
104 /* Optimize TCP settings */
107 setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, &option, sizeof(option));
109 setsockopt(nfd, SOL_TCP, TCP_NODELAY, &option, sizeof(option));
111 option = IPTOS_LOWDELAY;
112 setsockopt(nfd, SOL_IP, IP_TOS, &option, sizeof(option));
114 if(get_config_string(lookup_config(config_tree, "BindToInterface"), &interface))
116 memset(&ifr, 0, sizeof(ifr));
117 strncpy(ifr.ifr_ifrn.ifrn_name, interface, IFNAMSIZ);
118 if(setsockopt(nfd, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof(ifr)))
121 syslog(LOG_ERR, _("Can't bind to interface %s: %s"), interface, strerror(errno));
127 if(bind(nfd, &sa->sa, SALEN(sa->sa)))
130 addrstr = sockaddr2hostname(sa);
131 syslog(LOG_ERR, _("Can't bind to %s/tcp: %s"), addrstr, strerror(errno));
139 syslog(LOG_ERR, _("System call `%s' failed: %s"), "listen", strerror(errno));
146 int setup_vpn_in_socket(sockaddr_t *sa)
156 if((nfd = socket(sa->sa.sa_family, SOCK_DGRAM, IPPROTO_UDP)) < 0)
158 syslog(LOG_ERR, _("Creating UDP socket failed: %s"), strerror(errno));
162 flags = fcntl(nfd, F_GETFL);
163 if(fcntl(nfd, F_SETFL, flags | O_NONBLOCK) < 0)
166 syslog(LOG_ERR, _("System call `%s' failed: %s"), "fcntl", strerror(errno));
171 setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, &option, sizeof(option));
173 if(get_config_string(lookup_config(config_tree, "BindToInterface"), &interface))
175 memset(&ifr, 0, sizeof(ifr));
176 strncpy(ifr.ifr_ifrn.ifrn_name, interface, IFNAMSIZ);
177 if(setsockopt(nfd, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof(ifr)))
180 syslog(LOG_ERR, _("Can't bind to interface %s: %s"), interface, strerror(errno));
186 if(bind(nfd, &sa->sa, SALEN(sa->sa)))
189 addrstr = sockaddr2hostname(sa);
190 syslog(LOG_ERR, _("Can't bind to %s/udp: %s"), addrstr, strerror(errno));
198 void retry_outgoing(outgoing_t *outgoing)
202 outgoing->timeout += 5;
203 if(outgoing->timeout > maxtimeout)
204 outgoing->timeout = maxtimeout;
207 event->handler = (event_handler_t)setup_outgoing_connection;
208 event->time = time(NULL) + outgoing->timeout;
209 event->data = outgoing;
212 if(debug_lvl >= DEBUG_CONNECTIONS)
213 syslog(LOG_NOTICE, _("Trying to re-establish outgoing connection in %d seconds"), outgoing->timeout);
217 int setup_outgoing_socket(connection_t *c)
221 if(debug_lvl >= DEBUG_CONNECTIONS)
222 syslog(LOG_INFO, _("Trying to connect to %s (%s)"), c->name, c->hostname);
224 c->socket = socket(c->address.sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
228 syslog(LOG_ERR, _("Creating socket for %s failed: %s"), c->hostname, strerror(errno));
232 /* Optimize TCP settings */
236 setsockopt(c->socket, SOL_TCP, TCP_NODELAY, &option, sizeof(option));
238 option = IPTOS_LOWDELAY;
239 setsockopt(c->socket, SOL_IP, IP_TOS, &option, sizeof(option));
244 if(connect(c->socket, &c->address.sa, SALEN(c->address.sa)) == -1)
247 syslog(LOG_ERR, _("Error while connecting to %s (%s): %s"), c->name, c->hostname, strerror(errno));
251 if(debug_lvl >= DEBUG_CONNECTIONS)
252 syslog(LOG_INFO, _("Connected to %s (%s)"), c->name, c->hostname);
258 void finish_connecting(connection_t *c)
261 if(debug_lvl >= DEBUG_CONNECTIONS)
262 syslog(LOG_INFO, _("Connected to %s (%s)"), c->name, c->hostname);
264 c->last_ping_time = time(NULL);
270 void do_outgoing_connection(connection_t *c)
272 char *address, *port;
273 int option, result, flags;
278 if(!c->outgoing->cfg)
280 if(debug_lvl >= DEBUG_CONNECTIONS)
281 syslog(LOG_ERR, _("Could not set up a meta connection to %s"), c->name);
282 c->status.remove = 1;
284 retry_outgoing(c->outgoing);
288 get_config_string(c->outgoing->cfg, &address);
290 if(!get_config_string(lookup_config(c->config_tree, "Port"), &port))
291 asprintf(&port, "655");
293 c->outgoing->ai = str2addrinfo(address, port, SOCK_STREAM);
297 c->outgoing->aip = c->outgoing->ai;
298 c->outgoing->cfg = lookup_config_next(c->config_tree, c->outgoing->cfg);
301 if(!c->outgoing->aip)
303 freeaddrinfo(c->outgoing->ai);
304 c->outgoing->ai = NULL;
308 memcpy(&c->address, c->outgoing->aip->ai_addr, c->outgoing->aip->ai_addrlen);
309 c->outgoing->aip = c->outgoing->aip->ai_next;
314 c->hostname = sockaddr2hostname(&c->address);
316 if(debug_lvl >= DEBUG_CONNECTIONS)
317 syslog(LOG_INFO, _("Trying to connect to %s (%s)"), c->name, c->hostname);
319 c->socket = socket(c->address.sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
323 if(debug_lvl >= DEBUG_CONNECTIONS)
324 syslog(LOG_ERR, _("Creating socket for %s failed: %s"), c->hostname, strerror(errno));
329 /* Optimize TCP settings */
333 setsockopt(c->socket, SOL_TCP, TCP_NODELAY, &option, sizeof(option));
335 option = IPTOS_LOWDELAY;
336 setsockopt(c->socket, SOL_IP, IP_TOS, &option, sizeof(option));
341 flags = fcntl(c->socket, F_GETFL);
343 if(fcntl(c->socket, F_SETFL, flags | O_NONBLOCK) < 0)
345 syslog(LOG_ERR, _("fcntl for %s: %s"), c->hostname, strerror(errno));
350 result = connect(c->socket, &c->address.sa, SALEN(c->address.sa));
354 if(errno == EINPROGRESS)
356 c->status.connecting = 1;
362 if(debug_lvl >= DEBUG_CONNECTIONS)
363 syslog(LOG_ERR, _("%s: %s"), c->hostname, strerror(errno));
368 finish_connecting(c);
373 void setup_outgoing_connection(outgoing_t *outgoing)
378 n = lookup_node(outgoing->name);
383 if(debug_lvl >= DEBUG_CONNECTIONS)
384 syslog(LOG_INFO, _("Already connected to %s"), outgoing->name);
385 n->connection->outgoing = outgoing;
389 c = new_connection();
390 c->name = xstrdup(outgoing->name);
391 c->outcipher = myself->connection->outcipher;
392 c->outdigest = myself->connection->outdigest;
393 c->outmaclength = myself->connection->outmaclength;
394 c->outcompression = myself->connection->outcompression;
396 init_configuration(&c->config_tree);
397 read_connection_config(c);
399 outgoing->cfg = lookup_config(c->config_tree, "Address");
403 syslog(LOG_ERR, _("No address specified for %s"), c->name);
405 free(outgoing->name);
410 c->outgoing = outgoing;
411 c->last_ping_time = time(NULL);
415 do_outgoing_connection(c);
419 accept a new tcp connect and create a
422 int handle_new_meta_connection(int sock)
426 int fd, len = sizeof(sa);
428 if((fd = accept(sock, &sa.sa, &len)) < 0)
430 syslog(LOG_ERR, _("Accepting a new connection failed: %s"), strerror(errno));
434 c = new_connection();
435 c->outcipher = myself->connection->outcipher;
436 c->outdigest = myself->connection->outdigest;
437 c->outmaclength = myself->connection->outmaclength;
438 c->outcompression = myself->connection->outcompression;
441 c->hostname = sockaddr2hostname(&sa);
443 c->last_ping_time = time(NULL);
445 if(debug_lvl >= DEBUG_CONNECTIONS)
446 syslog(LOG_NOTICE, _("Connection from %s"), c->hostname);
450 c->allow_request = ID;
456 void try_outgoing_connections(void)
458 static config_t *cfg = NULL;
460 outgoing_t *outgoing;
462 for(cfg = lookup_config(config_tree, "ConnectTo"); cfg; cfg = lookup_config_next(config_tree, cfg))
464 get_config_string(cfg, &name);
468 syslog(LOG_ERR, _("Invalid name for outgoing connection in %s line %d"), cfg->file, cfg->line);
473 outgoing = xmalloc_and_zero(sizeof(*outgoing));
474 outgoing->name = name;
475 setup_outgoing_connection(outgoing);