dighost.c revision 3ab1cd05b892fbe8fcaed812db67b3d5165dca3e
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson/*
c19f42a378f76af272166f9cababe7bfbc1cf6abTinderbox User * Copyright (C) 2000 Internet Software Consortium.
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews *
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * 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
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * copyright notice and this permission notice appear in all copies.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson *
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson */
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
e1cf6fd20a9f56c6c31581480445dea96fc7ac60Mark Andrews/* $Id: dighost.c,v 1.112 2000/08/03 18:23:16 mws Exp $ */
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein/*
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * Notice to programmers: Do not use this code as an example of how to
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * use the ISC library to perform DNS lookups. Dig and Host both operate
f80ea74e1984e0b1dbe48dd86ecdd3a2380393cbBob Halley * on the request level, since they allow fine-tuning of output and are
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt * intended as debugging tools. As a result, they perform many of the
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence * functions which could be better handled using the dns_resolver
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt * functions in most applications.
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 */
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence#include <config.h>
fc8a6561cbb0a1fea849950a3217aacd3b8f1eacDavid Lawrence#include <stdlib.h>
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence#include <unistd.h>
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson#include <netdb.h>
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt#include <string.h>
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#include <limits.h>
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#if (!(defined(HAVE_ADDRINFO) && defined(HAVE_GETADDRINFO)))
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonextern int h_errno;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#endif
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
261a6a1f7d95eaf0cd882f3123dcfd775517a54fMark Andrews#include <dns/message.h>
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#include <dns/name.h>
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington#include <dns/rdata.h>
6c8abf481df85a67c3f32f5f107b554d3ff5a3edMark Andrews#include <dns/rdataclass.h>
6c8abf481df85a67c3f32f5f107b554d3ff5a3edMark Andrews#include <dns/rdatalist.h>
6c8abf481df85a67c3f32f5f107b554d3ff5a3edMark Andrews#include <dns/rdataset.h>
09f22ac5b09e70bc526015f37168ba33e21ea91fDavid Lawrence#include <dns/rdatastruct.h>
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt#include <dns/rdatatype.h>
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#include <dns/result.h>
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt#include <dns/tsig.h>
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt#include <dst/dst.h>
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉#include <isc/app.h>
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#include <isc/base64.h>
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff#include <isc/entropy.h>
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#include <isc/lang.h>
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#include <isc/netdb.h>
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#include <isc/result.h>
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#include <isc/string.h>
f3350b671881f175d03e16fa5d0f685a1691bcabMark Andrews#include <isc/task.h>
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff#include <isc/timer.h>
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#include <isc/types.h>
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence#include <isc/util.h>
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence#include <dig/dig.h>
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉
f991461dc144794d80ff8d85e430fd8a6e6113f3David LawrenceISC_LIST(dig_lookup_t) lookup_list;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssondig_serverlist_t server_list;
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉ISC_LIST(dig_searchlist_t) search_list;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonisc_boolean_t
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson have_ipv6 = ISC_FALSE,
584848087f7463c1f659ce4712dc047d8e7f2b07Francis Dupont specified_source = ISC_FALSE,
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence free_now = ISC_FALSE,
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson cancel_now = ISC_FALSE,
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson show_details = ISC_FALSE,
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson usesearch = ISC_FALSE,
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson qr = ISC_FALSE,
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson is_dst_up = ISC_FALSE,
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 have_domain = ISC_FALSE;
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉in_port_t port = 53;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonunsigned int timeout = 0;
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafssonisc_mem_t *mctx = NULL;
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrenceisc_taskmgr_t *taskmgr = NULL;
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrenceisc_task_t *global_task = NULL;
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrenceisc_timermgr_t *timermgr = NULL;
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉isc_socketmgr_t *socketmgr = NULL;
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉isc_sockaddr_t bind_address;
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉isc_sockaddr_t bind_any;
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉isc_buffer_t rootbuf;
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉int sendcount = 0;
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafssonint recvcount = 0;
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austeinint sockcount = 0;
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrenceint ndots = -1;
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrenceint tries = 2;
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrenceint lookup_counter = 0;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonchar fixeddomain[MXNAME] = "";
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉/*
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater * Exit Codes:
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 * 0 Everything went well, including things like NXDOMAIN
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater * 1 Usage error
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater * 7 Got too many RR's or Names
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater * 8 Couldn't open batch file
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater * 9 No reply from server
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater * 10 Internal error
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 */
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉int exitcode = 0;
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉char keynametext[MXNAME];
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updaterchar keyfile[MXNAME] = "";
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉char keysecret[MXNAME] = "";
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉dns_name_t keyname;
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉isc_buffer_t *namebuf = NULL;
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉dns_tsigkey_t *key = NULL;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updaterisc_boolean_t validated = ISC_TRUE;
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉isc_entropy_t *entp = NULL;
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉isc_mempool_t *commctx = NULL;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updaterisc_boolean_t debugging = ISC_FALSE;
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉char *progname = NULL;
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉isc_mutex_t lookup_lock;
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉dig_lookup_t *current_lookup = NULL;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonisc_uint32_t name_limit = INT_MAX;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonisc_uint32_t rr_limit = INT_MAX;
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence/*
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * Apply and clear locks at the event level in global task.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * Can I get rid of these using shutdown events? XXX
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson */
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater#define LOCK_LOOKUP {\
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 debug("lock_lookup %s:%d", __FILE__, __LINE__);\
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 check_result(isc_mutex_lock((&lookup_lock)), "isc_mutex_lock");\
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 debug("success");\
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt}
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt#define UNLOCK_LOOKUP {\
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉 debug("unlock_lookup %s:%d", __FILE__, __LINE__);\
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater check_result(isc_mutex_unlock((&lookup_lock)),\
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater "isc_mutex_unlock");\
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉}
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉static void
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉cancel_lookup(dig_lookup_t *lookup);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉static void
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉recv_done(isc_task_t *task, isc_event_t *event);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉
2a184ff86544cc67c36e2ce6bb3ddb5ac44684b8Evan Huntstatic void
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Huntconnect_timeout(isc_task_t *task, isc_event_t *event);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updaterstatic int
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉count_dots(char *string) {
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater char *s;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson int i = 0;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson s = string;
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 while (*s != '\0') {
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson if (*s == '.')
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson i++;
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson s++;
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence }
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater return (i);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson}
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonstatic void
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonhex_dump(isc_buffer_t *b) {
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson unsigned int len;
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 isc_region_t r;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews isc_buffer_remainingregion(b, &r);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews printf("Printing a buffer with length %d\n", r.length);
261a6a1f7d95eaf0cd882f3123dcfd775517a54fMark Andrews for (len = 0; len < r.length; len++) {
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 printf("%02x ", r.base[len]);
261a6a1f7d95eaf0cd882f3123dcfd775517a54fMark Andrews if (len != 0 && len % 16 == 0)
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington printf("\n");
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington }
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater if (len % 16 != 0)
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater printf("\n");
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater}
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellingtonvoid
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrencefatal(const char *format, ...) {
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt va_list args;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater fprintf(stderr, "%s: ", progname);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater va_start(args, format);
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉 vfprintf(stderr, format, args);
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt va_end(args);
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉 fprintf(stderr, "\n");
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉 if (exitcode < 10)
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉 exitcode = 10;
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉 exit(exitcode);
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt}
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Huntvoid
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Huntdebug(const char *format, ...) {
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt va_list args;
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt if (debugging) {
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt va_start(args, format);
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt vfprintf(stderr, format, args);
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt va_end(args);
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt fprintf(stderr, "\n");
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt }
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉}
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉void
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssoncheck_result(isc_result_t result, const char *msg) {
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater if (result != ISC_R_SUCCESS) {
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater fatal("%s: %s", msg, isc_result_totext(result));
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt }
5d7849ad7ffc6d08870dbfbc8d6bfffd90007488Tatuya JINMEI 神明達哉}
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater/*
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater * Create a server structure, which is part of the lookup structure.
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt * This is little more than a linked list of servers to query in hopes
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt * of finding the answer the user is looking for
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉 */
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updaterdig_server_t *
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Huntmake_server(const char *servname) {
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater dig_server_t *srv;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater REQUIRE(servname != NULL);
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt debug("make_server(%s)",servname);
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt srv = isc_mem_allocate(mctx, sizeof(struct dig_server));
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater if (srv == NULL)
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉 fatal("Memory allocation failure in %s:%d",
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉 __FILE__, __LINE__);
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt strncpy(srv->servername, servname, MXNAME);
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉 return (srv);
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉}
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉/*
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉 * Produce a cloned server list. The dest list must have already had
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉 * ISC_LIST_INIT applied.
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater */
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updatervoid
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updaterclone_server_list(dig_serverlist_t src,
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater dig_serverlist_t *dest)
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater{
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater dig_server_t *srv, *newsrv;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater debug("clone_server_list()");
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater srv = ISC_LIST_HEAD(src);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater while (srv != NULL) {
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater newsrv = make_server(srv->servername);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater ISC_LIST_ENQUEUE(*dest, newsrv, link);
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt srv = ISC_LIST_NEXT(srv, link);
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt }
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt}
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt/*
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt * Create an empty lookup structure, which holds all the information needed
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt * to get an answer to a user's question. This structure contains two
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater * linked lists: the server list (servers to query) and the query list
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater * (outstanding queries which have been made to the listed servers).
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt */
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updaterdig_lookup_t *
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updatermake_empty_lookup(void) {
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt dig_lookup_t *looknew;
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt debug("make_lookup()");
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt INSIST(!free_now);
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt looknew = isc_mem_allocate(mctx, sizeof(struct dig_lookup));
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt if (looknew == NULL)
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt fatal("Memory allocation failure in %s:%d",
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt __FILE__, __LINE__);
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt looknew->pending = ISC_TRUE;
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt looknew->textname[0]=0;
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt looknew->rdtype=dns_rdatatype_a;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater looknew->rdclass=dns_rdataclass_in;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater looknew->sendspace = NULL;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater looknew->sendmsg = NULL;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater looknew->name = NULL;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater looknew->oname = NULL;
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt looknew->timer = NULL;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater looknew->xfr_q = NULL;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater looknew->doing_xfr = ISC_FALSE;
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt looknew->ixfr_serial = ISC_FALSE;
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt looknew->defname = ISC_FALSE;
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt looknew->trace = ISC_FALSE;
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt looknew->trace_root = ISC_FALSE;
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt looknew->identify = ISC_FALSE;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater looknew->udpsize = 0;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater looknew->recurse = ISC_TRUE;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater looknew->aaonly = ISC_FALSE;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater looknew->adflag = ISC_FALSE;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater looknew->cdflag = ISC_FALSE;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater looknew->ns_search_only = ISC_FALSE;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater looknew->origin = NULL;
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt looknew->querysig = NULL;
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt looknew->retries = tries;
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt looknew->nsfound = 0;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater looknew->tcp_mode = ISC_FALSE;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater looknew->comments = ISC_TRUE;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater looknew->stats = ISC_TRUE;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater looknew->section_question = ISC_TRUE;
5d7849ad7ffc6d08870dbfbc8d6bfffd90007488Tatuya JINMEI 神明達哉 looknew->section_answer = ISC_TRUE;
5d7849ad7ffc6d08870dbfbc8d6bfffd90007488Tatuya JINMEI 神明達哉 looknew->section_authority = ISC_TRUE;
5d7849ad7ffc6d08870dbfbc8d6bfffd90007488Tatuya JINMEI 神明達哉 looknew->section_additional = ISC_TRUE;
5d7849ad7ffc6d08870dbfbc8d6bfffd90007488Tatuya JINMEI 神明達哉 looknew->new_search = ISC_FALSE;
5d7849ad7ffc6d08870dbfbc8d6bfffd90007488Tatuya JINMEI 神明達哉 ISC_LIST_INIT(looknew->q);
b8a9632333a92d73a503afe1aaa7990016c8bee9Evan Hunt ISC_LIST_INIT(looknew->my_server_list);
b8a9632333a92d73a503afe1aaa7990016c8bee9Evan Hunt return (looknew);
5d7849ad7ffc6d08870dbfbc8d6bfffd90007488Tatuya JINMEI 神明達哉}
5d7849ad7ffc6d08870dbfbc8d6bfffd90007488Tatuya JINMEI 神明達哉
5d7849ad7ffc6d08870dbfbc8d6bfffd90007488Tatuya JINMEI 神明達哉/*
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater * Clone a lookup, perhaps copying the server list. This does not clone
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater * the query list, since it will be regenerated by the setup_lookup()
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater * function, nor does it queue up the new lookup for processing.
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater * Caution: If you don't clone the servers, you MUST clone the server
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater * list seperately from somewhere else, or construct it by hand.
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 */
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉dig_lookup_t *
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉clone_lookup(dig_lookup_t *lookold, isc_boolean_t servers) {
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 dig_lookup_t *looknew;
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 debug("clone_lookup()");
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 INSIST(!free_now);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 looknew = make_empty_lookup();
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater INSIST(looknew != NULL);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater strncpy(looknew->textname, lookold-> textname, MXNAME);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater looknew->rdtype = lookold->rdtype;
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt looknew->rdclass = lookold->rdclass;
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt looknew->doing_xfr = lookold->doing_xfr;
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt looknew->ixfr_serial = lookold->ixfr_serial;
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt looknew->defname = lookold->defname;
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt looknew->trace = lookold->trace;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater looknew->trace_root = lookold->trace_root;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater looknew->identify = lookold->identify;
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson looknew->udpsize = lookold->udpsize;
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson looknew->recurse = lookold->recurse;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater looknew->aaonly = lookold->aaonly;
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington looknew->adflag = lookold->adflag;
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt looknew->cdflag = lookold->cdflag;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater looknew->ns_search_only = lookold->ns_search_only;
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt looknew->tcp_mode = lookold->tcp_mode;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater looknew->comments = lookold->comments;
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt looknew->stats = lookold->stats;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater looknew->section_question = lookold->section_question;
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington looknew->section_answer = lookold->section_answer;
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt looknew->section_authority = lookold->section_authority;
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington looknew->section_additional = lookold->section_additional;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt if (servers)
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt clone_server_list(lookold->my_server_list,
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington &looknew->my_server_list);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater return (looknew);
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson}
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt/*
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt * Requeue a lookup for further processing, perhaps copying the server
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt * list. The new lookup structure is returned to the caller, and is
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater * queued for processing. If servers are not cloned in the requeue, they
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * must be added before allowing the current event to complete, since the
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * completion of the event may result in the next entry on the lookup
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * queue getting run.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson */
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updaterdig_lookup_t *
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafssonrequeue_lookup(dig_lookup_t *lookold, isc_boolean_t servers) {
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater dig_lookup_t *looknew;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews debug("requeue_lookup()");
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson lookup_counter++;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater if (lookup_counter > LOOKUP_LIMIT)
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater fatal("Too many lookups");
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 looknew = clone_lookup(lookold, servers);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 INSIST(looknew != NULL);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 debug("before insertion, init@%p "
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 "-> %p, new@%p -> %p",
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 lookold, lookold->link.next, looknew, looknew->link.next);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 ISC_LIST_PREPEND(lookup_list, looknew, link);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 debug("after insertion, init -> "
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 "%p, new = %p, new -> %p",
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater lookold, looknew, looknew->link.next);
ce3c351d815569455892c30dc78d8c2e85ec1076Andreas Gustafsson return (looknew);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater}
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updaterstatic void
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonsetup_text_key(void) {
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater isc_result_t result;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater isc_buffer_t secretbuf;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson int secretsize;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater unsigned char *secretstore;
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt isc_stdtime_t now;
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt debug("setup_text_key()");
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt result = isc_buffer_allocate(mctx, &namebuf, MXNAME);
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt check_result(result, "isc_buffer_allocate");
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt dns_name_init(&keyname, NULL);
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt check_result(result, "dns_name_init");
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt isc_buffer_putstr(namebuf, keynametext);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater secretsize = strlen(keysecret) * 3 / 4;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater secretstore = isc_mem_allocate(mctx, secretsize);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater if (secretstore == NULL)
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater fatal("Memory allocation failure in %s:%d",
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater __FILE__, __LINE__);
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington isc_buffer_init(&secretbuf, secretstore, secretsize);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater result = isc_base64_decodestring(mctx, keysecret,
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater &secretbuf);
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington if (result != ISC_R_SUCCESS) {
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉 printf(";; Couldn't create key %s: %s\n",
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉 keynametext, isc_result_totext(result));
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉 goto failure;
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt }
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt secretsize = isc_buffer_usedlength(&secretbuf);
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt isc_stdtime_get(&now);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater result = dns_name_fromtext(&keyname, namebuf,
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt dns_rootname, ISC_FALSE,
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater namebuf);
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt if (result != ISC_R_SUCCESS) {
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt printf(";; Couldn't create key %s: %s\n",
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence keynametext, dns_result_totext(result));
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson goto failure;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson }
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson result = dns_tsigkey_create(&keyname, dns_tsig_hmacmd5_name,
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson secretstore, secretsize,
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson ISC_TRUE, NULL, now, now, mctx,
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater NULL, &key);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater if (result != ISC_R_SUCCESS) {
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson printf(";; Couldn't create key %s: %s\n",
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater keynametext, dns_result_totext(result));
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater }
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater failure:
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson isc_mem_free(mctx, secretstore);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater dns_name_invalidate(&keyname);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson isc_buffer_free(&namebuf);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson}
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updaterstatic void
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updatersetup_file_key(void) {
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater isc_result_t result;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater isc_buffer_t secretbuf;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater unsigned char *secretstore = NULL;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater int secretlen;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater dst_key_t *dstkey = NULL;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater isc_stdtime_t now;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater debug("setup_file_key()");
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater result = dst_key_fromnamedfile(keyfile, DST_TYPE_PRIVATE,
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater mctx, &dstkey);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater if (result != ISC_R_SUCCESS) {
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater fprintf(stderr, "Couldn't read key from %s: %s\n",
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater keyfile, isc_result_totext(result));
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater goto failure;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater }
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater /*
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater * Get key size in bits, convert to bytes, rounding up (?)
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater */
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater secretlen = (dst_key_size(dstkey) + 7) >> 3;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater secretstore = isc_mem_allocate(mctx, secretlen);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater if (secretstore == NULL)
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater fatal("out of memory");
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater isc_buffer_init(&secretbuf, secretstore, secretlen);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater result = dst_key_tobuffer(dstkey, &secretbuf);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater if (result != ISC_R_SUCCESS) {
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater fprintf(stderr, "Couldn't read key from %s: %s\n",
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater keyfile, isc_result_totext(result));
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater goto failure;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater }
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater isc_stdtime_get(&now);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater dns_name_init(&keyname, NULL);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater dns_name_clone(dst_key_name(dstkey), &keyname);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater result = dns_tsigkey_create(&keyname, dns_tsig_hmacmd5_name,
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater secretstore, secretlen,
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater ISC_TRUE, NULL, now, now, mctx,
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater NULL, &key);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater if (result != ISC_R_SUCCESS) {
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater printf(";; Couldn't create key %s: %s\n",
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater keynametext, dns_result_totext(result));
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson }
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson failure:
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson if (dstkey != NULL)
b120e39deac3e9c323cdc7801ed2313f8ebbb744Andreas Gustafsson dst_key_free(&dstkey);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater if (secretstore != NULL)
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater isc_mem_free(mctx, secretstore);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater}
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater/*
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater * Setup the system as a whole, reading key information and resolv.conf
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater * settings.
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence */
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonvoid
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonsetup_system(void) {
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson char rcinput[MXNAME];
4e1d3e67cdc76609bad5f0310ac48de10b442b9fMark Andrews FILE *fp;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater char *ptr;
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington dig_server_t *srv;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater dig_searchlist_t *search;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater isc_boolean_t get_servers;
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater debug("setup_system()");
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater if (fixeddomain[0] != 0) {
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence debug("using fixed domain %s", fixeddomain);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater search = isc_mem_allocate(mctx, sizeof(struct dig_server));
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater if (search == NULL)
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater fatal("Memory allocation failure in %s:%d",
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater __FILE__, __LINE__);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater strncpy(search->origin, fixeddomain,
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence sizeof(search->origin) - 1);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater /* XXX Check ordering, with search -vs- domain */
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson ISC_LIST_PREPEND(search_list, search, link);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson }
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson free_now = ISC_FALSE;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater get_servers = ISC_TF(server_list.head == NULL);
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington fp = fopen(RESOLVCONF, "r");
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater /* XXX Use lwres resolv.conf reader */
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington if (fp != NULL) {
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater while (fgets(rcinput, MXNAME, fp) != 0) {
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater ptr = strtok(rcinput, " \t\r\n");
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence if (ptr != NULL) {
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater if (get_servers &&
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater strcasecmp(ptr, "nameserver") == 0) {
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater debug("got a nameserver line");
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence ptr = strtok(NULL, " \t\r\n");
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater if (ptr != NULL) {
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson srv = make_server(ptr);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson ISC_LIST_APPEND
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson (server_list,
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson srv, link);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater }
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington } else if (strcasecmp(ptr, "options") == 0) {
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater ptr = strtok(NULL, " \t\r\n");
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson if (ptr != NULL) {
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater if((strncasecmp(ptr, "ndots:",
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater 6) == 0) &&
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence (ndots == -1)) {
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater ndots = atoi(
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater &ptr[6]);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater debug("ndots is "
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater "%d.",
7963a67df0bd8f4cea292228dbe1060fbb924dc7Mark Andrews ndots);
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence }
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington }
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson } else if (strcasecmp(ptr, "search") == 0){
8f79820c6930ee5ef6b4a54f36d2559400bdf47dAndreas Gustafsson while ((ptr = strtok(NULL, " \t\r\n"))
8f79820c6930ee5ef6b4a54f36d2559400bdf47dAndreas Gustafsson != NULL) {
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater debug("adding search %s",
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater ptr);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater search = isc_mem_allocate(
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater mctx, sizeof(struct
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater dig_server));
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater if (search == NULL)
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater fatal("Memory "
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater "allocation "
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater "failure in %s:"
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater "%d", __FILE__,
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater __LINE__);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater strncpy(search->
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater origin,
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater ptr,
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater MXNAME - 1);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater ISC_LIST_APPEND
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater (search_list,
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater search,
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater link);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater }
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater } else if ((strcasecmp(ptr, "domain") == 0) &&
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater (fixeddomain[0] == 0 )){
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater have_domain = ISC_TRUE;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater while ((ptr = strtok(NULL, " \t\r\n"))
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater != NULL) {
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater search = isc_mem_allocate(
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater mctx, sizeof(struct
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater dig_server));
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater if (search == NULL)
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater fatal("Memory "
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson "allocation "
618a5e4923c3d064c1cc0f1866e27e8ddca90700Andreas Gustafsson "failure in %s:"
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater "%d", __FILE__,
8f79820c6930ee5ef6b4a54f36d2559400bdf47dAndreas Gustafsson __LINE__);
8f79820c6930ee5ef6b4a54f36d2559400bdf47dAndreas Gustafsson strncpy(search->
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉 origin,
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉 ptr,
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉 MXNAME - 1);
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉 ISC_LIST_PREPEND
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉 (search_list,
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉 search,
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉 link);
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉 }
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉 }
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉 }
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉 }
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉 fclose(fp);
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉 }
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉 if (ndots == -1)
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉 ndots = 1;
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉 if (server_list.head == NULL) {
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉 srv = make_server("127.0.0.1");
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉 ISC_LIST_APPEND(server_list, srv, link);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson }
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson if (keyfile[0] != 0)
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson setup_file_key();
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson else if (keysecret[0] != 0)
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson setup_text_key();
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence}
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson/*
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater * Setup the ISC and DNS libraries for use by the system.
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff */
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updatervoid
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updatersetup_libs(void) {
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater isc_result_t result;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater debug("setup_libs()");
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater /*
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 * Warning: This is not particularly good randomness. We'll
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater * just use random() now for getting id values, but doing so
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 * does NOT insure that id's cann't be guessed.
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater */
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater srandom(getpid() + (int)&setup_libs);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 result = isc_net_probeipv4();
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 check_result(result, "isc_net_probeipv4");
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 result = isc_net_probeipv6();
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 if (result == ISC_R_SUCCESS)
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 have_ipv6 = ISC_TRUE;
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 result = isc_mem_create(0, 0, &mctx);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater check_result(result, "isc_mem_create");
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater result = isc_taskmgr_create(mctx, 1, 0, &taskmgr);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater check_result(result, "isc_taskmgr_create");
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater result = isc_task_create(taskmgr, 0, &global_task);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater check_result(result, "isc_task_create");
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater result = isc_timermgr_create(mctx, &timermgr);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater check_result(result, "isc_timermgr_create");
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater result = isc_socketmgr_create(mctx, &socketmgr);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater check_result(result, "isc_socketmgr_create");
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater result = isc_entropy_create(mctx, &entp);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater check_result(result, "isc_entropy_create");
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater result = dst_lib_init(mctx, entp, 0);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater check_result(result, "dst_lib_init");
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater is_dst_up = ISC_TRUE;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater result = isc_mempool_create(mctx, COMMSIZE, &commctx);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater check_result(result, "isc_mempool_create");
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater isc_mempool_setname(commctx, "COMMPOOL");
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 /*
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater * 6 and 2 set as reasonable parameters for 3 or 4 nameserver
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater * systems.
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater */
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater isc_mempool_setfreemax(commctx, 6);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater isc_mempool_setfillcount(commctx, 2);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater result = isc_mutex_init(&lookup_lock);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater check_result(result, "isc_mutex_init");
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater}
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉/*
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 * Add EDNS0 option record to a message. Currently, the only supported
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 * option is UDP buffer size.
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 */
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉static void
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉add_opt(dns_message_t *msg, isc_uint16_t udpsize) {
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 dns_rdataset_t *rdataset = NULL;
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 dns_rdatalist_t *rdatalist = NULL;
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 dns_rdata_t *rdata = NULL;
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 isc_result_t result;
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 debug("add_opt()");
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 result = dns_message_gettemprdataset(msg, &rdataset);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 check_result(result, "dns_message_gettemprdataset");
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 dns_rdataset_init(rdataset);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 result = dns_message_gettemprdatalist(msg, &rdatalist);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 check_result(result, "dns_message_gettemprdatalist");
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 result = dns_message_gettemprdata(msg, &rdata);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 check_result(result, "dns_message_gettemprdata");
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff debug("setting udp size of %d", udpsize);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater rdatalist->type = dns_rdatatype_opt;
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence rdatalist->covers = 0;
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson rdatalist->rdclass = udpsize;
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 rdatalist->ttl = 0;
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 rdata->data = NULL;
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 rdata->length = 0;
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 ISC_LIST_INIT(rdatalist->rdata);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater ISC_LIST_APPEND(rdatalist->rdata, rdata, link);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater dns_rdatalist_tordataset(rdatalist, rdataset);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater result = dns_message_setopt(msg, rdataset);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater check_result(result, "dns_message_setopt");
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉}
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater/*
ce3c351d815569455892c30dc78d8c2e85ec1076Andreas Gustafsson * Add a question section to a message, asking for the specified name,
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater * type, and class.
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson */
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafssonstatic void
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉add_question(dns_message_t *message, dns_name_t *name,
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 dns_rdataclass_t rdclass, dns_rdatatype_t rdtype)
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉{
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 dns_rdataset_t *rdataset;
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 isc_result_t result;
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 debug("add_question()");
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 rdataset = NULL;
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 result = dns_message_gettemprdataset(message, &rdataset);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 check_result(result, "dns_message_gettemprdataset()");
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 dns_rdataset_init(rdataset);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 dns_rdataset_makequestion(rdataset, rdclass, rdtype);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 ISC_LIST_APPEND(name->list, rdataset, link);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉}
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉/*
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 * Check if we're done with all the queued lookups, which is true iff
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 * all sockets, sends, and recvs are accounted for (counters == 0),
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 * and the lookup list is empty.
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 * If we are done, pass control back out to dighost_shutdown() (which is
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 * part of dig.c, host.c, or nslookup.c) to either shutdown the system as
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 * a whole or reseed the lookup list.
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 */
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉static void
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉check_if_done(void) {
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 debug("check_if_done()");
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 debug("list %s", ISC_LIST_EMPTY(lookup_list) ? "empty" : "full");
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 if (ISC_LIST_EMPTY(lookup_list) && current_lookup == NULL &&
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 sendcount == 0) {
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 INSIST(sockcount == 0);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 INSIST(recvcount == 0);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 debug("shutting down");
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 dighost_shutdown();
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 }
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉}
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉/*
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 * Clear out a query when we're done with it. WARNING: This routine
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 * WILL invalidate the query pointer.
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 */
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉static void
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉clear_query(dig_query_t *query) {
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 dig_lookup_t *lookup;
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 REQUIRE(query != NULL);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 debug("clear_query(%p)",query);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 lookup = query->lookup;
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 ISC_LIST_UNLINK(lookup->q, query, link);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 if (ISC_LINK_LINKED(&query->recvbuf, link))
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 ISC_LIST_DEQUEUE(query->recvlist, &query->recvbuf,
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 link);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 if (ISC_LINK_LINKED(&query->lengthbuf, link))
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 ISC_LIST_DEQUEUE(query->lengthlist, &query->lengthbuf,
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 link);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 INSIST(query->recvspace != NULL);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 if (query->sock != NULL) {
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 isc_socket_detach(&query->sock);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 sockcount--;
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 debug("sockcount=%d", sockcount);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 }
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 isc_mempool_put(commctx, query->recvspace);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 isc_buffer_invalidate(&query->recvbuf);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 isc_buffer_invalidate(&query->lengthbuf);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 isc_mem_free(mctx, query);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉}
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉/*
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 * Try and clear out a lookup if we're done with it. Return ISC_TRUE if
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 * the lookup was successfully cleared. If ISC_TRUE is returned, the
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 * lookup pointer has been invalidated.
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 */
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉static isc_boolean_t
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉try_clear_lookup(dig_lookup_t *lookup) {
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 dig_server_t *s;
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson dig_query_t *q;
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson void *ptr;
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson REQUIRE(lookup != NULL);
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater debug("try_clear_lookup(%p)", lookup);
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater if (ISC_LIST_HEAD(lookup->q) != NULL) {
e44487bfc23599b6b240e09d83d1c862fecfcc82Michael Graff if (debugging) {
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater q = ISC_LIST_HEAD(lookup->q);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater while (q != NULL) {
86a292d45977f1d8400df04b04bf659d9a5d68a1Andreas Gustafsson debug ("query to %s still pending",
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater q->servname);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater q = ISC_LIST_NEXT(q, link);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater }
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence return (ISC_FALSE);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 }
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 }
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 /*
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater * At this point, we know there are no queries on the lookup,
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * so can make it go away also.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson */
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 debug("cleared");
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 s = ISC_LIST_HEAD(lookup->my_server_list);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 while (s != NULL) {
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 debug("freeing server %p belonging to %p",
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 s, lookup);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 ptr = s;
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 s = ISC_LIST_NEXT(s, link);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 ISC_LIST_DEQUEUE(lookup->my_server_list,
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 (dig_server_t *)ptr, link);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 isc_mem_free(mctx, ptr);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 }
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 if (lookup->sendmsg != NULL)
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 dns_message_destroy(&lookup->sendmsg);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 if (lookup->querysig != NULL) {
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 debug("freeing buffer %p", lookup->querysig);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 isc_buffer_free(&lookup->querysig);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 }
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 if (lookup->timer != NULL)
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 isc_timer_detach(&lookup->timer);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 if (lookup->sendspace != NULL)
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 isc_mempool_put(commctx, lookup->sendspace);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 isc_mem_free(mctx, lookup);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 return (ISC_TRUE);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉}
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉/*
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 * If we can, start the next lookup in the queue running.
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 * This assumes that the lookup on the head of the queue hasn't been
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 * started yet. It also removes the lookup from the head of the queue,
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 * setting the current_lookup pointer pointing to it.
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 */
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉void
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉start_lookup(void) {
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 debug("start_lookup()");
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 if (cancel_now)
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 return;
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 /*
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 * If there's a current lookup running, we really shouldn't get
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 * here.
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 */
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 INSIST(current_lookup == NULL);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 current_lookup = ISC_LIST_HEAD(lookup_list);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 /*
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 * Put the current lookup somewhere so cancel_all can find it
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 */
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 if (current_lookup != NULL) {
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 ISC_LIST_DEQUEUE(lookup_list, current_lookup, link);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 setup_lookup(current_lookup);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 do_lookup(current_lookup);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 } else {
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 check_if_done();
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 }
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉}
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉/*
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 * If we can, clear the current lookup and start the next one running.
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 * This calls try_clear_lookup, so may invalidate the lookup pointer.
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 */
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉static void
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉check_next_lookup(dig_lookup_t *lookup) {
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 INSIST(!free_now);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 debug("check_next_lookup(%p)", lookup);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 if (ISC_LIST_HEAD(lookup->q) != NULL) {
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 debug("still have a worker");
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 return;
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 }
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 if (try_clear_lookup(lookup)) {
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 current_lookup = NULL;
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 start_lookup();
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 }
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉}
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉/*
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 * Create and queue a new lookup as a followup to the current lookup,
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 * based on the supplied message and section. This is used in trace and
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 * name server search modes to start a new lookup using servers from
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 * NS records in a reply.
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 */
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉static void
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉followup_lookup(dns_message_t *msg, dig_query_t *query,
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 dns_section_t section) {
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 dig_lookup_t *lookup = NULL;
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 dig_server_t *srv = NULL;
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 dns_rdataset_t *rdataset = NULL;
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 dns_rdata_t rdata;
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 dns_name_t *name = NULL;
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 isc_result_t result, loopresult;
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 isc_buffer_t *b = NULL;
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 isc_region_t r;
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 isc_boolean_t success = ISC_FALSE;
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 int len;
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 INSIST(!free_now);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 debug("followup_lookup()");
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 result = dns_message_firstname(msg,section);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 if (result != ISC_R_SUCCESS) {
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 debug("firstname returned %s",
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 isc_result_totext(result));
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 if ((section == DNS_SECTION_ANSWER) &&
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 (query->lookup->trace || query->lookup->ns_search_only))
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 followup_lookup(msg, query, DNS_SECTION_AUTHORITY);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 return;
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 }
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 debug("following up %s", query->lookup->textname);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 for (;;) {
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 name = NULL;
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 dns_message_currentname(msg, section, &name);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 for (rdataset = ISC_LIST_HEAD(name->list);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 rdataset != NULL;
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 rdataset = ISC_LIST_NEXT(rdataset, link)) {
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 loopresult = dns_rdataset_first(rdataset);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 while (loopresult == ISC_R_SUCCESS) {
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 dns_rdataset_current(rdataset, &rdata);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 debug("got rdata with type %d",
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 rdata.type);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 if ((rdata.type == dns_rdatatype_ns) &&
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 (!query->lookup->trace_root ||
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 (query->lookup->nsfound < MXSERV)))
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 {
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 query->lookup->nsfound++;
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 result = isc_buffer_allocate(mctx, &b,
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 BUFSIZE);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 check_result(result,
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 "isc_buffer_allocate");
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 result = dns_rdata_totext(&rdata,
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 NULL,
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 b);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 check_result(result,
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 "dns_rdata_totext");
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 isc_buffer_usedregion(b, &r);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 len = r.length-1;
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 if (len >= MXNAME)
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 len = MXNAME-1;
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 /* Initialize lookup if we've not yet */
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 debug("found NS %d %.*s",
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 (int)r.length, (int)r.length,
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 (char *)r.base);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 if (!success) {
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 success = ISC_TRUE;
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 lookup_counter++;
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 cancel_lookup(query->lookup);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 lookup = requeue_lookup
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 (query->lookup,
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 ISC_FALSE);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 lookup->doing_xfr = ISC_FALSE;
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 lookup->defname = ISC_FALSE;
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 if (section ==
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 DNS_SECTION_ANSWER) {
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 lookup->trace =
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 ISC_FALSE;
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 lookup->ns_search_only =
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson ISC_FALSE;
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence }
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson else {
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson lookup->trace =
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson query->
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater lookup->trace;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater lookup->ns_search_only =
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater query->
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater lookup->ns_search_only;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater }
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrews lookup->trace_root = ISC_FALSE;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater }
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater r.base[len]=0;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater srv = make_server(r.base);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater debug("adding server %s",
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater srv->servername);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater ISC_LIST_APPEND
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater (lookup->my_server_list,
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater srv, link);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater isc_buffer_free(&b);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater }
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater loopresult = dns_rdataset_next(rdataset);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater }
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater }
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater result = dns_message_nextname(msg, section);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater if (result != ISC_R_SUCCESS)
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater break;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater }
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater if ((lookup == NULL) && (section == DNS_SECTION_ANSWER) &&
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater (query->lookup->trace || query->lookup->ns_search_only))
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater followup_lookup(msg, query, DNS_SECTION_AUTHORITY);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater}
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater/*
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater * Create and queue a new lookup using the next origin from the origin
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater * list, read in setup_system().
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater */
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updaterstatic isc_boolean_t
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updaternext_origin(dns_message_t *msg, dig_query_t *query) {
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater dig_lookup_t *lookup;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater UNUSED(msg);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater INSIST(!free_now);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater debug("next_origin()");
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater debug("following up %s", query->lookup->textname);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater if (!usesearch)
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater /*
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater * We're not using a search list, so don't even think
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * about finding the next entry.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson */
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews return (ISC_FALSE);
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews if (query->lookup->origin == NULL)
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater /*
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater * Then we just did rootorg; there's nothing left.
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews */
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater return (ISC_FALSE);
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence cancel_lookup(query->lookup);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater lookup = requeue_lookup(query->lookup, ISC_TRUE);
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews lookup->defname = ISC_FALSE;
9a7d202077fae00fbdca610d8a8d90689e30f331Mark Andrews lookup->origin = ISC_LIST_NEXT(query->lookup->origin, link);
9a7d202077fae00fbdca610d8a8d90689e30f331Mark Andrews return (ISC_TRUE);
9a7d202077fae00fbdca610d8a8d90689e30f331Mark Andrews}
9a7d202077fae00fbdca610d8a8d90689e30f331Mark Andrews
9a7d202077fae00fbdca610d8a8d90689e30f331Mark Andrews/*
ce3c351d815569455892c30dc78d8c2e85ec1076Andreas Gustafsson * Insert an SOA record into the sendmessage in a lookup. Used for
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 * creating IXFR queries.
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 */
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉static void
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉insert_soa(dig_lookup_t *lookup) {
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater isc_result_t result;
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews dns_rdata_soa_t soa;
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews dns_rdata_t *rdata = NULL;
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews dns_rdatalist_t *rdatalist = NULL;
2a184ff86544cc67c36e2ce6bb3ddb5ac44684b8Evan Hunt dns_rdataset_t *rdataset = NULL;
2a184ff86544cc67c36e2ce6bb3ddb5ac44684b8Evan Hunt dns_name_t *soaname = NULL;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater debug("insert_soa()");
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater soa.mctx = mctx;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater soa.serial = lookup->ixfr_serial;
584848087f7463c1f659ce4712dc047d8e7f2b07Francis Dupont soa.refresh = 1;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater soa.retry = 1;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater soa.expire = 1;
f3350b671881f175d03e16fa5d0f685a1691bcabMark Andrews soa.minimum = 1;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater soa.common.rdclass = lookup->rdclass;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater soa.common.rdtype = dns_rdatatype_soa;
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉 dns_name_init(&soa.origin, NULL);
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉 dns_name_init(&soa.mname, NULL);
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 dns_name_clone(lookup->name, &soa.origin);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 dns_name_clone(lookup->name, &soa.mname);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater isc_buffer_init(&lookup->rdatabuf, lookup->rdatastore,
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater sizeof(lookup->rdatastore));
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater result = dns_message_gettemprdata(lookup->sendmsg, &rdata);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater check_result(result, "dns_message_gettemprdata");
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater result = dns_rdata_fromstruct(rdata, lookup->rdclass,
f3350b671881f175d03e16fa5d0f685a1691bcabMark Andrews dns_rdatatype_soa, &soa,
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater &lookup->rdatabuf);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater check_result(result, "isc_rdata_fromstruct");
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater result = dns_message_gettemprdatalist(lookup->sendmsg, &rdatalist);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater check_result(result, "dns_message_gettemprdatalist");
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater result = dns_message_gettemprdataset(lookup->sendmsg, &rdataset);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater check_result(result, "dns_message_gettemprdataset");
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater dns_rdatalist_init(rdatalist);
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews rdatalist->type = dns_rdatatype_soa;
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews rdatalist->rdclass = lookup->rdclass;
2a184ff86544cc67c36e2ce6bb3ddb5ac44684b8Evan Hunt rdatalist->covers = dns_rdatatype_soa;
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉 rdatalist->ttl = 1;
2a184ff86544cc67c36e2ce6bb3ddb5ac44684b8Evan Hunt ISC_LIST_INIT(rdatalist->rdata);
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉 ISC_LIST_APPEND(rdatalist->rdata, rdata, link);
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉 dns_rdataset_init(rdataset);
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉 dns_rdatalist_tordataset(rdatalist, rdataset);
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉 result = dns_message_gettempname(lookup->sendmsg, &soaname);
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉 check_result(result, "dns_message_gettempname");
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉 dns_name_init(soaname, NULL);
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉 dns_name_clone(lookup->name, soaname);
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉 ISC_LIST_INIT(soaname->list);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson ISC_LIST_APPEND(soaname->list, rdataset, link);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson dns_message_addname(lookup->sendmsg, soaname, DNS_SECTION_AUTHORITY);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson}
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson/*
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater * Setup the supplied lookup structure, making it ready to start sending
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater * queries to servers. Create and initialize the message to be sent as
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff * well as the query structures and buffer space for the replies. If the
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater * server list is empty, clone it from the system default list.
e44487bfc23599b6b240e09d83d1c862fecfcc82Michael Graff */
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updatervoid
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updatersetup_lookup(dig_lookup_t *lookup) {
e44487bfc23599b6b240e09d83d1c862fecfcc82Michael Graff isc_result_t result;
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 int len;
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 dig_server_t *serv;
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 dig_query_t *query;
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 isc_region_t r;
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 isc_buffer_t b;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater char store[MXNAME];
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater REQUIRE(lookup != NULL);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater INSIST(!free_now);
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater debug("setup_lookup(%p)", lookup);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence result = dns_message_create(mctx, DNS_MESSAGE_INTENTRENDER,
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater &lookup->sendmsg);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater check_result(result, "dns_message_create");
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater if (lookup->new_search) {
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater debug("resetting lookup counter.");
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater lookup_counter = 0;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater }
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater if (ISC_LIST_EMPTY(lookup->my_server_list)) {
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater debug("cloning server list");
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson clone_server_list(server_list, &lookup->my_server_list);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater }
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence result = dns_message_gettempname(lookup->sendmsg, &lookup->name);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater check_result(result, "dns_message_gettempname");
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater dns_name_init(lookup->name, NULL);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington isc_buffer_init(&lookup->namebuf, lookup->namespace,
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington sizeof(lookup->namespace));
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington isc_buffer_init(&lookup->onamebuf, lookup->onamespace,
307adf67922dd222bacf84ef0f074a0eb84d3a0aMark Andrews sizeof(lookup->onamespace));
307adf67922dd222bacf84ef0f074a0eb84d3a0aMark Andrews
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater /*
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater * If the name has too many dots, force the origin to be NULL
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater * (which produces an absolute lookup). Otherwise, take the origin
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater * we have if there's one in the struct already. If it's NULL,
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater * take the first entry in the searchlist iff either usesearch
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater * is TRUE or we got a domain line in the resolv.conf file.
307adf67922dd222bacf84ef0f074a0eb84d3a0aMark Andrews */
307adf67922dd222bacf84ef0f074a0eb84d3a0aMark Andrews /* XXX New search here? */
307adf67922dd222bacf84ef0f074a0eb84d3a0aMark Andrews if ((count_dots(lookup->textname) >= ndots) || lookup->defname)
307adf67922dd222bacf84ef0f074a0eb84d3a0aMark Andrews lookup->origin = NULL; /* Force abs lookup */
307adf67922dd222bacf84ef0f074a0eb84d3a0aMark Andrews else if (lookup->origin == NULL && lookup->new_search &&
307adf67922dd222bacf84ef0f074a0eb84d3a0aMark Andrews (usesearch || have_domain))
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater lookup->origin = ISC_LIST_HEAD(search_list);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater if (lookup->origin != NULL) {
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater debug("trying origin %s", lookup->origin->origin);
307adf67922dd222bacf84ef0f074a0eb84d3a0aMark Andrews result = dns_message_gettempname(lookup->sendmsg,
307adf67922dd222bacf84ef0f074a0eb84d3a0aMark Andrews &lookup->oname);
307adf67922dd222bacf84ef0f074a0eb84d3a0aMark Andrews check_result(result, "dns_message_gettempname");
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 dns_name_init(lookup->oname, NULL);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 /* XXX Helper funct to conv char* to name? */
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 len = strlen(lookup->origin->origin);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 isc_buffer_init(&b, lookup->origin->origin, len);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater isc_buffer_add(&b, len);
307adf67922dd222bacf84ef0f074a0eb84d3a0aMark Andrews result = dns_name_fromtext(lookup->oname, &b, dns_rootname,
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater ISC_FALSE, &lookup->onamebuf);
d81b6b94c41c682041e2a7b6deba002cbbfda278Evan Hunt if (result != ISC_R_SUCCESS) {
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater dns_message_puttempname(lookup->sendmsg,
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater &lookup->name);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater dns_message_puttempname(lookup->sendmsg,
307adf67922dd222bacf84ef0f074a0eb84d3a0aMark Andrews &lookup->oname);
307adf67922dd222bacf84ef0f074a0eb84d3a0aMark Andrews fatal("'%s' is not in legal name syntax (%s)",
307adf67922dd222bacf84ef0f074a0eb84d3a0aMark Andrews lookup->origin->origin,
307adf67922dd222bacf84ef0f074a0eb84d3a0aMark Andrews dns_result_totext(result));
307adf67922dd222bacf84ef0f074a0eb84d3a0aMark Andrews }
307adf67922dd222bacf84ef0f074a0eb84d3a0aMark Andrews if (lookup->trace_root) {
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater dns_name_clone(dns_rootname, lookup->name);
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington } else {
6c8abf481df85a67c3f32f5f107b554d3ff5a3edMark Andrews len = strlen(lookup->textname);
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt isc_buffer_init(&b, lookup->textname, len);
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt isc_buffer_add(&b, len);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater result = dns_name_fromtext(lookup->name, &b,
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater lookup->oname, ISC_FALSE,
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater &lookup->namebuf);
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt }
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater if (result != ISC_R_SUCCESS) {
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt dns_message_puttempname(lookup->sendmsg,
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater &lookup->name);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater dns_message_puttempname(lookup->sendmsg,
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater &lookup->oname);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater fatal("'%s' is not in legal name syntax (%s)",
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater lookup->textname, dns_result_totext(result));
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater }
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater dns_message_puttempname(lookup->sendmsg, &lookup->oname);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater } else {
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater debug("using root origin");
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt if (!lookup->trace_root) {
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater len = strlen(lookup->textname);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater isc_buffer_init(&b, lookup->textname, len);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater isc_buffer_add(&b, len);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater result = dns_name_fromtext(lookup->name, &b,
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater dns_rootname,
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt ISC_FALSE,
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater &lookup->namebuf);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater } else {
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater dns_name_clone(dns_rootname, lookup->name);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater }
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt if (result != ISC_R_SUCCESS) {
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt dns_message_puttempname(lookup->sendmsg,
6c8abf481df85a67c3f32f5f107b554d3ff5a3edMark Andrews &lookup->name);
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt isc_buffer_init(&b, store, MXNAME);
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt fatal("'%s' is not a legal name syntax "
e1cf6fd20a9f56c6c31581480445dea96fc7ac60Mark Andrews "(%s)", lookup->textname,
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt dns_result_totext(result));
dd185fb371632b9cf1b7be3097960185c9645a09Mark Andrews }
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt }
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt isc_buffer_init(&b, store, sizeof(store));
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt /* XXX Move some of this into function, dns_name_format. */
dd185fb371632b9cf1b7be3097960185c9645a09Mark Andrews dns_name_totext(lookup->name, ISC_FALSE, &b);
dd185fb371632b9cf1b7be3097960185c9645a09Mark Andrews isc_buffer_usedregion(&b, &r);
dd185fb371632b9cf1b7be3097960185c9645a09Mark Andrews trying((int)r.length, (char *)r.base, lookup);
dd185fb371632b9cf1b7be3097960185c9645a09Mark Andrews INSIST(dns_name_isabsolute(lookup->name));
dd185fb371632b9cf1b7be3097960185c9645a09Mark Andrews
dd185fb371632b9cf1b7be3097960185c9645a09Mark Andrews lookup->sendmsg->id = (unsigned short)(random() & 0xFFFF);
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt lookup->sendmsg->opcode = dns_opcode_query;
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt lookup->msgcounter = 0;
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt /*
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt * If this is a trace request, completely disallow recursion, since
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt * it's meaningless for traces.
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt */
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt if (lookup->recurse && !lookup->trace && !lookup->ns_search_only) {
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt debug("recursive query");
6214c3c93a43dc86f080dc0219e4560b69721f53Mark Andrews lookup->sendmsg->flags |= DNS_MESSAGEFLAG_RD;
6214c3c93a43dc86f080dc0219e4560b69721f53Mark Andrews }
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt /* XXX aaflag */
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt if (lookup->aaonly) {
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt debug("AA query");
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt lookup->sendmsg->flags |= DNS_MESSAGEFLAG_AA;
e1cf6fd20a9f56c6c31581480445dea96fc7ac60Mark Andrews }
e1cf6fd20a9f56c6c31581480445dea96fc7ac60Mark Andrews
e1cf6fd20a9f56c6c31581480445dea96fc7ac60Mark Andrews if (lookup->adflag) {
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt debug("AD query");
e1cf6fd20a9f56c6c31581480445dea96fc7ac60Mark Andrews lookup->sendmsg->flags |= DNS_MESSAGEFLAG_AD;
e1cf6fd20a9f56c6c31581480445dea96fc7ac60Mark Andrews }
e1cf6fd20a9f56c6c31581480445dea96fc7ac60Mark Andrews
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt if (lookup->cdflag) {
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt debug("CD query");
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt lookup->sendmsg->flags |= DNS_MESSAGEFLAG_CD;
e1cf6fd20a9f56c6c31581480445dea96fc7ac60Mark Andrews }
e1cf6fd20a9f56c6c31581480445dea96fc7ac60Mark Andrews
e1cf6fd20a9f56c6c31581480445dea96fc7ac60Mark Andrews dns_message_addname(lookup->sendmsg, lookup->name,
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt DNS_SECTION_QUESTION);
e1cf6fd20a9f56c6c31581480445dea96fc7ac60Mark Andrews
e1cf6fd20a9f56c6c31581480445dea96fc7ac60Mark Andrews if (lookup->trace_root)
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt lookup->rdtype = dns_rdatatype_soa;
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt if ((lookup->rdtype == dns_rdatatype_axfr) ||
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt (lookup->rdtype == dns_rdatatype_ixfr)) {
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt lookup->doing_xfr = ISC_TRUE;
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt /*
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt * Force TCP mode if we're doing an xfr.
e1cf6fd20a9f56c6c31581480445dea96fc7ac60Mark Andrews * XXX UDP ixfr's would be useful
e1cf6fd20a9f56c6c31581480445dea96fc7ac60Mark Andrews */
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt lookup->tcp_mode = ISC_TRUE;
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt }
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt add_question(lookup->sendmsg, lookup->name, lookup->rdclass,
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt lookup->rdtype);
dd185fb371632b9cf1b7be3097960185c9645a09Mark Andrews
dd185fb371632b9cf1b7be3097960185c9645a09Mark Andrews /* XXX add_soa */
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt if (lookup->rdtype == dns_rdatatype_ixfr)
e1cf6fd20a9f56c6c31581480445dea96fc7ac60Mark Andrews insert_soa(lookup);
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt /* XXX Insist this? */
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt lookup->tsigctx = NULL;
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt lookup->querysig = NULL;
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt if (key != NULL) {
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt debug("initializing keys");
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt result = dns_message_settsigkey(lookup->sendmsg, key);
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt check_result(result, "dns_message_settsigkey");
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt }
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt lookup->sendspace = isc_mempool_get(commctx);
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt if (lookup->sendspace == NULL)
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt fatal("memory allocation failure");
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt debug("starting to render the message");
6214c3c93a43dc86f080dc0219e4560b69721f53Mark Andrews isc_buffer_init(&lookup->sendbuf, lookup->sendspace, COMMSIZE);
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt result = dns_message_renderbegin(lookup->sendmsg, &lookup->sendbuf);
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt check_result(result, "dns_message_renderbegin");
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt if (lookup->udpsize > 0)
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt add_opt(lookup->sendmsg, lookup->udpsize);
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt result = dns_message_rendersection(lookup->sendmsg,
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt DNS_SECTION_QUESTION, 0);
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt check_result(result, "dns_message_rendersection");
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt result = dns_message_rendersection(lookup->sendmsg,
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt DNS_SECTION_AUTHORITY, 0);
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt check_result(result, "dns_message_rendersection");
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt result = dns_message_renderend(lookup->sendmsg);
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt check_result(result, "dns_message_renderend");
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt debug("done rendering");
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt lookup->pending = ISC_FALSE;
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt for (serv = ISC_LIST_HEAD(lookup->my_server_list);
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt serv != NULL;
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt serv = ISC_LIST_NEXT(serv, link)) {
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt query = isc_mem_allocate(mctx, sizeof(dig_query_t));
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt if (query == NULL)
6c8abf481df85a67c3f32f5f107b554d3ff5a3edMark Andrews fatal("Memory allocation failure in %s:%d",
6c8abf481df85a67c3f32f5f107b554d3ff5a3edMark Andrews __FILE__, __LINE__);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater debug("create query %p linked to lookup %p",
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater query, lookup);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff query->lookup = lookup;
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt query->waiting_connect = ISC_FALSE;
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt query->first_pass = ISC_TRUE;
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt query->first_soa_rcvd = ISC_FALSE;
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt query->second_rr_rcvd = ISC_FALSE;
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt query->second_rr_serial = 0;
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt query->servname = serv->servername;
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt query->name_count = 0;
d640b4a0ab9dec252749793f78a1ed1e8551ea19Evan Hunt query->rr_count = 0;
d640b4a0ab9dec252749793f78a1ed1e8551ea19Evan Hunt ISC_LIST_INIT(query->recvlist);
d640b4a0ab9dec252749793f78a1ed1e8551ea19Evan Hunt ISC_LIST_INIT(query->lengthlist);
d640b4a0ab9dec252749793f78a1ed1e8551ea19Evan Hunt query->sock = NULL;
d640b4a0ab9dec252749793f78a1ed1e8551ea19Evan Hunt query->recvspace = isc_mempool_get(commctx);
d640b4a0ab9dec252749793f78a1ed1e8551ea19Evan Hunt if (query->recvspace == NULL)
d640b4a0ab9dec252749793f78a1ed1e8551ea19Evan Hunt fatal("memory allocation failure");
d640b4a0ab9dec252749793f78a1ed1e8551ea19Evan Hunt
d640b4a0ab9dec252749793f78a1ed1e8551ea19Evan Hunt isc_buffer_init(&query->recvbuf, query->recvspace, COMMSIZE);
d640b4a0ab9dec252749793f78a1ed1e8551ea19Evan Hunt isc_buffer_init(&query->lengthbuf, query->lengthspace, 2);
d640b4a0ab9dec252749793f78a1ed1e8551ea19Evan Hunt isc_buffer_init(&query->slbuf, query->slspace, 2);
d640b4a0ab9dec252749793f78a1ed1e8551ea19Evan Hunt
d640b4a0ab9dec252749793f78a1ed1e8551ea19Evan Hunt ISC_LIST_ENQUEUE(lookup->q, query, link);
d640b4a0ab9dec252749793f78a1ed1e8551ea19Evan Hunt }
d640b4a0ab9dec252749793f78a1ed1e8551ea19Evan Hunt /* XXX qrflag, print_query, etc... */
d640b4a0ab9dec252749793f78a1ed1e8551ea19Evan Hunt if (!ISC_LIST_EMPTY(lookup->q) && qr) {
d640b4a0ab9dec252749793f78a1ed1e8551ea19Evan Hunt printmessage(ISC_LIST_HEAD(lookup->q), lookup->sendmsg,
d640b4a0ab9dec252749793f78a1ed1e8551ea19Evan Hunt ISC_TRUE);
d640b4a0ab9dec252749793f78a1ed1e8551ea19Evan Hunt }
d640b4a0ab9dec252749793f78a1ed1e8551ea19Evan Hunt}
d640b4a0ab9dec252749793f78a1ed1e8551ea19Evan Hunt
d640b4a0ab9dec252749793f78a1ed1e8551ea19Evan Hunt/*
d640b4a0ab9dec252749793f78a1ed1e8551ea19Evan Hunt * Event handler for send completion. Track send counter, and clear out
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt * the query if the send was canceled.
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt */
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Huntstatic void
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Huntsend_done(isc_task_t *_task, isc_event_t *event) {
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt REQUIRE(event->ev_type == ISC_SOCKEVENT_SENDDONE);
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt UNUSED(_task);
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt LOCK_LOOKUP;
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt isc_event_free(&event);
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt debug("send_done()");
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt sendcount--;
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt debug("sendcount=%d", sendcount);
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt INSIST(sendcount >= 0);
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt UNLOCK_LOOKUP;
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt}
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt/*
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt * Cancel a lookup, sending isc_socket_cancel() requests to all outstanding
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt * IO sockets. The cancel handlers should take care of cleaning up the
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt * query and lookup structures
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt */
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Huntvoid
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Huntcancel_lookup(dig_lookup_t *lookup) {
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt dig_query_t *query = NULL;
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt debug("cancel_lookup()");
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt for (query = ISC_LIST_HEAD(lookup->q);
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt query != NULL;
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt query = ISC_LIST_NEXT(query, link)) {
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt if (query->sock != NULL) {
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt isc_socket_cancel(query->sock, global_task,
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt ISC_SOCKCANCEL_ALL);
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt check_if_done();
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt }
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt }
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt lookup->pending = ISC_FALSE;
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt lookup->retries = 0;
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt}
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt/*
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt * Send a UDP packet to the remote nameserver, possible starting the
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt * recv action as well. Also make sure that the timer is running and
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt * is properly reset.
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt */
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Huntstatic void
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Huntsend_udp(dig_lookup_t *lookup, isc_boolean_t make_recv) {
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt dig_query_t *query;
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt isc_result_t result;
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt unsigned int local_timeout;
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt debug("send_udp()");
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt /*
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt * If the timer already exists, that means we're calling this
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt * a second time (for a retry). Don't need to recreate it,
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt * just reset it.
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt */
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt if (lookup->timer == NULL) {
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt if (timeout != INT_MAX) {
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt if (timeout == 0) {
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt local_timeout = UDP_TIMEOUT;
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt } else
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt local_timeout = timeout;
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt debug("have local timeout of %d", local_timeout);
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt isc_interval_set(&lookup->interval, local_timeout, 0);
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt result = isc_timer_create(timermgr,
5d79b60fc5e4dad4f04da39570517d20a2425f8bMukund Sivaraman isc_timertype_once, NULL,
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt &lookup->interval,
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt global_task,
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt connect_timeout, lookup,
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt &lookup->timer);
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt check_result(result, "isc_timer_create");
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt }
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt } else {
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt result = isc_timer_reset(lookup->timer, isc_timertype_once,
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt NULL, &lookup->interval,
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt ISC_TRUE);
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt check_result(result, "isc_timer_reset");
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt }
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt for (query = ISC_LIST_HEAD(lookup->q);
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt query != NULL;
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt query = ISC_LIST_NEXT(query, link)) {
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt debug("working on lookup %p, query %p",
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt query->lookup, query);
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt if (make_recv) {
bbf31e6b623af575138669bdd7031232867b9166Mark Andrews ISC_LIST_ENQUEUE(query->recvlist, &query->recvbuf,
bbf31e6b623af575138669bdd7031232867b9166Mark Andrews link);
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt debug("recving with lookup=%p, query=%p, sock=%p",
bbf31e6b623af575138669bdd7031232867b9166Mark Andrews query->lookup, query,
bbf31e6b623af575138669bdd7031232867b9166Mark Andrews query->sock);
b748b5e2c2fa40f758e3b35ad4f1fde99ab5e11fEvan Hunt result = isc_socket_recvv(query->sock,
b748b5e2c2fa40f758e3b35ad4f1fde99ab5e11fEvan Hunt &query->recvlist, 1,
b748b5e2c2fa40f758e3b35ad4f1fde99ab5e11fEvan Hunt global_task, recv_done,
bbf31e6b623af575138669bdd7031232867b9166Mark Andrews query);
b748b5e2c2fa40f758e3b35ad4f1fde99ab5e11fEvan Hunt check_result(result, "isc_socket_recvv");
b748b5e2c2fa40f758e3b35ad4f1fde99ab5e11fEvan Hunt recvcount++;
b748b5e2c2fa40f758e3b35ad4f1fde99ab5e11fEvan Hunt debug("recvcount=%d", recvcount);
b748b5e2c2fa40f758e3b35ad4f1fde99ab5e11fEvan Hunt }
bbf31e6b623af575138669bdd7031232867b9166Mark Andrews ISC_LIST_INIT(query->sendlist);
bbf31e6b623af575138669bdd7031232867b9166Mark Andrews ISC_LIST_ENQUEUE(query->sendlist, &lookup->sendbuf,
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt link);
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt debug("sending a request");
bbf31e6b623af575138669bdd7031232867b9166Mark Andrews result = isc_time_now(&query->time_sent);
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt check_result(result, "isc_time_now");
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt INSIST(query->sock != NULL);
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt result = isc_socket_sendtov(query->sock, &query->sendlist,
bbf31e6b623af575138669bdd7031232867b9166Mark Andrews global_task, send_done, query,
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt &query->sockaddr, NULL);
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt check_result(result, "isc_socket_sendtov");
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt sendcount++;
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt }
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt}
bbf31e6b623af575138669bdd7031232867b9166Mark Andrews
bbf31e6b623af575138669bdd7031232867b9166Mark Andrews/*
bbf31e6b623af575138669bdd7031232867b9166Mark Andrews * IO timeout handler, used for both connect and recv timeouts. If
bbf31e6b623af575138669bdd7031232867b9166Mark Andrews * retries are still allowed, either resend the UDP packet or queue a
bbf31e6b623af575138669bdd7031232867b9166Mark Andrews * new TCP lookup. Otherwise, cancel the lookup.
bbf31e6b623af575138669bdd7031232867b9166Mark Andrews */
bbf31e6b623af575138669bdd7031232867b9166Mark Andrewsstatic void
bbf31e6b623af575138669bdd7031232867b9166Mark Andrewsconnect_timeout(isc_task_t *task, isc_event_t *event) {
bbf31e6b623af575138669bdd7031232867b9166Mark Andrews dig_lookup_t *lookup=NULL;
bbf31e6b623af575138669bdd7031232867b9166Mark Andrews
bbf31e6b623af575138669bdd7031232867b9166Mark Andrews UNUSED(task);
bbf31e6b623af575138669bdd7031232867b9166Mark Andrews REQUIRE(event->ev_type == ISC_TIMEREVENT_IDLE);
bbf31e6b623af575138669bdd7031232867b9166Mark Andrews
bbf31e6b623af575138669bdd7031232867b9166Mark Andrews debug("connect_timeout()");
bbf31e6b623af575138669bdd7031232867b9166Mark Andrews
bbf31e6b623af575138669bdd7031232867b9166Mark Andrews LOCK_LOOKUP;
bbf31e6b623af575138669bdd7031232867b9166Mark Andrews lookup = event->ev_arg;
bbf31e6b623af575138669bdd7031232867b9166Mark Andrews isc_event_free(&event);
bbf31e6b623af575138669bdd7031232867b9166Mark Andrews
bbf31e6b623af575138669bdd7031232867b9166Mark Andrews INSIST(!free_now);
bbf31e6b623af575138669bdd7031232867b9166Mark Andrews if (lookup->retries > 1) {
bbf31e6b623af575138669bdd7031232867b9166Mark Andrews if (!lookup->tcp_mode) {
bbf31e6b623af575138669bdd7031232867b9166Mark Andrews lookup->retries--;
bbf31e6b623af575138669bdd7031232867b9166Mark Andrews debug("resending UDP request");
bbf31e6b623af575138669bdd7031232867b9166Mark Andrews send_udp(lookup, ISC_FALSE);
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt } else {
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt debug("making new TCP request");
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt cancel_lookup(lookup);
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt lookup->retries--;
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt requeue_lookup(lookup, ISC_TRUE);
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt }
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt }
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt else {
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt printf(";; connection timed out; no servers could be "
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt "reached\n");
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt cancel_lookup(lookup);
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt }
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt UNLOCK_LOOKUP;
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt}
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt/*
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt * Event handler for the TCP recv which gets the length header of TCP
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt * packets. Start the next recv of length bytes.
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt */
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Huntstatic void
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunttcp_length_done(isc_task_t *task, isc_event_t *event) {
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt isc_socketevent_t *sevent;
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt isc_buffer_t *b=NULL;
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt isc_region_t r;
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt isc_result_t result;
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt dig_query_t *query=NULL;
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt dig_lookup_t *l;
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt isc_uint16_t length;
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt REQUIRE(event->ev_type == ISC_SOCKEVENT_RECVDONE);
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt INSIST(!free_now);
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt UNUSED(task);
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt debug("tcp_length_done()");
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt LOCK_LOOKUP;
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt sevent = (isc_socketevent_t *)event;
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt query = event->ev_arg;
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt recvcount--;
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt INSIST(recvcount >= 0);
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt if (sevent->result == ISC_R_CANCELED) {
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt isc_event_free(&event);
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt l = query->lookup;
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt clear_query(query);
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt check_next_lookup(l);
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt UNLOCK_LOOKUP;
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt return;
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt }
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt if (sevent->result != ISC_R_SUCCESS) {
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt result = isc_buffer_allocate(mctx, &b, 256);
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt check_result(result, "isc_buffer_allocate");
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt result = isc_sockaddr_totext(&query->sockaddr, b);
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt check_result(result, "isc_sockaddr_totext");
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt isc_buffer_usedregion(b, &r);
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt printf(";; communications error to %.*s: %s\n",
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt (int)r.length, r.base,
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt isc_result_totext(sevent->result));
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt isc_buffer_free(&b);
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt l = query->lookup;
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt isc_socket_detach(&query->sock);
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt sockcount--;
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt debug("sockcount=%d",sockcount);
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt INSIST(sockcount >= 0);
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt isc_event_free(&event);
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt clear_query(query);
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt check_next_lookup(l);
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt UNLOCK_LOOKUP;
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt return;
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt }
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt b = ISC_LIST_HEAD(sevent->bufferlist);
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt ISC_LIST_DEQUEUE(sevent->bufferlist, &query->lengthbuf, link);
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt length = isc_buffer_getuint16(b);
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt if (length > COMMSIZE) {
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt isc_event_free(&event);
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt fatal("Length of %X was longer than I can handle!",
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt length);
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt }
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt /*
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt * Even though the buffer was already init'ed, we need
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt * to redo it now, to force the length we want.
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt */
isc_buffer_invalidate(&query->recvbuf);
isc_buffer_init(&query->recvbuf, query->recvspace, length);
ENSURE(ISC_LIST_EMPTY(query->recvlist));
ISC_LIST_ENQUEUE(query->recvlist, &query->recvbuf, link);
debug("recving with lookup=%p, query=%p",
query->lookup, query);
result = isc_socket_recvv(query->sock, &query->recvlist, length, task,
recv_done, query);
check_result(result, "isc_socket_recvv");
recvcount++;
debug("resubmitted recv request with length %d, recvcount=%d",
length, recvcount);
isc_event_free(&event);
UNLOCK_LOOKUP;
}
/*
* For transfers that involve multiple recvs (XFR's in particular),
* launch the next recv.
*/
static void
launch_next_query(dig_query_t *query, isc_boolean_t include_question) {
isc_result_t result;
dig_lookup_t *l;
INSIST(!free_now);
debug("launch_next_query()");
if (!query->lookup->pending) {
debug("ignoring launch_next_query because !pending");
isc_socket_detach(&query->sock);
sockcount--;
debug("sockcount=%d", sockcount);
INSIST(sockcount >= 0);
query->waiting_connect = ISC_FALSE;
l = query->lookup;
clear_query(query);
check_next_lookup(l);
return;
}
isc_buffer_clear(&query->slbuf);
isc_buffer_clear(&query->lengthbuf);
isc_buffer_putuint16(&query->slbuf, query->lookup->sendbuf.used);
ISC_LIST_INIT(query->sendlist);
ISC_LIST_ENQUEUE(query->sendlist, &query->slbuf, link);
if (include_question) {
ISC_LIST_ENQUEUE(query->sendlist, &query->lookup->sendbuf,
link);
}
ISC_LIST_ENQUEUE(query->lengthlist, &query->lengthbuf, link);
result = isc_socket_recvv(query->sock, &query->lengthlist, 0,
global_task, tcp_length_done, query);
check_result(result, "isc_socket_recvv");
recvcount++;
debug("recvcount=%d",recvcount);
if (!query->first_soa_rcvd) {
debug("sending a request in launch_next_query");
result = isc_time_now(&query->time_sent);
check_result(result, "isc_time_now");
result = isc_socket_sendv(query->sock, &query->sendlist,
global_task, send_done, query);
check_result(result, "isc_socket_sendv");
sendcount++;
debug("sendcount=%d", sendcount);
}
query->waiting_connect = ISC_FALSE;
#if 0
check_next_lookup(query->lookup);
#endif
return;
}
/*
* Event handler for TCP connect complete. Make sure the connection was
* successful, then pass into launch_next_query to actually send the
* question.
*/
static void
connect_done(isc_task_t *task, isc_event_t *event) {
isc_result_t result;
isc_socketevent_t *sevent = NULL;
dig_query_t *query = NULL;
dig_lookup_t *l;
isc_buffer_t *b = NULL;
isc_region_t r;
UNUSED(task);
REQUIRE(event->ev_type == ISC_SOCKEVENT_CONNECT);
INSIST(!free_now);
debug("connect_done()");
LOCK_LOOKUP;
sevent = (isc_socketevent_t *)event;
query = sevent->ev_arg;
INSIST(query->waiting_connect);
query->waiting_connect = ISC_FALSE;
if (sevent->result != ISC_R_SUCCESS) {
debug("unsuccessful connection: %s",
isc_result_totext(sevent->result));
result = isc_buffer_allocate(mctx, &b, 256);
check_result(result, "isc_buffer_allocate");
result = isc_sockaddr_totext(&query->sockaddr, b);
check_result(result, "isc_sockaddr_totext");
isc_buffer_usedregion(b, &r);
/* XXX isc_sockaddr_format */
if (sevent->result != ISC_R_CANCELED)
printf(";; Connection to %.*s(%s) for %s failed: "
"%s.\n", (int)r.length, r.base,
query->servname, query->lookup->textname,
isc_result_totext(sevent->result));
isc_socket_detach(&query->sock);
sockcount--;
INSIST(sockcount >= 0);
/* XXX Clean up exitcodes */
if (exitcode < 9)
exitcode = 9;
debug("sockcount=%d", sockcount);
isc_buffer_free(&b);
query->waiting_connect = ISC_FALSE;
isc_event_free(&event);
l = query->lookup;
clear_query(query);
check_next_lookup(l);
UNLOCK_LOOKUP;
return;
}
launch_next_query(query, ISC_TRUE);
isc_event_free(&event);
UNLOCK_LOOKUP;
}
/*
* Check if the ongoing XFR needs more data before it's complete, using
* the semantics of IXFR and AXFR protocols. Much of the complexity of
* this routine comes from determining when an IXFR is complete.
* ISC_FALSE means more data is on the way, and the recv has been issued.
*/
static isc_boolean_t
check_for_more_data(dig_query_t *query, dns_message_t *msg,
isc_socketevent_t *sevent)
{
dns_rdataset_t *rdataset = NULL;
dns_rdata_t rdata;
dns_rdata_soa_t soa;
isc_result_t result;
isc_buffer_t b;
isc_region_t r;
char abspace[MXNAME];
isc_boolean_t atlimit=ISC_FALSE;
debug("check_for_more_data()");
/*
* By the time we're in this routine, we know we're doing
* either an AXFR or IXFR. If there's no second_rr_type,
* then we don't yet know which kind of answer we got back
* from the server. Here, we're going to walk through the
* rr's in the message, acting as necessary whenever we hit
* an SOA rr.
*/
result = dns_message_firstname(msg, DNS_SECTION_ANSWER);
if (result != ISC_R_SUCCESS) {
puts("; Transfer failed.");
return (ISC_TRUE);
}
do {
dns_name_t *name;
name = NULL;
dns_message_currentname(msg, DNS_SECTION_ANSWER,
&name);
for (rdataset = ISC_LIST_HEAD(name->list);
rdataset != NULL;
rdataset = ISC_LIST_NEXT(rdataset, link)) {
result = dns_rdataset_first(rdataset);
if (result != ISC_R_SUCCESS)
continue;
do {
query->rr_count++;
if (query->rr_count >= rr_limit)
atlimit = ISC_TRUE;
dns_rdataset_current(rdataset, &rdata);
/*
* If this is the first rr, make sure
* it's an SOA
*/
if ((!query->first_soa_rcvd) &&
(rdata.type != dns_rdatatype_soa)) {
puts("; Transfer failed. "
"Didn't start with "
"SOA answer.");
return (ISC_TRUE);
}
if ((!query->second_rr_rcvd) &&
(rdata.type != dns_rdatatype_soa)) {
query->second_rr_rcvd = ISC_TRUE;
query->second_rr_serial = 0;
debug("got the second rr as nonsoa");
continue;
}
/*
* If the record is anything except an SOA
* now, just continue on...
*/
if (rdata.type != dns_rdatatype_soa)
goto next_rdata;
/* Now we have an SOA. Work with it. */
debug("got an SOA");
result = dns_rdata_tostruct(&rdata,
&soa,
mctx);
check_result(result,
"dns_rdata_tostruct");
if (!query->first_soa_rcvd) {
query->first_soa_rcvd =
ISC_TRUE;
query->first_rr_serial =
soa.serial;
debug("this is the first %d",
query->lookup->ixfr_serial);
if (query->lookup->ixfr_serial >=
soa.serial) {
dns_rdata_freestruct(&soa);
goto doexit;
}
dns_rdata_freestruct(&soa);
goto next_rdata;
}
if (query->lookup->rdtype ==
dns_rdatatype_axfr) {
debug("doing axfr, got second SOA");
dns_rdata_freestruct(&soa);
goto doexit;
}
if (!query->second_rr_rcvd) {
if (soa.serial ==
query->first_rr_serial) {
debug("doing ixfr, got "
"empty zone");
dns_rdata_freestruct(&soa);
goto doexit;
}
debug("this is the second %d",
query->lookup->ixfr_serial);
query->second_rr_rcvd = ISC_TRUE;
query->second_rr_serial =
soa.serial;
dns_rdata_freestruct(&soa);
goto next_rdata;
}
if (query->second_rr_serial == 0) {
/*
* If the second RR was a non-SOA
* record, and we're getting any
* other SOA, then this is an
* AXFR, and we're done.
*/
debug("done, since axfr");
dns_rdata_freestruct(&soa);
goto doexit;
}
/*
* If we get to this point, we're doing an
* IXFR and have to start really looking
* at serial numbers.
*/
if (query->first_rr_serial == soa.serial) {
debug("got a match for ixfr");
if (!query->first_repeat_rcvd) {
query->first_repeat_rcvd =
ISC_TRUE;
dns_rdata_freestruct(&soa);
goto next_rdata;
}
debug("done with ixfr");
dns_rdata_freestruct(&soa);
goto doexit;
}
debug("meaningless soa %d",
soa.serial);
dns_rdata_freestruct(&soa);
next_rdata:
result = dns_rdataset_next(rdataset);
} while (result == ISC_R_SUCCESS);
}
query->name_count++;
if (query->name_count >= name_limit) {
debug("name_count(%d) > name_limit(%d)",
query->name_count, name_limit);
atlimit = ISC_TRUE;
}
result = dns_message_nextname(msg, DNS_SECTION_ANSWER);
} while (result == ISC_R_SUCCESS);
if (atlimit) {
doexit:
isc_buffer_init(&b, abspace, MXNAME);
result = isc_sockaddr_totext(&sevent->address, &b);
check_result(result,
"isc_sockaddr_totext");
isc_buffer_usedregion(&b, &r);
received(b.used, r.length,
(char *)r.base, query);
if (atlimit)
if (exitcode < 7)
exitcode = 7;
return (ISC_TRUE);
}
launch_next_query(query, ISC_FALSE);
return (ISC_FALSE);
}
/*
* Event handler for recv complete. Perform whatever actions are necessary,
* based on the specifics of the user's request.
*/
static void
recv_done(isc_task_t *task, isc_event_t *event) {
isc_socketevent_t *sevent = NULL;
dig_query_t *query = NULL;
isc_buffer_t *b = NULL;
dns_message_t *msg = NULL;
isc_result_t result;
isc_buffer_t ab;
char abspace[MXNAME];
isc_region_t r;
dig_lookup_t *n, *l;
isc_boolean_t docancel = ISC_FALSE;
unsigned int local_timeout;
UNUSED(task);
INSIST(!free_now);
debug("recv_done()");
LOCK_LOOKUP;
recvcount--;
debug("recvcount=%d", recvcount);
INSIST(recvcount >= 0);
query = event->ev_arg;
debug("lookup=%p, query=%p", query->lookup, query);
l = query->lookup;
REQUIRE(event->ev_type == ISC_SOCKEVENT_RECVDONE);
sevent = (isc_socketevent_t *)event;
if ((l->tcp_mode) && (l->timer != NULL))
isc_timer_touch(l->timer);
if ((!l->pending && !l->ns_search_only)
|| cancel_now) {
debug("no longer pending. Got %s",
isc_result_totext(sevent->result));
query->waiting_connect = ISC_FALSE;
isc_event_free(&event);
clear_query(query);
check_next_lookup(l);
UNLOCK_LOOKUP;
return;
}
if (sevent->result == ISC_R_SUCCESS) {
b = ISC_LIST_HEAD(sevent->bufferlist);
ISC_LIST_DEQUEUE(sevent->bufferlist, &query->recvbuf, link);
result = dns_message_create(mctx, DNS_MESSAGE_INTENTPARSE,
&msg);
check_result(result, "dns_message_create");
if (key != NULL) {
if (l->querysig == NULL) {
debug("getting initial querysig");
result = dns_message_getquerytsig(
l->sendmsg,
mctx, &l->querysig);
check_result(result,
"dns_message_getquerytsig");
}
result = dns_message_setquerytsig(msg,
l->querysig);
check_result(result, "dns_message_setquerytsig");
result = dns_message_settsigkey(msg, key);
check_result(result, "dns_message_settsigkey");
msg->tsigctx = l->tsigctx;
if (l->msgcounter != 0)
msg->tcp_continuation = 1;
l->msgcounter++;
}
debug("before parse starts");
result = dns_message_parse(msg, b, ISC_TRUE);
if (result != ISC_R_SUCCESS) {
printf(";; Got bad packet:\n");
hex_dump(b);
query->waiting_connect = ISC_FALSE;
if (!l->tcp_mode) {
printf(";; Retrying in TCP mode.\n");
n = requeue_lookup(l, ISC_TRUE);
n->tcp_mode = ISC_TRUE;
}
dns_message_destroy(&msg);
isc_event_free(&event);
clear_query(query);
cancel_lookup(l);
check_next_lookup(l);
UNLOCK_LOOKUP;
return;
}
if (key != NULL) {
result = dns_tsig_verify(&query->recvbuf, msg,
NULL, NULL);
if (result != ISC_R_SUCCESS) {
printf(";; Couldn't verify signature: %s\n",
dns_result_totext(result));
validated = ISC_FALSE;
}
l->tsigctx = msg->tsigctx;
if (l->querysig != NULL) {
debug("freeing querysig buffer %p",
l->querysig);
isc_buffer_free(&l->querysig);
}
result = dns_message_getquerytsig(msg, mctx,
&l->querysig);
check_result(result,"dns_message_getquerytsig");
debug("querysig 3 is %p", l->querysig);
}
debug("after parse");
if (l->xfr_q == NULL) {
l->xfr_q = query;
/*
* Once we are in the XFR message, increase
* the timeout to much longer, so brief network
* outages won't cause the XFR to abort
*/
if ((timeout != INT_MAX) &&
(l->timer != NULL) &&
l->doing_xfr ) {
if (timeout == 0) {
if (l->tcp_mode)
local_timeout = TCP_TIMEOUT;
else
local_timeout = UDP_TIMEOUT;
} else {
if (timeout < (INT_MAX / 4))
local_timeout = timeout * 4;
else
local_timeout = INT_MAX;
}
debug("have local timeout of %d",
local_timeout);
isc_interval_set(&l->interval,
local_timeout, 0);
result = isc_timer_reset(l->timer,
isc_timertype_once,
NULL,
&l->interval,
ISC_FALSE);
check_result(result, "isc_timer_reset");
}
}
if (l->xfr_q == query) {
if ((l->trace)||
(l->ns_search_only)) {
debug("in TRACE code");
if (show_details ||
(((dns_message_firstname(msg,
DNS_SECTION_ANSWER)
== ISC_R_SUCCESS)) &&
!l->trace_root))
printmessage(query, msg, ISC_TRUE);
if ((msg->rcode != 0) &&
(l->origin != NULL)) {
if (!next_origin(msg, query)) {
printmessage(query, msg,
ISC_TRUE);
isc_buffer_init(&ab, abspace,
MXNAME);
result = isc_sockaddr_totext(
&sevent->address,
&ab);
check_result(result,
"isc_sockaddr_totext");
isc_buffer_usedregion(&ab, &r);
received(b->used, r.length,
(char *)r.base,
query);
}
} else {
result = dns_message_firstname
(msg,DNS_SECTION_ANSWER);
if ((result != ISC_R_SUCCESS) ||
l->trace_root)
followup_lookup(msg, query,
DNS_SECTION_AUTHORITY);
}
} else if ((msg->rcode != 0) &&
(l->origin != NULL)) {
if (!next_origin(msg, query)) {
printmessage(query, msg,
ISC_TRUE);
isc_buffer_init(&ab, abspace, MXNAME);
result = isc_sockaddr_totext(
&sevent->address,
&ab);
check_result(result,
"isc_sockaddr_totext");
isc_buffer_usedregion(&ab, &r);
received(b->used, r.length,
(char *)r.base,
query);
}
if (show_details) {
printmessage(query, msg, ISC_TRUE);
}
} else {
if (query->first_soa_rcvd &&
l->doing_xfr)
printmessage(query, msg, ISC_FALSE);
else
printmessage(query, msg, ISC_TRUE);
}
} else if ((dns_message_firstname(msg, DNS_SECTION_ANSWER)
== ISC_R_SUCCESS) &&
l->ns_search_only &&
!l->trace_root ) {
printmessage(query, msg, ISC_TRUE);
}
if (l->pending)
debug("still pending.");
if (l->doing_xfr) {
if (query != l->xfr_q) {
dns_message_destroy(&msg);
isc_event_free (&event);
query->waiting_connect = ISC_FALSE;
UNLOCK_LOOKUP;
return;
}
docancel = check_for_more_data(query, msg, sevent);
if (docancel) {
dns_message_destroy(&msg);
clear_query(query);
cancel_lookup(l);
check_next_lookup(l);
}
if (msg != NULL)
dns_message_destroy(&msg);
isc_event_free(&event);
}
else {
if ((msg->rcode == 0) ||
(l->origin == NULL)) {
isc_buffer_init(&ab, abspace, MXNAME);
result = isc_sockaddr_totext(&sevent->address,
&ab);
check_result(result, "isc_sockaddr_totext");
isc_buffer_usedregion(&ab, &r);
received(b->used, r.length,
(char *)r.base,
query);
}
query->lookup->pending = ISC_FALSE;
if (!query->lookup->ns_search_only ||
query->lookup->trace_root) {
dns_message_destroy(&msg);
cancel_lookup(l);
}
if (msg != NULL)
dns_message_destroy(&msg);
isc_event_free(&event);
clear_query(query);
check_next_lookup(l);
}
UNLOCK_LOOKUP;
return;
}
/*
* In truth, we should never get into the CANCELED routine, since
* the cancel_lookup() routine clears the pending flag.
* XXX Is this true anymore, since the bulk changes?
*/
if (sevent->result == ISC_R_CANCELED) {
debug("in recv cancel handler");
query->waiting_connect = ISC_FALSE;
isc_event_free(&event);
clear_query(query);
check_next_lookup(l);
UNLOCK_LOOKUP;
return;
}
printf(";; communications error: %s\n",
isc_result_totext(sevent->result));
isc_socket_detach(&query->sock);
sockcount--;
debug("sockcount=%d",sockcount);
INSIST(sockcount >= 0);
isc_event_free(&event);
clear_query(query);
check_next_lookup(l);
UNLOCK_LOOKUP;
return;
}
/*
* Turn a name into an address, using system-supplied routines. This is
* used in looking up server names, etc... and needs to use system-supplied
* routines, since they may be using a non-DNS system for these lookups.
*/
void
get_address(char *host, in_port_t port, isc_sockaddr_t *sockaddr) {
struct in_addr in4;
struct in6_addr in6;
#if defined(HAVE_ADDRINFO) && defined(HAVE_GETADDRINFO)
struct addrinfo *res = NULL;
int result;
#else
struct hostent *he;
#endif
debug("get_address()");
if (have_ipv6 && inet_pton(AF_INET6, host, &in6) == 1)
isc_sockaddr_fromin6(sockaddr, &in6, port);
else if (inet_pton(AF_INET, host, &in4) == 1)
isc_sockaddr_fromin(sockaddr, &in4, port);
else {
#if defined(HAVE_ADDRINFO) && defined(HAVE_GETADDRINFO)
result = getaddrinfo(host, NULL, NULL, &res);
if (result != 0) {
fatal("Couldn't find server '%s': %s",
host, gai_strerror(result));
}
memcpy(&sockaddr->type.sa,res->ai_addr, res->ai_addrlen);
sockaddr->length = res->ai_addrlen;
isc_sockaddr_setport(sockaddr, port);
freeaddrinfo(res);
#else
he = gethostbyname(host);
if (he == NULL)
fatal("Couldn't find server '%s' (h_errno=%d)",
host, h_errno);
INSIST(he->h_addrtype == AF_INET);
isc_sockaddr_fromin(sockaddr,
(struct in_addr *)(he->h_addr_list[0]),
port);
#endif
}
}
/*
* Initiate a TCP lookup, starting all of the queries running
*/
static void
do_lookup_tcp(dig_lookup_t *lookup) {
dig_query_t *query;
isc_result_t result;
unsigned int local_timeout;
debug("do_lookup_tcp()");
lookup->pending = ISC_TRUE;
if (timeout != INT_MAX) {
if (timeout == 0)
local_timeout = TCP_TIMEOUT;
else
local_timeout = timeout;
debug("have local timeout of %d", local_timeout);
isc_interval_set(&lookup->interval, local_timeout, 0);
result = isc_timer_create(timermgr, isc_timertype_once, NULL,
&lookup->interval, global_task,
connect_timeout, lookup,
&lookup->timer);
check_result(result, "isc_timer_create");
}
for (query = ISC_LIST_HEAD(lookup->q);
query != NULL;
query = ISC_LIST_NEXT(query, link)) {
query->waiting_connect = ISC_TRUE;
get_address(query->servname, port, &query->sockaddr);
INSIST(query->sock == NULL);
result = isc_socket_create(socketmgr,
isc_sockaddr_pf(&query->sockaddr),
isc_sockettype_tcp, &query->sock) ;
check_result(result, "isc_socket_create");
sockcount++;
debug("sockcount=%d",sockcount);
if (specified_source)
result = isc_socket_bind(query->sock, &bind_address);
else {
if (isc_sockaddr_pf(&query->sockaddr) == AF_INET)
isc_sockaddr_any(&bind_any);
else
isc_sockaddr_any6(&bind_any);
result = isc_socket_bind(query->sock, &bind_any);
}
check_result(result, "isc_socket_bind");
result = isc_socket_connect(query->sock, &query->sockaddr,
global_task, connect_done, query);
check_result(result, "isc_socket_connect");
}
}
/*
* Initiate a UDP lookup, starting all of the queries running
*/
static void
do_lookup_udp(dig_lookup_t *lookup) {
dig_query_t *query;
isc_result_t result;
debug("do_lookup_udp()");
INSIST(!lookup->tcp_mode);
lookup->pending = ISC_TRUE;
for (query = ISC_LIST_HEAD(lookup->q);
query != NULL;
query = ISC_LIST_NEXT(query, link)) {
/* XXX Check the sense of this, need assertion? */
query->waiting_connect = ISC_FALSE;
get_address(query->servname, port, &query->sockaddr);
result = isc_socket_create(socketmgr,
isc_sockaddr_pf(&query->sockaddr),
isc_sockettype_udp, &query->sock);
check_result(result, "isc_socket_create");
sockcount++;
debug("sockcount=%d", sockcount);
if (specified_source)
result = isc_socket_bind(query->sock, &bind_address);
else {
/* XXX Add this to lib, send gson mail. */
if (isc_sockaddr_pf(&query->sockaddr) == AF_INET)
isc_sockaddr_any(&bind_any);
else
isc_sockaddr_any6(&bind_any);
result = isc_socket_bind(query->sock, &bind_any);
}
check_result(result, "isc_socket_bind");
}
send_udp(lookup, ISC_TRUE);
}
/*
* Initiate either a TCP or UDP lookup
*/
void
do_lookup(dig_lookup_t *lookup) {
REQUIRE(lookup != NULL);
debug("do_lookup()");
if (lookup->tcp_mode)
do_lookup_tcp(lookup);
else
do_lookup_udp(lookup);
}
/*
* Start everything in action upon task startup.
*/
void
onrun_callback(isc_task_t *task, isc_event_t *event) {
UNUSED(task);
isc_event_free(&event);
LOCK_LOOKUP;
start_lookup();
UNLOCK_LOOKUP;
}
/*
* Make everything on the lookup queue go away. Mainly used by the
* SIGINT handler.
*/
void
cancel_all(void) {
dig_lookup_t *l, *n;
dig_query_t *q;
debug("cancel_all()");
LOCK_LOOKUP;
if (free_now) {
UNLOCK_LOOKUP;
return;
}
cancel_now = ISC_TRUE;
if (current_lookup != NULL) {
if (current_lookup->timer != NULL)
isc_timer_detach(&current_lookup->timer);
q = ISC_LIST_HEAD(current_lookup->q);
while (q != NULL) {
debug("cancelling query %p, belonging to %p",
q, current_lookup);
if (q->sock != NULL) {
isc_socket_cancel(q->sock, NULL,
ISC_SOCKCANCEL_ALL);
}
q = ISC_LIST_NEXT(q, link);
}
}
l = ISC_LIST_HEAD(lookup_list);
while (l != NULL) {
n = ISC_LIST_NEXT(l, link);
ISC_LIST_DEQUEUE(lookup_list, l, link);
try_clear_lookup(l);
l = n;
}
UNLOCK_LOOKUP;
}
/*
* Destroy all of the libs we are using, and get everything ready for a
* clean shutdown.
*/
void
destroy_libs(void) {
void *ptr;
dig_server_t *s;
dig_searchlist_t *o;
debug("destroy_libs()");
if (global_task != NULL) {
debug("freeing task");
isc_task_detach(&global_task);
}
/*
* The taskmgr_destroy() call blocks until all events are cleared
* from the task.
*/
if (taskmgr != NULL) {
debug("freeing taskmgr");
isc_taskmgr_destroy(&taskmgr);
}
LOCK_LOOKUP;
REQUIRE(sockcount == 0);
REQUIRE(recvcount == 0);
REQUIRE(sendcount == 0);
INSIST(ISC_LIST_HEAD(lookup_list) == NULL);
INSIST(current_lookup == NULL);
INSIST(!free_now);
free_now = ISC_TRUE;
s = ISC_LIST_HEAD(server_list);
while (s != NULL) {
debug("freeing global server %p", s);
ptr = s;
s = ISC_LIST_NEXT(s, link);
isc_mem_free(mctx, ptr);
}
o = ISC_LIST_HEAD(search_list);
while (o != NULL) {
debug("freeing search %p", o);
ptr = o;
o = ISC_LIST_NEXT(o, link);
isc_mem_free(mctx, ptr);
}
if (commctx != NULL) {
debug("freeing commctx");
isc_mempool_destroy(&commctx);
}
if (socketmgr != NULL) {
debug("freeing socketmgr");
isc_socketmgr_destroy(&socketmgr);
}
if (timermgr != NULL) {
debug("freeing timermgr");
isc_timermgr_destroy(&timermgr);
}
if (key != NULL) {
debug("freeing key %p", key);
dns_tsigkey_detach(&key);
}
if (namebuf != NULL)
isc_buffer_free(&namebuf);
if (is_dst_up) {
debug("destroy DST lib");
dst_lib_destroy();
is_dst_up = ISC_FALSE;
}
if (entp != NULL) {
debug("detach from entropy");
isc_entropy_detach(&entp);
}
isc_mutex_destroy(&lookup_lock);
if (isc_mem_debugging != 0)
isc_mem_stats(mctx, stderr);
if (mctx != NULL)
isc_mem_destroy(&mctx);
}