etc/services - sync with NetBSD-8
[minix.git] / external / bsd / bind / dist / bin / tests / rdata_test.c
blobe8ed4a8c819e1f04566f682146147fbdcadc2297
1 /* $NetBSD: rdata_test.c,v 1.8 2014/12/10 04:37:53 christos Exp $ */
3 /*
4 * Copyright (C) 2004-2007, 2011, 2012, 2014 Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 1998-2003 Internet Software Consortium.
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
20 #include <config.h>
22 #include <stdlib.h>
24 #include <isc/buffer.h>
25 #include <isc/commandline.h>
26 #include <isc/lex.h>
27 #include <isc/mem.h>
28 #include <isc/string.h>
29 #include <isc/util.h>
31 #include <dns/rdata.h>
32 #include <dns/compress.h>
33 #include <dns/rdataclass.h>
34 #include <dns/rdatastruct.h>
35 #include <dns/rdatatype.h>
36 #include <dns/result.h>
38 isc_mem_t *mctx;
39 isc_lex_t *lex;
41 isc_lexspecials_t specials;
43 static void
44 viastruct(dns_rdata_t *rdata, isc_mem_t *mctx,
45 dns_rdata_t *rdata2, isc_buffer_t *b)
47 isc_result_t result;
48 void *sp = NULL;
49 isc_boolean_t need_free = ISC_FALSE;
50 dns_rdatatype_t rdt;
51 dns_rdataclass_t rdc;
53 UNUSED(rdata2); /* XXXMPA remove when fromstruct is ready. */
54 UNUSED(b);
56 switch (rdata->type) {
57 case dns_rdatatype_a6: {
58 static dns_rdata_in_a6_t in_a6;
59 result = dns_rdata_tostruct(rdata, sp = &in_a6, NULL);
60 break;
62 case dns_rdatatype_a: {
63 switch (rdata->rdclass) {
64 case dns_rdataclass_hs: {
65 static dns_rdata_hs_a_t hs_a;
66 result = dns_rdata_tostruct(rdata, sp = &hs_a, NULL);
67 break;
69 case dns_rdataclass_in: {
70 static dns_rdata_in_a_t in_a;
71 result = dns_rdata_tostruct(rdata, sp = &in_a, NULL);
72 break;
74 default:
75 result = ISC_R_NOTIMPLEMENTED;
76 break;
78 break;
80 case dns_rdatatype_aaaa: {
81 static dns_rdata_in_aaaa_t in_aaaa;
82 result = dns_rdata_tostruct(rdata, sp = &in_aaaa, NULL);
83 break;
85 case dns_rdatatype_afsdb: {
86 static dns_rdata_afsdb_t afsdb;
87 result = dns_rdata_tostruct(rdata, sp = &afsdb, NULL);
88 break;
90 case dns_rdatatype_any: {
91 result = ISC_R_NOTIMPLEMENTED;
92 break;
94 case dns_rdatatype_apl: {
95 switch (rdata->rdclass) {
96 case dns_rdataclass_in: {
97 static dns_rdata_in_apl_t in_apl;
98 result = dns_rdata_tostruct(rdata, sp = &in_apl, NULL);
99 break;
101 default:
102 result = ISC_R_NOTIMPLEMENTED;
103 break;
105 break;
107 case dns_rdatatype_cert: {
108 static dns_rdata_cert_t cert;
109 result = dns_rdata_tostruct(rdata, sp = &cert, NULL);
110 break;
112 case dns_rdatatype_cname: {
113 static dns_rdata_cname_t cname;
114 result = dns_rdata_tostruct(rdata, sp = &cname, NULL);
115 break;
117 case dns_rdatatype_dname: {
118 static dns_rdata_dname_t dname;
119 result = dns_rdata_tostruct(rdata, sp = &dname, NULL);
120 break;
122 case dns_rdatatype_gpos: {
123 static dns_rdata_gpos_t gpos;
124 result = dns_rdata_tostruct(rdata, sp = &gpos, NULL);
125 break;
127 case dns_rdatatype_hinfo: {
128 static dns_rdata_hinfo_t hinfo;
129 result = dns_rdata_tostruct(rdata, sp = &hinfo, NULL);
130 break;
132 case dns_rdatatype_isdn: {
133 static dns_rdata_isdn_t isdn;
134 result = dns_rdata_tostruct(rdata, sp = &isdn, NULL);
135 break;
137 case dns_rdatatype_key: {
138 static dns_rdata_key_t key;
139 result = dns_rdata_tostruct(rdata, sp = &key, NULL);
140 break;
142 case dns_rdatatype_kx: {
143 static dns_rdata_in_kx_t in_kx;
144 result = dns_rdata_tostruct(rdata, sp = &in_kx, NULL);
145 break;
147 case dns_rdatatype_loc: {
148 static dns_rdata_loc_t loc;
149 result = dns_rdata_tostruct(rdata, sp = &loc, NULL);
150 break;
152 case dns_rdatatype_mb: {
153 static dns_rdata_mb_t mb;
154 result = dns_rdata_tostruct(rdata, sp = &mb, NULL);
155 break;
157 case dns_rdatatype_md: {
158 static dns_rdata_md_t md;
159 result = dns_rdata_tostruct(rdata, sp = &md, NULL);
160 break;
162 case dns_rdatatype_mf: {
163 static dns_rdata_mf_t mf;
164 result = dns_rdata_tostruct(rdata, sp = &mf, NULL);
165 break;
167 case dns_rdatatype_mg: {
168 static dns_rdata_mg_t mg;
169 result = dns_rdata_tostruct(rdata, sp = &mg, NULL);
170 break;
172 case dns_rdatatype_minfo: {
173 static dns_rdata_minfo_t minfo;
174 result = dns_rdata_tostruct(rdata, sp = &minfo, NULL);
175 break;
177 case dns_rdatatype_mr: {
178 static dns_rdata_mr_t mr;
179 result = dns_rdata_tostruct(rdata, sp = &mr, NULL);
180 break;
182 case dns_rdatatype_mx: {
183 static dns_rdata_mx_t mx;
184 result = dns_rdata_tostruct(rdata, sp = &mx, NULL);
185 break;
187 case dns_rdatatype_naptr: {
188 static dns_rdata_naptr_t naptr;
189 result = dns_rdata_tostruct(rdata, sp = &naptr, NULL);
190 break;
192 case dns_rdatatype_ns: {
193 static dns_rdata_ns_t ns;
194 result = dns_rdata_tostruct(rdata, sp = &ns, NULL);
195 break;
197 case dns_rdatatype_nsap: {
198 static dns_rdata_in_nsap_t in_nsap;
199 result = dns_rdata_tostruct(rdata, sp = &in_nsap, NULL);
200 break;
202 case dns_rdatatype_nsap_ptr: {
203 static dns_rdata_in_nsap_ptr_t in_nsap_ptr;
204 result = dns_rdata_tostruct(rdata, sp = &in_nsap_ptr, NULL);
205 break;
207 case dns_rdatatype_null: {
208 static dns_rdata_null_t null;
209 result = dns_rdata_tostruct(rdata, sp = &null, NULL);
210 break;
212 case dns_rdatatype_nxt: {
213 static dns_rdata_nxt_t nxt;
214 result = dns_rdata_tostruct(rdata, sp = &nxt, NULL);
215 break;
217 case dns_rdatatype_opt: {
218 static dns_rdata_opt_t opt;
219 result = dns_rdata_tostruct(rdata, sp = &opt, NULL);
220 break;
222 case dns_rdatatype_ptr: {
223 static dns_rdata_ptr_t ptr;
224 result = dns_rdata_tostruct(rdata, sp = &ptr, NULL);
225 break;
227 case dns_rdatatype_px: {
228 static dns_rdata_in_px_t in_px;
229 result = dns_rdata_tostruct(rdata, sp = &in_px, NULL);
230 break;
232 case dns_rdatatype_rp: {
233 static dns_rdata_rp_t rp;
234 result = dns_rdata_tostruct(rdata, sp = &rp, NULL);
235 break;
237 case dns_rdatatype_rt: {
238 static dns_rdata_rt_t rt;
239 result = dns_rdata_tostruct(rdata, sp = &rt, NULL);
240 break;
242 case dns_rdatatype_sig: {
243 static dns_rdata_sig_t sig;
244 result = dns_rdata_tostruct(rdata, sp = &sig, NULL);
245 break;
247 case dns_rdatatype_soa: {
248 static dns_rdata_soa_t soa;
249 result = dns_rdata_tostruct(rdata, sp = &soa, NULL);
250 break;
252 case dns_rdatatype_srv: {
253 static dns_rdata_in_srv_t in_srv;
254 result = dns_rdata_tostruct(rdata, sp = &in_srv, NULL);
255 break;
257 case dns_rdatatype_tkey: {
258 static dns_rdata_tkey_t tkey;
259 result = dns_rdata_tostruct(rdata, sp = &tkey, NULL);
260 break;
262 case dns_rdatatype_tsig: {
263 static dns_rdata_any_tsig_t tsig;
264 result = dns_rdata_tostruct(rdata, sp = &tsig, NULL);
265 break;
267 case dns_rdatatype_txt: {
268 static dns_rdata_txt_t txt;
269 result = dns_rdata_tostruct(rdata, sp = &txt, NULL);
270 break;
272 case dns_rdatatype_spf: {
273 static dns_rdata_spf_t spf;
274 result = dns_rdata_tostruct(rdata, sp = &spf, NULL);
275 break;
277 case dns_rdatatype_unspec: {
278 static dns_rdata_unspec_t unspec;
279 result = dns_rdata_tostruct(rdata, sp = &unspec, NULL);
280 break;
282 case dns_rdatatype_uri: {
283 static dns_rdata_uri_t uri;
284 result = dns_rdata_tostruct(rdata, sp = &uri, NULL);
285 break;
287 case dns_rdatatype_caa: {
288 static dns_rdata_caa_t caa;
289 result = dns_rdata_tostruct(rdata, sp = &caa, NULL);
290 break;
292 case dns_rdatatype_wks: {
293 static dns_rdata_in_wks_t in_wks;
294 result = dns_rdata_tostruct(rdata, sp = &in_wks, NULL);
295 break;
297 case dns_rdatatype_x25: {
298 static dns_rdata_x25_t x25;
299 result = dns_rdata_tostruct(rdata, sp = &x25, NULL);
300 break;
302 case dns_rdatatype_nsec: {
303 static dns_rdata_nsec_t nsec;
304 result = dns_rdata_tostruct(rdata, sp = &nsec, NULL);
305 break;
307 case dns_rdatatype_rrsig: {
308 static dns_rdata_rrsig_t rrsig;
309 result = dns_rdata_tostruct(rdata, sp = &rrsig, NULL);
310 break;
312 case dns_rdatatype_dnskey: {
313 static dns_rdata_dnskey_t dnskey;
314 result = dns_rdata_tostruct(rdata, sp = &dnskey, NULL);
315 break;
317 default:
318 result = ISC_R_NOTIMPLEMENTED;
319 break;
321 if (result != ISC_R_SUCCESS)
322 fprintf(stdout, "viastruct: tostruct %d %d return %s\n",
323 rdata->type, rdata->rdclass,
324 dns_result_totext(result));
325 else
326 dns_rdata_freestruct(sp);
328 switch (rdata->type) {
329 case dns_rdatatype_a6: {
330 static dns_rdata_in_a6_t in_a6;
331 result = dns_rdata_tostruct(rdata, sp = &in_a6, mctx);
332 break;
334 case dns_rdatatype_a: {
335 switch (rdata->rdclass) {
336 case dns_rdataclass_hs: {
337 static dns_rdata_hs_a_t hs_a;
338 result = dns_rdata_tostruct(rdata, sp = &hs_a, mctx);
339 break;
341 case dns_rdataclass_in: {
342 static dns_rdata_in_a_t in_a;
343 result = dns_rdata_tostruct(rdata, sp = &in_a, mctx);
344 break;
346 default:
347 result = ISC_R_NOTIMPLEMENTED;
348 break;
350 break;
352 case dns_rdatatype_aaaa: {
353 static dns_rdata_in_aaaa_t in_aaaa;
354 result = dns_rdata_tostruct(rdata, sp = &in_aaaa, mctx);
355 break;
357 case dns_rdatatype_afsdb: {
358 static dns_rdata_afsdb_t afsdb;
359 result = dns_rdata_tostruct(rdata, sp = &afsdb, mctx);
360 break;
362 case dns_rdatatype_any: {
363 result = ISC_R_NOTIMPLEMENTED;
364 break;
366 case dns_rdatatype_apl: {
367 switch (rdata->rdclass) {
368 case dns_rdataclass_in: {
369 static dns_rdata_in_apl_t in_apl;
370 result = dns_rdata_tostruct(rdata, sp = &in_apl, mctx);
371 break;
373 default:
374 result = ISC_R_NOTIMPLEMENTED;
375 break;
377 break;
379 case dns_rdatatype_cert: {
380 static dns_rdata_cert_t cert;
381 result = dns_rdata_tostruct(rdata, sp = &cert, mctx);
382 break;
384 case dns_rdatatype_cname: {
385 static dns_rdata_cname_t cname;
386 result = dns_rdata_tostruct(rdata, sp = &cname, mctx);
387 break;
389 case dns_rdatatype_dname: {
390 static dns_rdata_dname_t dname;
391 result = dns_rdata_tostruct(rdata, sp = &dname, mctx);
392 break;
394 case dns_rdatatype_gpos: {
395 static dns_rdata_gpos_t gpos;
396 result = dns_rdata_tostruct(rdata, sp = &gpos, mctx);
397 break;
399 case dns_rdatatype_hinfo: {
400 static dns_rdata_hinfo_t hinfo;
401 result = dns_rdata_tostruct(rdata, sp = &hinfo, mctx);
402 break;
404 case dns_rdatatype_isdn: {
405 static dns_rdata_isdn_t isdn;
406 result = dns_rdata_tostruct(rdata, sp = &isdn, mctx);
407 break;
409 case dns_rdatatype_key: {
410 static dns_rdata_key_t key;
411 result = dns_rdata_tostruct(rdata, sp = &key, mctx);
412 break;
414 case dns_rdatatype_kx: {
415 static dns_rdata_in_kx_t in_kx;
416 result = dns_rdata_tostruct(rdata, sp = &in_kx, mctx);
417 break;
419 case dns_rdatatype_loc: {
420 static dns_rdata_loc_t loc;
421 result = dns_rdata_tostruct(rdata, sp = &loc, mctx);
422 break;
424 case dns_rdatatype_mb: {
425 static dns_rdata_mb_t mb;
426 result = dns_rdata_tostruct(rdata, sp = &mb, mctx);
427 break;
429 case dns_rdatatype_md: {
430 static dns_rdata_md_t md;
431 result = dns_rdata_tostruct(rdata, sp = &md, mctx);
432 break;
434 case dns_rdatatype_mf: {
435 static dns_rdata_mf_t mf;
436 result = dns_rdata_tostruct(rdata, sp = &mf, mctx);
437 break;
439 case dns_rdatatype_mg: {
440 static dns_rdata_mg_t mg;
441 result = dns_rdata_tostruct(rdata, sp = &mg, mctx);
442 break;
444 case dns_rdatatype_minfo: {
445 static dns_rdata_minfo_t minfo;
446 result = dns_rdata_tostruct(rdata, sp = &minfo, mctx);
447 break;
449 case dns_rdatatype_mr: {
450 static dns_rdata_mr_t mr;
451 result = dns_rdata_tostruct(rdata, sp = &mr, mctx);
452 break;
454 case dns_rdatatype_mx: {
455 static dns_rdata_mx_t mx;
456 result = dns_rdata_tostruct(rdata, sp = &mx, mctx);
457 break;
459 case dns_rdatatype_naptr: {
460 static dns_rdata_naptr_t naptr;
461 result = dns_rdata_tostruct(rdata, sp = &naptr, mctx);
462 break;
464 case dns_rdatatype_ns: {
465 static dns_rdata_ns_t ns;
466 result = dns_rdata_tostruct(rdata, sp = &ns, mctx);
467 break;
469 case dns_rdatatype_nsap: {
470 static dns_rdata_in_nsap_t in_nsap;
471 result = dns_rdata_tostruct(rdata, sp = &in_nsap, mctx);
472 break;
474 case dns_rdatatype_nsap_ptr: {
475 static dns_rdata_in_nsap_ptr_t in_nsap_ptr;
476 result = dns_rdata_tostruct(rdata, sp = &in_nsap_ptr, mctx);
477 break;
479 case dns_rdatatype_null: {
480 static dns_rdata_null_t null;
481 result = dns_rdata_tostruct(rdata, sp = &null, mctx);
482 break;
484 case dns_rdatatype_nxt: {
485 static dns_rdata_nxt_t nxt;
486 result = dns_rdata_tostruct(rdata, sp = &nxt, mctx);
487 break;
489 case dns_rdatatype_opt: {
490 static dns_rdata_opt_t opt;
491 result = dns_rdata_tostruct(rdata, sp = &opt, mctx);
492 break;
494 case dns_rdatatype_ptr: {
495 static dns_rdata_ptr_t ptr;
496 result = dns_rdata_tostruct(rdata, sp = &ptr, mctx);
497 break;
499 case dns_rdatatype_px: {
500 static dns_rdata_in_px_t in_px;
501 result = dns_rdata_tostruct(rdata, sp = &in_px, mctx);
502 break;
504 case dns_rdatatype_rp: {
505 static dns_rdata_rp_t rp;
506 result = dns_rdata_tostruct(rdata, sp = &rp, mctx);
507 break;
509 case dns_rdatatype_rt: {
510 static dns_rdata_rt_t rt;
511 result = dns_rdata_tostruct(rdata, sp = &rt, mctx);
512 break;
514 case dns_rdatatype_sig: {
515 static dns_rdata_sig_t sig;
516 result = dns_rdata_tostruct(rdata, sp = &sig, mctx);
517 break;
519 case dns_rdatatype_soa: {
520 static dns_rdata_soa_t soa;
521 result = dns_rdata_tostruct(rdata, sp = &soa, mctx);
522 break;
524 case dns_rdatatype_srv: {
525 static dns_rdata_in_srv_t in_srv;
526 result = dns_rdata_tostruct(rdata, sp = &in_srv, mctx);
527 break;
529 case dns_rdatatype_tkey: {
530 static dns_rdata_tkey_t tkey;
531 result = dns_rdata_tostruct(rdata, sp = &tkey, mctx);
532 break;
534 case dns_rdatatype_tsig: {
535 static dns_rdata_any_tsig_t tsig;
536 result = dns_rdata_tostruct(rdata, sp = &tsig, mctx);
537 break;
539 case dns_rdatatype_txt: {
540 static dns_rdata_txt_t txt;
541 result = dns_rdata_tostruct(rdata, sp = &txt, mctx);
542 break;
544 case dns_rdatatype_spf: {
545 static dns_rdata_spf_t spf;
546 result = dns_rdata_tostruct(rdata, sp = &spf, mctx);
547 break;
549 case dns_rdatatype_unspec: {
550 static dns_rdata_unspec_t unspec;
551 result = dns_rdata_tostruct(rdata, sp = &unspec, mctx);
552 break;
554 case dns_rdatatype_uri: {
555 static dns_rdata_uri_t uri;
556 result = dns_rdata_tostruct(rdata, sp = &uri, mctx);
557 break;
559 case dns_rdatatype_caa: {
560 static dns_rdata_caa_t caa;
561 result = dns_rdata_tostruct(rdata, sp = &caa, mctx);
562 break;
564 case dns_rdatatype_wks: {
565 static dns_rdata_in_wks_t in_wks;
566 result = dns_rdata_tostruct(rdata, sp = &in_wks, mctx);
567 break;
569 case dns_rdatatype_x25: {
570 static dns_rdata_x25_t x25;
571 result = dns_rdata_tostruct(rdata, sp = &x25, mctx);
572 break;
574 case dns_rdatatype_nsec: {
575 static dns_rdata_nsec_t nsec;
576 result = dns_rdata_tostruct(rdata, sp = &nsec, mctx);
577 break;
579 case dns_rdatatype_rrsig: {
580 static dns_rdata_rrsig_t rrsig;
581 result = dns_rdata_tostruct(rdata, sp = &rrsig, mctx);
582 break;
584 case dns_rdatatype_dnskey: {
585 static dns_rdata_dnskey_t dnskey;
586 result = dns_rdata_tostruct(rdata, sp = &dnskey, mctx);
587 break;
589 default:
590 result = ISC_R_NOTIMPLEMENTED;
591 break;
593 if (result != ISC_R_SUCCESS)
594 fprintf(stdout, "viastruct: tostruct %d %d return %s\n",
595 rdata->type, rdata->rdclass,
596 dns_result_totext(result));
597 else {
598 need_free = ISC_TRUE;
600 rdc = rdata->rdclass;
601 rdt = rdata->type;
602 result = dns_rdata_fromstruct(rdata2, rdc, rdt, sp, b);
603 if (result != ISC_R_SUCCESS)
604 fprintf(stdout,
605 "viastruct: fromstruct %d %d return %s\n",
606 rdata->type, rdata->rdclass,
607 dns_result_totext(result));
608 else if (rdata->length != rdata2->length ||
609 memcmp(rdata->data, rdata2->data, rdata->length) != 0)
611 isc_uint32_t i;
612 isc_uint32_t l;
614 fprintf(stdout, "viastruct: memcmp failed\n");
616 fprintf(stdout, "%d %d\n",
617 rdata->length, rdata2->length);
618 l = rdata->length;
619 if (rdata2->length < l)
620 l = rdata2->length;
621 for (i = 0; i < l; i++)
622 fprintf(stdout, "%02x %02x\n",
623 rdata->data[i], rdata2->data[i]);
626 #if 0
627 switch (rdata->type) {
628 case dns_rdatatype_a6: {
629 dns_rdata_in_a6_t in_a6;
630 result = dns_rdata_fromstruct(rdata2, rdc, rdt, &in_a6, b);
631 break;
633 case dns_rdatatype_a: {
634 switch (rdata->rdclass) {
635 case dns_rdataclass_hs: {
636 dns_rdata_hs_a_t hs_a;
637 result = dns_rdata_fromstruct(rdata2, rdc, rdt,
638 &hs_a, b);
639 break;
641 case dns_rdataclass_in: {
642 dns_rdata_in_a_t in_a;
643 result = dns_rdata_fromstruct(rdata2, rdc, rdt,
644 &in_a, b);
645 break;
647 default:
648 result = ISC_R_NOTIMPLEMENTED;
649 break;
651 break;
653 case dns_rdatatype_aaaa: {
654 dns_rdata_in_aaaa_t in_aaaa;
655 result = dns_rdata_fromstruct(rdata2, rdc, rdt, &in_aaaa, b);
656 break;
658 case dns_rdatatype_afsdb: {
659 dns_rdata_afsdb_t afsdb;
660 result = dns_rdata_fromstruct(rdata2, rdc, rdt, &afsdb, b);
661 break;
663 case dns_rdatatype_any: {
664 result = ISC_R_NOTIMPLEMENTED;
665 break;
667 case dns_rdatatype_apl: {
668 switch (rdata->rdclass) {
669 case dns_rdataclass_in: {
670 dns_rdata_in_apl_t in_apl;
671 result = dns_rdata_fromstruct(rdata, rdc, rdt, &in_apl, b);
672 break;
674 default:
675 result = ISC_R_NOTIMPLEMENTED;
676 break;
678 break;
680 case dns_rdatatype_cert: {
681 dns_rdata_cert_t cert;
682 result = dns_rdata_fromstruct(rdata2, rdc, rdt, &cert, b);
683 break;
685 case dns_rdatatype_cname: {
686 dns_rdata_cname_t cname;
687 result = dns_rdata_fromstruct(rdata2, rdc, rdt, &cname, b);
688 break;
690 case dns_rdatatype_dname: {
691 dns_rdata_dname_t dname;
692 result = dns_rdata_fromstruct(rdata2, rdc, rdt, &dname, b);
693 break;
695 case dns_rdatatype_gpos: {
696 dns_rdata_gpos_t gpos;
697 result = dns_rdata_fromstruct(rdata2, rdc, rdt, &gpos, b);
698 break;
700 case dns_rdatatype_hinfo: {
701 dns_rdata_hinfo_t hinfo;
702 result = dns_rdata_fromstruct(rdata2, rdc, rdt, &hinfo, b);
703 break;
705 case dns_rdatatype_isdn: {
706 dns_rdata_isdn_t isdn;
707 result = dns_rdata_fromstruct(rdata2, rdc, rdt, &isdn, b);
708 break;
710 case dns_rdatatype_key: {
711 dns_rdata_key_t key;
712 result = dns_rdata_fromstruct(rdata2, rdc, rdt, &key, b);
713 break;
715 case dns_rdatatype_kx: {
716 dns_rdata_in_kx_t in_kx;
717 result = dns_rdata_fromstruct(rdata2, rdc, rdt, &in_kx, b);
718 break;
720 case dns_rdatatype_loc: {
721 dns_rdata_loc_t loc;
722 result = dns_rdata_fromstruct(rdata2, rdc, rdt, &loc, b);
723 break;
725 case dns_rdatatype_mb: {
726 dns_rdata_mb_t mb;
727 result = dns_rdata_fromstruct(rdata2, rdc, rdt, &mb, b);
728 break;
730 case dns_rdatatype_md: {
731 dns_rdata_md_t md;
732 result = dns_rdata_fromstruct(rdata2, rdc, rdt, &md, b);
733 break;
735 case dns_rdatatype_mf: {
736 dns_rdata_mf_t mf;
737 result = dns_rdata_fromstruct(rdata2, rdc, rdt, &mf, b);
738 break;
740 case dns_rdatatype_mg: {
741 dns_rdata_mg_t mg;
742 result = dns_rdata_fromstruct(rdata2, rdc, rdt, &mg, b);
743 break;
745 case dns_rdatatype_minfo: {
746 dns_rdata_minfo_t minfo;
747 result = dns_rdata_fromstruct(rdata2, rdc, rdt, &minfo, b);
748 break;
750 case dns_rdatatype_mr: {
751 dns_rdata_mr_t mr;
752 result = dns_rdata_fromstruct(rdata2, rdc, rdt, &mr, b);
753 break;
755 case dns_rdatatype_mx: {
756 dns_rdata_mx_t mx;
757 result = dns_rdata_fromstruct(rdata2, rdc, rdt, &mx, b);
758 break;
760 case dns_rdatatype_naptr: {
761 dns_rdata_naptr_t naptr;
762 result = dns_rdata_fromstruct(rdata2, rdc, rdt, &naptr, b);
763 break;
765 case dns_rdatatype_ns: {
766 dns_rdata_ns_t ns;
767 result = dns_rdata_fromstruct(rdata2, rdc, rdt, &ns, b);
768 break;
770 case dns_rdatatype_nsap: {
771 dns_rdata_in_nsap_t in_nsap;
772 result = dns_rdata_fromstruct(rdata2, rdc, rdt, &in_nsap, b);
773 break;
775 case dns_rdatatype_nsap_ptr: {
776 dns_rdata_in_nsap_ptr_t in_nsap_ptr;
777 result = dns_rdata_fromstruct(rdata2, rdc, rdt, &in_nsap_ptr,
779 break;
781 case dns_rdatatype_null: {
782 dns_rdata_null_t null;
783 result = dns_rdata_fromstruct(rdata2, rdc, rdt, &null, b);
784 break;
786 case dns_rdatatype_nxt: {
787 dns_rdata_nxt_t nxt;
788 result = dns_rdata_fromstruct(rdata2, rdc, rdt, &nxt, b);
789 break;
791 case dns_rdatatype_opt: {
792 dns_rdata_opt_t opt;
793 result = dns_rdata_fromstruct(rdata2, rdc, rdt, &opt, b);
794 break;
796 case dns_rdatatype_ptr: {
797 dns_rdata_ptr_t ptr;
798 result = dns_rdata_fromstruct(rdata2, rdc, rdt, &ptr, b);
799 break;
801 case dns_rdatatype_px: {
802 dns_rdata_in_px_t in_px;
803 result = dns_rdata_fromstruct(rdata2, rdc, rdt, &in_px, b);
804 break;
806 case dns_rdatatype_rp: {
807 dns_rdata_rp_t rp;
808 result = dns_rdata_fromstruct(rdata2, rdc, rdt, &rp, b);
809 break;
811 case dns_rdatatype_rt: {
812 dns_rdata_rt_t rt;
813 result = dns_rdata_fromstruct(rdata2, rdc, rdt, &rt, b);
814 break;
816 case dns_rdatatype_sig: {
817 dns_rdata_sig_t sig;
818 result = dns_rdata_fromstruct(rdata2, rdc, rdt, &sig, b);
819 break;
821 case dns_rdatatype_soa: {
822 dns_rdata_soa_t soa;
823 result = dns_rdata_fromstruct(rdata2, rdc, rdt, &soa, b);
824 break;
826 case dns_rdatatype_srv: {
827 dns_rdata_in_srv_t in_srv;
828 result = dns_rdata_fromstruct(rdata2, rdc, rdt, &in_srv, b);
829 break;
831 case dns_rdatatype_tkey: {
832 dns_rdata_tkey_t tkey;
833 result = dns_rdata_fromstruct(rdata2, rdc, rdt, &tkey, b);
834 break;
836 case dns_rdatatype_tsig: {
837 dns_rdata_any_tsig_t tsig;
838 result = dns_rdata_fromstruct(rdata2, rdc, rdt, &tsig, b);
839 break;
841 case dns_rdatatype_txt: {
842 dns_rdata_txt_t txt;
843 result = dns_rdata_fromstruct(rdata2, rdc, rdt, &txt, b);
844 break;
846 case dns_rdatatype_spf: {
847 dns_rdata_spf_t spf;
848 result = dns_rdata_fromstruct(rdata2, rdc, rdt, &spf, b);
849 break;
851 case dns_rdatatype_unspec: {
852 dns_rdata_unspec_t unspec;
853 result = dns_rdata_fromstruct(rdata2, rdc, rdt, &unspec, b);
854 break;
856 case dns_rdatatype_uri: {
857 dns_rdata_uri_t uri;
858 result = dns_rdata_fromstruct(rdata2, rdc, rdt, &uri, b);
859 break;
861 case dns_rdatatype_caa: {
862 dns_rdata_caa_t caa;
863 result = dns_rdata_fromstruct(rdata2, rdc, rdt, &caa, b);
864 break;
866 case dns_rdatatype_wks: {
867 dns_rdata_in_wks_t in_wks;
868 result = dns_rdata_fromstruct(rdata2, rdc, rdt, &in_wks, b);
869 break;
871 case dns_rdatatype_x25: {
872 dns_rdata_x25_t x25;
873 result = dns_rdata_fromstruct(rdata2, rdc, rdt, &x25, b);
874 break;
876 case dns_rdatatype_nsec: {
877 dns_rdata_nsec_t nsec;
878 result = dns_rdata_fromstruct(rdata2, rdc, rdt, &nsec, b);
879 break;
881 case dns_rdatatype_rrsig: {
882 dns_rdata_rrsig_t rrsig;
883 result = dns_rdata_fromstruct(rdata2, rdc, rdt, &rrsig, b);
884 break;
886 case dns_rdatatype_dnskey: {
887 dns_rdata_dnskey_t dnskey;
888 result = dns_rdata_fromstruct(rdata2, rdc, rdt, &dnskey, b);
889 break;
891 default:
892 result = ISC_R_NOTIMPLEMENTED;
893 break;
895 #endif
896 if (need_free)
897 dns_rdata_freestruct(sp);
901 main(int argc, char *argv[]) {
902 isc_token_t token;
903 isc_result_t result;
904 int quiet = 0;
905 int c;
906 int stats = 0;
907 unsigned int options = 0;
908 dns_rdatatype_t type;
909 dns_rdataclass_t class;
910 dns_rdatatype_t lasttype = 0;
911 char outbuf[16*1024];
912 char inbuf[16*1024];
913 char wirebuf[16*1024];
914 char viabuf[16*1024];
915 isc_buffer_t dbuf;
916 isc_buffer_t tbuf;
917 isc_buffer_t wbuf;
918 dns_rdata_t rdata = DNS_RDATA_INIT;
919 dns_rdata_t last = DNS_RDATA_INIT;
920 int need_eol = 0;
921 int wire = 0;
922 dns_compress_t cctx;
923 dns_decompress_t dctx;
924 int trunc = 0;
925 int add = 0;
926 int len;
927 int zero = 0;
928 int debug = 0;
929 isc_region_t region;
930 int first = 1;
931 int raw = 0;
932 int tostruct = 0;
934 while ((c = isc_commandline_parse(argc, argv, "dqswtarzS")) != -1) {
935 switch (c) {
936 case 'd':
937 debug = 1;
938 quiet = 0;
939 break;
940 case 'q':
941 quiet = 1;
942 debug = 0;
943 break;
944 case 's':
945 stats = 1;
946 break;
947 case 'w':
948 wire = 1;
949 break;
950 case 't':
951 trunc = 1;
952 break;
953 case 'a':
954 add = 1;
955 break;
956 case 'z':
957 zero = 1;
958 break;
959 case 'r':
960 raw++;
961 break;
962 case 'S':
963 tostruct++;
964 break;
968 memset(&dctx, 0, sizeof(dctx));
969 dctx.allowed = DNS_COMPRESS_ALL;
971 RUNTIME_CHECK(isc_mem_create(0, 0, &mctx) == ISC_R_SUCCESS);
972 RUNTIME_CHECK(isc_lex_create(mctx, 256, &lex) == ISC_R_SUCCESS);
975 * Set up to lex DNS master file.
978 specials['('] = 1;
979 specials[')'] = 1;
980 specials['"'] = 1;
981 isc_lex_setspecials(lex, specials);
982 options = ISC_LEXOPT_EOL;
983 isc_lex_setcomments(lex, ISC_LEXCOMMENT_DNSMASTERFILE);
985 RUNTIME_CHECK(isc_lex_openstream(lex, stdin) == ISC_R_SUCCESS);
987 dns_rdata_init(&last);
988 while ((result = isc_lex_gettoken(lex, options | ISC_LEXOPT_NUMBER,
989 &token)) == ISC_R_SUCCESS) {
990 if (debug) fprintf(stdout, "token.type = %d\n", token.type);
991 if (need_eol) {
992 if (token.type == isc_tokentype_eol)
993 need_eol = 0;
994 continue;
996 if (token.type == isc_tokentype_eof)
997 break;
1000 * Get type.
1002 if (token.type == isc_tokentype_number) {
1003 type = token.value.as_ulong;
1004 isc_buffer_init(&tbuf, outbuf, sizeof(outbuf));
1005 result = dns_rdatatype_totext(type, &tbuf);
1006 if (result != ISC_R_SUCCESS) {
1007 fprintf(stdout,
1008 "dns_rdatatype_totext "
1009 "returned %s(%d)\n",
1010 dns_result_totext(result), result);
1011 fflush(stdout);
1012 need_eol = 1;
1013 continue;
1015 fprintf(stdout, "type = %.*s(%d)\n",
1016 (int)tbuf.used, (char*)tbuf.base, type);
1017 } else if (token.type == isc_tokentype_string) {
1018 result = dns_rdatatype_fromtext(&type,
1019 &token.value.as_textregion);
1020 if (result != ISC_R_SUCCESS) {
1021 fprintf(stdout,
1022 "dns_rdatatype_fromtext "
1023 "returned %s(%d)\n",
1024 dns_result_totext(result), result);
1025 fflush(stdout);
1026 need_eol = 1;
1027 continue;
1029 fprintf(stdout, "type = %.*s(%d)\n",
1030 (int)token.value.as_textregion.length,
1031 token.value.as_textregion.base, type);
1032 } else
1033 continue;
1035 result = isc_lex_gettoken(lex, options | ISC_LEXOPT_NUMBER,
1036 &token);
1037 if (result != ISC_R_SUCCESS)
1038 break;
1039 if (token.type == isc_tokentype_eol)
1040 continue;
1041 if (token.type == isc_tokentype_eof)
1042 break;
1043 if (token.type == isc_tokentype_number) {
1044 class = token.value.as_ulong;
1045 isc_buffer_init(&tbuf, outbuf, sizeof(outbuf));
1046 result = dns_rdatatype_totext(class, &tbuf);
1047 if (result != ISC_R_SUCCESS) {
1048 fprintf(stdout, "dns_rdatatype_totext "
1049 "returned %s(%d)\n",
1050 dns_result_totext(result), result);
1051 fflush(stdout);
1052 need_eol = 1;
1053 continue;
1055 fprintf(stdout, "class = %.*s(%d)\n",
1056 (int)tbuf.used, (char*)tbuf.base, class);
1057 } else if (token.type == isc_tokentype_string) {
1058 result = dns_rdataclass_fromtext(&class,
1059 &token.value.as_textregion);
1060 if (result != ISC_R_SUCCESS) {
1061 fprintf(stdout, "dns_rdataclass_fromtext "
1062 "returned %s(%d)\n",
1063 dns_result_totext(result), result);
1064 fflush(stdout);
1065 need_eol = 1;
1066 continue;
1068 fprintf(stdout, "class = %.*s(%d)\n",
1069 (int)token.value.as_textregion.length,
1070 token.value.as_textregion.base, class);
1071 } else
1072 continue;
1074 fflush(stdout);
1075 dns_rdata_init(&rdata);
1076 isc_buffer_init(&dbuf, inbuf, sizeof(inbuf));
1077 result = dns_rdata_fromtext(&rdata, class, type, lex,
1078 NULL, 0, mctx, &dbuf,
1079 NULL);
1080 if (result != ISC_R_SUCCESS) {
1081 fprintf(stdout,
1082 "dns_rdata_fromtext returned %s(%d)\n",
1083 dns_result_totext(result), result);
1084 fflush(stdout);
1085 continue;
1087 if (raw) {
1088 unsigned int i;
1089 for (i = 0; i < rdata.length; /* */ ) {
1090 fprintf(stdout, "%02x", rdata.data[i]);
1091 if ((++i % 20) == 0)
1092 fputs("\n", stdout);
1093 else
1094 if (i == rdata.length)
1095 fputs("\n", stdout);
1096 else
1097 fputs(" ", stdout);
1102 * Convert to wire and back?
1104 if (wire) {
1105 result = dns_compress_init(&cctx, -1, mctx);
1106 if (result != ISC_R_SUCCESS) {
1107 fprintf(stdout,
1108 "dns_compress_init returned %s(%d)\n",
1109 dns_result_totext(result), result);
1110 continue;
1112 isc_buffer_init(&wbuf, wirebuf, sizeof(wirebuf));
1113 result = dns_rdata_towire(&rdata, &cctx, &wbuf);
1114 dns_compress_invalidate(&cctx);
1115 if (result != ISC_R_SUCCESS) {
1116 fprintf(stdout,
1117 "dns_rdata_towire returned %s(%d)\n",
1118 dns_result_totext(result), result);
1119 continue;
1121 len = wbuf.used - wbuf.current;
1122 if (raw > 2) {
1123 unsigned int i;
1124 fputs("\n", stdout);
1125 for (i = 0; i < (unsigned int)len; /* */ ) {
1126 fprintf(stdout, "%02x",
1127 ((unsigned char*)wbuf.base)[i + wbuf.current]);
1128 if ((++i % 20) == 0)
1129 fputs("\n", stdout);
1130 else
1131 if (i == wbuf.used)
1132 fputs("\n", stdout);
1133 else
1134 fputs(" ", stdout);
1137 if (zero)
1138 len = 0;
1139 if (trunc)
1140 len = (len * 3) / 4;
1141 if (add) {
1142 isc_buffer_add(&wbuf, len / 4 + 1);
1143 len += len / 4 + 1;
1146 isc_buffer_setactive(&wbuf, len);
1147 dns_rdata_init(&rdata);
1148 isc_buffer_init(&dbuf, inbuf, sizeof(inbuf));
1149 dns_decompress_init(&dctx, -1, DNS_DECOMPRESS_ANY);
1150 result = dns_rdata_fromwire(&rdata, class, type, &wbuf,
1151 &dctx, 0, &dbuf);
1152 dns_decompress_invalidate(&dctx);
1153 if (result != ISC_R_SUCCESS) {
1154 fprintf(stdout,
1155 "dns_rdata_fromwire returned %s(%d)\n",
1156 dns_result_totext(result), result);
1157 fflush(stdout);
1158 continue;
1161 if (raw > 1) {
1162 unsigned int i;
1163 fputs("\n", stdout);
1164 for (i = 0; i < rdata.length; /* */ ) {
1165 fprintf(stdout, "%02x", rdata.data[i]);
1166 if ((++i % 20) == 0)
1167 fputs("\n", stdout);
1168 else
1169 if (i == rdata.length)
1170 fputs("\n", stdout);
1171 else
1172 fputs(" ", stdout);
1175 if (tostruct) {
1176 isc_mem_t *mctx2 = NULL;
1177 dns_rdata_t rdata2 = DNS_RDATA_INIT;
1178 isc_buffer_t vbuf;
1180 RUNTIME_CHECK(isc_mem_create(0, 0, &mctx2)
1181 == ISC_R_SUCCESS);
1183 isc_buffer_init(&vbuf, viabuf, sizeof(viabuf));
1184 dns_rdata_init(&rdata2);
1185 viastruct(&rdata, mctx2, &rdata2, &vbuf);
1186 if (!quiet && stats)
1187 isc_mem_stats(mctx2, stdout);
1188 isc_mem_destroy(&mctx2);
1191 isc_buffer_init(&tbuf, outbuf, sizeof(outbuf));
1192 result = dns_rdata_totext(&rdata, NULL, &tbuf);
1193 if (result != ISC_R_SUCCESS)
1194 fprintf(stdout, "dns_rdata_totext returned %s(%d)\n",
1195 dns_result_totext(result), result);
1196 else
1197 fprintf(stdout, "\"%.*s\"\n",
1198 (int)tbuf.used, (char*)tbuf.base);
1199 fflush(stdout);
1200 if (lasttype == type) {
1201 fprintf(stdout, "dns_rdata_compare = %d\n",
1202 dns_rdata_compare(&rdata, &last));
1205 if (!first) {
1206 free(last.data);
1208 dns_rdata_init(&last);
1209 region.base = malloc(region.length = rdata.length);
1210 if (region.base) {
1211 memmove(region.base, rdata.data, rdata.length);
1212 dns_rdata_fromregion(&last, class, type, &region);
1213 lasttype = type;
1214 first = 0;
1215 } else
1216 first = 1;
1219 if (result != ISC_R_EOF)
1220 printf("Result: %s\n", isc_result_totext(result));
1222 isc_lex_close(lex);
1223 isc_lex_destroy(&lex);
1224 if (!quiet && stats)
1225 isc_mem_stats(mctx, stdout);
1226 isc_mem_destroy(&mctx);
1228 return (0);