static void print_name (dns_name_t * name) { isc_result_t result; isc_buffer_t source; isc_region_t r; char s[1000]; isc_buffer_init (&source, s, sizeof (s)); if (dns_name_countlabels (name) > 0) result = dns_name_totext (name, ISC_FALSE, &source); else result = ISC_R_SUCCESS; if (result == ISC_R_SUCCESS) { isc_buffer_usedregion (&source, &r); if (r.length > 0) printf ("%.*s\n", (int) r.length, r.base); else printf ("<empty text name>\n"); } else printf ("error: %s\n", dns_result_totext (result)); }
/* * Adapted from the original rbt_test.c. */ static int t1_add(char *name, dns_rbt_t *rbt, isc_mem_t *mctx, isc_result_t *dns_result) { int nprobs; dns_name_t *dns_name; nprobs = 0; if (name && dns_result) { if (create_name(name, mctx, &dns_name) == 0) { if (T_debug) t_info("dns_rbt_addname succeeded\n"); *dns_result = dns_rbt_addname(rbt, dns_name, dns_name); if (*dns_result != ISC_R_SUCCESS) { delete_name(dns_name, mctx); t_info("dns_rbt_addname failed %s\n", dns_result_totext(*dns_result)); ++nprobs; } } else { ++nprobs; } } else { ++nprobs; } return(nprobs); }
int main(int argc, char **argv) { char *file; isc_mem_t *mctx = NULL; isc_result_t result; isc_log_t *lctx = NULL; if (argc != 2) { printf("usage: %s journal\n", argv[0]); return(1); } file = argv[1]; isc__mem_register(); RUNTIME_CHECK(isc_mem_create(0, 0, &mctx) == ISC_R_SUCCESS); RUNTIME_CHECK(setup_logging(mctx, stderr, &lctx) == ISC_R_SUCCESS); result = dns_journal_print(mctx, file, stdout); if (result == DNS_R_NOJOURNAL) fprintf(stderr, "%s\n", dns_result_totext(result)); isc_log_destroy(&lctx); isc_mem_detach(&mctx); return(result != ISC_R_SUCCESS ? 1 : 0); }
static int t_namechk(isc_result_t dns_result, dns_fixedname_t *dns_name, char *exp_name, dns_fixedname_t *dns_origin, char *exp_origin, isc_result_t exp_result) { int nfails; nfails = 0; if (fixedname_cmp(dns_name, exp_name)) { t_info("\texpected name of %s, got %s\n", exp_name, fixedname_totext(dns_name)); ++nfails; } if (exp_origin != NULL) { t_info("checking for DNS_R_NEWORIGIN\n"); if (dns_result == exp_result) { if (fixedname_cmp(dns_origin, exp_origin)) { t_info("\torigin %s, expected %s\n", fixedname_totext(dns_origin), exp_origin); ++nfails; } } else { t_info("\tgot %s\n", dns_result_totext(dns_result)); ++nfails; } } return(nfails); }
static void dumpmessage(dns_message_t *msg) { isc_buffer_t outbuf; unsigned char *output; int len = TEMP_BUFFER_SZ; isc_result_t result; for (;;) { output = isc_mem_get(msg->mctx, len); if (output == NULL) return; isc_buffer_init(&outbuf, output, len); result = dns_message_totext(msg, &dns_master_style_debug, 0, &outbuf); if (result == ISC_R_NOSPACE) { isc_mem_put(msg->mctx, output, len); len *= 2; continue; } if (result == ISC_R_SUCCESS) tkey_log("%.*s", (int)isc_buffer_usedlength(&outbuf), (char *)isc_buffer_base(&outbuf)); else tkey_log("Warning: dns_message_totext: %s", dns_result_totext(result)); break; } if (output != NULL) isc_mem_put(msg->mctx, output, len); }
/* * Load test data into the RBT. */ static void add_test_data(isc_mem_t *mymctx, dns_rbt_t *rbt) { char buffer[1024]; isc_buffer_t b; isc_result_t result; dns_fixedname_t fname; dns_name_t *name; dns_compress_t cctx; rbt_testdata_t *testdatap = testdata; dns_compress_init(&cctx, -1, mymctx); while (testdatap->name != NULL && testdatap->data.data != NULL) { memmove(buffer, testdatap->name, testdatap->name_len); isc_buffer_init(&b, buffer, testdatap->name_len); isc_buffer_add(&b, testdatap->name_len); dns_fixedname_init(&fname); name = dns_fixedname_name(&fname); result = dns_name_fromtext(name, &b, dns_rootname, 0, NULL); if (result != ISC_R_SUCCESS) { testdatap++; continue; } if (name != NULL) { result = dns_rbt_addname(rbt, name, &testdatap->data); ATF_CHECK_STREQ(dns_result_totext(result), "success"); } testdatap++; } dns_compress_invalidate(&cctx); }
static inline void CHECKRESULT(isc_result_t result, const char *msg) { if (result != ISC_R_SUCCESS) { printf("%s: %s\n", msg, dns_result_totext(result)); exit(1); } }
static void iterate(dns_rbt_t *rbt, isc_boolean_t forward) { dns_name_t foundname, *origin; dns_rbtnodechain_t chain; dns_fixedname_t fixedorigin; isc_result_t result; isc_result_t (*move)(dns_rbtnodechain_t *chain, dns_name_t *name, dns_name_t *origin); dns_rbtnodechain_init(&chain, mctx); dns_name_init(&foundname, NULL); dns_fixedname_init(&fixedorigin); origin = dns_fixedname_name(&fixedorigin); if (forward) { printf("iterating forward\n" ); move = dns_rbtnodechain_next; result = dns_rbtnodechain_first(&chain, rbt, &foundname, origin); } else { printf("iterating backward\n" ); move = dns_rbtnodechain_prev; result = dns_rbtnodechain_last(&chain, rbt, &foundname, origin); } if (result != ISC_R_SUCCESS && result != DNS_R_NEWORIGIN) printf("start not found!\n"); else { for (;;) { if (result == DNS_R_NEWORIGIN) { printf(" new origin: "); print_name(origin); printf("\n"); } if (result == ISC_R_SUCCESS || result == DNS_R_NEWORIGIN) { print_name(&foundname); printf("\n"); } else { if (result != ISC_R_NOMORE) printf("UNEXEPCTED ITERATION ERROR: %s", dns_result_totext(result)); break; } result = move(&chain, &foundname, origin); } } }
static dns_name_t *create_name (char *s) { int length; isc_result_t result; isc_buffer_t source, target; static dns_name_t *name; if (s == NULL || *s == '\0') { printf ("missing name argument\n"); return (NULL); } length = strlen (s); isc_buffer_init (&source, s, length); isc_buffer_add (&source, length); /* * It isn't really necessary in this program to create individual * memory spaces for each name structure and its associated character * string. It is done here to provide a relatively easy way to test * the callback from dns_rbt_deletename that is supposed to free the * data associated with a node. * * The buffer for the actual name will immediately follow the * name structure. */ name = isc_mem_get (mctx, sizeof (*name) + DNSNAMELEN); if (name == NULL) { printf ("out of memory!\n"); return (NULL); } dns_name_init (name, NULL); isc_buffer_init (&target, name + 1, DNSNAMELEN); result = dns_name_fromtext (name, &source, dns_rootname, 0, &target); if (result != ISC_R_SUCCESS) { printf ("dns_name_fromtext(%s) failed: %s\n", s, dns_result_totext (result)); return (NULL); } return (name); }
int main(int argc, char *argv[]) { isc_result_t result; dns_name_t origin; isc_buffer_t source; isc_buffer_t target; unsigned char name_buf[255]; dns_rdatacallbacks_t callbacks; UNUSED(argc); RUNTIME_CHECK(isc_mem_create(0, 0, &mctx) == ISC_R_SUCCESS); if (argv[1]) { isc_buffer_init(&source, argv[1], strlen(argv[1])); isc_buffer_add(&source, strlen(argv[1])); isc_buffer_setactive(&source, strlen(argv[1])); isc_buffer_init(&target, name_buf, 255); dns_name_init(&origin, NULL); result = dns_name_fromtext(&origin, &source, dns_rootname, 0, &target); if (result != ISC_R_SUCCESS) { fprintf(stdout, "dns_name_fromtext: %s\n", dns_result_totext(result)); exit(1); } dns_rdatacallbacks_init_stdio(&callbacks); callbacks.add = print_dataset; result = dns_master_loadfile(argv[1], &origin, &origin, dns_rdataclass_in, 0, &callbacks, mctx); fprintf(stdout, "dns_master_loadfile: %s\n", dns_result_totext(result)); } return (0); }
static int create_name(char *s, isc_mem_t *mctx, dns_name_t **dns_name) { int nfails; int length; isc_result_t result; isc_buffer_t source; isc_buffer_t target; dns_name_t *name; nfails = 0; if (s && *s) { length = strlen(s); isc_buffer_init(&source, s, length); isc_buffer_add(&source, length); /* * The buffer for the actual name will immediately follow the * name structure. */ name = isc_mem_get(mctx, sizeof(*name) + DNSNAMELEN); if (name == NULL) { t_info("isc_mem_get failed\n"); ++nfails; } else { dns_name_init(name, NULL); isc_buffer_init(&target, name + 1, DNSNAMELEN); result = dns_name_fromtext(name, &source, dns_rootname, 0, &target); if (result != ISC_R_SUCCESS) { ++nfails; t_info("dns_name_fromtext(%s) failed %s\n", s, dns_result_totext(result)); isc_mem_put(mctx, name, sizeof(*name) + DNSNAMELEN); } else *dns_name = name; } } else { ++nfails; t_info("create_name: empty name\n"); } return(nfails); }
static void _dns_tkey_dumpmessage (dns_message_t * msg) { isc_buffer_t outbuf; unsigned char output[4096]; isc_result_t result; isc_buffer_init (&outbuf, output, sizeof (output)); result = dns_message_totext (msg, &dns_master_style_debug, 0, &outbuf); if (result != ISC_R_SUCCESS) fprintf (stderr, "Warning: dns_message_totext returned: %s\n", dns_result_totext (result)); fprintf (stderr, "%.*s\n", (int) isc_buffer_usedlength (&outbuf), (char *) isc_buffer_base (&outbuf)); }
static void process_answer(isc_task_t *task, isc_event_t *event) { struct query_trans *trans = event->ev_arg; dns_clientresevent_t *rev = (dns_clientresevent_t *)event; dns_name_t *name; dns_rdataset_t *rdataset; isc_result_t result; REQUIRE(task == query_task); REQUIRE(trans->inuse == ISC_TRUE); REQUIRE(outstanding_queries > 0); printf("answer[%2d]\n", trans->id); if (rev->result != ISC_R_SUCCESS) printf(" failed: %d(%s)\n", rev->result, dns_result_totext(rev->result)); for (name = ISC_LIST_HEAD(rev->answerlist); name != NULL; name = ISC_LIST_NEXT(name, link)) { for (rdataset = ISC_LIST_HEAD(name->list); rdataset != NULL; rdataset = ISC_LIST_NEXT(rdataset, link)) { (void)printdata(rdataset, name); } } dns_client_freeresanswer(client, &rev->answerlist); dns_client_destroyrestrans(&trans->xid); isc_event_free(&event); trans->inuse = ISC_FALSE; dns_fixedname_invalidate(&trans->fixedname); trans->qname = NULL; outstanding_queries--; result = dispatch_query(trans); #if 0 /* for cancel test */ if (result == ISC_R_SUCCESS) { static int count = 0; if ((++count) % 10 == 0) dns_client_cancelresolve(trans->xid); } #endif if (result == ISC_R_NOMORE && outstanding_queries == 0) isc_app_ctxshutdown(query_actx); }
static isc_result_t print_data(void *data) { isc_result_t dns_result; isc_buffer_t target; char *buffer[DNSNAMELEN]; isc_buffer_init(&target, buffer, sizeof(buffer)); dns_result = dns_name_totext(data, ISC_FALSE, &target); if (dns_result != ISC_R_SUCCESS) { t_info("dns_name_totext failed %s\n", dns_result_totext(dns_result)); } return(dns_result); }
static void print_rdataset(dns_name_t *name, dns_rdataset_t *rdataset) { isc_buffer_t text; char t[1000]; isc_result_t result; isc_region_t r; isc_buffer_init(&text, t, sizeof(t)); result = dns_rdataset_totext(rdataset, name, ISC_FALSE, ISC_FALSE, &text); isc_buffer_usedregion(&text, &r); if (result == ISC_R_SUCCESS) printf("%.*s", (int)r.length, (char *)r.base); else printf("%s\n", dns_result_totext(result)); }
static isc_result_t t1_add_callback(void *arg, dns_name_t *owner, dns_rdataset_t *dataset) { char buf[BIGBUFLEN]; isc_buffer_t target; isc_result_t result; UNUSED(arg); isc_buffer_init(&target, buf, BIGBUFLEN); result = dns_rdataset_totext(dataset, owner, ISC_FALSE, ISC_FALSE, &target); if (result != ISC_R_SUCCESS) t_info("dns_rdataset_totext: %s\n", dns_result_totext(result)); return(result); }
static void lookup(const char *target) { dns_name_t name; unsigned char namedata[256]; client_t *client; isc_buffer_t t, namebuf; isc_result_t result; unsigned int options; INSIST(target != NULL); client = new_client(); isc_buffer_constinit(&t, target, strlen(target)); isc_buffer_add(&t, strlen(target)); isc_buffer_init(&namebuf, namedata, sizeof(namedata)); dns_name_init(&name, NULL); result = dns_name_fromtext(&name, &t, dns_rootname, 0, &namebuf); check_result(result, "dns_name_fromtext %s", target); result = dns_name_dup(&name, mctx, &client->name); check_result(result, "dns_name_dup %s", target); options = 0; options |= DNS_ADBFIND_INET; options |= DNS_ADBFIND_INET6; options |= DNS_ADBFIND_WANTEVENT; options |= DNS_ADBFIND_HINTOK; options |= DNS_ADBFIND_GLUEOK; result = dns_adb_createfind(adb, t2, lookup_callback, client, &client->name, dns_rootname, 0, options, now, NULL, view->dstport, &client->find); if (result != ISC_R_SUCCESS) printf("DNS_ADB_CREATEFIND -> %s\n", dns_result_totext(result)); dns_adb_dumpfind(client->find, stderr); if ((client->find->options & DNS_ADBFIND_WANTEVENT) != 0) { client->target = target; ISC_LIST_APPEND(clients, client, link); } else { printf("NAME %s: err4 %s, err6 %s\n", target, isc_result_totext(client->find->result_v4), isc_result_totext(client->find->result_v6)); dns_adb_destroyfind(&client->find); free_client(&client); } }
/* * Initialize a database from filename. */ static int rbt_init(char *filename, dns_rbt_t **rbt, isc_mem_t *mctx) { int rval; isc_result_t dns_result; char *p; FILE *fp; fp = fopen(filename, "r"); if (fp == NULL) { t_info("No such file %s\n", filename); return(1); } dns_result = dns_rbt_create(mctx, delete_name, mctx, rbt); if (dns_result != ISC_R_SUCCESS) { t_info("dns_rbt_create failed %s\n", dns_result_totext(dns_result)); fclose(fp); return(1); } while ((p = t_fgetbs(fp)) != NULL) { /* * Skip any comment lines. */ if ((*p == '#') || (*p == '\0') || (*p == ' ')) { (void)free(p); continue; } if (T_debug) t_info("adding name %s to the rbt\n", p); rval = t1_add(p, *rbt, mctx, &dns_result); if ((rval != 0) || (dns_result != ISC_R_SUCCESS)) { t_info("add of %s failed\n", p); dns_rbt_destroy(rbt); fclose(fp); return(1); } (void) free(p); } fclose(fp); return(0); }
static isc_result_t print_dataset(void *arg, dns_name_t *owner, dns_rdataset_t *dataset) { char buf[64*1024]; isc_buffer_t target; isc_result_t result; UNUSED(arg); isc_buffer_init(&target, buf, 64*1024); result = dns_rdataset_totext(dataset, owner, ISC_FALSE, ISC_FALSE, &target); if (result == ISC_R_SUCCESS) fprintf(stdout, "%.*s\n", (int)target.used, (char*)target.base); else fprintf(stdout, "dns_rdataset_totext: %s\n", dns_result_totext(result)); return (ISC_R_SUCCESS); }
/* * Walk the tree and ensure that all the test nodes are present. */ static void check_test_data(dns_rbt_t *rbt) { char buffer[1024]; char *arg; dns_fixedname_t fname; dns_fixedname_t fixed; dns_name_t *name; isc_buffer_t b; data_holder_t *data; isc_result_t result; dns_name_t *foundname; rbt_testdata_t *testdatap = testdata; dns_fixedname_init(&fixed); foundname = dns_fixedname_name(&fixed); while (testdatap->name != NULL && testdatap->data.data != NULL) { memmove(buffer, testdatap->name, testdatap->name_len + 1); arg = buffer; isc_buffer_init(&b, arg, testdatap->name_len); isc_buffer_add(&b, testdatap->name_len); dns_fixedname_init(&fname); name = dns_fixedname_name(&fname); result = dns_name_fromtext(name, &b, dns_rootname, 0, NULL); if (result != ISC_R_SUCCESS) { testdatap++; continue; } data = NULL; result = dns_rbt_findname(rbt, name, 0, foundname, (void *) &data); ATF_CHECK_STREQ(dns_result_totext(result), "success"); testdatap++; } }
int main(int argc, char *argv[]) { int ch, i, gai_error; struct addrinfo hints, *res; isc_textregion_t tr; dns_client_t *client = NULL; isc_result_t result; isc_sockaddr_t sa; dns_message_t *qmessage, *rmessage; dns_rdatatype_t type = dns_rdatatype_a; isc_buffer_t *outputbuf; while ((ch = getopt(argc, argv, "t:")) != -1) { switch (ch) { case 't': tr.base = optarg; tr.length = strlen(optarg); result = dns_rdatatype_fromtext(&type, &tr); if (result != ISC_R_SUCCESS) { fprintf(stderr, "invalid RRtype: %s\n", optarg); exit(1); } break; default: usage(); } } argc -= optind; argv += optind; if (argc < 2) usage(); isc_lib_register(); result = dns_lib_init(); if (result != ISC_R_SUCCESS) { fprintf(stderr, "dns_lib_init failed: %d\n", result); exit(1); } result = dns_client_create(&client, 0); if (result != ISC_R_SUCCESS) { fprintf(stderr, "dns_client_create failed: %d\n", result); exit(1); } /* Prepare message structures */ mctx = NULL; qmessage = NULL; rmessage = NULL; result = isc_mem_create(0, 0, &mctx); if (result != ISC_R_SUCCESS) { fprintf(stderr, "failed to create a memory context\n"); exit(1); } result = dns_message_create(mctx, DNS_MESSAGE_INTENTRENDER, &qmessage); if (result == ISC_R_SUCCESS) { result = dns_message_create(mctx, DNS_MESSAGE_INTENTPARSE, &rmessage); } if (result != ISC_R_SUCCESS) { fprintf(stderr, "failed to create messages\n"); exit(1); } /* Initialize the nameserver address */ memset(&hints, 0, sizeof(hints)); hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_DGRAM; hints.ai_protocol = IPPROTO_UDP; hints.ai_flags = AI_NUMERICHOST; gai_error = getaddrinfo(argv[0], "53", &hints, &res); if (gai_error != 0) { fprintf(stderr, "getaddrinfo failed: %s\n", gai_strerror(gai_error)); exit(1); } INSIST(res->ai_addrlen <= sizeof(sa.type)); memcpy(&sa.type, res->ai_addr, res->ai_addrlen); freeaddrinfo(res); sa.length = res->ai_addrlen; ISC_LINK_INIT(&sa, link); /* Construct qname */ result = make_querymessage(qmessage, argv[1], type); if (result != ISC_R_SUCCESS) { fprintf(stderr, "failed to create a query\n"); exit(1); } /* Send request and wait for a response */ result = dns_client_request(client, qmessage, rmessage, &sa, 0, 0, NULL, 60, 0, 3); if (result != ISC_R_SUCCESS) { fprintf(stderr, "failed to get a response: %s\n", dns_result_totext(result)); } /* Dump the response */ outputbuf = NULL; result = isc_buffer_allocate(mctx, &outputbuf, 65535); if (result != ISC_R_SUCCESS) { fprintf(stderr, "failed to allocate a result buffer\n"); exit(1); } for (i = 0; i < DNS_SECTION_MAX; i++) { print_section(rmessage, i, outputbuf); isc_buffer_clear(outputbuf); } isc_buffer_free(&outputbuf); /* Cleanup */ dns_message_destroy(&qmessage); dns_message_destroy(&rmessage); isc_mem_destroy(&mctx); dns_client_destroy(&client); dns_lib_shutdown(); exit(0); }
static int t_dns_rbtnodechain_init(char *dbfile, char *findname, char *nextname, char *nextorigin, char *prevname, char *prevorigin, char *firstname, char *firstorigin, char *lastname, char *lastorigin) { int result; int len; int nfails; dns_rbt_t *rbt; dns_rbtnode_t *node; dns_rbtnodechain_t chain; isc_mem_t *mctx; isc_entropy_t *ectx; isc_result_t isc_result; isc_result_t dns_result; dns_fixedname_t dns_findname; dns_fixedname_t dns_foundname; dns_fixedname_t dns_firstname; dns_fixedname_t dns_lastname; dns_fixedname_t dns_prevname; dns_fixedname_t dns_nextname; dns_fixedname_t dns_origin; isc_buffer_t isc_buffer; result = T_UNRESOLVED; nfails = 0; mctx = NULL; ectx = NULL; isc_result = isc_mem_create(0, 0, &mctx); if (isc_result != ISC_R_SUCCESS) { t_info("isc_mem_create failed %s\n", isc_result_totext(isc_result)); return(result); } isc_result = isc_entropy_create(mctx, &ectx); if (isc_result != ISC_R_SUCCESS) { t_info("isc_entropy_create: %s: exiting\n", dns_result_totext(isc_result)); isc_mem_destroy(&mctx); return(T_UNRESOLVED); } isc_result = isc_hash_create(mctx, ectx, DNS_NAME_MAXWIRE); if (isc_result != ISC_R_SUCCESS) { t_info("isc_hash_create: %s: exiting\n", dns_result_totext(isc_result)); isc_entropy_detach(&ectx); isc_mem_destroy(&mctx); return(T_UNRESOLVED); } dns_rbtnodechain_init(&chain, mctx); rbt = NULL; if (rbt_init(dbfile, &rbt, mctx)) { t_info("rbt_init %s failed\n", dbfile); isc_hash_destroy(); isc_entropy_detach(&ectx); isc_mem_destroy(&mctx); return(result); } len = strlen(findname); isc_buffer_init(&isc_buffer, findname, len); isc_buffer_add(&isc_buffer, len); dns_fixedname_init(&dns_foundname); dns_fixedname_init(&dns_findname); dns_fixedname_init(&dns_firstname); dns_fixedname_init(&dns_origin); dns_fixedname_init(&dns_lastname); dns_fixedname_init(&dns_prevname); dns_fixedname_init(&dns_nextname); dns_result = dns_name_fromtext(dns_fixedname_name(&dns_findname), &isc_buffer, NULL, 0, NULL); if (dns_result != ISC_R_SUCCESS) { t_info("dns_name_fromtext failed %s\n", dns_result_totext(dns_result)); return(result); } /* * Set the starting node. */ node = NULL; dns_result = dns_rbt_findnode(rbt, dns_fixedname_name(&dns_findname), dns_fixedname_name(&dns_foundname), &node, &chain, DNS_RBTFIND_EMPTYDATA, NULL, NULL); if (dns_result != ISC_R_SUCCESS) { t_info("dns_rbt_findnode failed %s\n", dns_result_totext(dns_result)); return(result); } /* * Check next. */ t_info("checking for next name of %s and new origin of %s\n", nextname, nextorigin); dns_result = dns_rbtnodechain_next(&chain, dns_fixedname_name(&dns_nextname), dns_fixedname_name(&dns_origin)); if ((dns_result != ISC_R_SUCCESS) && (dns_result != DNS_R_NEWORIGIN)) { t_info("dns_rbtnodechain_next unexpectedly returned %s\n", dns_result_totext(dns_result)); } nfails += t_namechk(dns_result, &dns_nextname, nextname, &dns_origin, nextorigin, DNS_R_NEWORIGIN); /* * Set the starting node again. */ node = NULL; dns_fixedname_init(&dns_foundname); dns_result = dns_rbt_findnode(rbt, dns_fixedname_name(&dns_findname), dns_fixedname_name(&dns_foundname), &node, &chain, DNS_RBTFIND_EMPTYDATA, NULL, NULL); if (dns_result != ISC_R_SUCCESS) { t_info("\tdns_rbt_findnode failed %s\n", dns_result_totext(dns_result)); return(result); } /* * Check previous. */ t_info("checking for previous name of %s and new origin of %s\n", prevname, prevorigin); dns_fixedname_init(&dns_origin); dns_result = dns_rbtnodechain_prev(&chain, dns_fixedname_name(&dns_prevname), dns_fixedname_name(&dns_origin)); if ((dns_result != ISC_R_SUCCESS) && (dns_result != DNS_R_NEWORIGIN)) { t_info("dns_rbtnodechain_prev unexpectedly returned %s\n", dns_result_totext(dns_result)); } nfails += t_namechk(dns_result, &dns_prevname, prevname, &dns_origin, prevorigin, DNS_R_NEWORIGIN); /* * Check first. */ t_info("checking for first name of %s and new origin of %s\n", firstname, firstorigin); dns_result = dns_rbtnodechain_first(&chain, rbt, dns_fixedname_name(&dns_firstname), dns_fixedname_name(&dns_origin)); if (dns_result != DNS_R_NEWORIGIN) { t_info("dns_rbtnodechain_first unexpectedly returned %s\n", dns_result_totext(dns_result)); } nfails += t_namechk(dns_result, &dns_firstname, firstname, &dns_origin, firstorigin, DNS_R_NEWORIGIN); /* * Check last. */ t_info("checking for last name of %s\n", lastname); dns_result = dns_rbtnodechain_last(&chain, rbt, dns_fixedname_name(&dns_lastname), dns_fixedname_name(&dns_origin)); if (dns_result != DNS_R_NEWORIGIN) { t_info("dns_rbtnodechain_last unexpectedly returned %s\n", dns_result_totext(dns_result)); } nfails += t_namechk(dns_result, &dns_lastname, lastname, &dns_origin, lastorigin, DNS_R_NEWORIGIN); /* * Check node ordering. */ t_info("checking node ordering\n"); nfails += t9_walkchain(&chain, rbt); if (nfails) result = T_FAIL; else result = T_PASS; dns_rbtnodechain_invalidate(&chain); dns_rbt_destroy(&rbt); isc_hash_destroy(); isc_entropy_detach(&ectx); isc_mem_destroy(&mctx); return(result); }
static void query(void) { char buf[1024]; dns_fixedname_t name; dns_fixedname_t found; dns_db_t *db; char *s; isc_buffer_t buffer; isc_result_t result; dns_rdataset_t rdataset; dns_rdataset_t sigset; fd_set rfdset; db = NULL; result = dns_zone_getdb(zone, &db); if (result != ISC_R_SUCCESS) { fprintf(stderr, "%s() returned %s\n", "dns_zone_getdb", dns_result_totext(result)); return; } dns_fixedname_init(&found); dns_rdataset_init(&rdataset); dns_rdataset_init(&sigset); do { fprintf(stdout, "zone_test "); fflush(stdout); FD_ZERO(&rfdset); FD_SET(0, &rfdset); select(1, &rfdset, NULL, NULL, NULL); if (fgets(buf, sizeof(buf), stdin) == NULL) { fprintf(stdout, "\n"); break; } buf[sizeof(buf) - 1] = '\0'; s = strchr(buf, '\n'); if (s != NULL) *s = '\0'; s = strchr(buf, '\r'); if (s != NULL) *s = '\0'; if (strcmp(buf, "dump") == 0) { dns_zone_dumptostream(zone, stdout); continue; } if (strlen(buf) == 0U) continue; dns_fixedname_init(&name); isc_buffer_init(&buffer, buf, strlen(buf)); isc_buffer_add(&buffer, strlen(buf)); result = dns_name_fromtext(dns_fixedname_name(&name), &buffer, dns_rootname, 0, NULL); ERRCONT(result, "dns_name_fromtext"); result = dns_db_find(db, dns_fixedname_name(&name), NULL /*vesion*/, dns_rdatatype_a, 0 /*options*/, 0 /*time*/, NULL /*nodep*/, dns_fixedname_name(&found), &rdataset, &sigset); fprintf(stderr, "%s() returned %s\n", "dns_db_find", dns_result_totext(result)); switch (result) { case DNS_R_DELEGATION: print_rdataset(dns_fixedname_name(&found), &rdataset); break; case ISC_R_SUCCESS: print_rdataset(dns_fixedname_name(&name), &rdataset); break; default: break; } if (dns_rdataset_isassociated(&rdataset)) dns_rdataset_disassociate(&rdataset); if (dns_rdataset_isassociated(&sigset)) dns_rdataset_disassociate(&sigset); } while (1); dns_rdataset_invalidate(&rdataset); dns_db_detach(&db); }
static int t9_walkchain(dns_rbtnodechain_t *chain, dns_rbt_t *rbt) { int cnt; int order; unsigned int nlabels; int nprobs; isc_result_t dns_result; dns_fixedname_t name; dns_fixedname_t origin; dns_fixedname_t fullname1; dns_fixedname_t fullname2; cnt = 0; nprobs = 0; do { if (cnt == 0) { dns_fixedname_init(&name); dns_fixedname_init(&origin); dns_result = dns_rbtnodechain_first(chain, rbt, dns_fixedname_name(&name), dns_fixedname_name(&origin)); if (dns_result != DNS_R_NEWORIGIN) { t_info("dns_rbtnodechain_first returned %s, " "expecting DNS_R_NEWORIGIN\n", dns_result_totext(dns_result)); ++nprobs; break; } t_info("first name:\t<%s>\n", fixedname_totext(&name)); t_info("first origin:\t<%s>\n", fixedname_totext(&origin)); } else { dns_fixedname_init(&fullname1); dns_result = dns_name_concatenate( dns_fixedname_name(&name), dns_name_isabsolute(dns_fixedname_name(&name)) ? NULL : dns_fixedname_name(&origin), dns_fixedname_name(&fullname1), NULL); if (dns_result != ISC_R_SUCCESS) { t_info("dns_name_concatenate failed %s\n", dns_result_totext(dns_result)); ++nprobs; break; } /* * Expecting origin not to get touched if next * doesn't return NEWORIGIN. */ dns_fixedname_init(&name); dns_result = dns_rbtnodechain_next(chain, dns_fixedname_name(&name), dns_fixedname_name(&origin)); if ((dns_result != ISC_R_SUCCESS) && (dns_result != DNS_R_NEWORIGIN)) { if (dns_result != ISC_R_NOMORE) { t_info("dns_rbtnodechain_next " "failed %s\n", dns_result_totext(dns_result)); ++nprobs; } break; } t_info("next name:\t<%s>\n", fixedname_totext(&name)); t_info("next origin:\t<%s>\n", fixedname_totext(&origin)); dns_fixedname_init(&fullname2); dns_result = dns_name_concatenate( dns_fixedname_name(&name), dns_name_isabsolute(dns_fixedname_name(&name)) ? NULL : dns_fixedname_name(&origin), dns_fixedname_name(&fullname2), NULL); if (dns_result != ISC_R_SUCCESS) { t_info("dns_name_concatenate failed %s\n", dns_result_totext(dns_result)); ++nprobs; break; } t_info("comparing\t<%s>\n", fixedname_totext(&fullname1)); t_info("\twith\t<%s>\n", fixedname_totext(&fullname2)); (void)dns_name_fullcompare( dns_fixedname_name(&fullname1), dns_fixedname_name(&fullname2), &order, &nlabels); if (order >= 0) { t_info("unexpected order %s %s %s\n", dnsname_totext(dns_fixedname_name(&fullname1)), order == -1 ? "<" : (order == 0 ? "==" : ">"), dnsname_totext(dns_fixedname_name(&fullname2))); ++nprobs; } } ++cnt; } while (1); return (nprobs); }
static int test_rbt_gen(char *filename, char *command, char *testname, isc_result_t exp_result) { int rval; int result; dns_rbt_t *rbt; isc_result_t isc_result; isc_result_t dns_result; isc_mem_t *mctx; isc_entropy_t *ectx; dns_name_t *dns_name; result = T_UNRESOLVED; if (strcmp(command, "create") != 0) t_info("testing using name %s\n", testname); mctx = NULL; ectx = NULL; isc_result = isc_mem_create(0, 0, &mctx); if (isc_result != ISC_R_SUCCESS) { t_info("isc_mem_create: %s: exiting\n", dns_result_totext(isc_result)); return(T_UNRESOLVED); } isc_result = isc_entropy_create(mctx, &ectx); if (isc_result != ISC_R_SUCCESS) { t_info("isc_entropy_create: %s: exiting\n", dns_result_totext(isc_result)); isc_mem_destroy(&mctx); return(T_UNRESOLVED); } isc_result = isc_hash_create(mctx, ectx, DNS_NAME_MAXWIRE); if (isc_result != ISC_R_SUCCESS) { t_info("isc_hash_create: %s: exiting\n", dns_result_totext(isc_result)); isc_entropy_detach(&ectx); isc_mem_destroy(&mctx); return(T_UNRESOLVED); } rbt = NULL; if (rbt_init(filename, &rbt, mctx) != 0) { if (strcmp(command, "create") == 0) result = T_FAIL; isc_hash_destroy(); isc_entropy_detach(&ectx); isc_mem_destroy(&mctx); return(result); } /* * Now try the database command. */ if (strcmp(command, "create") == 0) { result = T_PASS; } else if (strcmp(command, "add") == 0) { if (create_name(testname, mctx, &dns_name) == 0) { dns_result = dns_rbt_addname(rbt, dns_name, dns_name); if (dns_result != ISC_R_SUCCESS) delete_name(dns_name, mctx); if (dns_result == exp_result) { if (dns_result == ISC_R_SUCCESS) { rval = t1_search(testname, rbt, mctx, &dns_result); if (rval == 0) { if (dns_result == ISC_R_SUCCESS) { result = T_PASS; } else { result = T_FAIL; } } else { t_info("t1_search failed\n"); result = T_UNRESOLVED; } } else { result = T_PASS; } } else { t_info("dns_rbt_addname returned %s, " "expected %s\n", dns_result_totext(dns_result), dns_result_totext(exp_result)); result = T_FAIL; } } else { t_info("create_name failed\n"); result = T_UNRESOLVED; } } else if ((strcmp(command, "delete") == 0) || (strcmp(command, "nuke") == 0)) { rval = t1_delete(testname, rbt, mctx, &dns_result); if (rval == 0) { if (dns_result == exp_result) { rval = t1_search(testname, rbt, mctx, &dns_result); if (rval == 0) { if (dns_result == ISC_R_SUCCESS) { t_info("dns_rbt_deletename " "didn't delete " "the name"); result = T_FAIL; } else { result = T_PASS; } } } else { t_info("delete returned %s, expected %s\n", dns_result_totext(dns_result), dns_result_totext(exp_result)); result = T_FAIL; } } } else if (strcmp(command, "search") == 0) { rval = t1_search(testname, rbt, mctx, &dns_result); if (rval == 0) { if (dns_result == exp_result) { result = T_PASS; } else { t_info("find returned %s, expected %s\n", dns_result_totext(dns_result), dns_result_totext(exp_result)); result = T_FAIL; } } } dns_rbt_destroy(&rbt); isc_hash_destroy(); isc_entropy_detach(&ectx); isc_mem_destroy(&mctx); return(result); }
void dns_root_checkhints(dns_view_t *view, dns_db_t *hints, dns_db_t *db) { isc_result_t result; dns_rdata_t rdata = DNS_RDATA_INIT; dns_rdata_ns_t ns; dns_rdataset_t hintns, rootns; const char *viewname = "", *sep = ""; isc_stdtime_t now; dns_name_t *name; dns_fixedname_t fixed; REQUIRE(hints != NULL); REQUIRE(db != NULL); REQUIRE(view != NULL); isc_stdtime_get(&now); if (strcmp(view->name, "_bind") != 0 && strcmp(view->name, "_default") != 0) { viewname = view->name; sep = ": view "; } dns_rdataset_init(&hintns); dns_rdataset_init(&rootns); dns_fixedname_init(&fixed); name = dns_fixedname_name(&fixed); result = dns_db_find(hints, dns_rootname, NULL, dns_rdatatype_ns, 0, now, NULL, name, &hintns, NULL); if (result != ISC_R_SUCCESS) { isc_log_write(dns_lctx, DNS_LOGCATEGORY_GENERAL, DNS_LOGMODULE_HINTS, ISC_LOG_WARNING, "checkhints%s%s: unable to get root NS rrset " "from hints: %s", sep, viewname, dns_result_totext(result)); goto cleanup; } result = dns_db_find(db, dns_rootname, NULL, dns_rdatatype_ns, 0, now, NULL, name, &rootns, NULL); if (result != ISC_R_SUCCESS) { isc_log_write(dns_lctx, DNS_LOGCATEGORY_GENERAL, DNS_LOGMODULE_HINTS, ISC_LOG_WARNING, "checkhints%s%s: unable to get root NS rrset " "from cache: %s", sep, viewname, dns_result_totext(result)); goto cleanup; } /* * Look for missing root NS names. */ result = dns_rdataset_first(&rootns); while (result == ISC_R_SUCCESS) { dns_rdataset_current(&rootns, &rdata); result = dns_rdata_tostruct(&rdata, &ns, NULL); RUNTIME_CHECK(result == ISC_R_SUCCESS); result = in_rootns(&hintns, &ns.name); if (result != ISC_R_SUCCESS) { char namebuf[DNS_NAME_FORMATSIZE]; /* missing from hints */ dns_name_format(&ns.name, namebuf, sizeof(namebuf)); isc_log_write(dns_lctx, DNS_LOGCATEGORY_GENERAL, DNS_LOGMODULE_HINTS, ISC_LOG_WARNING, "checkhints%s%s: unable to find root " "NS '%s' in hints", sep, viewname, namebuf); } else check_address_records(view, hints, db, &ns.name, now); dns_rdata_reset(&rdata); result = dns_rdataset_next(&rootns); } if (result != ISC_R_NOMORE) { goto cleanup; } /* * Look for extra root NS names. */ result = dns_rdataset_first(&hintns); while (result == ISC_R_SUCCESS) { dns_rdataset_current(&hintns, &rdata); result = dns_rdata_tostruct(&rdata, &ns, NULL); RUNTIME_CHECK(result == ISC_R_SUCCESS); result = in_rootns(&rootns, &ns.name); if (result != ISC_R_SUCCESS) { char namebuf[DNS_NAME_FORMATSIZE]; /* extra entry in hints */ dns_name_format(&ns.name, namebuf, sizeof(namebuf)); isc_log_write(dns_lctx, DNS_LOGCATEGORY_GENERAL, DNS_LOGMODULE_HINTS, ISC_LOG_WARNING, "checkhints%s%s: extra NS '%s' in hints", sep, viewname, namebuf); } dns_rdata_reset(&rdata); result = dns_rdataset_next(&hintns); } if (result != ISC_R_NOMORE) { goto cleanup; } cleanup: if (dns_rdataset_isassociated(&rootns)) dns_rdataset_disassociate(&rootns); if (dns_rdataset_isassociated(&hintns)) dns_rdataset_disassociate(&hintns); }
static void detail(dns_rbt_t *rbt, dns_name_t *name) { dns_name_t *foundname, *origin, *fullname; dns_fixedname_t fixedfoundname, fixedorigin, fixedfullname; dns_rbtnode_t *node1, *node2; dns_rbtnodechain_t chain; isc_result_t result; isc_boolean_t nodes_should_match = ISC_FALSE; dns_rbtnodechain_init(&chain, mctx); dns_fixedname_init(&fixedorigin); dns_fixedname_init(&fixedfullname); dns_fixedname_init(&fixedfoundname); origin = dns_fixedname_name(&fixedorigin); fullname = dns_fixedname_name(&fixedfullname); foundname = dns_fixedname_name(&fixedfoundname); node1 = node2 = NULL; printf("checking chain information for "); print_name(name); printf("\n"); result = dns_rbt_findnode(rbt, name, foundname, &node1, &chain, DNS_RBTFIND_EMPTYDATA, NULL, NULL); switch (result) { case ISC_R_SUCCESS: printf(" found exact."); nodes_should_match = ISC_TRUE; break; case DNS_R_PARTIALMATCH: printf(" found parent."); break; case ISC_R_NOTFOUND: printf(" name not found."); break; default: printf(" unexpected result: %s\n", dns_result_totext(result)); return; } if (node1 != NULL && node1->data != NULL) { printf(" data at node: "); print_name(node1->data); } else printf(" no data at node."); if (result == ISC_R_SUCCESS || result == DNS_R_PARTIALMATCH) { printf("\n name from dns_rbt_findnode: "); print_name(foundname); } result = dns_rbtnodechain_current(&chain, foundname, origin, &node2); if (result == ISC_R_SUCCESS) { printf("\n name from dns_rbtnodechain_current: "); result = dns_name_concatenate(foundname, origin, fullname, NULL); if (result == ISC_R_SUCCESS) print_name(fullname); else printf("%s\n", dns_result_totext(result)); printf("\n (foundname = "); print_name(foundname); printf(", origin = "); print_name(origin); printf(")\n"); if (nodes_should_match && node1 != node2) printf(" nodes returned from each function " "DO NOT match!\n"); } else printf("\n result from dns_rbtnodechain_current: %s\n", dns_result_totext(result)); printf(" level_matches = %d, level_count = %d\n", chain.level_matches, chain.level_count); }
static int t_dns_rbtnodechain_last(char *dbfile, char *expected_lastname, char *expected_lastorigin, char *expected_prevname, char *expected_prevorigin) { int result; int nfails; dns_rbt_t *rbt; dns_rbtnodechain_t chain; isc_mem_t *mctx; isc_entropy_t *ectx; isc_result_t isc_result; isc_result_t dns_result; dns_fixedname_t dns_name; dns_fixedname_t dns_origin; isc_result_t expected_result; result = T_UNRESOLVED; nfails = 0; mctx = NULL; ectx = NULL; dns_fixedname_init(&dns_name); dns_fixedname_init(&dns_origin); isc_result = isc_mem_create(0, 0, &mctx); if (isc_result != ISC_R_SUCCESS) { t_info("isc_mem_create failed %s\n", isc_result_totext(isc_result)); return(result); } isc_result = isc_entropy_create(mctx, &ectx); if (isc_result != ISC_R_SUCCESS) { t_info("isc_entropy_create: %s: exiting\n", dns_result_totext(isc_result)); isc_mem_destroy(&mctx); return(T_UNRESOLVED); } isc_result = isc_hash_create(mctx, ectx, DNS_NAME_MAXWIRE); if (isc_result != ISC_R_SUCCESS) { t_info("isc_hash_create: %s: exiting\n", dns_result_totext(isc_result)); isc_entropy_detach(&ectx); isc_mem_destroy(&mctx); return(T_UNRESOLVED); } dns_rbtnodechain_init(&chain, mctx); rbt = NULL; if (rbt_init(dbfile, &rbt, mctx)) { t_info("rbt_init %s failed\n", dbfile); isc_hash_destroy(); isc_entropy_detach(&ectx); isc_mem_destroy(&mctx); return(result); } t_info("testing for last name of %s, origin of %s\n", expected_lastname, expected_lastorigin); dns_result = dns_rbtnodechain_last(&chain, rbt, dns_fixedname_name(&dns_name), dns_fixedname_name(&dns_origin)); if (dns_result != DNS_R_NEWORIGIN) { t_info("dns_rbtnodechain_last unexpectedly returned %s\n", dns_result_totext(dns_result)); } nfails += t_namechk(dns_result, &dns_name, expected_lastname, &dns_origin, expected_lastorigin, DNS_R_NEWORIGIN); t_info("testing for previous name of %s, origin of %s\n", expected_prevname, expected_prevorigin); dns_fixedname_init(&dns_name); dns_result = dns_rbtnodechain_prev(&chain, dns_fixedname_name(&dns_name), dns_fixedname_name(&dns_origin)); if ((dns_result != ISC_R_SUCCESS) && (dns_result != DNS_R_NEWORIGIN)) { t_info("dns_rbtnodechain_prev unexpectedly returned %s\n", dns_result_totext(dns_result)); } if (strcasecmp(expected_lastorigin, expected_prevorigin) == 0) expected_result = ISC_R_SUCCESS; else expected_result = DNS_R_NEWORIGIN; nfails += t_namechk(dns_result, &dns_name, expected_prevname, &dns_origin, expected_prevorigin, expected_result); if (nfails) result = T_FAIL; else result = T_PASS; dns_rbtnodechain_invalidate(&chain); dns_rbt_destroy(&rbt); isc_hash_destroy(); isc_entropy_detach(&ectx); isc_mem_destroy(&mctx); return(result); }
int main(int argc, char **argv) { char *command, *arg, buffer[1024]; const char *whitespace; dns_name_t *name, *foundname; dns_fixedname_t fixedname; dns_rbt_t *rbt = NULL; int length, ch; isc_boolean_t show_final_mem = ISC_FALSE; isc_result_t result; void *data; progname = strrchr(*argv, '/'); if (progname != NULL) progname++; else progname = *argv; while ((ch = isc_commandline_parse(argc, argv, "m")) != -1) { switch (ch) { case 'm': show_final_mem = ISC_TRUE; break; } } argc -= isc_commandline_index; argv += isc_commandline_index; POST(argv); if (argc > 1) { printf("Usage: %s [-m]\n", progname); exit(1); } setbuf(stdout, NULL); /* * So isc_mem_stats() can report any allocation leaks. */ isc_mem_debugging = ISC_MEM_DEBUGRECORD; result = isc_mem_create(0, 0, &mctx); if (result != ISC_R_SUCCESS) { printf("isc_mem_create: %s: exiting\n", dns_result_totext(result)); exit(1); } result = dns_rbt_create(mctx, delete_name, NULL, &rbt); if (result != ISC_R_SUCCESS) { printf("dns_rbt_create: %s: exiting\n", dns_result_totext(result)); exit(1); } whitespace = " \t"; while (fgets(buffer, sizeof(buffer), stdin) != NULL) { length = strlen(buffer); if (buffer[length - 1] != '\n') { printf("line to long (%lu max), ignored\n", (unsigned long)sizeof(buffer) - 2); continue; } buffer[length - 1] = '\0'; command = buffer + strspn(buffer, whitespace); if (*command == '#') continue; arg = strpbrk(command, whitespace); if (arg != NULL) { *arg++ = '\0'; arg += strspn(arg, whitespace); } length = strlen(command); if (*command != '\0') { if (CMDCHECK("add")) { name = create_name(arg); if (name != NULL) { printf("adding name %s\n", arg); result = dns_rbt_addname(rbt, name, name); PRINTERR(result); } } else if (CMDCHECK("delete")) { name = create_name(arg); if (name != NULL) { printf("deleting name %s\n", arg); result = dns_rbt_deletename(rbt, name, ISC_FALSE); PRINTERR(result); delete_name(name, NULL); } } else if (CMDCHECK("nuke")) { name = create_name(arg); if (name != NULL) { printf("nuking name %s " "and its descendants\n", arg); result = dns_rbt_deletename(rbt, name, ISC_TRUE); PRINTERR(result); delete_name(name, NULL); } } else if (CMDCHECK("search")) { name = create_name(arg); if (name != NULL) { printf("searching for name %s ... ", arg); dns_fixedname_init(&fixedname); foundname = dns_fixedname_name(&fixedname); data = NULL; result = dns_rbt_findname(rbt, name, 0, foundname, &data); switch (result) { case ISC_R_SUCCESS: printf("found exact: "); print_name(data); putchar('\n'); break; case DNS_R_PARTIALMATCH: printf("found parent: "); print_name(data); printf("\n\t(foundname: "); print_name(foundname); printf(")\n"); break; case ISC_R_NOTFOUND: printf("NOT FOUND!\n"); break; case ISC_R_NOMEMORY: printf("OUT OF MEMORY!\n"); break; default: printf("UNEXPECTED RESULT\n"); } delete_name(name, NULL); } } else if (CMDCHECK("check")) { /* * Or "chain". I know, I know. Lame name. * I was having a hard time thinking of a * name (especially one that did not have * a conflicting first letter with another * command) that would differentiate this * from the search command. * * But it is just a test program, eh? */ name = create_name(arg); if (name != NULL) { detail(rbt, name); delete_name(name, NULL); } } else if (CMDCHECK("forward")) { iterate(rbt, ISC_TRUE); } else if (CMDCHECK("backward")) { iterate(rbt, ISC_FALSE); } else if (CMDCHECK("print")) { if (arg == NULL || *arg == '\0') dns_rbt_printall(rbt, NULL); else printf("usage: print\n"); } else if (CMDCHECK("quit")) { if (arg == NULL || *arg == '\0') break; else printf("usage: quit\n"); } else { printf("a(dd) NAME, d(elete) NAME, " "s(earch) NAME, p(rint), or q(uit)\n"); } } } dns_rbt_destroy(&rbt); if (show_final_mem) isc_mem_stats(mctx, stderr); return (0); }
int main(int argc, char *argv[]) { isc_token_t token; isc_result_t result; int c; unsigned int options = 0; dns_rdatatype_t rdtype; dns_rdataclass_t rdclass; char text[256*1024]; char data[64*1024]; isc_buffer_t tbuf; isc_buffer_t dbuf; dns_rdata_t rdata = DNS_RDATA_INIT; isc_boolean_t doexit = ISC_FALSE; isc_boolean_t once = ISC_FALSE; isc_boolean_t print = ISC_FALSE; isc_boolean_t unknown = ISC_FALSE; unsigned int t; char *origin = NULL; dns_fixedname_t fixed; dns_name_t *name = NULL; while ((c = isc_commandline_parse(argc, argv, "ho:puCPT")) != -1) { switch (c) { case 'o': origin = isc_commandline_argument; break; case 'p': print = ISC_TRUE; break; case 'u': unknown = ISC_TRUE; break; case 'C': for (t = 1; t <= 0xfeffu; t++) { if (dns_rdataclass_ismeta(t)) continue; dns_rdataclass_format(t, text, sizeof(text)); if (strncmp(text, "CLASS", 4) != 0) fprintf(stdout, "%s\n", text); } exit(0); case 'P': for (t = 0xff00; t <= 0xfffeu; t++) { if (dns_rdatatype_ismeta(t)) continue; dns_rdatatype_format(t, text, sizeof(text)); if (strncmp(text, "TYPE", 4) != 0) fprintf(stdout, "%s\n", text); } doexit = ISC_TRUE; break; case 'T': for (t = 1; t <= 0xfeffu; t++) { if (dns_rdatatype_ismeta(t)) continue; dns_rdatatype_format(t, text, sizeof(text)); if (strncmp(text, "TYPE", 4) != 0) fprintf(stdout, "%s\n", text); } doexit = ISC_TRUE; break; case '?': case 'h': /* Does not return. */ usage(); default: fprintf(stderr, "%s: unhandled option -%c\n", argv[0], isc_commandline_option); exit(1); } } if (doexit) exit(0); RUNTIME_CHECK(isc_mem_create(0, 0, &mctx) == ISC_R_SUCCESS); RUNTIME_CHECK(isc_lex_create(mctx, 256, &lex) == ISC_R_SUCCESS); /* * Set up to lex DNS master file. */ specials['('] = 1; specials[')'] = 1; specials['"'] = 1; isc_lex_setspecials(lex, specials); options = ISC_LEXOPT_EOL; isc_lex_setcomments(lex, ISC_LEXCOMMENT_DNSMASTERFILE); RUNTIME_CHECK(isc_lex_openstream(lex, stdin) == ISC_R_SUCCESS); if (origin != NULL) { dns_fixedname_init(&fixed); name = dns_fixedname_name(&fixed); result = dns_name_fromstring(name, origin, 0, NULL); if (result != ISC_R_SUCCESS) { fatal("dns_name_fromstring: %s", dns_result_totext(result)); } } while ((result = isc_lex_gettoken(lex, options | ISC_LEXOPT_NUMBER, &token)) == ISC_R_SUCCESS) { if (token.type == isc_tokentype_eof) break; if (token.type == isc_tokentype_eol) continue; if (once) { fatal("extra data"); } /* * Get class. */ if (token.type == isc_tokentype_number) { rdclass = (dns_rdataclass_t) token.value.as_ulong; if (token.value.as_ulong > 0xffffu) { fatal("class value too big %lu", token.value.as_ulong); } if (dns_rdataclass_ismeta(rdclass)) { fatal("class %lu is a meta value", token.value.as_ulong); } } else if (token.type == isc_tokentype_string) { result = dns_rdataclass_fromtext(&rdclass, &token.value.as_textregion); if (result != ISC_R_SUCCESS) { fatal("dns_rdataclass_fromtext: %s", dns_result_totext(result)); } if (dns_rdataclass_ismeta(rdclass)) { fatal("class %.*s(%d) is a meta value", (int)token.value.as_textregion.length, token.value.as_textregion.base, rdclass); } } else { fatal("unexpected token %u", token.type); } result = isc_lex_gettoken(lex, options | ISC_LEXOPT_NUMBER, &token); if (result != ISC_R_SUCCESS) break; if (token.type == isc_tokentype_eol) continue; if (token.type == isc_tokentype_eof) break; /* * Get type. */ if (token.type == isc_tokentype_number) { rdtype = (dns_rdatatype_t) token.value.as_ulong; if (token.value.as_ulong > 0xffffu) { fatal("type value too big %lu", token.value.as_ulong); } if (dns_rdatatype_ismeta(rdtype)) { fatal("type %lu is a meta value", token.value.as_ulong); } } else if (token.type == isc_tokentype_string) { result = dns_rdatatype_fromtext(&rdtype, &token.value.as_textregion); if (result != ISC_R_SUCCESS) { fatal("dns_rdatatype_fromtext: %s", dns_result_totext(result)); } if (dns_rdatatype_ismeta(rdtype)) { fatal("type %.*s(%d) is a meta value", (int)token.value.as_textregion.length, token.value.as_textregion.base, rdtype); } } else { fatal("unexpected token %u", token.type); } isc_buffer_init(&dbuf, data, sizeof(data)); result = dns_rdata_fromtext(&rdata, rdclass, rdtype, lex, name, 0, mctx, &dbuf, NULL); if (result != ISC_R_SUCCESS) { fatal("dns_rdata_fromtext: %s", dns_result_totext(result)); } once = ISC_TRUE; } if (result != ISC_R_EOF) { fatal("eof not found"); } if (!once) { fatal("no records found"); } if (print) { isc_buffer_init(&tbuf, text, sizeof(text)); result = dns_rdataclass_totext(rdclass, &tbuf); if (result != ISC_R_SUCCESS) { fatal("dns_rdataclass_totext: %s", dns_result_totext(result)); } isc_buffer_putstr(&tbuf, "\t"); result = dns_rdatatype_totext(rdtype, &tbuf); if (result != ISC_R_SUCCESS) { fatal("dns_rdatatype_totext: %s", dns_result_totext(result)); } isc_buffer_putstr(&tbuf, "\t"); result = dns_rdata_totext(&rdata, NULL, &tbuf); if (result != ISC_R_SUCCESS) { fatal("dns_rdata_totext: %s", dns_result_totext(result)); } printf("%.*s\n", (int)tbuf.used, (char*)tbuf.base); fflush(stdout); } if (unknown) { isc_buffer_init(&tbuf, text, sizeof(text)); result = dns_rdataclass_tounknowntext(rdclass, &tbuf); if (result != ISC_R_SUCCESS) { fatal("dns_rdataclass_tounknowntext: %s", dns_result_totext(result)); } isc_buffer_putstr(&tbuf, "\t"); result = dns_rdatatype_tounknowntext(rdtype, &tbuf); if (result != ISC_R_SUCCESS) { fatal("dns_rdatatype_tounknowntext: %s", dns_result_totext(result)); } isc_buffer_putstr(&tbuf, "\t"); result = dns_rdata_tofmttext(&rdata, NULL, DNS_STYLEFLAG_UNKNOWNFORMAT, 0, 0, "", &tbuf); if (result != ISC_R_SUCCESS) { fatal("dns_rdata_tofmttext: %sn", dns_result_totext(result)); } printf("%.*s\n", (int)tbuf.used, (char*)tbuf.base); fflush(stdout); } isc_lex_close(lex); isc_lex_destroy(&lex); isc_mem_destroy(&mctx); return (0); }