source: vendor/glibc-tests/glibc/inet/test-ifaddrs.c

Last change on this file was 2036, checked in by bird, 20 years ago

Initial revision

  • Property cvs2svn:cvs-rev set to 1.1
  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 2.6 KB
Line 
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#include <ifaddrs.h>
25#include <netinet/in.h>
26#include <arpa/inet.h>
27
28static int failures;
29
30static const char *
31addr_string (struct sockaddr *sa, char *buf, size_t size)
32{
33 if (sa == NULL)
34 return "<none>";
35
36 switch (sa->sa_family)
37 {
38 case AF_INET:
39 return inet_ntop (AF_INET, &((struct sockaddr_in *) sa)->sin_addr,
40 buf, size);
41 case AF_INET6:
42 return inet_ntop (AF_INET6, &((struct sockaddr_in6 *) sa)->sin6_addr,
43 buf, size);
44#ifdef AF_LINK
45 case AF_LINK:
46 return "<link>";
47#endif
48 case AF_UNSPEC:
49 return "---";
50
51#ifdef AF_PACKET
52 case AF_PACKET:
53 return "<packet>";
54#endif
55
56 default:
57 ++failures;
58 printf ("sa_family=%d %08x\n", sa->sa_family,
59 *(int*)&((struct sockaddr_in *) sa)->sin_addr.s_addr);
60 return "<unexpected sockaddr family>";
61 }
62}
63
64
65static int
66do_test (void)
67{
68 struct ifaddrs *ifaces, *ifa;
69
70 if (getifaddrs (&ifaces) < 0)
71 {
72 if (errno != ENOSYS)
73 {
74 printf ("Couldn't get any interfaces: %s.\n", strerror (errno));
75 exit (1);
76 }
77 /* The function is simply not implemented. */
78 exit (0);
79 }
80
81 puts ("\
82Name Flags Address Netmask Broadcast/Destination");
83
84 for (ifa = ifaces; ifa != NULL; ifa = ifa->ifa_next)
85 {
86 char abuf[64], mbuf[64], dbuf[64];
87 printf ("%-15s%#.4x %-15s %-15s %-15s\n",
88 ifa->ifa_name, ifa->ifa_flags,
89 addr_string (ifa->ifa_addr, abuf, sizeof (abuf)),
90 addr_string (ifa->ifa_netmask, mbuf, sizeof (mbuf)),
91 addr_string (ifa->ifa_broadaddr, dbuf, sizeof (dbuf)));
92 }
93
94 freeifaddrs (ifaces);
95
96 return failures ? 1 : 0;
97}
98
99#define TEST_FUNCTION do_test ()
100#include "../test-skeleton.c"
Note: See TracBrowser for help on using the repository browser.