]> git.ipfire.org Git - thirdparty/glibc.git/blob - nss/test-netdb.c
Update.
[thirdparty/glibc.git] / nss / test-netdb.c
1 /* Copyright (C) 1998, 1999 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.
4
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.
9
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.
14
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. */
19
20 /*
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).
31 */
32
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"
42
43 /*
44 The following define is necessary for glibc 2.0.6
45 */
46 #ifndef INET6_ADDRSTRLEN
47 # define INET6_ADDRSTRLEN 46
48 #endif
49
50 int error_count;
51
52 void
53 output_servent (const char *call, struct servent *sptr)
54 {
55 char **pptr;
56
57 if (sptr == NULL)
58 printf ("Call: %s returned NULL\n", call);
59 else
60 {
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);
65 }
66 }
67
68
69 void
70 test_services (void)
71 {
72 struct servent *sptr;
73
74 sptr = getservbyname ("domain", "tcp");
75 output_servent ("getservbyname (\"domain\", \"tcp\")", sptr);
76
77 sptr = getservbyname ("domain", "udp");
78 output_servent ("getservbyname (\"domain\", \"udp\")", sptr);
79
80 sptr = getservbyname ("domain", NULL);
81 output_servent ("getservbyname (\"domain\", NULL)", sptr);
82
83 sptr = getservbyname ("not-existant", NULL);
84 output_servent ("getservbyname (\"not-existant\", NULL)", sptr);
85
86 /* This shouldn't return anything. */
87 sptr = getservbyname ("", "");
88 output_servent ("getservbyname (\"\", \"\")", sptr);
89
90 sptr = getservbyname ("", "tcp");
91 output_servent ("getservbyname (\"\", \"tcp\")", sptr);
92
93 sptr = getservbyport (htons(53), "tcp");
94 output_servent ("getservbyport (htons(53), \"tcp\")", sptr);
95
96 sptr = getservbyport (htons(53), NULL);
97 output_servent ("getservbyport (htons(53), NULL)", sptr);
98
99 sptr = getservbyport (htons(1), "udp"); /* shouldn't exist */
100 output_servent ("getservbyport (htons(1), \"udp\")", sptr);
101
102 setservent (0);
103 do
104 {
105 sptr = getservent ();
106 output_servent ("getservent ()", sptr);
107 }
108 while (sptr != NULL);
109 endservent ();
110 }
111
112
113 void
114 output_hostent (const char *call, struct hostent *hptr)
115 {
116 char **pptr;
117 char buf[INET6_ADDRSTRLEN];
118
119 if (hptr == NULL)
120 printf ("Call: %s returned NULL\n", call);
121 else
122 {
123 printf ("Call: %s returned: name: %s, addr_type: %d\n",
124 call, hptr->h_name, hptr->h_addrtype);
125 if (hptr->h_aliases)
126 for (pptr = hptr->h_aliases; *pptr != NULL; pptr++)
127 printf (" alias: %s\n", *pptr);
128
129 for (pptr = hptr->h_addr_list; *pptr != NULL; pptr++)
130 printf (" ip: %s\n",
131 inet_ntop (hptr->h_addrtype, *pptr, buf, sizeof (buf)));
132 }
133 }
134
135 void
136 test_hosts (void)
137 {
138 struct hostent *hptr1, *hptr2;
139 char name[MAXHOSTNAMELEN];
140 size_t namelen = sizeof(name);
141 struct in_addr ip;
142
143 hptr1 = gethostbyname ("localhost");
144 hptr2 = gethostbyname ("LocalHost");
145 if (hptr1 != NULL || hptr2 != NULL)
146 {
147 if (hptr1 == NULL)
148 {
149 printf ("localhost not found - but LocalHost found:-(\n");
150 ++error_count;
151 }
152 else if (hptr2 == NULL)
153 {
154 printf ("LocalHost not found - but localhost found:-(\n");
155 ++error_count;
156 }
157 else if (strcmp (hptr1->h_name, hptr2->h_name) != 0)
158 {
159 printf ("localhost and LocalHost have different canoncial name\n");
160 printf ("gethostbyname (\"localhost\")->%s\n", hptr1->h_name);
161 printf ("gethostbyname (\"LocalHost\")->%s\n", hptr2->h_name);
162 ++error_count;
163 }
164 else
165 output_hostent ("gethostbyname(\"localhost\")", hptr1);
166 }
167
168 hptr1 = gethostbyname ("127.0.0.1");
169 output_hostent ("gethostbyname (\"127.0.0.1\")", hptr1);
170
171 hptr1 = gethostbyname ("10.1234");
172 output_hostent ("gethostbyname (\"10.1234\")", hptr1);
173
174 hptr1 = gethostbyname2 ("localhost", AF_INET);
175 output_hostent ("gethostbyname2 (\"localhost\", AF_INET)", hptr1);
176
177 if (gethostname (name, namelen) == 0)
178 {
179 printf ("Hostname: %s\n", name);
180 hptr1 = gethostbyname (name);
181 output_hostent ("gethostbyname (gethostname(...))", hptr1);
182 }
183
184 ip.s_addr = htonl (INADDR_LOOPBACK);
185 hptr1 = gethostbyaddr ((char *)&ip, sizeof(ip), AF_INET);
186 if (hptr1 != NULL)
187 {
188 printf ("official name of 127.0.0.1: %s\n", hptr1->h_name);
189 }
190
191 sethostent (0);
192 do
193 {
194 hptr1 = gethostent ();
195 output_hostent ("gethostent ()", hptr1);
196 }
197 while (hptr1 != NULL);
198 endhostent ();
199
200 }
201
202
203 void
204 output_netent (const char *call, struct netent *nptr)
205 {
206 char **pptr;
207
208 if (nptr == NULL)
209 printf ("Call: %s returned NULL\n", call);
210 else
211 {
212 struct in_addr ip;
213
214 ip.s_addr = htonl(nptr->n_net);
215 printf ("Call: %s, returned: n_name: %s, network_number: %s\n",
216 call, nptr->n_name, inet_ntoa (ip));
217
218 for (pptr = nptr->n_aliases; *pptr != NULL; pptr++)
219 printf (" alias: %s\n", *pptr);
220 }
221 }
222
223 void
224 test_network (void)
225 {
226 struct netent *nptr;
227 u_int32_t ip;
228
229 /*
230 This test needs the following line in /etc/networks:
231 loopback 127.0.0.0
232 */
233 nptr = getnetbyname ("loopback");
234 output_netent ("getnetbyname (\"loopback\")",nptr);
235
236 nptr = getnetbyname ("LoopBACK");
237 output_netent ("getnetbyname (\"LoopBACK\")",nptr);
238
239 ip = inet_network ("127.0.0.0");
240 nptr = getnetbyaddr (ip, AF_INET);
241 output_netent ("getnetbyaddr (inet_network (\"127.0.0.0\"), AF_INET)",nptr);
242
243 setnetent (0);
244 do
245 {
246 nptr = getnetent ();
247 output_netent ("getnetent ()", nptr);
248 }
249 while (nptr != NULL);
250 endnetent ();
251 }
252
253
254 void
255 output_protoent (const char *call, struct protoent *prptr)
256 {
257 char **pptr;
258
259 if (prptr == NULL)
260 printf ("Call: %s returned NULL\n", call);
261 else
262 {
263 printf ("Call: %s, returned: p_name: %s, p_proto: %d\n",
264 call, prptr->p_name, prptr->p_proto);
265 for (pptr = prptr->p_aliases; *pptr != NULL; pptr++)
266 printf (" alias: %s\n", *pptr);
267 }
268 }
269
270
271 void
272 test_protocols (void)
273 {
274 struct protoent *prptr;
275
276 prptr = getprotobyname ("IP");
277 output_protoent ("getprotobyname (\"IP\")", prptr);
278
279 prptr = getprotobynumber (1);
280 output_protoent ("getprotobynumber (1)", prptr);
281
282 setprotoent (0);
283 do
284 {
285 prptr = getprotoent ();
286 output_protoent ("getprotoent ()", prptr);
287 }
288 while (prptr != NULL);
289 endprotoent ();
290 }
291
292
293 void
294 output_rpcent (const char *call, struct rpcent *rptr)
295 {
296 char **pptr;
297
298 if (rptr == NULL)
299 printf ("Call: %s returned NULL\n", call);
300 else
301 {
302 printf ("Call: %s, returned: r_name: %s, r_number: %d\n",
303 call, rptr->r_name, rptr->r_number);
304 for (pptr = rptr->r_aliases; *pptr != NULL; pptr++)
305 printf (" alias: %s\n", *pptr);
306 }
307 }
308
309 void
310 test_rpc (void)
311 {
312 struct rpcent *rptr;
313
314 rptr = getrpcbyname ("portmap");
315 output_rpcent ("getrpcyname (\"portmap\")", rptr);
316
317 rptr = getrpcbynumber (100000);
318 output_rpcent ("getrpcbynumber (100000)", rptr);
319
320 setrpcent (0);
321 do
322 {
323 rptr = getrpcent ();
324 output_rpcent ("getrpcent ()", rptr);
325 }
326 while (rptr != NULL);
327 endrpcent ();
328 }
329
330 /*
331 Override /etc/nsswitch.conf for this program.
332 This is mainly useful for developers
333 */
334 void
335 setdb (const char *dbname)
336 {
337 if (strcmp ("db", dbname))
338 {
339 /*
340 db is not implemented for hosts, networks
341 */
342 __nss_configure_lookup ("hosts", dbname);
343 __nss_configure_lookup ("networks", dbname);
344 }
345 __nss_configure_lookup ("protocols", dbname);
346 __nss_configure_lookup ("rpc", dbname);
347 __nss_configure_lookup ("services", dbname);
348 }
349
350
351 int
352 main (void)
353 {
354 /*
355 setdb ("db");
356 */
357
358 test_hosts ();
359 test_network ();
360 test_protocols ();
361 test_rpc ();
362 test_services ();
363
364 if (error_count)
365 printf ("\n %d errors occurred!\n", error_count);
366 else
367 printf ("No visible errors occurred!\n");
368
369 exit (error_count);
370 }