Initially, the vpn_mask of a connection is 255.255.255.255 to avoid confusion with...
[oweals/tinc.git] / src / netutl.c
1 /*
2     netutl.c -- some supporting network utility code
3     Copyright (C) 1998,99 Ivo Timmermans <zarq@iname.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
20 #include "config.h"
21
22 #include <arpa/inet.h>
23 #include <netdb.h>
24 #include <netinet/in.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <sys/socket.h>
29 #include <syslog.h>
30
31 #include <utils.h>
32 #include <xalloc.h>
33
34 #include "encr.h"
35 #include "net.h"
36 #include "netutl.h"
37
38 /*
39   look for a connection associated with the given vpn ip,
40   return its connection structure
41 */
42 conn_list_t *lookup_conn(ip_t ip)
43 {
44   conn_list_t *p = conn_list;
45 cp
46   /* Exact match suggested by James B. MacLean */
47   for(p = conn_list; p != NULL; p = p->next)
48     if(ip  == p->vpn_ip)
49       return p;
50   for(p = conn_list; p != NULL; p = p->next)
51     if((ip & p->vpn_mask) == (p->vpn_ip & p->vpn_mask))
52       return p;
53 cp
54   return NULL;
55 }
56
57 /*
58   free a queue and all of its elements
59 */
60 void destroy_queue(packet_queue_t *pq)
61 {
62   queue_element_t *p, *q;
63 cp
64   for(p = pq->head; p != NULL; p = q)
65     {
66       q = p->next;
67       if(p->packet)
68         free(p->packet);
69       free(p);
70     }
71
72   free(pq);
73 cp
74 }
75
76 /*
77   free a conn_list_t element and all its pointers
78 */
79 void free_conn_element(conn_list_t *p)
80 {
81 cp
82   if(p->hostname)
83     free(p->hostname);
84   if(p->pp)
85     free(p->pp);
86   if(p->sq)
87     destroy_queue(p->sq);
88   if(p->rq)
89     destroy_queue(p->rq);
90   free_key(p->public_key);
91   free_key(p->key);
92   free(p);
93 cp
94 }
95
96 /*
97   remove all marked connections
98 */
99 void prune_conn_list(void)
100 {
101   conn_list_t *p, *prev = NULL, *next = NULL;
102 cp
103   for(p = conn_list; p != NULL; )
104     {
105       next = p->next;
106
107       if(p->status.remove)
108         {
109           if(prev)
110             prev->next = next;
111           else
112             conn_list = next;
113
114           free_conn_element(p);
115         }
116       else
117         prev = p;
118
119       p = next;
120     }
121 cp
122 }
123
124 /*
125   creates new conn_list element, and initializes it
126 */
127 conn_list_t *new_conn_list(void)
128 {
129   conn_list_t *p = xmalloc(sizeof(*p));
130 cp
131   /* initialise all those stupid pointers at once */
132   memset(p, '\0', sizeof(*p));
133   p->vpn_mask = (ip_t)(~0L); /* If this isn't done, it would be a
134                                 wastebucket for all packets with
135                                 unknown destination. */
136   p->nexthop = p;
137 cp
138   return p;
139 }
140
141 /*
142   free all elements of conn_list
143 */
144 void destroy_conn_list(void)
145 {
146   conn_list_t *p, *next;
147 cp
148   for(p = conn_list; p != NULL; )
149     {
150       next = p->next;
151       free_conn_element(p);
152       p = next;
153     }
154
155   conn_list = NULL;
156 cp
157 }
158
159 /*
160   look up the name associated with the ip
161   address `addr'
162 */
163 char *hostlookup(unsigned long addr)
164 {
165   char *name;
166   struct hostent *host = NULL;
167   struct in_addr in;
168 cp
169   in.s_addr = addr;
170
171   host = gethostbyaddr((char *)&in, sizeof(in), AF_INET);
172
173   if(host)
174     {
175       name = xmalloc(strlen(host->h_name)+20);
176       sprintf(name, "%s (%s)", host->h_name, inet_ntoa(in));
177     }
178   else
179     {
180       name = xmalloc(20);
181       sprintf(name, "%s", inet_ntoa(in));
182     }
183 cp
184   return name;
185 }
186
187 /*
188   Turn a string into an IP addy with netmask
189   return NULL on failure
190 */
191 ip_mask_t *strtoip(char *str)
192 {
193   ip_mask_t *ip;
194   int masker;
195   char *q, *p;
196   struct hostent *h;
197 cp
198   p = str;
199   if((q = strchr(p, '/')))
200     {
201       *q = '\0';
202       q++; /* q now points to netmask part, or NULL if no mask */
203     }
204
205   if(!(h = gethostbyname(p)))
206     {
207       fprintf(stderr, "Error looking up `%s': %s\n", p, sys_errlist[h_errno]);
208       return NULL;
209     }
210
211   masker = 0;
212   if(q)
213     {
214       masker = strtol(q, &p, 10);
215       if(q == p || (*p))
216         return NULL;
217     }
218
219   ip = xmalloc(sizeof(*ip));
220   ip->ip = ntohl(*((ip_t*)(h->h_addr_list[0])));
221
222   ip->mask = masker ? ~((1 << (32 - masker)) - 1) : 0;
223 cp
224   return ip;
225 }
226
227 void dump_conn_list(void)
228 {
229   conn_list_t *p;
230 cp
231   syslog(LOG_DEBUG, "Connection list:");
232
233   for(p = conn_list; p != NULL; p = p->next)
234     {
235       syslog(LOG_DEBUG, " " IP_ADDR_S "/" IP_ADDR_S ": %04x (%d|%d)",
236              IP_ADDR_V(p->vpn_ip), IP_ADDR_V(p->vpn_mask), p->status,
237              p->socket, p->meta_socket);
238     }
239 cp
240 }