A clone of btpd with my configuration changes.
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

145 řádky
3.4 KiB

  1. /*
  2. * Copyright (c) 2003-2006 Niels Provos <provos@citi.umich.edu>
  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. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  17. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  18. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  19. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  20. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  21. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  22. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  23. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  25. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #ifdef WIN32
  28. #include <winsock2.h>
  29. #include <windows.h>
  30. #endif
  31. #ifdef HAVE_CONFIG_H
  32. #include "config.h"
  33. #endif
  34. #include <sys/types.h>
  35. #include <sys/stat.h>
  36. #ifdef HAVE_SYS_TIME_H
  37. #include <sys/time.h>
  38. #endif
  39. #include <sys/queue.h>
  40. #ifndef WIN32
  41. #include <sys/socket.h>
  42. #include <sys/signal.h>
  43. #include <netinet/in.h>
  44. #include <arpa/inet.h>
  45. #include <unistd.h>
  46. #endif
  47. #include <netdb.h>
  48. #include <fcntl.h>
  49. #include <stdlib.h>
  50. #include <stdio.h>
  51. #include <string.h>
  52. #include <errno.h>
  53. #include "event.h"
  54. #include "evdns.h"
  55. #include "log.h"
  56. static int dns_ok = 0;
  57. void
  58. dns_gethostbyname_cb(int result, char type, int count, int ttl,
  59. void *addresses, void *arg)
  60. {
  61. dns_ok = 0;
  62. if (result != DNS_ERR_NONE)
  63. goto out;
  64. fprintf(stderr, "type: %d, count: %d, ttl: %d: ", type, count, ttl);
  65. switch (type) {
  66. case DNS_IPv4_A: {
  67. struct in_addr *in_addrs = addresses;
  68. int i;
  69. /* a resolution that's not valid does not help */
  70. if (ttl < 0)
  71. goto out;
  72. for (i = 0; i < count; ++i)
  73. fprintf(stderr, "%s ", inet_ntoa(in_addrs[0]));
  74. break;
  75. }
  76. case DNS_PTR:
  77. /* may get at most one PTR */
  78. if (count != 1)
  79. goto out;
  80. fprintf(stderr, "%s ", *(char **)addresses);
  81. break;
  82. default:
  83. goto out;
  84. }
  85. dns_ok = 1;
  86. out:
  87. event_loopexit(NULL);
  88. }
  89. void
  90. dns_gethostbyname()
  91. {
  92. fprintf(stdout, "Simple DNS resolve: ");
  93. dns_ok = 0;
  94. evdns_resolve_ipv4("www.monkey.org", 0, dns_gethostbyname_cb, NULL);
  95. event_dispatch();
  96. if (dns_ok) {
  97. fprintf(stdout, "OK\n");
  98. } else {
  99. fprintf(stdout, "FAILED\n");
  100. exit(1);
  101. }
  102. }
  103. void
  104. dns_gethostbyaddr()
  105. {
  106. struct in_addr in;
  107. in.s_addr = htonl(0x7f000001ul); /* 127.0.0.1 */
  108. fprintf(stdout, "Simple reverse DNS resolve: ");
  109. dns_ok = 0;
  110. evdns_resolve_reverse(&in, 0, dns_gethostbyname_cb, NULL);
  111. event_dispatch();
  112. if (dns_ok) {
  113. fprintf(stdout, "OK\n");
  114. } else {
  115. fprintf(stdout, "FAILED\n");
  116. exit(1);
  117. }
  118. }
  119. void
  120. dns_suite(void)
  121. {
  122. evdns_init();
  123. dns_gethostbyname();
  124. dns_gethostbyaddr();
  125. evdns_shutdown(0);
  126. }