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