ping6: stop using xgethostbyname2, remove it from libbb.
authorDenis Vlasenko <vda.linux@googlemail.com>
Mon, 22 Jan 2007 22:45:27 +0000 (22:45 -0000)
committerDenis Vlasenko <vda.linux@googlemail.com>
Mon, 22 Jan 2007 22:45:27 +0000 (22:45 -0000)
include/libbb.h
libbb/Kbuild
libbb/xconnect.c
libbb/xgethostbyname2.c
networking/ping.c
networking/ping6.c

index e41993796535b2bebe49df042adbbc9eda9ed53b..1a5ce24adf3a1f997fcfb27a87458c28ebe98429 100644 (file)
@@ -316,15 +316,21 @@ int xconnect_stream(const len_and_sockaddr *lsa);
  * (depending on host), but in theory nothing prevents e.g.
  * UNIX socket address being returned, IPX sockaddr etc... */
 len_and_sockaddr* host2sockaddr(const char *host, int port);
+#if ENABLE_FEATURE_IPV6
+/* Same, useful if you want to force family (e.g. IPv6) */
+len_and_sockaddr* host_and_af2sockaddr(const char *host, int port, sa_family_t af);
+#endif
 /* Assign sin[6]_port member if the socket is of corresponding type,
  * otherwise no-op. Useful for ftp.
  * NB: does NOT do htons() internally, just direct assignment. */
 void set_nport(len_and_sockaddr *lsa, unsigned port);
 /* Retrieve sin[6]_port or return -1 for non-INET[6] lsa's */
 int get_nport(len_and_sockaddr *lsa);
-/* Reverse DNS */
+/* Reverse DNS. Returns NULL on failure. */
 char* xmalloc_sockaddr2host(const struct sockaddr *sa, socklen_t salen);
-/* This one deosn't fall back to dotted IP and do not append :PORTNUM */
+/* This one doesn't append :PORTNUM */
+char* xmalloc_sockaddr2host_noport(const struct sockaddr *sa, socklen_t salen);
+/* This one also doesn't fall back to dotted IP (returns NULL) */
 char* xmalloc_sockaddr2hostonly_noport(const struct sockaddr *sa, socklen_t salen);
 /* inet_[ap]ton on steroids */
 char* xmalloc_sockaddr2dotted(const struct sockaddr *sa, socklen_t salen);
@@ -334,8 +340,8 @@ char* xmalloc_sockaddr2dotted_noport(const struct sockaddr *sa, socklen_t salen)
 //int xconnect_tcp_v4(struct sockaddr_in *s_addr);
 // users: traceroute.c hostname.c ifconfig.c ping.c
 struct hostent *xgethostbyname(const char *name);
-// ping6 is the only user - convert to new API
-struct hostent *xgethostbyname2(const char *name, int af);
+//// ping6 is the only user - convert to new API
+//struct hostent *xgethostbyname2(const char *name, int af);
 
 
 extern char *xstrdup(const char *s);
index 1e6b5fea79e6c71cb11a58cc9198cb10504387fc..a53b17f44a8f48f3f437ce5cba8064c74f45c792 100644 (file)
@@ -92,7 +92,6 @@ lib-y += xconnect.o
 lib-y += xfuncs.o
 lib-y += xgetcwd.o
 lib-y += xgethostbyname.o
-lib-y += xgethostbyname2.o
 lib-y += xreadlink.o
 
 # conditionally compiled objects:
index 188837e36e320650131f54946200fb13784c3c32..61fe7fd6c910ca390f88a96e40f55f7b3cec4c0f 100644 (file)
@@ -114,7 +114,10 @@ void set_nport(len_and_sockaddr *lsa, unsigned port)
 /* host: "1.2.3.4[:port]", "www.google.com[:port]"
  * port: if neither of above specifies port #
  */
-static len_and_sockaddr* str2sockaddr(const char *host, int port, int ai_flags)
+static len_and_sockaddr* str2sockaddr(
+               const char *host, int port,
+USE_FEATURE_IPV6(sa_family_t af,)
+               int ai_flags)
 {
        int rc;
        len_and_sockaddr *r; // = NULL;
@@ -147,9 +150,10 @@ static len_and_sockaddr* str2sockaddr(const char *host, int port, int ai_flags)
        }
 
        memset(&hint, 0 , sizeof(hint));
