resolve.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2012, 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 http://curl.haxx.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. #ifdef ENABLE_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. #ifdef ENABLE_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(!use_ipv6) {
  101. /* gethostbyname() resolve */
  102. struct hostent *he;
  103. he = gethostbyname(host);
  104. rc = !he;
  105. }
  106. else {
  107. #ifdef ENABLE_IPV6
  108. /* Check that the system has IPv6 enabled before checking the resolver */
  109. curl_socket_t s = socket(PF_INET6, SOCK_DGRAM, 0);
  110. if(s == CURL_SOCKET_BAD)
  111. /* an ipv6 address was requested and we can't get/use one */
  112. rc = -1;
  113. else {
  114. sclose(s);
  115. }
  116. if (rc == 0) {
  117. /* getaddrinfo() resolve */
  118. struct addrinfo *ai;
  119. struct addrinfo hints;
  120. memset(&hints, 0, sizeof(hints));
  121. hints.ai_family = PF_INET6;
  122. hints.ai_socktype = SOCK_STREAM;
  123. hints.ai_flags = AI_CANONNAME;
  124. /* Use parenthesis around function to stop it from being replaced by
  125. the macro in memdebug.h */
  126. rc = (getaddrinfo)(host, "80", &hints, &ai);
  127. }
  128. #else
  129. puts("IPv6 support has been disabled in this program");
  130. return 1;
  131. #endif
  132. }
  133. if(rc)
  134. printf("Resolving %s '%s' didn't work\n", ipv_inuse, host);
  135. return !!rc;
  136. }