resolve.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 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. #ifndef CURL_NO_GETADDRINFO_OVERRIDE
  25. #define CURL_NO_GETADDRINFO_OVERRIDE
  26. #endif
  27. #include "server_setup.h"
  28. /* Purpose
  29. *
  30. * Resolve the given name, using system name resolve functions (NOT any
  31. * function provided by libcurl). Used to see if the name exists and thus if
  32. * we can allow a test case to use it for testing.
  33. *
  34. * Like if 'localhost' actual exists etc.
  35. *
  36. */
  37. #include <signal.h>
  38. #ifdef HAVE_NETINET_IN_H
  39. #include <netinet/in.h>
  40. #endif
  41. #ifdef _XOPEN_SOURCE_EXTENDED
  42. /* This define is "almost" required to build on HP-UX 11 */
  43. #include <arpa/inet.h>
  44. #endif
  45. #ifdef HAVE_NETDB_H
  46. #include <netdb.h>
  47. #endif
  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. rc = getaddrinfo(host, "80", &hints, &ai);
  120. if(rc == 0)
  121. freeaddrinfo(ai);
  122. }
  123. #else
  124. if(use_ipv6) {
  125. puts("IPv6 support has been disabled in this program");
  126. return 1;
  127. }
  128. else {
  129. /* gethostbyname() resolve */
  130. struct hostent *he;
  131. #ifdef __AMIGA__
  132. he = gethostbyname((unsigned char *)host);
  133. #else
  134. he = gethostbyname(host);
  135. #endif
  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. }