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