Bug 32318

Summary: no userland tool available to test resolver facilities
Product: Base System Reporter: AnarCat <AnarCat>
Component: binAssignee: Crist J. Clark <cjc>
Status: Closed FIXED    
Severity: Affects Only Me    
Priority: Normal    
Version: 4.4-STABLE   
Hardware: Any   
OS: Any   

Description AnarCat 2001-11-26 23:10:01 UTC
Traditionally, since BIND has been part of FreeBSD for a good while, a
few basic tools are available to query the DNS servers configured on a
machine. However, no tool is available to query the resolver (in the
sense of gethostbyname/addr() routines) transparently. Host(1), dig(1),
nslookup(1) all use the name servers, and not the /etc/hosts files or
NIS/YP.

Fix: 

Possible implementation of a iplookup tool.

Chris J. Clark sent code to freebsd-questions, for a tool that would to
reverse lookups with gethostbyaddr(3). I hacked it a bit to lookup names
-> addresses in case the address isn't parsable.

See:

Message-ID: <20011126010753.E222@gohan.cjclark.org>

on -questions for the original code.

/*
 * Copyright (c) 2001 Crist J. Clark
 * $Id: iplookup.c,v 1.2 2001/11/26 22:01:40 anarcat Exp $
 */

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include <err.h>
#include <netdb.h>
#include <stdio.h>
#include <sysexits.h>

int main(int argc, char *argv[])
{
	struct hostent	*h;
	struct in_addr	a;
	int		i, name_l;
	char 		addr[24];
	char 		**p;

	for (i = 1; i < argc; i++) {
		if (name_l = (inet_aton(argv[i], &a) == 0)) {
			h = gethostbyname(argv[i]);
		} else {
			h = gethostbyaddr((char *)&a, sizeof a, AF_INET);
		}
		if (h == NULL)
			errx(EX_OSERR, "address, %s, failed: %s",
			    argv[i], hstrerror(h_errno));

		if (name_l) {
			printf("%s: ", h->h_name);
			a.s_addr = inet_addr(h->h_addr);
        		for (p = h->h_addr_list ; p != NULL && *p ;) {
                		addr[0] = '\0';
                		inet_ntop(h->h_addrtype, *p, addr, sizeof addr);
                		printf("%s",addr);
				if (++p != NULL && *p) {
					printf(", ");
				}
        		}
			printf("\n");
		} else {
			printf("%s: %s\n", argv[i], h->h_name);
		}

		for (p = h->h_aliases; p != NULL && *p; p++)
			printf("\t%s\n", *p);
	}

	return 0;
}
How-To-Repeat: 
Try to do a address to hostname lookup using a command line tool for an
address in /etc/hosts that will actually use gethostbyname(3). I don't
know of any such tool and a post on -questions didn't yield anything
better.
Comment 1 Makoto Matsushita 2001-11-27 02:11:10 UTC
Anyway how about putting this tool as a port?  It would be helpful for
other users.

anarcat> However, no tool is available to query the resolver (in the
anarcat> sense of gethostbyname/addr() routines)
anarcat> transparently. Host(1), dig(1), nslookup(1) all use the name
anarcat> servers, and not the /etc/hosts files or NIS/YP.

