Blame inet/test-ifaddrs.c

Packit 6c4009
/* Test listing of network interface addresses.
Packit 6c4009
   Copyright (C) 2002-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <ifaddrs.h>
Packit 6c4009
#include <netinet/in.h>
Packit 6c4009
#include <arpa/inet.h>
Packit 6c4009
Packit 6c4009
static int failures;
Packit 6c4009
Packit 6c4009
static const char *
Packit 6c4009
addr_string (struct sockaddr *sa, char *buf, size_t size)
Packit 6c4009
{
Packit 6c4009
  if (sa == NULL)
Packit 6c4009
    return "<none>";
Packit 6c4009
Packit 6c4009
  switch (sa->sa_family)
Packit 6c4009
    {
Packit 6c4009
    case AF_INET:
Packit 6c4009
      return inet_ntop (AF_INET, &((struct sockaddr_in *) sa)->sin_addr,
Packit 6c4009
			buf, size);
Packit 6c4009
    case AF_INET6:
Packit 6c4009
      return inet_ntop (AF_INET6, &((struct sockaddr_in6 *) sa)->sin6_addr,
Packit 6c4009
			buf, size);
Packit 6c4009
#ifdef AF_LINK
Packit 6c4009
    case AF_LINK:
Packit 6c4009
      return "<link>";
Packit 6c4009
#endif
Packit 6c4009
    case AF_UNSPEC:
Packit 6c4009
      return "---";
Packit 6c4009
Packit 6c4009
#ifdef AF_PACKET
Packit 6c4009
    case AF_PACKET:
Packit 6c4009
      return "<packet>";
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
    default:
Packit 6c4009
      ++failures;
Packit 6c4009
      printf ("sa_family=%d %08x\n", sa->sa_family,
Packit 6c4009
	      *(int*)&((struct sockaddr_in *) sa)->sin_addr.s_addr);
Packit 6c4009
      return "<unexpected sockaddr family>";
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
do_test (void)
Packit 6c4009
{
Packit 6c4009
  struct ifaddrs *ifaces, *ifa;
Packit 6c4009
Packit 6c4009
  if (getifaddrs (&ifaces) < 0)
Packit 6c4009
    {
Packit 6c4009
      if (errno != ENOSYS)
Packit 6c4009
	{
Packit 6c4009
	  printf ("Couldn't get any interfaces: %s.\n", strerror (errno));
Packit 6c4009
	  exit (1);
Packit 6c4009
	}
Packit 6c4009
      /* The function is simply not implemented.  */
Packit 6c4009
      exit (0);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  puts ("\
Packit 6c4009
Name           Flags   Address         Netmask         Broadcast/Destination");
Packit 6c4009
Packit 6c4009
  for (ifa = ifaces; ifa != NULL; ifa = ifa->ifa_next)
Packit 6c4009
    {
Packit 6c4009
      char abuf[64], mbuf[64], dbuf[64];
Packit 6c4009
      printf ("%-15s%#.4x  %-15s %-15s %-15s\n",
Packit 6c4009
	      ifa->ifa_name, ifa->ifa_flags,
Packit 6c4009
	      addr_string (ifa->ifa_addr, abuf, sizeof (abuf)),
Packit 6c4009
	      addr_string (ifa->ifa_netmask, mbuf, sizeof (mbuf)),
Packit 6c4009
	      addr_string (ifa->ifa_broadaddr, dbuf, sizeof (dbuf)));
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  freeifaddrs (ifaces);
Packit 6c4009
Packit 6c4009
  return failures ? 1 : 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#define TEST_FUNCTION do_test ()
Packit 6c4009
#include "../test-skeleton.c"