Blame view

addrs_ioctl.c 5 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * addrs_ioctl.c:
 *
 * Provides the get_addrs_ioctl() function for use on systems that
 * support a simple socket ioctl for acquiring low-level ethernet
 * information about interfaces.
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <net/if.h>
#include <netinet/in.h>

21
#if defined __FreeBSD__ || defined __OpenBSD__ || defined __APPLE__ \
22
      || ( defined __GLIBC__ && ! defined __linux__ )
23 24 25 26 27
#include <sys/param.h>
#include <sys/sysctl.h>
#include <net/if_dl.h>
#endif

28 29 30 31
#ifdef USE_GETIFADDRS
#include <ifaddrs.h>
#endif

32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
#include "iftop.h"

/*
 * This function identifies the IP address and ethernet address for the requested
 * interface
 *
 * This function returns -1 on catastrophic failure, or a bitwise OR of the
 * following values:
 *
 * 1 - Was able to get the ethernet address
 * 2 - Was able to get the IP address
 *
 * This function should return 3 if all information was found
 */

int
Paul Warren committed
48
get_addrs_ioctl(char *interface, u_int8_t if_hw_addr[], struct in_addr *if_ip_addr, struct in6_addr *if_ip6_addr)
49 50 51 52 53
{
  int s;
  struct ifreq ifr = {};
  int got_hw_addr = 0;
  int got_ip_addr = 0;
54 55 56 57
  int got_ip6_addr = 0;
#ifdef USE_GETIFADDRS
  struct ifaddrs *ifa, *ifas;
#endif
58 59 60

  /* -- */

61
  s = socket(AF_INET, SOCK_DGRAM, 0); /* any sort of IP socket will do */
62 63 64 65 66 67

  if (s == -1) {
    perror("socket");
    return -1;
  }

pdw committed
68
  fprintf(stderr,"interface: %s\n", interface);
69 70 71 72 73 74 75 76 77 78 79 80 81 82

  memset(if_hw_addr, 0, 6);
  strncpy(ifr.ifr_name, interface, IFNAMSIZ);

#ifdef SIOCGIFHWADDR
  if (ioctl(s, SIOCGIFHWADDR, &ifr) < 0) {
    fprintf(stderr, "Error getting hardware address for interface: %s\n", interface); 
    perror("ioctl(SIOCGIFHWADDR)");
  }
  else {
    memcpy(if_hw_addr, ifr.ifr_hwaddr.sa_data, 6);
    got_hw_addr = 1;
  }
#else
83
#if defined __FreeBSD__ || defined __OpenBSD__ || defined __APPLE__ \
84
      || ( defined __GLIBC__ && ! defined __linux__ )
85 86
  {
    int sysctlparam[6] = {CTL_NET, PF_ROUTE, 0, 0, NET_RT_IFLIST, 0};
87
    size_t needed = 0;
88 89 90 91 92 93
    char *buf = NULL;
    struct if_msghdr *msghdr = NULL;
    sysctlparam[5] = if_nametoindex(interface);
    if (sysctlparam[5] == 0) {
      fprintf(stderr, "Error getting hardware address for interface: %s\n", interface);
    }
94
    else if (sysctl(sysctlparam, 6, NULL, &needed, NULL, 0) < 0) {
95 96
      fprintf(stderr, "Error getting hardware address for interface: %s\n", interface);
    }
97
    else if ((buf = malloc(needed)) == NULL) {
98 99
      fprintf(stderr, "Error getting hardware address for interface: %s\n", interface);
    }
100
    else if (sysctl(sysctlparam, 6, buf, &needed, NULL, 0) < 0) {
101 102 103
      fprintf(stderr, "Error getting hardware address for interface: %s\n", interface);
      free(buf);
    }
104 105 106 107 108 109
    else {
      msghdr = (struct if_msghdr *) buf;
      memcpy(if_hw_addr, LLADDR((struct sockaddr_dl *)(buf + sizeof(struct if_msghdr) - sizeof(struct if_data) + sizeof(struct if_data))), 6);
      free(buf);
      got_hw_addr = 1;
    }
110 111
  }
#else
112 113
  fprintf(stderr, "Cannot obtain hardware address on this platform\n");
#endif
114
#endif
115 116
  
  /* Get the IP address of the interface */
117 118 119 120 121 122 123 124 125 126 127 128 129
#ifdef USE_GETIFADDRS
  if (getifaddrs(&ifas) == -1) {
    fprintf(stderr, "Unable to get IP address for interface: %s\n", interface); 
    perror("getifaddrs()");
  }
  else {
     for (ifa = ifas; ifa != NULL; ifa = ifa->ifa_next) {
        if (got_ip_addr && got_ip6_addr)
           break; /* Search is already complete. */

        if (strcmp(ifa->ifa_name, interface))
           continue; /* Not our interface. */

130 131 132
        if (ifa->ifa_addr == NULL)
           continue; /* Skip NULL interface address. */

133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
        if ( (ifa->ifa_addr->sa_family != AF_INET)
              && (ifa->ifa_addr->sa_family != AF_INET6) )
           continue; /* AF_PACKET is beyond our scope. */

        if ( (ifa->ifa_addr->sa_family == AF_INET)
              && !got_ip_addr ) {
           got_ip_addr = 2;
           memcpy(if_ip_addr,
                 &(((struct sockaddr_in *) ifa->ifa_addr)->sin_addr),
                 sizeof(*if_ip_addr));
           continue;
        }
        /* Must be a IPv6 address at this point. */
        struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *) ifa->ifa_addr;

        if ( IN6_IS_ADDR_LINKLOCAL(&(sa6->sin6_addr))
              || IN6_IS_ADDR_SITELOCAL(&(sa6->sin6_addr)) )
           continue;

        /* A useful IPv6 address. */
        memcpy(if_ip6_addr, &(sa6->sin6_addr), sizeof(*if_ip6_addr));
        got_ip6_addr = 4;
     }
     freeifaddrs(ifas);
  } /* getifaddrs() */
#elif defined(SIOCGIFADDR)
159 160
  (*(struct sockaddr_in *) &ifr.ifr_addr).sin_family = AF_INET;
  if (ioctl(s, SIOCGIFADDR, &ifr) < 0) {
pdw committed
161
    fprintf(stderr, "Unable to get IP address for interface: %s\n", interface); 
162 163 164 165 166 167 168 169 170 171 172 173
    perror("ioctl(SIOCGIFADDR)");
  }
  else {
    memcpy(if_ip_addr, &((*(struct sockaddr_in *) &ifr.ifr_addr).sin_addr), sizeof(struct in_addr));
    got_ip_addr = 2;
  }
#else
  fprintf(stderr, "Cannot obtain IP address on this platform\n");
#endif
  
  close(s);

174
  return got_hw_addr + got_ip_addr + got_ip6_addr;
175
}