Moving files, first attempt at gcrypt compatibility, more interface
[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.3 2002/04/13 11:07:12 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 <unistd.h>
40 #include <sys/ioctl.h>
41 /* SunOS really wants sys/socket.h BEFORE net/if.h,
42    and FreeBSD wants these lines below the rest. */
43 #include <arpa/inet.h>
44 #include <sys/socket.h>
45 #include <net/if.h>
46
47 #include <utils.h>
48 #include <xalloc.h>
49 #include <avl_tree.h>
50 #include <list.h>
51
52 #include "conf.h"
53 #include "connection.h"
54 #include "meta.h"
55 #include "net.h"
56 #include "netutl.h"
57 #include "process.h"
58 #include "protocol.h"
59 #include "subnet.h"
60 #include "graph.h"
61 #include "process.h"
62 #include "route.h"
63 #include "device.h"
64 #include "event.h"
65 #include "logging.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           retry_outgoing(c->outgoing);
290           return;
291         }
292
293       get_config_string(c->outgoing->cfg, &address);
294
295       if(!get_config_string(lookup_config(c->config_tree, "Port"), &port))
296         asprintf(&port, "655");
297
298       c->outgoing->ai = str2addrinfo(address, port, SOCK_STREAM);
299       free(address);
300       free(port);
301
302       c->outgoing->aip = c->outgoing->ai;
303       c->outgoing->cfg = lookup_config_next(c->config_tree, c->outgoing->cfg);
304     }
305
306   if(!c->outgoing->aip)
307     {
308       freeaddrinfo(c->outgoing->ai);
309       c->outgoing->ai = NULL;
310       goto begin;
311     }
312
313   memcpy(&c->address, c->outgoing->aip->ai_addr, c->outgoing->aip->ai_addrlen);
314   c->outgoing->aip = c->outgoing->aip->ai_next;
315
316   if(c->hostname)
317     free(c->hostname);
318
319   c->hostname = sockaddr2hostname(&c->address);
320
321   if(debug_lvl >= DEBUG_CONNECTIONS)
322     syslog(LOG_INFO, _("Trying to connect to %s (%s)"), c->name, c->hostname);
323
324   c->socket = socket(c->address.sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
325
326   if(c->socket == -1)
327     {
328       if(debug_lvl >= DEBUG_CONNECTIONS)
329         syslog(LOG_ERR, _("Creating socket for %s failed: %s"), c->hostname, strerror(errno));
330
331       goto begin;
332     }
333
334   /* Optimize TCP settings */
335
336 #ifdef HAVE_LINUX
337   option = 1;
338   setsockopt(c->socket, SOL_TCP, TCP_NODELAY, &option, sizeof(option));
339
340   option = IPTOS_LOWDELAY;
341   setsockopt(c->socket, SOL_IP, IP_TOS, &option, sizeof(option));
342 #endif
343
344   /* Non-blocking */
345
346   flags = fcntl(c->socket, F_GETFL);
347
348   if(fcntl(c->socket, F_SETFL, flags | O_NONBLOCK) < 0)
349     {
350       syslog(LOG_ERR, _("fcntl for %s: %s"), c->hostname, strerror(errno));
351     }
352
353   /* Connect */
354
355   result = connect(c->socket, &c->address.sa, SALEN(c->address.sa));
356
357   if(result == -1)
358     {
359       if(errno == EINPROGRESS)
360         {
361           c->status.connecting = 1;
362           return;
363         }
364
365       close(c->socket);
366
367       if(debug_lvl >= DEBUG_CONNECTIONS)
368         syslog(LOG_ERR, _("%s: %s"), c->hostname, strerror(errno));
369
370       goto begin;
371     }
372
373   finish_connecting(c);
374   return;
375 cp
376 }
377
378 void setup_outgoing_connection(outgoing_t *outgoing)
379 {
380   connection_t *c;
381   node_t *n;
382 cp
383   n = lookup_node(outgoing->name);
384   
385   if(n)
386     if(n->connection)
387       {
388         if(debug_lvl >= DEBUG_CONNECTIONS)       
389           syslog(LOG_INFO, _("Already connected to %s"), outgoing->name);
390         n->connection->outgoing = outgoing;
391         return;
392       }
393
394   c = new_connection();
395   c->name = xstrdup(outgoing->name);
396   c->outcipher = myself->connection->outcipher;
397   c->outdigest = myself->connection->outdigest;
398   c->outmaclength = myself->connection->outmaclength;
399   c->outcompression = myself->connection->outcompression;
400
401   init_configuration(&c->config_tree);
402   read_connection_config(c);
403   
404   outgoing->cfg = lookup_config(c->config_tree, "Address");
405   
406   if(!outgoing->cfg)
407     {
408       syslog(LOG_ERR, _("No address specified for %s"), c->name);
409       free_connection(c);
410       free(outgoing->name);
411       free(outgoing);
412       return;
413     }
414   
415   c->outgoing = outgoing;
416   c->last_ping_time = now;
417
418   connection_add(c);
419
420   do_outgoing_connection(c);
421 }
422
423 /*
424   accept a new tcp connect and create a
425   new connection
426 */
427 int handle_new_meta_connection(int sock)
428 {
429   connection_t *c;
430   sockaddr_t sa;
431   int fd, len = sizeof(sa);
432 cp
433   if((fd = accept(sock, &sa.sa, &len)) < 0)
434     {
435       syslog(LOG_ERR, _("Accepting a new connection failed: %s"), strerror(errno));
436       return -1;
437     }
438
439   sockaddrunmap(&sa);
440
441   c = new_connection();
442   c->outcipher = myself->connection->outcipher;
443   c->outdigest = myself->connection->outdigest;
444   c->outmaclength = myself->connection->outmaclength;
445   c->outcompression = myself->connection->outcompression;
446
447   c->address = sa;
448   c->hostname = sockaddr2hostname(&sa);
449   c->socket = fd;
450   c->last_ping_time = now;
451
452   if(debug_lvl >= DEBUG_CONNECTIONS)
453     syslog(LOG_NOTICE, _("Connection from %s"), c->hostname);
454
455   connection_add(c);
456
457   c->allow_request = ID;
458   send_id(c);
459 cp
460   return 0;
461 }
462
463 void try_outgoing_connections(void)
464 {
465   static config_t *cfg = NULL;
466   char *name;
467   outgoing_t *outgoing;
468 cp
469   for(cfg = lookup_config(config_tree, "ConnectTo"); cfg; cfg = lookup_config_next(config_tree, cfg))
470     {
471       get_config_string(cfg, &name);
472
473       if(check_id(name))
474         {
475           syslog(LOG_ERR, _("Invalid name for outgoing connection in %s line %d"), cfg->file, cfg->line);
476           free(name);
477           continue;
478         }
479
480       outgoing = xmalloc_and_zero(sizeof(*outgoing));
481       outgoing->name = name;
482       setup_outgoing_connection(outgoing);
483     }
484 }