gnunet-resolver.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2010 GNUnet e.V.
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @file util/gnunet-resolver.c
  18. * @brief tool to test resolver
  19. * @author Christian Grothoff
  20. */
  21. #include "platform.h"
  22. #include "gnunet_util_lib.h"
  23. #include "gnunet_resolver_service.h"
  24. #define GET_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
  25. /**
  26. * Flag for reverse lookup.
  27. */
  28. static int reverse;
  29. /**
  30. * Prints each hostname obtained from DNS.
  31. *
  32. * @param cls closure (unused)
  33. * @param hostname one of the names for the host, NULL
  34. * on the last call to the callback
  35. */
  36. static void
  37. print_hostname (void *cls,
  38. const char *hostname)
  39. {
  40. (void) cls;
  41. if (NULL == hostname)
  42. return;
  43. fprintf (stdout,
  44. "%s\n",
  45. hostname);
  46. }
  47. /**
  48. * Callback function to display address.
  49. *
  50. * @param cls closure (unused)
  51. * @param addr one of the addresses of the host, NULL for the last address
  52. * @param addrlen length of the address
  53. */
  54. static void
  55. print_sockaddr (void *cls,
  56. const struct sockaddr *addr,
  57. socklen_t addrlen)
  58. {
  59. (void) cls;
  60. if (NULL == addr)
  61. return;
  62. fprintf (stdout,
  63. "%s\n",
  64. GNUNET_a2s (addr,
  65. addrlen));
  66. }
  67. /**
  68. * Main function that will be run by the scheduler.
  69. *
  70. * @param cls closure
  71. * @param args remaining command-line arguments
  72. * @param cfgfile name of the configuration file used (for saving, can be NULL!)
  73. * @param cfg configuration
  74. */
  75. static void
  76. run (void *cls,
  77. char *const *args,
  78. const char *cfgfile,
  79. const struct GNUNET_CONFIGURATION_Handle *cfg)
  80. {
  81. const struct sockaddr *sa;
  82. socklen_t salen;
  83. struct sockaddr_in v4;
  84. struct sockaddr_in6 v6;
  85. (void) cls;
  86. (void) cfgfile;
  87. (void) cfg;
  88. if (NULL == args[0])
  89. return;
  90. if (! reverse)
  91. {
  92. GNUNET_RESOLVER_ip_get (args[0],
  93. AF_UNSPEC,
  94. GET_TIMEOUT,
  95. &print_sockaddr,
  96. NULL);
  97. return;
  98. }
  99. sa = NULL;
  100. memset (&v4, 0, sizeof(v4));
  101. v4.sin_family = AF_INET;
  102. #if HAVE_SOCKADDR_IN_SIN_LEN
  103. v4.sin_len = sizeof(v4);
  104. #endif
  105. if (1 == inet_pton (AF_INET,
  106. args[0],
  107. &v4.sin_addr))
  108. {
  109. sa = (struct sockaddr *) &v4;
  110. salen = sizeof(v4);
  111. }
  112. memset (&v6, 0, sizeof(v6));
  113. v6.sin6_family = AF_INET6;
  114. #if HAVE_SOCKADDR_IN_SIN_LEN
  115. v6.sin6_len = sizeof(v6);
  116. #endif
  117. if (1 == inet_pton (AF_INET6,
  118. args[0],
  119. &v6.sin6_addr))
  120. {
  121. sa = (struct sockaddr *) &v6;
  122. salen = sizeof(v6);
  123. }
  124. if (NULL == sa)
  125. {
  126. fprintf (stderr,
  127. "`%s' is not a valid IP: %s\n",
  128. args[0],
  129. strerror (errno));
  130. return;
  131. }
  132. GNUNET_RESOLVER_hostname_get (sa, salen,
  133. GNUNET_YES,
  134. GET_TIMEOUT,
  135. &print_hostname,
  136. NULL);
  137. }
  138. /**
  139. * The main function to access GNUnet's DNS resolver.
  140. *
  141. * @param argc number of arguments from the command line
  142. * @param argv command line arguments
  143. * @return 0 ok, 1 on error
  144. */
  145. int
  146. main (int argc, char *const *argv)
  147. {
  148. struct GNUNET_GETOPT_CommandLineOption options[] = {
  149. GNUNET_GETOPT_option_flag ('r',
  150. "reverse",
  151. gettext_noop ("perform a reverse lookup"),
  152. &reverse),
  153. GNUNET_GETOPT_OPTION_END
  154. };
  155. int ret;
  156. if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
  157. return 2;
  158. ret = (GNUNET_OK ==
  159. GNUNET_PROGRAM_run (argc, argv, "gnunet-resolver [hostname]",
  160. gettext_noop ("Use build-in GNUnet stub resolver"),
  161. options, &run, NULL)) ? 0 : 1;
  162. GNUNET_free ((void *) argv);
  163. return ret;
  164. }
  165. /* end of gnunet-resolver.c */