4 * Provides the get_addrs_ioctl() function for use on systems that
5 * support a simple socket ioctl for acquiring low-level ethernet
6 * information about interfaces.
15 #include <sys/types.h>
16 #include <sys/ioctl.h>
17 #include <sys/socket.h>
19 #include <netinet/in.h>
21 #if defined __FreeBSD__ || defined __OpenBSD__ || defined __APPLE__ \
22 || ( defined __GNUC__ && ! defined __linux__ )
23 #include <sys/param.h>
24 #include <sys/sysctl.h>
25 #include <net/if_dl.h>
35 * This function identifies the IP address and ethernet address for the requested
38 * This function returns -1 on catastrophic failure, or a bitwise OR of the
41 * 1 - Was able to get the ethernet address
42 * 2 - Was able to get the IP address
44 * This function should return 3 if all information was found
48 get_addrs_ioctl(char *interface, char if_hw_addr[], struct in_addr *if_ip_addr, struct in6_addr *if_ip6_addr)
51 struct ifreq ifr = {};
56 struct ifaddrs *ifa, *ifas;
61 s = socket(AF_INET, SOCK_DGRAM, 0); /* any sort of IP socket will do */
68 fprintf(stderr,"interface: %s\n", interface);
70 memset(if_hw_addr, 0, 6);
71 strncpy(ifr.ifr_name, interface, IFNAMSIZ);
74 if (ioctl(s, SIOCGIFHWADDR, &ifr) < 0) {
75 fprintf(stderr, "Error getting hardware address for interface: %s\n", interface);
76 perror("ioctl(SIOCGIFHWADDR)");
79 memcpy(if_hw_addr, ifr.ifr_hwaddr.sa_data, 6);
83 #if defined __FreeBSD__ || defined __OpenBSD__ || defined __APPLE__ \
84 || ( defined __GNUC__ && ! defined __linux__ )
86 int sysctlparam[6] = {CTL_NET, PF_ROUTE, 0, 0, NET_RT_IFLIST, 0};
89 struct if_msghdr *msghdr = NULL;
90 sysctlparam[5] = if_nametoindex(interface);
91 if (sysctlparam[5] == 0) {
92 fprintf(stderr, "Error getting hardware address for interface: %s\n", interface);
95 if (sysctl(sysctlparam, 6, NULL, &needed, NULL, 0) < 0) {
96 fprintf(stderr, "Error getting hardware address for interface: %s\n", interface);
99 if ((buf = malloc(needed)) == NULL) {
100 fprintf(stderr, "Error getting hardware address for interface: %s\n", interface);
103 if (sysctl(sysctlparam, 6, buf, &needed, NULL, 0) < 0) {
104 fprintf(stderr, "Error getting hardware address for interface: %s\n", interface);
108 msghdr = (struct if_msghdr *) buf;
109 memcpy(if_hw_addr, LLADDR((struct sockaddr_dl *)(buf + sizeof(struct if_msghdr) - sizeof(struct if_data) + sizeof(struct if_data))), 6);
114 1; /* compiler whines if there is a label at the end of a block...*/
117 fprintf(stderr, "Cannot obtain hardware address on this platform\n");
121 /* Get the IP address of the interface */
122 #ifdef USE_GETIFADDRS
123 if (getifaddrs(&ifas) == -1) {
124 fprintf(stderr, "Unable to get IP address for interface: %s\n", interface);
125 perror("getifaddrs()");
128 for (ifa = ifas; ifa != NULL; ifa = ifa->ifa_next) {
129 if (got_ip_addr && got_ip6_addr)
130 break; /* Search is already complete. */
132 if (strcmp(ifa->ifa_name, interface))
133 continue; /* Not our interface. */
135 if ( (ifa->ifa_addr->sa_family != AF_INET)
136 && (ifa->ifa_addr->sa_family != AF_INET6) )
137 continue; /* AF_PACKET is beyond our scope. */
139 if ( (ifa->ifa_addr->sa_family == AF_INET)
143 &(((struct sockaddr_in *) ifa->ifa_addr)->sin_addr),
144 sizeof(*if_ip_addr));
147 /* Must be a IPv6 address at this point. */
148 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *) ifa->ifa_addr;
150 if ( IN6_IS_ADDR_LINKLOCAL(&(sa6->sin6_addr))
151 || IN6_IS_ADDR_SITELOCAL(&(sa6->sin6_addr)) )
154 /* A useful IPv6 address. */
155 memcpy(if_ip6_addr, &(sa6->sin6_addr), sizeof(*if_ip6_addr));
160 #elif defined(SIOCGIFADDR)
161 (*(struct sockaddr_in *) &ifr.ifr_addr).sin_family = AF_INET;
162 if (ioctl(s, SIOCGIFADDR, &ifr) < 0) {
163 fprintf(stderr, "Unable to get IP address for interface: %s\n", interface);
164 perror("ioctl(SIOCGIFADDR)");
167 memcpy(if_ip_addr, &((*(struct sockaddr_in *) &ifr.ifr_addr).sin_addr), sizeof(struct in_addr));
171 fprintf(stderr, "Cannot obtain IP address on this platform\n");
176 return got_hw_addr + got_ip_addr + got_ip6_addr;