Small correction.
[tinc] / src / net_socket.c
1 /*
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>
5
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.
10
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.
15
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.
19
20     $Id: net_socket.c,v 1.1.2.10 2002/03/18 22:47:20 guus Exp $
21 */
22
23 #include "config.h"
24
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <netdb.h>
28 #include <netinet/in.h>
29 #ifdef HAVE_LINUX
30  #include <netinet/ip.h>
31  #include <netinet/tcp.h>
32 #endif
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <signal.h>
37 #include <sys/time.h>
38 #include <sys/types.h>
39 #include <syslog.h>
40 #include <unistd.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>
46 #include <net/if.h>
47
48 #include <utils.h>
49 #include <xalloc.h>
50 #include <avl_tree.h>
51 #include <list.h>
52
53 #include "conf.h"
54 #include "connection.h"
55 #include "meta.h"
56 #include "net.h"
57 #include "netutl.h"
58 #include "process.h"
59 #include "protocol.h"
60 #include "subnet.h"
61 #include "graph.h"
62 #include "process.h"
63 #include "route.h"
64 #include "device.h"
65 #include "event.h"
66
67 #include "system.h"
68
69 int addressfamily = AF_INET;
70 int maxtimeout = 900;
71 int seconds_till_retry = 5;
72
73 listen_socket_t listen_socket[MAXSOCKETS];
74 int listen_sockets = 0;
75
76 /* Setup sockets */
77
78 int setup_listen_socket(sockaddr_t *sa)
79 {
80   int nfd, flags;
81   char *addrstr;
82   int option;
83 #if defined(SOL_SOCKET) && defined(SO_BINDTODEVICE)
84   char *interface;
85   struct ifreq ifr;
86 #endif
87 cp
88   if((nfd = socket(sa->sa.sa_family, SOCK_STREAM, IPPROTO_TCP)) < 0)
89     {
90       syslog(LOG_ERR, _("Creating metasocket failed: %s"), strerror(errno));
91       return -1;
92     }
93
94   flags = fcntl(nfd, F_GETFL);
95   if(fcntl(nfd, F_SETFL, flags | O_NONBLOCK) < 0)
96     {
97       close(nfd);
98       syslog(LOG_ERR, _("System call `%s' failed: %s"), "fcntl", strerror(errno));
99       return -1;
100     }
101
102   /* Optimize TCP settings */
103
104   option = 1;
105   setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, &option, sizeof(option));
106
107 #if defined(SOL_TCP) && defined(TCP_NODELAY)
108   setsockopt(nfd, SOL_TCP, TCP_NODELAY, &option, sizeof(option));
109 #endif
110
111 #if defined(SOL_IP) && defined(IP_TOS) && defined(IPTOS_LOWDELAY)
112   option = IPTOS_LOWDELAY;
113   setsockopt(nfd, SOL_IP, IP_TOS, &option, sizeof(option));
114 #endif
115
116   if(get_config_string(lookup_config(config_tree, "BindToInterface"), &interface))
117     {
118 #if defined(SOL_SOCKET) && defined(SO_BINDTODEVICE)
119       memset(&ifr, 0, sizeof(ifr));
120       strncpy(ifr.ifr_ifrn.ifrn_name, interface, IFNAMSIZ);
121       if(setsockopt(nfd, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof(ifr)))
122         {
123           close(nfd);
124           syslog(LOG_ERR, _("Can't bind to interface %s: %s"), interface, strerror(errno));
125           return -1;
126         }
127 #else
128       syslog(LOG_WARNING, _("BindToDevice not supported on this platform"));
129 #endif
130     }
131
132   if(bind(nfd, &sa->sa, SALEN(sa->sa)))
133     {
134       close(nfd);
135       addrstr = sockaddr2hostname(sa);
136       syslog(LOG_ERR, _("Can't bind to %s/tcp: %s"), addrstr, strerror(errno));
137       free(addrstr);
138       return -1;
139     }
140
141   if(listen(nfd, 3))
142     {
143       close(nfd);
144       syslog(LOG_ERR, _("System call `%s' failed: %s"), "listen", strerror(errno));
145       return -1;
146     }
147 cp
148   return nfd;
149 }
150
151 int setup_vpn_in_socket(sockaddr_t *sa)
152 {
153   int nfd, flags;
154   char *addrstr;
155   int option;
156 #if defined(SOL_SOCKET) && defined(SO_BINDTODEVICE)
157   char *interface;
158   struct ifreq ifr;
159 #endif
160 cp
161   if((nfd = socket(sa->sa.sa_family, SOCK_DGRAM, IPPROTO_UDP)) < 0)
162     {
163       syslog(LOG_ERR, _("Creating UDP socket failed: %s"), strerror(errno));
164       return -1;
165     }
166
167   flags = fcntl(nfd, F_GETFL);
168   if(fcntl(nfd, F_SETFL, flags | O_NONBLOCK) < 0)
169     {
170       close(nfd);
171       syslog(LOG_ERR, _("System call `%s' failed: %s"), "fcntl", strerror(errno));
172       return -1;
173     }
174
175   option = 1;
176   setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, &option, sizeof(option));
177
178 #if defined(SOL_SOCKET) && defined(SO_BINDTODEVICE)
179   if(get_config_string(lookup_config(config_tree, "BindToInterface"), &interface))
180     {
181       memset(&ifr, 0, sizeof(ifr));
182       strncpy(ifr.ifr_ifrn.ifrn_name, interface, IFNAMSIZ);
183       if(setsockopt(nfd, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof(ifr)))
184         {
185           close(nfd);
186           syslog(LOG_ERR, _("Can't bind to interface %s: %s"), interface, strerror(errno));
187           return -1;
188         }
189     }
190 #endif
191
192   if(bind(nfd, &sa->sa, SALEN(sa->sa)))
193     {
194       close(nfd);
195       addrstr = sockaddr2hostname(sa);
196       syslog(LOG_ERR, _("Can't bind to %s/udp: %s"), addrstr, strerror(errno));
197       free(addrstr);
198       return -1;
199     }
200 cp
201   return nfd;
202 }
203
204 void retry_outgoing(outgoing_t *outgoing)
205 {
206   event_t *event;
207 cp
208   outgoing->timeout += 5;
209   if(outgoing->timeout > maxtimeout)
210     outgoing->timeout = maxtimeout;
211
212   event = new_event();
213   event->handler = (event_handler_t)setup_outgoing_connection;
214   event->time = now + outgoing->timeout;
215   event->data = outgoing;
216   event_add(event);
217
218   if(debug_lvl >= DEBUG_CONNECTIONS)
219     syslog(LOG_NOTICE, _("Trying to re-establish outgoing connection in %d seconds"), outgoing->timeout);
220 cp
221 }
222
223 int setup_outgoing_socket(connection_t *c)
224 {
225   int option;
226 cp
227   if(debug_lvl >= DEBUG_CONNECTIONS)
228     syslog(LOG_INFO, _("Trying to connect to %s (%s)"), c->name, c->hostname);
229
230   c->socket = socket(c->address.sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
231
232   if(c->socket == -1)
233     {
234       syslog(LOG_ERR, _("Creating socket for %s failed: %s"), c->hostname, strerror(errno));
235       return -1;
236     }
237
238   /* Optimize TCP settings */
239
240 #ifdef HAVE_LINUX
241   option = 1;
242   setsockopt(c->socket, SOL_TCP, TCP_NODELAY, &option, sizeof(option));
243
244   option = IPTOS_LOWDELAY;
245   setsockopt(c->socket, SOL_IP, IP_TOS, &option, sizeof(option));
246 #endif
247
248   /* Connect */
249
250   if(connect(c->socket, &c->address.sa, SALEN(c->address.sa)) == -1)
251     {
252       close(c->socket);
253       syslog(LOG_ERR, _("Error while connecting to %s (%s): %s"), c->name, c->hostname, strerror(errno));
254       return -1;
255     }
256
257   if(debug_lvl >= DEBUG_CONNECTIONS)
258     syslog(LOG_INFO, _("Connected to %s (%s)"), c->name, c->hostname);
259 cp
260   return 0;
261 }
262
263
264 void finish_connecting(connection_t *c)
265 {
266 cp
267   if(debug_lvl >= DEBUG_CONNECTIONS)
268     syslog(LOG_INFO, _("Connected to %s (%s)"), c->name, c->hostname);
269
270   c->last_ping_time = now;
271
272   send_id(c);
273 cp
274 }
275
276 void do_outgoing_connection(connection_t *c)
277 {
278   char *address, *port;
279   int option, result, flags;
280 cp
281 begin:
282   if(!c->outgoing->ai)
283     {
284       if(!c->outgoing->cfg)
285         {
286           if(debug_lvl >= DEBUG_CONNECTIONS)
287             syslog(LOG_ERR, _("Could not set up a meta connection to %s"), c->name);
288           c->status.remove = 1;
289           do_prune = 1;
290           retry_outgoing(c->outgoing);
291           return;
292         }
293
294       get_config_string(c->outgoing->cfg, &address);
295
296       if(!get_config_string(lookup_config(c->config_tree, "Port"), &port))
297         asprintf(&port, "655");
298
299       c->outgoing->ai = str2addrinfo(address, port, SOCK_STREAM);
300       free(address);
301       free(port);
302
303       c->outgoing->aip = c->outgoing->ai;
304       c->outgoing->cfg = lookup_config_next(c->config_tree, c->outgoing->cfg);
305     }
306
307   if(!c->outgoing->aip)
308     {
309       freeaddrinfo(c->outgoing->ai);
310       c->outgoing->ai = NULL;
311       goto begin;
312     }
313
314   memcpy(&c->address, c->outgoing->aip->ai_addr, c->outgoing->aip->ai_addrlen);
315   c->outgoing->aip = c->outgoing->aip->ai_next;
316
317   if(c->hostname)
318     free(c->hostname);
319
320   c->hostname = sockaddr2hostname(&c->address);
321
322   if(debug_lvl >= DEBUG_CONNECTIONS)
323     syslog(LOG_INFO, _("Trying to connect to %s (%s)"), c->name, c->hostname);
324
325   c->socket = socket(c->address.sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
326
327   if(c->socket == -1)
328     {
329       if(debug_lvl >= DEBUG_CONNECTIONS)
330         syslog(LOG_ERR, _("Creating socket for %s failed: %s"), c->hostname, strerror(errno));
331
332       goto begin;
333     }
334
335   /* Optimize TCP settings */
336
337 #ifdef HAVE_LINUX
338   option = 1;
339   setsockopt(c->socket, SOL_TCP, TCP_NODELAY, &option, sizeof(option));
340
341   option = IPTOS_LOWDELAY;
342   setsockopt(c->socket, SOL_IP, IP_TOS, &option, sizeof(option));
343 #endif
344
345   /* Non-blocking */
346
347   flags = fcntl(c->socket, F_GETFL);
348
349   if(fcntl(c->socket, F_SETFL, flags | O_NONBLOCK) < 0)
350     {
351       syslog(LOG_ERR, _("fcntl for %s: %s"), c->hostname, strerror(errno));
352     }
353
354   /* Connect */
355
356   result = connect(c->socket, &c->address.sa, SALEN(c->address.sa));
357
358   if(result == -1)
359     {
360       if(errno == EINPROGRESS)
361         {
362           c->status.connecting = 1;
363           return;
364         }
365
366       close(c->socket);
367
368       if(debug_lvl >= DEBUG_CONNECTIONS)
369         syslog(LOG_ERR, _("%s: %s"), c->hostname, strerror(errno));
370
371       goto begin;
372     }
373
374   finish_connecting(c);
375   return;
376 cp
377 }
378
379 void setup_outgoing_connection(outgoing_t *outgoing)
380 {
381   connection_t *c;
382   node_t *n;
383 cp
384   n = lookup_node(outgoing->name);
385   
386   if(n)
387     if(n->connection)
388       {
389         if(debug_lvl >= DEBUG_CONNECTIONS)       
390           syslog(LOG_INFO, _("Already connected to %s"), outgoing->name);
391         n->connection->outgoing = outgoing;
392         return;
393       }
394
395   c = new_connection();
396   c->name = xstrdup(outgoing->name);
397   c->outcipher = myself->connection->outcipher;
398   c->outdigest = myself->connection->outdigest;
399   c->outmaclength = myself->connection->outmaclength;
400   c->outcompression = myself->connection->outcompression;
401
402   init_configuration(&c->config_tree);
403   read_connection_config(c);
404   
405   outgoing->cfg = lookup_config(c->config_tree, "Address");
406   
407   if(!outgoing->cfg)
408     {
409       syslog(LOG_ERR, _("No address specified for %s"), c->name);
410       free_connection(c);
411       free(outgoing->name);
412       free(outgoing);
413       return;
414     }
415   
416   c->outgoing = outgoing;
417   c->last_ping_time = now;
418
419   connection_add(c);
420
421   do_outgoing_connection(c);
422 }
423
424 /*
425   accept a new tcp connect and create a
426   new connection
427 */
428 int handle_new_meta_connection(int sock)
429 {
430   connection_t *c;
431   sockaddr_t sa;
432   int fd, len = sizeof(sa);
433 cp
434   if((fd = accept(sock, &sa.sa, &len)) < 0)
435     {
436       syslog(LOG_ERR, _("Accepting a new connection failed: %s"), strerror(errno));
437       return -1;
438     }
439
440   sockaddrunmap(&sa);
441
442   c = new_connection();
443   c->outcipher = myself->connection->outcipher;
444   c->outdigest = myself->connection->outdigest;
445   c->outmaclength = myself->connection->outmaclength;
446   c->outcompression = myself->connection->outcompression;
447
448   c->address = sa;
449   c->hostname = sockaddr2hostname(&sa);
450   c->socket = fd;
451   c->last_ping_time = now;
452
453   if(debug_lvl >= DEBUG_CONNECTIONS)
454     syslog(LOG_NOTICE, _("Connection from %s"), c->hostname);
455
456   connection_add(c);
457
458   c->allow_request = ID;
459   send_id(c);
460 cp
461   return 0;
462 }
463
464 void try_outgoing_connections(void)
465 {
466   static config_t *cfg = NULL;
467   char *name;
468   outgoing_t *outgoing;
469 cp
470   for(cfg = lookup_config(config_tree, "ConnectTo"); cfg; cfg = lookup_config_next(config_tree, cfg))
471     {
472       get_config_string(cfg, &name);
473
474       if(check_id(name))
475         {
476           syslog(LOG_ERR, _("Invalid name for outgoing connection in %s line %d"), cfg->file, cfg->line);
477           free(name);
478           continue;
479         }
480
481       outgoing = xmalloc_and_zero(sizeof(*outgoing));
482       outgoing->name = name;
483       setup_outgoing_connection(outgoing);
484     }
485 }