PageRenderTime 52ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/contrib/iv_getaddrinfo/test.c

https://github.com/buytenh/ivykis
C | 202 lines | 156 code | 28 blank | 18 comment | 39 complexity | 7e58c561d5a77f747296402f418965d2 MD5 | raw file
  1. /*
  2. * ivykis, an event handling library
  3. * Copyright (C) 2011 Lennert Buytenhek
  4. *
  5. * This library is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License version
  7. * 2.1 as published by the Free Software Foundation.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU Lesser General Public License version 2.1 for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License version 2.1 along with this library; if not, write to the
  16. * Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
  17. * Boston, MA 02110-1301, USA.
  18. */
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <netinet/in.h>
  22. #include <arpa/inet.h>
  23. #include <iv.h>
  24. #include <iv_getaddrinfo.h>
  25. #include <netdb.h>
  26. #include <string.h>
  27. #include <sys/socket.h>
  28. #include <sys/types.h>
  29. #ifndef AI_ADDRCONFIG
  30. #define AI_ADDRCONFIG 0
  31. #endif
  32. #ifndef AI_V4MAPPED
  33. #define AI_V4MAPPED 0
  34. #endif
  35. static void got_results(void *_ig, int ret, struct addrinfo *res)
  36. {
  37. struct iv_getaddrinfo *ig = _ig;
  38. struct addrinfo *a;
  39. if (ret) {
  40. printf("%s: return code is %d\n", ig->node, ret);
  41. free(ig);
  42. return;
  43. }
  44. a = res;
  45. while (a != NULL) {
  46. #ifdef VERBOSE
  47. int flags;
  48. printf("\n");
  49. printf("address record:\n");
  50. printf("ai_flags =");
  51. flags = a->ai_flags;
  52. if (flags & AI_PASSIVE) {
  53. printf(" AI_PASSIVE");
  54. flags &= ~AI_PASSIVE;
  55. }
  56. if (flags & AI_CANONNAME) {
  57. printf(" AI_CANONNAME");
  58. flags &= ~AI_CANONNAME;
  59. }
  60. if (flags & AI_NUMERICHOST) {
  61. printf(" AI_NUMERICHOST");
  62. flags &= ~AI_NUMERICHOST;
  63. }
  64. if (flags & AI_V4MAPPED) {
  65. printf(" AI_V4MAPPED");
  66. flags &= ~AI_V4MAPPED;
  67. }
  68. if (flags & AI_ALL) {
  69. printf(" AI_ALL");
  70. flags &= ~AI_ALL;
  71. }
  72. if (flags & AI_ADDRCONFIG) {
  73. printf(" AI_ADDRCONFIG");
  74. flags &= ~AI_ADDRCONFIG;
  75. }
  76. if (flags & AI_IDN) {
  77. printf(" AI_IDN");
  78. flags &= ~AI_IDN;
  79. }
  80. if (flags & AI_CANONIDN) {
  81. printf(" AI_CANONIDN");
  82. flags &= ~AI_CANONIDN;
  83. }
  84. if (flags & AI_IDN_ALLOW_UNASSIGNED) {
  85. printf(" AI_IDN_ALLOW_UNASSIGNED");
  86. flags &= ~AI_IDN_ALLOW_UNASSIGNED;
  87. }
  88. if (flags & AI_IDN_USE_STD3_ASCII_RULES) {
  89. printf(" AI_IDN_USE_STD3_ASCII_RULES");
  90. flags &= ~AI_IDN_USE_STD3_ASCII_RULES;
  91. }
  92. if (flags & AI_NUMERICSERV) {
  93. printf(" AI_NUMERICSERV");
  94. flags &= ~AI_NUMERICSERV;
  95. }
  96. if (flags)
  97. printf(" %x", flags);
  98. printf("\n");
  99. printf("ai_family = ");
  100. if (a->ai_family == PF_INET)
  101. printf("PF_INET\n");
  102. else if (a->ai_family == PF_INET6)
  103. printf("PF_INET6\n");
  104. else
  105. printf("%d\n", a->ai_family);
  106. printf("ai_socktype = ");
  107. if (a->ai_socktype == SOCK_STREAM)
  108. printf("SOCK_STREAM\n");
  109. else if (a->ai_socktype == SOCK_DGRAM)
  110. printf("SOCK_DGRAM\n");
  111. else
  112. printf("%d\n", a->ai_socktype);
  113. printf("ai_protocol = ");
  114. if (a->ai_protocol == IPPROTO_TCP)
  115. printf("IPPROTO_TCP\n");
  116. else if (a->ai_protocol == IPPROTO_UDP)
  117. printf("IPPROTO_UDP\n");
  118. else
  119. printf("%d\n", a->ai_protocol);
  120. printf("ai_addr = ");
  121. #endif
  122. printf("%s: ", ig->node);
  123. if (a->ai_addr->sa_family == AF_INET) {
  124. struct sockaddr_in *in;
  125. char name[64];
  126. in = (struct sockaddr_in *)a->ai_addr;
  127. inet_ntop(AF_INET, &in->sin_addr, name, sizeof(name));
  128. printf("%s\n", name);
  129. #ifndef __hpux__
  130. } else if (a->ai_addr->sa_family == AF_INET6) {
  131. struct sockaddr_in6 *in;
  132. char name[64];
  133. in = (struct sockaddr_in6 *)a->ai_addr;
  134. inet_ntop(AF_INET6, &in->sin6_addr, name, sizeof(name));
  135. printf("%s\n", name);
  136. #endif
  137. } else {
  138. printf("unknown address family\n");
  139. }
  140. #ifdef VERBOSE
  141. printf("ai_canonname = %s\n", a->ai_canonname);
  142. #endif
  143. a = a->ai_next;
  144. }
  145. freeaddrinfo(res);
  146. free(ig);
  147. }
  148. int main(int argc, char *argv[])
  149. {
  150. struct addrinfo hints;
  151. int i;
  152. iv_init();
  153. memset(&hints, 0, sizeof(hints));
  154. hints.ai_family = PF_UNSPEC;
  155. hints.ai_socktype = SOCK_STREAM;
  156. hints.ai_protocol = 0;
  157. hints.ai_flags = AI_ADDRCONFIG | AI_V4MAPPED;
  158. for (i = 1; i < argc; i++) {
  159. struct iv_getaddrinfo *ig;
  160. ig = malloc(sizeof(*ig));
  161. if (ig == NULL)
  162. break;
  163. memset(ig, 0, sizeof(*ig));
  164. ig->node = argv[i];
  165. ig->service = NULL;
  166. ig->hints = &hints;
  167. ig->cookie = ig;
  168. ig->handler = got_results;
  169. iv_getaddrinfo_submit(ig);
  170. }
  171. iv_main();
  172. iv_deinit();
  173. return 0;
  174. }