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