2 net_socket.c -- Handle various kinds of sockets.
3 Copyright (C) 1998-2003 Ivo Timmermans <ivo@o2w.nl>,
4 2000-2003 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.29 2003/07/18 13:45:06 guus Exp $
27 #include "connection.h"
37 int addressfamily = AF_UNSPEC;
39 int seconds_till_retry = 5;
41 listen_socket_t listen_socket[MAXSOCKETS];
46 int setup_listen_socket(sockaddr_t *sa)
56 nfd = socket(sa->sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
59 logger(LOG_ERR, _("Creating metasocket failed: %s"), strerror(errno));
63 flags = fcntl(nfd, F_GETFL);
65 if(fcntl(nfd, F_SETFL, flags | O_NONBLOCK) < 0) {
67 logger(LOG_ERR, _("System call `%s' failed: %s"), "fcntl",
72 /* Optimize TCP settings */
75 setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, &option, sizeof(option));
77 #if defined(SOL_TCP) && defined(TCP_NODELAY)
78 setsockopt(nfd, SOL_TCP, TCP_NODELAY, &option, sizeof(option));
81 #if defined(SOL_IP) && defined(IP_TOS) && defined(IPTOS_LOWDELAY)
82 option = IPTOS_LOWDELAY;
83 setsockopt(nfd, SOL_IP, IP_TOS, &option, sizeof(option));
87 (lookup_config(config_tree, "BindToInterface"), &iface)) {
88 #if defined(SOL_SOCKET) && defined(SO_BINDTODEVICE)
89 memset(&ifr, 0, sizeof(ifr));
90 strncpy(ifr.ifr_ifrn.ifrn_name, iface, IFNAMSIZ);
92 if(setsockopt(nfd, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof(ifr))) {
94 logger(LOG_ERR, _("Can't bind to interface %s: %s"), iface,
99 logger(LOG_WARNING, _("BindToInterface not supported on this platform"));
103 if(bind(nfd, &sa->sa, SALEN(sa->sa))) {
105 addrstr = sockaddr2hostname(sa);
106 logger(LOG_ERR, _("Can't bind to %s/tcp: %s"), addrstr,
114 logger(LOG_ERR, _("System call `%s' failed: %s"), "listen",
122 int setup_vpn_in_socket(sockaddr_t *sa)
127 #if defined(SOL_SOCKET) && defined(SO_BINDTODEVICE)
134 nfd = socket(sa->sa.sa_family, SOCK_DGRAM, IPPROTO_UDP);
137 logger(LOG_ERR, _("Creating UDP socket failed: %s"), strerror(errno));
141 flags = fcntl(nfd, F_GETFL);
142 if(fcntl(nfd, F_SETFL, flags | O_NONBLOCK) < 0) {
144 logger(LOG_ERR, _("System call `%s' failed: %s"), "fcntl",
150 setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, &option, sizeof(option));
152 #if defined(SOL_SOCKET) && defined(SO_BINDTODEVICE)
154 (lookup_config(config_tree, "BindToInterface"), &iface)) {
155 memset(&ifr, 0, sizeof(ifr));
156 strncpy(ifr.ifr_ifrn.ifrn_name, iface, IFNAMSIZ);
158 if(setsockopt(nfd, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof(ifr))) {
160 logger(LOG_ERR, _("Can't bind to interface %s: %s"), iface,
167 if(bind(nfd, &sa->sa, SALEN(sa->sa))) {
169 addrstr = sockaddr2hostname(sa);
170 logger(LOG_ERR, _("Can't bind to %s/udp: %s"), addrstr,
179 void retry_outgoing(outgoing_t *outgoing)
185 outgoing->timeout += 5;
187 if(outgoing->timeout > maxtimeout)
188 outgoing->timeout = maxtimeout;
191 event->handler = (event_handler_t) setup_outgoing_connection;
192 event->time = now + outgoing->timeout;
193 event->data = outgoing;
196 ifdebug(CONNECTIONS) logger(LOG_NOTICE,
197 _("Trying to re-establish outgoing connection in %d seconds"),
201 void finish_connecting(connection_t *c)
205 ifdebug(CONNECTIONS) logger(LOG_INFO, _("Connected to %s (%s)"), c->name, c->hostname);
207 c->last_ping_time = now;
212 void do_outgoing_connection(connection_t *c)
214 char *address, *port;
215 int option, result, flags;
220 if(!c->outgoing->ai) {
221 if(!c->outgoing->cfg) {
222 ifdebug(CONNECTIONS) logger(LOG_ERR, _("Could not set up a meta connection to %s"),
224 c->status.remove = 1;
225 retry_outgoing(c->outgoing);
229 get_config_string(c->outgoing->cfg, &address);
231 if(!get_config_string(lookup_config(c->config_tree, "Port"), &port))
232 asprintf(&port, "655");
234 c->outgoing->ai = str2addrinfo(address, port, SOCK_STREAM);
238 c->outgoing->aip = c->outgoing->ai;
239 c->outgoing->cfg = lookup_config_next(c->config_tree, c->outgoing->cfg);
242 if(!c->outgoing->aip) {
243 freeaddrinfo(c->outgoing->ai);
244 c->outgoing->ai = NULL;
248 memcpy(&c->address, c->outgoing->aip->ai_addr,
249 c->outgoing->aip->ai_addrlen);
250 c->outgoing->aip = c->outgoing->aip->ai_next;
255 c->hostname = sockaddr2hostname(&c->address);
257 ifdebug(CONNECTIONS) logger(LOG_INFO, _("Trying to connect to %s (%s)"), c->name,
260 c->socket = socket(c->address.sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
262 if(c->socket == -1) {
263 ifdebug(CONNECTIONS) logger(LOG_ERR, _("Creating socket for %s failed: %s"), c->hostname,
269 /* Optimize TCP settings */
271 #if defined(SOL_TCP) && defined(TCP_NODELAY)
273 setsockopt(c->socket, SOL_TCP, TCP_NODELAY, &option, sizeof(option));
276 #if defined(SOL_IP) && defined(IP_TOS)
277 option = IPTOS_LOWDELAY;
278 setsockopt(c->socket, SOL_IP, IP_TOS, &option, sizeof(option));
283 flags = fcntl(c->socket, F_GETFL);
285 if(fcntl(c->socket, F_SETFL, flags | O_NONBLOCK) < 0) {
286 logger(LOG_ERR, _("fcntl for %s: %s"), c->hostname, strerror(errno));
291 result = connect(c->socket, &c->address.sa, SALEN(c->address.sa));
294 if(errno == EINPROGRESS) {
295 c->status.connecting = 1;
301 ifdebug(CONNECTIONS) logger(LOG_ERR, _("%s: %s"), c->hostname, strerror(errno));
306 finish_connecting(c);
311 void setup_outgoing_connection(outgoing_t *outgoing)
318 n = lookup_node(outgoing->name);
322 ifdebug(CONNECTIONS) logger(LOG_INFO, _("Already connected to %s"), outgoing->name);
324 n->connection->outgoing = outgoing;
328 c = new_connection();
329 c->name = xstrdup(outgoing->name);
330 c->outcipher = myself->connection->outcipher;
331 c->outdigest = myself->connection->outdigest;
332 c->outmaclength = myself->connection->outmaclength;
333 c->outcompression = myself->connection->outcompression;
335 init_configuration(&c->config_tree);
336 read_connection_config(c);
338 outgoing->cfg = lookup_config(c->config_tree, "Address");
341 logger(LOG_ERR, _("No address specified for %s"), c->name);
343 free(outgoing->name);
348 c->outgoing = outgoing;
349 c->last_ping_time = now;
353 do_outgoing_connection(c);
357 accept a new tcp connect and create a
360 int handle_new_meta_connection(int sock)
364 int fd, len = sizeof(sa);
368 fd = accept(sock, &sa.sa, &len);
371 logger(LOG_ERR, _("Accepting a new connection failed: %s"),
378 c = new_connection();
379 c->outcipher = myself->connection->outcipher;
380 c->outdigest = myself->connection->outdigest;
381 c->outmaclength = myself->connection->outmaclength;
382 c->outcompression = myself->connection->outcompression;
385 c->hostname = sockaddr2hostname(&sa);
387 c->last_ping_time = now;
389 ifdebug(CONNECTIONS) logger(LOG_NOTICE, _("Connection from %s"), c->hostname);
393 c->allow_request = ID;
399 void try_outgoing_connections(void)
401 static config_t *cfg = NULL;
403 outgoing_t *outgoing;
407 for(cfg = lookup_config(config_tree, "ConnectTo"); cfg;
408 cfg = lookup_config_next(config_tree, cfg)) {
409 get_config_string(cfg, &name);
413 _("Invalid name for outgoing connection in %s line %d"),
414 cfg->file, cfg->line);
419 outgoing = xmalloc_and_zero(sizeof(*outgoing));
420 outgoing->name = name;
421 setup_outgoing_connection(outgoing);