2
0

lookupdns.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * Portions created by SGI are Copyright (C) 2000 Silicon Graphics, Inc.
  3. * All Rights Reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of Silicon Graphics, Inc. nor the names of its
  15. * contributors may be used to endorse or promote products derived from
  16. * this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. * HOLDERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
  24. * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  25. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  26. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  27. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <unistd.h>
  33. #include <sys/time.h>
  34. #include <sys/types.h>
  35. #include <sys/socket.h>
  36. #include <netinet/in.h>
  37. #include <arpa/inet.h>
  38. #include <netdb.h>
  39. #include "st.h"
  40. #if !defined(NETDB_INTERNAL) && defined(h_NETDB_INTERNAL)
  41. #define NETDB_INTERNAL h_NETDB_INTERNAL
  42. #endif
  43. /* Resolution timeout (in microseconds) */
  44. #define TIMEOUT (2*1000000LL)
  45. /* External function defined in the res.c file */
  46. int dns_getaddr(const char *host, struct in_addr *addr, st_utime_t timeout);
  47. void *do_resolve(void *host)
  48. {
  49. struct in_addr addr;
  50. /* Use dns_getaddr() instead of gethostbyname(3) to get IP address */
  51. if (dns_getaddr(host, &addr, TIMEOUT) < 0) {
  52. fprintf(stderr, "dns_getaddr: can't resolve %s: ", (char *)host);
  53. if (h_errno == NETDB_INTERNAL)
  54. perror("");
  55. else
  56. herror("");
  57. } else
  58. printf("%-40s %s\n", (char *)host, inet_ntoa(addr));
  59. return NULL;
  60. }
  61. /*
  62. * Asynchronous DNS host name resolution. This program creates one
  63. * ST thread for each host name (specified as command line arguments).
  64. * All threads do host name resolution concurrently.
  65. */
  66. int main(int argc, char *argv[])
  67. {
  68. int i;
  69. if (argc < 2) {
  70. fprintf(stderr, "Usage: %s <hostname1> [<hostname2>] ...\n", argv[0]);
  71. exit(1);
  72. }
  73. if (st_init() < 0) {
  74. perror("st_init");
  75. exit(1);
  76. }
  77. for (i = 1; i < argc; i++) {
  78. /* Create a separate thread for each host name */
  79. if (st_thread_create(do_resolve, argv[i], 0, 0) == NULL) {
  80. perror("st_thread_create");
  81. exit(1);
  82. }
  83. }
  84. st_thread_exit(NULL);
  85. /* NOTREACHED */
  86. return 1;
  87. }