resolve.c 4.0 KB

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