svn commit: trunk/busybox: include libbb networking

vda at busybox.net vda at busybox.net
Mon Jan 22 22:45:27 UTC 2007


Author: vda
Date: 2007-01-22 14:45:27 -0800 (Mon, 22 Jan 2007)
New Revision: 17478

Log:
ping6: stop using xgethostbyname2, remove it from libbb.


Modified:
   trunk/busybox/include/libbb.h
   trunk/busybox/libbb/Kbuild
   trunk/busybox/libbb/xconnect.c
   trunk/busybox/libbb/xgethostbyname2.c
   trunk/busybox/networking/ping.c
   trunk/busybox/networking/ping6.c


Changeset:
Modified: trunk/busybox/include/libbb.h
===================================================================
--- trunk/busybox/include/libbb.h	2007-01-22 22:43:05 UTC (rev 17477)
+++ trunk/busybox/include/libbb.h	2007-01-22 22:45:27 UTC (rev 17478)
@@ -316,15 +316,21 @@
  * (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 @@
 //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);

Modified: trunk/busybox/libbb/Kbuild
===================================================================
--- trunk/busybox/libbb/Kbuild	2007-01-22 22:43:05 UTC (rev 17477)
+++ trunk/busybox/libbb/Kbuild	2007-01-22 22:45:27 UTC (rev 17478)
@@ -92,7 +92,6 @@
 lib-y += xfuncs.o
 lib-y += xgetcwd.o
 lib-y += xgethostbyname.o
-lib-y += xgethostbyname2.o
 lib-y += xreadlink.o
 
 # conditionally compiled objects:

Modified: trunk/busybox/libbb/xconnect.c
===================================================================
--- trunk/busybox/libbb/xconnect.c	2007-01-22 22:43:05 UTC (rev 17477)
+++ trunk/busybox/libbb/xconnect.c	2007-01-22 22:45:27 UTC (rev 17478)
@@ -114,7 +114,10 @@
 /* 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 @@
 	}
 
 	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 @@
 	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 @@
 	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);

Modified: trunk/busybox/libbb/xgethostbyname2.c
===================================================================
--- trunk/busybox/libbb/xgethostbyname2.c	2007-01-22 22:43:05 UTC (rev 17477)
+++ trunk/busybox/libbb/xgethostbyname2.c	2007-01-22 22:45:27 UTC (rev 17478)
@@ -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 */

Modified: trunk/busybox/networking/ping.c
===================================================================
--- trunk/busybox/networking/ping.c	2007-01-22 22:43:05 UTC (rev 17477)
+++ trunk/busybox/networking/ping.c	2007-01-22 22:45:27 UTC (rev 17478)
@@ -97,7 +97,7 @@
 	}
 
 	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 @@
 				break;
 		}
 	}
-	if (ENABLE_FEATURE_CLEAN_UP) close(pingsock);
+	if (ENABLE_FEATURE_CLEAN_UP)
+		close(pingsock);
 	printf("%s is alive!\n", hostname);
 }
 

Modified: trunk/busybox/networking/ping6.c
===================================================================
--- trunk/busybox/networking/ping6.c	2007-01-22 22:43:05 UTC (rev 17477)
+++ trunk/busybox/networking/ping6.c	2007-01-22 22:45:27 UTC (rev 17478)
@@ -48,7 +48,7 @@
 
 /* simple version */
 
-static struct hostent *h;
+//static struct hostent *h;
 
 static void noresp(int ign)
 {
@@ -58,6 +58,7 @@
 
 static void ping(const char *host)
 {
+	len_and_sockaddr *lsa;//
 	struct sockaddr_in6 pingaddr;
 	struct icmp6_hdr *pkt;
 	int pingsock, c;
@@ -66,12 +67,13 @@
 
 	pingsock = create_icmp6_socket();
 
-	memset(&pingaddr, 0, sizeof(pingaddr));
+	//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;
 
-	pingaddr.sin6_family = AF_INET6;
-	h = xgethostbyname2(host, AF_INET6);
-	memcpy(&pingaddr.sin6_addr, h->h_addr, sizeof(pingaddr.sin6_addr));
-
 	pkt = (struct icmp6_hdr *) packet;
 	memset(pkt, 0, sizeof(packet));
 	pkt->icmp6_type = ICMP6_ECHO_REQUEST;
@@ -88,7 +90,7 @@
 	}
 
 	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 @@
 				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 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 @@
 
 	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 @@
 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,15 +329,16 @@
 
 	pingsock = create_icmp6_socket();
 
-	memset(&pingaddr, 0, sizeof(pingaddr));
+	//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;
 
-	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));
-
 #ifdef ICMP6_FILTER
 	{
 		struct icmp6_filter filt;
@@ -367,7 +373,7 @@
 		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);




More information about the busybox-cvs mailing list