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.4 2002/02/26 23:26:41 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)
89 if((nfd = socket(sa->sa.sa_family, SOCK_STREAM, IPPROTO_TCP)) < 0)
91 syslog(LOG_ERR, _("Creating metasocket failed: %s"), strerror(errno));
95 flags = fcntl(nfd, F_GETFL);
96 if(fcntl(nfd, F_SETFL, flags | O_NONBLOCK) < 0)
99 syslog(LOG_ERR, _("System call `%s' failed: %s"), "fcntl", strerror(errno));
103 /* Optimize TCP settings */
106 setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, &option, sizeof(option));
108 setsockopt(nfd, SOL_TCP, TCP_NODELAY, &option, sizeof(option));
110 option = IPTOS_LOWDELAY;
111 setsockopt(nfd, SOL_IP, IP_TOS, &option, sizeof(option));
113 if(get_config_string(lookup_config(config_tree, "BindToInterface"), &interface))
114 if(setsockopt(nfd, SOL_SOCKET, SO_BINDTODEVICE, interface, strlen(interface)))
117 syslog(LOG_ERR, _("Can't bind to interface %s: %s"), interface, strerror(errno));
122 if(bind(nfd, &sa->sa, SALEN(sa->sa)))
125 addrstr = sockaddr2hostname(sa);
126 syslog(LOG_ERR, _("Can't bind to %s/tcp: %s"), addrstr, strerror(errno));
134 syslog(LOG_ERR, _("System call `%s' failed: %s"), "listen", strerror(errno));
141 int setup_vpn_in_socket(sockaddr_t *sa)
150 if((nfd = socket(sa->sa.sa_family, SOCK_DGRAM, IPPROTO_UDP)) < 0)
152 syslog(LOG_ERR, _("Creating UDP socket failed: %s"), strerror(errno));
156 flags = fcntl(nfd, F_GETFL);
157 if(fcntl(nfd, F_SETFL, flags | O_NONBLOCK) < 0)
160 syslog(LOG_ERR, _("System call `%s' failed: %s"), "fcntl", strerror(errno));
165 setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, &option, sizeof(option));
167 if(get_config_string(lookup_config(config_tree, "BindToInterface"), &interface))
168 if(setsockopt(nfd, SOL_SOCKET, SO_BINDTODEVICE, interface, strlen(interface)))
171 syslog(LOG_ERR, _("Can't bind to interface %s: %s"), interface, strerror(errno));
176 if(bind(nfd, &sa->sa, SALEN(sa->sa)))
179 addrstr = sockaddr2hostname(sa);
180 syslog(LOG_ERR, _("Can't bind to %s/udp: %s"), addrstr, strerror(errno));
188 void retry_outgoing(outgoing_t *outgoing)
192 outgoing->timeout += 5;
193 if(outgoing->timeout > maxtimeout)
194 outgoing->timeout = maxtimeout;
197 event->handler = (event_handler_t)setup_outgoing_connection;
198 event->time = time(NULL) + outgoing->timeout;
199 event->data = outgoing;
202 if(debug_lvl >= DEBUG_CONNECTIONS)
203 syslog(LOG_NOTICE, _("Trying to re-establish outgoing connection in %d seconds"), outgoing->timeout);
207 int setup_outgoing_socket(connection_t *c)
211 if(debug_lvl >= DEBUG_CONNECTIONS)
212 syslog(LOG_INFO, _("Trying to connect to %s (%s)"), c->name, c->hostname);
214 c->socket = socket(c->address.sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
218 syslog(LOG_ERR, _("Creating socket for %s failed: %s"), c->hostname, strerror(errno));
222 /* Optimize TCP settings */
226 setsockopt(c->socket, SOL_TCP, TCP_NODELAY, &option, sizeof(option));
228 option = IPTOS_LOWDELAY;
229 setsockopt(c->socket, SOL_IP, IP_TOS, &option, sizeof(option));
234 if(connect(c->socket, &c->address.sa, SALEN(c->address.sa)) == -1)
237 syslog(LOG_ERR, _("Error while connecting to %s (%s): %s"), c->name, c->hostname, strerror(errno));
241 if(debug_lvl >= DEBUG_CONNECTIONS)
242 syslog(LOG_INFO, _("Connected to %s (%s)"), c->name, c->hostname);
248 void finish_connecting(connection_t *c)
251 if(debug_lvl >= DEBUG_CONNECTIONS)
252 syslog(LOG_INFO, _("Connected to %s (%s)"), c->name, c->hostname);
254 c->last_ping_time = time(NULL);
260 void do_outgoing_connection(connection_t *c)
262 char *address, *port;
263 int option, result, flags;
268 if(!c->outgoing->cfg)
270 if(debug_lvl >= DEBUG_CONNECTIONS)
271 syslog(LOG_ERR, _("Could not set up a meta connection to %s"), c->name);
272 c->status.remove = 1;
274 retry_outgoing(c->outgoing);
278 get_config_string(c->outgoing->cfg, &address);
280 if(!get_config_string(lookup_config(c->config_tree, "Port"), &port))
281 asprintf(&port, "655");
283 c->outgoing->ai = str2addrinfo(address, port, SOCK_STREAM);
287 c->outgoing->aip = c->outgoing->ai;
288 c->outgoing->cfg = lookup_config_next(c->config_tree, c->outgoing->cfg);
291 if(!c->outgoing->aip)
293 freeaddrinfo(c->outgoing->ai);
294 c->outgoing->ai = NULL;
298 memcpy(&c->address, c->outgoing->aip->ai_addr, c->outgoing->aip->ai_addrlen);
299 c->outgoing->aip = c->outgoing->aip->ai_next;
304 c->hostname = sockaddr2hostname(&c->address);
306 if(debug_lvl >= DEBUG_CONNECTIONS)
307 syslog(LOG_INFO, _("Trying to connect to %s (%s)"), c->name, c->hostname);
309 c->socket = socket(c->address.sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
313 if(debug_lvl >= DEBUG_CONNECTIONS)
314 syslog(LOG_ERR, _("Creating socket for %s failed: %s"), c->hostname, strerror(errno));
319 /* Optimize TCP settings */
323 setsockopt(c->socket, SOL_TCP, TCP_NODELAY, &option, sizeof(option));
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)
335 syslog(LOG_ERR, _("fcntl for %s: %s"), c->hostname, strerror(errno));
340 result = connect(c->socket, &c->address.sa, SALEN(c->address.sa));
344 if(errno == EINPROGRESS)
346 c->status.connecting = 1;
352 if(debug_lvl >= DEBUG_CONNECTIONS)
353 syslog(LOG_ERR, _("%s: %s"), c->hostname, strerror(errno));
358 finish_connecting(c);
363 void setup_outgoing_connection(outgoing_t *outgoing)
368 n = lookup_node(outgoing->name);
373 if(debug_lvl >= DEBUG_CONNECTIONS)
374 syslog(LOG_INFO, _("Already connected to %s"), outgoing->name);
375 n->connection->outgoing = outgoing;
379 c = new_connection();
380 c->name = xstrdup(outgoing->name);
381 c->outcipher = myself->connection->outcipher;
382 c->outdigest = myself->connection->outdigest;
383 c->outmaclength = myself->connection->outmaclength;
384 c->outcompression = myself->connection->outcompression;
386 init_configuration(&c->config_tree);
387 read_connection_config(c);
389 outgoing->cfg = lookup_config(c->config_tree, "Address");
393 syslog(LOG_ERR, _("No address specified for %s"), c->name);
395 free(outgoing->name);
400 c->outgoing = outgoing;
401 c->last_ping_time = time(NULL);
405 do_outgoing_connection(c);
409 accept a new tcp connect and create a
412 int handle_new_meta_connection(int sock)
416 int fd, len = sizeof(sa);
418 if((fd = accept(sock, &sa.sa, &len)) < 0)
420 syslog(LOG_ERR, _("Accepting a new connection failed: %s"), strerror(errno));
424 c = new_connection();
425 c->outcipher = myself->connection->outcipher;
426 c->outdigest = myself->connection->outdigest;
427 c->outmaclength = myself->connection->outmaclength;
428 c->outcompression = myself->connection->outcompression;
431 c->hostname = sockaddr2hostname(&sa);
433 c->last_ping_time = time(NULL);
435 if(debug_lvl >= DEBUG_CONNECTIONS)
436 syslog(LOG_NOTICE, _("Connection from %s"), c->hostname);
440 c->allow_request = ID;
446 void try_outgoing_connections(void)
448 static config_t *cfg = NULL;
450 outgoing_t *outgoing;
452 for(cfg = lookup_config(config_tree, "ConnectTo"); cfg; cfg = lookup_config_next(config_tree, cfg))
454 get_config_string(cfg, &name);
458 syslog(LOG_ERR, _("Invalid name for outgoing connection in %s line %d"), cfg->file, cfg->line);
463 outgoing = xmalloc_and_zero(sizeof(*outgoing));
464 outgoing->name = name;
465 setup_outgoing_connection(outgoing);