2
0

netdb.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. #include "test_helpers.h"
  37. int error_count;
  38. static void
  39. output_servent (const char *call, struct servent *sptr)
  40. {
  41. char **pptr;
  42. if (sptr == NULL)
  43. printf ("Call: %s returned NULL\n", call);
  44. else
  45. {
  46. printf ("Call: %s, returned: s_name: %s, s_port: %d, s_proto: %s\n",
  47. call, sptr->s_name, ntohs(sptr->s_port), sptr->s_proto);
  48. for (pptr = sptr->s_aliases; *pptr != NULL; pptr++)
  49. printf (" alias: %s\n", *pptr);
  50. }
  51. }
  52. static void
  53. test_services (void)
  54. {
  55. struct servent *sptr;
  56. sptr = getservbyname ("domain", "tcp");
  57. // output_servent ("getservbyname (\"domain\", \"tcp\")", sptr);
  58. sptr = getservbyname ("domain", "udp");
  59. // output_servent ("getservbyname (\"domain\", \"udp\")", sptr);
  60. sptr = getservbyname ("domain", NULL);
  61. // output_servent ("getservbyname (\"domain\", NULL)", sptr);
  62. sptr = getservbyname ("not-existant", NULL);
  63. // output_servent ("getservbyname (\"not-existant\", NULL)", sptr);
  64. /* This shouldn't return anything. */
  65. sptr = getservbyname ("", "");
  66. // output_servent ("getservbyname (\"\", \"\")", sptr);
  67. sptr = getservbyname ("", "tcp");
  68. // output_servent ("getservbyname (\"\", \"tcp\")", sptr);
  69. sptr = getservbyport (htons(53), "tcp");
  70. // output_servent ("getservbyport (htons(53), \"tcp\")", sptr);
  71. sptr = getservbyport (htons(53), NULL);
  72. // output_servent ("getservbyport (htons(53), NULL)", sptr);
  73. sptr = getservbyport (htons(1), "udp"); /* shouldn't exist */
  74. // output_servent ("getservbyport (htons(1), \"udp\")", sptr);
  75. setservent (0);
  76. do
  77. {
  78. sptr = getservent ();
  79. //output_servent ("getservent ()", sptr);
  80. }
  81. while (sptr != NULL);
  82. endservent ();
  83. }
  84. static void
  85. output_hostent (const char *call, struct hostent *hptr)
  86. {
  87. char **pptr;
  88. char buf[INET6_ADDRSTRLEN];
  89. if (hptr == NULL)
  90. printf ("Call: %s returned NULL\n", call);
  91. else
  92. {
  93. printf ("Call: %s returned: name: %s, addr_type: %d\n",
  94. call, hptr->h_name, hptr->h_addrtype);
  95. if (hptr->h_aliases)
  96. for (pptr = hptr->h_aliases; *pptr != NULL; pptr++)
  97. printf (" alias: %s\n", *pptr);
  98. for (pptr = hptr->h_addr_list; *pptr != NULL; pptr++)
  99. printf (" ip: %s\n",
  100. inet_ntop (hptr->h_addrtype, *pptr, buf, sizeof (buf)));
  101. }
  102. }
  103. static void
  104. test_hosts (void)
  105. {
  106. struct hostent *hptr1, *hptr2;
  107. char *name = NULL;
  108. size_t namelen = 0;
  109. struct in_addr ip;
  110. hptr1 = gethostbyname ("localhost");
  111. hptr2 = gethostbyname ("LocalHost");
  112. if (hptr1 != NULL || hptr2 != NULL)
  113. {
  114. if (hptr1 == NULL)
  115. {
  116. printf ("localhost not found - but LocalHost found:-(\n");
  117. ++error_count;
  118. }
  119. else if (hptr2 == NULL)
  120. {
  121. printf ("LocalHost not found - but localhost found:-(\n");
  122. ++error_count;
  123. }
  124. else if (strcmp (hptr1->h_name, hptr2->h_name) != 0)
  125. {
  126. printf ("localhost and LocalHost have different canoncial name\n");
  127. printf ("gethostbyname (\"localhost\")->%s\n", hptr1->h_name);
  128. printf ("gethostbyname (\"LocalHost\")->%s\n", hptr2->h_name);
  129. ++error_count;
  130. }
  131. //else
  132. // output_hostent ("gethostbyname(\"localhost\")", hptr1);
  133. }
  134. hptr1 = gethostbyname ("127.0.0.1");
  135. //output_hostent ("gethostbyname (\"127.0.0.1\")", hptr1);
  136. while (gethostname (name, namelen) < 0 && errno == ENAMETOOLONG)
  137. {
  138. namelen += 2; /* tiny increments to test a lot */
  139. name = realloc (name, namelen);
  140. }
  141. if (gethostname (name, namelen) == 0)
  142. {
  143. // printf ("Hostname: %s\n", name);
  144. if (name != NULL)
  145. {
  146. hptr1 = gethostbyname (name);
  147. // output_hostent ("gethostbyname (gethostname(...))", hptr1);
  148. }
  149. }
  150. ip.s_addr = htonl (INADDR_LOOPBACK);
  151. hptr1 = gethostbyaddr ((char *) &ip, sizeof(ip), AF_INET);
  152. if (hptr1 != NULL)
  153. {
  154. // printf ("official name of 127.0.0.1: %s\n", hptr1->h_name);
  155. }
  156. sethostent (0);
  157. do
  158. {
  159. hptr1 = gethostent ();
  160. //output_hostent ("gethostent ()", hptr1);
  161. }
  162. while (hptr1 != NULL);
  163. endhostent ();
  164. struct hostent* redox = gethostbyname("redox-os.org");
  165. if (redox == NULL) {
  166. ++error_count;
  167. }
  168. //output_hostent("gethostbyname(\"redox-os.org\")", redox);
  169. struct in_addr el_goog;
  170. inet_aton("8.8.4.4", &el_goog);
  171. struct hostent* google = gethostbyaddr(&el_goog, 4, AF_INET);
  172. if (google == NULL) {
  173. ++error_count;
  174. }
  175. //output_hostent("gethostbyaddr(\"8.8.4.4\")",google);
  176. }
  177. static void
  178. output_protoent (const char *call, struct protoent *prptr)
  179. {
  180. char **pptr;
  181. if (prptr == NULL)
  182. printf ("Call: %s returned NULL\n", call);
  183. else
  184. {
  185. printf ("Call: %s, returned: p_name: %s, p_proto: %d\n",
  186. call, prptr->p_name, prptr->p_proto);
  187. for (pptr = prptr->p_aliases; *pptr != NULL; pptr++)
  188. printf (" alias: %s\n", *pptr);
  189. }
  190. }
  191. static void
  192. test_protocols (void)
  193. {
  194. struct protoent *prptr;
  195. prptr = getprotobyname ("ICMP");
  196. // output_protoent ("getprotobyname (\"ICMP\")", prptr);
  197. prptr = getprotobynumber (1);
  198. // output_protoent ("getprotobynumber (1)", prptr);
  199. setprotoent (0);
  200. do
  201. {
  202. prptr = getprotoent ();
  203. // output_protoent ("getprotoent ()", prptr);
  204. }
  205. while (prptr != NULL);
  206. endprotoent ();
  207. }
  208. static void
  209. test_network (void)
  210. {
  211. struct netent *nptr = getnetbyname ("loopback");
  212. if (nptr != NULL) {
  213. printf("network name %d", nptr->n_net);
  214. } else {
  215. ++error_count;
  216. }
  217. do
  218. {
  219. nptr = getnetent();
  220. if (nptr != NULL) {
  221. printf("network name %s", nptr->n_name);
  222. }
  223. }
  224. while (nptr != NULL);
  225. setnetent (0);
  226. }
  227. static int
  228. do_test (void)
  229. {
  230. /*
  231. setdb ("db");
  232. */
  233. test_hosts ();
  234. test_network ();
  235. test_protocols ();
  236. test_services ();
  237. if (error_count)
  238. printf ("\n %d errors occurred!\n", error_count);
  239. else
  240. printf ("No visible errors occurred!\n");
  241. return (error_count != 0);
  242. }
  243. int main(void) {
  244. do_test();
  245. }