/crypto/heimdal/lib/roken/resolve-test.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 185 lines · 130 code · 23 blank · 32 comment · 14 complexity · cb96666d06cb788e8cb6238e32a1fceb MD5 · raw file

  1. /*
  2. * Copyright (c) 1995 - 2004 Kungliga Tekniska Hรถgskolan
  3. * (Royal Institute of Technology, Stockholm, Sweden).
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. *
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. *
  17. * 3. Neither the name of the Institute nor the names of its contributors
  18. * may be used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
  22. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
  25. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. * SUCH DAMAGE.
  32. */
  33. #include <config.h>
  34. #include "roken.h"
  35. #include "getarg.h"
  36. #ifdef HAVE_ARPA_NAMESER_H
  37. #include <arpa/nameser.h>
  38. #endif
  39. #ifdef HAVE_RESOLV_H
  40. #include <resolv.h>
  41. #endif
  42. #include "resolve.h"
  43. static int loop_integer = 1;
  44. static int version_flag = 0;
  45. static int help_flag = 0;
  46. static struct getargs args[] = {
  47. {"loop", 0, arg_integer, &loop_integer,
  48. "loop resolving", NULL },
  49. {"version", 0, arg_flag, &version_flag,
  50. "print version", NULL },
  51. {"help", 0, arg_flag, &help_flag,
  52. NULL, NULL }
  53. };
  54. static void
  55. usage (int ret)
  56. {
  57. arg_printusage (args,
  58. sizeof(args)/sizeof(*args),
  59. NULL,
  60. "dns-record resource-record-type");
  61. exit (ret);
  62. }
  63. int
  64. main(int argc, char **argv)
  65. {
  66. struct rk_dns_reply *r;
  67. struct rk_resource_record *rr;
  68. int optidx = 0, i, exit_code = 0;
  69. setprogname (argv[0]);
  70. if(getarg(args, sizeof(args) / sizeof(args[0]), argc, argv, &optidx))
  71. usage(1);
  72. if (help_flag)
  73. usage (0);
  74. if(version_flag){
  75. printf("some version\n");
  76. exit(0);
  77. }
  78. argc -= optidx;
  79. argv += optidx;
  80. if (argc != 2)
  81. usage(1);
  82. for (i = 0; i < loop_integer; i++) {
  83. r = rk_dns_lookup(argv[0], argv[1]);
  84. if(r == NULL){
  85. printf("No reply.\n");
  86. exit_code = 1;
  87. break;
  88. }
  89. if(r->q.type == rk_ns_t_srv)
  90. rk_dns_srv_order(r);
  91. for(rr = r->head; rr;rr=rr->next){
  92. printf("%-30s %-5s %-6d ", rr->domain, rk_dns_type_to_string(rr->type), rr->ttl);
  93. switch(rr->type){
  94. case rk_ns_t_ns:
  95. case rk_ns_t_cname:
  96. case rk_ns_t_ptr:
  97. printf("%s\n", (char*)rr->u.data);
  98. break;
  99. case rk_ns_t_a:
  100. printf("%s\n", inet_ntoa(*rr->u.a));
  101. break;
  102. case rk_ns_t_mx:
  103. case rk_ns_t_afsdb:{
  104. printf("%d %s\n", rr->u.mx->preference, rr->u.mx->domain);
  105. break;
  106. }
  107. case rk_ns_t_srv:{
  108. struct rk_srv_record *srv = rr->u.srv;
  109. printf("%d %d %d %s\n", srv->priority, srv->weight,
  110. srv->port, srv->target);
  111. break;
  112. }
  113. case rk_ns_t_txt: {
  114. printf("%s\n", rr->u.txt);
  115. break;
  116. }
  117. case rk_ns_t_sig : {
  118. struct rk_sig_record *sig = rr->u.sig;
  119. const char *type_string = rk_dns_type_to_string (sig->type);
  120. printf ("type %u (%s), algorithm %u, labels %u, orig_ttl %u, sig_expiration %u, sig_inception %u, key_tag %u, signer %s\n",
  121. sig->type, type_string ? type_string : "",
  122. sig->algorithm, sig->labels, sig->orig_ttl,
  123. sig->sig_expiration, sig->sig_inception, sig->key_tag,
  124. sig->signer);
  125. break;
  126. }
  127. case rk_ns_t_key : {
  128. struct rk_key_record *key = rr->u.key;
  129. printf ("flags %u, protocol %u, algorithm %u\n",
  130. key->flags, key->protocol, key->algorithm);
  131. break;
  132. }
  133. case rk_ns_t_sshfp : {
  134. struct rk_sshfp_record *sshfp = rr->u.sshfp;
  135. size_t i;
  136. printf ("alg %u type %u length %lu data ", sshfp->algorithm,
  137. sshfp->type, (unsigned long)sshfp->sshfp_len);
  138. for (i = 0; i < sshfp->sshfp_len; i++)
  139. printf("%02X", sshfp->sshfp_data[i]);
  140. printf("\n");
  141. break;
  142. }
  143. case rk_ns_t_ds : {
  144. struct rk_ds_record *ds = rr->u.ds;
  145. size_t i;
  146. printf ("key tag %u alg %u type %u length %lu data ",
  147. ds->key_tag, ds->algorithm, ds->digest_type,
  148. (unsigned long)ds->digest_len);
  149. for (i = 0; i < ds->digest_len; i++)
  150. printf("%02X", ds->digest_data[i]);
  151. printf("\n");
  152. break;
  153. }
  154. default:
  155. printf("\n");
  156. break;
  157. }
  158. }
  159. rk_dns_free_data(r);
  160. }
  161. return exit_code;
  162. }