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.34 2003/10/06 14:41:45 guus Exp $
27 #include "connection.h"
38 #define EINPROGRESS WSAEINPROGRESS
41 int addressfamily = AF_UNSPEC;
43 int seconds_till_retry = 5;
45 listen_socket_t listen_socket[MAXSOCKETS];
50 int setup_listen_socket(const sockaddr_t *sa)
56 #ifdef SO_BINDTODEVICE
62 nfd = socket(sa->sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
65 ifdebug(STATUS) logger(LOG_ERR, _("Creating metasocket failed: %s"), strerror(errno));
70 flags = fcntl(nfd, F_GETFL);
72 if(fcntl(nfd, F_SETFL, flags | O_NONBLOCK) < 0) {
74 logger(LOG_ERR, _("System call `%s' failed: %s"), "fcntl",
80 /* Optimize TCP settings */
83 setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, &option, sizeof(option));
85 #if defined(SOL_TCP) && defined(TCP_NODELAY)
86 setsockopt(nfd, SOL_TCP, TCP_NODELAY, &option, sizeof(option));
89 #if defined(SOL_IP) && defined(IP_TOS) && defined(IPTOS_LOWDELAY)
90 option = IPTOS_LOWDELAY;
91 setsockopt(nfd, SOL_IP, IP_TOS, &option, sizeof(option));
95 (lookup_config(config_tree, "BindToInterface"), &iface)) {
96 #if defined(SOL_SOCKET) && defined(SO_BINDTODEVICE)
97 memset(&ifr, 0, sizeof(ifr));
98 strncpy(ifr.ifr_ifrn.ifrn_name, iface, IFNAMSIZ);
100 if(setsockopt(nfd, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof(ifr))) {
102 logger(LOG_ERR, _("Can't bind to interface %s: %s"), iface,
107 logger(LOG_WARNING, _("BindToInterface not supported on this platform"));
111 if(bind(nfd, &sa->sa, SALEN(sa->sa))) {
113 addrstr = sockaddr2hostname(sa);
114 logger(LOG_ERR, _("Can't bind to %s/tcp: %s"), addrstr,
122 logger(LOG_ERR, _("System call `%s' failed: %s"), "listen",
130 int setup_vpn_in_socket(const sockaddr_t *sa)
135 #if defined(SOL_SOCKET) && defined(SO_BINDTODEVICE)
142 nfd = socket(sa->sa.sa_family, SOCK_DGRAM, IPPROTO_UDP);
145 logger(LOG_ERR, _("Creating UDP socket failed: %s"), strerror(errno));
150 flags = fcntl(nfd, F_GETFL);
151 if(fcntl(nfd, F_SETFL, flags | O_NONBLOCK) < 0) {
153 logger(LOG_ERR, _("System call `%s' failed: %s"), "fcntl",
160 setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, &option, sizeof(option));
162 #if defined(SOL_SOCKET) && defined(SO_BINDTODEVICE)
164 (lookup_config(config_tree, "BindToInterface"), &iface)) {
165 memset(&ifr, 0, sizeof(ifr));
166 strncpy(ifr.ifr_ifrn.ifrn_name, iface, IFNAMSIZ);
168 if(setsockopt(nfd, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof(ifr))) {
170 logger(LOG_ERR, _("Can't bind to interface %s: %s"), iface,
177 if(bind(nfd, &sa->sa, SALEN(sa->sa))) {
179 addrstr = sockaddr2hostname(sa);
180 logger(LOG_ERR, _("Can't bind to %s/udp: %s"), addrstr,
189 void retry_outgoing(outgoing_t *outgoing)
195 outgoing->timeout += 5;
197 if(outgoing->timeout > maxtimeout)
198 outgoing->timeout = maxtimeout;
201 event->handler = (event_handler_t) setup_outgoing_connection;
202 event->time = now + outgoing->timeout;
203 event->data = outgoing;
206 ifdebug(CONNECTIONS) logger(LOG_NOTICE,
207 _("Trying to re-establish outgoing connection in %d seconds"),
211 void finish_connecting(connection_t *c)
215 ifdebug(CONNECTIONS) logger(LOG_INFO, _("Connected to %s (%s)"), c->name, c->hostname);
217 c->last_ping_time = now;
222 void do_outgoing_connection(connection_t *c)
224 char *address, *port;
225 int option, result, flags;
230 if(!c->outgoing->ai) {
231 if(!c->outgoing->cfg) {
232 ifdebug(CONNECTIONS) logger(LOG_ERR, _("Could not set up a meta connection to %s"),
234 c->status.remove = true;
235 retry_outgoing(c->outgoing);
239 get_config_string(c->outgoing->cfg, &address);
241 if(!get_config_string(lookup_config(c->config_tree, "Port"), &port))
242 asprintf(&port, "655");
244 c->outgoing->ai = str2addrinfo(address, port, SOCK_STREAM);
248 c->outgoing->aip = c->outgoing->ai;
249 c->outgoing->cfg = lookup_config_next(c->config_tree, c->outgoing->cfg);
252 if(!c->outgoing->aip) {
253 freeaddrinfo(c->outgoing->ai);
254 c->outgoing->ai = NULL;
258 memcpy(&c->address, c->outgoing->aip->ai_addr,
259 c->outgoing->aip->ai_addrlen);
260 c->outgoing->aip = c->outgoing->aip->ai_next;
265 c->hostname = sockaddr2hostname(&c->address);
267 ifdebug(CONNECTIONS) logger(LOG_INFO, _("Trying to connect to %s (%s)"), c->name,
270 c->socket = socket(c->address.sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
272 if(c->socket == -1) {
273 ifdebug(CONNECTIONS) logger(LOG_ERR, _("Creating socket for %s failed: %s"), c->hostname,
279 /* Optimize TCP settings */
281 #if defined(SOL_TCP) && defined(TCP_NODELAY)
283 setsockopt(c->socket, SOL_TCP, TCP_NODELAY, &option, sizeof(option));
286 #if defined(SOL_IP) && defined(IP_TOS)
287 option = IPTOS_LOWDELAY;
288 setsockopt(c->socket, SOL_IP, IP_TOS, &option, sizeof(option));
294 flags = fcntl(c->socket, F_GETFL);
296 if(fcntl(c->socket, F_SETFL, flags | O_NONBLOCK) < 0) {
297 logger(LOG_ERR, _("fcntl for %s: %s"), c->hostname, strerror(errno));
303 result = connect(c->socket, &c->address.sa, SALEN(c->address.sa));
306 if(errno == EINPROGRESS) {
307 c->status.connecting = true;
311 closesocket(c->socket);
313 ifdebug(CONNECTIONS) logger(LOG_ERR, _("%s: %s"), c->hostname, strerror(errno));
318 finish_connecting(c);
323 void setup_outgoing_connection(outgoing_t *outgoing)
330 n = lookup_node(outgoing->name);
334 ifdebug(CONNECTIONS) logger(LOG_INFO, _("Already connected to %s"), outgoing->name);
336 n->connection->outgoing = outgoing;
340 c = new_connection();
341 c->name = xstrdup(outgoing->name);
342 c->outcipher = myself->connection->outcipher;
343 c->outdigest = myself->connection->outdigest;
344 c->outmaclength = myself->connection->outmaclength;
345 c->outcompression = myself->connection->outcompression;
347 init_configuration(&c->config_tree);
348 read_connection_config(c);
350 outgoing->cfg = lookup_config(c->config_tree, "Address");
353 logger(LOG_ERR, _("No address specified for %s"), c->name);
355 free(outgoing->name);
360 c->outgoing = outgoing;
361 c->last_ping_time = now;
365 do_outgoing_connection(c);
369 accept a new tcp connect and create a
372 bool handle_new_meta_connection(int sock)
376 int fd, len = sizeof(sa);
380 fd = accept(sock, &sa.sa, &len);
383 logger(LOG_ERR, _("Accepting a new connection failed: %s"),
390 c = new_connection();
391 c->outcipher = myself->connection->outcipher;
392 c->outdigest = myself->connection->outdigest;
393 c->outmaclength = myself->connection->outmaclength;
394 c->outcompression = myself->connection->outcompression;
397 c->hostname = sockaddr2hostname(&sa);
399 c->last_ping_time = now;
401 ifdebug(CONNECTIONS) logger(LOG_NOTICE, _("Connection from %s"), c->hostname);
405 c->allow_request = ID;
411 void try_outgoing_connections(void)
413 static config_t *cfg = NULL;
415 outgoing_t *outgoing;
419 for(cfg = lookup_config(config_tree, "ConnectTo"); cfg;
420 cfg = lookup_config_next(config_tree, cfg)) {
421 get_config_string(cfg, &name);
423 if(!check_id(name)) {
425 _("Invalid name for outgoing connection in %s line %d"),
426 cfg->file, cfg->line);
431 outgoing = xmalloc_and_zero(sizeof(*outgoing));
432 outgoing->name = name;
433 setup_outgoing_connection(outgoing);