diff --git a/include/libbb.h b/include/libbb.h index 57531e491..e41993796 100644 --- a/include/libbb.h +++ b/include/libbb.h @@ -278,12 +278,12 @@ extern off_t xlseek(int fd, off_t offset, int whence); extern off_t fdlength(int fd); -extern int xsocket(int domain, int type, int protocol); -extern void xbind(int sockfd, struct sockaddr *my_addr, socklen_t addrlen); -extern void xlisten(int s, int backlog); -extern void xconnect(int s, const struct sockaddr *s_addr, socklen_t addrlen); -extern int setsockopt_reuseaddr(int fd); -extern int setsockopt_broadcast(int fd); +int xsocket(int domain, int type, int protocol); +void xbind(int sockfd, struct sockaddr *my_addr, socklen_t addrlen); +void xlisten(int s, int backlog); +void xconnect(int s, const struct sockaddr *s_addr, socklen_t addrlen); +int setsockopt_reuseaddr(int fd); +int setsockopt_broadcast(int fd); /* NB: returns port in host byte order */ unsigned bb_lookup_port(const char *port, const char *protocol, unsigned default_port); typedef struct len_and_sockaddr { @@ -303,34 +303,39 @@ int xsocket_stream(len_and_sockaddr **lsap); * numeric IP ("N.N.N.N") or numeric IPv6 address, * and can have ":PORT" suffix (for IPv6 use "[X:X:...:X]:PORT"). * If there is no suffix, port argument is used */ -extern int create_and_bind_stream_or_die(const char *bindaddr, int port); +int create_and_bind_stream_or_die(const char *bindaddr, int port); /* Create client TCP socket connected to peer:port. Peer cannot be NULL. * Peer can be numeric IP ("N.N.N.N"), numeric IPv6 address or hostname, * and can have ":PORT" suffix (for IPv6 use "[X:X:...:X]:PORT"). * If there is no suffix, port argument is used */ -extern int create_and_connect_stream_or_die(const char *peer, int port); +int create_and_connect_stream_or_die(const char *peer, int port); /* Connect to peer identified by lsa */ -extern int xconnect_stream(const len_and_sockaddr *lsa); +int xconnect_stream(const len_and_sockaddr *lsa); /* Return malloc'ed len_and_sockaddr with socket address of host:port * Currently will return IPv4 or IPv6 sockaddrs only * (depending on host), but in theory nothing prevents e.g. * UNIX socket address being returned, IPX sockaddr etc... */ -extern len_and_sockaddr* host2sockaddr(const char *host, int port); +len_and_sockaddr* host2sockaddr(const char *host, int port); /* 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. */ -extern void set_nport(len_and_sockaddr *lsa, unsigned port); +void set_nport(len_and_sockaddr *lsa, unsigned port); /* Retrieve sin[6]_port or return -1 for non-INET[6] lsa's */ -extern int get_nport(len_and_sockaddr *lsa); -extern char* xmalloc_sockaddr2host(const struct sockaddr *sa, socklen_t salen); -extern char* xmalloc_sockaddr2dotted(const struct sockaddr *sa, socklen_t salen); +int get_nport(len_and_sockaddr *lsa); +/* Reverse DNS */ +char* xmalloc_sockaddr2host(const struct sockaddr *sa, socklen_t salen); +/* This one deosn't fall back to dotted IP and do not append :PORTNUM */ +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); +char* xmalloc_sockaddr2dotted_noport(const struct sockaddr *sa, socklen_t salen); // "old" (ipv4 only) API //void bb_lookup_host(struct sockaddr_in *s_in, const char *host); -//extern int xconnect_tcp_v4(struct sockaddr_in *s_addr); +//int xconnect_tcp_v4(struct sockaddr_in *s_addr); // users: traceroute.c hostname.c ifconfig.c ping.c -extern struct hostent *xgethostbyname(const char *name); +struct hostent *xgethostbyname(const char *name); // ping6 is the only user - convert to new API -extern struct hostent *xgethostbyname2(const char *name, int af); +struct hostent *xgethostbyname2(const char *name, int af); extern char *xstrdup(const char *s); diff --git a/libbb/xconnect.c b/libbb/xconnect.c index 0addda157..188837e36 100644 --- a/libbb/xconnect.c +++ b/libbb/xconnect.c @@ -245,25 +245,36 @@ int xconnect_stream(const len_and_sockaddr *lsa) return fd; } +/* We hijack this constant to mean something else */ +/* It doesn't hurt because we will add this bit anyway */ +#define IGNORE_PORT NI_NUMERICSERV static char* sockaddr2str(const struct sockaddr *sa, socklen_t salen, int flags) { char host[128]; char serv[16]; int rc = getnameinfo(sa, salen, host, sizeof(host), + /* can do ((flags & IGNORE_PORT) ? NULL : serv) but why bother? */ serv, sizeof(serv), /* do not resolve port# into service _name_ */ flags | NI_NUMERICSERV ); if (rc) return NULL; + if (flags & IGNORE_PORT) + return xstrdup(host); #if ENABLE_FEATURE_IPV6 - if (sa->sa_family == AF_INET6) - return xasprintf("[%s]:%s", host, serv); + if (sa->sa_family == AF_INET6) { + if (strchr(host, ':')) /* heh, it's not a resolved hostname */ + return xasprintf("[%s]:%s", host, serv); + /*return xasprintf("%s:%s", host, serv);*/ + /* - fall through instead */ + } #endif /* For now we don't support anything else, so it has to be INET */ /*if (sa->sa_family == AF_INET)*/ return xasprintf("%s:%s", host, serv); + /*return xstrdup(host);*/ } char* xmalloc_sockaddr2host(const struct sockaddr *sa, socklen_t salen) @@ -271,7 +282,16 @@ char* xmalloc_sockaddr2host(const struct sockaddr *sa, socklen_t salen) return sockaddr2str(sa, salen, 0); } +char* xmalloc_sockaddr2hostonly_noport(const struct sockaddr *sa, socklen_t salen) +{ + return sockaddr2str(sa, salen, NI_NAMEREQD | IGNORE_PORT); +} char* xmalloc_sockaddr2dotted(const struct sockaddr *sa, socklen_t salen) { return sockaddr2str(sa, salen, NI_NUMERICHOST); } + +char* xmalloc_sockaddr2dotted_noport(const struct sockaddr *sa, socklen_t salen) +{ + return sockaddr2str(sa, salen, NI_NUMERICHOST | IGNORE_PORT); +} diff --git a/networking/nslookup.c b/networking/nslookup.c index 5a08844f0..af0816215 100644 --- a/networking/nslookup.c +++ b/networking/nslookup.c @@ -47,7 +47,7 @@ * ns3.kernel.org internet address = 204.152.191.36 */ -static int sockaddr_to_dotted(struct sockaddr *saddr, char *buf, int buflen) +/*static int sockaddr_to_dotted(struct sockaddr *saddr, char *buf, int buflen) { if (buflen <= 0) return -1; buf[0] = '\0'; @@ -61,9 +61,11 @@ static int sockaddr_to_dotted(struct sockaddr *saddr, char *buf, int buflen) } return -1; } +*/ static int print_host(const char *hostname, const char *header) { +#if 0 char str[128]; /* IPv6 address will fit, hostnames hopefully too */ struct addrinfo *result = NULL; int rc; @@ -76,6 +78,7 @@ static int print_host(const char *hostname, const char *header) hint.ai_socktype = SOCK_STREAM; // hint.ai_flags = AI_CANONNAME; rc = getaddrinfo(hostname, NULL /*service*/, &hint, &result); + if (!rc) { struct addrinfo *cur = result; // printf("%s\n", cur->ai_canonname); ? @@ -93,6 +96,75 @@ static int print_host(const char *hostname, const char *header) } freeaddrinfo(result); return (rc != 0); + +#else + /* We can't use host2sockaddr() - we want to get ALL addresses, + * not just one */ + + struct addrinfo *result = NULL; + int rc; + struct addrinfo hint; + + memset(&hint, 0 , sizeof(hint)); + /* hint.ai_family = AF_UNSPEC; - zero anyway */ + /* Needed. Or else we will get each address thrice (or more) + * for each possible socket type (tcp,udp,raw...): */ + hint.ai_socktype = SOCK_STREAM; + // hint.ai_flags = AI_CANONNAME; + rc = getaddrinfo(hostname, NULL /*service*/, &hint, &result); + + if (!rc) { + struct addrinfo *cur = result; + unsigned cnt = 0; + + printf("%-10s %s\n", header, hostname); + // printf("%s\n", cur->ai_canonname); ? + while (cur) { + char *dotted, *revhost; + dotted = xmalloc_sockaddr2dotted_noport(cur->ai_addr, cur->ai_addrlen); + revhost = xmalloc_sockaddr2hostonly_noport(cur->ai_addr, cur->ai_addrlen); + + printf("Address %u: %s%c", ++cnt, dotted, revhost ? ' ' : '\n'); + if (revhost) { + puts(revhost); + if (ENABLE_FEATURE_CLEAN_UP) + free(revhost); + } + if (ENABLE_FEATURE_CLEAN_UP) + free(dotted); + cur = cur->ai_next; + } + } else { +#if ENABLE_VERBOSE_RESOLUTION_ERRORS + bb_error_msg("getaddrinfo('%s') failed: %s", hostname, gai_strerror(rc)); +#else + bb_error_msg("can't resolve '%s'", hostname); +#endif + } + if (ENABLE_FEATURE_CLEAN_UP) + freeaddrinfo(result); + return (rc != 0); +#endif +} + + +/* lookup the default nameserver and display it */ +static void server_print(void) +{ + char *server; + + server = xmalloc_sockaddr2dotted_noport((struct sockaddr*)&_res.nsaddr_list[0], + sizeof(struct sockaddr_in)); + /* I honestly don't know what to do if DNS server has _IPv6 address_. + * Probably it is listed in + * _res._u._ext_.nsaddrs[MAXNS] (of type "struct sockaddr_in6*" each) + * but how to find out whether resolver uses + * _res.nsaddr_list[] or _res._u._ext_.nsaddrs[], or both? + * Looks like classic design from hell, BIND-grade. Hard to surpass. */ + print_host(server, "Server:"); + if (ENABLE_FEATURE_CLEAN_UP) + free(server); + puts(""); } @@ -109,40 +181,27 @@ static void set_default_dns(char *server) } -/* lookup the default nameserver and display it */ -static void server_print(void) -{ - char str[INET6_ADDRSTRLEN]; - - sockaddr_to_dotted((struct sockaddr*)&_res.nsaddr_list[0], str, sizeof(str)); - print_host(str, "Server:"); - puts(""); -} - - int nslookup_main(int argc, char **argv) { - /* - * initialize DNS structure _res used in printing the default - * name server and in the explicit name server option feature. - */ - - res_init(); - - /* - * We allow 1 or 2 arguments. - * The first is the name to be looked up and the second is an - * optional DNS server with which to do the lookup. - * More than 3 arguments is an error to follow the pattern of the - * standard nslookup - */ + /* We allow 1 or 2 arguments. + * The first is the name to be looked up and the second is an + * optional DNS server with which to do the lookup. + * More than 3 arguments is an error to follow the pattern of the + * standard nslookup */ if (argc < 2 || *argv[1] == '-' || argc > 3) bb_show_usage(); - else if(argc == 3) + + /* initialize DNS structure _res used in printing the default + * name server and in the explicit name server option feature. */ + res_init(); + /* rfc2133 says this enables IPv6 lookups */ + /* (but it also says "may be enabled in /etc/resolv.conf|) */ + /*_res.options |= RES_USE_INET6;*/ + + if(argc == 3) set_default_dns(argv[2]); server_print(); - return print_host(argv[1], "Name: "); + return print_host(argv[1], "Name:"); } -