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>
92 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
93 #define ifreq_len(ifr) \
94 max(sizeof(struct ifreq), sizeof((ifr)->ifr_name)+(ifr)->ifr_addr.sa_len)
96 #define ifreq_len(ifr) sizeof(struct ifreq)
104 #define IF_NAMESIZE 16
108 #define INADDR_NONE (~0U)
111 #define INITIAL_INTERFACES_ASSUMED 4
113 #define INDEX_IS_LOOPBACK 0x00800000
117 static int isLoopbackInterface(int fd
, const char *name
)
124 lstrcpynA(ifr
.ifr_name
, name
, IFNAMSIZ
);
125 if (ioctl(fd
, SIOCGIFFLAGS
, &ifr
) == 0)
126 ret
= ifr
.ifr_flags
& IFF_LOOPBACK
;
131 /* The comments say MAX_ADAPTER_NAME is required, but really only IF_NAMESIZE
132 * bytes are necessary.
134 char *getInterfaceNameByIndex(DWORD index
, char *name
)
136 return if_indextoname(index
, name
);
139 DWORD
getInterfaceIndexByName(const char *name
, PDWORD index
)
145 return ERROR_INVALID_PARAMETER
;
147 return ERROR_INVALID_PARAMETER
;
148 idx
= if_nametoindex(name
);
154 ret
= ERROR_INVALID_DATA
;
158 DWORD
getNumNonLoopbackInterfaces(void)
161 int fd
= socket(PF_INET
, SOCK_DGRAM
, 0);
164 struct if_nameindex
*indexes
= if_nameindex();
167 struct if_nameindex
*p
;
169 for (p
= indexes
, numInterfaces
= 0; p
&& p
->if_name
; p
++)
170 if (!isLoopbackInterface(fd
, p
->if_name
))
172 if_freenameindex(indexes
);
180 return numInterfaces
;
183 DWORD
getNumInterfaces(void)
186 struct if_nameindex
*indexes
= if_nameindex();
189 struct if_nameindex
*p
;
191 for (p
= indexes
, numInterfaces
= 0; p
&& p
->if_name
; p
++)
193 if_freenameindex(indexes
);
197 return numInterfaces
;
200 InterfaceIndexTable
*getInterfaceIndexTable(void)
203 InterfaceIndexTable
*ret
;
204 struct if_nameindex
*indexes
= if_nameindex();
207 struct if_nameindex
*p
;
209 for (p
= indexes
, numInterfaces
= 0; p
&& p
->if_name
; p
++)
211 ret
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
212 sizeof(InterfaceIndexTable
) + (numInterfaces
- 1) * sizeof(DWORD
));
214 for (p
= indexes
; p
&& p
->if_name
; p
++)
215 ret
->indexes
[ret
->numIndexes
++] = p
->if_index
;
217 if_freenameindex(indexes
);
224 InterfaceIndexTable
*getNonLoopbackInterfaceIndexTable(void)
227 InterfaceIndexTable
*ret
;
228 int fd
= socket(PF_INET
, SOCK_DGRAM
, 0);
231 struct if_nameindex
*indexes
= if_nameindex();
234 struct if_nameindex
*p
;
236 for (p
= indexes
, numInterfaces
= 0; p
&& p
->if_name
; p
++)
237 if (!isLoopbackInterface(fd
, p
->if_name
))
239 ret
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
240 sizeof(InterfaceIndexTable
) + (numInterfaces
- 1) * sizeof(DWORD
));
242 for (p
= indexes
; p
&& p
->if_name
; p
++)
243 if (!isLoopbackInterface(fd
, p
->if_name
))
244 ret
->indexes
[ret
->numIndexes
++] = p
->if_index
;
246 if_freenameindex(indexes
);
257 static DWORD
getInterfaceBCastAddrByName(const char *name
)
259 DWORD ret
= INADDR_ANY
;
262 int fd
= socket(PF_INET
, SOCK_DGRAM
, 0);
267 lstrcpynA(ifr
.ifr_name
, name
, IFNAMSIZ
);
268 if (ioctl(fd
, SIOCGIFBRDADDR
, &ifr
) == 0)
269 memcpy(&ret
, ifr
.ifr_addr
.sa_data
+ 2, sizeof(DWORD
));
276 static DWORD
getInterfaceMaskByName(const char *name
)
278 DWORD ret
= INADDR_NONE
;
281 int fd
= socket(PF_INET
, SOCK_DGRAM
, 0);
286 lstrcpynA(ifr
.ifr_name
, name
, IFNAMSIZ
);
287 if (ioctl(fd
, SIOCGIFNETMASK
, &ifr
) == 0)
288 memcpy(&ret
, ifr
.ifr_addr
.sa_data
+ 2, sizeof(DWORD
));
295 #if defined (SIOCGIFHWADDR)
296 DWORD
getInterfacePhysicalByName(const char *name
, PDWORD len
, PBYTE addr
,
302 if (!name
|| !len
|| !addr
|| !type
)
303 return ERROR_INVALID_PARAMETER
;
305 fd
= socket(PF_INET
, SOCK_DGRAM
, 0);
309 memset(&ifr
, 0, sizeof(struct ifreq
));
310 lstrcpynA(ifr
.ifr_name
, name
, IFNAMSIZ
);
311 if ((ioctl(fd
, SIOCGIFHWADDR
, &ifr
)))
312 ret
= ERROR_INVALID_DATA
;
314 unsigned int addrLen
;
316 switch (ifr
.ifr_hwaddr
.sa_family
)
318 #ifdef ARPHRD_LOOPBACK
319 case ARPHRD_LOOPBACK
:
321 *type
= MIB_IF_TYPE_LOOPBACK
;
327 *type
= MIB_IF_TYPE_ETHERNET
;
333 *type
= MIB_IF_TYPE_FDDI
;
336 #ifdef ARPHRD_IEEE802
337 case ARPHRD_IEEE802
: /* 802.2 Ethernet && Token Ring, guess TR? */
339 *type
= MIB_IF_TYPE_TOKENRING
;
342 #ifdef ARPHRD_IEEE802_TR
343 case ARPHRD_IEEE802_TR
: /* also Token Ring? */
345 *type
= MIB_IF_TYPE_TOKENRING
;
351 *type
= MIB_IF_TYPE_SLIP
;
357 *type
= MIB_IF_TYPE_PPP
;
361 addrLen
= min(MAX_INTERFACE_PHYSADDR
, sizeof(ifr
.ifr_hwaddr
.sa_data
));
362 *type
= MIB_IF_TYPE_OTHER
;
364 if (addrLen
> *len
) {
365 ret
= ERROR_INSUFFICIENT_BUFFER
;
370 memcpy(addr
, ifr
.ifr_hwaddr
.sa_data
, addrLen
);
371 /* zero out remaining bytes for broken implementations */
372 memset(addr
+ addrLen
, 0, *len
- addrLen
);
380 ret
= ERROR_NO_MORE_FILES
;
383 #elif defined (SIOCGARP)
384 DWORD
getInterfacePhysicalByName(const char *name
, PDWORD len
, PBYTE addr
,
390 if (!name
|| !len
|| !addr
|| !type
)
391 return ERROR_INVALID_PARAMETER
;
393 fd
= socket(PF_INET
, SOCK_DGRAM
, 0);
395 if (isLoopbackInterface(fd
, name
)) {
396 *type
= MIB_IF_TYPE_LOOPBACK
;
397 memset(addr
, 0, *len
);
403 struct sockaddr_in
*saddr
;
407 lstrcpynA(ifr
.ifr_name
, name
, IFNAMSIZ
);
408 ioctl(fd
, SIOCGIFADDR
, &ifr
);
409 memset(&arp
, 0, sizeof(struct arpreq
));
410 arp
.arp_pa
.sa_family
= AF_INET
;
411 saddr
= (struct sockaddr_in
*)&arp
; /* proto addr is first member */
412 saddr
->sin_family
= AF_INET
;
413 memcpy(&saddr
->sin_addr
.s_addr
, ifr
.ifr_addr
.sa_data
+ 2, sizeof(DWORD
));
414 if ((ioctl(fd
, SIOCGARP
, &arp
)))
415 ret
= ERROR_INVALID_DATA
;
417 /* FIXME: heh: who said it was ethernet? */
418 int addrLen
= ETH_ALEN
;
420 if (addrLen
> *len
) {
421 ret
= ERROR_INSUFFICIENT_BUFFER
;
426 memcpy(addr
, &arp
.arp_ha
.sa_data
[0], addrLen
);
427 /* zero out remaining bytes for broken implementations */
428 memset(addr
+ addrLen
, 0, *len
- addrLen
);
430 *type
= MIB_IF_TYPE_ETHERNET
;
438 ret
= ERROR_NO_MORE_FILES
;
442 #elif defined (HAVE_SYS_SYSCTL_H) && defined (HAVE_NET_IF_DL_H)
443 DWORD
getInterfacePhysicalByName(const char *name
, PDWORD len
, PBYTE addr
,
447 struct if_msghdr
*ifm
;
448 struct sockaddr_dl
*sdl
;
451 int mib
[] = { CTL_NET
, AF_ROUTE
, 0, AF_LINK
, NET_RT_IFLIST
, 0 };
455 if (!name
|| !len
|| !addr
|| !type
)
456 return ERROR_INVALID_PARAMETER
;
458 if (sysctl(mib
, 6, NULL
, &mibLen
, NULL
, 0) < 0)
459 return ERROR_NO_MORE_FILES
;
461 buf
= HeapAlloc(GetProcessHeap(), 0, mibLen
);
463 return ERROR_NOT_ENOUGH_MEMORY
;
465 if (sysctl(mib
, 6, buf
, &mibLen
, NULL
, 0) < 0) {
466 HeapFree(GetProcessHeap(), 0, buf
);
467 return ERROR_NO_MORE_FILES
;
470 ret
= ERROR_INVALID_DATA
;
471 for (p
= buf
; !found
&& p
< buf
+ mibLen
; p
+= ifm
->ifm_msglen
) {
472 ifm
= (struct if_msghdr
*)p
;
473 sdl
= (struct sockaddr_dl
*)(ifm
+ 1);
475 if (ifm
->ifm_type
!= RTM_IFINFO
|| (ifm
->ifm_addrs
& RTA_IFP
) == 0)
478 if (sdl
->sdl_family
!= AF_LINK
|| sdl
->sdl_nlen
== 0 ||
479 memcmp(sdl
->sdl_data
, name
, max(sdl
->sdl_nlen
, strlen(name
))) != 0)
483 addrLen
= min(MAX_INTERFACE_PHYSADDR
, sdl
->sdl_alen
);
484 if (addrLen
> *len
) {
485 ret
= ERROR_INSUFFICIENT_BUFFER
;
490 memcpy(addr
, LLADDR(sdl
), addrLen
);
491 /* zero out remaining bytes for broken implementations */
492 memset(addr
+ addrLen
, 0, *len
- addrLen
);
494 #if defined(HAVE_NET_IF_TYPES_H)
495 switch (sdl
->sdl_type
)
498 *type
= MIB_IF_TYPE_ETHERNET
;
501 *type
= MIB_IF_TYPE_FDDI
;
503 case IFT_ISO88024
: /* Token Bus */
504 *type
= MIB_IF_TYPE_TOKENRING
;
506 case IFT_ISO88025
: /* Token Ring */
507 *type
= MIB_IF_TYPE_TOKENRING
;
510 *type
= MIB_IF_TYPE_PPP
;
513 *type
= MIB_IF_TYPE_SLIP
;
516 *type
= MIB_IF_TYPE_LOOPBACK
;
519 *type
= MIB_IF_TYPE_OTHER
;
522 /* default if we don't know */
523 *type
= MIB_IF_TYPE_ETHERNET
;
528 HeapFree(GetProcessHeap(), 0, buf
);
533 DWORD
getInterfacePhysicalByIndex(DWORD index
, PDWORD len
, PBYTE addr
,
536 char nameBuf
[IF_NAMESIZE
];
537 char *name
= getInterfaceNameByIndex(index
, nameBuf
);
540 return getInterfacePhysicalByName(name
, len
, addr
, type
);
542 return ERROR_INVALID_DATA
;
545 static DWORD
getInterfaceMtuByName(const char *name
, PDWORD mtu
)
551 return ERROR_INVALID_PARAMETER
;
553 return ERROR_INVALID_PARAMETER
;
555 fd
= socket(PF_INET
, SOCK_DGRAM
, 0);
559 lstrcpynA(ifr
.ifr_name
, name
, IFNAMSIZ
);
560 if ((ioctl(fd
, SIOCGIFMTU
, &ifr
)))
561 ret
= ERROR_INVALID_DATA
;
566 *mtu
= ifr
.ifr_metric
;
573 ret
= ERROR_NO_MORE_FILES
;
577 static DWORD
getInterfaceStatusByName(const char *name
, PDWORD status
)
583 return ERROR_INVALID_PARAMETER
;
585 return ERROR_INVALID_PARAMETER
;
587 fd
= socket(PF_INET
, SOCK_DGRAM
, 0);
591 lstrcpynA(ifr
.ifr_name
, name
, IFNAMSIZ
);
592 if ((ioctl(fd
, SIOCGIFFLAGS
, &ifr
)))
593 ret
= ERROR_INVALID_DATA
;
595 if (ifr
.ifr_flags
& IFF_UP
)
596 *status
= MIB_IF_OPER_STATUS_OPERATIONAL
;
598 *status
= MIB_IF_OPER_STATUS_NON_OPERATIONAL
;
604 ret
= ERROR_NO_MORE_FILES
;
608 DWORD
getInterfaceEntryByName(const char *name
, PMIB_IFROW entry
)
610 BYTE addr
[MAX_INTERFACE_PHYSADDR
];
611 DWORD ret
, len
= sizeof(addr
), type
;
614 return ERROR_INVALID_PARAMETER
;
616 return ERROR_INVALID_PARAMETER
;
618 if (getInterfacePhysicalByName(name
, &len
, addr
, &type
) == NO_ERROR
) {
622 memset(entry
, 0, sizeof(MIB_IFROW
));
623 for (assigner
= entry
->wszName
, walker
= name
; *walker
;
624 walker
++, assigner
++)
627 getInterfaceIndexByName(name
, &entry
->dwIndex
);
628 entry
->dwPhysAddrLen
= len
;
629 memcpy(entry
->bPhysAddr
, addr
, len
);
630 memset(entry
->bPhysAddr
+ len
, 0, sizeof(entry
->bPhysAddr
) - len
);
631 entry
->dwType
= type
;
632 /* FIXME: how to calculate real speed? */
633 getInterfaceMtuByName(name
, &entry
->dwMtu
);
634 /* lie, there's no "administratively down" here */
635 entry
->dwAdminStatus
= MIB_IF_ADMIN_STATUS_UP
;
636 getInterfaceStatusByName(name
, &entry
->dwOperStatus
);
637 /* punt on dwLastChange? */
638 entry
->dwDescrLen
= min(strlen(name
), MAX_INTERFACE_DESCRIPTION
- 1);
639 memcpy(entry
->bDescr
, name
, entry
->dwDescrLen
);
640 entry
->bDescr
[entry
->dwDescrLen
] = '\0';
645 ret
= ERROR_INVALID_DATA
;
649 DWORD
getInterfaceEntryByIndex(DWORD index
, PMIB_IFROW entry
)
651 char nameBuf
[IF_NAMESIZE
];
652 char *name
= getInterfaceNameByIndex(index
, nameBuf
);
655 return getInterfaceEntryByName(name
, entry
);
657 return 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 *ppIpAddrTable
= HeapAlloc(heap
, flags
, sizeof(MIB_IPADDRTABLE
) +
748 (numAddresses
- 1) * sizeof(MIB_IPADDRROW
));
749 if (*ppIpAddrTable
) {
754 (*ppIpAddrTable
)->dwNumEntries
= numAddresses
;
756 while (!ret
&& ifPtr
&& ifPtr
< ifc
.ifc_buf
+ ifc
.ifc_len
) {
757 struct ifreq
*ifr
= (struct ifreq
*)ifPtr
;
759 ifPtr
+= ifreq_len(ifr
);
761 if (ifr
->ifr_addr
.sa_family
!= AF_INET
)
764 ret
= getInterfaceIndexByName(ifr
->ifr_name
,
765 &(*ppIpAddrTable
)->table
[i
].dwIndex
);
766 memcpy(&(*ppIpAddrTable
)->table
[i
].dwAddr
, ifr
->ifr_addr
.sa_data
+ 2,
768 (*ppIpAddrTable
)->table
[i
].dwMask
=
769 getInterfaceMaskByName(ifr
->ifr_name
);
770 /* the dwBCastAddr member isn't the broadcast address, it indicates
771 * whether the interface uses the 1's broadcast address (1) or the
772 * 0's broadcast address (0).
774 bcast
= getInterfaceBCastAddrByName(ifr
->ifr_name
);
775 (*ppIpAddrTable
)->table
[i
].dwBCastAddr
=
776 (bcast
& (*ppIpAddrTable
)->table
[i
].dwMask
) ? 1 : 0;
777 /* FIXME: hardcoded reasm size, not sure where to get it */
778 (*ppIpAddrTable
)->table
[i
].dwReasmSize
= 65535;
780 (*ppIpAddrTable
)->table
[i
].unused1
= 0;
781 (*ppIpAddrTable
)->table
[i
].wType
= 0;
786 ret
= ERROR_OUTOFMEMORY
;
787 HeapFree(GetProcessHeap(), 0, ifc
.ifc_buf
);
793 char *toIPAddressString(unsigned int addr
, char string
[16])
796 struct in_addr iAddr
;
799 /* extra-anal, just to make auditors happy */
800 lstrcpynA(string
, inet_ntoa(iAddr
), 16);