Debug level tweaking.
[tinc] / 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(conn_list_t));
130 cp
131   /* initialise all those stupid pointers at once */
132   memset(p, '\0', sizeof(conn_list_t));
133   p->nexthop = p;
134 cp
135   return p;
136 }
137
138 /*
139   free all elements of conn_list
140 */
141 void destroy_conn_list(void)
142 {
143   conn_list_t *p, *next;
144 cp
145   for(p = conn_list; p != NULL; )
146     {
147       next = p->next;
148       free_conn_element(p);
149       p = next;
150     }
151
152   conn_list = NULL;
153 cp
154 }
155
156 /*
157   look up the name associated with the ip
158   address `addr'
159 */
160 char *hostlookup(unsigned long addr)
161 {
162   char *name;
163   struct hostent *host = NULL;
164   struct in_addr in;
165 cp
166   in.s_addr = addr;
167
168   host = gethostbyaddr((char *)&in, sizeof(in), AF_INET);
169
170   if(host)
171     {
172       name = xmalloc(strlen(host->h_name)+20);
173       sprintf(name, "%s (%s)", host->h_name, inet_ntoa(in));
174     }
175   else
176     {
177       name = xmalloc(20);
178       sprintf(name, "%s", inet_ntoa(in));
179     }
180 cp
181   return name;
182 }
183
184 /*
185   Turn a string into an IP addy with netmask
186   return NULL on failure
187 */
188 ip_mask_t *strtoip(char *str)
189 {
190   ip_mask_t *ip;
191   int masker;
192   char *q, *p;
193   struct hostent *h;
194 cp
195   p = str;
196   if((q = strchr(p, '/')))
197     {
198       *q = '\0';
199       q++; /* q now points to netmask part, or NULL if no mask */
200     }
201
202   if(!(h = gethostbyname(p)))
203     {
204       fprintf(stderr, "Error looking up `%s': %s\n", p, sys_errlist[h_errno]);
205       return NULL;
206     }
207
208   masker = 0;
209   if(q)
210     {
211       masker = strtol(q, &p, 10);
212       if(q == p || (*p))
213         return NULL;
214     }
215
216   ip = xmalloc(sizeof(ip_mask_t));
217   ip->ip = ntohl(*((ip_t*)(h->h_addr_list[0])));
218
219   ip->mask = masker ? ~((1 << (32 - masker)) - 1) : 0;
220 cp
221   return ip;
222 }
223
224 void dump_conn_list(void)
225 {
226   conn_list_t *p;
227 cp
228   syslog(LOG_DEBUG, "Connection list:");
229
230   for(p = conn_list; p != NULL; p = p->next)
231     {
232       syslog(LOG_DEBUG, " " IP_ADDR_S "/" IP_ADDR_S ": %04x (%d|%d)",
233              IP_ADDR_V(p->vpn_ip), IP_ADDR_V(p->vpn_mask), p->status,
234              p->socket, p->meta_socket);
235     }
236 cp
237 }