Back to index

glibc  2.9
test-ifaddrs.c
Go to the documentation of this file.
00001 /* Test listing of network interface addresses.
00002    Copyright (C) 2002, 2004, 2005 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <errno.h>
00021 #include <stdio.h>
00022 #include <stdlib.h>
00023 #include <string.h>
00024 #include <ifaddrs.h>
00025 #include <netinet/in.h>
00026 #include <arpa/inet.h>
00027 
00028 static int failures;
00029 
00030 static const char *
00031 addr_string (struct sockaddr *sa, char *buf, size_t size)
00032 {
00033   if (sa == NULL)
00034     return "<none>";
00035 
00036   switch (sa->sa_family)
00037     {
00038     case AF_INET:
00039       return inet_ntop (AF_INET, &((struct sockaddr_in *) sa)->sin_addr,
00040                      buf, size);
00041     case AF_INET6:
00042       return inet_ntop (AF_INET6, &((struct sockaddr_in6 *) sa)->sin6_addr,
00043                      buf, size);
00044 #ifdef AF_LINK
00045     case AF_LINK:
00046       return "<link>";
00047 #endif
00048     case AF_UNSPEC:
00049       return "---";
00050 
00051 #ifdef AF_PACKET
00052     case AF_PACKET:
00053       return "<packet>";
00054 #endif
00055 
00056     default:
00057       ++failures;
00058       printf ("sa_family=%d %08x\n", sa->sa_family,
00059              *(int*)&((struct sockaddr_in *) sa)->sin_addr.s_addr);
00060       return "<unexpected sockaddr family>";
00061     }
00062 }
00063 
00064 
00065 static int
00066 do_test (void)
00067 {
00068   struct ifaddrs *ifaces, *ifa;
00069 
00070   if (getifaddrs (&ifaces) < 0)
00071     {
00072       if (errno != ENOSYS)
00073        {
00074          printf ("Couldn't get any interfaces: %s.\n", strerror (errno));
00075          exit (1);
00076        }
00077       /* The function is simply not implemented.  */
00078       exit (0);
00079     }
00080 
00081   puts ("\
00082 Name           Flags   Address         Netmask         Broadcast/Destination");
00083 
00084   for (ifa = ifaces; ifa != NULL; ifa = ifa->ifa_next)
00085     {
00086       char abuf[64], mbuf[64], dbuf[64];
00087       printf ("%-15s%#.4x  %-15s %-15s %-15s\n",
00088              ifa->ifa_name, ifa->ifa_flags,
00089              addr_string (ifa->ifa_addr, abuf, sizeof (abuf)),
00090              addr_string (ifa->ifa_netmask, mbuf, sizeof (mbuf)),
00091              addr_string (ifa->ifa_broadaddr, dbuf, sizeof (dbuf)));
00092     }
00093 
00094   freeifaddrs (ifaces);
00095 
00096   return failures ? 1 : 0;
00097 }
00098 
00099 #define TEST_FUNCTION do_test ()
00100 #include "../test-skeleton.c"