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