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