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.16 2000/11/22 18:54:08 guus Exp $
24 #include <arpa/inet.h>
26 #include <netinet/in.h>
30 #include <sys/socket.h>
45 free a queue and all of its elements
47 void destroy_queue(packet_queue_t *pq)
49 queue_element_t *p, *q;
51 for(p = pq->head; p != NULL; p = q)
64 char *hostlookup(unsigned long addr)
67 struct hostent *host = NULL;
75 if((cfg = get_config_val(config, config_hostnames)) != NULL)
76 if(cfg->data.val == stupid_true)
80 host = gethostbyaddr((char *)&in, sizeof(in), AF_INET);
82 if(!lookup_hostname || !host)
84 asprintf(&name, "%s", inet_ntoa(in));
88 asprintf(&name, "%s", host->h_name);
95 Turn a string into an IP addy with netmask
96 return NULL on failure
98 ip_mask_t *strtoip(char *str)
106 if((q = strchr(p, '/')))
109 q++; /* q now points to netmask part, or NULL if no mask */
112 if(!(h = gethostbyname(p)))
114 if(debug_lvl >= DEBUG_ERROR)
115 syslog(LOG_WARNING, _("Error looking up `%s': %s\n"), p, strerror(errno));
123 masker = strtol(q, &p, 10);
128 ip = xmalloc(sizeof(*ip));
129 ip->address = ntohl(*((ip_t*)(h->h_addr_list[0])));
131 ip->mask = masker ? ~((1 << (32 - masker)) - 1) : 0;