ping6: stop using xgethostbyname2, remove it from libbb.

This commit is contained in:
Denis Vlasenko 2007-01-22 22:45:27 +00:00
parent 448f0241e0
commit 9adc6ced4f
6 changed files with 62 additions and 52 deletions

View 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);

View 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:

View 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);

View 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 */

View 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);
}

View 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);