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
24 #include "iphlpapi_private.h"
26 WINE_DEFAULT_DEBUG_CHANNEL(iphlpapi
);
28 typedef struct _NAME_SERVER_LIST_CONTEXT
{
31 PIP_PER_ADAPTER_INFO pData
;
33 IP_ADDR_STRING
*pLastAddr
;
34 } NAME_SERVER_LIST_CONTEXT
, *PNAME_SERVER_LIST_CONTEXT
;
36 BOOL WINAPI
DllMain (HINSTANCE hinstDLL
, DWORD fdwReason
, LPVOID lpvReserved
)
39 case DLL_PROCESS_ATTACH
:
40 DisableThreadLibraryCalls( hinstDLL
);
44 case DLL_PROCESS_DETACH
:
51 /******************************************************************
52 * AddIPAddress (IPHLPAPI.@)
61 * NTEInstance [In/Out]
68 DWORD WINAPI
AddIPAddress(IPAddr Address
, IPMask Netmask
, DWORD IfIndex
, PULONG NteContext
, PULONG NteInstance
)
70 return RtlNtStatusToDosError(addIPAddress(Address
, Netmask
, IfIndex
, NteContext
, NteInstance
));
73 DWORD
getInterfaceGatewayByIndex(DWORD index
)
75 DWORD ndx
, retVal
= 0, numRoutes
= getNumRoutes();
76 RouteTable
*table
= getRouteTable();
79 for (ndx
= 0; ndx
< numRoutes
; ndx
++)
81 if ((table
->routes
[ndx
].ifIndex
== (index
)) && (table
->routes
[ndx
].dest
== 0))
82 retVal
= table
->routes
[ndx
].gateway
;
84 HeapFree(GetProcessHeap(), 0, table
);
88 /******************************************************************
89 * AllocateAndGetIfTableFromStack (IPHLPAPI.@)
94 * ppIfTable [Out] -- pointer into which the MIB_IFTABLE is
95 * allocated and returned.
96 * bOrder [In] -- passed to GetIfTable to order the table
97 * heap [In] -- heap from which the table is allocated
98 * flags [In] -- flags to HeapAlloc
100 * RETURNS -- ERROR_INVALID_PARAMETER if ppIfTable is NULL, whatever
101 * GetIfTable returns otherwise
104 DWORD WINAPI
AllocateAndGetIfTableFromStack(PMIB_IFTABLE
*ppIfTable
,
105 BOOL bOrder
, HANDLE heap
, DWORD flags
)
109 TRACE("ppIfTable %p, bOrder %ld, heap 0x%08lx, flags 0x%08lx\n", ppIfTable
,
110 (DWORD
)bOrder
, (DWORD
)heap
, flags
);
112 ret
= ERROR_INVALID_PARAMETER
;
116 ret
= GetIfTable(*ppIfTable
, &dwSize
, bOrder
);
117 if (ret
== ERROR_INSUFFICIENT_BUFFER
) {
118 *ppIfTable
= (PMIB_IFTABLE
)HeapAlloc(heap
, flags
, dwSize
);
119 ret
= GetIfTable(*ppIfTable
, &dwSize
, bOrder
);
122 TRACE("returning %ld\n", ret
);
127 /******************************************************************
128 * AllocateAndGetIpAddrTableFromStack (IPHLPAPI.@)
133 * ppIpAddrTable [Out]
134 * bOrder [In] -- passed to GetIpAddrTable to order the table
135 * heap [In] -- heap from which the table is allocated
136 * flags [In] -- flags to HeapAlloc
143 DWORD WINAPI
AllocateAndGetIpAddrTableFromStack(PMIB_IPADDRTABLE
*ppIpAddrTable
,
144 BOOL bOrder
, HANDLE heap
, DWORD flags
)
148 TRACE("ppIpAddrTable %p, bOrder %ld, heap 0x%08lx, flags 0x%08lx\n",
149 ppIpAddrTable
, (DWORD
)bOrder
, (DWORD
)heap
, flags
);
151 ret
= ERROR_INVALID_PARAMETER
;
155 ret
= GetIpAddrTable(*ppIpAddrTable
, &dwSize
, bOrder
);
156 if (ret
== ERROR_INSUFFICIENT_BUFFER
) {
157 *ppIpAddrTable
= (PMIB_IPADDRTABLE
)HeapAlloc(heap
, flags
, dwSize
);
158 ret
= GetIpAddrTable(*ppIpAddrTable
, &dwSize
, bOrder
);
161 TRACE("returning %ld\n", ret
);
166 /******************************************************************
167 * AllocateAndGetIpForwardTableFromStack (IPHLPAPI.@)
170 * ppIpForwardTable [Out] -- pointer into which the MIB_IPFORWARDTABLE is
171 * allocated and returned.
172 * bOrder [In] -- passed to GetIfTable to order the table
173 * heap [In] -- heap from which the table is allocated
174 * flags [In] -- flags to HeapAlloc
176 * RETURNS -- ERROR_INVALID_PARAMETER if ppIfTable is NULL, whatever
177 * GetIpForwardTable returns otherwise
180 DWORD WINAPI
AllocateAndGetIpForwardTableFromStack(PMIB_IPFORWARDTABLE
*
181 ppIpForwardTable
, BOOL bOrder
, HANDLE heap
, DWORD flags
)
185 TRACE("ppIpForwardTable %p, bOrder %ld, heap 0x%08lx, flags 0x%08lx\n",
186 ppIpForwardTable
, (DWORD
)bOrder
, (DWORD
)heap
, flags
);
187 if (!ppIpForwardTable
)
188 ret
= ERROR_INVALID_PARAMETER
;
192 ret
= GetIpForwardTable(*ppIpForwardTable
, &dwSize
, bOrder
);
193 if (ret
== ERROR_INSUFFICIENT_BUFFER
) {
194 *ppIpForwardTable
= (PMIB_IPFORWARDTABLE
)HeapAlloc(heap
, flags
, dwSize
);
195 ret
= GetIpForwardTable(*ppIpForwardTable
, &dwSize
, bOrder
);
198 TRACE("returning %ld\n", ret
);
203 /******************************************************************
204 * AllocateAndGetIpNetTableFromStack (IPHLPAPI.@)
210 * bOrder [In] -- passed to GetIpNetTable to order the table
211 * heap [In] -- heap from which the table is allocated
212 * flags [In] -- flags to HeapAlloc
219 DWORD WINAPI
AllocateAndGetIpNetTableFromStack(PMIB_IPNETTABLE
*ppIpNetTable
,
220 BOOL bOrder
, HANDLE heap
, DWORD flags
)
224 TRACE("ppIpNetTable %p, bOrder %ld, heap 0x%08lx, flags 0x%08lx\n",
225 ppIpNetTable
, (DWORD
)bOrder
, (DWORD
)heap
, flags
);
227 ret
= ERROR_INVALID_PARAMETER
;
231 ret
= GetIpNetTable(*ppIpNetTable
, &dwSize
, bOrder
);
232 if (ret
== ERROR_INSUFFICIENT_BUFFER
) {
233 *ppIpNetTable
= (PMIB_IPNETTABLE
)HeapAlloc(heap
, flags
, dwSize
);
234 ret
= GetIpNetTable(*ppIpNetTable
, &dwSize
, bOrder
);
237 TRACE("returning %ld\n", ret
);
242 /******************************************************************
243 * AllocateAndGetTcpTableFromStack (IPHLPAPI.@)
249 * bOrder [In] -- passed to GetTcpTable to order the table
250 * heap [In] -- heap from which the table is allocated
251 * flags [In] -- flags to HeapAlloc
258 DWORD WINAPI
AllocateAndGetTcpTableFromStack(PMIB_TCPTABLE
*ppTcpTable
,
259 BOOL bOrder
, HANDLE heap
, DWORD flags
)
263 TRACE("ppTcpTable %p, bOrder %ld, heap 0x%08lx, flags 0x%08lx\n",
264 ppTcpTable
, (DWORD
)bOrder
, (DWORD
)heap
, flags
);
266 ret
= ERROR_INVALID_PARAMETER
;
270 ret
= GetTcpTable(*ppTcpTable
, &dwSize
, bOrder
);
271 if (ret
== ERROR_INSUFFICIENT_BUFFER
) {
272 *ppTcpTable
= (PMIB_TCPTABLE
)HeapAlloc(heap
, flags
, dwSize
);
273 ret
= GetTcpTable(*ppTcpTable
, &dwSize
, bOrder
);
276 TRACE("returning %ld\n", ret
);
281 /******************************************************************
282 * AllocateAndGetUdpTableFromStack (IPHLPAPI.@)
288 * bOrder [In] -- passed to GetUdpTable to order the table
289 * heap [In] -- heap from which the table is allocated
290 * flags [In] -- flags to HeapAlloc
297 DWORD WINAPI
AllocateAndGetUdpTableFromStack(PMIB_UDPTABLE
*ppUdpTable
,
298 BOOL bOrder
, HANDLE heap
, DWORD flags
)
302 TRACE("ppUdpTable %p, bOrder %ld, heap 0x%08lx, flags 0x%08lx\n",
303 ppUdpTable
, (DWORD
)bOrder
, (DWORD
)heap
, flags
);
305 ret
= ERROR_INVALID_PARAMETER
;
309 ret
= GetUdpTable(*ppUdpTable
, &dwSize
, bOrder
);
310 if (ret
== ERROR_INSUFFICIENT_BUFFER
) {
311 *ppUdpTable
= (PMIB_UDPTABLE
)HeapAlloc(heap
, flags
, dwSize
);
312 ret
= GetUdpTable(*ppUdpTable
, &dwSize
, bOrder
);
315 TRACE("returning %ld\n", ret
);
320 /******************************************************************
321 * CreateIpForwardEntry (IPHLPAPI.@)
333 DWORD WINAPI
CreateIpForwardEntry(PMIB_IPFORWARDROW pRoute
)
335 return createIpForwardEntry( pRoute
);
339 /******************************************************************
340 * CreateIpNetEntry (IPHLPAPI.@)
352 DWORD WINAPI
CreateIpNetEntry(PMIB_IPNETROW pArpEntry
)
354 TRACE("pArpEntry %p\n", pArpEntry
);
355 /* could use SIOCSARP on systems that support it, not sure I want to */
361 /******************************************************************
362 * CreateProxyArpEntry (IPHLPAPI.@)
376 DWORD WINAPI
CreateProxyArpEntry(DWORD dwAddress
, DWORD dwMask
, DWORD dwIfIndex
)
378 TRACE("dwAddress 0x%08lx, dwMask 0x%08lx, dwIfIndex 0x%08lx\n", dwAddress
,
381 /* marking Win2K+ functions not supported */
382 return ERROR_NOT_SUPPORTED
;
386 /******************************************************************
387 * DeleteIPAddress (IPHLPAPI.@)
399 DWORD WINAPI
DeleteIPAddress(ULONG NTEContext
)
401 TRACE("NTEContext %ld\n", NTEContext
);
402 return RtlNtStatusToDosError(deleteIpAddress(NTEContext
));
406 /******************************************************************
407 * DeleteIpForwardEntry (IPHLPAPI.@)
419 DWORD WINAPI
DeleteIpForwardEntry(PMIB_IPFORWARDROW pRoute
)
421 return deleteIpForwardEntry( pRoute
);
425 /******************************************************************
426 * DeleteIpNetEntry (IPHLPAPI.@)
438 DWORD WINAPI
DeleteIpNetEntry(PMIB_IPNETROW pArpEntry
)
440 TRACE("pArpEntry %p\n", pArpEntry
);
441 /* could use SIOCDARP on systems that support it, not sure I want to */
447 /******************************************************************
448 * DeleteProxyArpEntry (IPHLPAPI.@)
462 DWORD WINAPI
DeleteProxyArpEntry(DWORD dwAddress
, DWORD dwMask
, DWORD dwIfIndex
)
464 TRACE("dwAddress 0x%08lx, dwMask 0x%08lx, dwIfIndex 0x%08lx\n", dwAddress
,
467 /* marking Win2K+ functions not supported */
468 return ERROR_NOT_SUPPORTED
;
471 /******************************************************************
472 * EnableRouter (IPHLPAPI.@)
478 * pOverlapped [In/Out]
485 DWORD WINAPI
EnableRouter(HANDLE
* pHandle
, OVERLAPPED
* pOverlapped
)
487 TRACE("pHandle %p, pOverlapped %p\n", pHandle
, pOverlapped
);
489 /* could echo "1" > /proc/net/sys/net/ipv4/ip_forward, not sure I want to
490 could map EACCESS to ERROR_ACCESS_DENIED, I suppose
491 marking Win2K+ functions not supported */
492 return ERROR_NOT_SUPPORTED
;
496 /******************************************************************
497 * FlushIpNetTable (IPHLPAPI.@)
509 DWORD WINAPI
FlushIpNetTable(DWORD dwIfIndex
)
511 TRACE("dwIfIndex 0x%08lx\n", dwIfIndex
);
513 /* this flushes the arp cache of the given index
514 marking Win2K+ functions not supported */
515 return ERROR_NOT_SUPPORTED
;
519 /******************************************************************
520 * GetAdapterIndex (IPHLPAPI.@)
525 * AdapterName [In/Out]
533 DWORD WINAPI
GetAdapterIndex(LPWSTR AdapterName
, PULONG IfIndex
)
535 TRACE("AdapterName %p, IfIndex %p\n", AdapterName
, IfIndex
);
537 /* marking Win2K+ functions not supported */
538 return ERROR_NOT_SUPPORTED
;
542 /******************************************************************
543 * GetAdaptersInfo (IPHLPAPI.@)
548 * pAdapterInfo [In/Out]
549 * pOutBufLen [In/Out]
556 DWORD WINAPI
GetAdaptersInfo(PIP_ADAPTER_INFO pAdapterInfo
, PULONG pOutBufLen
)
562 TRACE("pAdapterInfo %p, pOutBufLen %p\n", pAdapterInfo
, pOutBufLen
);
564 ret
= ERROR_INVALID_PARAMETER
;
566 DWORD numNonLoopbackInterfaces
= getNumNonLoopbackInterfaces();
568 if (numNonLoopbackInterfaces
> 0) {
569 /* this calculation assumes only one address in the IP_ADDR_STRING lists.
570 that's okay, because:
571 - we don't get multiple addresses per adapter anyway
572 - we don't know about per-adapter gateways
573 - DHCP and WINS servers can have max one entry per list */
574 ULONG size
= sizeof(IP_ADAPTER_INFO
) * numNonLoopbackInterfaces
;
576 if (!pAdapterInfo
|| *pOutBufLen
< size
) {
578 ret
= ERROR_BUFFER_OVERFLOW
;
581 InterfaceIndexTable
*table
= getNonLoopbackInterfaceIndexTable();
584 size
= sizeof(IP_ADAPTER_INFO
) * table
->numIndexes
;
585 if (*pOutBufLen
< size
) {
587 ret
= ERROR_INSUFFICIENT_BUFFER
;
592 BOOL winsEnabled
= FALSE
;
593 IP_ADDRESS_STRING primaryWINS
, secondaryWINS
;
595 memset(pAdapterInfo
, 0, size
);
596 if (RegOpenKeyExA(HKEY_LOCAL_MACHINE
,
597 "Software\\Wine\\Wine\\Config\\Network", 0, KEY_READ
,
598 &hKey
) == ERROR_SUCCESS
) {
599 DWORD size
= sizeof(primaryWINS
.String
);
602 RegQueryValueExA(hKey
, "WinsServer", NULL
, NULL
,
603 (PBYTE
)primaryWINS
.String
, &size
);
604 addr
= inet_addr(primaryWINS
.String
);
605 if (addr
!= INADDR_NONE
&& addr
!= INADDR_ANY
)
607 size
= sizeof(secondaryWINS
.String
);
608 RegQueryValueExA(hKey
, "BackupWinsServer", NULL
, NULL
,
609 (PBYTE
)secondaryWINS
.String
, &size
);
610 addr
= inet_addr(secondaryWINS
.String
);
611 if (addr
!= INADDR_NONE
&& addr
!= INADDR_ANY
)
615 TRACE("num of index is %lu\n", table
->numIndexes
);
616 for (ndx
= 0; ndx
< table
->numIndexes
; ndx
++) {
617 PIP_ADAPTER_INFO ptr
= &pAdapterInfo
[ndx
];
618 DWORD addrLen
= sizeof(ptr
->Address
), type
;
620 getInterfaceNameByIndex(table
->indexes
[ndx
]);
622 ret
= ERROR_OUTOFMEMORY
;
626 /* on Win98 this is left empty, but whatever */
627 strncpy(ptr
->AdapterName
,ifname
,sizeof(ptr
->AdapterName
));
628 consumeInterfaceName(ifname
);
629 ptr
->AdapterName
[MAX_ADAPTER_NAME_LENGTH
] = '\0';
630 getInterfacePhysicalByIndex(table
->indexes
[ndx
], &addrLen
,
631 ptr
->Address
, &type
);
632 /* MS defines address length and type as UINT in some places and
633 DWORD in others, **sigh**. Don't want to assume that PUINT and
634 PDWORD are equiv (64-bit?) */
635 ptr
->AddressLength
= addrLen
;
637 ptr
->Index
= table
->indexes
[ndx
];
638 toIPAddressString(getInterfaceIPAddrByIndex(table
->indexes
[ndx
]),
639 ptr
->IpAddressList
.IpAddress
.String
);
640 toIPAddressString(getInterfaceMaskByIndex(table
->indexes
[ndx
]),
641 ptr
->IpAddressList
.IpMask
.String
);
642 ptr
->IpAddressList
.Context
= ptr
->Index
;
643 toIPAddressString(getInterfaceGatewayByIndex(table
->indexes
[ndx
]),
644 ptr
->GatewayList
.IpAddress
.String
);
645 getDhcpInfoForAdapter(table
->indexes
[ndx
], &dhcpEnabled
,
646 &dhcpServer
, &ptr
->LeaseObtained
,
648 ptr
->DhcpEnabled
= (DWORD
) dhcpEnabled
;
649 toIPAddressString(dhcpServer
,
650 ptr
->DhcpServer
.IpAddress
.String
);
652 ptr
->HaveWins
= TRUE
;
653 memcpy(ptr
->PrimaryWinsServer
.IpAddress
.String
,
654 primaryWINS
.String
, sizeof(primaryWINS
.String
));
655 memcpy(ptr
->SecondaryWinsServer
.IpAddress
.String
,
656 secondaryWINS
.String
, sizeof(secondaryWINS
.String
));
658 if (ndx
< table
->numIndexes
- 1)
659 ptr
->Next
= &pAdapterInfo
[ndx
+ 1];
668 ret
= ERROR_OUTOFMEMORY
;
674 TRACE("returning %ld\n", ret
);
679 /******************************************************************
680 * GetBestInterface (IPHLPAPI.@)
686 * pdwBestIfIndex [In/Out]
693 DWORD WINAPI
GetBestInterface(IPAddr dwDestAddr
, PDWORD pdwBestIfIndex
)
697 TRACE("dwDestAddr 0x%08lx, pdwBestIfIndex %p\n", dwDestAddr
, pdwBestIfIndex
);
699 ret
= ERROR_INVALID_PARAMETER
;
701 MIB_IPFORWARDROW ipRow
;
703 ret
= GetBestRoute(dwDestAddr
, 0, &ipRow
);
704 if (ret
== ERROR_SUCCESS
)
705 *pdwBestIfIndex
= ipRow
.dwForwardIfIndex
;
707 TRACE("returning %ld\n", ret
);
712 /******************************************************************
713 * GetBestRoute (IPHLPAPI.@)
727 DWORD WINAPI
GetBestRoute(DWORD dwDestAddr
, DWORD dwSourceAddr
, PMIB_IPFORWARDROW pBestRoute
)
729 PMIB_IPFORWARDTABLE table
;
732 TRACE("dwDestAddr 0x%08lx, dwSourceAddr 0x%08lx, pBestRoute %p\n", dwDestAddr
,
733 dwSourceAddr
, pBestRoute
);
735 return ERROR_INVALID_PARAMETER
;
737 AllocateAndGetIpForwardTableFromStack(&table
, FALSE
, GetProcessHeap(), 0);
739 DWORD ndx
, matchedBits
, matchedNdx
= 0;
741 for (ndx
= 0, matchedBits
= 0; ndx
< table
->dwNumEntries
; ndx
++) {
742 if ((dwDestAddr
& table
->table
[ndx
].dwForwardMask
) ==
743 (table
->table
[ndx
].dwForwardDest
& table
->table
[ndx
].dwForwardMask
)) {
744 DWORD numShifts
, mask
;
746 for (numShifts
= 0, mask
= table
->table
[ndx
].dwForwardMask
;
747 mask
&& !(mask
& 1); mask
>>= 1, numShifts
++)
749 if (numShifts
> matchedBits
) {
750 matchedBits
= numShifts
;
755 memcpy(pBestRoute
, &table
->table
[matchedNdx
], sizeof(MIB_IPFORWARDROW
));
756 HeapFree(GetProcessHeap(), 0, table
);
760 ret
= ERROR_OUTOFMEMORY
;
761 TRACE("returning %ld\n", ret
);
765 /******************************************************************
766 * GetExtendedTcpTable (IPHLPAPI.@)
768 * Get the table of TCP endpoints available to the application.
771 * pTcpTable [Out] table struct with the filtered TCP endpoints available to application
772 * pdwSize [In/Out] estimated size of the structure returned in pTcpTable, in bytes
773 * bOrder [In] whether to order the table
774 * ulAf [in] version of IP used by the TCP endpoints
775 * TableClass [in] type of the TCP table structure from TCP_TABLE_CLASS
776 * Reserved [in] reserved - this value must be zero
780 * Failure: either ERROR_INSUFFICIENT_BUFFER or ERROR_INVALID_PARAMETER
785 DWORD WINAPI
GetExtendedTcpTable(PVOID pTcpTable
, PDWORD pdwSize
, BOOL bOrder
, ULONG ulAf
, TCP_TABLE_CLASS TableClass
, ULONG Reserved
)
787 DWORD ret
= NO_ERROR
;
789 if (TableClass
== TCP_TABLE_OWNER_PID_ALL
) {
791 *pdwSize
= sizeof(MIB_TCPTABLE_OWNER_PID
);
792 return ERROR_INSUFFICIENT_BUFFER
;
794 ZeroMemory(pTcpTable
, sizeof(MIB_TCPTABLE_OWNER_PID
));
805 /******************************************************************
806 * GetFriendlyIfIndex (IPHLPAPI.@)
818 DWORD WINAPI
GetFriendlyIfIndex(DWORD IfIndex
)
820 /* windows doesn't validate these, either, just makes sure the top byte is
821 cleared. I assume my ifenum module never gives an index with the top
823 TRACE("returning %ld\n", IfIndex
);
828 /******************************************************************
829 * GetIcmpStatistics (IPHLPAPI.@)
841 DWORD WINAPI
GetIcmpStatistics(PMIB_ICMP pStats
)
845 TRACE("pStats %p\n", pStats
);
846 ret
= getICMPStats(pStats
);
847 TRACE("returning %ld\n", ret
);
852 /******************************************************************
853 * GetIfEntry (IPHLPAPI.@)
865 DWORD WINAPI
GetIfEntry(PMIB_IFROW pIfRow
)
870 TRACE("pIfRow %p\n", pIfRow
);
872 return ERROR_INVALID_PARAMETER
;
874 name
= getInterfaceNameByIndex(pIfRow
->dwIndex
);
876 ret
= getInterfaceEntryByIndex(pIfRow
->dwIndex
, pIfRow
);
878 ret
= getInterfaceStatsByName(name
, pIfRow
);
879 consumeInterfaceName(name
);
882 ret
= ERROR_INVALID_DATA
;
883 TRACE("returning %ld\n", ret
);
888 static int IfTableSorter(const void *a
, const void *b
)
893 ret
= ((PMIB_IFROW
)a
)->dwIndex
- ((PMIB_IFROW
)b
)->dwIndex
;
900 /******************************************************************
901 * GetIfTable (IPHLPAPI.@)
915 DWORD WINAPI
GetIfTable(PMIB_IFTABLE pIfTable
, PULONG pdwSize
, BOOL bOrder
)
919 TRACE("pIfTable %p, pdwSize %p, bOrder %ld\n", pdwSize
, pdwSize
,
922 ret
= ERROR_INVALID_PARAMETER
;
924 DWORD numInterfaces
= getNumInterfaces();
926 TRACE("GetIfTable: numInterfaces = %d\n", (int)numInterfaces
);
927 size
= sizeof(MIB_IFTABLE
) + (numInterfaces
- 1) * sizeof(MIB_IFROW
);
929 if (!pIfTable
|| *pdwSize
< size
) {
931 ret
= ERROR_INSUFFICIENT_BUFFER
;
934 InterfaceIndexTable
*table
= getInterfaceIndexTable();
937 size
= sizeof(MIB_IFTABLE
) + (table
->numIndexes
- 1) *
939 if (*pdwSize
< size
) {
941 ret
= ERROR_INSUFFICIENT_BUFFER
;
946 pIfTable
->dwNumEntries
= 0;
947 for (ndx
= 0; ndx
< table
->numIndexes
; ndx
++) {
948 pIfTable
->table
[ndx
].dwIndex
= table
->indexes
[ndx
];
949 GetIfEntry(&pIfTable
->table
[ndx
]);
950 pIfTable
->dwNumEntries
++;
953 qsort(pIfTable
->table
, pIfTable
->dwNumEntries
, sizeof(MIB_IFROW
),
960 ret
= ERROR_OUTOFMEMORY
;
963 TRACE("returning %ld\n", ret
);
968 /******************************************************************
969 * GetInterfaceInfo (IPHLPAPI.@)
975 * dwOutBufLen [In/Out]
982 DWORD WINAPI
GetInterfaceInfo(PIP_INTERFACE_INFO pIfTable
, PULONG dwOutBufLen
)
986 TRACE("pIfTable %p, dwOutBufLen %p\n", pIfTable
, dwOutBufLen
);
988 ret
= ERROR_INVALID_PARAMETER
;
990 DWORD numNonLoopbackInterfaces
= getNumNonLoopbackInterfaces();
992 TRACE("numNonLoopbackInterfaces == 0x%x\n", numNonLoopbackInterfaces
);
993 size
= sizeof(IP_INTERFACE_INFO
) + (numNonLoopbackInterfaces
) *
994 sizeof(IP_ADAPTER_INDEX_MAP
);
996 if (!pIfTable
|| *dwOutBufLen
< size
) {
998 ret
= ERROR_INSUFFICIENT_BUFFER
;
1001 InterfaceIndexTable
*table
= getNonLoopbackInterfaceIndexTable();
1004 TRACE("table->numIndexes == 0x%x\n", table
->numIndexes
);
1005 size
= sizeof(IP_INTERFACE_INFO
) + (table
->numIndexes
) *
1006 sizeof(IP_ADAPTER_INDEX_MAP
);
1007 if (*dwOutBufLen
< size
) {
1008 *dwOutBufLen
= size
;
1009 ret
= ERROR_INSUFFICIENT_BUFFER
;
1014 pIfTable
->NumAdapters
= 0;
1015 for (ndx
= 0; ndx
< table
->numIndexes
; ndx
++) {
1016 const char *walker
, *name
;
1019 pIfTable
->Adapter
[ndx
].Index
= table
->indexes
[ndx
];
1020 name
= getInterfaceNameByIndex(table
->indexes
[ndx
]);
1021 for (walker
= name
, assigner
= pIfTable
->Adapter
[ndx
].Name
;
1022 walker
&& *walker
&&
1023 assigner
- pIfTable
->Adapter
[ndx
].Name
< MAX_ADAPTER_NAME
- 1;
1024 walker
++, assigner
++)
1025 *assigner
= *walker
;
1027 consumeInterfaceName(name
);
1028 pIfTable
->NumAdapters
++;
1035 ret
= ERROR_OUTOFMEMORY
;
1038 TRACE("returning %ld\n", ret
);
1043 static int IpAddrTableSorter(const void *a
, const void *b
)
1048 ret
= ((PMIB_IPADDRROW
)a
)->dwAddr
- ((PMIB_IPADDRROW
)b
)->dwAddr
;
1055 /******************************************************************
1056 * GetIpAddrTable (IPHLPAPI.@)
1061 * pIpAddrTable [In/Out]
1070 DWORD WINAPI
GetIpAddrTable(PMIB_IPADDRTABLE pIpAddrTable
, PULONG pdwSize
, BOOL bOrder
)
1074 TRACE("pIpAddrTable %p, pdwSize %p, bOrder %ld\n", pIpAddrTable
, pdwSize
,
1077 ret
= ERROR_INVALID_PARAMETER
;
1079 DWORD numInterfaces
= getNumInterfaces();
1080 ULONG size
= sizeof(MIB_IPADDRTABLE
) + (numInterfaces
- 1) *
1081 sizeof(MIB_IPADDRROW
);
1083 if (!pIpAddrTable
|| *pdwSize
< size
) {
1085 ret
= ERROR_INSUFFICIENT_BUFFER
;
1088 InterfaceIndexTable
*table
= getInterfaceIndexTable();
1091 size
= sizeof(MIB_IPADDRTABLE
) + (table
->numIndexes
- 1) *
1092 sizeof(MIB_IPADDRROW
);
1093 if (*pdwSize
< size
) {
1095 ret
= ERROR_INSUFFICIENT_BUFFER
;
1100 pIpAddrTable
->dwNumEntries
= 0;
1101 for (ndx
= 0; ndx
< table
->numIndexes
; ndx
++) {
1102 pIpAddrTable
->table
[ndx
].dwIndex
= table
->indexes
[ndx
];
1103 pIpAddrTable
->table
[ndx
].dwAddr
=
1104 getInterfaceIPAddrByIndex(table
->indexes
[ndx
]);
1105 pIpAddrTable
->table
[ndx
].dwMask
=
1106 getInterfaceMaskByIndex(table
->indexes
[ndx
]);
1107 /* the dwBCastAddr member isn't the broadcast address, it indicates
1108 * whether the interface uses the 1's broadcast address (1) or the
1109 * 0's broadcast address (0).
1111 bcast
= getInterfaceBCastAddrByIndex(table
->indexes
[ndx
]);
1112 pIpAddrTable
->table
[ndx
].dwBCastAddr
=
1113 (bcast
& pIpAddrTable
->table
[ndx
].dwMask
) ? 1 : 0;
1114 /* FIXME: hardcoded reasm size, not sure where to get it */
1115 pIpAddrTable
->table
[ndx
].dwReasmSize
= 65535;
1116 pIpAddrTable
->table
[ndx
].unused1
= 0;
1117 pIpAddrTable
->table
[ndx
].wType
= 0; /* aka unused2 */
1118 pIpAddrTable
->dwNumEntries
++;
1121 qsort(pIpAddrTable
->table
, pIpAddrTable
->dwNumEntries
,
1122 sizeof(MIB_IPADDRROW
), IpAddrTableSorter
);
1128 ret
= ERROR_OUTOFMEMORY
;
1131 TRACE("returning %ld\n", ret
);
1136 static int IpForwardTableSorter(const void *a
, const void *b
)
1141 PMIB_IPFORWARDROW rowA
= (PMIB_IPFORWARDROW
)a
, rowB
= (PMIB_IPFORWARDROW
)b
;
1143 ret
= rowA
->dwForwardDest
- rowB
->dwForwardDest
;
1145 ret
= rowA
->dwForwardProto
- rowB
->dwForwardProto
;
1147 ret
= rowA
->dwForwardPolicy
- rowB
->dwForwardPolicy
;
1149 ret
= rowA
->dwForwardNextHop
- rowB
->dwForwardNextHop
;
1159 /******************************************************************
1160 * GetIpForwardTable (IPHLPAPI.@)
1165 * pIpForwardTable [In/Out]
1174 DWORD WINAPI
GetIpForwardTable(PMIB_IPFORWARDTABLE pIpForwardTable
, PULONG pdwSize
, BOOL bOrder
)
1178 TRACE("pIpForwardTable %p, pdwSize %p, bOrder %ld\n", pIpForwardTable
,
1179 pdwSize
, (DWORD
)bOrder
);
1181 ret
= ERROR_INVALID_PARAMETER
;
1183 DWORD numRoutes
= getNumRoutes();
1184 ULONG sizeNeeded
= sizeof(MIB_IPFORWARDTABLE
) + (numRoutes
- 1) *
1185 sizeof(MIB_IPFORWARDROW
);
1187 if (!pIpForwardTable
|| *pdwSize
< sizeNeeded
) {
1188 *pdwSize
= sizeNeeded
;
1189 ret
= ERROR_INSUFFICIENT_BUFFER
;
1192 RouteTable
*table
= getRouteTable();
1194 sizeNeeded
= sizeof(MIB_IPFORWARDTABLE
) + (table
->numRoutes
- 1) *
1195 sizeof(MIB_IPFORWARDROW
);
1196 if (*pdwSize
< sizeNeeded
) {
1197 *pdwSize
= sizeNeeded
;
1198 ret
= ERROR_INSUFFICIENT_BUFFER
;
1203 pIpForwardTable
->dwNumEntries
= table
->numRoutes
;
1204 for (ndx
= 0; ndx
< numRoutes
; ndx
++) {
1205 pIpForwardTable
->table
[ndx
].dwForwardIfIndex
=
1206 table
->routes
[ndx
].ifIndex
;
1207 pIpForwardTable
->table
[ndx
].dwForwardDest
=
1208 table
->routes
[ndx
].dest
;
1209 pIpForwardTable
->table
[ndx
].dwForwardMask
=
1210 table
->routes
[ndx
].mask
;
1211 pIpForwardTable
->table
[ndx
].dwForwardPolicy
= 0;
1212 pIpForwardTable
->table
[ndx
].dwForwardNextHop
=
1213 table
->routes
[ndx
].gateway
;
1214 /* FIXME: this type is appropriate for local interfaces; may not
1215 always be appropriate */
1216 pIpForwardTable
->table
[ndx
].dwForwardType
= MIB_IPROUTE_TYPE_DIRECT
;
1217 /* FIXME: other protos might be appropriate, e.g. the default route
1218 is typically set with MIB_IPPROTO_NETMGMT instead */
1219 pIpForwardTable
->table
[ndx
].dwForwardProto
= MIB_IPPROTO_LOCAL
;
1220 /* punt on age and AS */
1221 pIpForwardTable
->table
[ndx
].dwForwardAge
= 0;
1222 pIpForwardTable
->table
[ndx
].dwForwardNextHopAS
= 0;
1223 pIpForwardTable
->table
[ndx
].dwForwardMetric1
=
1224 table
->routes
[ndx
].metric
;
1225 /* rest of the metrics are 0.. */
1226 pIpForwardTable
->table
[ndx
].dwForwardMetric2
= 0;
1227 pIpForwardTable
->table
[ndx
].dwForwardMetric3
= 0;
1228 pIpForwardTable
->table
[ndx
].dwForwardMetric4
= 0;
1229 pIpForwardTable
->table
[ndx
].dwForwardMetric5
= 0;
1232 qsort(pIpForwardTable
->table
, pIpForwardTable
->dwNumEntries
,
1233 sizeof(MIB_IPFORWARDROW
), IpForwardTableSorter
);
1236 HeapFree(GetProcessHeap(), 0, table
);
1239 ret
= ERROR_OUTOFMEMORY
;
1242 TRACE("returning %ld\n", ret
);
1247 static int IpNetTableSorter(const void *a
, const void *b
)
1252 ret
= ((PMIB_IPNETROW
)a
)->dwAddr
- ((PMIB_IPNETROW
)b
)->dwAddr
;
1259 /******************************************************************
1260 * GetIpNetTable (IPHLPAPI.@)
1265 * pIpNetTable [In/Out]
1274 DWORD WINAPI
GetIpNetTable(PMIB_IPNETTABLE pIpNetTable
, PULONG pdwSize
, BOOL bOrder
)
1276 DWORD ret
= NO_ERROR
;
1278 TRACE("pIpNetTable %p, pdwSize %p, bOrder %d\n", pIpNetTable
, pdwSize
,
1281 ret
= ERROR_INVALID_PARAMETER
;
1283 DWORD numEntries
= getNumArpEntries();
1284 ULONG size
= sizeof(MIB_IPNETTABLE
);
1287 size
+= (numEntries
- 1) * sizeof(MIB_IPNETROW
);
1288 if (!pIpNetTable
|| *pdwSize
< size
) {
1290 ret
= ERROR_INSUFFICIENT_BUFFER
;
1293 PMIB_IPNETTABLE table
= getArpTable();
1295 size
= sizeof(MIB_IPNETTABLE
);
1296 if (table
->dwNumEntries
> 1)
1297 size
+= (table
->dwNumEntries
- 1) * sizeof(MIB_IPNETROW
);
1298 if (*pdwSize
< size
) {
1300 ret
= ERROR_INSUFFICIENT_BUFFER
;
1304 memcpy(pIpNetTable
, table
, size
);
1306 qsort(pIpNetTable
->table
, pIpNetTable
->dwNumEntries
,
1307 sizeof(MIB_IPNETROW
), IpNetTableSorter
);
1310 HeapFree(GetProcessHeap(), 0, table
);
1314 TRACE("returning %d\n", ret
);
1319 /******************************************************************
1320 * GetIpStatistics (IPHLPAPI.@)
1332 DWORD WINAPI
GetIpStatistics(PMIB_IPSTATS pStats
)
1334 return GetIpStatisticsEx(pStats
, PF_INET
);
1337 /******************************************************************
1338 * GetIpStatisticsEx (IPHLPAPI.@)
1351 DWORD WINAPI
GetIpStatisticsEx(PMIB_IPSTATS pStats
, DWORD dwFamily
)
1355 TRACE("pStats %p\n", pStats
);
1356 ret
= getIPStats(pStats
, dwFamily
);
1357 TRACE("returning %ld\n", ret
);
1361 /******************************************************************
1362 * GetNetworkParams (IPHLPAPI.@)
1367 * pFixedInfo [In/Out]
1368 * pOutBufLen [In/Out]
1375 DWORD WINAPI
GetNetworkParams(PFIXED_INFO pFixedInfo
, PULONG pOutBufLen
)
1380 PIPHLP_RES_INFO resInfo
;
1382 TRACE("pFixedInfo %p, pOutBufLen %p\n", pFixedInfo
, pOutBufLen
);
1384 return ERROR_INVALID_PARAMETER
;
1386 resInfo
= getResInfo();
1388 return ERROR_OUTOFMEMORY
;
1390 size
= sizeof(FIXED_INFO
) + (resInfo
->riCount
> 1 ? (resInfo
->riCount
-1) *
1391 sizeof(IP_ADDR_STRING
) : 0);
1392 if (!pFixedInfo
|| *pOutBufLen
< size
) {
1394 disposeResInfo( resInfo
);
1395 return ERROR_BUFFER_OVERFLOW
;
1398 memset(pFixedInfo
, 0, size
);
1399 size
= sizeof(pFixedInfo
->HostName
);
1400 GetComputerNameExA(ComputerNameDnsHostname
, pFixedInfo
->HostName
, &size
);
1401 size
= sizeof(pFixedInfo
->DomainName
);
1402 GetComputerNameExA(ComputerNameDnsDomain
, pFixedInfo
->DomainName
, &size
);
1404 TRACE("GetComputerNameExA: %s\n", pFixedInfo
->DomainName
);
1406 if (resInfo
->riCount
> 0)
1408 CopyMemory(&pFixedInfo
->DnsServerList
, resInfo
->DnsList
, sizeof(IP_ADDR_STRING
));
1409 if (resInfo
->riCount
> 1)
1411 IP_ADDR_STRING
*pSrc
= resInfo
->DnsList
->Next
;
1412 IP_ADDR_STRING
*pTarget
= (struct _IP_ADDR_STRING
*)((char*)pFixedInfo
+ sizeof(FIXED_INFO
));
1414 pFixedInfo
->DnsServerList
.Next
= pTarget
;
1418 CopyMemory(pTarget
, pSrc
, sizeof(IP_ADDR_STRING
));
1420 if (resInfo
->riCount
> 1)
1422 pTarget
->Next
= (IP_ADDR_STRING
*)((char*)pTarget
+ sizeof(IP_ADDR_STRING
));
1423 pTarget
= pTarget
->Next
;
1428 pTarget
->Next
= NULL
;
1436 pFixedInfo
->DnsServerList
.Next
= NULL
;
1440 pFixedInfo
->NodeType
= HYBRID_NODETYPE
;
1441 regReturn
= RegOpenKeyExA(HKEY_LOCAL_MACHINE
,
1442 "SYSTEM\\CurrentControlSet\\Services\\VxD\\MSTCP", 0, KEY_READ
, &hKey
);
1443 if (regReturn
!= ERROR_SUCCESS
)
1444 regReturn
= RegOpenKeyExA(HKEY_LOCAL_MACHINE
,
1445 "SYSTEM\\CurrentControlSet\\Services\\NetBT\\Parameters", 0, KEY_READ
,
1447 if (regReturn
== ERROR_SUCCESS
)
1449 DWORD size
= sizeof(pFixedInfo
->ScopeId
);
1451 RegQueryValueExA(hKey
, "ScopeID", NULL
, NULL
, (PBYTE
)pFixedInfo
->ScopeId
, &size
);
1455 disposeResInfo( resInfo
);
1456 /* FIXME: can check whether routing's enabled in /proc/sys/net/ipv4/ip_forward
1457 I suppose could also check for a listener on port 53 to set EnableDns */
1459 TRACE("returning %ld\n", ret
);
1465 /******************************************************************
1466 * GetNumberOfInterfaces (IPHLPAPI.@)
1478 DWORD WINAPI
GetNumberOfInterfaces(PDWORD pdwNumIf
)
1482 TRACE("pdwNumIf %p\n", pdwNumIf
);
1484 ret
= ERROR_INVALID_PARAMETER
;
1486 *pdwNumIf
= getNumInterfaces();
1489 TRACE("returning %ld\n", ret
);
1494 /******************************************************************
1495 * GetOwnerModuleFromTcpEntry (IPHLPAPI.@)
1497 * Get data about the module that issued the context bind for a specific IPv4 TCP endpoint in a MIB table row
1500 * pTcpEntry [in] pointer to a MIB_TCPROW_OWNER_MODULE structure
1501 * Class [in] TCPIP_OWNER_MODULE_INFO_CLASS enumeration value
1502 * Buffer [out] pointer a buffer containing a TCPIP_OWNER_MODULE_BASIC_INFO structure with the owner module data.
1503 * pdwSize [in, out] estimated size of the structure returned in Buffer, in bytes
1507 * Failure: ERROR_INSUFFICIENT_BUFFER, ERROR_INVALID_PARAMETER, ERROR_NOT_ENOUGH_MEMORY
1508 * ERROR_NOT_FOUND or ERROR_PARTIAL_COPY
1511 * The type of data returned in Buffer is indicated by the value of the Class parameter.
1513 DWORD WINAPI
GetOwnerModuleFromTcpEntry( PMIB_TCPROW_OWNER_MODULE pTcpEntry
, TCPIP_OWNER_MODULE_INFO_CLASS Class
, PVOID Buffer
, PDWORD pdwSize
)
1515 DWORD ret
= NO_ERROR
;
1521 /******************************************************************
1522 * GetPerAdapterInfo (IPHLPAPI.@)
1528 * pPerAdapterInfo [In/Out]
1529 * pOutBufLen [In/Out]
1536 static void CreateNameServerListEnumNamesFunc( PWCHAR Interface
, PWCHAR Server
, PVOID Data
)
1538 IP_ADDR_STRING
*pNext
;
1539 PNAME_SERVER_LIST_CONTEXT Context
= (PNAME_SERVER_LIST_CONTEXT
)Data
;
1541 if (!Context
->NumServers
)
1543 if (Context
->uSizeAvailable
>= Context
->uSizeRequired
)
1545 WideCharToMultiByte(CP_ACP
, 0, Server
, -1, Context
->pData
->DnsServerList
.IpAddress
.String
, 16, NULL
, NULL
);
1546 Context
->pData
->DnsServerList
.IpAddress
.String
[15] = '\0';
1547 Context
->pLastAddr
= &Context
->pData
->DnsServerList
;
1552 Context
->uSizeRequired
+= sizeof(IP_ADDR_STRING
);
1553 if (Context
->uSizeAvailable
>= Context
->uSizeRequired
)
1555 pNext
= (IP_ADDR_STRING
*)(((char*)Context
->pLastAddr
) + sizeof(IP_ADDR_STRING
));
1556 WideCharToMultiByte(CP_ACP
, 0, Server
, -1, pNext
->IpAddress
.String
, 16, NULL
, NULL
);
1557 pNext
->IpAddress
.String
[15] = '\0';
1558 Context
->pLastAddr
->Next
= pNext
;
1559 Context
->pLastAddr
= pNext
;
1563 Context
->NumServers
++;
1566 DWORD WINAPI
GetPerAdapterInfo(ULONG IfIndex
, PIP_PER_ADAPTER_INFO pPerAdapterInfo
, PULONG pOutBufLen
)
1571 NAME_SERVER_LIST_CONTEXT Context
;
1572 WCHAR keyname
[200] = L
"SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Interfaces\\";
1575 return ERROR_INVALID_PARAMETER
;
1577 if (!pPerAdapterInfo
|| *pOutBufLen
< sizeof(IP_PER_ADAPTER_INFO
))
1579 *pOutBufLen
= sizeof(IP_PER_ADAPTER_INFO
);
1580 return ERROR_BUFFER_OVERFLOW
;
1583 ifName
= getInterfaceNameByIndex(IfIndex
);
1585 return ERROR_INVALID_PARAMETER
;
1587 MultiByteToWideChar(CP_ACP
, 0, ifName
, -1, &keyname
[62], sizeof(keyname
)/sizeof(WCHAR
) - 63);
1588 HeapFree(GetProcessHeap(), 0, (LPVOID
)ifName
);
1590 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE
, keyname
, 0, KEY_READ
, &hkey
) != ERROR_SUCCESS
)
1592 return ERROR_NOT_SUPPORTED
;
1594 Context
.NumServers
= 0;
1595 Context
.uSizeAvailable
= *pOutBufLen
;
1596 Context
.uSizeRequired
= sizeof(IP_PER_ADAPTER_INFO
);
1597 Context
.pData
= pPerAdapterInfo
;
1599 if (*pOutBufLen
>= sizeof(IP_PER_ADAPTER_INFO
))
1600 ZeroMemory(pPerAdapterInfo
, sizeof(IP_PER_ADAPTER_INFO
));
1602 EnumNameServers(hkey
, &keyname
[62], &Context
, CreateNameServerListEnumNamesFunc
);
1604 if (Context
.uSizeRequired
> Context
.uSizeAvailable
)
1606 *pOutBufLen
= Context
.uSizeRequired
;
1608 return ERROR_BUFFER_OVERFLOW
;
1611 if(RegQueryValueExW(hkey
, L
"NameServer", NULL
, NULL
, NULL
, &dwSize
) == ERROR_SUCCESS
)
1613 pPerAdapterInfo
->AutoconfigActive
= FALSE
;
1617 pPerAdapterInfo
->AutoconfigActive
= TRUE
;
1625 /******************************************************************
1626 * GetRTTAndHopCount (IPHLPAPI.@)
1631 * DestIpAddress [In]
1641 BOOL WINAPI
GetRTTAndHopCount(IPAddr DestIpAddress
, PULONG HopCount
, ULONG MaxHops
, PULONG RTT
)
1643 TRACE("DestIpAddress 0x%08lx, HopCount %p, MaxHops %ld, RTT %p\n",
1644 DestIpAddress
, HopCount
, MaxHops
, RTT
);
1650 /******************************************************************
1651 * GetTcpStatisticsEx (IPHLPAPI.@)
1664 DWORD WINAPI
GetTcpStatisticsEx(PMIB_TCPSTATS pStats
, DWORD dwFamily
)
1668 TRACE("pStats %p\n", pStats
);
1669 ret
= getTCPStats(pStats
, dwFamily
);
1670 TRACE("returning %ld\n", ret
);
1674 /******************************************************************
1675 * GetTcpStatistics (IPHLPAPI.@)
1687 DWORD WINAPI
GetTcpStatistics(PMIB_TCPSTATS pStats
)
1689 return GetTcpStatisticsEx(pStats
, PF_INET
);
1693 static int TcpTableSorter(const void *a
, const void *b
)
1698 PMIB_TCPROW rowA
= (PMIB_TCPROW
)a
, rowB
= (PMIB_TCPROW
)b
;
1700 ret
= rowA
->dwLocalAddr
- rowB
->dwLocalAddr
;
1702 ret
= rowA
->dwLocalPort
- rowB
->dwLocalPort
;
1704 ret
= rowA
->dwRemoteAddr
- rowB
->dwRemoteAddr
;
1706 ret
= rowA
->dwRemotePort
- rowB
->dwRemotePort
;
1716 /******************************************************************
1717 * GetTcpTable (IPHLPAPI.@)
1719 * Get the table of active TCP connections.
1722 * pTcpTable [Out] buffer for TCP connections table
1723 * pdwSize [In/Out] length of output buffer
1724 * bOrder [In] whether to order the table
1728 * Failure: error code from winerror.h
1731 * If pdwSize is less than required, the function will return
1732 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to
1733 * the required byte size.
1734 * If bOrder is true, the returned table will be sorted, first by
1735 * local address and port number, then by remote address and port
1738 DWORD WINAPI
GetTcpTable(PMIB_TCPTABLE pTcpTable
, PDWORD pdwSize
, BOOL bOrder
)
1740 DWORD ret
= ERROR_NO_DATA
;
1742 TRACE("pTcpTable %p, pdwSize %p, bOrder %d\n", pTcpTable
, pdwSize
,
1745 ret
= ERROR_INVALID_PARAMETER
;
1747 DWORD numEntries
= getNumTcpEntries();
1748 DWORD size
= sizeof(MIB_TCPTABLE
);
1751 size
+= (numEntries
- 1) * sizeof(MIB_TCPROW
);
1752 if (!pTcpTable
|| *pdwSize
< size
) {
1754 ret
= ERROR_INSUFFICIENT_BUFFER
;
1757 PMIB_TCPTABLE pOurTcpTable
= getTcpTable();
1760 size
= sizeof(MIB_TCPTABLE
);
1761 if (pOurTcpTable
->dwNumEntries
> 1)
1762 size
+= (pOurTcpTable
->dwNumEntries
- 1) * sizeof(MIB_TCPROW
);
1764 if (*pdwSize
< size
)
1768 ret
= ERROR_INSUFFICIENT_BUFFER
;
1772 memcpy(pTcpTable
, pOurTcpTable
, size
);
1775 qsort(pTcpTable
->table
, pTcpTable
->dwNumEntries
,
1776 sizeof(MIB_TCPROW
), TcpTableSorter
);
1785 TRACE("returning %d\n", ret
);
1790 /******************************************************************
1791 * GetUdpStatisticsEx (IPHLPAPI.@)
1804 DWORD WINAPI
GetUdpStatisticsEx(PMIB_UDPSTATS pStats
, DWORD dwFamily
)
1808 TRACE("pStats %p\n", pStats
);
1809 ret
= getUDPStats(pStats
, dwFamily
);
1810 TRACE("returning %ld\n", ret
);
1814 /******************************************************************
1815 * GetUdpStatistics (IPHLPAPI.@)
1827 DWORD WINAPI
GetUdpStatistics(PMIB_UDPSTATS pStats
)
1829 return GetUdpStatisticsEx(pStats
, PF_INET
);
1833 static int UdpTableSorter(const void *a
, const void *b
)
1838 PMIB_UDPROW rowA
= (PMIB_UDPROW
)a
, rowB
= (PMIB_UDPROW
)b
;
1840 ret
= rowA
->dwLocalAddr
- rowB
->dwLocalAddr
;
1842 ret
= rowA
->dwLocalPort
- rowB
->dwLocalPort
;
1850 /******************************************************************
1851 * GetUdpTable (IPHLPAPI.@)
1856 * pUdpTable [In/Out]
1865 DWORD WINAPI
GetUdpTable(PMIB_UDPTABLE pUdpTable
, PDWORD pdwSize
, BOOL bOrder
)
1869 TRACE("pUdpTable %p, pdwSize %p, bOrder %ld\n", pUdpTable
, pdwSize
,
1872 ret
= ERROR_INVALID_PARAMETER
;
1874 DWORD numEntries
= getNumUdpEntries();
1875 ULONG size
= sizeof(MIB_UDPTABLE
) + (numEntries
- 1) * sizeof(MIB_UDPROW
);
1877 if (!pUdpTable
|| *pdwSize
< size
) {
1879 ret
= ERROR_INSUFFICIENT_BUFFER
;
1882 PMIB_UDPTABLE table
= getUdpTable();
1885 size
= sizeof(MIB_UDPTABLE
) + (table
->dwNumEntries
- 1) *
1887 if (*pdwSize
< size
) {
1889 ret
= ERROR_INSUFFICIENT_BUFFER
;
1892 memcpy(pUdpTable
, table
, size
);
1894 qsort(pUdpTable
->table
, pUdpTable
->dwNumEntries
,
1895 sizeof(MIB_UDPROW
), UdpTableSorter
);
1901 ret
= ERROR_OUTOFMEMORY
;
1904 TRACE("returning %ld\n", ret
);
1909 /******************************************************************
1910 * GetUniDirectionalAdapterInfo (IPHLPAPI.@)
1912 * This is a Win98-only function to get information on "unidirectional"
1913 * adapters. Since this is pretty nonsensical in other contexts, it
1914 * never returns anything.
1917 * pIPIfInfo [Out] buffer for adapter infos
1918 * dwOutBufLen [Out] length of the output buffer
1922 * Failure: error code from winerror.h
1925 * Stub, returns ERROR_NOT_SUPPORTED.
1927 DWORD WINAPI
GetUniDirectionalAdapterInfo(PIP_UNIDIRECTIONAL_ADAPTER_ADDRESS pIPIfInfo
, PULONG dwOutBufLen
)
1929 TRACE("pIPIfInfo %p, dwOutBufLen %p\n", pIPIfInfo
, dwOutBufLen
);
1930 /* a unidirectional adapter?? not bloody likely! */
1931 return ERROR_NOT_SUPPORTED
;
1935 /******************************************************************
1936 * IpReleaseAddress (IPHLPAPI.@)
1938 * Release an IP obtained through DHCP,
1941 * AdapterInfo [In] adapter to release IP address
1945 * Failure: error code from winerror.h
1948 DWORD WINAPI
IpReleaseAddress(PIP_ADAPTER_INDEX_MAP AdapterInfo
)
1950 DWORD Status
, Version
= 0;
1952 if (!AdapterInfo
|| !AdapterInfo
->Name
)
1953 return ERROR_INVALID_PARAMETER
;
1955 /* Maybe we should do this in DllMain */
1956 if (DhcpCApiInitialize(&Version
) != ERROR_SUCCESS
)
1957 return ERROR_PROC_NOT_FOUND
;
1959 if (DhcpReleaseIpAddressLease(AdapterInfo
->Index
))
1960 Status
= ERROR_SUCCESS
;
1962 Status
= ERROR_PROC_NOT_FOUND
;
1970 /******************************************************************
1971 * IpRenewAddress (IPHLPAPI.@)
1973 * Renew an IP obtained through DHCP.
1976 * AdapterInfo [In] adapter to renew IP address
1980 * Failure: error code from winerror.h
1982 DWORD WINAPI
IpRenewAddress(PIP_ADAPTER_INDEX_MAP AdapterInfo
)
1984 DWORD Status
, Version
= 0;
1986 if (!AdapterInfo
|| !AdapterInfo
->Name
)
1987 return ERROR_INVALID_PARAMETER
;
1989 /* Maybe we should do this in DllMain */
1990 if (DhcpCApiInitialize(&Version
) != ERROR_SUCCESS
)
1991 return ERROR_PROC_NOT_FOUND
;
1993 if (DhcpRenewIpAddressLease(AdapterInfo
->Index
))
1994 Status
= ERROR_SUCCESS
;
1996 Status
= ERROR_PROC_NOT_FOUND
;
2004 /******************************************************************
2005 * NotifyAddrChange (IPHLPAPI.@)
2007 * Notify caller whenever the ip-interface map is changed.
2010 * Handle [Out] handle usable in asynchronous notification
2011 * overlapped [In] overlapped structure that notifies the caller
2015 * Failure: error code from winerror.h
2018 * Stub, returns ERROR_NOT_SUPPORTED.
2020 DWORD WINAPI
NotifyAddrChange(PHANDLE Handle
, LPOVERLAPPED overlapped
)
2022 FIXME("(Handle %p, overlapped %p): stub\n", Handle
, overlapped
);
2023 if (Handle
) *Handle
= INVALID_HANDLE_VALUE
;
2024 if (overlapped
) ((IO_STATUS_BLOCK
*) overlapped
)->Status
= STATUS_PENDING
;
2025 return ERROR_IO_PENDING
;
2029 /******************************************************************
2030 * NotifyRouteChange (IPHLPAPI.@)
2032 * Notify caller whenever the ip routing table is changed.
2035 * Handle [Out] handle usable in asynchronous notification
2036 * overlapped [In] overlapped structure that notifies the caller
2040 * Failure: error code from winerror.h
2043 * Stub, returns ERROR_NOT_SUPPORTED.
2045 DWORD WINAPI
NotifyRouteChange(PHANDLE Handle
, LPOVERLAPPED overlapped
)
2047 FIXME("(Handle %p, overlapped %p): stub\n", Handle
, overlapped
);
2048 return ERROR_NOT_SUPPORTED
;
2052 /******************************************************************
2053 * SendARP (IPHLPAPI.@)
2055 * Send an ARP request.
2058 * DestIP [In] attempt to obtain this IP
2059 * SrcIP [In] optional sender IP address
2060 * pMacAddr [Out] buffer for the mac address
2061 * PhyAddrLen [In/Out] length of the output buffer
2065 * Failure: error code from winerror.h
2068 * Stub, returns ERROR_NOT_SUPPORTED.
2070 DWORD WINAPI
SendARP(IPAddr DestIP
, IPAddr SrcIP
, PULONG pMacAddr
, PULONG PhyAddrLen
)
2072 FIXME("(DestIP 0x%08x, SrcIP 0x%08x, pMacAddr %p, PhyAddrLen %p): stub\n",
2073 DestIP
, SrcIP
, pMacAddr
, PhyAddrLen
);
2074 return ERROR_NOT_SUPPORTED
;
2078 /******************************************************************
2079 * SetIfEntry (IPHLPAPI.@)
2081 * Set the administrative status of an interface.
2084 * pIfRow [In] dwAdminStatus member specifies the new status.
2088 * Failure: error code from winerror.h
2091 * Stub, returns ERROR_NOT_SUPPORTED.
2093 DWORD WINAPI
SetIfEntry(PMIB_IFROW pIfRow
)
2095 FIXME("(pIfRow %p): stub\n", pIfRow
);
2096 /* this is supposed to set an interface administratively up or down.
2097 Could do SIOCSIFFLAGS and set/clear IFF_UP, but, not sure I want to, and
2098 this sort of down is indistinguishable from other sorts of down (e.g. no
2100 return ERROR_NOT_SUPPORTED
;
2104 /******************************************************************
2105 * SetIpForwardEntry (IPHLPAPI.@)
2107 * Modify an existing route.
2110 * pRoute [In] route with the new information
2114 * Failure: error code from winerror.h
2117 DWORD WINAPI
SetIpForwardEntry(PMIB_IPFORWARDROW pRoute
)
2119 return setIpForwardEntry( pRoute
);
2123 /******************************************************************
2124 * SetIpNetEntry (IPHLPAPI.@)
2126 * Modify an existing ARP entry.
2129 * pArpEntry [In] ARP entry with the new information
2133 * Failure: error code from winerror.h
2135 DWORD WINAPI
SetIpNetEntry(PMIB_IPNETROW pArpEntry
)
2139 TCP_REQUEST_SET_INFORMATION_EX_ARP_ENTRY req
=
2140 TCP_REQUEST_SET_INFORMATION_INIT
;
2143 PMIB_IPNETROW arpBuff
;
2146 return ERROR_INVALID_PARAMETER
;
2148 if (!NT_SUCCESS(openTcpFile( &tcpFile
)))
2149 return ERROR_NOT_SUPPORTED
;
2151 if (!NT_SUCCESS(getNthIpEntity( tcpFile
, pArpEntry
->dwIndex
, &id
)))
2153 closeTcpFile(tcpFile
);
2154 return ERROR_INVALID_PARAMETER
;
2157 req
.Req
.ID
.toi_class
= INFO_CLASS_PROTOCOL
;
2158 req
.Req
.ID
.toi_type
= INFO_TYPE_PROVIDER
;
2159 req
.Req
.ID
.toi_id
= IP_MIB_ARPTABLE_ENTRY_ID
;
2160 req
.Req
.ID
.toi_entity
.tei_instance
= id
.tei_instance
;
2161 req
.Req
.ID
.toi_entity
.tei_entity
= AT_ENTITY
;
2162 req
.Req
.BufferSize
= sizeof(MIB_IPNETROW
);
2163 arpBuff
= (PMIB_IPNETROW
)&req
.Req
.Buffer
[0];
2165 RtlCopyMemory(arpBuff
, pArpEntry
, sizeof(MIB_IPNETROW
));
2167 status
= DeviceIoControl( tcpFile
,
2168 IOCTL_TCP_SET_INFORMATION_EX
,
2176 closeTcpFile(tcpFile
);
2181 return ERROR_INVALID_PARAMETER
;
2185 /******************************************************************
2186 * SetIpStatistics (IPHLPAPI.@)
2188 * Toggle IP forwarding and det the default TTL value.
2191 * pIpStats [In] IP statistics with the new information
2195 * Failure: error code from winerror.h
2198 * Stub, returns NO_ERROR.
2200 DWORD WINAPI
SetIpStatistics(PMIB_IPSTATS pIpStats
)
2202 FIXME("(pIpStats %p): stub\n", pIpStats
);
2207 /******************************************************************
2208 * SetIpTTL (IPHLPAPI.@)
2210 * Set the default TTL value.
2213 * nTTL [In] new TTL value
2217 * Failure: error code from winerror.h
2220 * Stub, returns NO_ERROR.
2222 DWORD WINAPI
SetIpTTL(UINT nTTL
)
2224 FIXME("(nTTL %d): stub\n", nTTL
);
2229 /******************************************************************
2230 * SetTcpEntry (IPHLPAPI.@)
2232 * Set the state of a TCP connection.
2235 * pTcpRow [In] specifies connection with new state
2239 * Failure: error code from winerror.h
2242 * Stub, returns NO_ERROR.
2244 DWORD WINAPI
SetTcpEntry(PMIB_TCPROW pTcpRow
)
2246 FIXME("(pTcpRow %p): stub\n", pTcpRow
);
2251 /******************************************************************
2252 * UnenableRouter (IPHLPAPI.@)
2254 * Decrement the IP-forwarding reference count. Turn off IP-forwarding
2255 * if it reaches zero.
2258 * pOverlapped [In/Out] should be the same as in EnableRouter()
2259 * lpdwEnableCount [Out] optional, receives reference count
2263 * Failure: error code from winerror.h
2266 * Stub, returns ERROR_NOT_SUPPORTED.
2268 DWORD WINAPI
UnenableRouter(OVERLAPPED
* pOverlapped
, LPDWORD lpdwEnableCount
)
2270 FIXME("(pOverlapped %p, lpdwEnableCount %p): stub\n", pOverlapped
,
2272 return ERROR_NOT_SUPPORTED
;
2278 DWORD WINAPI
GetIpErrorString(IP_STATUS ErrorCode
,PWCHAR Buffer
,PDWORD Size
)
2288 PIP_ADAPTER_ORDER_MAP WINAPI
GetAdapterOrderMap(VOID
)
2298 DWORD WINAPI DECLSPEC_HOTPATCH
GetAdaptersAddresses(ULONG Family
,ULONG Flags
,PVOID Reserved
,PIP_ADAPTER_ADDRESSES pAdapterAddresses
,PULONG pOutBufLen
)
2300 InterfaceIndexTable
*indexTable
;
2303 ULONG ret
, requiredSize
= 0;
2304 PIP_ADAPTER_ADDRESSES currentAddress
;
2305 PUCHAR currentLocation
;
2308 if (!pOutBufLen
) return ERROR_INVALID_PARAMETER
;
2309 if (Reserved
) return ERROR_INVALID_PARAMETER
;
2311 indexTable
= getNonLoopbackInterfaceIndexTable(); //I think we want non-loopback here
2313 return ERROR_NOT_ENOUGH_MEMORY
;
2315 ret
= openTcpFile(&tcpFile
);
2316 if (!NT_SUCCESS(ret
))
2317 return ERROR_NO_DATA
;
2319 for (i
= indexTable
->numIndexes
; i
>= 0; i
--)
2321 if (NT_SUCCESS(getIPAddrEntryForIf(tcpFile
,
2323 indexTable
->indexes
[i
],
2326 /* The whole struct */
2327 requiredSize
+= sizeof(IP_ADAPTER_ADDRESSES
);
2330 if (!(Flags
& GAA_FLAG_SKIP_FRIENDLY_NAME
))
2331 requiredSize
+= strlen((char *)ifInfo
.if_info
.ent
.if_descr
) + 1; //FIXME
2334 requiredSize
+= strlen((char *)ifInfo
.if_info
.ent
.if_descr
) + 1;
2336 /* Unicast address */
2337 if (!(Flags
& GAA_FLAG_SKIP_UNICAST
))
2338 requiredSize
+= sizeof(IP_ADAPTER_UNICAST_ADDRESS
);
2340 /* FIXME: Implement multicast, anycast, and dns server stuff */
2342 /* FIXME: Implement dns suffix and description */
2343 requiredSize
+= 2 * sizeof(WCHAR
);
2345 /* We're only going to implement what's required for XP SP0 */
2349 if (*pOutBufLen
< requiredSize
)
2351 *pOutBufLen
= requiredSize
;
2352 closeTcpFile(tcpFile
);
2354 return ERROR_BUFFER_OVERFLOW
;
2357 RtlZeroMemory(pAdapterAddresses
, requiredSize
);
2359 /* Let's set up the pointers */
2360 currentAddress
= pAdapterAddresses
;
2361 for (i
= indexTable
->numIndexes
; i
>= 0; i
--)
2363 if (NT_SUCCESS(getIPAddrEntryForIf(tcpFile
,
2365 indexTable
->indexes
[i
],
2368 currentLocation
= (PUCHAR
)currentAddress
+ (ULONG_PTR
)sizeof(IP_ADAPTER_ADDRESSES
);
2370 /* FIXME: Friendly name */
2371 if (!(Flags
& GAA_FLAG_SKIP_FRIENDLY_NAME
))
2373 currentAddress
->FriendlyName
= (PVOID
)currentLocation
;
2374 currentLocation
+= sizeof(WCHAR
);
2378 currentAddress
->AdapterName
= (PVOID
)currentLocation
;
2379 currentLocation
+= strlen((char *)ifInfo
.if_info
.ent
.if_descr
) + 1;
2381 /* Unicast address */
2382 if (!(Flags
& GAA_FLAG_SKIP_UNICAST
))
2384 currentAddress
->FirstUnicastAddress
= (PVOID
)currentLocation
;
2385 currentLocation
+= sizeof(IP_ADAPTER_UNICAST_ADDRESS
);
2386 currentAddress
->FirstUnicastAddress
->Address
.lpSockaddr
= (PVOID
)currentLocation
;
2387 currentLocation
+= sizeof(struct sockaddr
);
2390 /* FIXME: Implement multicast, anycast, and dns server stuff */
2392 /* FIXME: Implement dns suffix and description */
2393 currentAddress
->DnsSuffix
= (PVOID
)currentLocation
;
2394 currentLocation
+= sizeof(WCHAR
);
2396 currentAddress
->Description
= (PVOID
)currentLocation
;
2397 currentLocation
+= sizeof(WCHAR
);
2399 currentAddress
->Next
= (PVOID
)currentLocation
;
2401 /* We're only going to implement what's required for XP SP0 */
2403 currentAddress
= currentAddress
->Next
;
2407 /* Terminate the last address correctly */
2409 currentAddress
->Next
= NULL
;
2411 /* Now again, for real this time */
2413 currentAddress
= pAdapterAddresses
;
2414 for (i
= indexTable
->numIndexes
; i
>= 0; i
--)
2416 if (NT_SUCCESS(getIPAddrEntryForIf(tcpFile
,
2418 indexTable
->indexes
[i
],
2421 /* Make sure we're not looping more than we hoped for */
2422 ASSERT(currentAddress
);
2424 /* Alignment information */
2425 currentAddress
->Length
= sizeof(IP_ADAPTER_ADDRESSES
);
2426 currentAddress
->IfIndex
= indexTable
->indexes
[i
];
2429 strcpy(currentAddress
->AdapterName
, (char *)ifInfo
.if_info
.ent
.if_descr
);
2431 if (!(Flags
& GAA_FLAG_SKIP_UNICAST
))
2433 currentAddress
->FirstUnicastAddress
->Length
= sizeof(IP_ADAPTER_UNICAST_ADDRESS
);
2434 currentAddress
->FirstUnicastAddress
->Flags
= 0; //FIXME
2435 currentAddress
->FirstUnicastAddress
->Next
= NULL
; //FIXME: Support more than one address per adapter
2436 currentAddress
->FirstUnicastAddress
->Address
.lpSockaddr
->sa_family
= AF_INET
;
2437 memcpy(currentAddress
->FirstUnicastAddress
->Address
.lpSockaddr
->sa_data
,
2438 &ifInfo
.ip_addr
.iae_addr
,
2439 sizeof(ifInfo
.ip_addr
.iae_addr
));
2440 currentAddress
->FirstUnicastAddress
->Address
.iSockaddrLength
= sizeof(ifInfo
.ip_addr
.iae_addr
) + sizeof(USHORT
);
2441 currentAddress
->FirstUnicastAddress
->PrefixOrigin
= IpPrefixOriginOther
; //FIXME
2442 currentAddress
->FirstUnicastAddress
->SuffixOrigin
= IpPrefixOriginOther
; //FIXME
2443 currentAddress
->FirstUnicastAddress
->DadState
= IpDadStatePreferred
; //FIXME
2444 currentAddress
->FirstUnicastAddress
->ValidLifetime
= 0xFFFFFFFF; //FIXME
2445 currentAddress
->FirstUnicastAddress
->PreferredLifetime
= 0xFFFFFFFF; //FIXME
2446 currentAddress
->FirstUnicastAddress
->LeaseLifetime
= 0xFFFFFFFF; //FIXME
2449 /* FIXME: Implement multicast, anycast, and dns server stuff */
2450 currentAddress
->FirstAnycastAddress
= NULL
;
2451 currentAddress
->FirstMulticastAddress
= NULL
;
2452 currentAddress
->FirstDnsServerAddress
= NULL
;
2454 /* FIXME: Implement dns suffix, description, and friendly name */
2455 currentAddress
->DnsSuffix
[0] = UNICODE_NULL
;
2456 currentAddress
->Description
[0] = UNICODE_NULL
;
2457 currentAddress
->FriendlyName
[0] = UNICODE_NULL
;
2459 /* Physical Address */
2460 memcpy(currentAddress
->PhysicalAddress
, ifInfo
.if_info
.ent
.if_physaddr
, ifInfo
.if_info
.ent
.if_physaddrlen
);
2461 currentAddress
->PhysicalAddressLength
= ifInfo
.if_info
.ent
.if_physaddrlen
;
2464 currentAddress
->Flags
= 0; //FIXME
2467 currentAddress
->Mtu
= ifInfo
.if_info
.ent
.if_mtu
;
2469 /* Interface type */
2470 currentAddress
->IfType
= ifInfo
.if_info
.ent
.if_type
;
2472 /* Operational status */
2473 currentAddress
->OperStatus
= ifInfo
.if_info
.ent
.if_operstatus
;
2475 /* We're only going to implement what's required for XP SP0 */
2477 /* Move to the next address */
2478 currentAddress
= currentAddress
->Next
;
2482 closeTcpFile(tcpFile
);
2492 BOOL WINAPI
CancelIPChangeNotify(LPOVERLAPPED notifyOverlapped
)
2501 DWORD WINAPI
GetBestInterfaceEx(struct sockaddr
*pDestAddr
,PDWORD pdwBestIfIndex
)
2510 DWORD WINAPI
NhpAllocateAndGetInterfaceInfoFromStack(IP_INTERFACE_NAME_INFO
**ppTable
,PDWORD pdwCount
,BOOL bOrder
,HANDLE hHeap
,DWORD dwFlags
)
2519 DWORD WINAPI
GetIcmpStatisticsEx(PMIB_ICMP_EX pStats
,DWORD dwFamily
)
2526 SetIpForwardEntryToStack(PMIB_IPFORWARDROW pRoute
)
2528 FIXME("SetIpForwardEntryToStack() stub\n");
2533 NhGetInterfaceNameFromDeviceGuid(DWORD dwUnknown1
,
2539 FIXME("NhGetInterfaceNameFromDeviceGuid() stub\n");