2 netutl.c -- some supporting network utility code
3 Copyright (C) 1998,1999,2000 Ivo Timmermans <itimmermans@bigfoot.com>
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.
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.
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.
19 $Id: netutl.c,v 1.12.4.6 2000/06/29 17:09:06 guus Exp $
24 #include <arpa/inet.h>
26 #include <netinet/in.h>
30 #include <sys/socket.h>
44 look for a connection associated with the given vpn ip,
45 return its connection structure.
46 Skips connections that are not activated!
48 conn_list_t *lookup_conn(ip_t ip)
50 conn_list_t *p = conn_list;
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)
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)
64 free a queue and all of its elements
66 void destroy_queue(packet_queue_t *pq)
68 queue_element_t *p, *q;
70 for(p = pq->head; p != NULL; p = q)
83 free a conn_list_t element and all its pointers
85 void free_conn_element(conn_list_t *p)
93 free(p->vpn_hostname);
95 free(p->real_hostname);
96 free_key(p->public_key);
103 remove all marked connections
105 void prune_conn_list(void)
107 conn_list_t *p, *prev = NULL, *next = NULL;
109 for(p = conn_list; p != NULL; )
120 free_conn_element(p);
131 creates new conn_list element, and initializes it
133 conn_list_t *new_conn_list(void)
135 conn_list_t *p = xmalloc(sizeof(*p));
137 /* initialise all those stupid pointers at once */
138 memset(p, '\0', sizeof(*p));
139 p->vpn_mask = (ip_t)(~0L); /* If this isn't done, it would be a
140 wastebucket for all packets with
141 unknown destination. */
148 free all elements of conn_list
150 void destroy_conn_list(void)
152 conn_list_t *p, *next;
154 for(p = conn_list; p != NULL; )
157 free_conn_element(p);
166 look up the name associated with the ip
170 char *hostlookup(unsigned long addr)
173 struct hostent *host = NULL;
181 if((cfg = get_config_val(resolve_dns)) != NULL)
182 if(cfg->data.val == stupid_true)
186 host = gethostbyaddr((char *)&in, sizeof(in), AF_INET);
188 if(!lookup_hostname || !host)
191 sprintf(name, "%s", inet_ntoa(in));
195 name = xmalloc(strlen(host->h_name)+1);
196 sprintf(name, "%s", host->h_name);
203 Turn a string into an IP addy with netmask
204 return NULL on failure
206 ip_mask_t *strtoip(char *str)
214 if((q = strchr(p, '/')))
217 q++; /* q now points to netmask part, or NULL if no mask */
220 if(!(h = gethostbyname(p)))
222 fprintf(stderr, _("Error looking up `%s': %s\n"), p, sys_errlist[h_errno]);
229 masker = strtol(q, &p, 10);
234 ip = xmalloc(sizeof(*ip));
235 ip->ip = ntohl(*((ip_t*)(h->h_addr_list[0])));
237 ip->mask = masker ? ~((1 << (32 - masker)) - 1) : 0;
242 void dump_conn_list(void)
246 syslog(LOG_DEBUG, _("Connection list:"));
248 for(p = conn_list; p != NULL; p = p->next)
250 syslog(LOG_DEBUG, _("%s netmask %d.%d.%d.%d at %s port %hd flags %d sockets %d, %d status %04x"),
251 p->vpn_hostname, IP_ADDR_V(p->vpn_mask), p->real_hostname, p->port, p->flags,
252 p->socket, p->meta_socket, p->status);