gethost.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. /*
  6. * File: gethost.c
  7. *
  8. * Description: tests various functions in prnetdb.h
  9. *
  10. * Usage: gethost [-6] [hostname]
  11. */
  12. #include "prio.h"
  13. #include "prnetdb.h"
  14. #include "plgetopt.h"
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #define DEFAULT_HOST_NAME "mozilla.org"
  18. static void Help(void)
  19. {
  20. fprintf(stderr, "Usage: gethost [-h] [hostname]\n");
  21. fprintf(stderr, "\t-h help\n");
  22. fprintf(stderr, "\thostname Name of host (default: %s)\n",
  23. DEFAULT_HOST_NAME);
  24. } /* Help */
  25. /*
  26. * Prints the contents of a PRHostEnt structure
  27. */
  28. void PrintHostent(const PRHostEnt *he)
  29. {
  30. int i;
  31. int j;
  32. printf("h_name: %s\n", he->h_name);
  33. for (i = 0; he->h_aliases[i]; i++) {
  34. printf("h_aliases[%d]: %s\n", i, he->h_aliases[i]);
  35. }
  36. printf("h_addrtype: %d\n", he->h_addrtype);
  37. printf("h_length: %d\n", he->h_length);
  38. for (i = 0; he->h_addr_list[i]; i++) {
  39. printf("h_addr_list[%d]: ", i);
  40. for (j = 0; j < he->h_length; j++) {
  41. if (j != 0) {
  42. printf(".");
  43. }
  44. printf("%u", (unsigned char)he->h_addr_list[i][j]);
  45. }
  46. printf("\n");
  47. }
  48. }
  49. int main(int argc, char **argv)
  50. {
  51. const char *hostName = DEFAULT_HOST_NAME;
  52. PRHostEnt he, reversehe;
  53. char buf[PR_NETDB_BUF_SIZE];
  54. char reversebuf[PR_NETDB_BUF_SIZE];
  55. PRIntn idx;
  56. PRNetAddr addr;
  57. PLOptStatus os;
  58. PLOptState *opt = PL_CreateOptState(argc, argv, "h");
  59. while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) {
  60. if (PL_OPT_BAD == os) {
  61. continue;
  62. }
  63. switch (opt->option) {
  64. case 0: /* naked */
  65. hostName = opt->value;
  66. break;
  67. case 'h': /* Help message */
  68. default:
  69. Help();
  70. return 2;
  71. }
  72. }
  73. PL_DestroyOptState(opt);
  74. if (PR_GetHostByName(hostName, buf, sizeof(buf), &he) == PR_FAILURE) {
  75. fprintf(stderr, "PR_GetHostByName failed\n");
  76. exit(1);
  77. }
  78. PrintHostent(&he);
  79. idx = 0;
  80. while (1) {
  81. idx = PR_EnumerateHostEnt(idx, &he, 0, &addr);
  82. if (idx == -1) {
  83. fprintf(stderr, "PR_EnumerateHostEnt failed\n");
  84. exit(1);
  85. }
  86. if (idx == 0) {
  87. break; /* normal loop termination */
  88. }
  89. printf("reverse lookup\n");
  90. if (PR_GetHostByAddr(&addr, reversebuf, sizeof(reversebuf),
  91. &reversehe) == PR_FAILURE) {
  92. fprintf(stderr, "PR_GetHostByAddr failed\n");
  93. exit(1);
  94. }
  95. PrintHostent(&reversehe);
  96. }
  97. printf("PR_GetIPNodeByName with PR_AF_INET\n");
  98. if (PR_GetIPNodeByName(hostName, PR_AF_INET, PR_AI_DEFAULT,
  99. buf, sizeof(buf), &he) == PR_FAILURE) {
  100. fprintf(stderr, "PR_GetIPNodeByName failed\n");
  101. exit(1);
  102. }
  103. PrintHostent(&he);
  104. printf("PR_GetIPNodeByName with PR_AF_INET6\n");
  105. if (PR_GetIPNodeByName(hostName, PR_AF_INET6, PR_AI_DEFAULT,
  106. buf, sizeof(buf), &he) == PR_FAILURE) {
  107. fprintf(stderr, "PR_GetIPNodeByName failed\n");
  108. exit(1);
  109. }
  110. PrintHostent(&he);
  111. idx = 0;
  112. printf("PR_GetHostByAddr with PR_AF_INET6\n");
  113. while (1) {
  114. idx = PR_EnumerateHostEnt(idx, &he, 0, &addr);
  115. if (idx == -1) {
  116. fprintf(stderr, "PR_EnumerateHostEnt failed\n");
  117. exit(1);
  118. }
  119. if (idx == 0) {
  120. break; /* normal loop termination */
  121. }
  122. printf("reverse lookup\n");
  123. if (PR_GetHostByAddr(&addr, reversebuf, sizeof(reversebuf),
  124. &reversehe) == PR_FAILURE) {
  125. fprintf(stderr, "PR_GetHostByAddr failed\n");
  126. exit(1);
  127. }
  128. PrintHostent(&reversehe);
  129. }
  130. printf("PR_GetHostByAddr with PR_AF_INET6 done\n");
  131. PR_StringToNetAddr("::1", &addr);
  132. if (PR_IsNetAddrType(&addr, PR_IpAddrV4Mapped) == PR_TRUE) {
  133. fprintf(stderr, "addr should not be ipv4 mapped address\n");
  134. exit(1);
  135. }
  136. if (PR_IsNetAddrType(&addr, PR_IpAddrLoopback) == PR_FALSE) {
  137. fprintf(stderr, "addr should be loopback address\n");
  138. exit(1);
  139. }
  140. PR_StringToNetAddr("127.0.0.1", &addr);
  141. if (PR_IsNetAddrType(&addr, PR_IpAddrLoopback) == PR_FALSE) {
  142. fprintf(stderr, "addr should be loopback address\n");
  143. exit(1);
  144. }
  145. PR_StringToNetAddr("::FFFF:127.0.0.1", &addr);
  146. if (PR_IsNetAddrType(&addr, PR_IpAddrV4Mapped) == PR_FALSE) {
  147. fprintf(stderr, "addr should be ipv4 mapped address\n");
  148. exit(1);
  149. }
  150. if (PR_IsNetAddrType(&addr, PR_IpAddrLoopback) == PR_FALSE) {
  151. fprintf(stderr, "addr should be loopback address\n");
  152. exit(1);
  153. }
  154. if (PR_InitializeNetAddr(PR_IpAddrAny, 0, &addr) == PR_FAILURE) {
  155. fprintf(stderr, "PR_InitializeNetAddr failed\n");
  156. exit(1);
  157. }
  158. if (PR_IsNetAddrType(&addr, PR_IpAddrAny) == PR_FALSE) {
  159. fprintf(stderr, "addr should be unspecified address\n");
  160. exit(1);
  161. }
  162. if (PR_InitializeNetAddr(PR_IpAddrLoopback, 0, &addr) == PR_FAILURE) {
  163. fprintf(stderr, "PR_InitializeNetAddr failed\n");
  164. exit(1);
  165. }
  166. if (PR_IsNetAddrType(&addr, PR_IpAddrLoopback) == PR_FALSE) {
  167. fprintf(stderr, "addr should be loopback address\n");
  168. exit(1);
  169. }
  170. if (PR_SetNetAddr(PR_IpAddrAny, PR_AF_INET, 0, &addr) == PR_FAILURE) {
  171. fprintf(stderr, "PR_SetNetAddr failed\n");
  172. exit(1);
  173. }
  174. if (PR_IsNetAddrType(&addr, PR_IpAddrAny) == PR_FALSE) {
  175. fprintf(stderr, "addr should be unspecified address\n");
  176. exit(1);
  177. }
  178. if (PR_SetNetAddr(PR_IpAddrLoopback, PR_AF_INET, 0, &addr) == PR_FAILURE) {
  179. fprintf(stderr, "PR_SetNetAddr failed\n");
  180. exit(1);
  181. }
  182. if (PR_IsNetAddrType(&addr, PR_IpAddrLoopback) == PR_FALSE) {
  183. fprintf(stderr, "addr should be loopback address\n");
  184. exit(1);
  185. }
  186. addr.inet.family = PR_AF_INET;
  187. addr.inet.port = 0;
  188. addr.inet.ip = PR_htonl(PR_INADDR_ANY);
  189. if (PR_IsNetAddrType(&addr, PR_IpAddrAny) == PR_FALSE) {
  190. fprintf(stderr, "addr should be unspecified address\n");
  191. exit(1);
  192. }
  193. {
  194. char buf[256];
  195. PR_NetAddrToString(&addr, buf, 256);
  196. printf("IPv4 INADDRANY: %s\n", buf);
  197. }
  198. addr.inet.family = PR_AF_INET;
  199. addr.inet.port = 0;
  200. addr.inet.ip = PR_htonl(PR_INADDR_LOOPBACK);
  201. if (PR_IsNetAddrType(&addr, PR_IpAddrLoopback) == PR_FALSE) {
  202. fprintf(stderr, "addr should be loopback address\n");
  203. exit(1);
  204. }
  205. {
  206. char buf[256];
  207. PR_NetAddrToString(&addr, buf, 256);
  208. printf("IPv4 LOOPBACK: %s\n", buf);
  209. }
  210. if (PR_SetNetAddr(PR_IpAddrAny, PR_AF_INET6, 0, &addr) == PR_FAILURE) {
  211. fprintf(stderr, "PR_SetNetAddr failed\n");
  212. exit(1);
  213. }
  214. if (PR_IsNetAddrType(&addr, PR_IpAddrAny) == PR_FALSE) {
  215. fprintf(stderr, "addr should be unspecified address\n");
  216. exit(1);
  217. }
  218. {
  219. char buf[256];
  220. PR_NetAddrToString(&addr, buf, 256);
  221. printf("IPv6 INADDRANY: %s\n", buf);
  222. }
  223. if (PR_SetNetAddr(PR_IpAddrLoopback, PR_AF_INET6, 0, &addr) == PR_FAILURE) {
  224. fprintf(stderr, "PR_SetNetAddr failed\n");
  225. exit(1);
  226. }
  227. if (PR_IsNetAddrType(&addr, PR_IpAddrLoopback) == PR_FALSE) {
  228. fprintf(stderr, "addr should be loopback address\n");
  229. exit(1);
  230. }
  231. {
  232. char buf[256];
  233. PR_NetAddrToString(&addr, buf, 256);
  234. printf("IPv6 LOOPBACK: %s\n", buf);
  235. }
  236. {
  237. PRIPv6Addr v6addr;
  238. char tmp_buf[256];
  239. PR_SetNetAddr(PR_IpAddrLoopback, PR_AF_INET, 0, &addr);
  240. PR_ConvertIPv4AddrToIPv6(addr.inet.ip, &v6addr);
  241. PR_SetNetAddr(PR_IpAddrAny, PR_AF_INET6, 0, &addr);
  242. addr.ipv6.ip = v6addr;
  243. PR_NetAddrToString(&addr, tmp_buf, 256);
  244. printf("IPv4-mapped IPv6 LOOPBACK: %s\n", tmp_buf);
  245. }
  246. printf("PASS\n");
  247. return 0;
  248. }