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.2 2002/02/20 19:25:09 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;
77 int setup_listen_socket(sockaddr_t *sa)
86 if((nfd = socket(sa->sa.sa_family, SOCK_STREAM, IPPROTO_TCP)) < 0)
88 syslog(LOG_ERR, _("Creating metasocket failed: %s"), strerror(errno));
92 flags = fcntl(nfd, F_GETFL);
93 if(fcntl(nfd, F_SETFL, flags | O_NONBLOCK) < 0)
96 syslog(LOG_ERR, _("System call `%s' failed: %s"), "fcntl", strerror(errno));
100 /* Optimize TCP settings */
103 setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, &option, sizeof(option));
105 setsockopt(nfd, SOL_TCP, TCP_NODELAY, &option, sizeof(option));
107 option = IPTOS_LOWDELAY;
108 setsockopt(nfd, SOL_IP, IP_TOS, &option, sizeof(option));
110 if(get_config_string(lookup_config(config_tree, "BindToInterface"), &interface))
111 if(setsockopt(nfd, SOL_SOCKET, SO_BINDTODEVICE, interface, strlen(interface)))
114 syslog(LOG_ERR, _("Can't bind to interface %s: %s"), interface, strerror(errno));
119 if(bind(nfd, &sa->sa, sizeof(*sa)))
122 addrstr = sockaddr2hostname(sa);
123 syslog(LOG_ERR, _("Can't bind to %s/tcp: %s"), addrstr, strerror(errno));
131 syslog(LOG_ERR, _("System call `%s' failed: %s"), "listen", strerror(errno));
138 int setup_vpn_in_socket(sockaddr_t *sa)
147 if((nfd = socket(sa->sa.sa_family, SOCK_DGRAM, IPPROTO_UDP)) < 0)
149 syslog(LOG_ERR, _("Creating UDP socket failed: %s"), strerror(errno));
153 flags = fcntl(nfd, F_GETFL);
154 if(fcntl(nfd, F_SETFL, flags | O_NONBLOCK) < 0)
157 syslog(LOG_ERR, _("System call `%s' failed: %s"), "fcntl", strerror(errno));
162 setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, &option, sizeof(option));
164 if(get_config_string(lookup_config(config_tree, "BindToInterface"), &interface))
165 if(setsockopt(nfd, SOL_SOCKET, SO_BINDTODEVICE, interface, strlen(interface)))
168 syslog(LOG_ERR, _("Can't bind to interface %s: %s"), interface, strerror(errno));
173 if(bind(nfd, &sa->sa, sizeof(*sa)))
176 addrstr = sockaddr2hostname(sa);
177 syslog(LOG_ERR, _("Can't bind to %s/udp: %s"), addrstr, strerror(errno));
185 void retry_outgoing(outgoing_t *outgoing)
189 outgoing->timeout += 5;
190 if(outgoing->timeout > maxtimeout)
191 outgoing->timeout = maxtimeout;
194 event->handler = (event_handler_t)setup_outgoing_connection;
195 event->time = time(NULL) + outgoing->timeout;
196 event->data = outgoing;
199 if(debug_lvl >= DEBUG_CONNECTIONS)
200 syslog(LOG_NOTICE, _("Trying to re-establish outgoing connection in %d seconds"), outgoing->timeout);
204 int setup_outgoing_socket(connection_t *c)
208 if(debug_lvl >= DEBUG_CONNECTIONS)
209 syslog(LOG_INFO, _("Trying to connect to %s (%s)"), c->name, c->hostname);
211 c->socket = socket(c->address.sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
215 syslog(LOG_ERR, _("Creating socket for %s failed: %s"), c->hostname, strerror(errno));
219 /* Optimize TCP settings */
223 setsockopt(c->socket, SOL_TCP, TCP_NODELAY, &option, sizeof(option));
225 option = IPTOS_LOWDELAY;
226 setsockopt(c->socket, SOL_IP, IP_TOS, &option, sizeof(option));
231 if(connect(c->socket, &c->address.sa, sizeof(c->address)) == -1)
234 syslog(LOG_ERR, _("%s: %s"), c->hostname, strerror(errno));
238 if(debug_lvl >= DEBUG_CONNECTIONS)
239 syslog(LOG_INFO, _("Connected to %s (%s)"), c->name, c->hostname);
245 void finish_connecting(connection_t *c)
248 if(debug_lvl >= DEBUG_CONNECTIONS)
249 syslog(LOG_INFO, _("Connected to %s (%s)"), c->name, c->hostname);
251 c->last_ping_time = time(NULL);
257 void do_outgoing_connection(connection_t *c)
259 char *address, *port;
260 int option, result, flags;
265 if(!c->outgoing->cfg)
267 if(debug_lvl >= DEBUG_CONNECTIONS)
268 syslog(LOG_ERR, _("Could not set up a meta connection to %s"), c->name);
269 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)
290 freeaddrinfo(c->outgoing->ai);
291 c->outgoing->ai = NULL;
295 memcpy(&c->address, c->outgoing->aip->ai_addr, 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, c->hostname);
306 c->socket = socket(c->address.sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
310 if(debug_lvl >= DEBUG_CONNECTIONS)
311 syslog(LOG_ERR, _("Creating socket for %s failed: %s"), c->hostname, strerror(errno));
316 /* Optimize TCP settings */
320 setsockopt(c->socket, SOL_TCP, TCP_NODELAY, &option, sizeof(option));
322 option = IPTOS_LOWDELAY;
323 setsockopt(c->socket, SOL_IP, IP_TOS, &option, sizeof(option));
328 flags = fcntl(c->socket, F_GETFL);
330 if(fcntl(c->socket, F_SETFL, flags | O_NONBLOCK) < 0)
332 syslog(LOG_ERR, _("fcntl for %s: %s"), c->hostname, strerror(errno));
337 result = connect(c->socket, &c->address.sa, sizeof(c->address));
341 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)
365 n = lookup_node(outgoing->name);
370 if(debug_lvl >= DEBUG_CONNECTIONS)
371 syslog(LOG_INFO, _("Already connected to %s"), outgoing->name);
372 n->connection->outgoing = outgoing;
376 c = new_connection();
377 c->name = xstrdup(outgoing->name);
378 c->outcipher = myself->connection->outcipher;
379 c->outdigest = myself->connection->outdigest;
380 c->outmaclength = myself->connection->outmaclength;
381 c->outcompression = myself->connection->outcompression;
383 init_configuration(&c->config_tree);
384 read_connection_config(c);
386 outgoing->cfg = lookup_config(c->config_tree, "Address");
390 syslog(LOG_ERR, _("No address specified for %s"), c->name);
392 free(outgoing->name);
397 c->outgoing = outgoing;
398 c->last_ping_time = time(NULL);
402 do_outgoing_connection(c);
406 accept a new tcp connect and create a
409 int handle_new_meta_connection()
413 int fd, len = sizeof(sa);
415 if((fd = accept(tcp_socket, &sa.sa, &len)) < 0)
417 syslog(LOG_ERR, _("Accepting a new connection failed: %s"), strerror(errno));
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 = time(NULL);
432 if(debug_lvl >= DEBUG_CONNECTIONS)
433 syslog(LOG_NOTICE, _("Connection from %s"), c->hostname);
437 c->allow_request = ID;
443 void try_outgoing_connections(void)
445 static config_t *cfg = NULL;
447 outgoing_t *outgoing;
449 for(cfg = lookup_config(config_tree, "ConnectTo"); cfg; cfg = lookup_config_next(config_tree, cfg))
451 get_config_string(cfg, &name);
455 syslog(LOG_ERR, _("Invalid name for outgoing connection in %s line %d"), cfg->file, cfg->line);
460 outgoing = xmalloc_and_zero(sizeof(*outgoing));
461 outgoing->name = name;
462 setup_outgoing_connection(outgoing);