aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Cantrell <dcantrell@redhat.com>2008-09-26 16:41:21 -1000
committerHarald Hoyer <harald@redhat.com>2010-09-13 17:27:22 +0200
commitb74c943aa01f6a8facc36246dc20ccebd9eb690c (patch)
treea637dfb9a3ca7a219d7d4487ecf195f6a0b2c110
parentf085cdfdc0d67fe240c23e75d14dce6534d36c8a (diff)
downloadinitscripts-b74c943aa01f6a8facc36246dc20ccebd9eb690c.tar
initscripts-b74c943aa01f6a8facc36246dc20ccebd9eb690c.tar.gz
initscripts-b74c943aa01f6a8facc36246dc20ccebd9eb690c.tar.bz2
initscripts-b74c943aa01f6a8facc36246dc20ccebd9eb690c.tar.xz
initscripts-b74c943aa01f6a8facc36246dc20ccebd9eb690c.zip
Expand get_hostname() function to support IPv4 and IPv6.
Make sure that get_hostname() if the user specifies either address family.
-rw-r--r--src/ipcalc.c36
1 files changed, 29 insertions, 7 deletions
diff --git a/src/ipcalc.c b/src/ipcalc.c
index 1c4c9331..5dbdfc9c 100644
--- a/src/ipcalc.c
+++ b/src/ipcalc.c
@@ -158,21 +158,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;
@@ -198,7 +213,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;
@@ -418,7 +434,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);