But only host(1) and its friend does check only DNS, these are the
exceptions.  You can use other networking tools such as ping(8),
telnet(1), and more (I don't know which tool fits for you).

anarcat> Try to do a address to hostname lookup using a command line
anarcat> tool for an address in /etc/hosts that will actually use
anarcat> gethostbyname(3).

It would be better that you use getaddrinfo(3) instead.  This tool
only works with IPv4 address but FreeBSD knows what IPv6 is.

-- -
Makoto `MAR' Matsushita
Comment 2 Crist J. Clark freebsd_committer freebsd_triage 2001-11-27 07:55:48 UTC
Responsible Changed
From-To: freebsd-bugs->cjc

I helped start it. I'll finish it.
Comment 3 Peter Pentchev 2001-11-27 15:11:18 UTC
On Mon, Nov 26, 2001 at 06:20:01PM -0800, Makoto Matsushita wrote:
> The following reply was made to PR bin/32318; it has been noted by GNATS.
> 
> From: Makoto Matsushita <matusita@jp.freebsd.org>
> To: FreeBSD-gnats-submit@FreeBSD.ORG
> Cc:  
> Subject: Re: bin/32318: no userland tool available to test resolver
>  facilities
> Date: Tue, 27 Nov 2001 11:11:10 +0900
> 
>  Anyway how about putting this tool as a port?  It would be helpful for
>  other users.
>  
>  anarcat> However, no tool is available to query the resolver (in the
>  anarcat> sense of gethostbyname/addr() routines)
>  anarcat> transparently. Host(1), dig(1), nslookup(1) all use the name
>  anarcat> servers, and not the /etc/hosts files or NIS/YP.
>  
>  But only host(1) and its friend does check only DNS, these are the
>  exceptions.  You can use other networking tools such as ping(8),
>  telnet(1), and more (I don't know which tool fits for you).
>  
>  anarcat> Try to do a address to hostname lookup using a command line
>  anarcat> tool for an address in /etc/hosts that will actually use
>  anarcat> gethostbyname(3).
>  
>  It would be better that you use getaddrinfo(3) instead.  This tool
>  only works with IPv4 address but FreeBSD knows what IPv6 is.

For the record, 'cd /usr/ports && make search key=resolv' lists, among
others, the net/ghtool and the net/geta ports.  I know that ghtool does
not do IPv6, but the geta description seems to imply that it does.

G'luck,
Peter

-- 
This sentence would be seven words long if it were six words shorter.
Comment 4 Makoto Matsushita 2001-11-27 17:18:23 UTC
roam> For the record, 'cd /usr/ports && make search key=resolv' lists, among
roam> others, the net/ghtool and the net/geta ports.  I know that ghtool does
roam> not do IPv6, but the geta description seems to imply that it does.

I've installed geta and found that it works pretty well.

galtvalion % cat /etc/nsswitch.conf
hosts: files dns
galtvalion % grep www.FreeBSD.org /etc/hosts
127.0.0.3               www.FreeBSD.org
galtvalion % cat /etc/nsswitch.conf
hosts: files dns
galtvalion % geta www.FreeBSD.org
127.0.0.3

Great :-)

-- -
Makoto `MAR' Matsushita
Comment 5 Crist J. Clark 2001-11-30 11:51:56 UTC
Are the existing ports that serve this function enough for you, Peter?
Would you like to close up the PR?
-- 
Crist J. Clark                     |     cjclark@alum.mit.edu
                                   |     cjclark@jhu.edu
http://people.freebsd.org/~cjc/    |     cjc@freebsd.org
Comment 6 Peter Pentchev 2001-11-30 12:03:44 UTC
On Fri, Nov 30, 2001 at 03:51:56AM -0800, Crist J. Clark wrote:
> Are the existing ports that serve this function enough for you, Peter?
> Would you like to close up the PR?

I think net/geta works fine.  Closing this PR would be fine by me;
what does the submitter - the Anarcat - think about it, though? :)

G'luck,
Peter

-- 
If wishes were fishes, the antecedent of this conditional would be true.
Comment 7 Crist J. Clark 2001-11-30 12:09:02 UTC
On Fri, Nov 30, 2001 at 04:00:02AM -0800, Crist J. Clark wrote:
> The following reply was made to PR bin/32318; it has been noted by GNATS.
> 
> From: "Crist J. Clark" <cristjc@earthlink.net>
> To: Peter Pentchev <roam@ringlet.net>
> Cc: bug-followup@freebsd.org,
> 	Makoto Matsushita <matusita@jp.freebsd.org>
> Subject: Re: bin/32318: no userland tool available to test resolver facilities
> Date: Fri, 30 Nov 2001 03:51:56 -0800
> 
>  Are the existing ports that serve this function enough for you, Peter?
>  Would you like to close up the PR?

Err... I mean The Anarcat, not Peter.
-- 
Crist J. Clark                     |     cjclark@alum.mit.edu
                                   |     cjclark@jhu.edu
http://people.freebsd.org/~cjc/    |     cjc@freebsd.org
Comment 8 AnarCat 2001-11-30 12:35:30 UTC
"Crist J. Clark" wrote:
> 
> On Fri, Nov 30, 2001 at 04:00:02AM -0800, Crist J. Clark wrote:
> > The following reply was made to PR bin/32318; it has been noted by GNATS.
> >
> > From: "Crist J. Clark" <cristjc@earthlink.net>
> > To: Peter Pentchev <roam@ringlet.net>
> > Cc: bug-followup@freebsd.org,
> >       Makoto Matsushita <matusita@jp.freebsd.org>
> > Subject: Re: bin/32318: no userland tool available to test resolver facilities
> > Date: Fri, 30 Nov 2001 03:51:56 -0800
> >
> >  Are the existing ports that serve this function enough for you, Peter?
> >  Would you like to close up the PR?
> 
> Err... I mean The Anarcat, not Peter.
> --
> Crist J. Clark                     |     cjclark@alum.mit.edu
>                                    |     cjclark@jhu.edu
> http://people.freebsd.org/~cjc/    |     cjc@freebsd.org


No. I don't think I have been clear enough. Ping(1) and friends are good
to test one *part* of the functionality: classic address resolution. But
they do not help in testing reverse address resolution, from what I can
tell from net/geta/pkg-descr...

