resolve.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2022, 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. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "server_setup.h"
  25. /* Purpose
  26. *
  27. * Resolve the given name, using system name resolve functions (NOT any
  28. * function provided by libcurl). Used to see if the name exists and thus if
  29. * we can allow a test case to use it for testing.
  30. *
  31. * Like if 'localhost' actual exists etc.
  32. *
  33. */
  34. #ifdef HAVE_SIGNAL_H
  35. #include <signal.h>
  36. #endif
  37. #ifdef HAVE_NETINET_IN_H
  38. #include <netinet/in.h>
  39. #endif
  40. #ifdef _XOPEN_SOURCE_EXTENDED
  41. /* This define is "almost" required to build on HPUX 11 */
  42. #include <arpa/inet.h>
  43. #endif
  44. #ifdef HAVE_NETDB_H
  45. #include <netdb.h>
  46. #endif
  47. #define ENABLE_CURLX_PRINTF
  48. /* make the curlx header define all printf() functions to use the curlx_*
  49. versions instead */
  50. #include "curlx.h" /* from the private lib dir */
  51. #include "util.h"
  52. /* include memdebug.h last */
  53. #include "memdebug.h"
  54. static bool use_ipv6 = FALSE;
  55. static const char *ipv_inuse = "IPv4";
  56. const char *serverlogfile = ""; /* for a util.c function we don't use */
  57. int main(int argc, char *argv[])
  58. {
  59. int arg = 1;
  60. const char *host = NULL;
  61. int rc = 0;
  62. while(argc>arg) {
  63. if(!strcmp("--version", argv[arg])) {
  64. printf("resolve IPv4%s\n",
  65. #if defined(CURLRES_IPV6)
  66. "/IPv6"
  67. #else
  68. ""
  69. #endif
  70. );
  71. return 0;
  72. }
  73. else if(!strcmp("--ipv6", argv[arg])) {
  74. ipv_inuse = "IPv6";
  75. use_ipv6 = TRUE;
  76. arg++;
  77. }
  78. else if(!strcmp("--ipv4", argv[arg])) {
  79. /* for completeness, we support this option as well */
  80. ipv_inuse = "IPv4";
  81. use_ipv6 = FALSE;
  82. arg++;
  83. }
  84. else {
  85. host = argv[arg++];
  86. }
  87. }
  88. if(!host) {
  89. puts("Usage: resolve [option] <host>\n"
  90. " --version\n"
  91. " --ipv4"
  92. #if defined(CURLRES_IPV6)
  93. "\n --ipv6"
  94. #endif
  95. );
  96. return 1;
  97. }
  98. #ifdef WIN32
  99. win32_init();
  100. atexit(win32_cleanup);
  101. #endif
  102. #if defined(CURLRES_IPV6)
  103. if(use_ipv6) {
  104. /* Check that the system has IPv6 enabled before checking the resolver */
  105. curl_socket_t s = socket(PF_INET6, SOCK_DGRAM, 0);
  106. if(s == CURL_SOCKET_BAD)
  107. /* an IPv6 address was requested and we can't get/use one */
  108. rc = -1;
  109. else {
  110. sclose(s);
  111. }
  112. }
  113. if(rc == 0) {
  114. /* getaddrinfo() resolve */
  115. struct addrinfo *ai;
  116. struct addrinfo hints;
  117. memset(&hints, 0, sizeof(hints));
  118. hints.ai_family = use_ipv6 ? PF_INET6 : PF_INET;
  119. hints.ai_socktype = SOCK_STREAM;
  120. hints.ai_flags = 0;
  121. /* Use parenthesis around functions to stop them from being replaced by
  122. the macro in memdebug.h */
  123. rc = (getaddrinfo)(host, "80", &hints, &ai);
  124. if(rc == 0)
  125. (freeaddrinfo)(ai);
  126. }
  127. #else
  128. if(use_ipv6) {
  129. puts("IPv6 support has been disabled in this program");
  130. return 1;
  131. }
  132. else {
  133. /* gethostbyname() resolve */
  134. struct hostent *he;
  135. he = gethostbyname(host);
  136. rc = !he;
  137. }
  138. #endif
  139. if(rc)
  140. printf("Resolving %s '%s' didn't work\n", ipv_inuse, host);
  141. return !!rc;
  142. }