resolve.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2020, Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. ***************************************************************************/
  22. #include "server_setup.h"
  23. /* Purpose
  24. *
  25. * Resolve the given name, using system name resolve functions (NOT any
  26. * function provided by libcurl). Used to see if the name exists and thus if
  27. * we can allow a test case to use it for testing.
  28. *
  29. * Like if 'localhost' actual exists etc.
  30. *
  31. */
  32. #ifdef HAVE_SIGNAL_H
  33. #include <signal.h>
  34. #endif
  35. #ifdef HAVE_NETINET_IN_H
  36. #include <netinet/in.h>
  37. #endif
  38. #ifdef _XOPEN_SOURCE_EXTENDED
  39. /* This define is "almost" required to build on HPUX 11 */
  40. #include <arpa/inet.h>
  41. #endif
  42. #ifdef HAVE_NETDB_H
  43. #include <netdb.h>
  44. #endif
  45. #define ENABLE_CURLX_PRINTF
  46. /* make the curlx header define all printf() functions to use the curlx_*
  47. versions instead */
  48. #include "curlx.h" /* from the private lib dir */
  49. #include "util.h"
  50. /* include memdebug.h last */
  51. #include "memdebug.h"
  52. static bool use_ipv6 = FALSE;
  53. static const char *ipv_inuse = "IPv4";
  54. const char *serverlogfile = ""; /* for a util.c function we don't use */
  55. int main(int argc, char *argv[])
  56. {
  57. int arg = 1;
  58. const char *host = NULL;
  59. int rc = 0;
  60. while(argc>arg) {
  61. if(!strcmp("--version", argv[arg])) {
  62. printf("resolve IPv4%s\n",
  63. #if defined(CURLRES_IPV6)
  64. "/IPv6"
  65. #else
  66. ""
  67. #endif
  68. );
  69. return 0;
  70. }
  71. else if(!strcmp("--ipv6", argv[arg])) {
  72. ipv_inuse = "IPv6";
  73. use_ipv6 = TRUE;
  74. arg++;
  75. }
  76. else if(!strcmp("--ipv4", argv[arg])) {
  77. /* for completeness, we support this option as well */
  78. ipv_inuse = "IPv4";
  79. use_ipv6 = FALSE;
  80. arg++;
  81. }
  82. else {
  83. host = argv[arg++];
  84. }
  85. }
  86. if(!host) {
  87. puts("Usage: resolve [option] <host>\n"
  88. " --version\n"
  89. " --ipv4"
  90. #if defined(CURLRES_IPV6)
  91. "\n --ipv6"
  92. #endif
  93. );
  94. return 1;
  95. }
  96. #ifdef WIN32
  97. win32_init();
  98. atexit(win32_cleanup);
  99. #endif
  100. #if defined(CURLRES_IPV6)
  101. if(use_ipv6) {
  102. /* Check that the system has IPv6 enabled before checking the resolver */
  103. curl_socket_t s = socket(PF_INET6, SOCK_DGRAM, 0);
  104. if(s == CURL_SOCKET_BAD)
  105. /* an IPv6 address was requested and we can't get/use one */
  106. rc = -1;
  107. else {
  108. sclose(s);
  109. }
  110. }
  111. if(rc == 0) {
  112. /* getaddrinfo() resolve */
  113. struct addrinfo *ai;
  114. struct addrinfo hints;
  115. memset(&hints, 0, sizeof(hints));
  116. hints.ai_family = use_ipv6 ? PF_INET6 : PF_INET;
  117. hints.ai_socktype = SOCK_STREAM;
  118. hints.ai_flags = 0;
  119. /* Use parenthesis around functions to stop them from being replaced by
  120. the macro in memdebug.h */
  121. rc = (getaddrinfo)(host, "80", &hints, &ai);
  122. if(rc == 0)
  123. (freeaddrinfo)(ai);
  124. }
  125. #else
  126. if(use_ipv6) {
  127. puts("IPv6 support has been disabled in this program");
  128. return 1;
  129. }
  130. else {
  131. /* gethostbyname() resolve */
  132. struct hostent *he;
  133. he = gethostbyname(host);
  134. rc = !he;
  135. }
  136. #endif
  137. if(rc)
  138. printf("Resolving %s '%s' didn't work\n", ipv_inuse, host);
  139. return !!rc;
  140. }