]>
Commit | Line | Data |
---|---|---|
b168057a | 1 | /* Copyright (C) 1998-2015 Free Software Foundation, Inc. |
6591c335 | 2 | This file is part of the GNU C Library. |
3e1f480e | 3 | Contributed by Andreas Jaeger <aj@suse.de>, 1998. |
6591c335 UD |
4 | |
5 | The GNU C Library is free software; you can redistribute it and/or | |
41bdb6e2 AJ |
6 | modify it under the terms of the GNU Lesser General Public |
7 | License as published by the Free Software Foundation; either | |
8 | version 2.1 of the License, or (at your option) any later version. | |
6591c335 UD |
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 | |
41bdb6e2 | 13 | Lesser General Public License for more details. |
6591c335 | 14 | |
41bdb6e2 | 15 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 PE |
16 | License along with the GNU C Library; if not, see |
17 | <http://www.gnu.org/licenses/>. */ | |
6591c335 UD |
18 | |
19 | /* | |
20 | Testing of some network related lookup functions. | |
21 | The system databases looked up are: | |
22 | - /etc/services | |
23 | - /etc/hosts | |
24 | - /etc/networks | |
25 | - /etc/protocols | |
26 | - /etc/rpc | |
27 | The tests try to be fairly generic and simple so that they work on | |
28 | every possible setup (and might therefore not detect some possible | |
29 | errors). | |
30 | */ | |
31 | ||
32 | #include <netdb.h> | |
33 | #include <rpc/netdb.h> | |
34 | #include <stdio.h> | |
3e1f480e AJ |
35 | #include <stdlib.h> |
36 | #include <string.h> | |
6591c335 UD |
37 | #include <arpa/inet.h> |
38 | #include <netinet/in.h> | |
39 | #include <sys/param.h> | |
40 | #include <sys/socket.h> | |
da2d1bc5 | 41 | #include <unistd.h> |
9efc8b95 | 42 | #include <errno.h> |
da2d1bc5 | 43 | #include "nss.h" |
6591c335 UD |
44 | |
45 | /* | |
49c091e5 | 46 | The following define is necessary for glibc 2.0.6 |
6591c335 UD |
47 | */ |
48 | #ifndef INET6_ADDRSTRLEN | |
49 | # define INET6_ADDRSTRLEN 46 | |
50 | #endif | |
51 | ||
52 | int error_count; | |
53 | ||
d6f1fe47 | 54 | static void |
6591c335 UD |
55 | output_servent (const char *call, struct servent *sptr) |
56 | { | |
57 | char **pptr; | |
58 | ||
59 | if (sptr == NULL) | |
60 | printf ("Call: %s returned NULL\n", call); | |
61 | else | |
62 | { | |
63 | printf ("Call: %s, returned: s_name: %s, s_port: %d, s_proto: %s\n", | |
64 | call, sptr->s_name, ntohs(sptr->s_port), sptr->s_proto); | |
65 | for (pptr = sptr->s_aliases; *pptr != NULL; pptr++) | |
66 | printf (" alias: %s\n", *pptr); | |
67 | } | |
68 | } | |
69 | ||
70 | ||
d6f1fe47 | 71 | static void |
6591c335 UD |
72 | test_services (void) |
73 | { | |
74 | struct servent *sptr; | |
75 | ||
76 | sptr = getservbyname ("domain", "tcp"); | |
77 | output_servent ("getservbyname (\"domain\", \"tcp\")", sptr); | |
78 | ||
79 | sptr = getservbyname ("domain", "udp"); | |
80 | output_servent ("getservbyname (\"domain\", \"udp\")", sptr); | |
81 | ||
82 | sptr = getservbyname ("domain", NULL); | |
83 | output_servent ("getservbyname (\"domain\", NULL)", sptr); | |
84 | ||
85 | sptr = getservbyname ("not-existant", NULL); | |
86 | output_servent ("getservbyname (\"not-existant\", NULL)", sptr); | |
87 | ||
dc30f461 UD |
88 | /* This shouldn't return anything. */ |
89 | sptr = getservbyname ("", ""); | |
90 | output_servent ("getservbyname (\"\", \"\")", sptr); | |
91 | ||
92 | sptr = getservbyname ("", "tcp"); | |
93 | output_servent ("getservbyname (\"\", \"tcp\")", sptr); | |
94 | ||
6591c335 UD |
95 | sptr = getservbyport (htons(53), "tcp"); |
96 | output_servent ("getservbyport (htons(53), \"tcp\")", sptr); | |
97 | ||
98 | sptr = getservbyport (htons(53), NULL); | |
99 | output_servent ("getservbyport (htons(53), NULL)", sptr); | |
100 | ||
101 | sptr = getservbyport (htons(1), "udp"); /* shouldn't exist */ | |
102 | output_servent ("getservbyport (htons(1), \"udp\")", sptr); | |
103 | ||
104 | setservent (0); | |
105 | do | |
106 | { | |
107 | sptr = getservent (); | |
108 | output_servent ("getservent ()", sptr); | |
109 | } | |
110 | while (sptr != NULL); | |
111 | endservent (); | |
112 | } | |
113 | ||
114 | ||
d6f1fe47 | 115 | static void |
6591c335 UD |
116 | output_hostent (const char *call, struct hostent *hptr) |
117 | { | |
6591c335 UD |
118 | char **pptr; |
119 | char buf[INET6_ADDRSTRLEN]; | |
120 | ||
121 | if (hptr == NULL) | |
122 | printf ("Call: %s returned NULL\n", call); | |
123 | else | |
124 | { | |
125 | printf ("Call: %s returned: name: %s, addr_type: %d\n", | |
126 | call, hptr->h_name, hptr->h_addrtype); | |
43498f02 AS |
127 | if (hptr->h_aliases) |
128 | for (pptr = hptr->h_aliases; *pptr != NULL; pptr++) | |
129 | printf (" alias: %s\n", *pptr); | |
6591c335 UD |
130 | |
131 | for (pptr = hptr->h_addr_list; *pptr != NULL; pptr++) | |
132 | printf (" ip: %s\n", | |
133 | inet_ntop (hptr->h_addrtype, *pptr, buf, sizeof (buf))); | |
134 | } | |
135 | } | |
136 | ||
d6f1fe47 | 137 | static void |
6591c335 UD |
138 | test_hosts (void) |
139 | { | |
140 | struct hostent *hptr1, *hptr2; | |
9efc8b95 RM |
141 | char *name = NULL; |
142 | size_t namelen = 0; | |
6591c335 UD |
143 | struct in_addr ip; |
144 | ||
145 | hptr1 = gethostbyname ("localhost"); | |
146 | hptr2 = gethostbyname ("LocalHost"); | |
147 | if (hptr1 != NULL || hptr2 != NULL) | |
148 | { | |
149 | if (hptr1 == NULL) | |
150 | { | |
151 | printf ("localhost not found - but LocalHost found:-(\n"); | |
152 | ++error_count; | |
153 | } | |
154 | else if (hptr2 == NULL) | |
155 | { | |
156 | printf ("LocalHost not found - but localhost found:-(\n"); | |
157 | ++error_count; | |
158 | } | |
159 | else if (strcmp (hptr1->h_name, hptr2->h_name) != 0) | |
160 | { | |
161 | printf ("localhost and LocalHost have different canoncial name\n"); | |
162 | printf ("gethostbyname (\"localhost\")->%s\n", hptr1->h_name); | |
163 | printf ("gethostbyname (\"LocalHost\")->%s\n", hptr2->h_name); | |
164 | ++error_count; | |
165 | } | |
166 | else | |
167 | output_hostent ("gethostbyname(\"localhost\")", hptr1); | |
168 | } | |
169 | ||
dc30f461 UD |
170 | hptr1 = gethostbyname ("127.0.0.1"); |
171 | output_hostent ("gethostbyname (\"127.0.0.1\")", hptr1); | |
172 | ||
16b0f634 UD |
173 | hptr1 = gethostbyname ("10.1234"); |
174 | output_hostent ("gethostbyname (\"10.1234\")", hptr1); | |
175 | ||
dc30f461 UD |
176 | hptr1 = gethostbyname2 ("localhost", AF_INET); |
177 | output_hostent ("gethostbyname2 (\"localhost\", AF_INET)", hptr1); | |
6591c335 | 178 | |
9efc8b95 RM |
179 | while (gethostname (name, namelen) < 0 && errno == ENAMETOOLONG) |
180 | { | |
181 | namelen += 2; /* tiny increments to test a lot */ | |
182 | name = realloc (name, namelen); | |
183 | } | |
6591c335 UD |
184 | if (gethostname (name, namelen) == 0) |
185 | { | |
186 | printf ("Hostname: %s\n", name); | |
bb84d3b3 UD |
187 | if (name != NULL) |
188 | { | |
189 | hptr1 = gethostbyname (name); | |
190 | output_hostent ("gethostbyname (gethostname(...))", hptr1); | |
191 | } | |
6591c335 UD |
192 | } |
193 | ||
194 | ip.s_addr = htonl (INADDR_LOOPBACK); | |
bb84d3b3 | 195 | hptr1 = gethostbyaddr ((char *) &ip, sizeof(ip), AF_INET); |
6591c335 UD |
196 | if (hptr1 != NULL) |
197 | { | |
198 | printf ("official name of 127.0.0.1: %s\n", hptr1->h_name); | |
199 | } | |
200 | ||
201 | sethostent (0); | |
202 | do | |
203 | { | |
204 | hptr1 = gethostent (); | |
205 | output_hostent ("gethostent ()", hptr1); | |
206 | } | |
207 | while (hptr1 != NULL); | |
208 | endhostent (); | |
209 | ||
210 | } | |
211 | ||
212 | ||
d6f1fe47 | 213 | static void |
6591c335 UD |
214 | output_netent (const char *call, struct netent *nptr) |
215 | { | |
216 | char **pptr; | |
217 | ||
218 | if (nptr == NULL) | |
219 | printf ("Call: %s returned NULL\n", call); | |
220 | else | |
221 | { | |
222 | struct in_addr ip; | |
223 | ||
224 | ip.s_addr = htonl(nptr->n_net); | |
225 | printf ("Call: %s, returned: n_name: %s, network_number: %s\n", | |
226 | call, nptr->n_name, inet_ntoa (ip)); | |
227 | ||
228 | for (pptr = nptr->n_aliases; *pptr != NULL; pptr++) | |
229 | printf (" alias: %s\n", *pptr); | |
230 | } | |
231 | } | |
232 | ||
d6f1fe47 | 233 | static void |
6591c335 UD |
234 | test_network (void) |
235 | { | |
236 | struct netent *nptr; | |
237 | u_int32_t ip; | |
238 | ||
239 | /* | |
dc30f461 | 240 | This test needs the following line in /etc/networks: |
6591c335 UD |
241 | loopback 127.0.0.0 |
242 | */ | |
243 | nptr = getnetbyname ("loopback"); | |
244 | output_netent ("getnetbyname (\"loopback\")",nptr); | |
245 | ||
dc30f461 UD |
246 | nptr = getnetbyname ("LoopBACK"); |
247 | output_netent ("getnetbyname (\"LoopBACK\")",nptr); | |
248 | ||
6591c335 UD |
249 | ip = inet_network ("127.0.0.0"); |
250 | nptr = getnetbyaddr (ip, AF_INET); | |
251 | output_netent ("getnetbyaddr (inet_network (\"127.0.0.0\"), AF_INET)",nptr); | |
252 | ||
253 | setnetent (0); | |
254 | do | |
255 | { | |
256 | nptr = getnetent (); | |
257 | output_netent ("getnetent ()", nptr); | |
258 | } | |
259 | while (nptr != NULL); | |
260 | endnetent (); | |
261 | } | |
262 | ||
263 | ||
d6f1fe47 | 264 | static void |
6591c335 UD |
265 | output_protoent (const char *call, struct protoent *prptr) |
266 | { | |
267 | char **pptr; | |
268 | ||
269 | if (prptr == NULL) | |
270 | printf ("Call: %s returned NULL\n", call); | |
271 | else | |
272 | { | |
273 | printf ("Call: %s, returned: p_name: %s, p_proto: %d\n", | |
274 | call, prptr->p_name, prptr->p_proto); | |
275 | for (pptr = prptr->p_aliases; *pptr != NULL; pptr++) | |
276 | printf (" alias: %s\n", *pptr); | |
277 | } | |
278 | } | |
279 | ||
280 | ||
d6f1fe47 | 281 | static void |
6591c335 UD |
282 | test_protocols (void) |
283 | { | |
284 | struct protoent *prptr; | |
285 | ||
286 | prptr = getprotobyname ("IP"); | |
287 | output_protoent ("getprotobyname (\"IP\")", prptr); | |
288 | ||
289 | prptr = getprotobynumber (1); | |
290 | output_protoent ("getprotobynumber (1)", prptr); | |
291 | ||
292 | setprotoent (0); | |
293 | do | |
294 | { | |
295 | prptr = getprotoent (); | |
296 | output_protoent ("getprotoent ()", prptr); | |
297 | } | |
298 | while (prptr != NULL); | |
299 | endprotoent (); | |
300 | } | |
301 | ||
302 | ||
d6f1fe47 | 303 | static void |
6591c335 UD |
304 | output_rpcent (const char *call, struct rpcent *rptr) |
305 | { | |
306 | char **pptr; | |
307 | ||
308 | if (rptr == NULL) | |
309 | printf ("Call: %s returned NULL\n", call); | |
310 | else | |
311 | { | |
312 | printf ("Call: %s, returned: r_name: %s, r_number: %d\n", | |
313 | call, rptr->r_name, rptr->r_number); | |
314 | for (pptr = rptr->r_aliases; *pptr != NULL; pptr++) | |
315 | printf (" alias: %s\n", *pptr); | |
316 | } | |
317 | } | |
318 | ||
d6f1fe47 | 319 | static void |
6591c335 UD |
320 | test_rpc (void) |
321 | { | |
322 | struct rpcent *rptr; | |
323 | ||
324 | rptr = getrpcbyname ("portmap"); | |
325 | output_rpcent ("getrpcyname (\"portmap\")", rptr); | |
326 | ||
327 | rptr = getrpcbynumber (100000); | |
328 | output_rpcent ("getrpcbynumber (100000)", rptr); | |
329 | ||
330 | setrpcent (0); | |
331 | do | |
332 | { | |
333 | rptr = getrpcent (); | |
334 | output_rpcent ("getrpcent ()", rptr); | |
335 | } | |
336 | while (rptr != NULL); | |
337 | endrpcent (); | |
338 | } | |
339 | ||
d6f1fe47 AJ |
340 | /* Override /etc/nsswitch.conf for this program. This is mainly |
341 | useful for developers. */ | |
342 | static void __attribute__ ((unused)) | |
6591c335 UD |
343 | setdb (const char *dbname) |
344 | { | |
345 | if (strcmp ("db", dbname)) | |
346 | { | |
347 | /* | |
348 | db is not implemented for hosts, networks | |
349 | */ | |
350 | __nss_configure_lookup ("hosts", dbname); | |
351 | __nss_configure_lookup ("networks", dbname); | |
352 | } | |
353 | __nss_configure_lookup ("protocols", dbname); | |
354 | __nss_configure_lookup ("rpc", dbname); | |
355 | __nss_configure_lookup ("services", dbname); | |
356 | } | |
357 | ||
358 | ||
29955b5d AS |
359 | static int |
360 | do_test (void) | |
6591c335 UD |
361 | { |
362 | /* | |
363 | setdb ("db"); | |
364 | */ | |
dc30f461 | 365 | |
6591c335 UD |
366 | test_hosts (); |
367 | test_network (); | |
368 | test_protocols (); | |
369 | test_rpc (); | |
370 | test_services (); | |
371 | ||
372 | if (error_count) | |
49c091e5 | 373 | printf ("\n %d errors occurred!\n", error_count); |
6591c335 | 374 | else |
49c091e5 | 375 | printf ("No visible errors occurred!\n"); |
6591c335 | 376 | |
d6f1fe47 | 377 | return (error_count != 0); |
6591c335 | 378 | } |
29955b5d AS |
379 | |
380 | #define TEST_FUNCTION do_test () | |
381 | #include "../test-skeleton.c" |