I haven't tested geta, though. Give me time and I will.

A.
Comment 9 Peter Pentchev 2001-11-30 12:54:25 UTC
On Fri, Nov 30, 2001 at 07:35:30AM -0500, The Anarcat wrote:
> "Crist J. Clark" wrote:
> > 
> > On Fri, Nov 30, 2001 at 04:00:02AM -0800, Crist J. Clark wrote:
> > > The following reply was made to PR bin/32318; it has been noted by GNATS.
> > >
> > > From: "Crist J. Clark" <cristjc@earthlink.net>
> > > To: Peter Pentchev <roam@ringlet.net>
> > > Cc: bug-followup@freebsd.org,
> > >       Makoto Matsushita <matusita@jp.freebsd.org>
> > > Subject: Re: bin/32318: no userland tool available to test resolver facilities
> > > Date: Fri, 30 Nov 2001 03:51:56 -0800
> > >
> > >  Are the existing ports that serve this function enough for you, Peter?
> > >  Would you like to close up the PR?
> > 
> > Err... I mean The Anarcat, not Peter.
> > --
> > Crist J. Clark                     |     cjclark@alum.mit.edu
> >                                    |     cjclark@jhu.edu
> > http://people.freebsd.org/~cjc/    |     cjc@freebsd.org
> 
> 
> No. I don't think I have been clear enough. Ping(1) and friends are good
> to test one *part* of the functionality: classic address resolution. But
> they do not help in testing reverse address resolution, from what I can
> tell from net/geta/pkg-descr...
> 
> I haven't tested geta, though. Give me time and I will.

It works, though.

[root@straylight:v0 ~]# fgrep 1.2.3.4 /etc/hosts
1.2.3.4                 geta.test.nonexist
[root@straylight:v0 ~]# geta geta.test.nonexist
1.2.3.4
[root@straylight:v0 ~]# geta -r 1.2.3.4
1.2.3.4
Reverse map:   geta.test.nonexist
[root@straylight:v0 ~]#

And no, I do not have either 'nonexist' or '4.in-addr.arpa'
in any DNS map :)

G'luck,
Peter

-- 
Hey, out there - is it *you* reading me, or is it someone else?
Comment 10 AnarCat 2001-12-01 19:47:24 UTC
On Fri Nov 30, 2001 at 02:54:25PM +0200, Peter Pentchev wrote:
> On Fri, Nov 30, 2001 at 07:35:30AM -0500, The Anarcat wrote:
> > "Crist J. Clark" wrote:
> > > 
> > > On Fri, Nov 30, 2001 at 04:00:02AM -0800, Crist J. Clark wrote:
> > > > The following reply was made to PR bin/32318; it has been noted by GNATS.
> > > >
> > > > From: "Crist J. Clark" <cristjc@earthlink.net>
> > > > To: Peter Pentchev <roam@ringlet.net>
> > > > Cc: bug-followup@freebsd.org,
> > > >       Makoto Matsushita <matusita@jp.freebsd.org>
> > > > Subject: Re: bin/32318: no userland tool available to test resolver facilities
> > > > Date: Fri, 30 Nov 2001 03:51:56 -0800
> > > >
> > > >  Are the existing ports that serve this function enough for you, Peter?
> > > >  Would you like to close up the PR?
> > > 
> > > Err... I mean The Anarcat, not Peter.
> > > --
> > > Crist J. Clark                     |     cjclark@alum.mit.edu
> > >                                    |     cjclark@jhu.edu
> > > http://people.freebsd.org/~cjc/    |     cjc@freebsd.org
> > 
> > 
> > No. I don't think I have been clear enough. Ping(1) and friends are good
> > to test one *part* of the functionality: classic address resolution. But
> > they do not help in testing reverse address resolution, from what I can
> > tell from net/geta/pkg-descr...
> > 
> > I haven't tested geta, though. Give me time and I will.
> 
> It works, though.
> 
> [root@straylight:v0 ~]# fgrep 1.2.3.4 /etc/hosts
> 1.2.3.4                 geta.test.nonexist
> [root@straylight:v0 ~]# geta geta.test.nonexist
> 1.2.3.4
> [root@straylight:v0 ~]# geta -r 1.2.3.4
> 1.2.3.4
> Reverse map:   geta.test.nonexist
> [root@straylight:v0 ~]#


Darn.. I should have tested it. :)

It would be nice of having such a tool in the base system, not in the
ports.

But I guess I won't win this one and I guess that "this pr can be
closed" then. :)

thanks.

a.
Comment 11 Crist J. Clark freebsd_committer freebsd_triage 2002-08-26 19:25:30 UTC
State Changed
From-To: open->closed

There seem to be plenty of tools available for this with respect to 
the demand. Since this topic hasn't come up again for a while, close 
this up.