resolve.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2008, Daniel Stenberg, <[email protected]>, 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_SOCKET_H
  40. #include <sys/socket.h>
  41. #endif
  42. #ifdef HAVE_NETINET_IN_H
  43. #include <netinet/in.h>
  44. #endif
  45. #ifdef _XOPEN_SOURCE_EXTENDED
  46. /* This define is "almost" required to build on HPUX 11 */
  47. #include <arpa/inet.h>
  48. #endif
  49. #ifdef HAVE_NETDB_H
  50. #include <netdb.h>
  51. #endif
  52. #define ENABLE_CURLX_PRINTF
  53. /* make the curlx header define all printf() functions to use the curlx_*
  54. versions instead */
  55. #include "curlx.h" /* from the private lib dir */
  56. #include "util.h"
  57. /* include memdebug.h last */
  58. #include "memdebug.h"
  59. char use_ipv6=FALSE;
  60. const char *serverlogfile=""; /* for a util.c function we don't use */
  61. int main(int argc, char *argv[])
  62. {
  63. int arg=1;
  64. char *host;
  65. int rc;
  66. while(argc>arg) {
  67. if(!strcmp("--version", argv[arg])) {
  68. printf("resolve IPv4%s\n",
  69. #ifdef ENABLE_IPV6
  70. "/IPv6"
  71. #else
  72. ""
  73. #endif
  74. );
  75. return 0;
  76. }
  77. else if(!strcmp("--ipv6", argv[arg])) {
  78. #ifdef ENABLE_IPV6
  79. use_ipv6=TRUE;
  80. #endif
  81. arg++;
  82. }
  83. else if(!strcmp("--ipv4", argv[arg])) {
  84. /* for completeness, we support this option as well */
  85. use_ipv6=FALSE;
  86. arg++;
  87. }
  88. else {
  89. host = argv[arg++];
  90. }
  91. }
  92. if(!host) {
  93. puts("Usage: resolve [option] <host>\n"
  94. " --version\n"
  95. " --ipv4\n"
  96. " --ipv6");
  97. return 0;
  98. }
  99. #ifdef WIN32
  100. win32_init();
  101. atexit(win32_cleanup);
  102. #endif
  103. #ifdef ENABLE_IPV6
  104. if(!use_ipv6)
  105. #endif
  106. {
  107. /* gethostbyname() resolve */
  108. struct hostent *he;
  109. he = gethostbyname(host);
  110. rc = !he;
  111. }
  112. #ifdef ENABLE_IPV6
  113. else {
  114. /* getaddrinfo() resolve */
  115. struct addrinfo *ai;
  116. struct addrinfo hints;
  117. memset(&hints, 0, sizeof(hints));
  118. hints.ai_family = PF_INET6;
  119. hints.ai_socktype = SOCK_STREAM;
  120. hints.ai_flags = AI_CANONNAME;
  121. rc = (getaddrinfo)(host, "80", &hints, &ai);
  122. }
  123. #endif
  124. if(rc)
  125. printf("Resolving '%s' didn't work\n", host);
  126. return !rc?0:1;
  127. }