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