C code for DNS lookups
Cedric Puddy
cedric at itactics.itactics.com
Tue Nov 9 16:26:43 UTC 1999
On Mon, 8 Nov 1999, Rui Prior wrote:
> As I had no response to my previous message and I really need to know how to
> implement DNS access in C (making queries, parsing results), I'm going to
> repost it. I'd be very grateful if someone could help me.
>
> Sorry for any inconvenience.
>
> Rui Prior
Hi Rui,
Please find the code for a program I call "fastlook" below.
It does not do all of the things that you desire -- indeed
it merely calls the resolvers and hopes that _someone_
set the resolv.conf up and so on. Writing this wasn't
as easy as one might have thought it would be (the
dig/nslookup code, linux libraries, and bind documentation
combined to make a nice morass), but here it be. :)
Regards,
-Cedric
------------- fastlook.c start ---------------
/***********************************************************************
* fastlook - a bare bones tool for resolving IP addresses/names into
* canical DNS names.
*
* (c)1999 james Partanen, Cedric Puddy
* $Header: /usr/src/cp/fastlook/fastlook.c,v 1.2 1999/04/23 20:56:37 cedric Exp $
*
* The tool can act as a filter, or take a list of addresses on the
* command line. It will output the addresses in the form of
* <queryed addr><tab><resolved address>
* if running as filter, there will be no superfluous newlines, etc
* output at any time.
*
* The reason for the creation of the program is simple. Our Linux
* firewalls log raw IP addresses. A quick skim of docs and such
* suggested that they could not be easily made to do lookups as they
* logged, and some brief thought indicated that this would not be
* a good idea anyway (perhaps there's a reason the option doesn't
* exist). A client wanted to pull the logs into a database and
* run reports based on this data and other data using some
* reporting tool. The solution used was to pull the raw data into
* one table, and pull the IP<->Name mappings (that we generate with
* fastlook) into a second table. When he wants reports with
* regular names, he uses the IP<->Name mapping table as if it where
* an associative array, and all is well. (Crufty, yes, but it suits
* the application and need:).
*
* The program was written on a stock RedHat 5.2 linux machine,
* with a 2.0.35 kernel and headers. It does nothing particularly
* complicated or strange, and would probably compile without
* difficulty on other machines. As time goes by, I will probably
* build and make avaiable binaries for Linux 2.0.x, BSD/OS (BSDI),
* UnixWare, and Interix 2.x (the platforms I have ready access to)
*
* Naturally, this code comes completely without warrentee of
* any kind. It is offered and made available with the express
* understanding that you, the user, undertake any and all risk
* in the implementation and use hereof. This code is public
* domain, or if you are the social type, postcard-ware. :)
*
* Cedric Puddy, cedric at thinkers.org
* ClearLine Services
* 118 Louisa Street, Kitchener, Ontario, N2H 5M3, Canada
*
********************************************************************/
#include <errno.h>
#include <string.h>
#include <limits.h>
#include <stdio.h>
#include <unistd.h>
#include <netdb.h>
#include <sys/socket.h>
int main(int argc, char *argv[])
{
struct hostent *host;
char **args, **names, ch, *s, buf[LINE_MAX];
unsigned int filter;
if ((char *)0 == argv[1])
filter = 1;
else
filter = 0;
if (isatty(1))
printf("\n");
if (!filter)
args = &argv[1];
while ((char *)0 != (s=(filter)?gets(buf):*(args++))) {
if ( (struct hostent *)0 == (host=gethostbyname(s))
|| (struct hostent *)0 == (host=gethostbyaddr(host->h_addr_list[0],
host->h_length,
AF_INET))) {
/* Verbose error reporting may not be desirable. If it is
required/desired, then use this printf instead. */
/* printf("%s\tFailed: %s\n", s, hstrerror(h_errno)); */
printf("%s\t%s",s ,s);
} else {
printf("%s\t%s", s, host->h_name);
for (names=&host->h_aliases[0]; (char *)0 != *names; names++)
printf(" %s", *names);
printf("\n");
}
}
}
--------------------- fastlook.c end --------------
-
| CCj/ClearLine - Unix/NT Administration and TCP/IP Network Services
| 118 Louisa Street, Kitchener, Ontario, N2H 5M3, 519-741-2157
\____________________________________________________________________
Cedric Puddy, IS Director cedric at thinkers.org
PGP Key Available at: http://www.thinkers.org/cedric
More information about the bind-users
mailing list