On Wed, Nov 11, 2009 at 05:00:03PM -0600,
 da...@from525.com <da...@from525.com> wrote 
 a message of 60 lines which said:

> I am wondering if anyone knows of an app similar to nslookup or
> dig that actually uses the system resolver. 

C source attached. Compile, for instance, with:

gcc -o resolve-name resolve-name.c

> I am basically trying to uinderstand why the system resolver was
> getting stuck on the third entry within the resolv.conf while it
> should have tried one of the first two working DNS servers first.

Not sure it will help.

#include <stdbool.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <errno.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/ip6.h>

#define MAXHOSTNAMELEN 256

char            progname[MAXHOSTNAMELEN + 1];

void
usage()
{
    fprintf(stderr, "Usage: %s hostname\n", progname);
}

char           *
text_of(struct sockaddr *address)
{
    char           *text = malloc(INET6_ADDRSTRLEN);
    struct sockaddr_in6 *address_v6;
    struct sockaddr_in *address_v4;
    if (address->sa_family == AF_INET6) {
        address_v6 = (struct sockaddr_in6 *) address;
        inet_ntop(AF_INET6, &address_v6->sin6_addr, text, INET6_ADDRSTRLEN);
    } else if (address->sa_family == AF_INET) {
        address_v4 = (struct sockaddr_in *) address;
        inet_ntop(AF_INET, &address_v4->sin_addr, text, INET_ADDRSTRLEN);
    } else {
        return ("[Unknown family address]");
    }
    return text;
}

int
main(int argc, char **argv)
{
    char            hostname[MAXHOSTNAMELEN + 1];
    struct addrinfo hints_numeric, hints;
    struct addrinfo *result, *hostref;
    int             status;

    strncpy(progname, argv[0], MAXHOSTNAMELEN);
    progname[MAXHOSTNAMELEN] = 0;
    if (argc != 2) {
        usage();
        exit(1);
    }
    strncpy(hostname, argv[1], MAXHOSTNAMELEN);
    hostname[MAXHOSTNAMELEN] = 0;
    /* RFC 1123 says we must try IP addresses first */
    memset(&hints_numeric, 0, sizeof(hints_numeric));
    hints_numeric.ai_flags = AI_NUMERICHOST;
    hints_numeric.ai_socktype = SOCK_STREAM;
    result = malloc(sizeof(struct addrinfo));
    status = getaddrinfo(hostname, NULL, &hints_numeric, &result);
    if (!status) {
        fprintf(stdout, "%s is an IP address\n", hostname);
    } else {
        if (status == EAI_NONAME) {
            /* Not an IP address */
            memset(&hints, 0, sizeof(hints));
            hints.ai_family = AF_UNSPEC;
            hints.ai_socktype = SOCK_STREAM;
            result = malloc(sizeof(struct addrinfo));
            status = getaddrinfo(hostname, NULL, &hints, &result);
            if (status) {
                fprintf(stderr, "Nothing found about host name %s\n", hostname);
                abort();
            }
        } else {
            fprintf(stderr, "Internal error, cannot resolve %s (error %i)\n",
                    hostname, status);
            abort();
        }
        fprintf(stdout, "Address(es) of %s is(are):", hostname);
        fprintf(stdout, " %s ", text_of(result->ai_addr));
        for (hostref = result->ai_next; hostref != NULL; hostref = hostref->ai_next) {
            fprintf(stdout, "%s ", text_of(hostref->ai_addr));
        }
        fprintf(stdout, "\n");
    }
    exit(0);
}
_______________________________________________
bind-users mailing list
bind-users@lists.isc.org
https://lists.isc.org/mailman/listinfo/bind-users

Reply via email to