Cleanups.
[tinc] / src / netutl.c
1 /*
2     netutl.c -- some supporting network utility code
3     Copyright (C) 1998,1999,2000 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->sq)
85     destroy_queue(p->sq);
86   if(p->rq)
87     destroy_queue(p->rq);
88   free_key(p->public_key);
89   free_key(p->key);
90   free(p);
91 cp
92 }
93
94 /*
95   remove all marked connections
96 */
97 void prune_conn_list(void)
98 {
99   conn_list_t *p, *prev = NULL, *next = NULL;
100 cp
101   for(p = conn_list; p != NULL; )
102     {
103       next = p->next;
104
105       if(p->status.remove)
106         {
107           if(prev)
108             prev->next = next;
109           else
110             conn_list = next;
111
112           free_conn_element(p);
113         }
114       else
115         prev = p;
116
117       p = next;
118     }
119 cp
120 }
121
122 /*
123   creates new conn_list element, and initializes it
124 */
125 conn_list_t *new_conn_list(void)
126 {
127   conn_list_t *p = xmalloc(sizeof(*p));
128 cp
129   /* initialise all those stupid pointers at once */
130   memset(p, '\0', sizeof(*p));
131   p->vpn_mask = (ip_t)(~0L); /* If this isn't done, it would be a
132                                 wastebucket for all packets with
133                                 unknown destination. */
134   p->nexthop = p;
135 cp
136   return p;
137 }
138
139 /*
140   free all elements of conn_list
141 */
142 void destroy_conn_list(void)
143 {
144   conn_list_t *p, *next;
145 cp
146   for(p = conn_list; p != NULL; )
147     {
148       next = p->next;
149       free_conn_element(p);
150       p = next;
151     }
152
153   conn_list = NULL;
154 cp
155 }
156
157 /*
158   look up the name associated with the ip
159   address `addr'
160 */
161 char *hostlookup(unsigned long addr)
162 {
163   char *name;
164   struct hostent *host = NULL;
165   struct in_addr in;
166 cp
167   in.s_addr = addr;
168
169   host = gethostbyaddr((char *)&in, sizeof(in), AF_INET);
170
171   if(host)
172     {
173       name = xmalloc(strlen(host->h_name)+20);
174       sprintf(name, "%s (%s)", host->h_name, inet_ntoa(in));
175     }
176   else
177     {
178       name = xmalloc(20);
179       sprintf(name, "%s", inet_ntoa(in));
180     }
181 cp
182   return name;
183 }
184
185 /*
186   Turn a string into an IP addy with netmask
187   return NULL on failure
188 */
189 ip_mask_t *strtoip(char *str)
190 {
191   ip_mask_t *ip;
192   int masker;
193   char *q, *p;
194   struct hostent *h;
195 cp
196   p = str;
197   if((q = strchr(p, '/')))
198     {
199       *q = '\0';
200       q++; /* q now points to netmask part, or NULL if no mask */
201     }
202
203   if(!(h = gethostbyname(p)))
204     {
205       fprintf(stderr, "Error looking up `%s': %s\n", p, sys_errlist[h_errno]);
206       return NULL;
207     }
208
209   masker = 0;
210   if(q)
211     {
212       masker = strtol(q, &p, 10);
213       if(q == p || (*p))
214         return NULL;
215     }
216
217   ip = xmalloc(sizeof(*ip));
218   ip->ip = ntohl(*((ip_t*)(h->h_addr_list[0])));
219
220   ip->mask = masker ? ~((1 << (32 - masker)) - 1) : 0;
221 cp
222   return ip;
223 }
224
225 void dump_conn_list(void)
226 {
227   conn_list_t *p;
228 cp
229   syslog(LOG_DEBUG, "Connection list:");
230
231   for(p = conn_list; p != NULL; p = p->next)
232     {
233       syslog(LOG_DEBUG, " " IP_ADDR_S "/" IP_ADDR_S ": %04x (%d|%d)",
234              IP_ADDR_V(p->vpn_ip), IP_ADDR_V(p->vpn_mask), p->status,
235              p->socket, p->meta_socket);
236     }
237 cp
238 }