1 /* Copyright (C) 1998-2024 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
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.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <https://www.gnu.org/licenses/>. */
19 Testing of some network related lookup functions.
20 The system databases looked up are:
25 The tests try to be fairly generic and simple so that they work on
26 every possible setup (and might therefore not detect some possible
34 #include <arpa/inet.h>
35 #include <netinet/in.h>
36 #include <sys/param.h>
37 #include <sys/socket.h>
42 #include <support/support.h>
45 The following define is necessary for glibc 2.0.6
47 #ifndef INET6_ADDRSTRLEN
48 # define INET6_ADDRSTRLEN 46
54 output_servent (const char *call
, struct servent
*sptr
)
59 printf ("Call: %s returned NULL\n", call
);
62 printf ("Call: %s, returned: s_name: %s, s_port: %d, s_proto: %s\n",
63 call
, sptr
->s_name
, ntohs(sptr
->s_port
), sptr
->s_proto
);
64 for (pptr
= sptr
->s_aliases
; *pptr
!= NULL
; pptr
++)
65 printf (" alias: %s\n", *pptr
);
75 sptr
= getservbyname ("domain", "tcp");
76 output_servent ("getservbyname (\"domain\", \"tcp\")", sptr
);
78 sptr
= getservbyname ("domain", "udp");
79 output_servent ("getservbyname (\"domain\", \"udp\")", sptr
);
81 sptr
= getservbyname ("domain", NULL
);
82 output_servent ("getservbyname (\"domain\", NULL)", sptr
);
84 sptr
= getservbyname ("not-existant", NULL
);
85 output_servent ("getservbyname (\"not-existant\", NULL)", sptr
);
87 /* This shouldn't return anything. */
88 sptr
= getservbyname ("", "");
89 output_servent ("getservbyname (\"\", \"\")", sptr
);
91 sptr
= getservbyname ("", "tcp");
92 output_servent ("getservbyname (\"\", \"tcp\")", sptr
);
94 sptr
= getservbyport (htons(53), "tcp");
95 output_servent ("getservbyport (htons(53), \"tcp\")", sptr
);
97 sptr
= getservbyport (htons(53), NULL
);
98 output_servent ("getservbyport (htons(53), NULL)", sptr
);
100 sptr
= getservbyport (htons(1), "udp"); /* shouldn't exist */
101 output_servent ("getservbyport (htons(1), \"udp\")", sptr
);
106 sptr
= getservent ();
107 output_servent ("getservent ()", sptr
);
109 while (sptr
!= NULL
);
115 output_hostent (const char *call
, struct hostent
*hptr
)
118 char buf
[INET6_ADDRSTRLEN
];
121 printf ("Call: %s returned NULL\n", call
);
124 printf ("Call: %s returned: name: %s, addr_type: %d\n",
125 call
, hptr
->h_name
, hptr
->h_addrtype
);
127 for (pptr
= hptr
->h_aliases
; *pptr
!= NULL
; pptr
++)
128 printf (" alias: %s\n", *pptr
);
130 for (pptr
= hptr
->h_addr_list
; *pptr
!= NULL
; pptr
++)
132 inet_ntop (hptr
->h_addrtype
, *pptr
, buf
, sizeof (buf
)));
139 struct hostent
*hptr1
, *hptr2
;
144 hptr1
= gethostbyname ("localhost");
145 hptr2
= gethostbyname ("LocalHost");
146 if (hptr1
!= NULL
|| hptr2
!= NULL
)
150 printf ("localhost not found - but LocalHost found:-(\n");
153 else if (hptr2
== NULL
)
155 printf ("LocalHost not found - but localhost found:-(\n");
158 else if (strcmp (hptr1
->h_name
, hptr2
->h_name
) != 0)
160 printf ("localhost and LocalHost have different canonical name\n");
161 printf ("gethostbyname (\"localhost\")->%s\n", hptr1
->h_name
);
162 printf ("gethostbyname (\"LocalHost\")->%s\n", hptr2
->h_name
);
166 output_hostent ("gethostbyname(\"localhost\")", hptr1
);
169 hptr1
= gethostbyname ("127.0.0.1");
170 output_hostent ("gethostbyname (\"127.0.0.1\")", hptr1
);
172 hptr1
= gethostbyname ("10.1234");
173 output_hostent ("gethostbyname (\"10.1234\")", hptr1
);
175 hptr1
= gethostbyname2 ("localhost", AF_INET
);
176 output_hostent ("gethostbyname2 (\"localhost\", AF_INET)", hptr1
);
178 while (gethostname (name
, namelen
) < 0 && errno
== ENAMETOOLONG
)
180 namelen
+= 2; /* tiny increments to test a lot */
181 name
= xrealloc (name
, namelen
);
183 if (gethostname (name
, namelen
) == 0)
185 printf ("Hostname: %s\n", name
);
188 hptr1
= gethostbyname (name
);
189 output_hostent ("gethostbyname (gethostname(...))", hptr1
);
193 ip
.s_addr
= htonl (INADDR_LOOPBACK
);
194 hptr1
= gethostbyaddr ((char *) &ip
, sizeof (ip
), AF_INET
);
197 printf ("official name of 127.0.0.1: %s\n", hptr1
->h_name
);
203 hptr1
= gethostent ();
204 output_hostent ("gethostent ()", hptr1
);
206 while (hptr1
!= NULL
);
213 output_netent (const char *call
, struct netent
*nptr
)
218 printf ("Call: %s returned NULL\n", call
);
223 ip
.s_addr
= htonl(nptr
->n_net
);
224 printf ("Call: %s, returned: n_name: %s, network_number: %s\n",
225 call
, nptr
->n_name
, inet_ntoa (ip
));
227 for (pptr
= nptr
->n_aliases
; *pptr
!= NULL
; pptr
++)
228 printf (" alias: %s\n", *pptr
);
239 This test needs the following line in /etc/networks:
242 nptr
= getnetbyname ("loopback");
243 output_netent ("getnetbyname (\"loopback\")",nptr
);
245 nptr
= getnetbyname ("LoopBACK");
246 output_netent ("getnetbyname (\"LoopBACK\")",nptr
);
248 ip
= inet_network ("127.0.0.0");
249 nptr
= getnetbyaddr (ip
, AF_INET
);
250 output_netent ("getnetbyaddr (inet_network (\"127.0.0.0\"), AF_INET)",nptr
);
256 output_netent ("getnetent ()", nptr
);
258 while (nptr
!= NULL
);
264 output_protoent (const char *call
, struct protoent
*prptr
)
269 printf ("Call: %s returned NULL\n", call
);
272 printf ("Call: %s, returned: p_name: %s, p_proto: %d\n",
273 call
, prptr
->p_name
, prptr
->p_proto
);
274 for (pptr
= prptr
->p_aliases
; *pptr
!= NULL
; pptr
++)
275 printf (" alias: %s\n", *pptr
);
281 test_protocols (void)
283 struct protoent
*prptr
;
285 prptr
= getprotobyname ("IP");
286 output_protoent ("getprotobyname (\"IP\")", prptr
);
288 prptr
= getprotobynumber (1);
289 output_protoent ("getprotobynumber (1)", prptr
);
294 prptr
= getprotoent ();
295 output_protoent ("getprotoent ()", prptr
);
297 while (prptr
!= NULL
);
302 /* Override /etc/nsswitch.conf for this program. This is mainly
303 useful for developers. */
304 static void __attribute__ ((unused
))
305 setdb (const char *dbname
)
307 if (strcmp ("db", dbname
))
310 db is not implemented for hosts, networks
312 __nss_configure_lookup ("hosts", dbname
);
313 __nss_configure_lookup ("networks", dbname
);
315 __nss_configure_lookup ("protocols", dbname
);
316 __nss_configure_lookup ("services", dbname
);
333 printf ("\n %d errors occurred!\n", error_count
);
335 printf ("No visible errors occurred!\n");
337 return (error_count
!= 0);
340 #include <support/test-driver.c>