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