C code for DNS lookups

John Hascall john at iastate.edu
Tue Nov 9 18:31:11 UTC 1999


Here's a snipet I put together as part of a bit
to help screen out obviously bad email addresses.
It is rather simplistic but it shows some basics:

---------------------------------->8------snippy-snip-----8<---------------
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/nameser.h>
#include <resolv.h>
#include <stdlib.h>


/*
 * XXX: this really should actually check the answer fields...
 */
/*ARGSUSED*/
static int replyGood (
	unsigned char *	reply,
	int		nbytes
) {
	HEADER *hp;
	int	ancount;

	hp = (HEADER *)reply;
	ancount = ntohs((short)hp->ancount);
	return (ancount > 0);			/* got at least 1 answer */
}

int validMailHost (
	char *	host
) {
	unsigned char	reply[512];
	int		rl;			/* reply length */

	if ((_res.options & RES_INIT) == 0) res_init();
	_res.options &= ~RES_DEFNAMES;
	_res.retry = 2;
	if ((rl = res_query(host, C_IN, T_MX, reply, sizeof(reply))) > 0) {
		if (replyGood(reply, rl)) return TRUE;
	}
	if ((rl = res_query(host, C_IN, T_A,  reply, sizeof(reply))) > 0) {
		if (replyGood(reply, rl)) return TRUE;
	}
	return FALSE;
}
---------------------------------->8------snippy-snip-----8<---------------


More information about the bind-users mailing list