2 * iphlpapi dll implementation
4 * Copyright (C) 2003 Juan Lang
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "iphlpapi_private.h"
26 #include <sys/types.h>
27 #ifdef HAVE_NETINET_IN_H
28 # include <netinet/in.h>
30 #ifdef HAVE_ARPA_INET_H
31 # include <arpa/inet.h>
33 #ifdef HAVE_ARPA_NAMESER_H
34 # include <arpa/nameser.h>
50 #include "wine/debug.h"
54 WINE_DEFAULT_DEBUG_CHANNEL(iphlpapi
);
56 typedef struct _NAME_SERVER_LIST_CONTEXT
{
59 PIP_PER_ADAPTER_INFO pData
;
61 IP_ADDR_STRING
*pLastAddr
;
62 } NAME_SERVER_LIST_CONTEXT
, *PNAME_SERVER_LIST_CONTEXT
;
64 BOOL WINAPI
DllMain (HINSTANCE hinstDLL
, DWORD fdwReason
, LPVOID lpvReserved
)
68 case DLL_PROCESS_ATTACH
:
69 DisableThreadLibraryCalls( hinstDLL
);
73 case DLL_PROCESS_DETACH
:
80 /******************************************************************
81 * AddIPAddress (IPHLPAPI.@)
90 * NTEInstance [In/Out]
97 DWORD WINAPI
AddIPAddress(IPAddr Address
, IPMask Netmask
, DWORD IfIndex
, PULONG NteContext
, PULONG NteInstance
)
99 return RtlNtStatusToDosError(addIPAddress(Address
, Netmask
, IfIndex
, NteContext
, NteInstance
));
102 DWORD
getInterfaceGatewayByIndex(DWORD index
)
104 DWORD ndx
, retVal
= 0, numRoutes
= getNumRoutes();
105 RouteTable
*table
= getRouteTable();
106 if (!table
) return 0;
108 for (ndx
= 0; ndx
< numRoutes
; ndx
++)
110 if ((table
->routes
[ndx
].ifIndex
== (index
)) && (table
->routes
[ndx
].dest
== 0))
111 retVal
= table
->routes
[ndx
].gateway
;
113 HeapFree(GetProcessHeap(), 0, table
);
117 /******************************************************************
118 * AllocateAndGetIfTableFromStack (IPHLPAPI.@)
123 * ppIfTable [Out] -- pointer into which the MIB_IFTABLE is
124 * allocated and returned.
125 * bOrder [In] -- passed to GetIfTable to order the table
126 * heap [In] -- heap from which the table is allocated
127 * flags [In] -- flags to HeapAlloc
129 * RETURNS -- ERROR_INVALID_PARAMETER if ppIfTable is NULL, whatever
130 * GetIfTable returns otherwise
133 DWORD WINAPI
AllocateAndGetIfTableFromStack(PMIB_IFTABLE
*ppIfTable
,
134 BOOL bOrder
, HANDLE heap
, DWORD flags
)
138 TRACE("ppIfTable %p, bOrder %ld, heap 0x%08lx, flags 0x%08lx\n", ppIfTable
,
139 (DWORD
)bOrder
, (DWORD
)heap
, flags
);
141 ret
= ERROR_INVALID_PARAMETER
;
145 ret
= GetIfTable(*ppIfTable
, &dwSize
, bOrder
);
146 if (ret
== ERROR_INSUFFICIENT_BUFFER
) {
147 *ppIfTable
= (PMIB_IFTABLE
)HeapAlloc(heap
, flags
, dwSize
);
148 ret
= GetIfTable(*ppIfTable
, &dwSize
, bOrder
);
151 TRACE("returning %ld\n", ret
);
156 /******************************************************************
157 * AllocateAndGetIpAddrTableFromStack (IPHLPAPI.@)
162 * ppIpAddrTable [Out]
163 * bOrder [In] -- passed to GetIpAddrTable to order the table
164 * heap [In] -- heap from which the table is allocated
165 * flags [In] -- flags to HeapAlloc
172 DWORD WINAPI
AllocateAndGetIpAddrTableFromStack(PMIB_IPADDRTABLE
*ppIpAddrTable
,
173 BOOL bOrder
, HANDLE heap
, DWORD flags
)
177 TRACE("ppIpAddrTable %p, bOrder %ld, heap 0x%08lx, flags 0x%08lx\n",
178 ppIpAddrTable
, (DWORD
)bOrder
, (DWORD
)heap
, flags
);
180 ret
= ERROR_INVALID_PARAMETER
;
184 ret
= GetIpAddrTable(*ppIpAddrTable
, &dwSize
, bOrder
);
185 if (ret
== ERROR_INSUFFICIENT_BUFFER
) {
186 *ppIpAddrTable
= (PMIB_IPADDRTABLE
)HeapAlloc(heap
, flags
, dwSize
);
187 ret
= GetIpAddrTable(*ppIpAddrTable
, &dwSize
, bOrder
);
190 TRACE("returning %ld\n", ret
);
195 /******************************************************************
196 * AllocateAndGetIpForwardTableFromStack (IPHLPAPI.@)
199 * ppIpForwardTable [Out] -- pointer into which the MIB_IPFORWARDTABLE is
200 * allocated and returned.
201 * bOrder [In] -- passed to GetIfTable to order the table
202 * heap [In] -- heap from which the table is allocated
203 * flags [In] -- flags to HeapAlloc
205 * RETURNS -- ERROR_INVALID_PARAMETER if ppIfTable is NULL, whatever
206 * GetIpForwardTable returns otherwise
209 DWORD WINAPI
AllocateAndGetIpForwardTableFromStack(PMIB_IPFORWARDTABLE
*
210 ppIpForwardTable
, BOOL bOrder
, HANDLE heap
, DWORD flags
)
214 TRACE("ppIpForwardTable %p, bOrder %ld, heap 0x%08lx, flags 0x%08lx\n",
215 ppIpForwardTable
, (DWORD
)bOrder
, (DWORD
)heap
, flags
);
216 if (!ppIpForwardTable
)
217 ret
= ERROR_INVALID_PARAMETER
;
221 ret
= GetIpForwardTable(*ppIpForwardTable
, &dwSize
, bOrder
);
222 if (ret
== ERROR_INSUFFICIENT_BUFFER
) {
223 *ppIpForwardTable
= (PMIB_IPFORWARDTABLE
)HeapAlloc(heap
, flags
, dwSize
);
224 ret
= GetIpForwardTable(*ppIpForwardTable
, &dwSize
, bOrder
);
227 TRACE("returning %ld\n", ret
);
232 /******************************************************************
233 * AllocateAndGetIpNetTableFromStack (IPHLPAPI.@)
239 * bOrder [In] -- passed to GetIpNetTable to order the table
240 * heap [In] -- heap from which the table is allocated
241 * flags [In] -- flags to HeapAlloc
248 DWORD WINAPI
AllocateAndGetIpNetTableFromStack(PMIB_IPNETTABLE
*ppIpNetTable
,
249 BOOL bOrder
, HANDLE heap
, DWORD flags
)
253 TRACE("ppIpNetTable %p, bOrder %ld, heap 0x%08lx, flags 0x%08lx\n",
254 ppIpNetTable
, (DWORD
)bOrder
, (DWORD
)heap
, flags
);
256 ret
= ERROR_INVALID_PARAMETER
;
260 ret
= GetIpNetTable(*ppIpNetTable
, &dwSize
, bOrder
);
261 if (ret
== ERROR_INSUFFICIENT_BUFFER
) {
262 *ppIpNetTable
= (PMIB_IPNETTABLE
)HeapAlloc(heap
, flags
, dwSize
);
263 ret
= GetIpNetTable(*ppIpNetTable
, &dwSize
, bOrder
);
266 TRACE("returning %ld\n", ret
);
271 /******************************************************************
272 * AllocateAndGetTcpTableFromStack (IPHLPAPI.@)
278 * bOrder [In] -- passed to GetTcpTable to order the table
279 * heap [In] -- heap from which the table is allocated
280 * flags [In] -- flags to HeapAlloc
287 DWORD WINAPI
AllocateAndGetTcpTableFromStack(PMIB_TCPTABLE
*ppTcpTable
,
288 BOOL bOrder
, HANDLE heap
, DWORD flags
)
292 TRACE("ppTcpTable %p, bOrder %ld, heap 0x%08lx, flags 0x%08lx\n",
293 ppTcpTable
, (DWORD
)bOrder
, (DWORD
)heap
, flags
);
295 ret
= ERROR_INVALID_PARAMETER
;
299 ret
= GetTcpTable(*ppTcpTable
, &dwSize
, bOrder
);
300 if (ret
== ERROR_INSUFFICIENT_BUFFER
) {
301 *ppTcpTable
= (PMIB_TCPTABLE
)HeapAlloc(heap
, flags
, dwSize
);
302 ret
= GetTcpTable(*ppTcpTable
, &dwSize
, bOrder
);
305 TRACE("returning %ld\n", ret
);
310 /******************************************************************
311 * AllocateAndGetUdpTableFromStack (IPHLPAPI.@)
317 * bOrder [In] -- passed to GetUdpTable to order the table
318 * heap [In] -- heap from which the table is allocated
319 * flags [In] -- flags to HeapAlloc
326 DWORD WINAPI
AllocateAndGetUdpTableFromStack(PMIB_UDPTABLE
*ppUdpTable
,
327 BOOL bOrder
, HANDLE heap
, DWORD flags
)
331 TRACE("ppUdpTable %p, bOrder %ld, heap 0x%08lx, flags 0x%08lx\n",
332 ppUdpTable
, (DWORD
)bOrder
, (DWORD
)heap
, flags
);
334 ret
= ERROR_INVALID_PARAMETER
;
338 ret
= GetUdpTable(*ppUdpTable
, &dwSize
, bOrder
);
339 if (ret
== ERROR_INSUFFICIENT_BUFFER
) {
340 *ppUdpTable
= (PMIB_UDPTABLE
)HeapAlloc(heap
, flags
, dwSize
);
341 ret
= GetUdpTable(*ppUdpTable
, &dwSize
, bOrder
);
344 TRACE("returning %ld\n", ret
);
349 /******************************************************************
350 * CreateIpForwardEntry (IPHLPAPI.@)
362 DWORD WINAPI
CreateIpForwardEntry(PMIB_IPFORWARDROW pRoute
)
364 return createIpForwardEntry( pRoute
);
368 /******************************************************************
369 * CreateIpNetEntry (IPHLPAPI.@)
381 DWORD WINAPI
CreateIpNetEntry(PMIB_IPNETROW pArpEntry
)
383 TRACE("pArpEntry %p\n", pArpEntry
);
384 /* could use SIOCSARP on systems that support it, not sure I want to */
390 /******************************************************************
391 * CreateProxyArpEntry (IPHLPAPI.@)
405 DWORD WINAPI
CreateProxyArpEntry(DWORD dwAddress
, DWORD dwMask
, DWORD dwIfIndex
)
407 TRACE("dwAddress 0x%08lx, dwMask 0x%08lx, dwIfIndex 0x%08lx\n", dwAddress
,
410 /* marking Win2K+ functions not supported */
411 return ERROR_NOT_SUPPORTED
;
415 /******************************************************************
416 * DeleteIPAddress (IPHLPAPI.@)
428 DWORD WINAPI
DeleteIPAddress(ULONG NTEContext
)
430 TRACE("NTEContext %ld\n", NTEContext
);
431 return RtlNtStatusToDosError(deleteIpAddress(NTEContext
));
435 /******************************************************************
436 * DeleteIpForwardEntry (IPHLPAPI.@)
448 DWORD WINAPI
DeleteIpForwardEntry(PMIB_IPFORWARDROW pRoute
)
450 return deleteIpForwardEntry( pRoute
);
454 /******************************************************************
455 * DeleteIpNetEntry (IPHLPAPI.@)
467 DWORD WINAPI
DeleteIpNetEntry(PMIB_IPNETROW pArpEntry
)
469 TRACE("pArpEntry %p\n", pArpEntry
);
470 /* could use SIOCDARP on systems that support it, not sure I want to */
476 /******************************************************************
477 * DeleteProxyArpEntry (IPHLPAPI.@)
491 DWORD WINAPI
DeleteProxyArpEntry(DWORD dwAddress
, DWORD dwMask
, DWORD dwIfIndex
)
493 TRACE("dwAddress 0x%08lx, dwMask 0x%08lx, dwIfIndex 0x%08lx\n", dwAddress
,
496 /* marking Win2K+ functions not supported */
497 return ERROR_NOT_SUPPORTED
;
500 /******************************************************************
501 * EnableRouter (IPHLPAPI.@)
507 * pOverlapped [In/Out]
514 DWORD WINAPI
EnableRouter(HANDLE
* pHandle
, OVERLAPPED
* pOverlapped
)
516 TRACE("pHandle %p, pOverlapped %p\n", pHandle
, pOverlapped
);
518 /* could echo "1" > /proc/net/sys/net/ipv4/ip_forward, not sure I want to
519 could map EACCESS to ERROR_ACCESS_DENIED, I suppose
520 marking Win2K+ functions not supported */
521 return ERROR_NOT_SUPPORTED
;
525 /******************************************************************
526 * FlushIpNetTable (IPHLPAPI.@)
538 DWORD WINAPI
FlushIpNetTable(DWORD dwIfIndex
)
540 TRACE("dwIfIndex 0x%08lx\n", dwIfIndex
);
542 /* this flushes the arp cache of the given index
543 marking Win2K+ functions not supported */
544 return ERROR_NOT_SUPPORTED
;
548 /******************************************************************
549 * GetAdapterIndex (IPHLPAPI.@)
554 * AdapterName [In/Out]
562 DWORD WINAPI
GetAdapterIndex(LPWSTR AdapterName
, PULONG IfIndex
)
564 TRACE("AdapterName %p, IfIndex %p\n", AdapterName
, IfIndex
);
566 /* marking Win2K+ functions not supported */
567 return ERROR_NOT_SUPPORTED
;
571 /******************************************************************
572 * GetAdaptersInfo (IPHLPAPI.@)
577 * pAdapterInfo [In/Out]
578 * pOutBufLen [In/Out]
585 DWORD WINAPI
GetAdaptersInfo(PIP_ADAPTER_INFO pAdapterInfo
, PULONG pOutBufLen
)
591 TRACE("pAdapterInfo %p, pOutBufLen %p\n", pAdapterInfo
, pOutBufLen
);
593 ret
= ERROR_INVALID_PARAMETER
;
595 DWORD numNonLoopbackInterfaces
= getNumNonLoopbackInterfaces();
597 if (numNonLoopbackInterfaces
> 0) {
598 /* this calculation assumes only one address in the IP_ADDR_STRING lists.
599 that's okay, because:
600 - we don't get multiple addresses per adapter anyway
601 - we don't know about per-adapter gateways
602 - DHCP and WINS servers can have max one entry per list */
603 ULONG size
= sizeof(IP_ADAPTER_INFO
) * numNonLoopbackInterfaces
;
605 if (!pAdapterInfo
|| *pOutBufLen
< size
) {
607 ret
= ERROR_BUFFER_OVERFLOW
;
610 InterfaceIndexTable
*table
= getNonLoopbackInterfaceIndexTable();
613 size
= sizeof(IP_ADAPTER_INFO
) * table
->numIndexes
;
614 if (*pOutBufLen
< size
) {
616 ret
= ERROR_INSUFFICIENT_BUFFER
;
621 BOOL winsEnabled
= FALSE
;
622 IP_ADDRESS_STRING primaryWINS
, secondaryWINS
;
624 memset(pAdapterInfo
, 0, size
);
625 if (RegOpenKeyExA(HKEY_LOCAL_MACHINE
,
626 "Software\\Wine\\Wine\\Config\\Network", 0, KEY_READ
,
627 &hKey
) == ERROR_SUCCESS
) {
628 DWORD size
= sizeof(primaryWINS
.String
);
631 RegQueryValueExA(hKey
, "WinsServer", NULL
, NULL
,
632 (PBYTE
)primaryWINS
.String
, &size
);
633 addr
= inet_addr(primaryWINS
.String
);
634 if (addr
!= INADDR_NONE
&& addr
!= INADDR_ANY
)
636 size
= sizeof(secondaryWINS
.String
);
637 RegQueryValueExA(hKey
, "BackupWinsServer", NULL
, NULL
,
638 (PBYTE
)secondaryWINS
.String
, &size
);
639 addr
= inet_addr(secondaryWINS
.String
);
640 if (addr
!= INADDR_NONE
&& addr
!= INADDR_ANY
)
644 TRACE("num of index is %lu\n", table
->numIndexes
);
645 for (ndx
= 0; ndx
< table
->numIndexes
; ndx
++) {
646 PIP_ADAPTER_INFO ptr
= &pAdapterInfo
[ndx
];
647 DWORD addrLen
= sizeof(ptr
->Address
), type
;
649 getInterfaceNameByIndex(table
->indexes
[ndx
]);
651 ret
= ERROR_OUTOFMEMORY
;
655 /* on Win98 this is left empty, but whatever */
656 strncpy(ptr
->AdapterName
,ifname
,sizeof(ptr
->AdapterName
));
657 consumeInterfaceName(ifname
);
658 ptr
->AdapterName
[MAX_ADAPTER_NAME_LENGTH
] = '\0';
659 getInterfacePhysicalByIndex(table
->indexes
[ndx
], &addrLen
,
660 ptr
->Address
, &type
);
661 /* MS defines address length and type as UINT in some places and
662 DWORD in others, **sigh**. Don't want to assume that PUINT and
663 PDWORD are equiv (64-bit?) */
664 ptr
->AddressLength
= addrLen
;
666 ptr
->Index
= table
->indexes
[ndx
];
667 toIPAddressString(getInterfaceIPAddrByIndex(table
->indexes
[ndx
]),
668 ptr
->IpAddressList
.IpAddress
.String
);
669 toIPAddressString(getInterfaceMaskByIndex(table
->indexes
[ndx
]),
670 ptr
->IpAddressList
.IpMask
.String
);
671 toIPAddressString(getInterfaceGatewayByIndex(table
->indexes
[ndx
]),
672 ptr
->GatewayList
.IpAddress
.String
);
673 getDhcpInfoForAdapter(table
->indexes
[ndx
], &dhcpEnabled
,
674 &dhcpServer
, &ptr
->LeaseObtained
,
676 ptr
->DhcpEnabled
= (DWORD
) dhcpEnabled
;
677 toIPAddressString(dhcpServer
,
678 ptr
->DhcpServer
.IpAddress
.String
);
680 ptr
->HaveWins
= TRUE
;
681 memcpy(ptr
->PrimaryWinsServer
.IpAddress
.String
,
682 primaryWINS
.String
, sizeof(primaryWINS
.String
));
683 memcpy(ptr
->SecondaryWinsServer
.IpAddress
.String
,
684 secondaryWINS
.String
, sizeof(secondaryWINS
.String
));
686 if (ndx
< table
->numIndexes
- 1)
687 ptr
->Next
= &pAdapterInfo
[ndx
+ 1];
696 ret
= ERROR_OUTOFMEMORY
;
702 TRACE("returning %ld\n", ret
);
707 /******************************************************************
708 * GetBestInterface (IPHLPAPI.@)
714 * pdwBestIfIndex [In/Out]
721 DWORD WINAPI
GetBestInterface(IPAddr dwDestAddr
, PDWORD pdwBestIfIndex
)
725 TRACE("dwDestAddr 0x%08lx, pdwBestIfIndex %p\n", dwDestAddr
, pdwBestIfIndex
);
727 ret
= ERROR_INVALID_PARAMETER
;
729 MIB_IPFORWARDROW ipRow
;
731 ret
= GetBestRoute(dwDestAddr
, 0, &ipRow
);
732 if (ret
== ERROR_SUCCESS
)
733 *pdwBestIfIndex
= ipRow
.dwForwardIfIndex
;
735 TRACE("returning %ld\n", ret
);
740 /******************************************************************
741 * GetBestRoute (IPHLPAPI.@)
755 DWORD WINAPI
GetBestRoute(DWORD dwDestAddr
, DWORD dwSourceAddr
, PMIB_IPFORWARDROW pBestRoute
)
757 PMIB_IPFORWARDTABLE table
;
760 TRACE("dwDestAddr 0x%08lx, dwSourceAddr 0x%08lx, pBestRoute %p\n", dwDestAddr
,
761 dwSourceAddr
, pBestRoute
);
763 return ERROR_INVALID_PARAMETER
;
765 AllocateAndGetIpForwardTableFromStack(&table
, FALSE
, GetProcessHeap(), 0);
767 DWORD ndx
, matchedBits
, matchedNdx
= 0;
769 for (ndx
= 0, matchedBits
= 0; ndx
< table
->dwNumEntries
; ndx
++) {
770 if ((dwDestAddr
& table
->table
[ndx
].dwForwardMask
) ==
771 (table
->table
[ndx
].dwForwardDest
& table
->table
[ndx
].dwForwardMask
)) {
772 DWORD numShifts
, mask
;
774 for (numShifts
= 0, mask
= table
->table
[ndx
].dwForwardMask
;
775 mask
&& !(mask
& 1); mask
>>= 1, numShifts
++)
777 if (numShifts
> matchedBits
) {
778 matchedBits
= numShifts
;
783 memcpy(pBestRoute
, &table
->table
[matchedNdx
], sizeof(MIB_IPFORWARDROW
));
784 HeapFree(GetProcessHeap(), 0, table
);
788 ret
= ERROR_OUTOFMEMORY
;
789 TRACE("returning %ld\n", ret
);
793 /******************************************************************
794 * GetExtendedTcpTable (IPHLPAPI.@)
796 * Get the table of TCP endpoints available to the application.
799 * pTcpTable [Out] table struct with the filtered TCP endpoints available to application
800 * pdwSize [In/Out] estimated size of the structure returned in pTcpTable, in bytes
801 * bOrder [In] whether to order the table
802 * ulAf [in] version of IP used by the TCP endpoints
803 * TableClass [in] type of the TCP table structure from TCP_TABLE_CLASS
804 * Reserved [in] reserved - this value must be zero
808 * Failure: either ERROR_INSUFFICIENT_BUFFER or ERROR_INVALID_PARAMETER
812 DWORD WINAPI
GetExtendedTcpTable(PVOID pTcpTable
, PDWORD pdwSize
, BOOL bOrder
, ULONG ulAf
, TCP_TABLE_CLASS TableClass
, ULONG Reserved
)
814 DWORD ret
= NO_ERROR
;
820 /******************************************************************
821 * GetFriendlyIfIndex (IPHLPAPI.@)
833 DWORD WINAPI
GetFriendlyIfIndex(DWORD IfIndex
)
835 /* windows doesn't validate these, either, just makes sure the top byte is
836 cleared. I assume my ifenum module never gives an index with the top
838 TRACE("returning %ld\n", IfIndex
);
843 /******************************************************************
844 * GetIcmpStatistics (IPHLPAPI.@)
856 DWORD WINAPI
GetIcmpStatistics(PMIB_ICMP pStats
)
860 TRACE("pStats %p\n", pStats
);
861 ret
= getICMPStats(pStats
);
862 TRACE("returning %ld\n", ret
);
867 /******************************************************************
868 * GetIfEntry (IPHLPAPI.@)
880 DWORD WINAPI
GetIfEntry(PMIB_IFROW pIfRow
)
885 TRACE("pIfRow %p\n", pIfRow
);
887 return ERROR_INVALID_PARAMETER
;
889 name
= getInterfaceNameByIndex(pIfRow
->dwIndex
);
891 ret
= getInterfaceEntryByIndex(pIfRow
->dwIndex
, pIfRow
);
893 ret
= getInterfaceStatsByName(name
, pIfRow
);
894 consumeInterfaceName(name
);
897 ret
= ERROR_INVALID_DATA
;
898 TRACE("returning %ld\n", ret
);
903 static int IfTableSorter(const void *a
, const void *b
)
908 ret
= ((PMIB_IFROW
)a
)->dwIndex
- ((PMIB_IFROW
)b
)->dwIndex
;
915 /******************************************************************
916 * GetIfTable (IPHLPAPI.@)
930 DWORD WINAPI
GetIfTable(PMIB_IFTABLE pIfTable
, PULONG pdwSize
, BOOL bOrder
)
934 TRACE("pIfTable %p, pdwSize %p, bOrder %ld\n", pdwSize
, pdwSize
,
937 ret
= ERROR_INVALID_PARAMETER
;
939 DWORD numInterfaces
= getNumInterfaces();
941 TRACE("GetIfTable: numInterfaces = %d\n", (int)numInterfaces
);
942 size
= sizeof(MIB_IFTABLE
) + (numInterfaces
- 1) * sizeof(MIB_IFROW
);
944 if (!pIfTable
|| *pdwSize
< size
) {
946 ret
= ERROR_INSUFFICIENT_BUFFER
;
949 InterfaceIndexTable
*table
= getInterfaceIndexTable();
952 size
= sizeof(MIB_IFTABLE
) + (table
->numIndexes
- 1) *
954 if (*pdwSize
< size
) {
956 ret
= ERROR_INSUFFICIENT_BUFFER
;
961 pIfTable
->dwNumEntries
= 0;
962 for (ndx
= 0; ndx
< table
->numIndexes
; ndx
++) {
963 pIfTable
->table
[ndx
].dwIndex
= table
->indexes
[ndx
];
964 GetIfEntry(&pIfTable
->table
[ndx
]);
965 pIfTable
->dwNumEntries
++;
968 qsort(pIfTable
->table
, pIfTable
->dwNumEntries
, sizeof(MIB_IFROW
),
975 ret
= ERROR_OUTOFMEMORY
;
978 TRACE("returning %ld\n", ret
);
983 /******************************************************************
984 * GetInterfaceInfo (IPHLPAPI.@)
990 * dwOutBufLen [In/Out]
997 DWORD WINAPI
GetInterfaceInfo(PIP_INTERFACE_INFO pIfTable
, PULONG dwOutBufLen
)
1001 TRACE("pIfTable %p, dwOutBufLen %p\n", pIfTable
, dwOutBufLen
);
1003 ret
= ERROR_INVALID_PARAMETER
;
1005 DWORD numNonLoopbackInterfaces
= getNumNonLoopbackInterfaces();
1007 TRACE("numNonLoopbackInterfaces == 0x%x\n", numNonLoopbackInterfaces
);
1008 size
= sizeof(IP_INTERFACE_INFO
) + (numNonLoopbackInterfaces
) *
1009 sizeof(IP_ADAPTER_INDEX_MAP
);
1011 if (!pIfTable
|| *dwOutBufLen
< size
) {
1012 *dwOutBufLen
= size
;
1013 ret
= ERROR_INSUFFICIENT_BUFFER
;
1016 InterfaceIndexTable
*table
= getNonLoopbackInterfaceIndexTable();
1019 TRACE("table->numIndexes == 0x%x\n", table
->numIndexes
);
1020 size
= sizeof(IP_INTERFACE_INFO
) + (table
->numIndexes
) *
1021 sizeof(IP_ADAPTER_INDEX_MAP
);
1022 if (*dwOutBufLen
< size
) {
1023 *dwOutBufLen
= size
;
1024 ret
= ERROR_INSUFFICIENT_BUFFER
;
1029 pIfTable
->NumAdapters
= 0;
1030 for (ndx
= 0; ndx
< table
->numIndexes
; ndx
++) {
1031 const char *walker
, *name
;
1034 pIfTable
->Adapter
[ndx
].Index
= table
->indexes
[ndx
];
1035 name
= getInterfaceNameByIndex(table
->indexes
[ndx
]);
1036 for (walker
= name
, assigner
= pIfTable
->Adapter
[ndx
].Name
;
1037 walker
&& *walker
&&
1038 assigner
- pIfTable
->Adapter
[ndx
].Name
< MAX_ADAPTER_NAME
- 1;
1039 walker
++, assigner
++)
1040 *assigner
= *walker
;
1042 consumeInterfaceName(name
);
1043 pIfTable
->NumAdapters
++;
1050 ret
= ERROR_OUTOFMEMORY
;
1053 TRACE("returning %ld\n", ret
);
1058 static int IpAddrTableSorter(const void *a
, const void *b
)
1063 ret
= ((PMIB_IPADDRROW
)a
)->dwAddr
- ((PMIB_IPADDRROW
)b
)->dwAddr
;
1070 /******************************************************************
1071 * GetIpAddrTable (IPHLPAPI.@)
1076 * pIpAddrTable [In/Out]
1085 DWORD WINAPI
GetIpAddrTable(PMIB_IPADDRTABLE pIpAddrTable
, PULONG pdwSize
, BOOL bOrder
)
1089 TRACE("pIpAddrTable %p, pdwSize %p, bOrder %ld\n", pIpAddrTable
, pdwSize
,
1092 ret
= ERROR_INVALID_PARAMETER
;
1094 DWORD numInterfaces
= getNumInterfaces();
1095 ULONG size
= sizeof(MIB_IPADDRTABLE
) + (numInterfaces
- 1) *
1096 sizeof(MIB_IPADDRROW
);
1098 if (!pIpAddrTable
|| *pdwSize
< size
) {
1100 ret
= ERROR_INSUFFICIENT_BUFFER
;
1103 InterfaceIndexTable
*table
= getInterfaceIndexTable();
1106 size
= sizeof(MIB_IPADDRTABLE
) + (table
->numIndexes
- 1) *
1107 sizeof(MIB_IPADDRROW
);
1108 if (*pdwSize
< size
) {
1110 ret
= ERROR_INSUFFICIENT_BUFFER
;
1115 pIpAddrTable
->dwNumEntries
= 0;
1116 for (ndx
= 0; ndx
< table
->numIndexes
; ndx
++) {
1117 pIpAddrTable
->table
[ndx
].dwIndex
= table
->indexes
[ndx
];
1118 pIpAddrTable
->table
[ndx
].dwAddr
=
1119 getInterfaceIPAddrByIndex(table
->indexes
[ndx
]);
1120 pIpAddrTable
->table
[ndx
].dwMask
=
1121 getInterfaceMaskByIndex(table
->indexes
[ndx
]);
1122 /* the dwBCastAddr member isn't the broadcast address, it indicates
1123 * whether the interface uses the 1's broadcast address (1) or the
1124 * 0's broadcast address (0).
1126 bcast
= getInterfaceBCastAddrByIndex(table
->indexes
[ndx
]);
1127 pIpAddrTable
->table
[ndx
].dwBCastAddr
=
1128 (bcast
& pIpAddrTable
->table
[ndx
].dwMask
) ? 1 : 0;
1129 /* FIXME: hardcoded reasm size, not sure where to get it */
1130 pIpAddrTable
->table
[ndx
].dwReasmSize
= 65535;
1131 pIpAddrTable
->table
[ndx
].unused1
= 0;
1132 pIpAddrTable
->table
[ndx
].wType
= 0; /* aka unused2 */
1133 pIpAddrTable
->dwNumEntries
++;
1136 qsort(pIpAddrTable
->table
, pIpAddrTable
->dwNumEntries
,
1137 sizeof(MIB_IPADDRROW
), IpAddrTableSorter
);
1143 ret
= ERROR_OUTOFMEMORY
;
1146 TRACE("returning %ld\n", ret
);
1151 static int IpForwardTableSorter(const void *a
, const void *b
)
1156 PMIB_IPFORWARDROW rowA
= (PMIB_IPFORWARDROW
)a
, rowB
= (PMIB_IPFORWARDROW
)b
;
1158 ret
= rowA
->dwForwardDest
- rowB
->dwForwardDest
;
1160 ret
= rowA
->dwForwardProto
- rowB
->dwForwardProto
;
1162 ret
= rowA
->dwForwardPolicy
- rowB
->dwForwardPolicy
;
1164 ret
= rowA
->dwForwardNextHop
- rowB
->dwForwardNextHop
;
1174 /******************************************************************
1175 * GetIpForwardTable (IPHLPAPI.@)
1180 * pIpForwardTable [In/Out]
1189 DWORD WINAPI
GetIpForwardTable(PMIB_IPFORWARDTABLE pIpForwardTable
, PULONG pdwSize
, BOOL bOrder
)
1193 TRACE("pIpForwardTable %p, pdwSize %p, bOrder %ld\n", pIpForwardTable
,
1194 pdwSize
, (DWORD
)bOrder
);
1196 ret
= ERROR_INVALID_PARAMETER
;
1198 DWORD numRoutes
= getNumRoutes();
1199 ULONG sizeNeeded
= sizeof(MIB_IPFORWARDTABLE
) + (numRoutes
- 1) *
1200 sizeof(MIB_IPFORWARDROW
);
1202 if (!pIpForwardTable
|| *pdwSize
< sizeNeeded
) {
1203 *pdwSize
= sizeNeeded
;
1204 ret
= ERROR_INSUFFICIENT_BUFFER
;
1207 RouteTable
*table
= getRouteTable();
1209 sizeNeeded
= sizeof(MIB_IPFORWARDTABLE
) + (table
->numRoutes
- 1) *
1210 sizeof(MIB_IPFORWARDROW
);
1211 if (*pdwSize
< sizeNeeded
) {
1212 *pdwSize
= sizeNeeded
;
1213 ret
= ERROR_INSUFFICIENT_BUFFER
;
1218 pIpForwardTable
->dwNumEntries
= table
->numRoutes
;
1219 for (ndx
= 0; ndx
< numRoutes
; ndx
++) {
1220 pIpForwardTable
->table
[ndx
].dwForwardIfIndex
=
1221 table
->routes
[ndx
].ifIndex
+ 1;
1222 pIpForwardTable
->table
[ndx
].dwForwardDest
=
1223 table
->routes
[ndx
].dest
;
1224 pIpForwardTable
->table
[ndx
].dwForwardMask
=
1225 table
->routes
[ndx
].mask
;
1226 pIpForwardTable
->table
[ndx
].dwForwardPolicy
= 0;
1227 pIpForwardTable
->table
[ndx
].dwForwardNextHop
=
1228 table
->routes
[ndx
].gateway
;
1229 /* FIXME: this type is appropriate for local interfaces; may not
1230 always be appropriate */
1231 pIpForwardTable
->table
[ndx
].dwForwardType
= MIB_IPROUTE_TYPE_DIRECT
;
1232 /* FIXME: other protos might be appropriate, e.g. the default route
1233 is typically set with MIB_IPPROTO_NETMGMT instead */
1234 pIpForwardTable
->table
[ndx
].dwForwardProto
= MIB_IPPROTO_LOCAL
;
1235 /* punt on age and AS */
1236 pIpForwardTable
->table
[ndx
].dwForwardAge
= 0;
1237 pIpForwardTable
->table
[ndx
].dwForwardNextHopAS
= 0;
1238 pIpForwardTable
->table
[ndx
].dwForwardMetric1
=
1239 table
->routes
[ndx
].metric
;
1240 /* rest of the metrics are 0.. */
1241 pIpForwardTable
->table
[ndx
].dwForwardMetric2
= 0;
1242 pIpForwardTable
->table
[ndx
].dwForwardMetric3
= 0;
1243 pIpForwardTable
->table
[ndx
].dwForwardMetric4
= 0;
1244 pIpForwardTable
->table
[ndx
].dwForwardMetric5
= 0;
1247 qsort(pIpForwardTable
->table
, pIpForwardTable
->dwNumEntries
,
1248 sizeof(MIB_IPFORWARDROW
), IpForwardTableSorter
);
1251 HeapFree(GetProcessHeap(), 0, table
);
1254 ret
= ERROR_OUTOFMEMORY
;
1257 TRACE("returning %ld\n", ret
);
1262 static int IpNetTableSorter(const void *a
, const void *b
)
1267 ret
= ((PMIB_IPNETROW
)a
)->dwAddr
- ((PMIB_IPNETROW
)b
)->dwAddr
;
1274 /******************************************************************
1275 * GetIpNetTable (IPHLPAPI.@)
1280 * pIpNetTable [In/Out]
1289 DWORD WINAPI
GetIpNetTable(PMIB_IPNETTABLE pIpNetTable
, PULONG pdwSize
, BOOL bOrder
)
1291 DWORD ret
= NO_ERROR
;
1293 TRACE("pIpNetTable %p, pdwSize %p, bOrder %d\n", pIpNetTable
, pdwSize
,
1296 ret
= ERROR_INVALID_PARAMETER
;
1298 DWORD numEntries
= getNumArpEntries();
1299 ULONG size
= sizeof(MIB_IPNETTABLE
);
1302 size
+= (numEntries
- 1) * sizeof(MIB_IPNETROW
);
1303 if (!pIpNetTable
|| *pdwSize
< size
) {
1305 ret
= ERROR_INSUFFICIENT_BUFFER
;
1308 PMIB_IPNETTABLE table
= getArpTable();
1310 size
= sizeof(MIB_IPNETTABLE
);
1311 if (table
->dwNumEntries
> 1)
1312 size
+= (table
->dwNumEntries
- 1) * sizeof(MIB_IPNETROW
);
1313 if (*pdwSize
< size
) {
1315 ret
= ERROR_INSUFFICIENT_BUFFER
;
1319 memcpy(pIpNetTable
, table
, size
);
1321 qsort(pIpNetTable
->table
, pIpNetTable
->dwNumEntries
,
1322 sizeof(MIB_IPNETROW
), IpNetTableSorter
);
1325 HeapFree(GetProcessHeap(), 0, table
);
1329 TRACE("returning %d\n", ret
);
1334 /******************************************************************
1335 * GetIpStatistics (IPHLPAPI.@)
1347 DWORD WINAPI
GetIpStatistics(PMIB_IPSTATS pStats
)
1349 return GetIpStatisticsEx(pStats
, PF_INET
);
1352 /******************************************************************
1353 * GetIpStatisticsEx (IPHLPAPI.@)
1366 DWORD WINAPI
GetIpStatisticsEx(PMIB_IPSTATS pStats
, DWORD dwFamily
)
1370 TRACE("pStats %p\n", pStats
);
1371 ret
= getIPStats(pStats
, dwFamily
);
1372 TRACE("returning %ld\n", ret
);
1376 /******************************************************************
1377 * GetNetworkParams (IPHLPAPI.@)
1382 * pFixedInfo [In/Out]
1383 * pOutBufLen [In/Out]
1390 DWORD WINAPI
GetNetworkParams(PFIXED_INFO pFixedInfo
, PULONG pOutBufLen
)
1395 PIPHLP_RES_INFO resInfo
;
1397 TRACE("pFixedInfo %p, pOutBufLen %p\n", pFixedInfo
, pOutBufLen
);
1399 return ERROR_INVALID_PARAMETER
;
1401 resInfo
= getResInfo();
1403 return ERROR_OUTOFMEMORY
;
1405 size
= sizeof(FIXED_INFO
) + (resInfo
->riCount
> 1 ? (resInfo
->riCount
-1) *
1406 sizeof(IP_ADDR_STRING
) : 0);
1407 if (!pFixedInfo
|| *pOutBufLen
< size
) {
1409 disposeResInfo( resInfo
);
1410 return ERROR_BUFFER_OVERFLOW
;
1413 memset(pFixedInfo
, 0, size
);
1414 size
= sizeof(pFixedInfo
->HostName
);
1415 GetComputerNameExA(ComputerNameDnsHostname
, pFixedInfo
->HostName
, &size
);
1416 size
= sizeof(pFixedInfo
->DomainName
);
1417 GetComputerNameExA(ComputerNameDnsDomain
, pFixedInfo
->DomainName
, &size
);
1419 TRACE("GetComputerNameExA: %s\n", pFixedInfo
->DomainName
);
1421 if (resInfo
->riCount
> 0)
1423 CopyMemory(&pFixedInfo
->DnsServerList
, resInfo
->DnsList
, sizeof(IP_ADDR_STRING
));
1424 if (resInfo
->riCount
> 1)
1426 IP_ADDR_STRING
*pSrc
= resInfo
->DnsList
->Next
;
1427 IP_ADDR_STRING
*pTarget
= (struct _IP_ADDR_STRING
*)((char*)pFixedInfo
+ sizeof(FIXED_INFO
));
1429 pFixedInfo
->DnsServerList
.Next
= pTarget
;
1433 CopyMemory(pTarget
, pSrc
, sizeof(IP_ADDR_STRING
));
1435 if (resInfo
->riCount
> 1)
1437 pTarget
->Next
= (IP_ADDR_STRING
*)((char*)pTarget
+ sizeof(IP_ADDR_STRING
));
1438 pTarget
= pTarget
->Next
;
1443 pTarget
->Next
= NULL
;
1451 pFixedInfo
->DnsServerList
.Next
= NULL
;
1455 pFixedInfo
->NodeType
= HYBRID_NODETYPE
;
1456 regReturn
= RegOpenKeyExA(HKEY_LOCAL_MACHINE
,
1457 "SYSTEM\\CurrentControlSet\\Services\\VxD\\MSTCP", 0, KEY_READ
, &hKey
);
1458 if (regReturn
!= ERROR_SUCCESS
)
1459 regReturn
= RegOpenKeyExA(HKEY_LOCAL_MACHINE
,
1460 "SYSTEM\\CurrentControlSet\\Services\\NetBT\\Parameters", 0, KEY_READ
,
1462 if (regReturn
== ERROR_SUCCESS
)
1464 DWORD size
= sizeof(pFixedInfo
->ScopeId
);
1466 RegQueryValueExA(hKey
, "ScopeID", NULL
, NULL
, (PBYTE
)pFixedInfo
->ScopeId
, &size
);
1470 disposeResInfo( resInfo
);
1471 /* FIXME: can check whether routing's enabled in /proc/sys/net/ipv4/ip_forward
1472 I suppose could also check for a listener on port 53 to set EnableDns */
1474 TRACE("returning %ld\n", ret
);
1480 /******************************************************************
1481 * GetNumberOfInterfaces (IPHLPAPI.@)
1493 DWORD WINAPI
GetNumberOfInterfaces(PDWORD pdwNumIf
)
1497 TRACE("pdwNumIf %p\n", pdwNumIf
);
1499 ret
= ERROR_INVALID_PARAMETER
;
1501 *pdwNumIf
= getNumInterfaces();
1504 TRACE("returning %ld\n", ret
);
1509 /******************************************************************
1510 * GetOwnerModuleFromTcpEntry (IPHLPAPI.@)
1512 * Get data about the module that issued the context bind for a specific IPv4 TCP endpoint in a MIB table row
1515 * pTcpEntry [in] pointer to a MIB_TCPROW_OWNER_MODULE structure
1516 * Class [in] TCPIP_OWNER_MODULE_INFO_CLASS enumeration value
1517 * Buffer [out] pointer a buffer containing a TCPIP_OWNER_MODULE_BASIC_INFO structure with the owner module data.
1518 * pdwSize [in, out] estimated size of the structure returned in Buffer, in bytes
1522 * Failure: ERROR_INSUFFICIENT_BUFFER, ERROR_INVALID_PARAMETER, ERROR_NOT_ENOUGH_MEMORY
1523 * ERROR_NOT_FOUND or ERROR_PARTIAL_COPY
1526 * The type of data returned in Buffer is indicated by the value of the Class parameter.
1528 DWORD WINAPI
GetOwnerModuleFromTcpEntry( PMIB_TCPROW_OWNER_MODULE pTcpEntry
, TCPIP_OWNER_MODULE_INFO_CLASS Class
, PVOID Buffer
, PDWORD pdwSize
)
1530 DWORD ret
= NO_ERROR
;
1536 /******************************************************************
1537 * GetPerAdapterInfo (IPHLPAPI.@)
1543 * pPerAdapterInfo [In/Out]
1544 * pOutBufLen [In/Out]
1551 static void CreateNameServerListEnumNamesFunc( PWCHAR Interface
, PWCHAR Server
, PVOID Data
)
1553 IP_ADDR_STRING
*pNext
;
1554 PNAME_SERVER_LIST_CONTEXT Context
= (PNAME_SERVER_LIST_CONTEXT
)Data
;
1556 if (!Context
->NumServers
)
1558 if (Context
->uSizeAvailable
>= Context
->uSizeRequired
)
1560 WideCharToMultiByte(CP_ACP
, 0, Server
, -1, Context
->pData
->DnsServerList
.IpAddress
.String
, 16, NULL
, NULL
);
1561 Context
->pData
->DnsServerList
.IpAddress
.String
[15] = '\0';
1562 Context
->pLastAddr
= &Context
->pData
->DnsServerList
;
1567 Context
->uSizeRequired
+= sizeof(IP_ADDR_STRING
);
1568 if (Context
->uSizeAvailable
>= Context
->uSizeRequired
)
1570 pNext
= (IP_ADDR_STRING
*)(((char*)Context
->pLastAddr
) + sizeof(IP_ADDR_STRING
));
1571 WideCharToMultiByte(CP_ACP
, 0, Server
, -1, pNext
->IpAddress
.String
, 16, NULL
, NULL
);
1572 pNext
->IpAddress
.String
[15] = '\0';
1573 Context
->pLastAddr
->Next
= pNext
;
1574 Context
->pLastAddr
= pNext
;
1578 Context
->NumServers
++;
1581 DWORD WINAPI
GetPerAdapterInfo(ULONG IfIndex
, PIP_PER_ADAPTER_INFO pPerAdapterInfo
, PULONG pOutBufLen
)
1586 NAME_SERVER_LIST_CONTEXT Context
;
1587 WCHAR keyname
[200] = L
"SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Interfaces\\";
1590 return ERROR_INVALID_PARAMETER
;
1592 if (!pPerAdapterInfo
|| *pOutBufLen
< sizeof(IP_PER_ADAPTER_INFO
))
1594 *pOutBufLen
= sizeof(IP_PER_ADAPTER_INFO
);
1595 return ERROR_BUFFER_OVERFLOW
;
1598 ifName
= getInterfaceNameByIndex(IfIndex
);
1600 return ERROR_INVALID_PARAMETER
;
1602 MultiByteToWideChar(CP_ACP
, 0, ifName
, -1, &keyname
[62], sizeof(keyname
)/sizeof(WCHAR
) - 63);
1603 HeapFree(GetProcessHeap(), 0, (LPVOID
)ifName
);
1605 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE
, keyname
, 0, KEY_READ
, &hkey
) != ERROR_SUCCESS
)
1607 return ERROR_NOT_SUPPORTED
;
1609 Context
.NumServers
= 0;
1610 Context
.uSizeAvailable
= *pOutBufLen
;
1611 Context
.uSizeRequired
= sizeof(IP_PER_ADAPTER_INFO
);
1612 Context
.pData
= pPerAdapterInfo
;
1614 if (*pOutBufLen
>= sizeof(IP_PER_ADAPTER_INFO
))
1615 ZeroMemory(pPerAdapterInfo
, sizeof(IP_PER_ADAPTER_INFO
));
1617 EnumNameServers(hkey
, &keyname
[62], &Context
, CreateNameServerListEnumNamesFunc
);
1619 if (Context
.uSizeRequired
> Context
.uSizeAvailable
)
1621 *pOutBufLen
= Context
.uSizeRequired
;
1623 return ERROR_BUFFER_OVERFLOW
;
1626 if(RegQueryValueExW(hkey
, L
"DHCPNameServer", NULL
, NULL
, NULL
, &dwSize
) == ERROR_SUCCESS
)
1628 pPerAdapterInfo
->AutoconfigActive
= TRUE
;
1636 /******************************************************************
1637 * GetRTTAndHopCount (IPHLPAPI.@)
1642 * DestIpAddress [In]
1652 BOOL WINAPI
GetRTTAndHopCount(IPAddr DestIpAddress
, PULONG HopCount
, ULONG MaxHops
, PULONG RTT
)
1654 TRACE("DestIpAddress 0x%08lx, HopCount %p, MaxHops %ld, RTT %p\n",
1655 DestIpAddress
, HopCount
, MaxHops
, RTT
);
1661 /******************************************************************
1662 * GetTcpStatisticsEx (IPHLPAPI.@)
1675 DWORD WINAPI
GetTcpStatisticsEx(PMIB_TCPSTATS pStats
, DWORD dwFamily
)
1679 TRACE("pStats %p\n", pStats
);
1680 ret
= getTCPStats(pStats
, dwFamily
);
1681 TRACE("returning %ld\n", ret
);
1685 /******************************************************************
1686 * GetTcpStatistics (IPHLPAPI.@)
1698 DWORD WINAPI
GetTcpStatistics(PMIB_TCPSTATS pStats
)
1700 return GetTcpStatisticsEx(pStats
, PF_INET
);
1704 static int TcpTableSorter(const void *a
, const void *b
)
1709 PMIB_TCPROW rowA
= (PMIB_TCPROW
)a
, rowB
= (PMIB_TCPROW
)b
;
1711 ret
= rowA
->dwLocalAddr
- rowB
->dwLocalAddr
;
1713 ret
= rowA
->dwLocalPort
- rowB
->dwLocalPort
;
1715 ret
= rowA
->dwRemoteAddr
- rowB
->dwRemoteAddr
;
1717 ret
= rowA
->dwRemotePort
- rowB
->dwRemotePort
;
1727 /******************************************************************
1728 * GetTcpTable (IPHLPAPI.@)
1730 * Get the table of active TCP connections.
1733 * pTcpTable [Out] buffer for TCP connections table
1734 * pdwSize [In/Out] length of output buffer
1735 * bOrder [In] whether to order the table
1739 * Failure: error code from winerror.h
1742 * If pdwSize is less than required, the function will return
1743 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to
1744 * the required byte size.
1745 * If bOrder is true, the returned table will be sorted, first by
1746 * local address and port number, then by remote address and port
1749 DWORD WINAPI
GetTcpTable(PMIB_TCPTABLE pTcpTable
, PDWORD pdwSize
, BOOL bOrder
)
1751 DWORD ret
= NO_ERROR
;
1753 TRACE("pTcpTable %p, pdwSize %p, bOrder %d\n", pTcpTable
, pdwSize
,
1756 ret
= ERROR_INVALID_PARAMETER
;
1758 DWORD numEntries
= getNumTcpEntries();
1759 DWORD size
= sizeof(MIB_TCPTABLE
);
1762 size
+= (numEntries
- 1) * sizeof(MIB_TCPROW
);
1763 if (!pTcpTable
|| *pdwSize
< size
) {
1765 ret
= ERROR_INSUFFICIENT_BUFFER
;
1768 PMIB_TCPTABLE pTcpTable
= getTcpTable();
1771 size
= sizeof(MIB_TCPTABLE
);
1772 if (pTcpTable
->dwNumEntries
> 1)
1773 size
+= (pTcpTable
->dwNumEntries
- 1) * sizeof(MIB_TCPROW
);
1777 qsort(pTcpTable
->table
, pTcpTable
->dwNumEntries
,
1778 sizeof(MIB_TCPROW
), TcpTableSorter
);
1783 TRACE("returning %d\n", ret
);
1788 /******************************************************************
1789 * GetUdpStatisticsEx (IPHLPAPI.@)
1802 DWORD WINAPI
GetUdpStatisticsEx(PMIB_UDPSTATS pStats
, DWORD dwFamily
)
1806 TRACE("pStats %p\n", pStats
);
1807 ret
= getUDPStats(pStats
, dwFamily
);
1808 TRACE("returning %ld\n", ret
);
1812 /******************************************************************
1813 * GetUdpStatistics (IPHLPAPI.@)
1825 DWORD WINAPI
GetUdpStatistics(PMIB_UDPSTATS pStats
)
1827 return GetUdpStatisticsEx(pStats
, PF_INET
);
1831 static int UdpTableSorter(const void *a
, const void *b
)
1836 PMIB_UDPROW rowA
= (PMIB_UDPROW
)a
, rowB
= (PMIB_UDPROW
)b
;
1838 ret
= rowA
->dwLocalAddr
- rowB
->dwLocalAddr
;
1840 ret
= rowA
->dwLocalPort
- rowB
->dwLocalPort
;
1848 /******************************************************************
1849 * GetUdpTable (IPHLPAPI.@)
1854 * pUdpTable [In/Out]
1863 DWORD WINAPI
GetUdpTable(PMIB_UDPTABLE pUdpTable
, PDWORD pdwSize
, BOOL bOrder
)
1867 TRACE("pUdpTable %p, pdwSize %p, bOrder %ld\n", pUdpTable
, pdwSize
,
1870 ret
= ERROR_INVALID_PARAMETER
;
1872 DWORD numEntries
= getNumUdpEntries();
1873 ULONG size
= sizeof(MIB_UDPTABLE
) + (numEntries
- 1) * sizeof(MIB_UDPROW
);
1875 if (!pUdpTable
|| *pdwSize
< size
) {
1877 ret
= ERROR_INSUFFICIENT_BUFFER
;
1880 PMIB_UDPTABLE table
= getUdpTable();
1883 size
= sizeof(MIB_UDPTABLE
) + (table
->dwNumEntries
- 1) *
1885 if (*pdwSize
< size
) {
1887 ret
= ERROR_INSUFFICIENT_BUFFER
;
1890 memcpy(pUdpTable
, table
, size
);
1892 qsort(pUdpTable
->table
, pUdpTable
->dwNumEntries
,
1893 sizeof(MIB_UDPROW
), UdpTableSorter
);
1899 ret
= ERROR_OUTOFMEMORY
;
1902 TRACE("returning %ld\n", ret
);
1907 /******************************************************************
1908 * GetUniDirectionalAdapterInfo (IPHLPAPI.@)
1910 * This is a Win98-only function to get information on "unidirectional"
1911 * adapters. Since this is pretty nonsensical in other contexts, it
1912 * never returns anything.
1915 * pIPIfInfo [Out] buffer for adapter infos
1916 * dwOutBufLen [Out] length of the output buffer
1920 * Failure: error code from winerror.h
1923 * Stub, returns ERROR_NOT_SUPPORTED.
1925 DWORD WINAPI
GetUniDirectionalAdapterInfo(PIP_UNIDIRECTIONAL_ADAPTER_ADDRESS pIPIfInfo
, PULONG dwOutBufLen
)
1927 TRACE("pIPIfInfo %p, dwOutBufLen %p\n", pIPIfInfo
, dwOutBufLen
);
1928 /* a unidirectional adapter?? not bloody likely! */
1929 return ERROR_NOT_SUPPORTED
;
1933 /******************************************************************
1934 * IpReleaseAddress (IPHLPAPI.@)
1936 * Release an IP obtained through DHCP,
1939 * AdapterInfo [In] adapter to release IP address
1943 * Failure: error code from winerror.h
1946 DWORD WINAPI
IpReleaseAddress(PIP_ADAPTER_INDEX_MAP AdapterInfo
)
1948 DWORD Status
, Version
= 0;
1950 if (!AdapterInfo
|| !AdapterInfo
->Name
)
1951 return ERROR_INVALID_PARAMETER
;
1953 /* Maybe we should do this in DllMain */
1954 if (DhcpCApiInitialize(&Version
) != ERROR_SUCCESS
)
1955 return ERROR_PROC_NOT_FOUND
;
1957 if (DhcpReleaseIpAddressLease(AdapterInfo
->Index
))
1958 Status
= ERROR_SUCCESS
;
1960 Status
= ERROR_PROC_NOT_FOUND
;
1968 /******************************************************************
1969 * IpRenewAddress (IPHLPAPI.@)
1971 * Renew an IP obtained through DHCP.
1974 * AdapterInfo [In] adapter to renew IP address
1978 * Failure: error code from winerror.h
1980 DWORD WINAPI
IpRenewAddress(PIP_ADAPTER_INDEX_MAP AdapterInfo
)
1982 DWORD Status
, Version
= 0;
1984 if (!AdapterInfo
|| !AdapterInfo
->Name
)
1985 return ERROR_INVALID_PARAMETER
;
1987 /* Maybe we should do this in DllMain */
1988 if (DhcpCApiInitialize(&Version
) != ERROR_SUCCESS
)
1989 return ERROR_PROC_NOT_FOUND
;
1991 if (DhcpRenewIpAddressLease(AdapterInfo
->Index
))
1992 Status
= ERROR_SUCCESS
;
1994 Status
= ERROR_PROC_NOT_FOUND
;
2002 /******************************************************************
2003 * NotifyAddrChange (IPHLPAPI.@)
2005 * Notify caller whenever the ip-interface map is changed.
2008 * Handle [Out] handle usable in asynchronous notification
2009 * overlapped [In] overlapped structure that notifies the caller
2013 * Failure: error code from winerror.h
2016 * Stub, returns ERROR_NOT_SUPPORTED.
2018 DWORD WINAPI
NotifyAddrChange(PHANDLE Handle
, LPOVERLAPPED overlapped
)
2020 FIXME("(Handle %p, overlapped %p): stub\n", Handle
, overlapped
);
2021 return ERROR_NOT_SUPPORTED
;
2025 /******************************************************************
2026 * NotifyRouteChange (IPHLPAPI.@)
2028 * Notify caller whenever the ip routing table is changed.
2031 * Handle [Out] handle usable in asynchronous notification
2032 * overlapped [In] overlapped structure that notifies the caller
2036 * Failure: error code from winerror.h
2039 * Stub, returns ERROR_NOT_SUPPORTED.
2041 DWORD WINAPI
NotifyRouteChange(PHANDLE Handle
, LPOVERLAPPED overlapped
)
2043 FIXME("(Handle %p, overlapped %p): stub\n", Handle
, overlapped
);
2044 return ERROR_NOT_SUPPORTED
;
2048 /******************************************************************
2049 * SendARP (IPHLPAPI.@)
2051 * Send an ARP request.
2054 * DestIP [In] attempt to obtain this IP
2055 * SrcIP [In] optional sender IP address
2056 * pMacAddr [Out] buffer for the mac address
2057 * PhyAddrLen [In/Out] length of the output buffer
2061 * Failure: error code from winerror.h
2064 * Stub, returns ERROR_NOT_SUPPORTED.
2066 DWORD WINAPI
SendARP(IPAddr DestIP
, IPAddr SrcIP
, PULONG pMacAddr
, PULONG PhyAddrLen
)
2068 FIXME("(DestIP 0x%08x, SrcIP 0x%08x, pMacAddr %p, PhyAddrLen %p): stub\n",
2069 DestIP
, SrcIP
, pMacAddr
, PhyAddrLen
);
2070 return ERROR_NOT_SUPPORTED
;
2074 /******************************************************************
2075 * SetIfEntry (IPHLPAPI.@)
2077 * Set the administrative status of an interface.
2080 * pIfRow [In] dwAdminStatus member specifies the new status.
2084 * Failure: error code from winerror.h
2087 * Stub, returns ERROR_NOT_SUPPORTED.
2089 DWORD WINAPI
SetIfEntry(PMIB_IFROW pIfRow
)
2091 FIXME("(pIfRow %p): stub\n", pIfRow
);
2092 /* this is supposed to set an interface administratively up or down.
2093 Could do SIOCSIFFLAGS and set/clear IFF_UP, but, not sure I want to, and
2094 this sort of down is indistinguishable from other sorts of down (e.g. no
2096 return ERROR_NOT_SUPPORTED
;
2100 /******************************************************************
2101 * SetIpForwardEntry (IPHLPAPI.@)
2103 * Modify an existing route.
2106 * pRoute [In] route with the new information
2110 * Failure: error code from winerror.h
2113 DWORD WINAPI
SetIpForwardEntry(PMIB_IPFORWARDROW pRoute
)
2115 return setIpForwardEntry( pRoute
);
2119 /******************************************************************
2120 * SetIpNetEntry (IPHLPAPI.@)
2122 * Modify an existing ARP entry.
2125 * pArpEntry [In] ARP entry with the new information
2129 * Failure: error code from winerror.h
2131 DWORD WINAPI
SetIpNetEntry(PMIB_IPNETROW pArpEntry
)
2135 TCP_REQUEST_SET_INFORMATION_EX_ARP_ENTRY req
=
2136 TCP_REQUEST_SET_INFORMATION_INIT
;
2139 PMIB_IPNETROW arpBuff
;
2142 return ERROR_INVALID_PARAMETER
;
2144 if (!NT_SUCCESS(openTcpFile( &tcpFile
)))
2145 return ERROR_NOT_SUPPORTED
;
2147 if (!NT_SUCCESS(getNthIpEntity( tcpFile
, pArpEntry
->dwIndex
, &id
)))
2149 closeTcpFile(tcpFile
);
2150 return ERROR_INVALID_PARAMETER
;
2153 req
.Req
.ID
.toi_class
= INFO_CLASS_PROTOCOL
;
2154 req
.Req
.ID
.toi_type
= INFO_TYPE_PROVIDER
;
2155 req
.Req
.ID
.toi_id
= IP_MIB_ARPTABLE_ENTRY_ID
;
2156 req
.Req
.ID
.toi_entity
.tei_instance
= id
.tei_instance
;
2157 req
.Req
.ID
.toi_entity
.tei_entity
= AT_ENTITY
;
2158 req
.Req
.BufferSize
= sizeof(MIB_IPNETROW
);
2159 arpBuff
= (PMIB_IPNETROW
)&req
.Req
.Buffer
[0];
2161 RtlCopyMemory(arpBuff
, pArpEntry
, sizeof(MIB_IPNETROW
));
2163 status
= DeviceIoControl( tcpFile
,
2164 IOCTL_TCP_SET_INFORMATION_EX
,
2172 closeTcpFile(tcpFile
);
2177 return ERROR_INVALID_PARAMETER
;
2181 /******************************************************************
2182 * SetIpStatistics (IPHLPAPI.@)
2184 * Toggle IP forwarding and det the default TTL value.
2187 * pIpStats [In] IP statistics with the new information
2191 * Failure: error code from winerror.h
2194 * Stub, returns NO_ERROR.
2196 DWORD WINAPI
SetIpStatistics(PMIB_IPSTATS pIpStats
)
2198 FIXME("(pIpStats %p): stub\n", pIpStats
);
2203 /******************************************************************
2204 * SetIpTTL (IPHLPAPI.@)
2206 * Set the default TTL value.
2209 * nTTL [In] new TTL value
2213 * Failure: error code from winerror.h
2216 * Stub, returns NO_ERROR.
2218 DWORD WINAPI
SetIpTTL(UINT nTTL
)
2220 FIXME("(nTTL %d): stub\n", nTTL
);
2225 /******************************************************************
2226 * SetTcpEntry (IPHLPAPI.@)
2228 * Set the state of a TCP connection.
2231 * pTcpRow [In] specifies connection with new state
2235 * Failure: error code from winerror.h
2238 * Stub, returns NO_ERROR.
2240 DWORD WINAPI
SetTcpEntry(PMIB_TCPROW pTcpRow
)
2242 FIXME("(pTcpRow %p): stub\n", pTcpRow
);
2247 /******************************************************************
2248 * UnenableRouter (IPHLPAPI.@)
2250 * Decrement the IP-forwarding reference count. Turn off IP-forwarding
2251 * if it reaches zero.
2254 * pOverlapped [In/Out] should be the same as in EnableRouter()
2255 * lpdwEnableCount [Out] optional, receives reference count
2259 * Failure: error code from winerror.h
2262 * Stub, returns ERROR_NOT_SUPPORTED.
2264 DWORD WINAPI
UnenableRouter(OVERLAPPED
* pOverlapped
, LPDWORD lpdwEnableCount
)
2266 FIXME("(pOverlapped %p, lpdwEnableCount %p): stub\n", pOverlapped
,
2268 return ERROR_NOT_SUPPORTED
;
2274 DWORD WINAPI
GetIpErrorString(IP_STATUS ErrorCode
,PWCHAR Buffer
,PDWORD Size
)
2284 PIP_ADAPTER_ORDER_MAP WINAPI
GetAdapterOrderMap(VOID
)
2293 DWORD WINAPI
GetAdaptersAddresses(ULONG Family
,ULONG Flags
,PVOID Reserved
,PIP_ADAPTER_ADDRESSES pAdapterAddresses
,PULONG pOutBufLen
)
2295 InterfaceIndexTable
*indexTable
;
2298 ULONG ret
, requiredSize
= 0;
2299 PIP_ADAPTER_ADDRESSES currentAddress
;
2300 PUCHAR currentLocation
;
2303 if (!pOutBufLen
) return ERROR_INVALID_PARAMETER
;
2304 if (Reserved
) return ERROR_INVALID_PARAMETER
;
2306 indexTable
= getNonLoopbackInterfaceIndexTable(); //I think we want non-loopback here
2308 return ERROR_NOT_ENOUGH_MEMORY
;
2310 ret
= openTcpFile(&tcpFile
);
2311 if (!NT_SUCCESS(ret
))
2312 return ERROR_NO_DATA
;
2314 for (i
= indexTable
->numIndexes
; i
>= 0; i
--)
2316 if (NT_SUCCESS(getIPAddrEntryForIf(tcpFile
,
2318 indexTable
->indexes
[i
],
2321 /* The whole struct */
2322 requiredSize
+= sizeof(IP_ADAPTER_ADDRESSES
);
2325 if (!(Flags
& GAA_FLAG_SKIP_FRIENDLY_NAME
))
2326 requiredSize
+= strlen((char *)ifInfo
.if_info
.ent
.if_descr
) + 1; //FIXME
2329 requiredSize
+= strlen((char *)ifInfo
.if_info
.ent
.if_descr
) + 1;
2331 /* Unicast address */
2332 if (!(Flags
& GAA_FLAG_SKIP_UNICAST
))
2333 requiredSize
+= sizeof(IP_ADAPTER_UNICAST_ADDRESS
);
2335 /* FIXME: Implement multicast, anycast, and dns server stuff */
2337 /* FIXME: Implement dns suffix and description */
2338 requiredSize
+= 2 * sizeof(WCHAR
);
2340 /* We're only going to implement what's required for XP SP0 */
2344 if (*pOutBufLen
< requiredSize
)
2346 *pOutBufLen
= requiredSize
;
2347 closeTcpFile(tcpFile
);
2349 return ERROR_BUFFER_OVERFLOW
;
2352 RtlZeroMemory(pAdapterAddresses
, requiredSize
);
2354 /* Let's set up the pointers */
2355 currentAddress
= pAdapterAddresses
;
2356 for (i
= indexTable
->numIndexes
; i
>= 0; i
--)
2358 if (NT_SUCCESS(getIPAddrEntryForIf(tcpFile
,
2360 indexTable
->indexes
[i
],
2363 currentLocation
= (PUCHAR
)currentAddress
+ (ULONG_PTR
)sizeof(IP_ADAPTER_ADDRESSES
);
2365 /* FIXME: Friendly name */
2366 if (!(Flags
& GAA_FLAG_SKIP_FRIENDLY_NAME
))
2368 currentAddress
->FriendlyName
= (PVOID
)currentLocation
;
2369 currentLocation
+= sizeof(WCHAR
);
2373 currentAddress
->AdapterName
= (PVOID
)currentLocation
;
2374 currentLocation
+= strlen((char *)ifInfo
.if_info
.ent
.if_descr
) + 1;
2376 /* Unicast address */
2377 if (!(Flags
& GAA_FLAG_SKIP_UNICAST
))
2379 currentAddress
->FirstUnicastAddress
= (PVOID
)currentLocation
;
2380 currentLocation
+= sizeof(IP_ADAPTER_UNICAST_ADDRESS
);
2381 currentAddress
->FirstUnicastAddress
->Address
.lpSockaddr
= (PVOID
)currentLocation
;
2382 currentLocation
+= sizeof(struct sockaddr
);
2385 /* FIXME: Implement multicast, anycast, and dns server stuff */
2387 /* FIXME: Implement dns suffix and description */
2388 currentAddress
->DnsSuffix
= (PVOID
)currentLocation
;
2389 currentLocation
+= sizeof(WCHAR
);
2391 currentAddress
->Description
= (PVOID
)currentLocation
;
2392 currentLocation
+= sizeof(WCHAR
);
2394 currentAddress
->Next
= (PVOID
)currentLocation
;
2396 /* We're only going to implement what's required for XP SP0 */
2398 currentAddress
= currentAddress
->Next
;
2402 /* Terminate the last address correctly */
2404 currentAddress
->Next
= NULL
;
2406 /* Now again, for real this time */
2408 currentAddress
= pAdapterAddresses
;
2409 for (i
= indexTable
->numIndexes
; i
>= 0; i
--)
2411 if (NT_SUCCESS(getIPAddrEntryForIf(tcpFile
,
2413 indexTable
->indexes
[i
],
2416 /* Make sure we're not looping more than we hoped for */
2417 ASSERT(currentAddress
);
2419 /* Alignment information */
2420 currentAddress
->Length
= sizeof(IP_ADAPTER_ADDRESSES
);
2421 currentAddress
->IfIndex
= indexTable
->indexes
[i
];
2424 strcpy(currentAddress
->AdapterName
, (char *)ifInfo
.if_info
.ent
.if_descr
);
2426 if (!(Flags
& GAA_FLAG_SKIP_UNICAST
))
2428 currentAddress
->FirstUnicastAddress
->Length
= sizeof(IP_ADAPTER_UNICAST_ADDRESS
);
2429 currentAddress
->FirstUnicastAddress
->Flags
= 0; //FIXME
2430 currentAddress
->FirstUnicastAddress
->Next
= NULL
; //FIXME: Support more than one address per adapter
2431 currentAddress
->FirstUnicastAddress
->Address
.lpSockaddr
->sa_family
= AF_INET
;
2432 memcpy(currentAddress
->FirstUnicastAddress
->Address
.lpSockaddr
->sa_data
,
2433 &ifInfo
.ip_addr
.iae_addr
,
2434 sizeof(ifInfo
.ip_addr
.iae_addr
));
2435 currentAddress
->FirstUnicastAddress
->Address
.iSockaddrLength
= sizeof(ifInfo
.ip_addr
.iae_addr
) + sizeof(USHORT
);
2436 currentAddress
->FirstUnicastAddress
->PrefixOrigin
= IpPrefixOriginOther
; //FIXME
2437 currentAddress
->FirstUnicastAddress
->SuffixOrigin
= IpPrefixOriginOther
; //FIXME
2438 currentAddress
->FirstUnicastAddress
->DadState
= IpDadStatePreferred
; //FIXME
2439 currentAddress
->FirstUnicastAddress
->ValidLifetime
= 0xFFFFFFFF; //FIXME
2440 currentAddress
->FirstUnicastAddress
->PreferredLifetime
= 0xFFFFFFFF; //FIXME
2441 currentAddress
->FirstUnicastAddress
->LeaseLifetime
= 0xFFFFFFFF; //FIXME
2444 /* FIXME: Implement multicast, anycast, and dns server stuff */
2445 currentAddress
->FirstAnycastAddress
= NULL
;
2446 currentAddress
->FirstMulticastAddress
= NULL
;
2447 currentAddress
->FirstDnsServerAddress
= NULL
;
2449 /* FIXME: Implement dns suffix, description, and friendly name */
2450 currentAddress
->DnsSuffix
[0] = UNICODE_NULL
;
2451 currentAddress
->Description
[0] = UNICODE_NULL
;
2452 currentAddress
->FriendlyName
[0] = UNICODE_NULL
;
2454 /* Physical Address */
2455 memcpy(currentAddress
->PhysicalAddress
, ifInfo
.if_info
.ent
.if_physaddr
, ifInfo
.if_info
.ent
.if_physaddrlen
);
2456 currentAddress
->PhysicalAddressLength
= ifInfo
.if_info
.ent
.if_physaddrlen
;
2459 currentAddress
->Flags
= 0; //FIXME
2462 currentAddress
->Mtu
= ifInfo
.if_info
.ent
.if_mtu
;
2464 /* Interface type */
2465 currentAddress
->IfType
= ifInfo
.if_info
.ent
.if_type
;
2467 /* Operational status */
2468 currentAddress
->OperStatus
= ifInfo
.if_info
.ent
.if_operstatus
;
2470 /* We're only going to implement what's required for XP SP0 */
2472 /* Move to the next address */
2473 currentAddress
= currentAddress
->Next
;
2477 closeTcpFile(tcpFile
);
2486 BOOL WINAPI
CancelIPChangeNotify(LPOVERLAPPED notifyOverlapped
)
2495 DWORD WINAPI
GetBestInterfaceEx(struct sockaddr
*pDestAddr
,PDWORD pdwBestIfIndex
)
2504 DWORD WINAPI
NhpAllocateAndGetInterfaceInfoFromStack(IP_INTERFACE_NAME_INFO
**ppTable
,PDWORD pdwCount
,BOOL bOrder
,HANDLE hHeap
,DWORD dwFlags
)
2513 DWORD WINAPI
GetIcmpStatisticsEx(PMIB_ICMP_EX pStats
,DWORD dwFamily
)
2519 /******************************************************************
2520 * GetIfTable2 (IPHLPAPI.@)
2526 NETIOAPI_API WINAPI
GetIfTable2(PMIB_IF_TABLE2
*pIfTable
)
2529 return ERROR_NOT_SUPPORTED
;
2532 /******************************************************************
2533 * GetIfEntry2 (IPHLPAPI.@)
2538 NETIOAPI_API WINAPI
GetIfEntry2(IN OUT PMIB_IF_ROW2 pIfRow
)
2540 TRACE("pIfRow %p\n", pIfRow
);
2542 return ERROR_INVALID_PARAMETER
;
2545 return ERROR_NOT_SUPPORTED
;
2549 SetIpForwardEntryToStack(PMIB_IPFORWARDROW pRoute
)
2551 FIXME("SetIpForwardEntryToStack() stub\n");
2556 NhGetInterfaceNameFromDeviceGuid(DWORD dwUnknown1
,
2562 FIXME("NhGetInterfaceNameFromDeviceGuid() stub\n");