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