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