1 /* Copyright (C) 2003,2006 Juan Lang
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2.1 of the License, or (at your option) any later version.
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17 * Implementation notes
19 * - I don't support IPv6 addresses here, since SIOCGIFCONF can't return them
21 * There are three implemented methods for determining the MAC address of an
23 * - a specific IOCTL (Linux)
24 * - looking in the ARP cache (at least Solaris)
25 * - using the sysctl interface (FreeBSD and Mac OS X)
26 * Solaris and some others have SIOCGENADDR, but I haven't gotten that to work
27 * on the Solaris boxes at SourceForge's compile farm, whereas SIOCGARP does.
41 #include <sys/types.h>
42 #ifdef HAVE_SYS_PARAM_H
43 #include <sys/param.h>
46 #ifdef HAVE_SYS_SOCKET_H
47 #include <sys/socket.h>
50 #ifdef HAVE_NETINET_IN_H
51 #include <netinet/in.h>
54 #ifdef HAVE_ARPA_INET_H
55 #include <arpa/inet.h>
62 #ifdef HAVE_NET_IF_ARP_H
63 #include <net/if_arp.h>
66 #ifdef HAVE_NET_ROUTE_H
67 #include <net/route.h>
70 #ifdef HAVE_SYS_IOCTL_H
71 #include <sys/ioctl.h>
74 #ifdef HAVE_SYS_SYSCTL_H
75 #include <sys/sysctl.h>
78 #ifdef HAVE_SYS_SOCKIO_H
79 #include <sys/sockio.h>
82 #ifdef HAVE_NET_IF_DL_H
83 #include <net/if_dl.h>
86 #ifdef HAVE_NET_IF_TYPES_H
87 #include <net/if_types.h>
97 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
98 #define ifreq_len(ifr) \
99 max(sizeof(struct ifreq), sizeof((ifr)->ifr_name)+(ifr)->ifr_addr.sa_len)
101 #define ifreq_len(ifr) sizeof(struct ifreq)
109 #define IF_NAMESIZE 16
113 #define INADDR_NONE (~0U)
116 #define INITIAL_INTERFACES_ASSUMED 4
118 #define INDEX_IS_LOOPBACK 0x00800000
122 static int isLoopbackInterface(int fd, const char *name)
129 lstrcpynA(ifr.ifr_name, name, IFNAMSIZ);
130 if (ioctl(fd, SIOCGIFFLAGS, &ifr) == 0)
131 ret = ifr.ifr_flags & IFF_LOOPBACK;
136 /* The comments say MAX_ADAPTER_NAME is required, but really only IF_NAMESIZE
137 * bytes are necessary.
139 char *getInterfaceNameByIndex(DWORD index, char *name)
141 return if_indextoname(index, name);
144 DWORD getInterfaceIndexByName(const char *name, PDWORD index)
150 return ERROR_INVALID_PARAMETER;
152 return ERROR_INVALID_PARAMETER;
153 idx = if_nametoindex(name);
159 ret = ERROR_INVALID_DATA;
163 DWORD getNumNonLoopbackInterfaces(void)
166 int fd = socket(PF_INET, SOCK_DGRAM, 0);
169 struct if_nameindex *indexes = if_nameindex();
172 struct if_nameindex *p;
174 for (p = indexes, numInterfaces = 0; p && p->if_name; p++)
175 if (!isLoopbackInterface(fd, p->if_name))
177 if_freenameindex(indexes);
185 return numInterfaces;
188 DWORD getNumInterfaces(void)
191 struct if_nameindex *indexes = if_nameindex();
194 struct if_nameindex *p;
196 for (p = indexes, numInterfaces = 0; p && p->if_name; p++)
198 if_freenameindex(indexes);
202 return numInterfaces;
205 InterfaceIndexTable *getInterfaceIndexTable(void)
208 InterfaceIndexTable *ret;
209 struct if_nameindex *indexes = if_nameindex();
212 struct if_nameindex *p;
213 DWORD size = sizeof(InterfaceIndexTable);
215 for (p = indexes, numInterfaces = 0; p && p->if_name; p++)
217 if (numInterfaces > 1)
218 size += (numInterfaces - 1) * sizeof(DWORD);
219 ret = HeapAlloc(GetProcessHeap(), 0, size);
222 for (p = indexes; p && p->if_name; p++)
223 ret->indexes[ret->numIndexes++] = p->if_index;
225 if_freenameindex(indexes);
232 InterfaceIndexTable *getNonLoopbackInterfaceIndexTable(void)
235 InterfaceIndexTable *ret;
236 int fd = socket(PF_INET, SOCK_DGRAM, 0);
239 struct if_nameindex *indexes = if_nameindex();
242 struct if_nameindex *p;
243 DWORD size = sizeof(InterfaceIndexTable);
245 for (p = indexes, numInterfaces = 0; p && p->if_name; p++)
246 if (!isLoopbackInterface(fd, p->if_name))
248 if (numInterfaces > 1)
249 size += (numInterfaces - 1) * sizeof(DWORD);
250 ret = HeapAlloc(GetProcessHeap(), 0, size);
253 for (p = indexes; p && p->if_name; p++)
254 if (!isLoopbackInterface(fd, p->if_name))
255 ret->indexes[ret->numIndexes++] = p->if_index;
257 if_freenameindex(indexes);
268 static DWORD getInterfaceBCastAddrByName(const char *name)
270 DWORD ret = INADDR_ANY;
273 int fd = socket(PF_INET, SOCK_DGRAM, 0);
278 lstrcpynA(ifr.ifr_name, name, IFNAMSIZ);
279 if (ioctl(fd, SIOCGIFBRDADDR, &ifr) == 0)
280 memcpy(&ret, ifr.ifr_addr.sa_data + 2, sizeof(DWORD));
287 static DWORD getInterfaceMaskByName(const char *name)
289 DWORD ret = INADDR_NONE;
292 int fd = socket(PF_INET, SOCK_DGRAM, 0);
297 lstrcpynA(ifr.ifr_name, name, IFNAMSIZ);
298 if (ioctl(fd, SIOCGIFNETMASK, &ifr) == 0)
299 memcpy(&ret, ifr.ifr_addr.sa_data + 2, sizeof(DWORD));
306 #if defined (SIOCGIFHWADDR) && defined (HAVE_STRUCT_IFREQ_IFR_HWADDR)
307 static DWORD getInterfacePhysicalByName(const char *name, PDWORD len, PBYTE addr,
313 if (!name || !len || !addr || !type)
314 return ERROR_INVALID_PARAMETER;
316 fd = socket(PF_INET, SOCK_DGRAM, 0);
320 memset(&ifr, 0, sizeof(struct ifreq));
321 lstrcpynA(ifr.ifr_name, name, IFNAMSIZ);
322 if ((ioctl(fd, SIOCGIFHWADDR, &ifr)))
323 ret = ERROR_INVALID_DATA;
325 unsigned int addrLen;
327 switch (ifr.ifr_hwaddr.sa_family)
329 #ifdef ARPHRD_LOOPBACK
330 case ARPHRD_LOOPBACK:
332 *type = MIB_IF_TYPE_LOOPBACK;
338 *type = MIB_IF_TYPE_ETHERNET;
344 *type = MIB_IF_TYPE_FDDI;
347 #ifdef ARPHRD_IEEE802
348 case ARPHRD_IEEE802: /* 802.2 Ethernet && Token Ring, guess TR? */
350 *type = MIB_IF_TYPE_TOKENRING;
353 #ifdef ARPHRD_IEEE802_TR
354 case ARPHRD_IEEE802_TR: /* also Token Ring? */
356 *type = MIB_IF_TYPE_TOKENRING;
362 *type = MIB_IF_TYPE_SLIP;
368 *type = MIB_IF_TYPE_PPP;
372 addrLen = min(MAX_INTERFACE_PHYSADDR, sizeof(ifr.ifr_hwaddr.sa_data));
373 *type = MIB_IF_TYPE_OTHER;
375 if (addrLen > *len) {
376 ret = ERROR_INSUFFICIENT_BUFFER;
381 memcpy(addr, ifr.ifr_hwaddr.sa_data, addrLen);
382 /* zero out remaining bytes for broken implementations */
383 memset(addr + addrLen, 0, *len - addrLen);
391 ret = ERROR_NO_MORE_FILES;
394 #elif defined (SIOCGARP)
395 static DWORD getInterfacePhysicalByName(const char *name, PDWORD len, PBYTE addr,
401 if (!name || !len || !addr || !type)
402 return ERROR_INVALID_PARAMETER;
404 fd = socket(PF_INET, SOCK_DGRAM, 0);
406 if (isLoopbackInterface(fd, name)) {
407 *type = MIB_IF_TYPE_LOOPBACK;
408 memset(addr, 0, *len);
414 struct sockaddr_in *saddr;
418 lstrcpynA(ifr.ifr_name, name, IFNAMSIZ);
419 ioctl(fd, SIOCGIFADDR, &ifr);
420 memset(&arp, 0, sizeof(struct arpreq));
421 arp.arp_pa.sa_family = AF_INET;
422 saddr = (struct sockaddr_in *)&arp; /* proto addr is first member */
423 saddr->sin_family = AF_INET;
424 memcpy(&saddr->sin_addr.s_addr, ifr.ifr_addr.sa_data + 2, sizeof(DWORD));
425 if ((ioctl(fd, SIOCGARP, &arp)))
426 ret = ERROR_INVALID_DATA;
428 /* FIXME: heh: who said it was ethernet? */
429 int addrLen = ETH_ALEN;
431 if (addrLen > *len) {
432 ret = ERROR_INSUFFICIENT_BUFFER;
437 memcpy(addr, &arp.arp_ha.sa_data[0], addrLen);
438 /* zero out remaining bytes for broken implementations */
439 memset(addr + addrLen, 0, *len - addrLen);
441 *type = MIB_IF_TYPE_ETHERNET;
449 ret = ERROR_NO_MORE_FILES;
453 #elif defined (HAVE_SYS_SYSCTL_H) && defined (HAVE_NET_IF_DL_H)
454 static DWORD getInterfacePhysicalByName(const char *name, PDWORD len, PBYTE addr,
458 struct if_msghdr *ifm;
459 struct sockaddr_dl *sdl;
462 int mib[] = { CTL_NET, AF_ROUTE, 0, AF_LINK, NET_RT_IFLIST, 0 };
466 if (!name || !len || !addr || !type)
467 return ERROR_INVALID_PARAMETER;
469 if (sysctl(mib, 6, NULL, &mibLen, NULL, 0) < 0)
470 return ERROR_NO_MORE_FILES;
472 buf = HeapAlloc(GetProcessHeap(), 0, mibLen);
474 return ERROR_NOT_ENOUGH_MEMORY;
476 if (sysctl(mib, 6, buf, &mibLen, NULL, 0) < 0) {
477 HeapFree(GetProcessHeap(), 0, buf);
478 return ERROR_NO_MORE_FILES;
481 ret = ERROR_INVALID_DATA;
482 for (p = buf; !found && p < buf + mibLen; p += ifm->ifm_msglen) {
483 ifm = (struct if_msghdr *)p;
484 sdl = (struct sockaddr_dl *)(ifm + 1);
486 if (ifm->ifm_type != RTM_IFINFO || (ifm->ifm_addrs & RTA_IFP) == 0)
489 if (sdl->sdl_family != AF_LINK || sdl->sdl_nlen == 0 ||
490 memcmp(sdl->sdl_data, name, max(sdl->sdl_nlen, strlen(name))) != 0)
494 addrLen = min(MAX_INTERFACE_PHYSADDR, sdl->sdl_alen);
495 if (addrLen > *len) {
496 ret = ERROR_INSUFFICIENT_BUFFER;
501 memcpy(addr, LLADDR(sdl), addrLen);
502 /* zero out remaining bytes for broken implementations */
503 memset(addr + addrLen, 0, *len - addrLen);
505 #if defined(HAVE_NET_IF_TYPES_H)
506 switch (sdl->sdl_type)
509 *type = MIB_IF_TYPE_ETHERNET;
512 *type = MIB_IF_TYPE_FDDI;
514 case IFT_ISO88024: /* Token Bus */
515 *type = MIB_IF_TYPE_TOKENRING;
517 case IFT_ISO88025: /* Token Ring */
518 *type = MIB_IF_TYPE_TOKENRING;
521 *type = MIB_IF_TYPE_PPP;
524 *type = MIB_IF_TYPE_SLIP;
527 *type = MIB_IF_TYPE_LOOPBACK;
530 *type = MIB_IF_TYPE_OTHER;
533 /* default if we don't know */
534 *type = MIB_IF_TYPE_ETHERNET;
539 HeapFree(GetProcessHeap(), 0, buf);
544 DWORD getInterfacePhysicalByIndex(DWORD index, PDWORD len, PBYTE addr,
547 char nameBuf[IF_NAMESIZE];
548 char *name = getInterfaceNameByIndex(index, nameBuf);
551 return getInterfacePhysicalByName(name, len, addr, type);
553 return ERROR_INVALID_DATA;
556 DWORD getInterfaceMtuByName(const char *name, PDWORD mtu)
562 return ERROR_INVALID_PARAMETER;
564 return ERROR_INVALID_PARAMETER;
566 fd = socket(PF_INET, SOCK_DGRAM, 0);
570 lstrcpynA(ifr.ifr_name, name, IFNAMSIZ);
571 if ((ioctl(fd, SIOCGIFMTU, &ifr)))
572 ret = ERROR_INVALID_DATA;
577 *mtu = ifr.ifr_metric;
584 ret = ERROR_NO_MORE_FILES;
588 DWORD getInterfaceStatusByName(const char *name, PDWORD status)
594 return ERROR_INVALID_PARAMETER;
596 return ERROR_INVALID_PARAMETER;
598 fd = socket(PF_INET, SOCK_DGRAM, 0);
602 lstrcpynA(ifr.ifr_name, name, IFNAMSIZ);
603 if ((ioctl(fd, SIOCGIFFLAGS, &ifr)))
604 ret = ERROR_INVALID_DATA;
606 if (ifr.ifr_flags & IFF_UP)
607 *status = MIB_IF_OPER_STATUS_OPERATIONAL;
609 *status = MIB_IF_OPER_STATUS_NON_OPERATIONAL;
615 ret = ERROR_NO_MORE_FILES;
619 DWORD getInterfaceEntryByName(const char *name, PMIB_IFROW entry)
621 BYTE addr[MAX_INTERFACE_PHYSADDR];
622 DWORD ret, len = sizeof(addr), type;
625 return ERROR_INVALID_PARAMETER;
627 return ERROR_INVALID_PARAMETER;
629 if (getInterfacePhysicalByName(name, &len, addr, &type) == NO_ERROR) {
633 memset(entry, 0, sizeof(MIB_IFROW));
634 for (assigner = entry->wszName, walker = name; *walker;
635 walker++, assigner++)
638 getInterfaceIndexByName(name, &entry->dwIndex);
639 entry->dwPhysAddrLen = len;
640 memcpy(entry->bPhysAddr, addr, len);
641 memset(entry->bPhysAddr + len, 0, sizeof(entry->bPhysAddr) - len);
642 entry->dwType = type;
643 /* FIXME: how to calculate real speed? */
644 getInterfaceMtuByName(name, &entry->dwMtu);
645 /* lie, there's no "administratively down" here */
646 entry->dwAdminStatus = MIB_IF_ADMIN_STATUS_UP;
647 getInterfaceStatusByName(name, &entry->dwOperStatus);
648 /* punt on dwLastChange? */
649 entry->dwDescrLen = min(strlen(name), MAX_INTERFACE_DESCRIPTION - 1);
650 memcpy(entry->bDescr, name, entry->dwDescrLen);
651 entry->bDescr[entry->dwDescrLen] = '\0';
656 ret = ERROR_INVALID_DATA;
660 /* Enumerates the IP addresses in the system using SIOCGIFCONF, returning
661 * the count to you in *pcAddresses. It also returns to you the struct ifconf
662 * used by the call to ioctl, so that you may process the addresses further.
663 * Free ifc->ifc_buf using HeapFree.
664 * Returns NO_ERROR on success, something else on failure.
666 static DWORD enumIPAddresses(PDWORD pcAddresses, struct ifconf *ifc)
671 fd = socket(PF_INET, SOCK_DGRAM, 0);
674 DWORD guessedNumAddresses = 0, numAddresses = 0;
681 /* there is no way to know the interface count beforehand,
682 so we need to loop again and again upping our max each time
683 until returned is constant across 2 calls */
685 lastlen = ifc->ifc_len;
686 HeapFree(GetProcessHeap(), 0, ifc->ifc_buf);
687 if (guessedNumAddresses == 0)
688 guessedNumAddresses = INITIAL_INTERFACES_ASSUMED;
690 guessedNumAddresses *= 2;
691 ifc->ifc_len = sizeof(struct ifreq) * guessedNumAddresses;
692 ifc->ifc_buf = HeapAlloc(GetProcessHeap(), 0, ifc->ifc_len);
693 ioctlRet = ioctl(fd, SIOCGIFCONF, ifc);
694 } while ((ioctlRet == 0) && (ifc->ifc_len != lastlen));
697 ifPtr = ifc->ifc_buf;
698 while (ifPtr && ifPtr < ifc->ifc_buf + ifc->ifc_len) {
699 struct ifreq *ifr = (struct ifreq *)ifPtr;
701 if (ifr->ifr_addr.sa_family == AF_INET)
704 ifPtr += ifreq_len((struct ifreq *)ifPtr);
708 ret = ERROR_INVALID_PARAMETER; /* FIXME: map from errno to Win32 */
710 *pcAddresses = numAddresses;
713 HeapFree(GetProcessHeap(), 0, ifc->ifc_buf);
719 ret = ERROR_NO_SYSTEM_RESOURCES;
723 DWORD getNumIPAddresses(void)
725 DWORD numAddresses = 0;
728 if (!enumIPAddresses(&numAddresses, &ifc))
729 HeapFree(GetProcessHeap(), 0, ifc.ifc_buf);
733 DWORD getIPAddrTable(PMIB_IPADDRTABLE *ppIpAddrTable, HANDLE heap, DWORD flags)
738 ret = ERROR_INVALID_PARAMETER;
741 DWORD numAddresses = 0;
744 ret = enumIPAddresses(&numAddresses, &ifc);
747 DWORD size = sizeof(MIB_IPADDRTABLE);
749 if (numAddresses > 1)
750 size += (numAddresses - 1) * sizeof(MIB_IPADDRROW);
751 *ppIpAddrTable = HeapAlloc(heap, flags, size);
752 if (*ppIpAddrTable) {
757 (*ppIpAddrTable)->dwNumEntries = numAddresses;
759 while (!ret && ifPtr && ifPtr < ifc.ifc_buf + ifc.ifc_len) {
760 struct ifreq *ifr = (struct ifreq *)ifPtr;
762 ifPtr += ifreq_len(ifr);
764 if (ifr->ifr_addr.sa_family != AF_INET)
767 ret = getInterfaceIndexByName(ifr->ifr_name,
768 &(*ppIpAddrTable)->table[i].dwIndex);
769 memcpy(&(*ppIpAddrTable)->table[i].dwAddr, ifr->ifr_addr.sa_data + 2,
771 (*ppIpAddrTable)->table[i].dwMask =
772 getInterfaceMaskByName(ifr->ifr_name);
773 /* the dwBCastAddr member isn't the broadcast address, it indicates
774 * whether the interface uses the 1's broadcast address (1) or the
775 * 0's broadcast address (0).
777 bcast = getInterfaceBCastAddrByName(ifr->ifr_name);
778 (*ppIpAddrTable)->table[i].dwBCastAddr =
779 (bcast & (*ppIpAddrTable)->table[i].dwMask) ? 1 : 0;
780 /* FIXME: hardcoded reasm size, not sure where to get it */
781 (*ppIpAddrTable)->table[i].dwReasmSize = 65535;
783 (*ppIpAddrTable)->table[i].unused1 = 0;
784 (*ppIpAddrTable)->table[i].wType = 0;
789 ret = ERROR_OUTOFMEMORY;
790 HeapFree(GetProcessHeap(), 0, ifc.ifc_buf);
796 #ifdef HAVE_IFADDRS_H
797 ULONG v6addressesFromIndex(DWORD index, SOCKET_ADDRESS **addrs, ULONG *num_addrs)
802 if (!getifaddrs(&ifa))
808 getInterfaceNameByIndex(index, name);
809 for (p = ifa, n = 0; p; p = p->ifa_next)
810 if (p->ifa_addr && p->ifa_addr->sa_family == AF_INET6 &&
811 !strcmp(name, p->ifa_name))
815 *addrs = HeapAlloc(GetProcessHeap(), 0, n * (sizeof(SOCKET_ADDRESS) +
816 sizeof(struct WS_sockaddr_in6)));
819 struct WS_sockaddr_in6 *next_addr = (struct WS_sockaddr_in6 *)(
820 (BYTE *)*addrs + n * sizeof(SOCKET_ADDRESS));
822 for (p = ifa, n = 0; p; p = p->ifa_next)
824 if (p->ifa_addr && p->ifa_addr->sa_family == AF_INET6 &&
825 !strcmp(name, p->ifa_name))
827 struct sockaddr_in6 *addr = (struct sockaddr_in6 *)p->ifa_addr;
829 next_addr->sin6_family = WS_AF_INET6;
830 next_addr->sin6_port = addr->sin6_port;
831 next_addr->sin6_flowinfo = addr->sin6_flowinfo;
832 memcpy(&next_addr->sin6_addr, &addr->sin6_addr,
833 sizeof(next_addr->sin6_addr));
834 next_addr->sin6_scope_id = addr->sin6_scope_id;
835 (*addrs)[n].lpSockaddr = (LPSOCKADDR)next_addr;
836 (*addrs)[n].iSockaddrLength = sizeof(struct WS_sockaddr_in6);
845 ret = ERROR_OUTOFMEMORY;
860 ULONG v6addressesFromIndex(DWORD index, SOCKET_ADDRESS **addrs, ULONG *num_addrs)
864 return ERROR_SUCCESS;
868 char *toIPAddressString(unsigned int addr, char string[16])
871 struct in_addr iAddr;
874 /* extra-anal, just to make auditors happy */
875 lstrcpynA(string, inet_ntoa(iAddr), 16);