From fd554aee69b567c6c2a1233ed46e608c1d29f70f Mon Sep 17 00:00:00 2001 From: David Cantrell Date: Fri, 26 Sep 2008 15:35:57 -1000 Subject: Use inet_ntop() and inet_pton() The inet_ntoa() and inet_aton() functions are deprecated and limited to AF_INET only. --- src/ipcalc.c | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) (limited to 'src/ipcalc.c') diff --git a/src/ipcalc.c b/src/ipcalc.c index d1791eba..52e2f24f 100644 --- a/src/ipcalc.c +++ b/src/ipcalc.c @@ -198,6 +198,7 @@ int main(int argc, const char **argv) { int rc; poptContext optCon; char *ipStr, *prefixStr, *netmaskStr, *hostName, *chptr; + char namebuf[INET6_ADDRSTRLEN+1]; struct in_addr ip, netmask, network, broadcast; int prefix = 0; char errBuf[250]; @@ -270,7 +271,7 @@ int main(int argc, const char **argv) { } return 1; } else if (netmaskStr) { - if (!inet_aton(netmaskStr, &netmask)) { + if (inet_pton(AF_INET, netmaskStr, &netmask) <= 0) { if (!beSilent) fprintf(stderr, "ipcalc: bad netmask: %s\n", netmaskStr); return 1; @@ -307,7 +308,7 @@ int main(int argc, const char **argv) { } } - if (!inet_aton(ipStr, (struct in_addr *) &ip)) { + if (inet_pton(AF_INET, ipStr, &ip) <= 0) { if (!beSilent) fprintf(stderr, "ipcalc: bad ip address: %s\n", ipStr); return 1; @@ -331,7 +332,14 @@ int main(int argc, const char **argv) { prefix = mask2prefix(netmask); } - printf("NETMASK=%s\n", inet_ntoa(netmask)); + memset(&namebuf, '\0', sizeof(namebuf)); + + if (inet_ntop(AF_INET, &netmask, namebuf, INET_ADDRSTRLEN) == NULL) { + fprintf(stderr, "Memory allocation failure line %d\n", __LINE__); + abort(); + } + + printf("NETMASK=%s\n", namebuf); } if (showPrefix) { @@ -342,12 +350,26 @@ int main(int argc, const char **argv) { if (showBroadcast) { broadcast = calc_broadcast(ip, prefix); - printf("BROADCAST=%s\n", inet_ntoa(broadcast)); + memset(&namebuf, '\0', sizeof(namebuf)); + + if (inet_ntop(AF_INET, &broadcast, namebuf, INET_ADDRSTRLEN) == NULL) { + fprintf(stderr, "Memory allocation failure line %d\n", __LINE__); + abort(); + } + + printf("BROADCAST=%s\n", namebuf); } if (showNetwork) { network = calc_network(ip, prefix); - printf("NETWORK=%s\n", inet_ntoa(network)); + memset(&namebuf, '\0', sizeof(namebuf)); + + if (inet_ntop(AF_INET, &network, namebuf, INET_ADDRSTRLEN) == NULL) { + fprintf(stderr, "Memory allocation failure line %d\n", __LINE__); + abort(); + } + + printf("NETWORK=%s\n", namebuf); } if (showHostname) { -- cgit v1.2.1