f3ef1341246b9a8f6f0ce963f902161bfe6cd225
[tinc] / src / netutl.c
1 /*
2     netutl.c -- some supporting network utility code
3     Copyright (C) 1998,1999,2000 Ivo Timmermans <itimmermans@bigfoot.com>
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19     $Id: netutl.c,v 1.12.4.7 2000/08/17 16:51:07 guus Exp $
20 */
21
22 #include "config.h"
23
24 #include <arpa/inet.h>
25 #include <netdb.h>
26 #include <netinet/in.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <sys/socket.h>
31 #include <syslog.h>
32
33 #include <utils.h>
34 #include <xalloc.h>
35
36 #include "conf.h"
37 #include "encr.h"
38 #include "net.h"
39 #include "netutl.h"
40
41 #include "system.h"
42
43 /*
44   look for a connection associated with the given vpn ip,
45   return its connection structure.
46   Skips connections that are not activated!
47 */
48 conn_list_t *lookup_conn(ip_t ip)
49 {
50   conn_list_t *p = conn_list;
51 cp
52   /* Exact match suggested by James B. MacLean */
53   for(p = conn_list; p != NULL; p = p->next)
54     if((ip == p->vpn_ip) && p->status.active)
55       return p;
56   for(p = conn_list; p != NULL; p = p->next)
57     if(((ip & p->vpn_mask) == (p->vpn_ip & p->vpn_mask)) && p->status.active)
58       return p;
59 cp
60   return NULL;
61 }
62
63 /*
64   free a queue and all of its elements
65 */
66 void destroy_queue(packet_queue_t *pq)
67 {
68   queue_element_t *p, *q;
69 cp
70   for(p = pq->head; p != NULL; p = q)
71     {
72       q = p->next;
73       if(p->packet)
74         free(p->packet);
75       free(p);
76     }
77
78   free(pq);
79 cp
80 }
81
82 /*
83   free a conn_list_t element and all its pointers
84 */
85 void free_conn_element(conn_list_t *p)
86 {
87 cp
88   if(p->sq)
89     destroy_queue(p->sq);
90   if(p->rq)
91     destroy_queue(p->rq);
92   if(p->vpn_hostname)
93     free(p->vpn_hostname);
94   if(p->real_hostname)
95     free(p->real_hostname);
96   free_key(p->public_key);
97   free_key(p->key);
98   free(p);
99 cp
100 }
101
102 /*
103   remove all marked connections
104 */
105 void prune_conn_list(void)
106 {
107   conn_list_t *p, *prev = NULL, *next = NULL;
108 cp
109   for(p = conn_list; p != NULL; )
110     {
111       next = p->next;
112
113       if(p->status.remove)
114         {
115           if(prev)
116             prev->next = next;
117           else
118             conn_list = next;
119
120           free_conn_element(p);
121         }
122       else
123         prev = p;
124
125       p = next;
126     }
127 cp
128 }
129
130 /*
131   creates new conn_list element, and initializes it
132 */
133 conn_list_t *new_conn_list(void)
134 {
135   conn_list_t *p = xmalloc(sizeof(*p));
136 cp
137   /* initialise all those stupid pointers at once */
138   memset(p, '\0', sizeof(*p));
139   p->vpn_mask = (ip_t)(~0L); /* If this isn't done, it would be a
140                                 wastebucket for all packets with
141                                 unknown destination. */
142   p->nexthop = p;
143 cp
144   return p;
145 }
146
147 /*
148   free all elements of conn_list
149 */
150 void destroy_conn_list(void)
151 {
152   conn_list_t *p, *next;
153 cp
154   for(p = conn_list; p != NULL; )
155     {
156       next = p->next;
157       free_conn_element(p);
158       p = next;
159     }
160
161   conn_list = NULL;
162 cp
163 }
164
165 /*
166   look up the name associated with the ip
167   address `addr'
168 */
169
170 char *hostlookup(unsigned long addr)
171 {
172   char *name;
173   struct hostent *host = NULL;
174   struct in_addr in;
175   config_t const *cfg;
176   int lookup_hostname;
177 cp
178   in.s_addr = addr;
179
180   lookup_hostname = 0;
181   if((cfg = get_config_val(resolve_dns)) != NULL)
182     if(cfg->data.val == stupid_true)
183       lookup_hostname = 1;
184
185   if(lookup_hostname)
186     host = gethostbyaddr((char *)&in, sizeof(in), AF_INET);
187
188   if(!lookup_hostname || !host)
189     {
190       asprintf(&name, "%s", inet_ntoa(in));
191     }
192   else
193     {
194       asprintf(&name, "%s", host->h_name);
195     }
196 cp
197   return name;
198 }
199
200 /*
201   Turn a string into an IP addy with netmask
202   return NULL on failure
203 */
204 ip_mask_t *strtoip(char *str)
205 {
206   ip_mask_t *ip;
207   int masker;
208   char *q, *p;
209   struct hostent *h;
210 cp
211   p = str;
212   if((q = strchr(p, '/')))
213     {
214       *q = '\0';
215       q++; /* q now points to netmask part, or NULL if no mask */
216     }
217
218   if(!(h = gethostbyname(p)))
219     {
220       fprintf(stderr, _("Error looking up `%s': %s\n"), p, sys_errlist[h_errno]);
221       return NULL;
222     }
223
224   masker = 0;
225   if(q)
226     {
227       masker = strtol(q, &p, 10);
228       if(q == p || (*p))
229         return NULL;
230     }
231
232   ip = xmalloc(sizeof(*ip));
233   ip->ip = ntohl(*((ip_t*)(h->h_addr_list[0])));
234
235   ip->mask = masker ? ~((1 << (32 - masker)) - 1) : 0;
236 cp
237   return ip;
238 }
239
240 void dump_conn_list(void)
241 {
242   conn_list_t *p;
243 cp
244   syslog(LOG_DEBUG, _("Connection list:"));
245
246   for(p = conn_list; p != NULL; p = p->next)
247     {
248       syslog(LOG_DEBUG, _("%s netmask %d.%d.%d.%d at %s port %hd flags %d sockets %d, %d status %04x"),
249              p->vpn_hostname, IP_ADDR_V(p->vpn_mask), p->real_hostname, p->port, p->flags,
250              p->socket, p->meta_socket, p->status);
251     }
252 cp
253 }