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.13 2000/10/24 15:46:17 guus Exp $
24 #include <arpa/inet.h>
26 #include <netinet/in.h>
30 #include <sys/socket.h>
46 free a queue and all of its elements
48 void destroy_queue(packet_queue_t *pq)
50 queue_element_t *p, *q;
52 for(p = pq->head; p != NULL; p = q)
65 char *hostlookup(unsigned long addr)
68 struct hostent *host = NULL;
76 if((cfg = get_config_val(config, resolve_dns)) != NULL)
77 if(cfg->data.val == stupid_true)
81 host = gethostbyaddr((char *)&in, sizeof(in), AF_INET);
83 if(!lookup_hostname || !host)
85 asprintf(&name, "%s", inet_ntoa(in));
89 asprintf(&name, "%s", host->h_name);
96 Turn a string into an IP addy with netmask
97 return NULL on failure
99 ip_mask_t *strtoip(char *str)
107 if((q = strchr(p, '/')))
110 q++; /* q now points to netmask part, or NULL if no mask */
113 if(!(h = gethostbyname(p)))
115 fprintf(stderr, _("Error looking up `%s': %s\n"), p, strerror(errno));
122 masker = strtol(q, &p, 10);
127 ip = xmalloc(sizeof(*ip));
128 ip->address = ntohl(*((ip_t*)(h->h_addr_list[0])));
130 ip->mask = masker ? ~((1 << (32 - masker)) - 1) : 0;