-       /* hint.ai_family = AF_UNSPEC; - zero anyway */
 #if !ENABLE_FEATURE_IPV6
        hint.ai_family = AF_INET; /* do not try to find IPv6 */
+#else
+       hint.ai_family = af;
 #endif
        /* Needed. Or else we will get each address thrice (or more)
         * for each possible socket type (tcp,udp,raw...): */
@@ -165,15 +169,25 @@ static len_and_sockaddr* str2sockaddr(const char *host, int port, int ai_flags)
        freeaddrinfo(result);
        return r;
 }
+#if !ENABLE_FEATURE_IPV6
+#define str2sockaddr(host, port, af, ai_flags) str2sockaddr(host, port, ai_flags)
+#endif
+
+#if ENABLE_FEATURE_IPV6
+len_and_sockaddr* host_and_af2sockaddr(const char *host, int port, sa_family_t af)
+{
+       return str2sockaddr(host, port, af, 0);
+}
+#endif
 
 len_and_sockaddr* host2sockaddr(const char *host, int port)
 {
-       return str2sockaddr(host, port, 0);
+       return str2sockaddr(host, port, AF_UNSPEC, 0);
 }
 
 static len_and_sockaddr* dotted2sockaddr(const char *host, int port)
 {
-       return str2sockaddr(host, port, NI_NUMERICHOST);
+       return str2sockaddr(host, port, AF_UNSPEC, NI_NUMERICHOST);
 }
 
 int xsocket_stream(len_and_sockaddr **lsap)
@@ -282,6 +296,11 @@ char* xmalloc_sockaddr2host(const struct sockaddr *sa, socklen_t salen)
        return sockaddr2str(sa, salen, 0);
 }
 
+char* xmalloc_sockaddr2host_noport(const struct sockaddr *sa, socklen_t salen)
+{
+       return sockaddr2str(sa, salen, IGNORE_PORT);
+}
+
 char* xmalloc_sockaddr2hostonly_noport(const struct sockaddr *sa, socklen_t salen)
 {
        return sockaddr2str(sa, salen, NI_NAMEREQD | IGNORE_PORT);
index 83d5386699e6dac1e50d1fda906b206fa31d7c22..7af2f75fbc073b5b6bf686d819f22b39f606769f 100644 (file)
@@ -1,22 +1 @@
-/* vi: set sw=4 ts=4: */
-/*
- * Mini xgethostbyname2 implementation.
- *
- * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
- */
-
-#include <netdb.h>
-#include "libbb.h"
-
-
-#ifdef CONFIG_FEATURE_IPV6
-struct hostent *xgethostbyname2(const char *name, int af)
-{
-       struct hostent *retval;
-
-       if ((retval = gethostbyname2(name, af)) == NULL)
-               bb_herror_msg_and_die("%s", name);
-
-       return retval;
-}
-#endif
+/* TO DELETE */
index 91708d2821941790208522c3e617bdc79aca57fd..fc2de456a2a3440231ccc93bb77ce0a75172f4b5 100644 (file)
@@ -97,7 +97,7 @@ static void ping(const char *host)
        }
 
        signal(SIGALRM, noresp);
-       alarm(5);                                       /* give the host 5000ms to respond */
+       alarm(5); /* give the host 5000ms to respond */
        /* listen for replies */
        while (1) {
                struct sockaddr_in from;
@@ -118,7 +118,8 @@ static void ping(const char *host)
                                break;
                }
        }
-       if (ENABLE_FEATURE_CLEAN_UP) close(pingsock);
+       if (ENABLE_FEATURE_CLEAN_UP)
+               close(pingsock);
        printf("%s is alive!\n", hostname);
 }
 
index a92387e793489de75f45bccf27f603cfd6920da9..ccb19b2b565d20972318ad2ad37754e669674cdf 100644 (file)
@@ -48,7 +48,7 @@ static void ping(const char *host);
 
 /* simple version */
 
