]>
Commit | Line | Data |
---|---|---|
7f1deee6 | 1 | /* Test listing of network interface addresses. |
dff8da6b | 2 | Copyright (C) 2002-2024 Free Software Foundation, Inc. |
7f1deee6 RM |
3 | This file is part of the GNU C Library. |
4 | ||
5 | The GNU C Library is free software; you can redistribute it and/or | |
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. | |
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 | Lesser General Public License for more details. | |
14 | ||
15 | You should have received a copy of the GNU Lesser General Public | |
59ba27a6 | 16 | License along with the GNU C Library; if not, see |
5a82c748 | 17 | <https://www.gnu.org/licenses/>. */ |
7f1deee6 RM |
18 | |
19 | #include <errno.h> | |
20 | #include <stdio.h> | |
21 | #include <stdlib.h> | |
22 | #include <string.h> | |
23 | #include <ifaddrs.h> | |
24 | #include <netinet/in.h> | |
25 | #include <arpa/inet.h> | |
26 | ||
abca9f7f UD |
27 | static int failures; |
28 | ||
29 | static const char * | |
30 | addr_string (struct sockaddr *sa, char *buf, size_t size) | |
31 | { | |
32 | if (sa == NULL) | |
33 | return "<none>"; | |
34 | ||
35 | switch (sa->sa_family) | |
36 | { | |
37 | case AF_INET: | |
38 | return inet_ntop (AF_INET, &((struct sockaddr_in *) sa)->sin_addr, | |
39 | buf, size); | |
40 | case AF_INET6: | |
41 | return inet_ntop (AF_INET6, &((struct sockaddr_in6 *) sa)->sin6_addr, | |
42 | buf, size); | |
43 | #ifdef AF_LINK | |
44 | case AF_LINK: | |
45 | return "<link>"; | |
46 | #endif | |
47 | case AF_UNSPEC: | |
48 | return "---"; | |
49 | ||
fbf86dda | 50 | #ifdef AF_PACKET |
abca9f7f UD |
51 | case AF_PACKET: |
52 | return "<packet>"; | |
fbf86dda | 53 | #endif |
abca9f7f UD |
54 | |
55 | default: | |
56 | ++failures; | |
57 | printf ("sa_family=%d %08x\n", sa->sa_family, | |
58 | *(int*)&((struct sockaddr_in *) sa)->sin_addr.s_addr); | |
59 | return "<unexpected sockaddr family>"; | |
60 | } | |
61 | } | |
62 | ||
63 | ||
c9738df4 UD |
64 | static int |
65 | do_test (void) | |
7f1deee6 | 66 | { |
7f1deee6 RM |
67 | struct ifaddrs *ifaces, *ifa; |
68 | ||
69 | if (getifaddrs (&ifaces) < 0) | |
70 | { | |
71 | if (errno != ENOSYS) | |
72 | { | |
73 | printf ("Couldn't get any interfaces: %s.\n", strerror (errno)); | |
74 | exit (1); | |
75 | } | |
76 | /* The function is simply not implemented. */ | |
77 | exit (0); | |
78 | } | |
79 | ||
80 | puts ("\ | |
81 | Name Flags Address Netmask Broadcast/Destination"); | |
82 | ||
83 | for (ifa = ifaces; ifa != NULL; ifa = ifa->ifa_next) | |
84 | { | |
85 | char abuf[64], mbuf[64], dbuf[64]; | |
7f1deee6 RM |
86 | printf ("%-15s%#.4x %-15s %-15s %-15s\n", |
87 | ifa->ifa_name, ifa->ifa_flags, | |
abca9f7f UD |
88 | addr_string (ifa->ifa_addr, abuf, sizeof (abuf)), |
89 | addr_string (ifa->ifa_netmask, mbuf, sizeof (mbuf)), | |
90 | addr_string (ifa->ifa_broadaddr, dbuf, sizeof (dbuf))); | |
7f1deee6 RM |
91 | } |
92 | ||
93 | freeifaddrs (ifaces); | |
94 | ||
95 | return failures ? 1 : 0; | |
96 | } | |
c9738df4 UD |
97 | |
98 | #define TEST_FUNCTION do_test () | |
99 | #include "../test-skeleton.c" |