netdb.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /* Copyright (C) 1998-2018 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Andreas Jaeger <[email protected]>, 1998.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. /*
  16. Testing of some network related lookup functions.
  17. The system databases looked up are:
  18. - /etc/services
  19. - /etc/hosts
  20. - /etc/networks
  21. - /etc/protocols
  22. The tests try to be fairly generic and simple so that they work on
  23. every possible setup (and might therefore not detect some possible
  24. errors).
  25. */
  26. #include <netdb.h>
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <arpa/inet.h>
  31. #include <netinet/in.h>
  32. #include <sys/param.h>
  33. #include <sys/socket.h>
  34. #include <unistd.h>
  35. #include <errno.h>
  36. int error_count;
  37. static void
  38. output_servent (const char *call, struct servent *sptr)
  39. {
  40. char **pptr;
  41. if (sptr == NULL)
  42. printf ("Call: %s returned NULL\n", call);
  43. else
  44. {
  45. printf ("Call: %s, returned: s_name: %s, s_port: %d, s_proto: %s\n",
  46. call, sptr->s_name, ntohs(sptr->s_port), sptr->s_proto);
  47. for (pptr = sptr->s_aliases; *pptr != NULL; pptr++)
  48. printf (" alias: %s\n", *pptr);
  49. }
  50. }
  51. static void
  52. test_services (void)
  53. {
  54. struct servent *sptr;
  55. sptr = getservbyname ("domain", "tcp");
  56. // output_servent ("getservbyname (\"domain\", \"tcp\")", sptr);
  57. sptr = getservbyname ("domain", "udp");
  58. // output_servent ("getservbyname (\"domain\", \"udp\")", sptr);
  59. sptr = getservbyname ("domain", NULL);
  60. // output_servent ("getservbyname (\"domain\", NULL)", sptr);
  61. sptr = getservbyname ("not-existant", NULL);
  62. // output_servent ("getservbyname (\"not-existant\", NULL)", sptr);
  63. /* This shouldn't return anything. */
  64. sptr = getservbyname ("", "");
  65. // output_servent ("getservbyname (\"\", \"\")", sptr);
  66. sptr = getservbyname ("", "tcp");
  67. // output_servent ("getservbyname (\"\", \"tcp\")", sptr);
  68. sptr = getservbyport (htons(53), "tcp");
  69. // output_servent ("getservbyport (htons(53), \"tcp\")", sptr);
  70. sptr = getservbyport (htons(53), NULL);
  71. // output_servent ("getservbyport (htons(53), NULL)", sptr);
  72. sptr = getservbyport (htons(1), "udp"); /* shouldn't exist */
  73. // output_servent ("getservbyport (htons(1), \"udp\")", sptr);
  74. setservent (0);
  75. do
  76. {
  77. sptr = getservent ();
  78. //output_servent ("getservent ()", sptr);
  79. }
  80. while (sptr != NULL);
  81. endservent ();
  82. }
  83. static void
  84. output_hostent (const char *call, struct hostent *hptr)
  85. {
  86. char **pptr;
  87. char buf[INET6_ADDRSTRLEN];
  88. if (hptr == NULL)
  89. printf ("Call: %s returned NULL\n", call);
  90. else
  91. {
  92. printf ("Call: %s returned: name: %s, addr_type: %d\n",
  93. call, hptr->h_name, hptr->h_addrtype);
  94. if (hptr->h_aliases)
  95. for (pptr = hptr->h_aliases; *pptr != NULL; pptr++)
  96. printf (" alias: %s\n", *pptr);
  97. for (pptr = hptr->h_addr_list; *pptr != NULL; pptr++)
  98. printf (" ip: %s\n",
  99. inet_ntop (hptr->h_addrtype, *pptr, buf, sizeof (buf)));
  100. }
  101. }
  102. static void
  103. test_hosts (void)
  104. {
  105. struct hostent *hptr1, *hptr2;
  106. char *name = NULL;
  107. size_t namelen = 0;
  108. struct in_addr ip;
  109. hptr1 = gethostbyname ("localhost");
  110. hptr2 = gethostbyname ("LocalHost");
  111. if (hptr1 != NULL || hptr2 != NULL)
  112. {
  113. if (hptr1 == NULL)
  114. {
  115. printf ("localhost not found - but LocalHost found:-(\n");
  116. ++error_count;
  117. }
  118. else if (hptr2 == NULL)
  119. {
  120. printf ("LocalHost not found - but localhost found:-(\n");
  121. ++error_count;
  122. }
  123. else if (strcmp (hptr1->h_name, hptr2->h_name) != 0)
  124. {
  125. printf ("localhost and LocalHost have different canoncial name\n");
  126. printf ("gethostbyname (\"localhost\")->%s\n", hptr1->h_name);
  127. printf ("gethostbyname (\"LocalHost\")->%s\n", hptr2->h_name);
  128. ++error_count;
  129. }
  130. //else
  131. // output_hostent ("gethostbyname(\"localhost\")", hptr1);
  132. }
  133. hptr1 = gethostbyname ("127.0.0.1");
  134. //output_hostent ("gethostbyname (\"127.0.0.1\")", hptr1);
  135. while (gethostname (name, namelen) < 0 && errno == ENAMETOOLONG)
  136. {
  137. namelen += 2; /* tiny increments to test a lot */
  138. name = realloc (name, namelen);
  139. }
  140. if (gethostname (name, namelen) == 0)
  141. {
  142. // printf ("Hostname: %s\n", name);
  143. if (name != NULL)
  144. {
  145. hptr1 = gethostbyname (name);
  146. // output_hostent ("gethostbyname (gethostname(...))", hptr1);
  147. }
  148. }
  149. ip.s_addr = htonl (INADDR_LOOPBACK);
  150. hptr1 = gethostbyaddr ((char *) &ip, sizeof(ip), AF_INET);
  151. if (hptr1 != NULL)
  152. {
  153. // printf ("official name of 127.0.0.1: %s\n", hptr1->h_name);
  154. }
  155. sethostent (0);
  156. do
  157. {
  158. hptr1 = gethostent ();
  159. //output_hostent ("gethostent ()", hptr1);
  160. }
  161. while (hptr1 != NULL);
  162. endhostent ();
  163. struct hostent* redox = gethostbyname("redox-os.org");
  164. if (redox == NULL) {
  165. ++error_count;
  166. }
  167. //output_hostent("gethostbyname(\"redox-os.org\")", redox);
  168. struct in_addr el_goog;
  169. inet_aton("8.8.4.4", &el_goog);
  170. struct hostent* google = gethostbyaddr(&el_goog, 4, AF_INET);
  171. if (google == NULL) {
  172. ++error_count;
  173. }
  174. //output_hostent("gethostbyaddr(\"8.8.4.4\")",google);
  175. }
  176. static void
  177. output_protoent (const char *call, struct protoent *prptr)
  178. {
  179. char **pptr;
  180. if (prptr == NULL)
  181. printf ("Call: %s returned NULL\n", call);
  182. else
  183. {
  184. printf ("Call: %s, returned: p_name: %s, p_proto: %d\n",
  185. call, prptr->p_name, prptr->p_proto);
  186. for (pptr = prptr->p_aliases; *pptr != NULL; pptr++)
  187. printf (" alias: %s\n", *pptr);
  188. }
  189. }
  190. static void
  191. test_protocols (void)
  192. {
  193. struct protoent *prptr;
  194. prptr = getprotobyname ("ICMP");
  195. // output_protoent ("getprotobyname (\"ICMP\")", prptr);
  196. prptr = getprotobynumber (1);
  197. // output_protoent ("getprotobynumber (1)", prptr);
  198. setprotoent (0);
  199. do
  200. {
  201. prptr = getprotoent ();
  202. // output_protoent ("getprotoent ()", prptr);
  203. }
  204. while (prptr != NULL);
  205. endprotoent ();
  206. }
  207. static int
  208. do_test (void)
  209. {
  210. /*
  211. setdb ("db");
  212. */
  213. test_hosts ();
  214. //test_network ();
  215. test_protocols ();
  216. test_services ();
  217. if (error_count)
  218. printf ("\n %d errors occurred!\n", error_count);
  219. else
  220. printf ("No visible errors occurred!\n");
  221. return (error_count != 0);
  222. }
  223. int main(void) {
  224. do_test();
  225. }