-static struct hostent *h;
+//static struct hostent *h;
 
 static void noresp(int ign)
 {
@@ -58,6 +58,7 @@ static void noresp(int ign)
 
 static void ping(const char *host)
 {
+       len_and_sockaddr *lsa;//
        struct sockaddr_in6 pingaddr;
        struct icmp6_hdr *pkt;
        int pingsock, c;
@@ -66,11 +67,12 @@ static void ping(const char *host)
 
        pingsock = create_icmp6_socket();
 
-       memset(&pingaddr, 0, sizeof(pingaddr));
-
-       pingaddr.sin6_family = AF_INET6;
-       h = xgethostbyname2(host, AF_INET6);
-       memcpy(&pingaddr.sin6_addr, h->h_addr, sizeof(pingaddr.sin6_addr));
+       //memset(&pingaddr, 0, sizeof(pingaddr));
+       //pingaddr.sin6_family = AF_INET6;
+       //h = xgethostbyname2(host, AF_INET6);
+       //memcpy(&pingaddr.sin6_addr, h->h_addr, sizeof(pingaddr.sin6_addr));
+       lsa = host_and_af2sockaddr(host, 0, AF_INET6);
+       pingaddr = lsa->sin6;
 
        pkt = (struct icmp6_hdr *) packet;
        memset(pkt, 0, sizeof(packet));
@@ -88,7 +90,7 @@ static void ping(const char *host)
        }
 
        signal(SIGALRM, noresp);
-       alarm(5);                                       /* give the host 5000ms to respond */
+       alarm(5); /* give the host 5000ms to respond */
        /* listen for replies */
        while (1) {
                struct sockaddr_in6 from;
@@ -107,7 +109,8 @@ static void ping(const char *host)
                                break;
                }
        }
-       if (ENABLE_FEATURE_CLEAN_UP) close(pingsock);
+       if (ENABLE_FEATURE_CLEAN_UP)
+               close(pingsock);
        printf("%s is alive!\n", h->h_name);
 }
 
@@ -141,7 +144,8 @@ static int myid;
 static unsigned long tmin = ULONG_MAX, tmax, tsum;
 static char rcvd_tbl[MAX_DUP_CHK / 8];
 
-static struct hostent *hostent;
+//static struct hostent *hostent;
+char *hostname;
 
 static void sendping(int);
 static void pingstats(int);
@@ -161,7 +165,7 @@ static void pingstats(int junk)
 
        signal(SIGINT, SIG_IGN);
 
-       printf("\n--- %s ping statistics ---\n", hostent->h_name);
+       printf("\n--- %s ping statistics ---\n", hostname);
        printf("%lu packets transmitted, ", ntransmitted);
        printf("%lu packets received, ", nreceived);
        if (nrepeats)
@@ -314,6 +318,7 @@ static void unpack(char *packet, int sz, struct sockaddr_in6 *from, int hoplimit
 extern int BUG_bad_offsetof_icmp6_cksum(void);
 static void ping(const char *host)
 {
+       len_and_sockaddr *lsa;//
        char packet[datalen + MAXIPLEN + MAXICMPLEN];
        char buf[INET6_ADDRSTRLEN];
        int sockopt;
@@ -324,14 +329,15 @@ static void ping(const char *host)
 
        pingsock = create_icmp6_socket();
 
-       memset(&pingaddr, 0, sizeof(pingaddr));
-
-       pingaddr.sin6_family = AF_INET6;
-       hostent = xgethostbyname2(host, AF_INET6);
-       if (hostent->h_addrtype != AF_INET6)
-               bb_error_msg_and_die("unknown address type; only AF_INET6 is currently supported");
-
-       memcpy(&pingaddr.sin6_addr, hostent->h_addr, sizeof(pingaddr.sin6_addr));
+       //memset(&pingaddr, 0, sizeof(pingaddr));
+       //pingaddr.sin6_family = AF_INET6;
+       //hostent = xgethostbyname2(host, AF_INET6);
+       //if (hostent->h_addrtype != AF_INET6)
+       //      bb_error_msg_and_die("unknown address type; only AF_INET6 is currently supported");
+       //memcpy(&pingaddr.sin6_addr, hostent->h_addr, sizeof(pingaddr.sin6_addr));
+       lsa = host_and_af2sockaddr(host, 0, AF_INET6);
+       hostname = xmalloc_sockaddr2host_noport(&lsa->sa, lsa->len);
+       pingaddr = lsa->sin6;
 
 #ifdef ICMP6_FILTER
        {
@@ -367,7 +373,7 @@ static void ping(const char *host)
                pingaddr.sin6_scope_id = if_index;
 
        printf("PING %s (%s): %d data bytes\n",
-                       hostent->h_name,
+                       hostname,
                        inet_ntop(AF_INET6, &pingaddr.sin6_addr,
                        buf, sizeof(buf)),
                        datalen);