dighost.c revision b8dbd3c5dce84fd6e4d1d4be23b087d46f183928
499b34cea04a46823d003d4c0520c8b03e8513cbBrian Wellington * Copyright (C) 2000 Internet Software Consortium.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * Permission to use, copy, modify, and distribute this software for any
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * purpose with or without fee is hereby granted, provided that the above
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * copyright notice and this permission notice appear in all copies.
15a44745412679c30a6d022733925af70a38b715David Lawrence * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
15a44745412679c30a6d022733925af70a38b715David Lawrence * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
15a44745412679c30a6d022733925af70a38b715David Lawrence * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
15a44745412679c30a6d022733925af70a38b715David Lawrence * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
15a44745412679c30a6d022733925af70a38b715David Lawrence * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
15a44745412679c30a6d022733925af70a38b715David Lawrence * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
15a44745412679c30a6d022733925af70a38b715David Lawrence * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence/* $Id: dighost.c,v 1.66 2000/06/30 22:57:47 bwelling Exp $ */
f80ea74e1984e0b1dbe48dd86ecdd3a2380393cbBob Halley * Notice to programmers: Do not use this code as an example of how to
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence * use the ISC library to perform DNS lookups. Dig and Host both operate
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence * on the request level, since they allow fine-tuning of output and are
fc8a6561cbb0a1fea849950a3217aacd3b8f1eacDavid Lawrence * intended as debugging tools. As a result, they perform many of the
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence * functions which could be better handled using the dns_resolver
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson * functions in most applications.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#if defined(HAVE_ADDRINFO) && defined(HAVE_GETADDRINFO)
8f79820c6930ee5ef6b4a54f36d2559400bdf47dAndreas Gustafssonisc_boolean_t have_ipv6 = ISC_FALSE, specified_source = ISC_FALSE,
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson free_now = ISC_FALSE, show_details = ISC_FALSE, usesearch=ISC_TRUE,
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson while (*s != 0) {
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson if (*s == '.')
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews printf("Printing a buffer with length %d\n", r.length);
a6733246eafeb43755ce6d7ec3627ac4209cbccbMark Andrewscheck_result(isc_result_t result, const char *msg) {
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson fatal("%s: %s", msg, isc_result_totext(result));
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson /* Tests if a field is a class, without needing isc libs
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff * initialized. This list will have to be manually kept in
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson * sync with what the libs support.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson const char *classlist[] = {"in", "hs", "chaos"};
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson for (i = 0; i < numclasses; i++)
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson /* Tests if a field is a type, without needing isc libs
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * initialized. This list will have to be manually kept in
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * sync with what the libs support.
f80ea74e1984e0b1dbe48dd86ecdd3a2380393cbBob Halley const char *typelist[] = {"a", "ns", "md", "mf", "cname",
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson for (i = 0; i < numtypes; i++) {
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson*requeue_lookup(dig_lookup_t *lookold, isc_boolean_t servers) {
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson fatal ("Memory allocation failure in %s:%d",
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson strncpy (looknew->textname, lookold-> textname, MXNAME);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson strncpy (looknew->rttext, lookold-> rttext, 32);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson strncpy (looknew->rctext, lookold-> rctext, 32);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson looknew->ixfr_serial = lookold->ixfr_serial;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson looknew->trace_root = lookold->trace_root;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson looknew->ns_search_only = lookold->ns_search_only;
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington looknew->section_question = lookold->section_question;
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington looknew->section_answer = lookold->section_answer;
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington looknew->section_authority = lookold->section_authority;
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington looknew->section_additional = lookold->section_additional;
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson looknew->use_my_server_list = lookold->use_my_server_list;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson s = ISC_LIST_HEAD(lookold->my_server_list);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson while (s != NULL) {
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson srv = isc_mem_allocate (mctx, sizeof(struct
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson ISC_LIST_ENQUEUE(looknew->my_server_list, srv,
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington "-> %p, new@%p "
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson ISC_LIST_INSERTAFTER(lookup_list, lookold, looknew, link);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson "%p, new = %p, "
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington debug("Using fixed domain %s", fixeddomain);
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington search = isc_mem_allocate( mctx, sizeof(struct dig_server));
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington fatal("Memory allocation failure in %s:%d",
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington strncpy(search->origin, fixeddomain, MXNAME - 1);
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington ISC_LIST_PREPEND(search_list, search, link);
8f79820c6930ee5ef6b4a54f36d2559400bdf47dAndreas Gustafsson get_servers = ISC_TF(server_list.head == NULL);
618a5e4923c3d064c1cc0f1866e27e8ddca90700Andreas Gustafsson while (fgets(rcinput, MXNAME, fp) != 0) {
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence sizeof(struct dig_server));
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence "failure in "
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson } else if (strcasecmp(ptr, "options") == 0) {
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews mctx, sizeof(struct
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson "failure in %s:"
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson } else if ((strcasecmp(ptr, "domain") == 0) &&
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson mctx, sizeof(struct
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson "failure in %s:"
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews srv = isc_mem_allocate(mctx, sizeof(dig_server_t));
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews if (keysecret[0] != 0) {
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson result = dns_tsigkeyring_create(mctx, &keyring);
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson check_result(result, "dns_tsigkeyring_create");
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson result = isc_buffer_allocate(mctx, &namebuf, MXNAME);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson check_result(result, "isc_buffer_allocate");
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson isc_buffer_init(&secretsrc, keysecret, strlen(keysecret));
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson isc_buffer_add(&secretsrc, strlen(keysecret));
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson isc_buffer_init(&secretbuf, secretstore, secretsize);
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence result = isc_lex_create(mctx, strlen(keysecret), &lex);
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson result = isc_lex_openbuffer(lex, &secretsrc);
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson check_result(result, "isc_lex_openbuffer");
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson result = isc_base64_tobuffer(lex, &secretbuf, -1);
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence secretsize = isc_buffer_usedlength(&secretbuf);
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence result = dns_name_fromtext(&keyname, namebuf,
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence result = dns_tsigkey_create(&keyname, dns_tsig_hmacmd5_name,
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson isc_mem_put(mctx, secretstore, secretsize);
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson * Warning: This is not particularly good randomness. We'll
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson * just use random() now for getting id values, but doing so
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson * does NOT insure that id's cann't be guessed.
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson result = isc_taskmgr_create (mctx, 1, 0, &taskmgr);
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson check_result(result, "isc_taskmgr_create");
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews result = isc_task_create (taskmgr, 0, &global_task);
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews result = isc_timermgr_create (mctx, &timermgr);
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews result = isc_socketmgr_create (mctx, &socketmgr);
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrewsadd_opt (dns_message_t *msg, isc_uint16_t udpsize) {
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson result = dns_message_gettemprdataset(msg, &rdataset);
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson check_result (result, "dns_message_gettemprdataset");
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson result = dns_message_gettemprdatalist(msg, &rdatalist);
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson check_result (result, "dns_message_gettemprdatalist");
e44487bfc23599b6b240e09d83d1c862fecfcc82Michael Graff result = dns_message_gettemprdata(msg, &rdata);
e44487bfc23599b6b240e09d83d1c862fecfcc82Michael Graff check_result (result, "dns_message_gettemprdata");
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson ISC_LIST_APPEND(rdatalist->rdata, rdata, link);
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson dns_rdatalist_tordataset(rdatalist, rdataset);
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson result = dns_message_setopt(msg, rdataset);
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson check_result (result, "dns_message_setopt");
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafssonadd_type(dns_message_t *message, dns_name_t *name, dns_rdataclass_t rdclass,
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence result = dns_message_gettemprdataset(message, &rdataset);
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson check_result(result, "dns_message_gettemprdataset()");
951c247f2923f667c5c97355b31dc564bd540bfeDavid Lawrence dns_rdataset_makequestion(rdataset, rdclass, rdtype);
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence debug ("Lookup %s pending", lookup->pending?"is":"is not");
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrencefollowup_lookup(dns_message_t *msg, dig_query_t *query,
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson result = dns_message_firstname (msg,section);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson (query->lookup->trace || query->lookup->ns_search_only))
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson followup_lookup (msg, query, DNS_SECTION_AUTHORITY);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson debug ("Following up %s", query->lookup->textname);
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence dns_message_currentname(msg, section, &name);
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson for (rdataset = ISC_LIST_HEAD(name->list);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson rdataset = ISC_LIST_NEXT(rdataset, link)) {
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews "isc_buffer_allocate");
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews "dns_rdata_totext");
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence /* Initialize lookup if we've not yet */
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence "failure in %s:%d",
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson loopresult = dns_rdataset_next(rdataset);
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson result = dns_message_nextname (msg, section);
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson if ((lookup == NULL) && (section == DNS_SECTION_ANSWER) &&
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson (query->lookup->trace || query->lookup->ns_search_only))
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson followup_lookup(msg, query, DNS_SECTION_AUTHORITY);
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrencenext_origin(dns_message_t *msg, dig_query_t *query) {
if (free_now)
MXNAME);
int len;
isc_region_t r;
isc_buffer_t b;
if (free_now)
lookup_counter = 0;
isc_buffer_usedregion (&b, &r);
isc_buffer_usedregion (&b, &r);
DNS_SECTION_QUESTION, 0);
ISC_TRUE);
sockcount--;
sendcount++;
isc_region_t r;
q != NULL;
if (q->working) {
if (!free_now) {
isc_buffer_clear(b);
isc_buffer_usedregion(b, &r);
isc_buffer_free(&b);
isc_region_t r;
if (free_now) {
isc_buffer_usedregion(b, &r);
isc_buffer_free(&b);
sockcount--;
length);
if (free_now)
sockcount--;
if (include_question) {
link);
sendcount++;
isc_region_t r;
if (free_now) {
isc_buffer_usedregion(b, &r);
isc_buffer_free(&b);
static isc_boolean_t
return (ISC_TRUE);
return (ISC_FALSE);
static isc_boolean_t
isc_buffer_t b;
isc_region_t r;
return (ISC_TRUE);
#ifdef NEVER
&name);
return (ISC_TRUE);
goto NEXT_RDATA;
&soa,
mctx);
goto XFR_DONE;
goto NEXT_RDATA;
goto NEXT_RDATA;
isc_buffer_usedregion(&b, &r);
return (ISC_TRUE);
goto NEXT_RDATA;
goto XFR_DONE;
return (ISC_FALSE);
isc_region_t r;
dig_lookup_t *n;
if (free_now) {
if (free_now) {
sendcount--;
&msg);
hex_dump(b);
if ((show_details ||
== ISC_R_SUCCESS))) &&
if (show_details) {
== ISC_R_SUCCESS) &&
if (docancel) {
&ab);
== ISC_R_SUCCESS) &&
(char *)r.base,
query);
int result;
if (result != 0) {
port);
sockcount++;
if (specified_source)
sockcount++;
if (specified_source)
start_lookup(void) {
if (free_now)
void *ptr;
dig_lookup_t *l;
dig_query_t *q;
dig_server_t *s;
dig_searchlist_t *o;
if (free_now)
while (l != NULL) {
q = ISC_LIST_HEAD(l->q);
while (q != NULL) {
sockcount--;
while (s != NULL) {
ptr = s;
while (o != NULL) {
ptr = o;
while (l != NULL) {
q = ISC_LIST_HEAD(l->q);
while (q != NULL) {
link);
link);
ptr = q;
if (l->use_my_server_list) {
while (s != NULL) {
ptr = s;
ptr = l;
if (is_dst_up) {
if (_exitcode != 0)