2 netutl.c -- some supporting network utility code
3 Copyright (C) 1998,99 Ivo Timmermans <zarq@iname.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.
22 #include <arpa/inet.h>
24 #include <netinet/in.h>
28 #include <sys/socket.h>
39 look for a connection associated with the given vpn ip,
40 return its connection structure
42 conn_list_t *lookup_conn(ip_t ip)
44 conn_list_t *p = conn_list;
46 /* Exact match suggested by James B. MacLean */
47 for(p = conn_list; p != NULL; p = p->next)
50 for(p = conn_list; p != NULL; p = p->next)
51 if((ip & p->vpn_mask) == (p->vpn_ip & p->vpn_mask))
58 free a queue and all of its elements
60 void destroy_queue(packet_queue_t *pq)
62 queue_element_t *p, *q;
64 for(p = pq->head; p != NULL; p = q)
76 free a conn_list_t element and all its pointers
78 void free_conn_element(conn_list_t *p)
88 free_key(p->public_key);
94 remove all marked connections
96 void prune_conn_list(void)
98 conn_list_t *p, *prev = NULL, *next = NULL;
100 for(p = conn_list; p != NULL; )
111 free_conn_element(p);
121 creates new conn_list element, and initializes it
123 conn_list_t *new_conn_list(void)
125 conn_list_t *p = xmalloc(sizeof(conn_list_t));
127 /* initialise all those stupid pointers at once */
128 memset(p, '\0', sizeof(conn_list_t));
134 free all elements of conn_list
136 void destroy_conn_list(void)
138 conn_list_t *p, *next;
141 for(p = conn_list; p != NULL; )
144 free_conn_element(p);
153 look up the name associated with the ip
156 char *hostlookup(unsigned long addr)
159 struct hostent *host = NULL;
164 host = gethostbyaddr((char *)&in, sizeof(in), AF_INET);
168 name = xmalloc(strlen(host->h_name)+20);
169 sprintf(name, "%s (%s)", host->h_name, inet_ntoa(in));
174 sprintf(name, "%s", inet_ntoa(in));
181 Turn a string into an IP addy with netmask
182 return NULL on failure
184 ip_mask_t *strtoip(char *str)
192 if((q = strchr(p, '/')))
195 q++; /* q now points to netmask part, or NULL if no mask */
198 if(!(h = gethostbyname(p)))
200 fprintf(stderr, "Error looking up `%s': %s\n", p, sys_errlist[h_errno]);
207 masker = strtol(q, &p, 10);
212 ip = xmalloc(sizeof(ip_mask_t));
213 ip->ip = ntohl(*((ip_t*)(h->h_addr_list[0])));
215 ip->mask = masker ? ~((1 << (32 - masker)) - 1) : 0;
220 void dump_conn_list(void)
224 syslog(LOG_DEBUG, "Connection list:");
226 for(p = conn_list; p != NULL; p = p->next)
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);