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