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"
27 WINE_DEFAULT_DEBUG_CHANNEL(iphlpapi
);
29 typedef struct _NAME_SERVER_LIST_CONTEXT
{
32 PIP_PER_ADAPTER_INFO pData
;
34 IP_ADDR_STRING
*pLastAddr
;
35 } NAME_SERVER_LIST_CONTEXT
, *PNAME_SERVER_LIST_CONTEXT
;
37 BOOL WINAPI
DllMain (HINSTANCE hinstDLL
, DWORD fdwReason
, LPVOID lpvReserved
)
40 case DLL_PROCESS_ATTACH
:
41 DisableThreadLibraryCalls( hinstDLL
);
45 case DLL_PROCESS_DETACH
:
52 /******************************************************************
53 * AddIPAddress (IPHLPAPI.@)
62 * NTEInstance [In/Out]
69 DWORD WINAPI
AddIPAddress(IPAddr Address
, IPMask Netmask
, DWORD IfIndex
, PULONG NteContext
, PULONG NteInstance
)
71 return RtlNtStatusToDosError(addIPAddress(Address
, Netmask
, IfIndex
, NteContext
, NteInstance
));
74 DWORD
getInterfaceGatewayByIndex(DWORD index
)
76 DWORD ndx
, retVal
= 0, numRoutes
= getNumRoutes();
77 RouteTable
*table
= getRouteTable();
80 for (ndx
= 0; ndx
< numRoutes
; ndx
++)
82 if ((table
->routes
[ndx
].ifIndex
== (index
)) && (table
->routes
[ndx
].dest
== 0))
83 retVal
= table
->routes
[ndx
].gateway
;
85 HeapFree(GetProcessHeap(), 0, table
);
89 /******************************************************************
90 * AllocateAndGetIfTableFromStack (IPHLPAPI.@)
95 * ppIfTable [Out] -- pointer into which the MIB_IFTABLE is
96 * allocated and returned.
97 * bOrder [In] -- passed to GetIfTable to order the table
98 * heap [In] -- heap from which the table is allocated
99 * flags [In] -- flags to HeapAlloc
101 * RETURNS -- ERROR_INVALID_PARAMETER if ppIfTable is NULL, whatever
102 * GetIfTable returns otherwise
105 DWORD WINAPI
AllocateAndGetIfTableFromStack(PMIB_IFTABLE
*ppIfTable
,
106 BOOL bOrder
, HANDLE heap
, DWORD flags
)
110 TRACE("ppIfTable %p, bOrder %ld, heap 0x%08lx, flags 0x%08lx\n", ppIfTable
,
111 (DWORD
)bOrder
, (DWORD
)heap
, flags
);
113 ret
= ERROR_INVALID_PARAMETER
;
118 ret
= GetIfTable(*ppIfTable
, &dwSize
, bOrder
);
119 if (ret
== ERROR_INSUFFICIENT_BUFFER
) {
120 *ppIfTable
= (PMIB_IFTABLE
)HeapAlloc(heap
, flags
, dwSize
);
121 ret
= GetIfTable(*ppIfTable
, &dwSize
, bOrder
);
122 if (ret
!= NO_ERROR
) {
123 HeapFree(heap
, flags
, *ppIfTable
);
128 TRACE("returning %ld\n", ret
);
133 /******************************************************************
134 * AllocateAndGetIpAddrTableFromStack (IPHLPAPI.@)
139 * ppIpAddrTable [Out]
140 * bOrder [In] -- passed to GetIpAddrTable to order the table
141 * heap [In] -- heap from which the table is allocated
142 * flags [In] -- flags to HeapAlloc
149 DWORD WINAPI
AllocateAndGetIpAddrTableFromStack(PMIB_IPADDRTABLE
*ppIpAddrTable
,
150 BOOL bOrder
, HANDLE heap
, DWORD flags
)
154 TRACE("ppIpAddrTable %p, bOrder %ld, heap 0x%08lx, flags 0x%08lx\n",
155 ppIpAddrTable
, (DWORD
)bOrder
, (DWORD
)heap
, flags
);
157 ret
= ERROR_INVALID_PARAMETER
;
161 *ppIpAddrTable
= NULL
;
162 ret
= GetIpAddrTable(*ppIpAddrTable
, &dwSize
, bOrder
);
163 if (ret
== ERROR_INSUFFICIENT_BUFFER
) {
164 *ppIpAddrTable
= (PMIB_IPADDRTABLE
)HeapAlloc(heap
, flags
, dwSize
);
165 ret
= GetIpAddrTable(*ppIpAddrTable
, &dwSize
, bOrder
);
166 if (ret
!= NO_ERROR
) {
167 HeapFree(heap
, flags
, *ppIpAddrTable
);
168 *ppIpAddrTable
= NULL
;
172 TRACE("returning %ld\n", ret
);
177 /******************************************************************
178 * AllocateAndGetIpForwardTableFromStack (IPHLPAPI.@)
181 * ppIpForwardTable [Out] -- pointer into which the MIB_IPFORWARDTABLE is
182 * allocated and returned.
183 * bOrder [In] -- passed to GetIfTable to order the table
184 * heap [In] -- heap from which the table is allocated
185 * flags [In] -- flags to HeapAlloc
187 * RETURNS -- ERROR_INVALID_PARAMETER if ppIfTable is NULL, whatever
188 * GetIpForwardTable returns otherwise
191 DWORD WINAPI
AllocateAndGetIpForwardTableFromStack(PMIB_IPFORWARDTABLE
*
192 ppIpForwardTable
, BOOL bOrder
, HANDLE heap
, DWORD flags
)
196 TRACE("ppIpForwardTable %p, bOrder %ld, heap 0x%08lx, flags 0x%08lx\n",
197 ppIpForwardTable
, (DWORD
)bOrder
, (DWORD
)heap
, flags
);
198 if (!ppIpForwardTable
)
199 ret
= ERROR_INVALID_PARAMETER
;
203 *ppIpForwardTable
= NULL
;
204 ret
= GetIpForwardTable(*ppIpForwardTable
, &dwSize
, bOrder
);
205 if (ret
== ERROR_INSUFFICIENT_BUFFER
) {
206 *ppIpForwardTable
= (PMIB_IPFORWARDTABLE
)HeapAlloc(heap
, flags
, dwSize
);
207 ret
= GetIpForwardTable(*ppIpForwardTable
, &dwSize
, bOrder
);
208 if (ret
!= NO_ERROR
) {
209 HeapFree(heap
, flags
, *ppIpForwardTable
);
210 *ppIpForwardTable
= NULL
;
214 TRACE("returning %ld\n", ret
);
219 /******************************************************************
220 * AllocateAndGetIpNetTableFromStack (IPHLPAPI.@)
226 * bOrder [In] -- passed to GetIpNetTable to order the table
227 * heap [In] -- heap from which the table is allocated
228 * flags [In] -- flags to HeapAlloc
235 DWORD WINAPI
AllocateAndGetIpNetTableFromStack(PMIB_IPNETTABLE
*ppIpNetTable
,
236 BOOL bOrder
, HANDLE heap
, DWORD flags
)
240 TRACE("ppIpNetTable %p, bOrder %ld, heap 0x%08lx, flags 0x%08lx\n",
241 ppIpNetTable
, (DWORD
)bOrder
, (DWORD
)heap
, flags
);
243 ret
= ERROR_INVALID_PARAMETER
;
247 *ppIpNetTable
= NULL
;
248 ret
= GetIpNetTable(*ppIpNetTable
, &dwSize
, bOrder
);
249 if (ret
== ERROR_INSUFFICIENT_BUFFER
) {
250 *ppIpNetTable
= (PMIB_IPNETTABLE
)HeapAlloc(heap
, flags
, dwSize
);
251 ret
= GetIpNetTable(*ppIpNetTable
, &dwSize
, bOrder
);
252 if (ret
!= NO_ERROR
) {
253 HeapFree(heap
, flags
, *ppIpNetTable
);
254 *ppIpNetTable
= NULL
;
258 TRACE("returning %ld\n", ret
);
263 /******************************************************************
264 * AllocateAndGetTcpTableFromStack (IPHLPAPI.@)
270 * bOrder [In] -- passed to GetTcpTable to order the table
271 * heap [In] -- heap from which the table is allocated
272 * flags [In] -- flags to HeapAlloc
279 DWORD WINAPI
AllocateAndGetTcpTableFromStack(PMIB_TCPTABLE
*ppTcpTable
,
280 BOOL bOrder
, HANDLE heap
, DWORD flags
)
284 TRACE("ppTcpTable %p, bOrder %ld, heap 0x%08lx, flags 0x%08lx\n",
285 ppTcpTable
, (DWORD
)bOrder
, (DWORD
)heap
, flags
);
287 ret
= ERROR_INVALID_PARAMETER
;
292 ret
= GetTcpTable(*ppTcpTable
, &dwSize
, bOrder
);
293 if (ret
== ERROR_INSUFFICIENT_BUFFER
) {
294 *ppTcpTable
= (PMIB_TCPTABLE
)HeapAlloc(heap
, flags
, dwSize
);
295 ret
= GetTcpTable(*ppTcpTable
, &dwSize
, bOrder
);
296 if (ret
!= NO_ERROR
) {
297 HeapFree(heap
, flags
, *ppTcpTable
);
302 TRACE("returning %ld\n", ret
);
307 /******************************************************************
308 * AllocateAndGetTcpExTableFromStack (IPHLPAPI.@)
314 * bOrder [In] -- passed to GetExtendedTcpTable to order the table
315 * heap [In] -- heap from which the table is allocated
316 * flags [In] -- flags to HeapAlloc
317 * family [In] -- passed to GetExtendedTcpTable to select INET family
324 DWORD WINAPI
AllocateAndGetTcpExTableFromStack(PVOID
*ppTcpTable
,
325 BOOL bOrder
, HANDLE heap
, DWORD flags
, DWORD family
)
329 TRACE("ppTcpTable %p, bOrder %ld, heap 0x%08lx, flags 0x%08lx, family 0x%08lx\n",
330 ppTcpTable
, (DWORD
)bOrder
, (DWORD
)heap
, flags
, family
);
332 ret
= ERROR_INVALID_PARAMETER
;
337 ret
= GetExtendedTcpTable(*ppTcpTable
, &dwSize
, bOrder
, family
, TCP_TABLE_OWNER_PID_ALL
, 0);
338 if (ret
== ERROR_INSUFFICIENT_BUFFER
) {
339 *ppTcpTable
= (PMIB_TCPTABLE_OWNER_PID
)HeapAlloc(heap
, flags
, dwSize
);
340 ret
= GetExtendedTcpTable(*ppTcpTable
, &dwSize
, bOrder
, family
, TCP_TABLE_OWNER_PID_ALL
, 0);
341 if (ret
!= NO_ERROR
) {
342 HeapFree(heap
, flags
, *ppTcpTable
);
347 TRACE("returning %ld\n", ret
);
352 /******************************************************************
353 * AllocateAndGetTcpExTable2FromStack (IPHLPAPI.@)
359 * bOrder [In] -- passed to GetExtendedTcpTable to order the table
360 * heap [In] -- heap from which the table is allocated
361 * flags [In] -- flags to HeapAlloc
362 * family [In] -- passed to GetExtendedTcpTable to select INET family
363 * class [In] -- passed to GetExtendedTcpTable to select information
370 DWORD WINAPI
AllocateAndGetTcpExTable2FromStack(PVOID
*ppTcpTable
,
371 BOOL bOrder
, HANDLE heap
, DWORD flags
, DWORD family
, TCP_TABLE_CLASS
class)
375 TRACE("ppTcpTable %p, bOrder %ld, heap 0x%08lx, flags 0x%08lx, family %ld, class %ld\n",
376 ppTcpTable
, (DWORD
)bOrder
, (DWORD
)heap
, flags
, family
, class);
378 ret
= ERROR_INVALID_PARAMETER
;
383 ret
= GetExtendedTcpTable(*ppTcpTable
, &dwSize
, bOrder
, family
, class, 0);
384 if (ret
== ERROR_INSUFFICIENT_BUFFER
) {
385 *ppTcpTable
= HeapAlloc(heap
, flags
, dwSize
);
386 ret
= GetExtendedTcpTable(*ppTcpTable
, &dwSize
, bOrder
, family
, class, 0);
387 if (ret
!= NO_ERROR
) {
388 HeapFree(heap
, flags
, *ppTcpTable
);
393 TRACE("returning %ld\n", ret
);
398 /******************************************************************
399 * AllocateAndGetUdpTableFromStack (IPHLPAPI.@)
405 * bOrder [In] -- passed to GetUdpTable to order the table
406 * heap [In] -- heap from which the table is allocated
407 * flags [In] -- flags to HeapAlloc
414 DWORD WINAPI
AllocateAndGetUdpTableFromStack(PMIB_UDPTABLE
*ppUdpTable
,
415 BOOL bOrder
, HANDLE heap
, DWORD flags
)
419 TRACE("ppUdpTable %p, bOrder %ld, heap 0x%08lx, flags 0x%08lx\n",
420 ppUdpTable
, (DWORD
)bOrder
, (DWORD
)heap
, flags
);
422 ret
= ERROR_INVALID_PARAMETER
;
427 ret
= GetUdpTable(*ppUdpTable
, &dwSize
, bOrder
);
428 if (ret
== ERROR_INSUFFICIENT_BUFFER
) {
429 *ppUdpTable
= (PMIB_UDPTABLE
)HeapAlloc(heap
, flags
, dwSize
);
430 ret
= GetUdpTable(*ppUdpTable
, &dwSize
, bOrder
);
431 if (ret
!= NO_ERROR
) {
432 HeapFree(heap
, flags
, *ppUdpTable
);
437 TRACE("returning %ld\n", ret
);
442 /******************************************************************
443 * AllocateAndGetUdpExTableFromStack (IPHLPAPI.@)
449 * bOrder [In] -- passed to GetExtendedUdpTable to order the table
450 * heap [In] -- heap from which the table is allocated
451 * flags [In] -- flags to HeapAlloc
452 * family [In] -- passed to GetExtendedUdpTable to select INET family
459 DWORD WINAPI
AllocateAndGetUdpExTableFromStack(PVOID
*ppUdpTable
,
460 BOOL bOrder
, HANDLE heap
, DWORD flags
, DWORD family
)
464 TRACE("ppUdpTable %p, bOrder %ld, heap 0x%08lx, flags 0x%08lx, family 0x%08lx\n",
465 ppUdpTable
, (DWORD
)bOrder
, (DWORD
)heap
, flags
, family
);
467 ret
= ERROR_INVALID_PARAMETER
;
472 ret
= GetExtendedUdpTable(*ppUdpTable
, &dwSize
, bOrder
, family
, UDP_TABLE_OWNER_PID
, 0);
473 if (ret
== ERROR_INSUFFICIENT_BUFFER
) {
474 *ppUdpTable
= (PMIB_UDPTABLE_OWNER_PID
)HeapAlloc(heap
, flags
, dwSize
);
475 ret
= GetExtendedUdpTable(*ppUdpTable
, &dwSize
, bOrder
, family
, UDP_TABLE_OWNER_PID
, 0);
476 if (ret
!= NO_ERROR
) {
477 HeapFree(heap
, flags
, *ppUdpTable
);
482 TRACE("returning %ld\n", ret
);
487 /******************************************************************
488 * AllocateAndGetUdpExTable2FromStack (IPHLPAPI.@)
494 * bOrder [In] -- passed to GetExtendedUdpTable to order the table
495 * heap [In] -- heap from which the table is allocated
496 * flags [In] -- flags to HeapAlloc
497 * family [In] -- passed to GetExtendedUdpTable to select INET family
498 * class [In] -- passed to GetExtendedUdpTable to select information
505 DWORD WINAPI
AllocateAndGetUdpExTable2FromStack(PVOID
*ppUdpTable
,
506 BOOL bOrder
, HANDLE heap
, DWORD flags
, DWORD family
, UDP_TABLE_CLASS
class)
510 TRACE("ppUdpTable %p, bOrder %ld, heap 0x%08lx, flags 0x%08lx, family %ld, class %ld\n",
511 ppUdpTable
, (DWORD
)bOrder
, (DWORD
)heap
, flags
, family
, class);
513 ret
= ERROR_INVALID_PARAMETER
;
518 ret
= GetExtendedUdpTable(*ppUdpTable
, &dwSize
, bOrder
, family
, class, 0);
519 if (ret
== ERROR_INSUFFICIENT_BUFFER
) {
520 *ppUdpTable
= HeapAlloc(heap
, flags
, dwSize
);
521 ret
= GetExtendedUdpTable(*ppUdpTable
, &dwSize
, bOrder
, family
, class, 0);
522 if (ret
!= NO_ERROR
) {
523 HeapFree(heap
, flags
, *ppUdpTable
);
528 TRACE("returning %ld\n", ret
);
533 /******************************************************************
534 * CreateIpForwardEntry (IPHLPAPI.@)
546 DWORD WINAPI
CreateIpForwardEntry(PMIB_IPFORWARDROW pRoute
)
548 return createIpForwardEntry( pRoute
);
552 /******************************************************************
553 * CreateIpNetEntry (IPHLPAPI.@)
565 DWORD WINAPI
CreateIpNetEntry(PMIB_IPNETROW pArpEntry
)
567 TRACE("pArpEntry %p\n", pArpEntry
);
568 /* could use SIOCSARP on systems that support it, not sure I want to */
574 /******************************************************************
575 * CreateProxyArpEntry (IPHLPAPI.@)
589 DWORD WINAPI
CreateProxyArpEntry(DWORD dwAddress
, DWORD dwMask
, DWORD dwIfIndex
)
591 TRACE("dwAddress 0x%08lx, dwMask 0x%08lx, dwIfIndex 0x%08lx\n", dwAddress
,
594 /* marking Win2K+ functions not supported */
595 return ERROR_NOT_SUPPORTED
;
599 /******************************************************************
600 * DeleteIPAddress (IPHLPAPI.@)
612 DWORD WINAPI
DeleteIPAddress(ULONG NTEContext
)
614 TRACE("NTEContext %ld\n", NTEContext
);
615 return RtlNtStatusToDosError(deleteIpAddress(NTEContext
));
619 /******************************************************************
620 * DeleteIpForwardEntry (IPHLPAPI.@)
632 DWORD WINAPI
DeleteIpForwardEntry(PMIB_IPFORWARDROW pRoute
)
634 return deleteIpForwardEntry( pRoute
);
638 /******************************************************************
639 * DeleteIpNetEntry (IPHLPAPI.@)
651 DWORD WINAPI
DeleteIpNetEntry(PMIB_IPNETROW pArpEntry
)
653 TRACE("pArpEntry %p\n", pArpEntry
);
654 /* could use SIOCDARP on systems that support it, not sure I want to */
660 /******************************************************************
661 * DeleteProxyArpEntry (IPHLPAPI.@)
675 DWORD WINAPI
DeleteProxyArpEntry(DWORD dwAddress
, DWORD dwMask
, DWORD dwIfIndex
)
677 TRACE("dwAddress 0x%08lx, dwMask 0x%08lx, dwIfIndex 0x%08lx\n", dwAddress
,
680 /* marking Win2K+ functions not supported */
681 return ERROR_NOT_SUPPORTED
;
684 /******************************************************************
685 * EnableRouter (IPHLPAPI.@)
691 * pOverlapped [In/Out]
698 DWORD WINAPI
EnableRouter(HANDLE
* pHandle
, OVERLAPPED
* pOverlapped
)
700 TRACE("pHandle %p, pOverlapped %p\n", pHandle
, pOverlapped
);
702 /* could echo "1" > /proc/net/sys/net/ipv4/ip_forward, not sure I want to
703 could map EACCESS to ERROR_ACCESS_DENIED, I suppose
704 marking Win2K+ functions not supported */
705 return ERROR_NOT_SUPPORTED
;
709 /******************************************************************
710 * FlushIpNetTable (IPHLPAPI.@)
722 DWORD WINAPI
FlushIpNetTable(DWORD dwIfIndex
)
724 TRACE("dwIfIndex 0x%08lx\n", dwIfIndex
);
726 /* this flushes the arp cache of the given index
727 marking Win2K+ functions not supported */
728 return ERROR_NOT_SUPPORTED
;
732 /******************************************************************
733 * GetAdapterIndex (IPHLPAPI.@)
738 * AdapterName [In/Out]
746 DWORD WINAPI
GetAdapterIndex(LPWSTR AdapterName
, PULONG IfIndex
)
748 TRACE("AdapterName %p, IfIndex %p\n", AdapterName
, IfIndex
);
750 /* marking Win2K+ functions not supported */
751 return ERROR_NOT_SUPPORTED
;
755 /******************************************************************
756 * GetAdaptersInfo (IPHLPAPI.@)
761 * pAdapterInfo [In/Out]
762 * pOutBufLen [In/Out]
769 DWORD WINAPI
GetAdaptersInfo(PIP_ADAPTER_INFO pAdapterInfo
, PULONG pOutBufLen
)
775 TRACE("pAdapterInfo %p, pOutBufLen %p\n", pAdapterInfo
, pOutBufLen
);
777 ret
= ERROR_INVALID_PARAMETER
;
779 DWORD numNonLoopbackInterfaces
= getNumNonLoopbackInterfaces();
781 if (numNonLoopbackInterfaces
> 0) {
782 /* this calculation assumes only one address in the IP_ADDR_STRING lists.
783 that's okay, because:
784 - we don't get multiple addresses per adapter anyway
785 - we don't know about per-adapter gateways
786 - DHCP and WINS servers can have max one entry per list */
787 ULONG size
= sizeof(IP_ADAPTER_INFO
) * numNonLoopbackInterfaces
;
789 if (!pAdapterInfo
|| *pOutBufLen
< size
) {
791 ret
= ERROR_BUFFER_OVERFLOW
;
794 InterfaceIndexTable
*table
= getNonLoopbackInterfaceIndexTable();
797 size
= sizeof(IP_ADAPTER_INFO
) * table
->numIndexes
;
798 if (*pOutBufLen
< size
) {
800 ret
= ERROR_INSUFFICIENT_BUFFER
;
805 BOOL winsEnabled
= FALSE
;
806 IP_ADDRESS_STRING primaryWINS
, secondaryWINS
;
808 memset(pAdapterInfo
, 0, size
);
809 if (RegOpenKeyExA(HKEY_LOCAL_MACHINE
,
810 "Software\\Wine\\Wine\\Config\\Network", 0, KEY_READ
,
811 &hKey
) == ERROR_SUCCESS
) {
812 DWORD size
= sizeof(primaryWINS
.String
);
815 RegQueryValueExA(hKey
, "WinsServer", NULL
, NULL
,
816 (PBYTE
)primaryWINS
.String
, &size
);
817 addr
= inet_addr(primaryWINS
.String
);
818 if (addr
!= INADDR_NONE
&& addr
!= INADDR_ANY
)
820 size
= sizeof(secondaryWINS
.String
);
821 RegQueryValueExA(hKey
, "BackupWinsServer", NULL
, NULL
,
822 (PBYTE
)secondaryWINS
.String
, &size
);
823 addr
= inet_addr(secondaryWINS
.String
);
824 if (addr
!= INADDR_NONE
&& addr
!= INADDR_ANY
)
828 TRACE("num of index is %lu\n", table
->numIndexes
);
829 for (ndx
= 0; ndx
< table
->numIndexes
; ndx
++) {
830 PIP_ADAPTER_INFO ptr
= &pAdapterInfo
[ndx
];
831 DWORD addrLen
= sizeof(ptr
->Address
), type
;
833 getInterfaceNameByIndex(table
->indexes
[ndx
]);
835 ret
= ERROR_OUTOFMEMORY
;
839 /* on Win98 this is left empty, but whatever */
840 strncpy(ptr
->AdapterName
,ifname
,sizeof(ptr
->AdapterName
));
841 consumeInterfaceName(ifname
);
842 ptr
->AdapterName
[MAX_ADAPTER_NAME_LENGTH
] = '\0';
843 getInterfacePhysicalByIndex(table
->indexes
[ndx
], &addrLen
,
844 ptr
->Address
, &type
);
845 /* MS defines address length and type as UINT in some places and
846 DWORD in others, **sigh**. Don't want to assume that PUINT and
847 PDWORD are equiv (64-bit?) */
848 ptr
->AddressLength
= addrLen
;
850 ptr
->Index
= table
->indexes
[ndx
];
851 toIPAddressString(getInterfaceIPAddrByIndex(table
->indexes
[ndx
]),
852 ptr
->IpAddressList
.IpAddress
.String
);
853 toIPAddressString(getInterfaceMaskByIndex(table
->indexes
[ndx
]),
854 ptr
->IpAddressList
.IpMask
.String
);
855 ptr
->IpAddressList
.Context
= ptr
->Index
;
856 toIPAddressString(getInterfaceGatewayByIndex(table
->indexes
[ndx
]),
857 ptr
->GatewayList
.IpAddress
.String
);
858 getDhcpInfoForAdapter(table
->indexes
[ndx
], &dhcpEnabled
,
859 &dhcpServer
, &ptr
->LeaseObtained
,
861 ptr
->DhcpEnabled
= (DWORD
) dhcpEnabled
;
862 toIPAddressString(dhcpServer
,
863 ptr
->DhcpServer
.IpAddress
.String
);
865 ptr
->HaveWins
= TRUE
;
866 memcpy(ptr
->PrimaryWinsServer
.IpAddress
.String
,
867 primaryWINS
.String
, sizeof(primaryWINS
.String
));
868 memcpy(ptr
->SecondaryWinsServer
.IpAddress
.String
,
869 secondaryWINS
.String
, sizeof(secondaryWINS
.String
));
871 if (ndx
< table
->numIndexes
- 1)
872 ptr
->Next
= &pAdapterInfo
[ndx
+ 1];
881 ret
= ERROR_OUTOFMEMORY
;
887 TRACE("returning %ld\n", ret
);
892 /******************************************************************
893 * GetBestInterface (IPHLPAPI.@)
899 * pdwBestIfIndex [In/Out]
906 DWORD WINAPI
GetBestInterface(IPAddr dwDestAddr
, PDWORD pdwBestIfIndex
)
910 TRACE("dwDestAddr 0x%08lx, pdwBestIfIndex %p\n", dwDestAddr
, pdwBestIfIndex
);
912 ret
= ERROR_INVALID_PARAMETER
;
914 MIB_IPFORWARDROW ipRow
;
916 ret
= GetBestRoute(dwDestAddr
, 0, &ipRow
);
917 if (ret
== ERROR_SUCCESS
)
918 *pdwBestIfIndex
= ipRow
.dwForwardIfIndex
;
920 TRACE("returning %ld\n", ret
);
925 /******************************************************************
926 * GetBestRoute (IPHLPAPI.@)
940 DWORD WINAPI
GetBestRoute(DWORD dwDestAddr
, DWORD dwSourceAddr
, PMIB_IPFORWARDROW pBestRoute
)
942 PMIB_IPFORWARDTABLE table
;
945 TRACE("dwDestAddr 0x%08lx, dwSourceAddr 0x%08lx, pBestRoute %p\n", dwDestAddr
,
946 dwSourceAddr
, pBestRoute
);
948 return ERROR_INVALID_PARAMETER
;
950 AllocateAndGetIpForwardTableFromStack(&table
, FALSE
, GetProcessHeap(), 0);
952 DWORD ndx
, minMaskSize
, matchedNdx
= 0;
954 for (ndx
= 0, minMaskSize
= 255; ndx
< table
->dwNumEntries
; ndx
++) {
955 if ((dwDestAddr
& table
->table
[ndx
].dwForwardMask
) ==
956 (table
->table
[ndx
].dwForwardDest
& table
->table
[ndx
].dwForwardMask
)) {
959 if (!_BitScanForward(&hostMaskSize
, ntohl(table
->table
[ndx
].dwForwardMask
)))
963 if (hostMaskSize
< minMaskSize
) {
964 minMaskSize
= hostMaskSize
;
969 memcpy(pBestRoute
, &table
->table
[matchedNdx
], sizeof(MIB_IPFORWARDROW
));
970 HeapFree(GetProcessHeap(), 0, table
);
974 ret
= ERROR_OUTOFMEMORY
;
975 TRACE("returning %ld\n", ret
);
979 static int TcpTableSorter(const void *a
, const void *b
)
984 PMIB_TCPROW rowA
= (PMIB_TCPROW
)a
, rowB
= (PMIB_TCPROW
)b
;
986 ret
= rowA
->dwLocalAddr
- rowB
->dwLocalAddr
;
988 ret
= rowA
->dwLocalPort
- rowB
->dwLocalPort
;
990 ret
= rowA
->dwRemoteAddr
- rowB
->dwRemoteAddr
;
992 ret
= rowA
->dwRemotePort
- rowB
->dwRemotePort
;
1001 /******************************************************************
1002 * GetExtendedTcpTable (IPHLPAPI.@)
1004 * Get the table of TCP endpoints available to the application.
1007 * pTcpTable [Out] table struct with the filtered TCP endpoints available to application
1008 * pdwSize [In/Out] estimated size of the structure returned in pTcpTable, in bytes
1009 * bOrder [In] whether to order the table
1010 * ulAf [in] version of IP used by the TCP endpoints
1011 * TableClass [in] type of the TCP table structure from TCP_TABLE_CLASS
1012 * Reserved [in] reserved - this value must be zero
1016 * Failure: either ERROR_INSUFFICIENT_BUFFER or ERROR_INVALID_PARAMETER
1021 DWORD WINAPI
GetExtendedTcpTable(PVOID pTcpTable
, PDWORD pdwSize
, BOOL bOrder
, ULONG ulAf
, TCP_TABLE_CLASS TableClass
, ULONG Reserved
)
1024 DWORD ret
= NO_ERROR
;
1028 return ERROR_INVALID_PARAMETER
;
1031 if (ulAf
!= AF_INET
)
1034 return ERROR_INVALID_PARAMETER
;
1039 case TCP_TABLE_BASIC_ALL
:
1041 PMIB_TCPTABLE pOurTcpTable
= getTcpTable();
1042 PMIB_TCPTABLE pTheirTcpTable
= pTcpTable
;
1046 if (sizeof(DWORD
) + pOurTcpTable
->dwNumEntries
* sizeof(MIB_TCPROW
) > *pdwSize
|| !pTheirTcpTable
)
1048 *pdwSize
= sizeof(DWORD
) + pOurTcpTable
->dwNumEntries
* sizeof(MIB_TCPROW
);
1049 ret
= ERROR_INSUFFICIENT_BUFFER
;
1053 memcpy(pTheirTcpTable
, pOurTcpTable
, sizeof(DWORD
) + pOurTcpTable
->dwNumEntries
* sizeof(MIB_TCPROW
));
1056 qsort(pTheirTcpTable
->table
, pTheirTcpTable
->dwNumEntries
,
1057 sizeof(MIB_TCPROW
), TcpTableSorter
);
1065 case TCP_TABLE_BASIC_CONNECTIONS
:
1067 PMIB_TCPTABLE pOurTcpTable
= getTcpTable();
1068 PMIB_TCPTABLE pTheirTcpTable
= pTcpTable
;
1072 for (i
= 0, count
= 0; i
< pOurTcpTable
->dwNumEntries
; ++i
)
1074 if (pOurTcpTable
->table
[i
].State
!= MIB_TCP_STATE_LISTEN
)
1080 if (sizeof(DWORD
) + count
* sizeof(MIB_TCPROW
) > *pdwSize
|| !pTheirTcpTable
)
1082 *pdwSize
= sizeof(DWORD
) + count
* sizeof(MIB_TCPROW
);
1083 ret
= ERROR_INSUFFICIENT_BUFFER
;
1087 pTheirTcpTable
->dwNumEntries
= count
;
1089 for (i
= 0, count
= 0; i
< pOurTcpTable
->dwNumEntries
; ++i
)
1091 if (pOurTcpTable
->table
[i
].State
!= MIB_TCP_STATE_LISTEN
)
1093 memcpy(&pTheirTcpTable
->table
[count
], &pOurTcpTable
->table
[i
], sizeof(MIB_TCPROW
));
1097 ASSERT(count
== pTheirTcpTable
->dwNumEntries
);
1100 qsort(pTheirTcpTable
->table
, pTheirTcpTable
->dwNumEntries
,
1101 sizeof(MIB_TCPROW
), TcpTableSorter
);
1109 case TCP_TABLE_BASIC_LISTENER
:
1111 PMIB_TCPTABLE pOurTcpTable
= getTcpTable();
1112 PMIB_TCPTABLE pTheirTcpTable
= pTcpTable
;
1116 for (i
= 0, count
= 0; i
< pOurTcpTable
->dwNumEntries
; ++i
)
1118 if (pOurTcpTable
->table
[i
].State
== MIB_TCP_STATE_LISTEN
)
1124 if (sizeof(DWORD
) + count
* sizeof(MIB_TCPROW
) > *pdwSize
|| !pTheirTcpTable
)
1126 *pdwSize
= sizeof(DWORD
) + count
* sizeof(MIB_TCPROW
);
1127 ret
= ERROR_INSUFFICIENT_BUFFER
;
1131 pTheirTcpTable
->dwNumEntries
= count
;
1133 for (i
= 0, count
= 0; i
< pOurTcpTable
->dwNumEntries
; ++i
)
1135 if (pOurTcpTable
->table
[i
].State
== MIB_TCP_STATE_LISTEN
)
1137 memcpy(&pTheirTcpTable
->table
[count
], &pOurTcpTable
->table
[i
], sizeof(MIB_TCPROW
));
1141 ASSERT(count
== pTheirTcpTable
->dwNumEntries
);
1144 qsort(pTheirTcpTable
->table
, pTheirTcpTable
->dwNumEntries
,
1145 sizeof(MIB_TCPROW
), TcpTableSorter
);
1153 case TCP_TABLE_OWNER_PID_ALL
:
1155 PMIB_TCPTABLE_OWNER_PID pOurTcpTable
= getOwnerTcpTable();
1156 PMIB_TCPTABLE_OWNER_PID pTheirTcpTable
= pTcpTable
;
1160 if (sizeof(DWORD
) + pOurTcpTable
->dwNumEntries
* sizeof(MIB_TCPROW_OWNER_PID
) > *pdwSize
|| !pTheirTcpTable
)
1162 *pdwSize
= sizeof(DWORD
) + pOurTcpTable
->dwNumEntries
* sizeof(MIB_TCPROW_OWNER_PID
);
1163 ret
= ERROR_INSUFFICIENT_BUFFER
;
1167 memcpy(pTheirTcpTable
, pOurTcpTable
, sizeof(DWORD
) + pOurTcpTable
->dwNumEntries
* sizeof(MIB_TCPROW_OWNER_PID
));
1169 /* Don't sort on PID, so use basic helper */
1171 qsort(pTheirTcpTable
->table
, pTheirTcpTable
->dwNumEntries
,
1172 sizeof(MIB_TCPROW_OWNER_PID
), TcpTableSorter
);
1180 case TCP_TABLE_OWNER_PID_CONNECTIONS
:
1182 PMIB_TCPTABLE_OWNER_PID pOurTcpTable
= getOwnerTcpTable();
1183 PMIB_TCPTABLE_OWNER_PID pTheirTcpTable
= pTcpTable
;
1187 for (i
= 0, count
= 0; i
< pOurTcpTable
->dwNumEntries
; ++i
)
1189 if (pOurTcpTable
->table
[i
].dwState
!= MIB_TCP_STATE_LISTEN
)
1195 if (sizeof(DWORD
) + count
* sizeof(MIB_TCPROW_OWNER_PID
) > *pdwSize
|| !pTheirTcpTable
)
1197 *pdwSize
= sizeof(DWORD
) + count
* sizeof(MIB_TCPROW_OWNER_PID
);
1198 ret
= ERROR_INSUFFICIENT_BUFFER
;
1202 pTheirTcpTable
->dwNumEntries
= count
;
1204 for (i
= 0, count
= 0; i
< pOurTcpTable
->dwNumEntries
; ++i
)
1206 if (pOurTcpTable
->table
[i
].dwState
!= MIB_TCP_STATE_LISTEN
)
1208 memcpy(&pTheirTcpTable
->table
[count
], &pOurTcpTable
->table
[i
], sizeof(MIB_TCPROW_OWNER_PID
));
1212 ASSERT(count
== pTheirTcpTable
->dwNumEntries
);
1214 /* Don't sort on PID, so use basic helper */
1216 qsort(pTheirTcpTable
->table
, pTheirTcpTable
->dwNumEntries
,
1217 sizeof(MIB_TCPROW_OWNER_PID
), TcpTableSorter
);
1225 case TCP_TABLE_OWNER_PID_LISTENER
:
1227 PMIB_TCPTABLE_OWNER_PID pOurTcpTable
= getOwnerTcpTable();
1228 PMIB_TCPTABLE_OWNER_PID pTheirTcpTable
= pTcpTable
;
1232 for (i
= 0, count
= 0; i
< pOurTcpTable
->dwNumEntries
; ++i
)
1234 if (pOurTcpTable
->table
[i
].dwState
== MIB_TCP_STATE_LISTEN
)
1240 if (sizeof(DWORD
) + count
* sizeof(MIB_TCPROW_OWNER_PID
) > *pdwSize
|| !pTheirTcpTable
)
1242 *pdwSize
= sizeof(DWORD
) + count
* sizeof(MIB_TCPROW_OWNER_PID
);
1243 ret
= ERROR_INSUFFICIENT_BUFFER
;
1247 pTheirTcpTable
->dwNumEntries
= count
;
1249 for (i
= 0, count
= 0; i
< pOurTcpTable
->dwNumEntries
; ++i
)
1251 if (pOurTcpTable
->table
[i
].dwState
== MIB_TCP_STATE_LISTEN
)
1253 memcpy(&pTheirTcpTable
->table
[count
], &pOurTcpTable
->table
[i
], sizeof(MIB_TCPROW_OWNER_PID
));
1257 ASSERT(count
== pTheirTcpTable
->dwNumEntries
);
1259 /* Don't sort on PID, so use basic helper */
1261 qsort(pTheirTcpTable
->table
, pTheirTcpTable
->dwNumEntries
,
1262 sizeof(MIB_TCPROW_OWNER_PID
), TcpTableSorter
);
1272 ret
= ERROR_INVALID_PARAMETER
;
1280 static int UdpTableSorter(const void *a
, const void *b
)
1285 PMIB_UDPROW rowA
= (PMIB_UDPROW
)a
, rowB
= (PMIB_UDPROW
)b
;
1287 ret
= rowA
->dwLocalAddr
- rowB
->dwLocalAddr
;
1289 ret
= rowA
->dwLocalPort
- rowB
->dwLocalPort
;
1296 /******************************************************************
1297 * GetExtendedUdpTable (IPHLPAPI.@)
1299 * Get the table of UDP endpoints available to the application.
1302 * pUdpTable [Out] table struct with the filtered UDP endpoints available to application
1303 * pdwSize [In/Out] estimated size of the structure returned in pUdpTable, in bytes
1304 * bOrder [In] whether to order the table
1305 * ulAf [in] version of IP used by the UDP endpoints
1306 * TableClass [in] type of the UDP table structure from UDP_TABLE_CLASS
1307 * Reserved [in] reserved - this value must be zero
1311 * Failure: either ERROR_INSUFFICIENT_BUFFER or ERROR_INVALID_PARAMETER
1316 DWORD WINAPI
GetExtendedUdpTable(PVOID pUdpTable
, PDWORD pdwSize
, BOOL bOrder
, ULONG ulAf
, UDP_TABLE_CLASS TableClass
, ULONG Reserved
)
1318 DWORD ret
= NO_ERROR
;
1322 return ERROR_INVALID_PARAMETER
;
1325 if (ulAf
!= AF_INET
)
1328 return ERROR_INVALID_PARAMETER
;
1333 case UDP_TABLE_BASIC
:
1335 PMIB_UDPTABLE pOurUdpTable
= getUdpTable();
1336 PMIB_UDPTABLE pTheirUdpTable
= pUdpTable
;
1340 if (sizeof(DWORD
) + pOurUdpTable
->dwNumEntries
* sizeof(MIB_UDPROW
) > *pdwSize
|| !pTheirUdpTable
)
1342 *pdwSize
= sizeof(DWORD
) + pOurUdpTable
->dwNumEntries
* sizeof(MIB_UDPROW
);
1343 ret
= ERROR_INSUFFICIENT_BUFFER
;
1347 memcpy(pTheirUdpTable
, pOurUdpTable
, sizeof(DWORD
) + pOurUdpTable
->dwNumEntries
* sizeof(MIB_UDPROW_OWNER_PID
));
1350 qsort(pTheirUdpTable
->table
, pTheirUdpTable
->dwNumEntries
,
1351 sizeof(MIB_UDPROW
), UdpTableSorter
);
1359 case UDP_TABLE_OWNER_PID
:
1361 PMIB_UDPTABLE_OWNER_PID pOurUdpTable
= getOwnerUdpTable();
1362 PMIB_UDPTABLE_OWNER_PID pTheirUdpTable
= pUdpTable
;
1366 if (sizeof(DWORD
) + pOurUdpTable
->dwNumEntries
* sizeof(MIB_UDPROW_OWNER_PID
) > *pdwSize
|| !pTheirUdpTable
)
1368 *pdwSize
= sizeof(DWORD
) + pOurUdpTable
->dwNumEntries
* sizeof(MIB_UDPROW_OWNER_PID
);
1369 ret
= ERROR_INSUFFICIENT_BUFFER
;
1373 memcpy(pTheirUdpTable
, pOurUdpTable
, sizeof(DWORD
) + pOurUdpTable
->dwNumEntries
* sizeof(MIB_UDPROW_OWNER_PID
));
1376 qsort(pTheirUdpTable
->table
, pTheirUdpTable
->dwNumEntries
,
1377 sizeof(MIB_UDPROW_OWNER_PID
), UdpTableSorter
);
1387 ret
= ERROR_INVALID_PARAMETER
;
1395 /******************************************************************
1396 * GetFriendlyIfIndex (IPHLPAPI.@)
1408 DWORD WINAPI
GetFriendlyIfIndex(DWORD IfIndex
)
1410 /* windows doesn't validate these, either, just makes sure the top byte is
1411 cleared. I assume my ifenum module never gives an index with the top
1413 TRACE("returning %ld\n", IfIndex
);
1418 /******************************************************************
1419 * GetIcmpStatistics (IPHLPAPI.@)
1431 DWORD WINAPI
GetIcmpStatistics(PMIB_ICMP pStats
)
1435 TRACE("pStats %p\n", pStats
);
1436 ret
= getICMPStats(pStats
);
1437 TRACE("returning %ld\n", ret
);
1442 /******************************************************************
1443 * GetIfEntry (IPHLPAPI.@)
1455 DWORD WINAPI
GetIfEntry(PMIB_IFROW pIfRow
)
1460 TRACE("pIfRow %p\n", pIfRow
);
1462 return ERROR_INVALID_PARAMETER
;
1464 name
= getInterfaceNameByIndex(pIfRow
->dwIndex
);
1466 ret
= getInterfaceEntryByIndex(pIfRow
->dwIndex
, pIfRow
);
1467 if (ret
== NO_ERROR
)
1468 ret
= getInterfaceStatsByName(name
, pIfRow
);
1469 consumeInterfaceName(name
);
1472 ret
= ERROR_INVALID_DATA
;
1473 TRACE("returning %ld\n", ret
);
1478 static int IfTableSorter(const void *a
, const void *b
)
1483 ret
= ((PMIB_IFROW
)a
)->dwIndex
- ((PMIB_IFROW
)b
)->dwIndex
;
1490 /******************************************************************
1491 * GetIfTable (IPHLPAPI.@)
1505 DWORD WINAPI
GetIfTable(PMIB_IFTABLE pIfTable
, PULONG pdwSize
, BOOL bOrder
)
1509 TRACE("pIfTable %p, pdwSize %p, bOrder %ld\n", pdwSize
, pdwSize
,
1512 ret
= ERROR_INVALID_PARAMETER
;
1514 DWORD numInterfaces
= getNumInterfaces();
1516 TRACE("GetIfTable: numInterfaces = %d\n", (int)numInterfaces
);
1517 size
= sizeof(MIB_IFTABLE
) + (numInterfaces
- 1) * sizeof(MIB_IFROW
);
1519 if (!pIfTable
|| *pdwSize
< size
) {
1521 ret
= ERROR_INSUFFICIENT_BUFFER
;
1524 InterfaceIndexTable
*table
= getInterfaceIndexTable();
1527 size
= sizeof(MIB_IFTABLE
) + (table
->numIndexes
- 1) *
1529 if (*pdwSize
< size
) {
1531 ret
= ERROR_INSUFFICIENT_BUFFER
;
1536 pIfTable
->dwNumEntries
= 0;
1537 for (ndx
= 0; ndx
< table
->numIndexes
; ndx
++) {
1538 pIfTable
->table
[ndx
].dwIndex
= table
->indexes
[ndx
];
1539 GetIfEntry(&pIfTable
->table
[ndx
]);
1540 pIfTable
->dwNumEntries
++;
1543 qsort(pIfTable
->table
, pIfTable
->dwNumEntries
, sizeof(MIB_IFROW
),
1550 ret
= ERROR_OUTOFMEMORY
;
1553 TRACE("returning %ld\n", ret
);
1558 /******************************************************************
1559 * GetInterfaceInfo (IPHLPAPI.@)
1565 * dwOutBufLen [In/Out]
1572 DWORD WINAPI
GetInterfaceInfo(PIP_INTERFACE_INFO pIfTable
, PULONG dwOutBufLen
)
1576 TRACE("pIfTable %p, dwOutBufLen %p\n", pIfTable
, dwOutBufLen
);
1578 ret
= ERROR_INVALID_PARAMETER
;
1580 DWORD numNonLoopbackInterfaces
= getNumNonLoopbackInterfaces();
1582 TRACE("numNonLoopbackInterfaces == 0x%x\n", numNonLoopbackInterfaces
);
1583 size
= sizeof(IP_INTERFACE_INFO
) + (numNonLoopbackInterfaces
) *
1584 sizeof(IP_ADAPTER_INDEX_MAP
);
1586 if (!pIfTable
|| *dwOutBufLen
< size
) {
1587 *dwOutBufLen
= size
;
1588 ret
= ERROR_INSUFFICIENT_BUFFER
;
1591 InterfaceIndexTable
*table
= getNonLoopbackInterfaceIndexTable();
1594 TRACE("table->numIndexes == 0x%x\n", table
->numIndexes
);
1595 size
= sizeof(IP_INTERFACE_INFO
) + (table
->numIndexes
) *
1596 sizeof(IP_ADAPTER_INDEX_MAP
);
1597 if (*dwOutBufLen
< size
) {
1598 *dwOutBufLen
= size
;
1599 ret
= ERROR_INSUFFICIENT_BUFFER
;
1604 pIfTable
->NumAdapters
= 0;
1605 for (ndx
= 0; ndx
< table
->numIndexes
; ndx
++) {
1606 const char *walker
, *name
;
1609 pIfTable
->Adapter
[ndx
].Index
= table
->indexes
[ndx
];
1610 name
= getInterfaceNameByIndex(table
->indexes
[ndx
]);
1611 for (walker
= name
, assigner
= pIfTable
->Adapter
[ndx
].Name
;
1612 walker
&& *walker
&&
1613 assigner
- pIfTable
->Adapter
[ndx
].Name
< MAX_ADAPTER_NAME
- 1;
1614 walker
++, assigner
++)
1615 *assigner
= *walker
;
1617 consumeInterfaceName(name
);
1618 pIfTable
->NumAdapters
++;
1625 ret
= ERROR_OUTOFMEMORY
;
1628 TRACE("returning %ld\n", ret
);
1633 static int IpAddrTableSorter(const void *a
, const void *b
)
1638 ret
= ((PMIB_IPADDRROW
)a
)->dwAddr
- ((PMIB_IPADDRROW
)b
)->dwAddr
;
1645 /******************************************************************
1646 * GetIpAddrTable (IPHLPAPI.@)
1651 * pIpAddrTable [In/Out]
1660 DWORD WINAPI
GetIpAddrTable(PMIB_IPADDRTABLE pIpAddrTable
, PULONG pdwSize
, BOOL bOrder
)
1664 TRACE("pIpAddrTable %p, pdwSize %p, bOrder %ld\n", pIpAddrTable
, pdwSize
,
1667 ret
= ERROR_INVALID_PARAMETER
;
1669 DWORD numInterfaces
= getNumInterfaces();
1670 ULONG size
= sizeof(MIB_IPADDRTABLE
) + (numInterfaces
- 1) *
1671 sizeof(MIB_IPADDRROW
);
1673 if (!pIpAddrTable
|| *pdwSize
< size
) {
1675 ret
= ERROR_INSUFFICIENT_BUFFER
;
1678 InterfaceIndexTable
*table
= getInterfaceIndexTable();
1681 size
= sizeof(MIB_IPADDRTABLE
) + (table
->numIndexes
- 1) *
1682 sizeof(MIB_IPADDRROW
);
1683 if (*pdwSize
< size
) {
1685 ret
= ERROR_INSUFFICIENT_BUFFER
;
1690 pIpAddrTable
->dwNumEntries
= 0;
1691 for (ndx
= 0; ndx
< table
->numIndexes
; ndx
++) {
1692 pIpAddrTable
->table
[ndx
].dwIndex
= table
->indexes
[ndx
];
1693 pIpAddrTable
->table
[ndx
].dwAddr
=
1694 getInterfaceIPAddrByIndex(table
->indexes
[ndx
]);
1695 pIpAddrTable
->table
[ndx
].dwMask
=
1696 getInterfaceMaskByIndex(table
->indexes
[ndx
]);
1697 /* the dwBCastAddr member isn't the broadcast address, it indicates
1698 * whether the interface uses the 1's broadcast address (1) or the
1699 * 0's broadcast address (0).
1701 bcast
= getInterfaceBCastAddrByIndex(table
->indexes
[ndx
]);
1702 pIpAddrTable
->table
[ndx
].dwBCastAddr
=
1703 (bcast
& pIpAddrTable
->table
[ndx
].dwMask
) ? 1 : 0;
1704 /* FIXME: hardcoded reasm size, not sure where to get it */
1705 pIpAddrTable
->table
[ndx
].dwReasmSize
= 65535;
1706 pIpAddrTable
->table
[ndx
].unused1
= 0;
1707 pIpAddrTable
->table
[ndx
].wType
= 0; /* aka unused2 */
1708 pIpAddrTable
->dwNumEntries
++;
1711 qsort(pIpAddrTable
->table
, pIpAddrTable
->dwNumEntries
,
1712 sizeof(MIB_IPADDRROW
), IpAddrTableSorter
);
1718 ret
= ERROR_OUTOFMEMORY
;
1721 TRACE("returning %ld\n", ret
);
1726 static int IpForwardTableSorter(const void *a
, const void *b
)
1731 PMIB_IPFORWARDROW rowA
= (PMIB_IPFORWARDROW
)a
, rowB
= (PMIB_IPFORWARDROW
)b
;
1733 ret
= rowA
->dwForwardDest
- rowB
->dwForwardDest
;
1735 ret
= rowA
->dwForwardProto
- rowB
->dwForwardProto
;
1737 ret
= rowA
->dwForwardPolicy
- rowB
->dwForwardPolicy
;
1739 ret
= rowA
->dwForwardNextHop
- rowB
->dwForwardNextHop
;
1749 /******************************************************************
1750 * GetIpForwardTable (IPHLPAPI.@)
1755 * pIpForwardTable [In/Out]
1764 DWORD WINAPI
GetIpForwardTable(PMIB_IPFORWARDTABLE pIpForwardTable
, PULONG pdwSize
, BOOL bOrder
)
1768 TRACE("pIpForwardTable %p, pdwSize %p, bOrder %ld\n", pIpForwardTable
,
1769 pdwSize
, (DWORD
)bOrder
);
1771 ret
= ERROR_INVALID_PARAMETER
;
1773 DWORD numRoutes
= getNumRoutes();
1774 ULONG sizeNeeded
= sizeof(MIB_IPFORWARDTABLE
) + (numRoutes
- 1) *
1775 sizeof(MIB_IPFORWARDROW
);
1777 if (!pIpForwardTable
|| *pdwSize
< sizeNeeded
) {
1778 *pdwSize
= sizeNeeded
;
1779 ret
= ERROR_INSUFFICIENT_BUFFER
;
1782 RouteTable
*table
= getRouteTable();
1784 sizeNeeded
= sizeof(MIB_IPFORWARDTABLE
) + (table
->numRoutes
- 1) *
1785 sizeof(MIB_IPFORWARDROW
);
1786 if (*pdwSize
< sizeNeeded
) {
1787 *pdwSize
= sizeNeeded
;
1788 ret
= ERROR_INSUFFICIENT_BUFFER
;
1793 pIpForwardTable
->dwNumEntries
= table
->numRoutes
;
1794 for (ndx
= 0; ndx
< numRoutes
; ndx
++) {
1795 pIpForwardTable
->table
[ndx
].dwForwardIfIndex
=
1796 table
->routes
[ndx
].ifIndex
;
1797 pIpForwardTable
->table
[ndx
].dwForwardDest
=
1798 table
->routes
[ndx
].dest
;
1799 pIpForwardTable
->table
[ndx
].dwForwardMask
=
1800 table
->routes
[ndx
].mask
;
1801 pIpForwardTable
->table
[ndx
].dwForwardPolicy
= 0;
1802 pIpForwardTable
->table
[ndx
].dwForwardNextHop
=
1803 table
->routes
[ndx
].gateway
;
1804 /* FIXME: this type is appropriate for local interfaces; may not
1805 always be appropriate */
1806 pIpForwardTable
->table
[ndx
].dwForwardType
= MIB_IPROUTE_TYPE_DIRECT
;
1807 /* FIXME: other protos might be appropriate, e.g. the default route
1808 is typically set with MIB_IPPROTO_NETMGMT instead */
1809 pIpForwardTable
->table
[ndx
].dwForwardProto
= MIB_IPPROTO_LOCAL
;
1810 /* punt on age and AS */
1811 pIpForwardTable
->table
[ndx
].dwForwardAge
= 0;
1812 pIpForwardTable
->table
[ndx
].dwForwardNextHopAS
= 0;
1813 pIpForwardTable
->table
[ndx
].dwForwardMetric1
=
1814 table
->routes
[ndx
].metric
;
1815 /* rest of the metrics are 0.. */
1816 pIpForwardTable
->table
[ndx
].dwForwardMetric2
= 0;
1817 pIpForwardTable
->table
[ndx
].dwForwardMetric3
= 0;
1818 pIpForwardTable
->table
[ndx
].dwForwardMetric4
= 0;
1819 pIpForwardTable
->table
[ndx
].dwForwardMetric5
= 0;
1822 qsort(pIpForwardTable
->table
, pIpForwardTable
->dwNumEntries
,
1823 sizeof(MIB_IPFORWARDROW
), IpForwardTableSorter
);
1826 HeapFree(GetProcessHeap(), 0, table
);
1829 ret
= ERROR_OUTOFMEMORY
;
1832 TRACE("returning %ld\n", ret
);
1837 static int IpNetTableSorter(const void *a
, const void *b
)
1842 ret
= ((PMIB_IPNETROW
)a
)->dwAddr
- ((PMIB_IPNETROW
)b
)->dwAddr
;
1849 /******************************************************************
1850 * GetIpNetTable (IPHLPAPI.@)
1855 * pIpNetTable [In/Out]
1864 DWORD WINAPI
GetIpNetTable(PMIB_IPNETTABLE pIpNetTable
, PULONG pdwSize
, BOOL bOrder
)
1866 DWORD ret
= NO_ERROR
;
1868 TRACE("pIpNetTable %p, pdwSize %p, bOrder %d\n", pIpNetTable
, pdwSize
,
1871 ret
= ERROR_INVALID_PARAMETER
;
1873 DWORD numEntries
= getNumArpEntries();
1874 ULONG size
= sizeof(MIB_IPNETTABLE
);
1877 size
+= (numEntries
- 1) * sizeof(MIB_IPNETROW
);
1878 if (!pIpNetTable
|| *pdwSize
< size
) {
1880 ret
= ERROR_INSUFFICIENT_BUFFER
;
1883 PMIB_IPNETTABLE table
= getArpTable();
1885 size
= sizeof(MIB_IPNETTABLE
);
1886 if (table
->dwNumEntries
> 1)
1887 size
+= (table
->dwNumEntries
- 1) * sizeof(MIB_IPNETROW
);
1888 if (*pdwSize
< size
) {
1890 ret
= ERROR_INSUFFICIENT_BUFFER
;
1894 memcpy(pIpNetTable
, table
, size
);
1896 qsort(pIpNetTable
->table
, pIpNetTable
->dwNumEntries
,
1897 sizeof(MIB_IPNETROW
), IpNetTableSorter
);
1900 HeapFree(GetProcessHeap(), 0, table
);
1904 TRACE("returning %d\n", ret
);
1909 /******************************************************************
1910 * GetIpStatistics (IPHLPAPI.@)
1922 DWORD WINAPI
GetIpStatistics(PMIB_IPSTATS pStats
)
1924 return GetIpStatisticsEx(pStats
, PF_INET
);
1927 /******************************************************************
1928 * GetIpStatisticsEx (IPHLPAPI.@)
1941 DWORD WINAPI
GetIpStatisticsEx(PMIB_IPSTATS pStats
, DWORD dwFamily
)
1945 TRACE("pStats %p\n", pStats
);
1946 ret
= getIPStats(pStats
, dwFamily
);
1947 TRACE("returning %ld\n", ret
);
1951 /******************************************************************
1952 * GetNetworkParams (IPHLPAPI.@)
1957 * pFixedInfo [In/Out]
1958 * pOutBufLen [In/Out]
1965 DWORD WINAPI
GetNetworkParams(PFIXED_INFO pFixedInfo
, PULONG pOutBufLen
)
1967 DWORD ret
, size
, type
;
1970 PIPHLP_RES_INFO resInfo
;
1972 TRACE("pFixedInfo %p, pOutBufLen %p\n", pFixedInfo
, pOutBufLen
);
1974 return ERROR_INVALID_PARAMETER
;
1976 resInfo
= getResInfo();
1978 return ERROR_OUTOFMEMORY
;
1980 size
= sizeof(FIXED_INFO
) + (resInfo
->riCount
> 1 ? (resInfo
->riCount
-1) *
1981 sizeof(IP_ADDR_STRING
) : 0);
1982 if (!pFixedInfo
|| *pOutBufLen
< size
) {
1984 disposeResInfo( resInfo
);
1985 return ERROR_BUFFER_OVERFLOW
;
1988 memset(pFixedInfo
, 0, size
);
1989 /* Check for DhcpHostname and DhcpDomain first */
1990 regReturn
= RegOpenKeyExA(HKEY_LOCAL_MACHINE
,
1991 "SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters",
1995 if (regReturn
== ERROR_SUCCESS
) {
1996 /* Windows doesn't honor DHCP option 12 even if RFC requires it if it is returned by DHCP server! */
1999 size
= sizeof(pFixedInfo
->HostName
);
2000 regReturn
= RegQueryValueExA(hKey
,
2004 (LPBYTE
)pFixedInfo
->HostName
,
2006 if (regReturn
== ERROR_FILE_NOT_FOUND
|| (regReturn
== ERROR_SUCCESS
&& size
< 1))
2010 size
= sizeof(pFixedInfo
->HostName
);
2011 regReturn
= RegQueryValueExA(hKey
,
2015 (LPBYTE
)pFixedInfo
->HostName
,
2022 size
= sizeof(pFixedInfo
->DomainName
);
2023 regReturn
= RegQueryValueExA(hKey
,
2027 (LPBYTE
)pFixedInfo
->DomainName
,
2029 if (regReturn
== ERROR_FILE_NOT_FOUND
|| (regReturn
== ERROR_SUCCESS
&& size
< 1))
2032 size
= sizeof(pFixedInfo
->DomainName
);
2033 regReturn
= RegQueryValueExA(hKey
,
2037 (LPBYTE
)pFixedInfo
->DomainName
,
2043 TRACE("GetComputerNameExA: %s\n", pFixedInfo
->DomainName
);
2045 if (resInfo
->riCount
> 0)
2047 CopyMemory(&pFixedInfo
->DnsServerList
, resInfo
->DnsList
, sizeof(IP_ADDR_STRING
));
2048 if (resInfo
->riCount
> 1)
2050 IP_ADDR_STRING
*pSrc
= resInfo
->DnsList
->Next
;
2051 IP_ADDR_STRING
*pTarget
= (struct _IP_ADDR_STRING
*)((char*)pFixedInfo
+ sizeof(FIXED_INFO
));
2053 pFixedInfo
->DnsServerList
.Next
= pTarget
;
2057 CopyMemory(pTarget
, pSrc
, sizeof(IP_ADDR_STRING
));
2059 if (resInfo
->riCount
> 1)
2061 pTarget
->Next
= (IP_ADDR_STRING
*)((char*)pTarget
+ sizeof(IP_ADDR_STRING
));
2062 pTarget
= pTarget
->Next
;
2067 pTarget
->Next
= NULL
;
2075 pFixedInfo
->DnsServerList
.Next
= NULL
;
2079 pFixedInfo
->NodeType
= HYBRID_NODETYPE
;
2080 regReturn
= RegOpenKeyExA(HKEY_LOCAL_MACHINE
,
2081 "SYSTEM\\CurrentControlSet\\Services\\VxD\\MSTCP", 0, KEY_READ
, &hKey
);
2082 if (regReturn
!= ERROR_SUCCESS
)
2083 regReturn
= RegOpenKeyExA(HKEY_LOCAL_MACHINE
,
2084 "SYSTEM\\CurrentControlSet\\Services\\NetBT\\Parameters", 0, KEY_READ
,
2086 if (regReturn
== ERROR_SUCCESS
)
2088 DWORD size
= sizeof(pFixedInfo
->ScopeId
);
2090 RegQueryValueExA(hKey
, "ScopeID", NULL
, NULL
, (PBYTE
)pFixedInfo
->ScopeId
, &size
);
2094 disposeResInfo( resInfo
);
2095 /* FIXME: can check whether routing's enabled in /proc/sys/net/ipv4/ip_forward
2096 I suppose could also check for a listener on port 53 to set EnableDns */
2098 TRACE("returning %ld\n", ret
);
2104 /******************************************************************
2105 * GetNumberOfInterfaces (IPHLPAPI.@)
2117 DWORD WINAPI
GetNumberOfInterfaces(PDWORD pdwNumIf
)
2121 TRACE("pdwNumIf %p\n", pdwNumIf
);
2123 ret
= ERROR_INVALID_PARAMETER
;
2125 *pdwNumIf
= getNumInterfaces();
2128 TRACE("returning %ld\n", ret
);
2133 /******************************************************************
2134 * GetOwnerModuleFromTcpEntry (IPHLPAPI.@)
2136 * Get data about the module that issued the context bind for a specific IPv4 TCP endpoint in a MIB table row
2139 * pTcpEntry [in] pointer to a MIB_TCPROW_OWNER_MODULE structure
2140 * Class [in] TCPIP_OWNER_MODULE_INFO_CLASS enumeration value
2141 * Buffer [out] pointer a buffer containing a TCPIP_OWNER_MODULE_BASIC_INFO structure with the owner module data.
2142 * pdwSize [in, out] estimated size of the structure returned in Buffer, in bytes
2146 * Failure: ERROR_INSUFFICIENT_BUFFER, ERROR_INVALID_PARAMETER, ERROR_NOT_ENOUGH_MEMORY
2147 * ERROR_NOT_FOUND or ERROR_PARTIAL_COPY
2150 * The type of data returned in Buffer is indicated by the value of the Class parameter.
2152 DWORD WINAPI
GetOwnerModuleFromTcpEntry( PMIB_TCPROW_OWNER_MODULE pTcpEntry
, TCPIP_OWNER_MODULE_INFO_CLASS Class
, PVOID Buffer
, PDWORD pdwSize
)
2154 DWORD ret
= NO_ERROR
;
2159 static void CreateNameServerListEnumNamesFunc( PWCHAR Interface
, PWCHAR Server
, PVOID Data
)
2161 IP_ADDR_STRING
*pNext
;
2162 PNAME_SERVER_LIST_CONTEXT Context
= (PNAME_SERVER_LIST_CONTEXT
)Data
;
2164 if (!Context
->NumServers
)
2166 if (Context
->uSizeAvailable
>= Context
->uSizeRequired
)
2168 WideCharToMultiByte(CP_ACP
, 0, Server
, -1, Context
->pData
->DnsServerList
.IpAddress
.String
, 16, NULL
, NULL
);
2169 Context
->pData
->DnsServerList
.IpAddress
.String
[15] = '\0';
2170 Context
->pLastAddr
= &Context
->pData
->DnsServerList
;
2175 Context
->uSizeRequired
+= sizeof(IP_ADDR_STRING
);
2176 if (Context
->uSizeAvailable
>= Context
->uSizeRequired
)
2178 pNext
= (IP_ADDR_STRING
*)(((char*)Context
->pLastAddr
) + sizeof(IP_ADDR_STRING
));
2179 WideCharToMultiByte(CP_ACP
, 0, Server
, -1, pNext
->IpAddress
.String
, 16, NULL
, NULL
);
2180 pNext
->IpAddress
.String
[15] = '\0';
2181 Context
->pLastAddr
->Next
= pNext
;
2182 Context
->pLastAddr
= pNext
;
2186 Context
->NumServers
++;
2189 /******************************************************************
2190 * GetPerAdapterInfo (IPHLPAPI.@)
2196 * pPerAdapterInfo [In/Out]
2197 * pOutBufLen [In/Out]
2204 DWORD WINAPI
GetPerAdapterInfo(ULONG IfIndex
, PIP_PER_ADAPTER_INFO pPerAdapterInfo
, PULONG pOutBufLen
)
2209 NAME_SERVER_LIST_CONTEXT Context
;
2210 WCHAR keyname
[200] = L
"SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Interfaces\\";
2213 return ERROR_INVALID_PARAMETER
;
2215 if (!pPerAdapterInfo
|| *pOutBufLen
< sizeof(IP_PER_ADAPTER_INFO
))
2217 *pOutBufLen
= sizeof(IP_PER_ADAPTER_INFO
);
2218 return ERROR_BUFFER_OVERFLOW
;
2221 ifName
= getInterfaceNameByIndex(IfIndex
);
2223 return ERROR_INVALID_PARAMETER
;
2225 MultiByteToWideChar(CP_ACP
, 0, ifName
, -1, &keyname
[62], sizeof(keyname
)/sizeof(WCHAR
) - 63);
2226 HeapFree(GetProcessHeap(), 0, (LPVOID
)ifName
);
2228 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE
, keyname
, 0, KEY_READ
, &hkey
) != ERROR_SUCCESS
)
2230 return ERROR_NOT_SUPPORTED
;
2232 Context
.NumServers
= 0;
2233 Context
.uSizeAvailable
= *pOutBufLen
;
2234 Context
.uSizeRequired
= sizeof(IP_PER_ADAPTER_INFO
);
2235 Context
.pData
= pPerAdapterInfo
;
2237 if (*pOutBufLen
>= sizeof(IP_PER_ADAPTER_INFO
))
2238 ZeroMemory(pPerAdapterInfo
, sizeof(IP_PER_ADAPTER_INFO
));
2240 EnumNameServers(hkey
, &keyname
[62], &Context
, CreateNameServerListEnumNamesFunc
);
2242 if (Context
.uSizeRequired
> Context
.uSizeAvailable
)
2244 *pOutBufLen
= Context
.uSizeRequired
;
2246 return ERROR_BUFFER_OVERFLOW
;
2249 if(RegQueryValueExW(hkey
, L
"NameServer", NULL
, NULL
, NULL
, &dwSize
) == ERROR_SUCCESS
)
2251 pPerAdapterInfo
->AutoconfigActive
= FALSE
;
2255 pPerAdapterInfo
->AutoconfigActive
= TRUE
;
2263 /******************************************************************
2264 * GetRTTAndHopCount (IPHLPAPI.@)
2269 * DestIpAddress [In]
2279 BOOL WINAPI
GetRTTAndHopCount(IPAddr DestIpAddress
, PULONG HopCount
, ULONG MaxHops
, PULONG RTT
)
2281 TRACE("DestIpAddress 0x%08lx, HopCount %p, MaxHops %ld, RTT %p\n",
2282 DestIpAddress
, HopCount
, MaxHops
, RTT
);
2288 /******************************************************************
2289 * GetTcpStatisticsEx (IPHLPAPI.@)
2302 DWORD WINAPI
GetTcpStatisticsEx(PMIB_TCPSTATS pStats
, DWORD dwFamily
)
2306 TRACE("pStats %p\n", pStats
);
2307 ret
= getTCPStats(pStats
, dwFamily
);
2308 TRACE("returning %ld\n", ret
);
2312 /******************************************************************
2313 * GetTcpStatistics (IPHLPAPI.@)
2325 DWORD WINAPI
GetTcpStatistics(PMIB_TCPSTATS pStats
)
2327 return GetTcpStatisticsEx(pStats
, PF_INET
);
2331 /******************************************************************
2332 * GetTcpTable (IPHLPAPI.@)
2334 * Get the table of active TCP connections.
2337 * pTcpTable [Out] buffer for TCP connections table
2338 * pdwSize [In/Out] length of output buffer
2339 * bOrder [In] whether to order the table
2343 * Failure: error code from winerror.h
2346 * If pdwSize is less than required, the function will return
2347 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to
2348 * the required byte size.
2349 * If bOrder is true, the returned table will be sorted, first by
2350 * local address and port number, then by remote address and port
2353 DWORD WINAPI
GetTcpTable(PMIB_TCPTABLE pTcpTable
, PDWORD pdwSize
, BOOL bOrder
)
2355 return GetExtendedTcpTable(pTcpTable
, pdwSize
, bOrder
, AF_INET
, TCP_TABLE_BASIC_ALL
, 0);
2359 /******************************************************************
2360 * GetUdpStatisticsEx (IPHLPAPI.@)
2373 DWORD WINAPI
GetUdpStatisticsEx(PMIB_UDPSTATS pStats
, DWORD dwFamily
)
2377 TRACE("pStats %p\n", pStats
);
2378 ret
= getUDPStats(pStats
, dwFamily
);
2379 TRACE("returning %ld\n", ret
);
2383 /******************************************************************
2384 * GetUdpStatistics (IPHLPAPI.@)
2396 DWORD WINAPI
GetUdpStatistics(PMIB_UDPSTATS pStats
)
2398 return GetUdpStatisticsEx(pStats
, PF_INET
);
2402 /******************************************************************
2403 * GetUdpTable (IPHLPAPI.@)
2408 * pUdpTable [In/Out]
2417 DWORD WINAPI
GetUdpTable(PMIB_UDPTABLE pUdpTable
, PDWORD pdwSize
, BOOL bOrder
)
2419 return GetExtendedUdpTable(pUdpTable
, pdwSize
, bOrder
, AF_INET
, UDP_TABLE_BASIC
, 0);
2423 /******************************************************************
2424 * GetUniDirectionalAdapterInfo (IPHLPAPI.@)
2426 * This is a Win98-only function to get information on "unidirectional"
2427 * adapters. Since this is pretty nonsensical in other contexts, it
2428 * never returns anything.
2431 * pIPIfInfo [Out] buffer for adapter infos
2432 * dwOutBufLen [Out] length of the output buffer
2436 * Failure: error code from winerror.h
2439 * Stub, returns ERROR_NOT_SUPPORTED.
2441 DWORD WINAPI
GetUniDirectionalAdapterInfo(PIP_UNIDIRECTIONAL_ADAPTER_ADDRESS pIPIfInfo
, PULONG dwOutBufLen
)
2443 TRACE("pIPIfInfo %p, dwOutBufLen %p\n", pIPIfInfo
, dwOutBufLen
);
2444 /* a unidirectional adapter?? not bloody likely! */
2445 return ERROR_NOT_SUPPORTED
;
2449 /******************************************************************
2450 * IpReleaseAddress (IPHLPAPI.@)
2452 * Release an IP obtained through DHCP,
2455 * AdapterInfo [In] adapter to release IP address
2459 * Failure: error code from winerror.h
2462 DWORD WINAPI
IpReleaseAddress(PIP_ADAPTER_INDEX_MAP AdapterInfo
)
2464 DWORD Status
, Version
= 0;
2467 return ERROR_INVALID_PARAMETER
;
2469 /* Maybe we should do this in DllMain */
2470 if (DhcpCApiInitialize(&Version
) != ERROR_SUCCESS
)
2471 return ERROR_PROC_NOT_FOUND
;
2473 if (DhcpReleaseIpAddressLease(AdapterInfo
->Index
))
2474 Status
= ERROR_SUCCESS
;
2476 Status
= ERROR_PROC_NOT_FOUND
;
2484 /******************************************************************
2485 * IpRenewAddress (IPHLPAPI.@)
2487 * Renew an IP obtained through DHCP.
2490 * AdapterInfo [In] adapter to renew IP address
2494 * Failure: error code from winerror.h
2496 DWORD WINAPI
IpRenewAddress(PIP_ADAPTER_INDEX_MAP AdapterInfo
)
2498 DWORD Status
, Version
= 0;
2501 return ERROR_INVALID_PARAMETER
;
2503 /* Maybe we should do this in DllMain */
2504 if (DhcpCApiInitialize(&Version
) != ERROR_SUCCESS
)
2505 return ERROR_PROC_NOT_FOUND
;
2507 if (DhcpRenewIpAddressLease(AdapterInfo
->Index
))
2508 Status
= ERROR_SUCCESS
;
2510 Status
= ERROR_PROC_NOT_FOUND
;
2518 /******************************************************************
2519 * NotifyAddrChange (IPHLPAPI.@)
2521 * Notify caller whenever the ip-interface map is changed.
2524 * Handle [Out] handle usable in asynchronous notification
2525 * overlapped [In] overlapped structure that notifies the caller
2529 * Failure: error code from winerror.h
2532 * Stub, returns ERROR_NOT_SUPPORTED.
2534 DWORD WINAPI
NotifyAddrChange(PHANDLE Handle
, LPOVERLAPPED overlapped
)
2536 FIXME("(Handle %p, overlapped %p): stub\n", Handle
, overlapped
);
2537 if (Handle
) *Handle
= INVALID_HANDLE_VALUE
;
2538 if (overlapped
) ((IO_STATUS_BLOCK
*) overlapped
)->Status
= STATUS_PENDING
;
2539 return ERROR_IO_PENDING
;
2543 /******************************************************************
2544 * NotifyRouteChange (IPHLPAPI.@)
2546 * Notify caller whenever the ip routing table is changed.
2549 * Handle [Out] handle usable in asynchronous notification
2550 * overlapped [In] overlapped structure that notifies the caller
2554 * Failure: error code from winerror.h
2557 * Stub, returns ERROR_NOT_SUPPORTED.
2559 DWORD WINAPI
NotifyRouteChange(PHANDLE Handle
, LPOVERLAPPED overlapped
)
2561 FIXME("(Handle %p, overlapped %p): stub\n", Handle
, overlapped
);
2562 return ERROR_NOT_SUPPORTED
;
2565 /******************************************************************
2566 * SendARP (IPHLPAPI.@)
2568 * Send an ARP request.
2571 * DestIP [In] attempt to obtain this IP
2572 * SrcIP [In] optional sender IP address
2573 * pMacAddr [Out] buffer for the mac address
2574 * PhyAddrLen [In/Out] length of the output buffer
2578 * Failure: error code from winerror.h
2580 DWORD WINAPI
SendARP(IPAddr DestIP
, IPAddr SrcIP
, PULONG pMacAddr
, PULONG PhyAddrLen
)
2585 if (IsBadWritePtr(pMacAddr
, sizeof(ULONG
)) || IsBadWritePtr(PhyAddrLen
, sizeof(ULONG
)))
2586 return ERROR_INVALID_PARAMETER
;
2591 return TCPSendIoctl(INVALID_HANDLE_VALUE
, IOCTL_QUERY_IP_HW_ADDRESS
, IPs
, &Size
, pMacAddr
, PhyAddrLen
);
2595 /******************************************************************
2596 * SetIfEntry (IPHLPAPI.@)
2598 * Set the administrative status of an interface.
2601 * pIfRow [In] dwAdminStatus member specifies the new status.
2605 * Failure: error code from winerror.h
2608 * Stub, returns ERROR_NOT_SUPPORTED.
2610 DWORD WINAPI
SetIfEntry(PMIB_IFROW pIfRow
)
2612 FIXME("(pIfRow %p): stub\n", pIfRow
);
2613 /* this is supposed to set an interface administratively up or down.
2614 Could do SIOCSIFFLAGS and set/clear IFF_UP, but, not sure I want to, and
2615 this sort of down is indistinguishable from other sorts of down (e.g. no
2617 return ERROR_NOT_SUPPORTED
;
2621 /******************************************************************
2622 * SetIpForwardEntry (IPHLPAPI.@)
2624 * Modify an existing route.
2627 * pRoute [In] route with the new information
2631 * Failure: error code from winerror.h
2634 DWORD WINAPI
SetIpForwardEntry(PMIB_IPFORWARDROW pRoute
)
2636 return setIpForwardEntry( pRoute
);
2640 /******************************************************************
2641 * SetIpNetEntry (IPHLPAPI.@)
2643 * Modify an existing ARP entry.
2646 * pArpEntry [In] ARP entry with the new information
2650 * Failure: error code from winerror.h
2652 DWORD WINAPI
SetIpNetEntry(PMIB_IPNETROW pArpEntry
)
2656 TCP_REQUEST_SET_INFORMATION_EX_ARP_ENTRY req
=
2657 TCP_REQUEST_SET_INFORMATION_INIT
;
2660 PMIB_IPNETROW arpBuff
;
2663 return ERROR_INVALID_PARAMETER
;
2665 if (!NT_SUCCESS(openTcpFile( &tcpFile
, FILE_READ_DATA
| FILE_WRITE_DATA
)))
2666 return ERROR_NOT_SUPPORTED
;
2668 if (!NT_SUCCESS(getNthIpEntity( tcpFile
, pArpEntry
->dwIndex
, &id
)))
2670 closeTcpFile(tcpFile
);
2671 return ERROR_INVALID_PARAMETER
;
2674 req
.Req
.ID
.toi_class
= INFO_CLASS_PROTOCOL
;
2675 req
.Req
.ID
.toi_type
= INFO_TYPE_PROVIDER
;
2676 req
.Req
.ID
.toi_id
= IP_MIB_ARPTABLE_ENTRY_ID
;
2677 req
.Req
.ID
.toi_entity
.tei_instance
= id
.tei_instance
;
2678 req
.Req
.ID
.toi_entity
.tei_entity
= AT_ENTITY
;
2679 req
.Req
.BufferSize
= sizeof(MIB_IPNETROW
);
2680 arpBuff
= (PMIB_IPNETROW
)&req
.Req
.Buffer
[0];
2682 RtlCopyMemory(arpBuff
, pArpEntry
, sizeof(MIB_IPNETROW
));
2684 status
= DeviceIoControl( tcpFile
,
2685 IOCTL_TCP_SET_INFORMATION_EX
,
2693 closeTcpFile(tcpFile
);
2698 return ERROR_INVALID_PARAMETER
;
2702 /******************************************************************
2703 * SetIpStatistics (IPHLPAPI.@)
2705 * Toggle IP forwarding and det the default TTL value.
2708 * pIpStats [In] IP statistics with the new information
2712 * Failure: error code from winerror.h
2715 * Stub, returns NO_ERROR.
2717 DWORD WINAPI
SetIpStatistics(PMIB_IPSTATS pIpStats
)
2719 FIXME("(pIpStats %p): stub\n", pIpStats
);
2724 /******************************************************************
2725 * SetIpTTL (IPHLPAPI.@)
2727 * Set the default TTL value.
2730 * nTTL [In] new TTL value
2734 * Failure: error code from winerror.h
2737 * Stub, returns NO_ERROR.
2739 DWORD WINAPI
SetIpTTL(UINT nTTL
)
2741 FIXME("(nTTL %d): stub\n", nTTL
);
2746 /******************************************************************
2747 * SetTcpEntry (IPHLPAPI.@)
2749 * Set the state of a TCP connection.
2752 * pTcpRow [In] specifies connection with new state
2756 * Failure: error code from winerror.h
2759 * Stub, returns NO_ERROR.
2761 DWORD WINAPI
SetTcpEntry(PMIB_TCPROW pTcpRow
)
2763 FIXME("(pTcpRow %p): stub\n", pTcpRow
);
2768 /******************************************************************
2769 * UnenableRouter (IPHLPAPI.@)
2771 * Decrement the IP-forwarding reference count. Turn off IP-forwarding
2772 * if it reaches zero.
2775 * pOverlapped [In/Out] should be the same as in EnableRouter()
2776 * lpdwEnableCount [Out] optional, receives reference count
2780 * Failure: error code from winerror.h
2783 * Stub, returns ERROR_NOT_SUPPORTED.
2785 DWORD WINAPI
UnenableRouter(OVERLAPPED
* pOverlapped
, LPDWORD lpdwEnableCount
)
2787 FIXME("(pOverlapped %p, lpdwEnableCount %p): stub\n", pOverlapped
,
2789 return ERROR_NOT_SUPPORTED
;
2795 DWORD WINAPI
GetIpErrorString(IP_STATUS ErrorCode
,PWCHAR Buffer
,PDWORD Size
)
2805 PIP_ADAPTER_ORDER_MAP WINAPI
GetAdapterOrderMap(VOID
)
2814 #ifdef GetAdaptersAddressesV1
2815 DWORD WINAPI DECLSPEC_HOTPATCH
GetAdaptersAddresses(ULONG Family
,ULONG Flags
,PVOID Reserved
,PIP_ADAPTER_ADDRESSES pAdapterAddresses
,PULONG pOutBufLen
)
2817 InterfaceIndexTable
*indexTable
;
2820 ULONG ret
, requiredSize
= 0;
2821 PIP_ADAPTER_ADDRESSES currentAddress
;
2822 PUCHAR currentLocation
;
2825 if (!pOutBufLen
) return ERROR_INVALID_PARAMETER
;
2826 if (Reserved
) return ERROR_INVALID_PARAMETER
;
2828 indexTable
= getInterfaceIndexTable();
2830 return ERROR_NOT_ENOUGH_MEMORY
;
2832 ret
= openTcpFile(&tcpFile
, FILE_READ_DATA
);
2833 if (!NT_SUCCESS(ret
))
2834 return ERROR_NO_DATA
;
2836 for (i
= indexTable
->numIndexes
; i
>= 0; i
--)
2838 if (NT_SUCCESS(getIPAddrEntryForIf(tcpFile
,
2840 indexTable
->indexes
[i
],
2843 /* The whole struct */
2844 requiredSize
+= sizeof(IP_ADAPTER_ADDRESSES
);
2847 if (!(Flags
& GAA_FLAG_SKIP_FRIENDLY_NAME
))
2848 requiredSize
+= strlen((char *)ifInfo
.if_info
.ent
.if_descr
) + 1; //FIXME
2851 requiredSize
+= strlen((char *)ifInfo
.if_info
.ent
.if_descr
) + 1;
2853 /* Unicast address */
2854 if (!(Flags
& GAA_FLAG_SKIP_UNICAST
))
2855 requiredSize
+= sizeof(IP_ADAPTER_UNICAST_ADDRESS
);
2857 /* FIXME: Implement multicast, anycast, and dns server stuff */
2859 /* FIXME: Implement dns suffix and description */
2860 requiredSize
+= 2 * sizeof(WCHAR
);
2862 /* We're only going to implement what's required for XP SP0 */
2865 TRACE("size: %d, requiredSize: %d\n", *pOutBufLen
, requiredSize
);
2866 if (!pAdapterAddresses
|| *pOutBufLen
< requiredSize
)
2868 *pOutBufLen
= requiredSize
;
2869 closeTcpFile(tcpFile
);
2871 return ERROR_BUFFER_OVERFLOW
;
2874 RtlZeroMemory(pAdapterAddresses
, requiredSize
);
2876 /* Let's set up the pointers */
2877 currentAddress
= pAdapterAddresses
;
2878 for (i
= indexTable
->numIndexes
; i
>= 0; i
--)
2880 if (NT_SUCCESS(getIPAddrEntryForIf(tcpFile
,
2882 indexTable
->indexes
[i
],
2885 currentLocation
= (PUCHAR
)currentAddress
+ (ULONG_PTR
)sizeof(IP_ADAPTER_ADDRESSES
);
2887 /* FIXME: Friendly name */
2888 if (!(Flags
& GAA_FLAG_SKIP_FRIENDLY_NAME
))
2890 currentAddress
->FriendlyName
= (PVOID
)currentLocation
;
2891 currentLocation
+= sizeof(WCHAR
);
2895 currentAddress
->AdapterName
= (PVOID
)currentLocation
;
2896 currentLocation
+= strlen((char *)ifInfo
.if_info
.ent
.if_descr
) + 1;
2898 /* Unicast address */
2899 if (!(Flags
& GAA_FLAG_SKIP_UNICAST
))
2901 currentAddress
->FirstUnicastAddress
= (PVOID
)currentLocation
;
2902 currentLocation
+= sizeof(IP_ADAPTER_UNICAST_ADDRESS
);
2903 currentAddress
->FirstUnicastAddress
->Address
.lpSockaddr
= (PVOID
)currentLocation
;
2904 currentLocation
+= sizeof(struct sockaddr
);
2907 /* FIXME: Implement multicast, anycast, and dns server stuff */
2909 /* FIXME: Implement dns suffix and description */
2910 currentAddress
->DnsSuffix
= (PVOID
)currentLocation
;
2911 currentLocation
+= sizeof(WCHAR
);
2913 currentAddress
->Description
= (PVOID
)currentLocation
;
2914 currentLocation
+= sizeof(WCHAR
);
2916 currentAddress
->Next
= (PVOID
)currentLocation
;
2917 /* Terminate the last address correctly */
2919 currentAddress
->Next
= NULL
;
2921 /* We're only going to implement what's required for XP SP0 */
2923 currentAddress
= currentAddress
->Next
;
2927 /* Now again, for real this time */
2929 currentAddress
= pAdapterAddresses
;
2930 for (i
= indexTable
->numIndexes
; i
>= 0; i
--)
2932 if (NT_SUCCESS(getIPAddrEntryForIf(tcpFile
,
2934 indexTable
->indexes
[i
],
2937 /* Make sure we're not looping more than we hoped for */
2938 ASSERT(currentAddress
);
2940 /* Alignment information */
2941 currentAddress
->Length
= sizeof(IP_ADAPTER_ADDRESSES
);
2942 currentAddress
->IfIndex
= indexTable
->indexes
[i
];
2945 strcpy(currentAddress
->AdapterName
, (char *)ifInfo
.if_info
.ent
.if_descr
);
2947 if (!(Flags
& GAA_FLAG_SKIP_UNICAST
))
2949 currentAddress
->FirstUnicastAddress
->Length
= sizeof(IP_ADAPTER_UNICAST_ADDRESS
);
2950 currentAddress
->FirstUnicastAddress
->Flags
= 0; //FIXME
2951 currentAddress
->FirstUnicastAddress
->Next
= NULL
; //FIXME: Support more than one address per adapter
2952 currentAddress
->FirstUnicastAddress
->Address
.lpSockaddr
->sa_family
= AF_INET
;
2953 memcpy(currentAddress
->FirstUnicastAddress
->Address
.lpSockaddr
->sa_data
,
2954 &ifInfo
.ip_addr
.iae_addr
,
2955 sizeof(ifInfo
.ip_addr
.iae_addr
));
2956 currentAddress
->FirstUnicastAddress
->Address
.iSockaddrLength
= sizeof(ifInfo
.ip_addr
.iae_addr
) + sizeof(USHORT
);
2957 currentAddress
->FirstUnicastAddress
->PrefixOrigin
= IpPrefixOriginOther
; //FIXME
2958 currentAddress
->FirstUnicastAddress
->SuffixOrigin
= IpPrefixOriginOther
; //FIXME
2959 currentAddress
->FirstUnicastAddress
->DadState
= IpDadStatePreferred
; //FIXME
2960 currentAddress
->FirstUnicastAddress
->ValidLifetime
= 0xFFFFFFFF; //FIXME
2961 currentAddress
->FirstUnicastAddress
->PreferredLifetime
= 0xFFFFFFFF; //FIXME
2962 currentAddress
->FirstUnicastAddress
->LeaseLifetime
= 0xFFFFFFFF; //FIXME
2965 /* FIXME: Implement multicast, anycast, and dns server stuff */
2966 currentAddress
->FirstAnycastAddress
= NULL
;
2967 currentAddress
->FirstMulticastAddress
= NULL
;
2968 currentAddress
->FirstDnsServerAddress
= NULL
;
2970 /* FIXME: Implement dns suffix, description, and friendly name */
2971 currentAddress
->DnsSuffix
[0] = UNICODE_NULL
;
2972 currentAddress
->Description
[0] = UNICODE_NULL
;
2973 currentAddress
->FriendlyName
[0] = UNICODE_NULL
;
2975 /* Physical Address */
2976 memcpy(currentAddress
->PhysicalAddress
, ifInfo
.if_info
.ent
.if_physaddr
, ifInfo
.if_info
.ent
.if_physaddrlen
);
2977 currentAddress
->PhysicalAddressLength
= ifInfo
.if_info
.ent
.if_physaddrlen
;
2980 currentAddress
->Flags
= 0; //FIXME
2983 currentAddress
->Mtu
= ifInfo
.if_info
.ent
.if_mtu
;
2985 /* Interface type */
2986 currentAddress
->IfType
= ifInfo
.if_info
.ent
.if_type
;
2988 /* Operational status */
2989 if(ifInfo
.if_info
.ent
.if_operstatus
>= IF_OPER_STATUS_CONNECTING
)
2990 currentAddress
->OperStatus
= IfOperStatusUp
;
2992 currentAddress
->OperStatus
= IfOperStatusDown
;
2994 /* We're only going to implement what's required for XP SP0 */
2996 /* Move to the next address */
2997 currentAddress
= currentAddress
->Next
;
3001 closeTcpFile(tcpFile
);
3011 BOOL WINAPI
CancelIPChangeNotify(LPOVERLAPPED notifyOverlapped
)
3020 DWORD WINAPI
GetBestInterfaceEx(struct sockaddr
*pDestAddr
,PDWORD pdwBestIfIndex
)
3029 DWORD WINAPI
NhpAllocateAndGetInterfaceInfoFromStack(IP_INTERFACE_NAME_INFO
**ppTable
,PDWORD pdwCount
,BOOL bOrder
,HANDLE hHeap
,DWORD dwFlags
)
3038 DWORD WINAPI
GetIcmpStatisticsEx(PMIB_ICMP_EX pStats
,DWORD dwFamily
)
3043 return ERROR_INVALID_PARAMETER
;
3045 if (dwFamily
!= AF_INET
&& dwFamily
!= AF_INET6
)
3046 return ERROR_INVALID_PARAMETER
;
3052 SetIpForwardEntryToStack(PMIB_IPFORWARDROW pRoute
)
3054 FIXME("SetIpForwardEntryToStack() stub\n");
3058 DWORD
GetInterfaceNameInternal(_In_
const GUID
* pInterfaceGUID
,
3059 _Out_writes_bytes_to_(*pOutBufLen
, *pOutBufLen
) PWCHAR pInterfaceName
,
3060 _Inout_ PULONG pOutBufLen
)
3062 UNICODE_STRING GuidString
;
3064 WCHAR szKeyName
[2*MAX_PATH
];
3068 if (pInterfaceGUID
== NULL
|| pOutBufLen
== NULL
)
3069 return ERROR_INVALID_PARAMETER
;
3071 result
= RtlStringFromGUID(pInterfaceGUID
, &GuidString
);
3073 if (!NT_SUCCESS(result
))
3075 // failed to convert guid to string
3076 return RtlNtStatusToDosError(result
);
3079 hr
= StringCbPrintfW(szKeyName
, sizeof(szKeyName
), L
"SYSTEM\\CurrentControlSet\\Control\\Network\\{4D36E972-E325-11CE-BFC1-08002BE10318}\\%s\\Connection", GuidString
.Buffer
);
3080 RtlFreeUnicodeString(&GuidString
);
3084 // key name is too long
3085 return ERROR_BUFFER_OVERFLOW
;
3088 result
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
, szKeyName
, 0, KEY_READ
, &hKey
);
3090 if (result
!= ERROR_SUCCESS
)
3092 // failed to find adapter entry
3093 return ERROR_NOT_FOUND
;
3096 result
= RegQueryValueExW(hKey
, L
"Name", NULL
, &type
, (PVOID
)pInterfaceName
, pOutBufLen
);
3100 if (result
== ERROR_MORE_DATA
)
3102 *pOutBufLen
= MAX_INTERFACE_NAME_LEN
* 2;
3103 return ERROR_INSUFFICIENT_BUFFER
;
3106 if (result
!= ERROR_SUCCESS
|| type
!= REG_SZ
)
3108 // failed to read adapter name
3109 return ERROR_NO_DATA
;
3111 return ERROR_SUCCESS
;
3118 NhGetInterfaceNameFromDeviceGuid(_In_
const GUID
* pInterfaceGUID
,
3119 _Out_writes_bytes_to_(*pOutBufLen
, *pOutBufLen
) PWCHAR pInterfaceName
,
3120 _Inout_ PULONG pOutBufLen
,
3124 SetLastError(ERROR_SUCCESS
);
3126 if (pInterfaceName
== NULL
)
3127 return ERROR_INVALID_PARAMETER
;
3129 return GetInterfaceNameInternal(pInterfaceGUID
, pInterfaceName
, pOutBufLen
);
3136 NhGetInterfaceNameFromGuid(_In_
const GUID
* pInterfaceGUID
,
3137 _Out_writes_bytes_to_(*pOutBufLen
, *pOutBufLen
) PWCHAR pInterfaceName
,
3138 _Inout_ PULONG pOutBufLen
,
3144 result
= GetInterfaceNameInternal(pInterfaceGUID
, pInterfaceName
, pOutBufLen
);
3146 if (result
== ERROR_NOT_FOUND
)
3147 SetLastError(ERROR_PATH_NOT_FOUND
);