2 netutl.c -- some supporting network utility code
3 Copyright (C) 1998-2001 Ivo Timmermans <itimmermans@bigfoot.com>
4 2000,2001 Guus Sliepen <guus@sliepen.warande.net>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 $Id: netutl.c,v 1.12.4.19 2001/05/07 19:08:46 guus Exp $
25 #include <arpa/inet.h>
27 #include <netinet/in.h>
31 #include <sys/socket.h>
44 char *hostlookup(unsigned long addr)
47 struct hostent *host = NULL;
55 if((cfg = get_config_val(config, config_hostnames)) != NULL)
56 if(cfg->data.val == stupid_true)
60 host = gethostbyaddr((char *)&in, sizeof(in), AF_INET);
62 if(!lookup_hostname || !host)
64 asprintf(&name, "%s", inet_ntoa(in));
68 asprintf(&name, "%s", host->h_name);
75 Turn a string into an IP addy with netmask
76 return NULL on failure
78 ip_mask_t *strtoip(char *str)
86 if((q = strchr(p, '/')))
89 q++; /* q now points to netmask part, or NULL if no mask */
92 if(!(h = gethostbyname(p)))
94 if(debug_lvl >= DEBUG_ERROR)
95 syslog(LOG_WARNING, _("Error looking up `%s': %s\n"), p, strerror(errno));
103 masker = strtol(q, &p, 10);
108 ip = xmalloc(sizeof(*ip));
109 ip->address = ntohl(*((ipv4_t*)(h->h_addr_list[0])));
111 ip->mask = masker ? ~((1 << (32 - masker)) - 1) : 0;