2 netutl.c -- some supporting network utility code
3 Copyright (C) 1998-2002 Ivo Timmermans <itimmermans@bigfoot.com>
4 2000-2002 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.23 2002/02/11 10:16:18 guus Exp $
27 #include <netinet/in.h>
31 #include <sys/socket.h>
33 #include <arpa/inet.h>
45 char *hostlookup(unsigned long addr)
48 struct hostent *host = NULL;
50 int lookup_hostname = 0;
54 get_config_bool(lookup_config(config_tree, "Hostnames"), &lookup_hostname);
57 host = gethostbyaddr((char *)&in, sizeof(in), AF_INET);
59 if(!lookup_hostname || !host)
61 asprintf(&name, "%s", inet_ntoa(in));
65 asprintf(&name, "%s", host->h_name);
72 Turn a string into an IP address
73 return NULL on failure
74 Should support IPv6 and other stuff in the future.
76 ipv4_t str2address(char *str)
81 if(!(h = gethostbyname(str)))
83 if(debug_lvl >= DEBUG_ERROR)
84 syslog(LOG_WARNING, _("Error looking up `%s': %s\n"), str, strerror(errno));
89 address = ntohl(*((ipv4_t*)(h->h_addr_list[0])));
94 char *address2str(ipv4_t address)
98 asprintf(&str, "%hu.%hu.%hu.%hu",
99 (unsigned short int)((address >> 24) & 255),
100 (unsigned short int)((address >> 16) & 255),
101 (unsigned short int)((address >> 8) & 255),
102 (unsigned short int)(address & 255));