| 1 | /* Test listing of network interface addresses.
|
|---|
| 2 | Copyright (C) 2002, 2004, 2005 Free Software Foundation, Inc.
|
|---|
| 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
|
|---|
| 16 | License along with the GNU C Library; if not, write to the Free
|
|---|
| 17 | Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
|---|
| 18 | 02111-1307 USA. */
|
|---|
| 19 |
|
|---|
| 20 | #include <errno.h>
|
|---|
| 21 | #include <stdio.h>
|
|---|
| 22 | #include <stdlib.h>
|
|---|
| 23 | #include <string.h>
|
|---|
| 24 | #ifdef __BSD__
|
|---|
| 25 | # include <sys/types.h>
|
|---|
| 26 | # include <sys/socket.h>
|
|---|
| 27 | #endif
|
|---|
| 28 | #include <ifaddrs.h>
|
|---|
| 29 | #include <netinet/in.h>
|
|---|
| 30 | #include <arpa/inet.h>
|
|---|
| 31 |
|
|---|
| 32 | static int failures;
|
|---|
| 33 |
|
|---|
| 34 | static const char *
|
|---|
| 35 | addr_string (struct sockaddr *sa, char *buf, size_t size)
|
|---|
| 36 | {
|
|---|
| 37 | if (sa == NULL)
|
|---|
| 38 | return "<none>";
|
|---|
| 39 |
|
|---|
| 40 | switch (sa->sa_family)
|
|---|
| 41 | {
|
|---|
| 42 | case AF_INET:
|
|---|
| 43 | return inet_ntop (AF_INET, &((struct sockaddr_in *) sa)->sin_addr,
|
|---|
| 44 | buf, size);
|
|---|
| 45 | case AF_INET6:
|
|---|
| 46 | return inet_ntop (AF_INET6, &((struct sockaddr_in6 *) sa)->sin6_addr,
|
|---|
| 47 | buf, size);
|
|---|
| 48 | #ifdef AF_LINK
|
|---|
| 49 | case AF_LINK:
|
|---|
| 50 | return "<link>";
|
|---|
| 51 | #endif
|
|---|
| 52 | case AF_UNSPEC:
|
|---|
| 53 | return "---";
|
|---|
| 54 |
|
|---|
| 55 | #ifdef AF_PACKET
|
|---|
| 56 | case AF_PACKET:
|
|---|
| 57 | return "<packet>";
|
|---|
| 58 | #endif
|
|---|
| 59 |
|
|---|
| 60 | default:
|
|---|
| 61 | ++failures;
|
|---|
| 62 | printf ("sa_family=%d %08x\n", sa->sa_family,
|
|---|
| 63 | *(int*)&((struct sockaddr_in *) sa)->sin_addr.s_addr);
|
|---|
| 64 | return "<unexpected sockaddr family>";
|
|---|
| 65 | }
|
|---|
| 66 | }
|
|---|
| 67 |
|
|---|
| 68 |
|
|---|
| 69 | static int
|
|---|
| 70 | do_test (void)
|
|---|
| 71 | {
|
|---|
| 72 | struct ifaddrs *ifaces, *ifa;
|
|---|
| 73 |
|
|---|
| 74 | if (getifaddrs (&ifaces) < 0)
|
|---|
| 75 | {
|
|---|
| 76 | if (errno != ENOSYS)
|
|---|
| 77 | {
|
|---|
| 78 | printf ("Couldn't get any interfaces: %s.\n", strerror (errno));
|
|---|
| 79 | exit (1);
|
|---|
| 80 | }
|
|---|
| 81 | /* The function is simply not implemented. */
|
|---|
| 82 | exit (0);
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 | puts ("\
|
|---|
| 86 | Name Flags Address Netmask Broadcast/Destination");
|
|---|
| 87 |
|
|---|
| 88 | for (ifa = ifaces; ifa != NULL; ifa = ifa->ifa_next)
|
|---|
| 89 | {
|
|---|
| 90 | char abuf[64], mbuf[64], dbuf[64];
|
|---|
| 91 | printf ("%-15s%#.4x %-15s %-15s %-15s\n",
|
|---|
| 92 | ifa->ifa_name, ifa->ifa_flags,
|
|---|
| 93 | addr_string (ifa->ifa_addr, abuf, sizeof (abuf)),
|
|---|
| 94 | addr_string (ifa->ifa_netmask, mbuf, sizeof (mbuf)),
|
|---|
| 95 | addr_string (ifa->ifa_broadaddr, dbuf, sizeof (dbuf)));
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | freeifaddrs (ifaces);
|
|---|
| 99 |
|
|---|
| 100 | return failures ? 1 : 0;
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | #define TEST_FUNCTION do_test ()
|
|---|
| 104 | #include "../test-skeleton.c"
|
|---|