host.c revision 16d00613cfd499ce96fcc3ad00275cb50ec5de74
6ceca14e5ea7972a9ba8bc1dca0de1f63f669cfdDanny Mayer/*
70e5a7403f0e0a3bd292b8287c5fed5772c15270Automatic Updater * Copyright (C) 2000 Internet Software Consortium.
1fa26403d7679235a30fbf6289f68fed5872df30Mark Andrews *
6ceca14e5ea7972a9ba8bc1dca0de1f63f669cfdDanny Mayer * Permission to use, copy, modify, and distribute this software for any
ec5347e2c775f027573ce5648b910361aa926c01Automatic Updater * purpose with or without fee is hereby granted, provided that the above
6ceca14e5ea7972a9ba8bc1dca0de1f63f669cfdDanny Mayer * copyright notice and this permission notice appear in all copies.
6ceca14e5ea7972a9ba8bc1dca0de1f63f669cfdDanny Mayer *
6ceca14e5ea7972a9ba8bc1dca0de1f63f669cfdDanny Mayer * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
1fa26403d7679235a30fbf6289f68fed5872df30Mark Andrews * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
1fa26403d7679235a30fbf6289f68fed5872df30Mark Andrews * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
1fa26403d7679235a30fbf6289f68fed5872df30Mark Andrews * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
1fa26403d7679235a30fbf6289f68fed5872df30Mark Andrews * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
1fa26403d7679235a30fbf6289f68fed5872df30Mark Andrews * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
1fa26403d7679235a30fbf6289f68fed5872df30Mark Andrews * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
1fa26403d7679235a30fbf6289f68fed5872df30Mark Andrews * SOFTWARE.
6ceca14e5ea7972a9ba8bc1dca0de1f63f669cfdDanny Mayer */
6ceca14e5ea7972a9ba8bc1dca0de1f63f669cfdDanny Mayer
07555e64d9102eae058efd58f872b4a3b9ddff61Mark Andrews#include <config.h>
6ceca14e5ea7972a9ba8bc1dca0de1f63f669cfdDanny Mayer
6ceca14e5ea7972a9ba8bc1dca0de1f63f669cfdDanny Mayer#include <errno.h>
6ceca14e5ea7972a9ba8bc1dca0de1f63f669cfdDanny Mayer#include <stdio.h>
6ceca14e5ea7972a9ba8bc1dca0de1f63f669cfdDanny Mayer#include <stdlib.h>
6ceca14e5ea7972a9ba8bc1dca0de1f63f669cfdDanny Mayer#include <string.h>
6ceca14e5ea7972a9ba8bc1dca0de1f63f669cfdDanny Mayer#include <unistd.h>
6ceca14e5ea7972a9ba8bc1dca0de1f63f669cfdDanny Mayer
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrenceextern int h_errno;
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence#include <isc/types.h>
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence#include <isc/app.h>
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence#include <isc/assertions.h>
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence#include <isc/error.h>
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence#include <isc/mem.h>
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence#include <isc/net.h>
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence#include <isc/netdb.h>
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence#include <isc/result.h>
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer#include <isc/sockaddr.h>
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer#include <isc/socket.h>
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer#include <isc/task.h>
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer#include <isc/timer.h>
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer#include <isc/time.h>
667c498942c0e605c1aab98bca6b90aac4cbbd71Mark Andrews#include <isc/buffer.h>
667c498942c0e605c1aab98bca6b90aac4cbbd71Mark Andrews#include <isc/list.h>
667c498942c0e605c1aab98bca6b90aac4cbbd71Mark Andrews#include <isc/util.h>
667c498942c0e605c1aab98bca6b90aac4cbbd71Mark Andrews#include <isc/commandline.h>
667c498942c0e605c1aab98bca6b90aac4cbbd71Mark Andrews
667c498942c0e605c1aab98bca6b90aac4cbbd71Mark Andrews#include <dns/types.h>
667c498942c0e605c1aab98bca6b90aac4cbbd71Mark Andrews#include <dns/message.h>
667c498942c0e605c1aab98bca6b90aac4cbbd71Mark Andrews#include <dns/name.h>
667c498942c0e605c1aab98bca6b90aac4cbbd71Mark Andrews#include <dns/fixedname.h>
667c498942c0e605c1aab98bca6b90aac4cbbd71Mark Andrews#include <dns/rdata.h>
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer#include <dns/rdatalist.h>
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer#include <dns/rdataset.h>
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer#include <dns/rdataclass.h>
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer#include <dns/rdatatype.h>
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence#include <dns/result.h>
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence#include <dig/dig.h>
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrenceextern ISC_LIST(dig_lookup_t) lookup_list;
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrenceextern ISC_LIST(dig_server_t) server_list;
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrenceextern ISC_LIST(dig_searchlist_t) search_list;
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrenceextern isc_boolean_t tcp_mode,
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence have_ipv6;
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrenceextern in_port_t port;
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrenceextern unsigned int timeout;
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrenceextern isc_mem_t *mctx;
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrenceextern isc_taskmgr_t *taskmgr;
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrenceextern isc_task_t *task;
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrenceextern isc_timermgr_t *timermgr;
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrenceextern isc_socketmgr_t *socketmgr;
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrenceextern dns_messageid_t id;
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrenceextern dns_name_t rootorg;
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrenceextern char *rootspace[BUFSIZE];
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrenceextern isc_buffer_t rootbuf;
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrenceextern int sendcount;
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrenceextern int ndots;
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrenceextern int tries;
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrenceisc_boolean_t short_form=ISC_TRUE,
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence filter=ISC_FALSE,
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence showallsoa=ISC_FALSE;
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrencestatic char *opcodetext[] = {
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence "QUERY",
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence "IQUERY",
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence "STATUS",
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence "RESERVED3",
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence "NOTIFY",
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence "UPDATE",
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence "RESERVED6",
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence "RESERVED7",
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence "RESERVED8",
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence "RESERVED9",
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence "RESERVED10",
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence "RESERVED11",
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence "RESERVED12",
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence "RESERVED13",
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence "RESERVED14",
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence "RESERVED15"
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence};
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrencestatic char *rcodetext[] = {
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence "NOERROR",
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence "FORMERR",
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence "SERVFAIL",
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence "NXDOMAIN",
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence "NOTIMPL",
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence "REFUSED",
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence "YXDOMAIN",
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence "YXRRSET",
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence "NXRRSET",
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence "NOTAUTH",
6ceca14e5ea7972a9ba8bc1dca0de1f63f669cfdDanny Mayer "NOTZONE",
6ceca14e5ea7972a9ba8bc1dca0de1f63f669cfdDanny Mayer "RESERVED11",
6ceca14e5ea7972a9ba8bc1dca0de1f63f669cfdDanny Mayer "RESERVED12",
ecffc3aae3e72c5c65b1911ec3f4e1d180dc4bbcMark Andrews "RESERVED13",
ecffc3aae3e72c5c65b1911ec3f4e1d180dc4bbcMark Andrews "RESERVED14",
ecffc3aae3e72c5c65b1911ec3f4e1d180dc4bbcMark Andrews "RESERVED15",
ecffc3aae3e72c5c65b1911ec3f4e1d180dc4bbcMark Andrews "BADVERS"
ecffc3aae3e72c5c65b1911ec3f4e1d180dc4bbcMark Andrews};
ecffc3aae3e72c5c65b1911ec3f4e1d180dc4bbcMark Andrews
ecffc3aae3e72c5c65b1911ec3f4e1d180dc4bbcMark Andrewsstatic char *rtypetext[] = {
ecffc3aae3e72c5c65b1911ec3f4e1d180dc4bbcMark Andrews "zero", /* 0 */
ecffc3aae3e72c5c65b1911ec3f4e1d180dc4bbcMark Andrews "has address", /* 1 */
07555e64d9102eae058efd58f872b4a3b9ddff61Mark Andrews "name server", /* 2 */
578f588228f5e04ccf648b6ae596f396ad6a22c9Mark Andrews "MD", /* 3 */
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer "MF", /* 4 */
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer "is an alias for", /* 5 */
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer "SOA", /* 6 */
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer "MB", /* 7 */
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer "MG", /* 8 */
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer "MR", /* 9 */
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer "NULL", /* 10 */
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer "has well known services", /* 11 */
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer "domain name pointer", /* 12 */
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer "host information", /* 13 */
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer "MINFO", /* 14 */
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer "mail is handled by", /* 15 */
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer "text", /* 16 */
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer "RP", /* 17 */
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer "AFSDB", /* 18 */
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer "x25 address", /* 19 */
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer "isdn address", /* 20 */
6ceca14e5ea7972a9ba8bc1dca0de1f63f669cfdDanny Mayer "RT" /* 21 */
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer "NSAP", /* 22 */
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer "NSAP_PTR", /* 23 */
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer "has signature", /* 24 */
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer "has key", /* 25 */
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer "PX", /* 26 */
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer "GPOS", /* 27 */
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer "has AAAA address", /* 28 */
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer "LOC", /* 29 */
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer "has next record", /* 30 */
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer "has 31 record", /* 31 */
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer "has 32 record", /* 32 */
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer "SRV", /* 33 */
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence "has 34 record", /* 34 */
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence "NAPTR", /* 35 */
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence "KX", /* 36 */
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer "CERT", /* 37 */
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer "has v6 address", /* 38 */
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer "DNAME", /* 39 */
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer "has 40 record", /* 40 */
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer "has optional information"}; /* 41 */
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayervoid
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayercheck_next_lookup (dig_lookup_t *lookup) {
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer dig_lookup_t *next;
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer dig_query_t *query;
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer isc_boolean_t still_working=ISC_FALSE;
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer debug("In check_next_lookup");
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer for (query = ISC_LIST_HEAD(lookup->q);
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer query != NULL;
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer query = ISC_LIST_NEXT(query, link)) {
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer if (query->working) {
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer debug("Still have a worker.");
6fa7a247a1037be08d7a8e0b4c4fd3785b2f268cAndreas Gustafsson still_working=ISC_TRUE;
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer }
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer }
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer if (still_working)
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer return;
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence next = ISC_LIST_NEXT(lookup, link);
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence debug ("Have %d retries left for %s\n",
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence lookup->retries, lookup->textname);
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer if ((next == NULL)&&((lookup->retries <= 1)
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer ||tcp_mode)) {
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer debug("Shutting Down.", stderr);
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer isc_app_shutdown();
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer return;
6ceca14e5ea7972a9ba8bc1dca0de1f63f669cfdDanny Mayer }
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer if (tcp_mode) {
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer setup_lookup(next);
922a15c7c07b876c7ca1dbfa871da86ddaae8d4dDanny Mayer do_lookup_tcp(next);
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer } else {
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer if (lookup->retries > 1) {
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer lookup->retries --;
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer send_udp(lookup);
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer } else {
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer setup_lookup(next);
6ceca14e5ea7972a9ba8bc1dca0de1f63f669cfdDanny Mayer do_lookup_udp(next);
6ceca14e5ea7972a9ba8bc1dca0de1f63f669cfdDanny Mayer }
6ceca14e5ea7972a9ba8bc1dca0de1f63f669cfdDanny Mayer }
6ceca14e5ea7972a9ba8bc1dca0de1f63f669cfdDanny Mayer
6ceca14e5ea7972a9ba8bc1dca0de1f63f669cfdDanny Mayer#ifdef NEVER
6ceca14e5ea7972a9ba8bc1dca0de1f63f669cfdDanny Mayer next = ISC_LIST_NEXT (lookup, link);
6ceca14e5ea7972a9ba8bc1dca0de1f63f669cfdDanny Mayer if (next == NULL) {
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer debug ("Shutting Down.");
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer isc_app_shutdown();
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer return;
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer }
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer
90039392500d15bf056ab4505d048fcba0d485dfMark Andrews setup_lookup(next);
b39213ce599dc433c6f99144c97ca3d562f40344Mark Andrews if (tcp_mode)
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer do_lookup_tcp(next);
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer else
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer do_lookup_udp(next);
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer#endif
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer}
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayerstatic void
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayershow_usage() {
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer fputs (
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer"Usage: host [-aCdlrTwv] [-c class] [-N ndots] [-t type] [-W time]\n"
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer" [-R number] hostname [server]\n"
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer" -a is equivalent to -v -t *\n"
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer" -c specifies query class for non-IN data\n"
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer" -C compares SOA records on authorative nameservers\n"
" -d is equivalent to -v\n"
" -l lists all hosts in a domain, using AXFR\n"
" -N changes the number of dots allowed before root lookup is done\n"
" -r disables recursive processing\n"
" -R specifies number of retries for UDP packets\n"
" -t specifies the query type\n"
" -T enables TCP/IP mode\n"
" -v enables verbose output\n"
" -w specifies to wait forever for a reply\n"
" -W specifies how long to wait for a reply\n", stderr);
exit (0);
}
void
received(int bytes, int frmsize, char *frm, dig_query_t *query) {
UNUSED(query);
if (!short_form)
printf("Received %u bytes from %.*s\n",
bytes, frmsize, frm);
}
void
trying(int frmsize, char *frm, dig_lookup_t *lookup) {
UNUSED (lookup);
if (!short_form)
printf ("Trying \"%.*s\"\n", frmsize, frm);
}
static void
say_message(dns_name_t *name, char *msg, dns_rdata_t *rdata,
dig_query_t *query)
{
isc_buffer_t *b=NULL, *b2=NULL;
isc_region_t r, r2;
isc_result_t result;
result = isc_buffer_allocate(mctx, &b, BUFSIZE);
check_result (result, "isc_buffer_allocate");
result = isc_buffer_allocate(mctx, &b2, BUFSIZE);
check_result (result, "isc_buffer_allocate");
result = dns_name_totext(name, ISC_FALSE, b);
check_result(result, "dns_name_totext");
isc_buffer_usedregion(b, &r);
result = dns_rdata_totext(rdata, NULL, b2);
check_result(result, "dns_rdata_totext");
isc_buffer_usedregion(b2, &r2);
printf ( "%.*s %s %.*s", (int)r.length, (char *)r.base,
msg, (int)r2.length, (char *)r2.base);
if (query->lookup->identify) {
printf (" on server %s", query->servname);
}
printf ("\n");
isc_buffer_free(&b);
isc_buffer_free(&b2);
}
static isc_result_t
printsection(dns_message_t *msg, dns_section_t sectionid, char *section_name,
isc_boolean_t headers, dig_query_t *query)
{
dns_name_t *name, *print_name;
dns_rdataset_t *rdataset;
dns_rdata_t rdata;
isc_buffer_t target;
isc_result_t result, loopresult;
isc_region_t r;
dns_name_t empty_name;
char t[4096];
isc_boolean_t first;
isc_boolean_t no_rdata;
char *rtt;
if (sectionid == DNS_SECTION_QUESTION)
no_rdata = ISC_TRUE;
else
no_rdata = ISC_FALSE;
if (headers)
printf(";; %s SECTION:\n", section_name);
dns_name_init(&empty_name, NULL);
result = dns_message_firstname(msg, sectionid);
if (result == ISC_R_NOMORE)
return (ISC_R_SUCCESS);
else if (result != ISC_R_SUCCESS)
return (result);
for (;;) {
name = NULL;
dns_message_currentname(msg, sectionid, &name);
isc_buffer_init(&target, t, sizeof(t));
first = ISC_TRUE;
print_name = name;
for (rdataset = ISC_LIST_HEAD(name->list);
rdataset != NULL;
rdataset = ISC_LIST_NEXT(rdataset, link)) {
if (!short_form) {
result = dns_rdataset_totext(rdataset,
print_name,
ISC_FALSE,
no_rdata,
&target);
if (result != ISC_R_SUCCESS)
return (result);
#ifdef USEINITALWS
if (first) {
print_name = &empty_name;
first = ISC_FALSE;
}
#endif
} else {
loopresult = dns_rdataset_first(rdataset);
while (loopresult == ISC_R_SUCCESS) {
dns_rdataset_current(rdataset, &rdata);
if (rdata.type <= 41)
rtt=rtypetext[rdata.type];
else if (rdata.type == 103)
rtt="unspecified data";
else if (rdata.type == 249)
rtt="key";
else if (rdata.type == 250)
rtt="signature";
else
rtt="unknown";
say_message(print_name,
rtypetext[rdata.type],
&rdata, query);
loopresult = dns_rdataset_next(
rdataset);
}
}
}
if (!short_form) {
isc_buffer_usedregion(&target, &r);
if (no_rdata)
printf(";%.*s", (int)r.length,
(char *)r.base);
else
printf("%.*s", (int)r.length, (char *)r.base);
}
result = dns_message_nextname(msg, sectionid);
if (result == ISC_R_NOMORE)
break;
else if (result != ISC_R_SUCCESS)
return (result);
}
return (ISC_R_SUCCESS);
}
static isc_result_t
printrdata(dns_message_t *msg, dns_rdataset_t *rdataset, dns_name_t *owner,
char *set_name, isc_boolean_t headers)
{
isc_buffer_t target;
isc_result_t result;
isc_region_t r;
char t[4096];
UNUSED(msg);
if (headers)
printf(";; %s SECTION:\n", set_name);
isc_buffer_init(&target, t, sizeof(t));
result = dns_rdataset_totext(rdataset, owner, ISC_FALSE, ISC_FALSE,
&target);
if (result != ISC_R_SUCCESS)
return (result);
isc_buffer_usedregion(&target, &r);
printf("%.*s", (int)r.length, (char *)r.base);
return (ISC_R_SUCCESS);
}
isc_result_t
printmessage(dig_query_t *query, dns_message_t *msg, isc_boolean_t headers) {
isc_boolean_t did_flag = ISC_FALSE;
dns_rdataset_t *opt, *tsig = NULL;
dns_name_t *tsigname;
isc_result_t result = ISC_R_SUCCESS;
UNUSED (headers);
if (msg->rcode != 0) {
printf ("Host not found: %d(%s)\n",
msg->rcode, rcodetext[msg->rcode]);
return (ISC_R_SUCCESS);
}
if (!short_form) {
printf(";; ->>HEADER<<- opcode: %s, status: %s, id: %u\n",
opcodetext[msg->opcode], rcodetext[msg->rcode],
msg->id);
printf(";; flags: ");
if ((msg->flags & DNS_MESSAGEFLAG_QR) != 0) {
printf("qr");
did_flag = ISC_TRUE;
}
if ((msg->flags & DNS_MESSAGEFLAG_AA) != 0) {
printf("%saa", did_flag ? " " : "");
did_flag = ISC_TRUE;
}
if ((msg->flags & DNS_MESSAGEFLAG_TC) != 0) {
printf("%stc", did_flag ? " " : "");
did_flag = ISC_TRUE;
}
if ((msg->flags & DNS_MESSAGEFLAG_RD) != 0) {
printf("%srd", did_flag ? " " : "");
did_flag = ISC_TRUE;
}
if ((msg->flags & DNS_MESSAGEFLAG_RA) != 0) {
printf("%sra", did_flag ? " " : "");
did_flag = ISC_TRUE;
}
if ((msg->flags & DNS_MESSAGEFLAG_AD) != 0) {
printf("%sad", did_flag ? " " : "");
did_flag = ISC_TRUE;
}
if ((msg->flags & DNS_MESSAGEFLAG_CD) != 0) {
printf("%scd", did_flag ? " " : "");
did_flag = ISC_TRUE;
}
printf("; QUERY: %u, ANSWER: %u, "
"AUTHORITY: %u, ADDITIONAL: %u\n",
msg->counts[DNS_SECTION_QUESTION],
msg->counts[DNS_SECTION_ANSWER],
msg->counts[DNS_SECTION_AUTHORITY],
msg->counts[DNS_SECTION_ADDITIONAL]);
opt = dns_message_getopt(msg);
if (opt != NULL)
printf(";; EDNS: version: %u, udp=%u\n",
(unsigned int)((opt->ttl & 0x00ff0000) >> 16),
(unsigned int)opt->rdclass);
tsigname = NULL;
tsig = dns_message_gettsig(msg, &tsigname);
if (tsig != NULL)
printf(";; PSEUDOSECTIONS: TSIG\n");
}
if (! ISC_LIST_EMPTY(msg->sections[DNS_SECTION_QUESTION]) &&
!short_form ) {
printf("\n");
result = printsection(msg, DNS_SECTION_QUESTION, "QUESTION",
ISC_TRUE, query);
if (result != ISC_R_SUCCESS)
return (result);
}
if (! ISC_LIST_EMPTY(msg->sections[DNS_SECTION_ANSWER])) {
if (!short_form)
printf("\n");
result = printsection(msg, DNS_SECTION_ANSWER, "ANSWER",
!short_form, query);
if (result != ISC_R_SUCCESS)
return (result);
}
if (! ISC_LIST_EMPTY(msg->sections[DNS_SECTION_AUTHORITY]) &&
!short_form ) {
printf("\n");
result = printsection(msg, DNS_SECTION_AUTHORITY, "AUTHORITY",
ISC_TRUE, query);
if (result != ISC_R_SUCCESS)
return (result);
}
if (! ISC_LIST_EMPTY(msg->sections[DNS_SECTION_ADDITIONAL]) &&
!short_form ) {
printf("\n");
result = printsection(msg, DNS_SECTION_ADDITIONAL,
"ADDITIONAL", ISC_TRUE, query);
if (result != ISC_R_SUCCESS)
return (result);
}
if ((tsig != NULL) && !short_form) {
printf("\n");
result = printrdata(msg, tsig, tsigname,
"PSEUDOSECTION TSIG", ISC_TRUE);
if (result != ISC_R_SUCCESS)
return (result);
}
if (!short_form)
printf("\n");
return (result);
}
void
parse_args(isc_boolean_t is_batchfile, int argc, char **argv) {
isc_boolean_t have_host=ISC_FALSE,
recursion=ISC_TRUE,
xfr_mode=ISC_FALSE,
nsfind=ISC_FALSE;
char hostname[MXNAME];
char querytype[32]="";
char queryclass[32]="";
dig_server_t *srv;
dig_lookup_t *lookup;
int c;
UNUSED(is_batchfile);
while ((c = isc_commandline_parse(argc, argv, "lvwrdt:c:aTCN:R:W:"))
!= EOF) {
switch (c) {
case 'l':
tcp_mode = ISC_TRUE;
xfr_mode = ISC_TRUE;
filter = ISC_TRUE;
strcpy (querytype, "axfr");
break;
case 'v':
case 'd':
short_form = ISC_FALSE;
break;
case 'r':
recursion = ISC_FALSE;
break;
case 't':
strncpy (querytype, isc_commandline_argument, 32);
break;
case 'c':
strncpy (queryclass, isc_commandline_argument, 32);
break;
case 'a':
strcpy (querytype, "any");
short_form = ISC_FALSE;
break;
case 'w':
/* XXXMWS This should be a system-indep.
* thing! */
timeout = 32767;
break;
case 'W':
timeout = atoi(isc_commandline_argument);
if (timeout < 1)
timeout = 1;
break;
case 'R':
tries = atoi(isc_commandline_argument);
if (tries < 1)
tries = 1;
break;
case 'T':
tcp_mode = ISC_TRUE;
break;
case 'C':
debug ("Showing all SOA's");
if (querytype[0] == 0)
strcpy (querytype, "soa");
if (queryclass[0] == 0)
strcpy (queryclass, "in");
nsfind = ISC_TRUE;
showallsoa = ISC_TRUE;
break;
case 'N':
debug ("Setting NDOTS to %s",
isc_commandline_argument);
ndots = atoi(isc_commandline_argument);
break;
}
}
if (isc_commandline_index >= argc) {
show_usage();
}
strncpy (hostname, argv[isc_commandline_index], MXNAME);
if (argc > isc_commandline_index+1) {
srv=isc_mem_allocate(mctx, sizeof(struct dig_server));
if (srv == NULL)
fatal ("Memory allocation failure.");
strncpy(srv->servername,
argv[isc_commandline_index+1], MXNAME-1);
debug("Server is %s", srv->servername);
ISC_LIST_APPEND(server_list, srv, link);
}
if (querytype[0] == 0)
strcpy (querytype, "a");
if (queryclass[0] == 0)
strcpy (queryclass, "in");
lookup = isc_mem_allocate (mctx,
sizeof(struct dig_lookup));
if (lookup == NULL)
fatal ("Memory allocation failure.");
lookup->pending = ISC_FALSE;
strncpy (lookup->textname, hostname, MXNAME);
strncpy (lookup->rttext, querytype, 32);
strncpy (lookup->rctext, queryclass, 32);
lookup->namespace[0]=0;
lookup->sendspace[0]=0;
lookup->sendmsg=NULL;
lookup->name=NULL;
lookup->oname=NULL;
lookup->timer = NULL;
lookup->xfr_q = NULL;
lookup->doing_xfr = ISC_FALSE;
lookup->identify = ISC_FALSE;
lookup->recurse = recursion;
lookup->ns_search_only = showallsoa;
lookup->use_my_server_list = ISC_FALSE;
lookup->retries = tries;
ISC_LIST_INIT(lookup->q);
ISC_LIST_APPEND(lookup_list, lookup, link);
lookup->origin = NULL;
ISC_LIST_INIT(lookup->my_server_list);
have_host = ISC_TRUE;
}