diff options
author | David Cantrell <dcantrell@redhat.com> | 2008-09-26 16:41:21 -1000 |
---|---|---|
committer | Bill Nottingham <notting@redhat.com> | 2009-03-19 15:50:32 -0400 |
commit | 8d96a60141a556e25e251d817f072aca7e4d4e8d (patch) | |
tree | f87a26ebc7fa0237c6ddaa7428780fd63e414521 /src/ipcalc.c | |
parent | 3319050ec2e58442a4feb9288f36db76f5265a0a (diff) | |
download | initscripts-8d96a60141a556e25e251d817f072aca7e4d4e8d.tar initscripts-8d96a60141a556e25e251d817f072aca7e4d4e8d.tar.gz initscripts-8d96a60141a556e25e251d817f072aca7e4d4e8d.tar.bz2 initscripts-8d96a60141a556e25e251d817f072aca7e4d4e8d.tar.xz initscripts-8d96a60141a556e25e251d817f072aca7e4d4e8d.zip |
Expand get_hostname() function to support IPv4 and IPv6.
Make sure that get_hostname() if the user specifies either address
family.
Diffstat (limited to 'src/ipcalc.c')
-rw-r--r-- | src/ipcalc.c | 36 |
1 files changed, 29 insertions, 7 deletions
diff --git a/src/ipcalc.c b/src/ipcalc.c index 82afa75f..ebb7f61a 100644 --- a/src/ipcalc.c +++ b/src/ipcalc.c @@ -164,21 +164,36 @@ struct in_addr calc_network(struct in_addr addr, int prefix) } /*! - \fn const char *get_hostname(struct in_addr addr) + \fn const char *get_hostname(int family, void *addr) \brief returns the hostname associated with the specified IP address - \param addr an IP address to find a hostname for, in network byte order + \param family the address family, either AF_INET or AF_INET6. + \param addr an IP address to find a hostname for, in network byte order, + should either be a pointer to a struct in_addr or a struct in6_addr. \return a hostname, or NULL if one cannot be determined. Hostname is stored in a static buffer that may disappear at any time, the caller should copy the data if it needs permanent storage. */ -const char *get_hostname(struct in_addr addr) +char *get_hostname(int family, void *addr) { - struct hostent * hostinfo; + struct hostent * hostinfo = NULL; int x; + struct in_addr addr4; + struct in6_addr addr6; + + if (family == AF_INET) { + memset(&addr4, 0, sizeof(addr4)); + memcpy(&addr4, addr, sizeof(addr4)); + hostinfo = gethostbyaddr((const void *) &addr4, + sizeof(addr4), family); + } else if (family == AF_INET6) { + memset(&addr6, 0, sizeof(addr6)); + memcpy(&addr6, addr, sizeof(addr6)); + hostinfo = gethostbyaddr((const void *) &addr6, + sizeof(addr6), family); + } - hostinfo = gethostbyaddr((char *) &addr, sizeof(addr), AF_INET); if (!hostinfo) return NULL; @@ -204,7 +219,8 @@ int main(int argc, const char **argv) { int doCheck = 0, familyIPv4 = 0, familyIPv6 = 0; int rc; poptContext optCon; - char *ipStr, *prefixStr, *netmaskStr, *hostName, *chptr; + char *ipStr, *prefixStr, *netmaskStr, *chptr; + char *hostName = NULL; char namebuf[INET6_ADDRSTRLEN+1]; struct in_addr ip, netmask, network, broadcast; struct in6_addr ip6; @@ -424,7 +440,13 @@ int main(int argc, const char **argv) { } if (showHostname) { - if ((hostName = (char *) get_hostname(ip)) == NULL) { + if (familyIPv4) { + hostName = get_hostname(AF_INET, &ip); + } else if (familyIPv6) { + hostName = get_hostname(AF_INET6, &ip6); + } + + if (hostName == NULL) { if (!beSilent) { sprintf(errBuf, "ipcalc: cannot find hostname for %s", ipStr); herror(errBuf); |