Update.
[glibc.git] / nss / test-netdb.c
blob7f1a767b4e291cb46f219b90645375ed24e80e7b
1 /* Copyright (C) 1998 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Andreas Jaeger <aj@arthur.rhein-neckar.de>, 1998.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
21 Testing of some network related lookup functions.
22 The system databases looked up are:
23 - /etc/services
24 - /etc/hosts
25 - /etc/networks
26 - /etc/protocols
27 - /etc/rpc
28 The tests try to be fairly generic and simple so that they work on
29 every possible setup (and might therefore not detect some possible
30 errors).
33 #include <netdb.h>
34 #include <rpc/netdb.h>
35 #include <stdio.h>
36 #include <arpa/inet.h>
37 #include <netinet/in.h>
38 #include <sys/param.h>
39 #include <sys/socket.h>
40 #include <unistd.h>
41 #include "nss.h"
44 The following define is neccessary for glibc 2.0.6
46 #ifndef INET6_ADDRSTRLEN
47 # define INET6_ADDRSTRLEN 46
48 #endif
50 int error_count;
52 void
53 output_servent (const char *call, struct servent *sptr)
55 char **pptr;
57 if (sptr == NULL)
58 printf ("Call: %s returned NULL\n", call);
59 else
61 printf ("Call: %s, returned: s_name: %s, s_port: %d, s_proto: %s\n",
62 call, sptr->s_name, ntohs(sptr->s_port), sptr->s_proto);
63 for (pptr = sptr->s_aliases; *pptr != NULL; pptr++)
64 printf (" alias: %s\n", *pptr);
69 void
70 test_services (void)
72 struct servent *sptr;
74 sptr = getservbyname ("domain", "tcp");
75 output_servent ("getservbyname (\"domain\", \"tcp\")", sptr);
77 sptr = getservbyname ("domain", "udp");
78 output_servent ("getservbyname (\"domain\", \"udp\")", sptr);
80 sptr = getservbyname ("domain", NULL);
81 output_servent ("getservbyname (\"domain\", NULL)", sptr);
83 sptr = getservbyname ("not-existant", NULL);
84 output_servent ("getservbyname (\"not-existant\", NULL)", sptr);
86 /* This shouldn't return anything. */
87 sptr = getservbyname ("", "");
88 output_servent ("getservbyname (\"\", \"\")", sptr);
90 sptr = getservbyname ("", "tcp");
91 output_servent ("getservbyname (\"\", \"tcp\")", sptr);
93 sptr = getservbyport (htons(53), "tcp");
94 output_servent ("getservbyport (htons(53), \"tcp\")", sptr);
96 sptr = getservbyport (htons(53), NULL);
97 output_servent ("getservbyport (htons(53), NULL)", sptr);
99 sptr = getservbyport (htons(1), "udp"); /* shouldn't exist */
100 output_servent ("getservbyport (htons(1), \"udp\")", sptr);
102 setservent (0);
105 sptr = getservent ();
106 output_servent ("getservent ()", sptr);
108 while (sptr != NULL);
109 endservent ();
113 void
114 output_hostent (const char *call, struct hostent *hptr)
116 char **pptr;
117 char buf[INET6_ADDRSTRLEN];
119 if (hptr == NULL)
120 printf ("Call: %s returned NULL\n", call);
121 else
123 printf ("Call: %s returned: name: %s, addr_type: %d\n",
124 call, hptr->h_name, hptr->h_addrtype);
125 for (pptr = hptr->h_aliases; *pptr != NULL; pptr++)
126 printf (" alias: %s\n", *pptr);
128 for (pptr = hptr->h_addr_list; *pptr != NULL; pptr++)
129 printf (" ip: %s\n",
130 inet_ntop (hptr->h_addrtype, *pptr, buf, sizeof (buf)));
134 void
135 test_hosts (void)
137 struct hostent *hptr1, *hptr2;
138 char name[MAXHOSTNAMELEN];
139 size_t namelen = sizeof(name);
140 struct in_addr ip;
142 hptr1 = gethostbyname ("localhost");
143 hptr2 = gethostbyname ("LocalHost");
144 if (hptr1 != NULL || hptr2 != NULL)
146 if (hptr1 == NULL)
148 printf ("localhost not found - but LocalHost found:-(\n");
149 ++error_count;
151 else if (hptr2 == NULL)
153 printf ("LocalHost not found - but localhost found:-(\n");
154 ++error_count;
156 else if (strcmp (hptr1->h_name, hptr2->h_name) != 0)
158 printf ("localhost and LocalHost have different canoncial name\n");
159 printf ("gethostbyname (\"localhost\")->%s\n", hptr1->h_name);
160 printf ("gethostbyname (\"LocalHost\")->%s\n", hptr2->h_name);
161 ++error_count;
163 else
164 output_hostent ("gethostbyname(\"localhost\")", hptr1);
167 hptr1 = gethostbyname ("127.0.0.1");
168 output_hostent ("gethostbyname (\"127.0.0.1\")", hptr1);
170 hptr1 = gethostbyname2 ("localhost", AF_INET);
171 output_hostent ("gethostbyname2 (\"localhost\", AF_INET)", hptr1);
173 if (gethostname (name, namelen) == 0)
175 printf ("Hostname: %s\n", name);
176 hptr1 = gethostbyname (name);
177 output_hostent ("gethostbyname (gethostname(...))", hptr1);
180 ip.s_addr = htonl (INADDR_LOOPBACK);
181 hptr1 = gethostbyaddr ((char *)&ip, sizeof(ip), AF_INET);
182 if (hptr1 != NULL)
184 printf ("official name of 127.0.0.1: %s\n", hptr1->h_name);
187 sethostent (0);
190 hptr1 = gethostent ();
191 output_hostent ("gethostent ()", hptr1);
193 while (hptr1 != NULL);
194 endhostent ();
199 void
200 output_netent (const char *call, struct netent *nptr)
202 char **pptr;
204 if (nptr == NULL)
205 printf ("Call: %s returned NULL\n", call);
206 else
208 struct in_addr ip;
210 ip.s_addr = htonl(nptr->n_net);
211 printf ("Call: %s, returned: n_name: %s, network_number: %s\n",
212 call, nptr->n_name, inet_ntoa (ip));
214 for (pptr = nptr->n_aliases; *pptr != NULL; pptr++)
215 printf (" alias: %s\n", *pptr);
219 void
220 test_network (void)
222 struct netent *nptr;
223 u_int32_t ip;
226 This test needs the following line in /etc/networks:
227 loopback 127.0.0.0
229 nptr = getnetbyname ("loopback");
230 output_netent ("getnetbyname (\"loopback\")",nptr);
232 nptr = getnetbyname ("LoopBACK");
233 output_netent ("getnetbyname (\"LoopBACK\")",nptr);
235 ip = inet_network ("127.0.0.0");
236 nptr = getnetbyaddr (ip, AF_INET);
237 output_netent ("getnetbyaddr (inet_network (\"127.0.0.0\"), AF_INET)",nptr);
239 setnetent (0);
242 nptr = getnetent ();
243 output_netent ("getnetent ()", nptr);
245 while (nptr != NULL);
246 endnetent ();
250 void
251 output_protoent (const char *call, struct protoent *prptr)
253 char **pptr;
255 if (prptr == NULL)
256 printf ("Call: %s returned NULL\n", call);
257 else
259 printf ("Call: %s, returned: p_name: %s, p_proto: %d\n",
260 call, prptr->p_name, prptr->p_proto);
261 for (pptr = prptr->p_aliases; *pptr != NULL; pptr++)
262 printf (" alias: %s\n", *pptr);
267 void
268 test_protocols (void)
270 struct protoent *prptr;
272 prptr = getprotobyname ("IP");
273 output_protoent ("getprotobyname (\"IP\")", prptr);
275 prptr = getprotobynumber (1);
276 output_protoent ("getprotobynumber (1)", prptr);
278 setprotoent (0);
281 prptr = getprotoent ();
282 output_protoent ("getprotoent ()", prptr);
284 while (prptr != NULL);
285 endprotoent ();
289 void
290 output_rpcent (const char *call, struct rpcent *rptr)
292 char **pptr;
294 if (rptr == NULL)
295 printf ("Call: %s returned NULL\n", call);
296 else
298 printf ("Call: %s, returned: r_name: %s, r_number: %d\n",
299 call, rptr->r_name, rptr->r_number);
300 for (pptr = rptr->r_aliases; *pptr != NULL; pptr++)
301 printf (" alias: %s\n", *pptr);
305 void
306 test_rpc (void)
308 struct rpcent *rptr;
310 rptr = getrpcbyname ("portmap");
311 output_rpcent ("getrpcyname (\"portmap\")", rptr);
313 rptr = getrpcbynumber (100000);
314 output_rpcent ("getrpcbynumber (100000)", rptr);
316 setrpcent (0);
319 rptr = getrpcent ();
320 output_rpcent ("getrpcent ()", rptr);
322 while (rptr != NULL);
323 endrpcent ();
327 Override /etc/nsswitch.conf for this program.
328 This is mainly useful for developers
330 void
331 setdb (const char *dbname)
333 if (strcmp ("db", dbname))
336 db is not implemented for hosts, networks
338 __nss_configure_lookup ("hosts", dbname);
339 __nss_configure_lookup ("networks", dbname);
341 __nss_configure_lookup ("protocols", dbname);
342 __nss_configure_lookup ("rpc", dbname);
343 __nss_configure_lookup ("services", dbname);
348 main (void)
351 setdb ("db");
354 test_hosts ();
355 test_network ();
356 test_protocols ();
357 test_rpc ();
358 test_services ();
360 if (error_count)
361 printf ("\n %d errors occured!\n", error_count);
362 else
363 printf ("No visible errors occured!\n");
365 exit (error_count);