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