test-netdb.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /* Copyright (C) 1998-2019 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Andreas Jaeger <aj@suse.de>, 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 "nss.h"
  37. #include <support/support.h>
  38. /*
  39. The following define is necessary for glibc 2.0.6
  40. */
  41. #ifndef INET6_ADDRSTRLEN
  42. # define INET6_ADDRSTRLEN 46
  43. #endif
  44. int error_count;
  45. static void
  46. output_servent (const char *call, struct servent *sptr)
  47. {
  48. char **pptr;
  49. if (sptr == NULL)
  50. printf ("Call: %s returned NULL\n", call);
  51. else
  52. {
  53. printf ("Call: %s, returned: s_name: %s, s_port: %d, s_proto: %s\n",
  54. call, sptr->s_name, ntohs(sptr->s_port), sptr->s_proto);
  55. for (pptr = sptr->s_aliases; *pptr != NULL; pptr++)
  56. printf (" alias: %s\n", *pptr);
  57. }
  58. }
  59. static void
  60. test_services (void)
  61. {
  62. struct servent *sptr;
  63. sptr = getservbyname ("domain", "tcp");
  64. output_servent ("getservbyname (\"domain\", \"tcp\")", sptr);
  65. sptr = getservbyname ("domain", "udp");
  66. output_servent ("getservbyname (\"domain\", \"udp\")", sptr);
  67. sptr = getservbyname ("domain", NULL);
  68. output_servent ("getservbyname (\"domain\", NULL)", sptr);
  69. sptr = getservbyname ("not-existant", NULL);
  70. output_servent ("getservbyname (\"not-existant\", NULL)", sptr);
  71. /* This shouldn't return anything. */
  72. sptr = getservbyname ("", "");
  73. output_servent ("getservbyname (\"\", \"\")", sptr);
  74. sptr = getservbyname ("", "tcp");
  75. output_servent ("getservbyname (\"\", \"tcp\")", sptr);
  76. sptr = getservbyport (htons(53), "tcp");
  77. output_servent ("getservbyport (htons(53), \"tcp\")", sptr);
  78. sptr = getservbyport (htons(53), NULL);
  79. output_servent ("getservbyport (htons(53), NULL)", sptr);
  80. sptr = getservbyport (htons(1), "udp"); /* shouldn't exist */
  81. output_servent ("getservbyport (htons(1), \"udp\")", sptr);
  82. setservent (0);
  83. do
  84. {
  85. sptr = getservent ();
  86. output_servent ("getservent ()", sptr);
  87. }
  88. while (sptr != NULL);
  89. endservent ();
  90. }
  91. static void
  92. output_hostent (const char *call, struct hostent *hptr)
  93. {
  94. char **pptr;
  95. char buf[INET6_ADDRSTRLEN];
  96. if (hptr == NULL)
  97. printf ("Call: %s returned NULL\n", call);
  98. else
  99. {
  100. printf ("Call: %s returned: name: %s, addr_type: %d\n",
  101. call, hptr->h_name, hptr->h_addrtype);
  102. if (hptr->h_aliases)
  103. for (pptr = hptr->h_aliases; *pptr != NULL; pptr++)
  104. printf (" alias: %s\n", *pptr);
  105. for (pptr = hptr->h_addr_list; *pptr != NULL; pptr++)
  106. printf (" ip: %s\n",
  107. inet_ntop (hptr->h_addrtype, *pptr, buf, sizeof (buf)));
  108. }
  109. }
  110. static void
  111. test_hosts (void)
  112. {
  113. struct hostent *hptr1, *hptr2;
  114. char *name = NULL;
  115. size_t namelen = 0;
  116. struct in_addr ip;
  117. hptr1 = gethostbyname ("localhost");
  118. hptr2 = gethostbyname ("LocalHost");
  119. if (hptr1 != NULL || hptr2 != NULL)
  120. {
  121. if (hptr1 == NULL)
  122. {
  123. printf ("localhost not found - but LocalHost found:-(\n");
  124. ++error_count;
  125. }
  126. else if (hptr2 == NULL)
  127. {
  128. printf ("LocalHost not found - but localhost found:-(\n");
  129. ++error_count;
  130. }
  131. else if (strcmp (hptr1->h_name, hptr2->h_name) != 0)
  132. {
  133. printf ("localhost and LocalHost have different canoncial name\n");
  134. printf ("gethostbyname (\"localhost\")->%s\n", hptr1->h_name);
  135. printf ("gethostbyname (\"LocalHost\")->%s\n", hptr2->h_name);
  136. ++error_count;
  137. }
  138. else
  139. output_hostent ("gethostbyname(\"localhost\")", hptr1);
  140. }
  141. hptr1 = gethostbyname ("127.0.0.1");
  142. output_hostent ("gethostbyname (\"127.0.0.1\")", hptr1);
  143. hptr1 = gethostbyname ("10.1234");
  144. output_hostent ("gethostbyname (\"10.1234\")", hptr1);
  145. hptr1 = gethostbyname2 ("localhost", AF_INET);
  146. output_hostent ("gethostbyname2 (\"localhost\", AF_INET)", hptr1);
  147. while (gethostname (name, namelen) < 0 && errno == ENAMETOOLONG)
  148. {
  149. namelen += 2; /* tiny increments to test a lot */
  150. name = xrealloc (name, namelen);
  151. }
  152. if (gethostname (name, namelen) == 0)
  153. {
  154. printf ("Hostname: %s\n", name);
  155. if (name != NULL)
  156. {
  157. hptr1 = gethostbyname (name);
  158. output_hostent ("gethostbyname (gethostname(...))", hptr1);
  159. }
  160. }
  161. ip.s_addr = htonl (INADDR_LOOPBACK);
  162. hptr1 = gethostbyaddr ((char *) &ip, sizeof(ip), AF_INET);
  163. if (hptr1 != NULL)
  164. {
  165. printf ("official name of 127.0.0.1: %s\n", hptr1->h_name);
  166. }
  167. sethostent (0);
  168. do
  169. {
  170. hptr1 = gethostent ();
  171. output_hostent ("gethostent ()", hptr1);
  172. }
  173. while (hptr1 != NULL);
  174. endhostent ();
  175. }
  176. static void
  177. output_netent (const char *call, struct netent *nptr)
  178. {
  179. char **pptr;
  180. if (nptr == NULL)
  181. printf ("Call: %s returned NULL\n", call);
  182. else
  183. {
  184. struct in_addr ip;
  185. ip.s_addr = htonl(nptr->n_net);
  186. printf ("Call: %s, returned: n_name: %s, network_number: %s\n",
  187. call, nptr->n_name, inet_ntoa (ip));
  188. for (pptr = nptr->n_aliases; *pptr != NULL; pptr++)
  189. printf (" alias: %s\n", *pptr);
  190. }
  191. }
  192. static void
  193. test_network (void)
  194. {
  195. struct netent *nptr;
  196. uint32_t ip;
  197. /*
  198. This test needs the following line in /etc/networks:
  199. loopback 127.0.0.0
  200. */
  201. nptr = getnetbyname ("loopback");
  202. output_netent ("getnetbyname (\"loopback\")",nptr);
  203. nptr = getnetbyname ("LoopBACK");
  204. output_netent ("getnetbyname (\"LoopBACK\")",nptr);
  205. ip = inet_network ("127.0.0.0");
  206. nptr = getnetbyaddr (ip, AF_INET);
  207. output_netent ("getnetbyaddr (inet_network (\"127.0.0.0\"), AF_INET)",nptr);
  208. setnetent (0);
  209. do
  210. {
  211. nptr = getnetent ();
  212. output_netent ("getnetent ()", nptr);
  213. }
  214. while (nptr != NULL);
  215. endnetent ();
  216. }
  217. static void
  218. output_protoent (const char *call, struct protoent *prptr)
  219. {
  220. char **pptr;
  221. if (prptr == NULL)
  222. printf ("Call: %s returned NULL\n", call);
  223. else
  224. {
  225. printf ("Call: %s, returned: p_name: %s, p_proto: %d\n",
  226. call, prptr->p_name, prptr->p_proto);
  227. for (pptr = prptr->p_aliases; *pptr != NULL; pptr++)
  228. printf (" alias: %s\n", *pptr);
  229. }
  230. }
  231. static void
  232. test_protocols (void)
  233. {
  234. struct protoent *prptr;
  235. prptr = getprotobyname ("IP");
  236. output_protoent ("getprotobyname (\"IP\")", prptr);
  237. prptr = getprotobynumber (1);
  238. output_protoent ("getprotobynumber (1)", prptr);
  239. setprotoent (0);
  240. do
  241. {
  242. prptr = getprotoent ();
  243. output_protoent ("getprotoent ()", prptr);
  244. }
  245. while (prptr != NULL);
  246. endprotoent ();
  247. }
  248. /* Override /etc/nsswitch.conf for this program. This is mainly
  249. useful for developers. */
  250. static void __attribute__ ((unused))
  251. setdb (const char *dbname)
  252. {
  253. if (strcmp ("db", dbname))
  254. {
  255. /*
  256. db is not implemented for hosts, networks
  257. */
  258. __nss_configure_lookup ("hosts", dbname);
  259. __nss_configure_lookup ("networks", dbname);
  260. }
  261. __nss_configure_lookup ("protocols", dbname);
  262. __nss_configure_lookup ("services", dbname);
  263. }
  264. static int
  265. do_test (void)
  266. {
  267. /*
  268. setdb ("db");
  269. */
  270. test_hosts ();
  271. test_network ();
  272. test_protocols ();
  273. test_services ();
  274. if (error_count)
  275. printf ("\n %d errors occurred!\n", error_count);
  276. else
  277. printf ("No visible errors occurred!\n");
  278. return (error_count != 0);
  279. }
  280. #include <support/test-driver.c>