dnssec.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * Copyright (C) 2011-2012 Reto Guadagnini
  3. * HSR Hochschule fuer Technik Rapperswil
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the
  7. * Free Software Foundation; either version 2 of the License, or (at your
  8. * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  12. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  13. * for more details.
  14. */
  15. #include <stdio.h>
  16. #include <library.h>
  17. #include <utils/debug.h>
  18. /**
  19. * Define debug level
  20. */
  21. static level_t dbg_level = 1;
  22. static void dbg_dnssec(debug_t group, level_t level, char *fmt, ...)
  23. {
  24. if ((level <= dbg_level) || level <= 1)
  25. {
  26. va_list args;
  27. va_start(args, fmt);
  28. vfprintf(stderr, fmt, args);
  29. fprintf(stderr, "\n");
  30. va_end(args);
  31. }
  32. }
  33. int main(int argc, char *argv[])
  34. {
  35. resolver_t *resolver;
  36. resolver_response_t *response;
  37. enumerator_t *enumerator;
  38. chunk_t rdata;
  39. rr_set_t *rrset;
  40. rr_t *rr;
  41. library_init(NULL, "dnssec");
  42. atexit(library_deinit);
  43. dbg = dbg_dnssec;
  44. if (!lib->plugins->load(lib->plugins, PLUGINS))
  45. {
  46. return 1;
  47. }
  48. if (argc != 2)
  49. {
  50. fprintf(stderr, "usage: dnssec <name>\n");
  51. return 1;
  52. }
  53. resolver = lib->resolver->create(lib->resolver);
  54. if (!resolver)
  55. {
  56. printf("failed to create a resolver!\n");
  57. return 1;
  58. }
  59. response = resolver->query(resolver, argv[1], RR_CLASS_IN, RR_TYPE_A);
  60. if (!response)
  61. {
  62. printf("no response received!\n");
  63. resolver->destroy(resolver);
  64. return 1;
  65. }
  66. printf("DNS response:\n");
  67. if (!response->has_data(response) || !response->query_name_exist(response))
  68. {
  69. if (!response->has_data(response))
  70. {
  71. printf(" no data in the response\n");
  72. }
  73. if (!response->query_name_exist(response))
  74. {
  75. printf(" query name does not exist\n");
  76. }
  77. response->destroy(response);
  78. resolver->destroy(resolver);
  79. return 1;
  80. }
  81. printf(" RRs in the response:\n");
  82. rrset = response->get_rr_set(response);
  83. if (!rrset)
  84. {
  85. printf(" response contains no RRset!\n");
  86. response->destroy(response);
  87. resolver->destroy(resolver);
  88. return 1;
  89. }
  90. enumerator = rrset->create_rr_enumerator(rrset);
  91. while (enumerator->enumerate(enumerator, &rr))
  92. {
  93. printf(" name: %s\n", rr->get_name(rr));
  94. }
  95. enumerator = rrset->create_rrsig_enumerator(rrset);
  96. if (enumerator)
  97. {
  98. printf(" RRSIGs for the RRset:\n");
  99. while (enumerator->enumerate(enumerator, &rr))
  100. {
  101. rdata = rr->get_rdata(rr);
  102. printf(" name: %s\n", rr->get_name(rr));
  103. printf(" RDATA: %#B\n", &rdata);
  104. }
  105. }
  106. printf(" security status of the response: ");
  107. switch (response->get_security_state(response))
  108. {
  109. case SECURE:
  110. printf("SECURE\n\n");
  111. break;
  112. case INSECURE:
  113. printf("INSECURE\n\n");
  114. break;
  115. case BOGUS:
  116. printf("BOGUS\n\n");
  117. break;
  118. case INDETERMINATE:
  119. printf("INDETERMINATE\n\n");
  120. break;
  121. }
  122. response->destroy(response);
  123. resolver->destroy(resolver);
  124. return 0;
  125. }