push f19c907ab00e303c34c91d18c0dfddd2e9ddf00c
[wine/hacks.git] / dlls / iphlpapi / iphlpapi_main.c
blobc80e1e0ed9c94ddb79ebd06f46c7cc4f044a9a46
1 /*
2 * iphlpapi dll implementation
4 * Copyright (C) 2003,2006 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 St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
23 #include <stdarg.h>
24 #include <stdlib.h>
25 #include <sys/types.h>
26 #ifdef HAVE_NETINET_IN_H
27 # include <netinet/in.h>
28 #endif
29 #ifdef HAVE_ARPA_INET_H
30 # include <arpa/inet.h>
31 #endif
32 #ifdef HAVE_ARPA_NAMESER_H
33 # include <arpa/nameser.h>
34 #endif
35 #ifdef HAVE_RESOLV_H
36 # include <resolv.h>
37 #endif
39 #include "windef.h"
40 #include "winbase.h"
41 #include "winreg.h"
42 #include "iphlpapi.h"
43 #include "ifenum.h"
44 #include "ipstats.h"
45 #include "wine/debug.h"
47 WINE_DEFAULT_DEBUG_CHANNEL(iphlpapi);
49 #ifndef INADDR_NONE
50 #define INADDR_NONE ~0UL
51 #endif
53 static int resolver_initialised;
55 /* call res_init() just once because of a bug in Mac OSX 10.4 */
56 static void initialise_resolver(void)
58 if (!resolver_initialised)
60 res_init();
61 resolver_initialised = 1;
65 BOOL WINAPI DllMain (HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
67 switch (fdwReason) {
68 case DLL_PROCESS_ATTACH:
69 DisableThreadLibraryCalls( hinstDLL );
70 break;
72 case DLL_PROCESS_DETACH:
73 break;
75 return TRUE;
78 /******************************************************************
79 * AddIPAddress (IPHLPAPI.@)
81 * Add an IP address to an adapter.
83 * PARAMS
84 * Address [In] IP address to add to the adapter
85 * IpMask [In] subnet mask for the IP address
86 * IfIndex [In] adapter index to add the address
87 * NTEContext [Out] Net Table Entry (NTE) context for the IP address
88 * NTEInstance [Out] NTE instance for the IP address
90 * RETURNS
91 * Success: NO_ERROR
92 * Failure: error code from winerror.h
94 * FIXME
95 * Stub. Currently returns ERROR_NOT_SUPPORTED.
97 DWORD WINAPI AddIPAddress(IPAddr Address, IPMask IpMask, DWORD IfIndex, PULONG NTEContext, PULONG NTEInstance)
99 FIXME(":stub\n");
100 return ERROR_NOT_SUPPORTED;
104 /******************************************************************
105 * AllocateAndGetIfTableFromStack (IPHLPAPI.@)
107 * Get table of local interfaces.
108 * Like GetIfTable(), but allocate the returned table from heap.
110 * PARAMS
111 * ppIfTable [Out] pointer into which the MIB_IFTABLE is
112 * allocated and returned.
113 * bOrder [In] whether to sort the table
114 * heap [In] heap from which the table is allocated
115 * flags [In] flags to HeapAlloc
117 * RETURNS
118 * ERROR_INVALID_PARAMETER if ppIfTable is NULL, whatever
119 * GetIfTable() returns otherwise.
121 DWORD WINAPI AllocateAndGetIfTableFromStack(PMIB_IFTABLE *ppIfTable,
122 BOOL bOrder, HANDLE heap, DWORD flags)
124 DWORD ret;
126 TRACE("ppIfTable %p, bOrder %d, heap %p, flags 0x%08x\n", ppIfTable,
127 bOrder, heap, flags);
128 if (!ppIfTable)
129 ret = ERROR_INVALID_PARAMETER;
130 else {
131 DWORD dwSize = 0;
133 ret = GetIfTable(*ppIfTable, &dwSize, bOrder);
134 if (ret == ERROR_INSUFFICIENT_BUFFER) {
135 *ppIfTable = HeapAlloc(heap, flags, dwSize);
136 ret = GetIfTable(*ppIfTable, &dwSize, bOrder);
139 TRACE("returning %d\n", ret);
140 return ret;
144 static int IpAddrTableSorter(const void *a, const void *b)
146 int ret;
148 if (a && b)
149 ret = ((const MIB_IPADDRROW*)a)->dwAddr - ((const MIB_IPADDRROW*)b)->dwAddr;
150 else
151 ret = 0;
152 return ret;
156 /******************************************************************
157 * AllocateAndGetIpAddrTableFromStack (IPHLPAPI.@)
159 * Get interface-to-IP address mapping table.
160 * Like GetIpAddrTable(), but allocate the returned table from heap.
162 * PARAMS
163 * ppIpAddrTable [Out] pointer into which the MIB_IPADDRTABLE is
164 * allocated and returned.
165 * bOrder [In] whether to sort the table
166 * heap [In] heap from which the table is allocated
167 * flags [In] flags to HeapAlloc
169 * RETURNS
170 * ERROR_INVALID_PARAMETER if ppIpAddrTable is NULL, other error codes on
171 * failure, NO_ERROR on success.
173 DWORD WINAPI AllocateAndGetIpAddrTableFromStack(PMIB_IPADDRTABLE *ppIpAddrTable,
174 BOOL bOrder, HANDLE heap, DWORD flags)
176 DWORD ret;
178 TRACE("ppIpAddrTable %p, bOrder %d, heap %p, flags 0x%08x\n",
179 ppIpAddrTable, bOrder, heap, flags);
180 ret = getIPAddrTable(ppIpAddrTable, heap, flags);
181 if (!ret && bOrder)
182 qsort((*ppIpAddrTable)->table, (*ppIpAddrTable)->dwNumEntries,
183 sizeof(MIB_IPADDRROW), IpAddrTableSorter);
184 TRACE("returning %d\n", ret);
185 return ret;
189 static int IpForwardTableSorter(const void *a, const void *b)
191 int ret;
193 if (a && b) {
194 const MIB_IPFORWARDROW* rowA = (const MIB_IPFORWARDROW*)a;
195 const MIB_IPFORWARDROW* rowB = (const MIB_IPFORWARDROW*)b;
197 ret = rowA->dwForwardDest - rowB->dwForwardDest;
198 if (ret == 0) {
199 ret = rowA->dwForwardProto - rowB->dwForwardProto;
200 if (ret == 0) {
201 ret = rowA->dwForwardPolicy - rowB->dwForwardPolicy;
202 if (ret == 0)
203 ret = rowA->dwForwardNextHop - rowB->dwForwardNextHop;
207 else
208 ret = 0;
209 return ret;
213 /******************************************************************
214 * AllocateAndGetIpForwardTableFromStack (IPHLPAPI.@)
216 * Get the route table.
217 * Like GetIpForwardTable(), but allocate the returned table from heap.
219 * PARAMS
220 * ppIpForwardTable [Out] pointer into which the MIB_IPFORWARDTABLE is
221 * allocated and returned.
222 * bOrder [In] whether to sort the table
223 * heap [In] heap from which the table is allocated
224 * flags [In] flags to HeapAlloc
226 * RETURNS
227 * ERROR_INVALID_PARAMETER if ppIfTable is NULL, other error codes
228 * on failure, NO_ERROR on success.
230 DWORD WINAPI AllocateAndGetIpForwardTableFromStack(PMIB_IPFORWARDTABLE *
231 ppIpForwardTable, BOOL bOrder, HANDLE heap, DWORD flags)
233 DWORD ret;
235 TRACE("ppIpForwardTable %p, bOrder %d, heap %p, flags 0x%08x\n",
236 ppIpForwardTable, bOrder, heap, flags);
237 ret = getRouteTable(ppIpForwardTable, heap, flags);
238 if (!ret && bOrder)
239 qsort((*ppIpForwardTable)->table, (*ppIpForwardTable)->dwNumEntries,
240 sizeof(MIB_IPFORWARDROW), IpForwardTableSorter);
241 TRACE("returning %d\n", ret);
242 return ret;
246 static int IpNetTableSorter(const void *a, const void *b)
248 int ret;
250 if (a && b)
251 ret = ((const MIB_IPNETROW*)a)->dwAddr - ((const MIB_IPNETROW*)b)->dwAddr;
252 else
253 ret = 0;
254 return ret;
258 /******************************************************************
259 * AllocateAndGetIpNetTableFromStack (IPHLPAPI.@)
261 * Get the IP-to-physical address mapping table.
262 * Like GetIpNetTable(), but allocate the returned table from heap.
264 * PARAMS
265 * ppIpNetTable [Out] pointer into which the MIB_IPNETTABLE is
266 * allocated and returned.
267 * bOrder [In] whether to sort the table
268 * heap [In] heap from which the table is allocated
269 * flags [In] flags to HeapAlloc
271 * RETURNS
272 * ERROR_INVALID_PARAMETER if ppIpNetTable is NULL, other error codes
273 * on failure, NO_ERROR on success.
275 DWORD WINAPI AllocateAndGetIpNetTableFromStack(PMIB_IPNETTABLE *ppIpNetTable,
276 BOOL bOrder, HANDLE heap, DWORD flags)
278 DWORD ret;
280 TRACE("ppIpNetTable %p, bOrder %d, heap %p, flags 0x%08x\n",
281 ppIpNetTable, bOrder, heap, flags);
282 ret = getArpTable(ppIpNetTable, heap, flags);
283 if (!ret && bOrder)
284 qsort((*ppIpNetTable)->table, (*ppIpNetTable)->dwNumEntries,
285 sizeof(MIB_IPADDRROW), IpNetTableSorter);
286 TRACE("returning %d\n", ret);
287 return ret;
291 static int TcpTableSorter(const void *a, const void *b)
293 int ret;
295 if (a && b) {
296 const MIB_TCPROW* rowA = a;
297 const MIB_TCPROW* rowB = b;
299 ret = rowA->dwLocalAddr - rowB->dwLocalAddr;
300 if (ret == 0) {
301 ret = rowA->dwLocalPort - rowB->dwLocalPort;
302 if (ret == 0) {
303 ret = rowA->dwRemoteAddr - rowB->dwRemoteAddr;
304 if (ret == 0)
305 ret = rowA->dwRemotePort - rowB->dwRemotePort;
309 else
310 ret = 0;
311 return ret;
315 /******************************************************************
316 * AllocateAndGetTcpTableFromStack (IPHLPAPI.@)
318 * Get the TCP connection table.
319 * Like GetTcpTable(), but allocate the returned table from heap.
321 * PARAMS
322 * ppTcpTable [Out] pointer into which the MIB_TCPTABLE is
323 * allocated and returned.
324 * bOrder [In] whether to sort the table
325 * heap [In] heap from which the table is allocated
326 * flags [In] flags to HeapAlloc
328 * RETURNS
329 * ERROR_INVALID_PARAMETER if ppTcpTable is NULL, whatever GetTcpTable()
330 * returns otherwise.
332 DWORD WINAPI AllocateAndGetTcpTableFromStack(PMIB_TCPTABLE *ppTcpTable,
333 BOOL bOrder, HANDLE heap, DWORD flags)
335 DWORD ret;
337 TRACE("ppTcpTable %p, bOrder %d, heap %p, flags 0x%08x\n",
338 ppTcpTable, bOrder, heap, flags);
339 ret = getTcpTable(ppTcpTable, heap, flags);
340 if (!ret && bOrder)
341 qsort((*ppTcpTable)->table, (*ppTcpTable)->dwNumEntries,
342 sizeof(MIB_TCPROW), TcpTableSorter);
343 TRACE("returning %d\n", ret);
344 return ret;
348 static int UdpTableSorter(const void *a, const void *b)
350 int ret;
352 if (a && b) {
353 const MIB_UDPROW* rowA = (const MIB_UDPROW*)a;
354 const MIB_UDPROW* rowB = (const MIB_UDPROW*)b;
356 ret = rowA->dwLocalAddr - rowB->dwLocalAddr;
357 if (ret == 0)
358 ret = rowA->dwLocalPort - rowB->dwLocalPort;
360 else
361 ret = 0;
362 return ret;
366 /******************************************************************
367 * AllocateAndGetUdpTableFromStack (IPHLPAPI.@)
369 * Get the UDP listener table.
370 * Like GetUdpTable(), but allocate the returned table from heap.
372 * PARAMS
373 * ppUdpTable [Out] pointer into which the MIB_UDPTABLE is
374 * allocated and returned.
375 * bOrder [In] whether to sort the table
376 * heap [In] heap from which the table is allocated
377 * flags [In] flags to HeapAlloc
379 * RETURNS
380 * ERROR_INVALID_PARAMETER if ppUdpTable is NULL, whatever GetUdpTable()
381 * returns otherwise.
383 DWORD WINAPI AllocateAndGetUdpTableFromStack(PMIB_UDPTABLE *ppUdpTable,
384 BOOL bOrder, HANDLE heap, DWORD flags)
386 DWORD ret;
388 TRACE("ppUdpTable %p, bOrder %d, heap %p, flags 0x%08x\n",
389 ppUdpTable, bOrder, heap, flags);
390 ret = getUdpTable(ppUdpTable, heap, flags);
391 if (!ret && bOrder)
392 qsort((*ppUdpTable)->table, (*ppUdpTable)->dwNumEntries,
393 sizeof(MIB_UDPROW), UdpTableSorter);
394 TRACE("returning %d\n", ret);
395 return ret;
399 /******************************************************************
400 * CreateIpForwardEntry (IPHLPAPI.@)
402 * Create a route in the local computer's IP table.
404 * PARAMS
405 * pRoute [In] new route information
407 * RETURNS
408 * Success: NO_ERROR
409 * Failure: error code from winerror.h
411 * FIXME
412 * Stub, always returns NO_ERROR.
414 DWORD WINAPI CreateIpForwardEntry(PMIB_IPFORWARDROW pRoute)
416 FIXME("(pRoute %p): stub\n", pRoute);
417 /* could use SIOCADDRT, not sure I want to */
418 return (DWORD) 0;
422 /******************************************************************
423 * CreateIpNetEntry (IPHLPAPI.@)
425 * Create entry in the ARP table.
427 * PARAMS
428 * pArpEntry [In] new ARP entry
430 * RETURNS
431 * Success: NO_ERROR
432 * Failure: error code from winerror.h
434 * FIXME
435 * Stub, always returns NO_ERROR.
437 DWORD WINAPI CreateIpNetEntry(PMIB_IPNETROW pArpEntry)
439 FIXME("(pArpEntry %p)\n", pArpEntry);
440 /* could use SIOCSARP on systems that support it, not sure I want to */
441 return (DWORD) 0;
445 /******************************************************************
446 * CreateProxyArpEntry (IPHLPAPI.@)
448 * Create a Proxy ARP (PARP) entry for an IP address.
450 * PARAMS
451 * dwAddress [In] IP address for which this computer acts as a proxy.
452 * dwMask [In] subnet mask for dwAddress
453 * dwIfIndex [In] interface index
455 * RETURNS
456 * Success: NO_ERROR
457 * Failure: error code from winerror.h
459 * FIXME
460 * Stub, returns ERROR_NOT_SUPPORTED.
462 DWORD WINAPI CreateProxyArpEntry(DWORD dwAddress, DWORD dwMask, DWORD dwIfIndex)
464 FIXME("(dwAddress 0x%08x, dwMask 0x%08x, dwIfIndex 0x%08x): stub\n",
465 dwAddress, dwMask, dwIfIndex);
466 return ERROR_NOT_SUPPORTED;
470 /******************************************************************
471 * DeleteIPAddress (IPHLPAPI.@)
473 * Delete an IP address added with AddIPAddress().
475 * PARAMS
476 * NTEContext [In] NTE context from AddIPAddress();
478 * RETURNS
479 * Success: NO_ERROR
480 * Failure: error code from winerror.h
482 * FIXME
483 * Stub, returns ERROR_NOT_SUPPORTED.
485 DWORD WINAPI DeleteIPAddress(ULONG NTEContext)
487 FIXME("(NTEContext %d): stub\n", NTEContext);
488 return ERROR_NOT_SUPPORTED;
492 /******************************************************************
493 * DeleteIpForwardEntry (IPHLPAPI.@)
495 * Delete a route.
497 * PARAMS
498 * pRoute [In] route to delete
500 * RETURNS
501 * Success: NO_ERROR
502 * Failure: error code from winerror.h
504 * FIXME
505 * Stub, returns NO_ERROR.
507 DWORD WINAPI DeleteIpForwardEntry(PMIB_IPFORWARDROW pRoute)
509 FIXME("(pRoute %p): stub\n", pRoute);
510 /* could use SIOCDELRT, not sure I want to */
511 return (DWORD) 0;
515 /******************************************************************
516 * DeleteIpNetEntry (IPHLPAPI.@)
518 * Delete an ARP entry.
520 * PARAMS
521 * pArpEntry [In] ARP entry to delete
523 * RETURNS
524 * Success: NO_ERROR
525 * Failure: error code from winerror.h
527 * FIXME
528 * Stub, returns NO_ERROR.
530 DWORD WINAPI DeleteIpNetEntry(PMIB_IPNETROW pArpEntry)
532 FIXME("(pArpEntry %p): stub\n", pArpEntry);
533 /* could use SIOCDARP on systems that support it, not sure I want to */
534 return (DWORD) 0;
538 /******************************************************************
539 * DeleteProxyArpEntry (IPHLPAPI.@)
541 * Delete a Proxy ARP entry.
543 * PARAMS
544 * dwAddress [In] IP address for which this computer acts as a proxy.
545 * dwMask [In] subnet mask for dwAddress
546 * dwIfIndex [In] interface index
548 * RETURNS
549 * Success: NO_ERROR
550 * Failure: error code from winerror.h
552 * FIXME
553 * Stub, returns ERROR_NOT_SUPPORTED.
555 DWORD WINAPI DeleteProxyArpEntry(DWORD dwAddress, DWORD dwMask, DWORD dwIfIndex)
557 FIXME("(dwAddress 0x%08x, dwMask 0x%08x, dwIfIndex 0x%08x): stub\n",
558 dwAddress, dwMask, dwIfIndex);
559 return ERROR_NOT_SUPPORTED;
563 /******************************************************************
564 * EnableRouter (IPHLPAPI.@)
566 * Turn on ip forwarding.
568 * PARAMS
569 * pHandle [In/Out]
570 * pOverlapped [In/Out] hEvent member should contain a valid handle.
572 * RETURNS
573 * Success: ERROR_IO_PENDING
574 * Failure: error code from winerror.h
576 * FIXME
577 * Stub, returns ERROR_NOT_SUPPORTED.
579 DWORD WINAPI EnableRouter(HANDLE * pHandle, OVERLAPPED * pOverlapped)
581 FIXME("(pHandle %p, pOverlapped %p): stub\n", pHandle, pOverlapped);
582 /* could echo "1" > /proc/net/sys/net/ipv4/ip_forward, not sure I want to
583 could map EACCESS to ERROR_ACCESS_DENIED, I suppose
585 return ERROR_NOT_SUPPORTED;
589 /******************************************************************
590 * FlushIpNetTable (IPHLPAPI.@)
592 * Delete all ARP entries of an interface
594 * PARAMS
595 * dwIfIndex [In] interface index
597 * RETURNS
598 * Success: NO_ERROR
599 * Failure: error code from winerror.h
601 * FIXME
602 * Stub, returns ERROR_NOT_SUPPORTED.
604 DWORD WINAPI FlushIpNetTable(DWORD dwIfIndex)
606 FIXME("(dwIfIndex 0x%08x): stub\n", dwIfIndex);
607 /* this flushes the arp cache of the given index */
608 return ERROR_NOT_SUPPORTED;
612 /******************************************************************
613 * GetAdapterIndex (IPHLPAPI.@)
615 * Get interface index from its name.
617 * PARAMS
618 * AdapterName [In] unicode string with the adapter name
619 * IfIndex [Out] returns found interface index
621 * RETURNS
622 * Success: NO_ERROR
623 * Failure: error code from winerror.h
625 * FIXME
626 * Stub, returns ERROR_NOT_SUPPORTED.
628 DWORD WINAPI GetAdapterIndex(LPWSTR AdapterName, PULONG IfIndex)
630 FIXME("(AdapterName %p, IfIndex %p): stub\n", AdapterName, IfIndex);
631 /* FIXME: implement using getInterfaceIndexByName */
632 return ERROR_NOT_SUPPORTED;
636 /******************************************************************
637 * GetAdaptersInfo (IPHLPAPI.@)
639 * Get information about adapters.
641 * PARAMS
642 * pAdapterInfo [Out] buffer for adapter infos
643 * pOutBufLen [In] length of output buffer
645 * RETURNS
646 * Success: NO_ERROR
647 * Failure: error code from winerror.h
649 DWORD WINAPI GetAdaptersInfo(PIP_ADAPTER_INFO pAdapterInfo, PULONG pOutBufLen)
651 DWORD ret;
653 TRACE("pAdapterInfo %p, pOutBufLen %p\n", pAdapterInfo, pOutBufLen);
654 if (!pOutBufLen)
655 ret = ERROR_INVALID_PARAMETER;
656 else {
657 DWORD numNonLoopbackInterfaces = getNumNonLoopbackInterfaces();
659 if (numNonLoopbackInterfaces > 0) {
660 DWORD numIPAddresses = getNumIPAddresses();
661 ULONG size;
663 /* This may slightly overestimate the amount of space needed, because
664 * the IP addresses include the loopback address, but it's easier
665 * to make sure there's more than enough space than to make sure there's
666 * precisely enough space.
668 size = sizeof(IP_ADAPTER_INFO) * numNonLoopbackInterfaces;
669 size += numIPAddresses * sizeof(IP_ADDR_STRING);
670 if (!pAdapterInfo || *pOutBufLen < size) {
671 *pOutBufLen = size;
672 ret = ERROR_BUFFER_OVERFLOW;
674 else {
675 InterfaceIndexTable *table = NULL;
676 PMIB_IPADDRTABLE ipAddrTable = NULL;
678 ret = getIPAddrTable(&ipAddrTable, GetProcessHeap(), 0);
679 if (!ret)
680 table = getNonLoopbackInterfaceIndexTable();
681 if (table) {
682 size = sizeof(IP_ADAPTER_INFO) * table->numIndexes;
683 size += ipAddrTable->dwNumEntries * sizeof(IP_ADDR_STRING);
684 if (*pOutBufLen < size) {
685 *pOutBufLen = size;
686 ret = ERROR_INSUFFICIENT_BUFFER;
688 else {
689 DWORD ndx;
690 HKEY hKey;
691 BOOL winsEnabled = FALSE;
692 IP_ADDRESS_STRING primaryWINS, secondaryWINS;
693 PIP_ADDR_STRING nextIPAddr = (PIP_ADDR_STRING)((LPBYTE)pAdapterInfo
694 + numNonLoopbackInterfaces * sizeof(IP_ADAPTER_INFO));
696 memset(pAdapterInfo, 0, size);
697 /* @@ Wine registry key: HKCU\Software\Wine\Network */
698 if (RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Network",
699 &hKey) == ERROR_SUCCESS) {
700 DWORD size = sizeof(primaryWINS.String);
701 unsigned long addr;
703 RegQueryValueExA(hKey, "WinsServer", NULL, NULL,
704 (LPBYTE)primaryWINS.String, &size);
705 addr = inet_addr(primaryWINS.String);
706 if (addr != INADDR_NONE && addr != INADDR_ANY)
707 winsEnabled = TRUE;
708 size = sizeof(secondaryWINS.String);
709 RegQueryValueExA(hKey, "BackupWinsServer", NULL, NULL,
710 (LPBYTE)secondaryWINS.String, &size);
711 addr = inet_addr(secondaryWINS.String);
712 if (addr != INADDR_NONE && addr != INADDR_ANY)
713 winsEnabled = TRUE;
714 RegCloseKey(hKey);
716 for (ndx = 0; ndx < table->numIndexes; ndx++) {
717 PIP_ADAPTER_INFO ptr = &pAdapterInfo[ndx];
718 DWORD addrLen = sizeof(ptr->Address), type, i;
719 PIP_ADDR_STRING currentIPAddr = &ptr->IpAddressList;
720 BOOL firstIPAddr = TRUE;
722 /* on Win98 this is left empty, but whatever */
723 getInterfaceNameByIndex(table->indexes[ndx], ptr->AdapterName);
724 getInterfacePhysicalByIndex(table->indexes[ndx], &addrLen,
725 ptr->Address, &type);
726 /* MS defines address length and type as UINT in some places and
727 DWORD in others, **sigh**. Don't want to assume that PUINT and
728 PDWORD are equiv (64-bit?) */
729 ptr->AddressLength = addrLen;
730 ptr->Type = type;
731 ptr->Index = table->indexes[ndx];
732 for (i = 0; i < ipAddrTable->dwNumEntries; i++) {
733 if (ipAddrTable->table[i].dwIndex == ptr->Index) {
734 if (firstIPAddr) {
735 toIPAddressString(ipAddrTable->table[i].dwAddr,
736 ptr->IpAddressList.IpAddress.String);
737 toIPAddressString(ipAddrTable->table[i].dwMask,
738 ptr->IpAddressList.IpMask.String);
739 firstIPAddr = FALSE;
741 else {
742 currentIPAddr->Next = nextIPAddr;
743 currentIPAddr = nextIPAddr;
744 toIPAddressString(ipAddrTable->table[i].dwAddr,
745 currentIPAddr->IpAddress.String);
746 toIPAddressString(ipAddrTable->table[i].dwMask,
747 currentIPAddr->IpMask.String);
748 nextIPAddr++;
752 if (winsEnabled) {
753 ptr->HaveWins = TRUE;
754 memcpy(ptr->PrimaryWinsServer.IpAddress.String,
755 primaryWINS.String, sizeof(primaryWINS.String));
756 memcpy(ptr->SecondaryWinsServer.IpAddress.String,
757 secondaryWINS.String, sizeof(secondaryWINS.String));
759 if (ndx < table->numIndexes - 1)
760 ptr->Next = &pAdapterInfo[ndx + 1];
761 else
762 ptr->Next = NULL;
764 ret = NO_ERROR;
766 HeapFree(GetProcessHeap(), 0, table);
768 else
769 ret = ERROR_OUTOFMEMORY;
770 HeapFree(GetProcessHeap(), 0, ipAddrTable);
773 else
774 ret = ERROR_NO_DATA;
776 TRACE("returning %d\n", ret);
777 return ret;
781 /******************************************************************
782 * GetBestInterface (IPHLPAPI.@)
784 * Get the interface, with the best route for the given IP address.
786 * PARAMS
787 * dwDestAddr [In] IP address to search the interface for
788 * pdwBestIfIndex [Out] found best interface
790 * RETURNS
791 * Success: NO_ERROR
792 * Failure: error code from winerror.h
794 DWORD WINAPI GetBestInterface(IPAddr dwDestAddr, PDWORD pdwBestIfIndex)
796 DWORD ret;
798 TRACE("dwDestAddr 0x%08lx, pdwBestIfIndex %p\n", dwDestAddr, pdwBestIfIndex);
799 if (!pdwBestIfIndex)
800 ret = ERROR_INVALID_PARAMETER;
801 else {
802 MIB_IPFORWARDROW ipRow;
804 ret = GetBestRoute(dwDestAddr, 0, &ipRow);
805 if (ret == ERROR_SUCCESS)
806 *pdwBestIfIndex = ipRow.dwForwardIfIndex;
808 TRACE("returning %d\n", ret);
809 return ret;
813 /******************************************************************
814 * GetBestRoute (IPHLPAPI.@)
816 * Get the best route for the given IP address.
818 * PARAMS
819 * dwDestAddr [In] IP address to search the best route for
820 * dwSourceAddr [In] optional source IP address
821 * pBestRoute [Out] found best route
823 * RETURNS
824 * Success: NO_ERROR
825 * Failure: error code from winerror.h
827 DWORD WINAPI GetBestRoute(DWORD dwDestAddr, DWORD dwSourceAddr, PMIB_IPFORWARDROW pBestRoute)
829 PMIB_IPFORWARDTABLE table;
830 DWORD ret;
832 TRACE("dwDestAddr 0x%08x, dwSourceAddr 0x%08x, pBestRoute %p\n", dwDestAddr,
833 dwSourceAddr, pBestRoute);
834 if (!pBestRoute)
835 return ERROR_INVALID_PARAMETER;
837 AllocateAndGetIpForwardTableFromStack(&table, FALSE, GetProcessHeap(), 0);
838 if (table) {
839 DWORD ndx, matchedBits, matchedNdx = 0;
841 for (ndx = 0, matchedBits = 0; ndx < table->dwNumEntries; ndx++) {
842 if (table->table[ndx].dwForwardType != MIB_IPROUTE_TYPE_INVALID &&
843 (dwDestAddr & table->table[ndx].dwForwardMask) ==
844 (table->table[ndx].dwForwardDest & table->table[ndx].dwForwardMask)) {
845 DWORD numShifts, mask;
847 for (numShifts = 0, mask = table->table[ndx].dwForwardMask;
848 mask && !(mask & 1); mask >>= 1, numShifts++)
850 if (numShifts > matchedBits) {
851 matchedBits = numShifts;
852 matchedNdx = ndx;
856 if (matchedNdx < table->dwNumEntries) {
857 memcpy(pBestRoute, &table->table[matchedNdx], sizeof(MIB_IPFORWARDROW));
858 ret = ERROR_SUCCESS;
860 else {
861 /* No route matches, which can happen if there's no default route. */
862 ret = ERROR_HOST_UNREACHABLE;
864 HeapFree(GetProcessHeap(), 0, table);
866 else
867 ret = ERROR_OUTOFMEMORY;
868 TRACE("returning %d\n", ret);
869 return ret;
873 /******************************************************************
874 * GetFriendlyIfIndex (IPHLPAPI.@)
876 * Get a "friendly" version of IfIndex, which is one that doesn't
877 * have the top byte set. Doesn't validate whether IfIndex is a valid
878 * adapter index.
880 * PARAMS
881 * IfIndex [In] interface index to get the friendly one for
883 * RETURNS
884 * A friendly version of IfIndex.
886 DWORD WINAPI GetFriendlyIfIndex(DWORD IfIndex)
888 /* windows doesn't validate these, either, just makes sure the top byte is
889 cleared. I assume my ifenum module never gives an index with the top
890 byte set. */
891 TRACE("returning %d\n", IfIndex);
892 return IfIndex;
896 /******************************************************************
897 * GetIcmpStatistics (IPHLPAPI.@)
899 * Get the ICMP statistics for the local computer.
901 * PARAMS
902 * pStats [Out] buffer for ICMP statistics
904 * RETURNS
905 * Success: NO_ERROR
906 * Failure: error code from winerror.h
908 DWORD WINAPI GetIcmpStatistics(PMIB_ICMP pStats)
910 DWORD ret;
912 TRACE("pStats %p\n", pStats);
913 ret = getICMPStats(pStats);
914 TRACE("returning %d\n", ret);
915 return ret;
919 /******************************************************************
920 * GetIfEntry (IPHLPAPI.@)
922 * Get information about an interface.
924 * PARAMS
925 * pIfRow [In/Out] In: dwIndex of MIB_IFROW selects the interface.
926 * Out: interface information
928 * RETURNS
929 * Success: NO_ERROR
930 * Failure: error code from winerror.h
932 DWORD WINAPI GetIfEntry(PMIB_IFROW pIfRow)
934 DWORD ret;
935 char nameBuf[MAX_ADAPTER_NAME];
936 char *name;
938 TRACE("pIfRow %p\n", pIfRow);
939 if (!pIfRow)
940 return ERROR_INVALID_PARAMETER;
942 name = getInterfaceNameByIndex(pIfRow->dwIndex, nameBuf);
943 if (name) {
944 ret = getInterfaceEntryByName(name, pIfRow);
945 if (ret == NO_ERROR)
946 ret = getInterfaceStatsByName(name, pIfRow);
948 else
949 ret = ERROR_INVALID_DATA;
950 TRACE("returning %d\n", ret);
951 return ret;
955 static int IfTableSorter(const void *a, const void *b)
957 int ret;
959 if (a && b)
960 ret = ((const MIB_IFROW*)a)->dwIndex - ((const MIB_IFROW*)b)->dwIndex;
961 else
962 ret = 0;
963 return ret;
967 /******************************************************************
968 * GetIfTable (IPHLPAPI.@)
970 * Get a table of local interfaces.
972 * PARAMS
973 * pIfTable [Out] buffer for local interfaces table
974 * pdwSize [In/Out] length of output buffer
975 * bOrder [In] whether to sort the table
977 * RETURNS
978 * Success: NO_ERROR
979 * Failure: error code from winerror.h
981 * NOTES
982 * If pdwSize is less than required, the function will return
983 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the required byte
984 * size.
985 * If bOrder is true, the returned table will be sorted by interface index.
987 DWORD WINAPI GetIfTable(PMIB_IFTABLE pIfTable, PULONG pdwSize, BOOL bOrder)
989 DWORD ret;
991 TRACE("pIfTable %p, pdwSize %p, bOrder %d\n", pdwSize, pdwSize,
992 (DWORD)bOrder);
993 if (!pdwSize)
994 ret = ERROR_INVALID_PARAMETER;
995 else {
996 DWORD numInterfaces = getNumInterfaces();
997 ULONG size = sizeof(MIB_IFTABLE) + (numInterfaces - 1) * sizeof(MIB_IFROW);
999 if (!pIfTable || *pdwSize < size) {
1000 *pdwSize = size;
1001 ret = ERROR_INSUFFICIENT_BUFFER;
1003 else {
1004 InterfaceIndexTable *table = getInterfaceIndexTable();
1006 if (table) {
1007 size = sizeof(MIB_IFTABLE) + (table->numIndexes - 1) *
1008 sizeof(MIB_IFROW);
1009 if (*pdwSize < size) {
1010 *pdwSize = size;
1011 ret = ERROR_INSUFFICIENT_BUFFER;
1013 else {
1014 DWORD ndx;
1016 *pdwSize = size;
1017 pIfTable->dwNumEntries = 0;
1018 for (ndx = 0; ndx < table->numIndexes; ndx++) {
1019 pIfTable->table[ndx].dwIndex = table->indexes[ndx];
1020 GetIfEntry(&pIfTable->table[ndx]);
1021 pIfTable->dwNumEntries++;
1023 if (bOrder)
1024 qsort(pIfTable->table, pIfTable->dwNumEntries, sizeof(MIB_IFROW),
1025 IfTableSorter);
1026 ret = NO_ERROR;
1028 HeapFree(GetProcessHeap(), 0, table);
1030 else
1031 ret = ERROR_OUTOFMEMORY;
1034 TRACE("returning %d\n", ret);
1035 return ret;
1039 /******************************************************************
1040 * GetInterfaceInfo (IPHLPAPI.@)
1042 * Get a list of network interface adapters.
1044 * PARAMS
1045 * pIfTable [Out] buffer for interface adapters
1046 * dwOutBufLen [Out] if buffer is too small, returns required size
1048 * RETURNS
1049 * Success: NO_ERROR
1050 * Failure: error code from winerror.h
1052 * BUGS
1053 * MSDN states this should return non-loopback interfaces only.
1055 DWORD WINAPI GetInterfaceInfo(PIP_INTERFACE_INFO pIfTable, PULONG dwOutBufLen)
1057 DWORD ret;
1059 TRACE("pIfTable %p, dwOutBufLen %p\n", pIfTable, dwOutBufLen);
1060 if (!dwOutBufLen)
1061 ret = ERROR_INVALID_PARAMETER;
1062 else {
1063 DWORD numInterfaces = getNumInterfaces();
1064 ULONG size = sizeof(IP_INTERFACE_INFO) + (numInterfaces - 1) *
1065 sizeof(IP_ADAPTER_INDEX_MAP);
1067 if (!pIfTable || *dwOutBufLen < size) {
1068 *dwOutBufLen = size;
1069 ret = ERROR_INSUFFICIENT_BUFFER;
1071 else {
1072 InterfaceIndexTable *table = getInterfaceIndexTable();
1074 if (table) {
1075 size = sizeof(IP_INTERFACE_INFO) + (table->numIndexes - 1) *
1076 sizeof(IP_ADAPTER_INDEX_MAP);
1077 if (*dwOutBufLen < size) {
1078 *dwOutBufLen = size;
1079 ret = ERROR_INSUFFICIENT_BUFFER;
1081 else {
1082 DWORD ndx;
1083 char nameBuf[MAX_ADAPTER_NAME];
1085 *dwOutBufLen = size;
1086 pIfTable->NumAdapters = 0;
1087 for (ndx = 0; ndx < table->numIndexes; ndx++) {
1088 const char *walker, *name;
1089 WCHAR *assigner;
1091 pIfTable->Adapter[ndx].Index = table->indexes[ndx];
1092 name = getInterfaceNameByIndex(table->indexes[ndx], nameBuf);
1093 for (walker = name, assigner = pIfTable->Adapter[ndx].Name;
1094 walker && *walker &&
1095 assigner - pIfTable->Adapter[ndx].Name < MAX_ADAPTER_NAME - 1;
1096 walker++, assigner++)
1097 *assigner = *walker;
1098 *assigner = 0;
1099 pIfTable->NumAdapters++;
1101 ret = NO_ERROR;
1103 HeapFree(GetProcessHeap(), 0, table);
1105 else
1106 ret = ERROR_OUTOFMEMORY;
1109 TRACE("returning %d\n", ret);
1110 return ret;
1114 /******************************************************************
1115 * GetIpAddrTable (IPHLPAPI.@)
1117 * Get interface-to-IP address mapping table.
1119 * PARAMS
1120 * pIpAddrTable [Out] buffer for mapping table
1121 * pdwSize [In/Out] length of output buffer
1122 * bOrder [In] whether to sort the table
1124 * RETURNS
1125 * Success: NO_ERROR
1126 * Failure: error code from winerror.h
1128 * NOTES
1129 * If pdwSize is less than required, the function will return
1130 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the required byte
1131 * size.
1132 * If bOrder is true, the returned table will be sorted by the next hop and
1133 * an assortment of arbitrary parameters.
1135 DWORD WINAPI GetIpAddrTable(PMIB_IPADDRTABLE pIpAddrTable, PULONG pdwSize, BOOL bOrder)
1137 DWORD ret;
1139 TRACE("pIpAddrTable %p, pdwSize %p, bOrder %d\n", pIpAddrTable, pdwSize,
1140 (DWORD)bOrder);
1141 if (!pdwSize)
1142 ret = ERROR_INVALID_PARAMETER;
1143 else {
1144 PMIB_IPADDRTABLE table;
1146 ret = getIPAddrTable(&table, GetProcessHeap(), 0);
1147 if (ret == NO_ERROR)
1149 ULONG size = sizeof(MIB_IPADDRTABLE) + (table->dwNumEntries - 1) *
1150 sizeof(MIB_IPADDRROW);
1152 if (!pIpAddrTable || *pdwSize < size) {
1153 *pdwSize = size;
1154 ret = ERROR_INSUFFICIENT_BUFFER;
1156 else {
1157 *pdwSize = size;
1158 memcpy(pIpAddrTable, table, sizeof(MIB_IPADDRTABLE) +
1159 (table->dwNumEntries - 1) * sizeof(MIB_IPADDRROW));
1160 if (bOrder)
1161 qsort(pIpAddrTable->table, pIpAddrTable->dwNumEntries,
1162 sizeof(MIB_IPADDRROW), IpAddrTableSorter);
1163 ret = NO_ERROR;
1165 HeapFree(GetProcessHeap(), 0, table);
1168 TRACE("returning %d\n", ret);
1169 return ret;
1173 /******************************************************************
1174 * GetIpForwardTable (IPHLPAPI.@)
1176 * Get the route table.
1178 * PARAMS
1179 * pIpForwardTable [Out] buffer for route table
1180 * pdwSize [In/Out] length of output buffer
1181 * bOrder [In] whether to sort the table
1183 * RETURNS
1184 * Success: NO_ERROR
1185 * Failure: error code from winerror.h
1187 * NOTES
1188 * If pdwSize is less than required, the function will return
1189 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the required byte
1190 * size.
1191 * If bOrder is true, the returned table will be sorted by the next hop and
1192 * an assortment of arbitrary parameters.
1194 DWORD WINAPI GetIpForwardTable(PMIB_IPFORWARDTABLE pIpForwardTable, PULONG pdwSize, BOOL bOrder)
1196 DWORD ret;
1198 TRACE("pIpForwardTable %p, pdwSize %p, bOrder %d\n", pIpForwardTable,
1199 pdwSize, (DWORD)bOrder);
1200 if (!pdwSize)
1201 ret = ERROR_INVALID_PARAMETER;
1202 else {
1203 DWORD numRoutes = getNumRoutes();
1204 ULONG sizeNeeded = sizeof(MIB_IPFORWARDTABLE) + (numRoutes - 1) *
1205 sizeof(MIB_IPFORWARDROW);
1207 if (!pIpForwardTable || *pdwSize < sizeNeeded) {
1208 *pdwSize = sizeNeeded;
1209 ret = ERROR_INSUFFICIENT_BUFFER;
1211 else {
1212 PMIB_IPFORWARDTABLE table;
1214 ret = getRouteTable(&table, GetProcessHeap(), 0);
1215 if (!ret) {
1216 sizeNeeded = sizeof(MIB_IPFORWARDTABLE) + (table->dwNumEntries - 1) *
1217 sizeof(MIB_IPFORWARDROW);
1218 if (*pdwSize < sizeNeeded) {
1219 *pdwSize = sizeNeeded;
1220 ret = ERROR_INSUFFICIENT_BUFFER;
1222 else {
1223 *pdwSize = sizeNeeded;
1224 memcpy(pIpForwardTable, table, sizeNeeded);
1225 if (bOrder)
1226 qsort(pIpForwardTable->table, pIpForwardTable->dwNumEntries,
1227 sizeof(MIB_IPFORWARDROW), IpForwardTableSorter);
1228 ret = NO_ERROR;
1230 HeapFree(GetProcessHeap(), 0, table);
1234 TRACE("returning %d\n", ret);
1235 return ret;
1239 /******************************************************************
1240 * GetIpNetTable (IPHLPAPI.@)
1242 * Get the IP-to-physical address mapping table.
1244 * PARAMS
1245 * pIpNetTable [Out] buffer for mapping table
1246 * pdwSize [In/Out] length of output buffer
1247 * bOrder [In] whether to sort the table
1249 * RETURNS
1250 * Success: NO_ERROR
1251 * Failure: error code from winerror.h
1253 * NOTES
1254 * If pdwSize is less than required, the function will return
1255 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the required byte
1256 * size.
1257 * If bOrder is true, the returned table will be sorted by IP address.
1259 DWORD WINAPI GetIpNetTable(PMIB_IPNETTABLE pIpNetTable, PULONG pdwSize, BOOL bOrder)
1261 DWORD ret;
1263 TRACE("pIpNetTable %p, pdwSize %p, bOrder %d\n", pIpNetTable, pdwSize,
1264 (DWORD)bOrder);
1265 if (!pdwSize)
1266 ret = ERROR_INVALID_PARAMETER;
1267 else {
1268 DWORD numEntries = getNumArpEntries();
1269 ULONG size = sizeof(MIB_IPNETTABLE) + (numEntries - 1) *
1270 sizeof(MIB_IPNETROW);
1272 if (!pIpNetTable || *pdwSize < size) {
1273 *pdwSize = size;
1274 ret = ERROR_INSUFFICIENT_BUFFER;
1276 else {
1277 PMIB_IPNETTABLE table;
1279 ret = getArpTable(&table, GetProcessHeap(), 0);
1280 if (!ret) {
1281 size = sizeof(MIB_IPNETTABLE) + (table->dwNumEntries - 1) *
1282 sizeof(MIB_IPNETROW);
1283 if (*pdwSize < size) {
1284 *pdwSize = size;
1285 ret = ERROR_INSUFFICIENT_BUFFER;
1287 else {
1288 *pdwSize = size;
1289 memcpy(pIpNetTable, table, size);
1290 if (bOrder)
1291 qsort(pIpNetTable->table, pIpNetTable->dwNumEntries,
1292 sizeof(MIB_IPNETROW), IpNetTableSorter);
1293 ret = NO_ERROR;
1295 HeapFree(GetProcessHeap(), 0, table);
1299 TRACE("returning %d\n", ret);
1300 return ret;
1304 /******************************************************************
1305 * GetIpStatistics (IPHLPAPI.@)
1307 * Get the IP statistics for the local computer.
1309 * PARAMS
1310 * pStats [Out] buffer for IP statistics
1312 * RETURNS
1313 * Success: NO_ERROR
1314 * Failure: error code from winerror.h
1316 DWORD WINAPI GetIpStatistics(PMIB_IPSTATS pStats)
1318 DWORD ret;
1320 TRACE("pStats %p\n", pStats);
1321 ret = getIPStats(pStats);
1322 TRACE("returning %d\n", ret);
1323 return ret;
1327 /******************************************************************
1328 * GetNetworkParams (IPHLPAPI.@)
1330 * Get the network parameters for the local computer.
1332 * PARAMS
1333 * pFixedInfo [Out] buffer for network parameters
1334 * pOutBufLen [In/Out] length of output buffer
1336 * RETURNS
1337 * Success: NO_ERROR
1338 * Failure: error code from winerror.h
1340 * NOTES
1341 * If pOutBufLen is less than required, the function will return
1342 * ERROR_INSUFFICIENT_BUFFER, and pOutBufLen will be set to the required byte
1343 * size.
1345 DWORD WINAPI GetNetworkParams(PFIXED_INFO pFixedInfo, PULONG pOutBufLen)
1347 DWORD ret, size;
1348 LONG regReturn;
1349 HKEY hKey;
1351 TRACE("pFixedInfo %p, pOutBufLen %p\n", pFixedInfo, pOutBufLen);
1352 if (!pOutBufLen)
1353 return ERROR_INVALID_PARAMETER;
1355 initialise_resolver();
1356 size = sizeof(FIXED_INFO) + (_res.nscount > 0 ? (_res.nscount - 1) *
1357 sizeof(IP_ADDR_STRING) : 0);
1358 if (!pFixedInfo || *pOutBufLen < size) {
1359 *pOutBufLen = size;
1360 return ERROR_BUFFER_OVERFLOW;
1363 memset(pFixedInfo, 0, size);
1364 size = sizeof(pFixedInfo->HostName);
1365 GetComputerNameExA(ComputerNameDnsHostname, pFixedInfo->HostName, &size);
1366 size = sizeof(pFixedInfo->DomainName);
1367 GetComputerNameExA(ComputerNameDnsDomain, pFixedInfo->DomainName, &size);
1368 if (_res.nscount > 0) {
1369 PIP_ADDR_STRING ptr;
1370 int i;
1372 for (i = 0, ptr = &pFixedInfo->DnsServerList; i < _res.nscount && ptr;
1373 i++, ptr = ptr->Next) {
1374 toIPAddressString(_res.nsaddr_list[i].sin_addr.s_addr,
1375 ptr->IpAddress.String);
1376 if (i == _res.nscount - 1)
1377 ptr->Next = NULL;
1378 else if (i == 0)
1379 ptr->Next = (PIP_ADDR_STRING)((LPBYTE)pFixedInfo + sizeof(FIXED_INFO));
1380 else
1381 ptr->Next = (PIP_ADDR_STRING)((PBYTE)ptr + sizeof(IP_ADDR_STRING));
1384 pFixedInfo->NodeType = HYBRID_NODETYPE;
1385 regReturn = RegOpenKeyExA(HKEY_LOCAL_MACHINE,
1386 "SYSTEM\\CurrentControlSet\\Services\\VxD\\MSTCP", 0, KEY_READ, &hKey);
1387 if (regReturn != ERROR_SUCCESS)
1388 regReturn = RegOpenKeyExA(HKEY_LOCAL_MACHINE,
1389 "SYSTEM\\CurrentControlSet\\Services\\NetBT\\Parameters", 0, KEY_READ,
1390 &hKey);
1391 if (regReturn == ERROR_SUCCESS)
1393 DWORD size = sizeof(pFixedInfo->ScopeId);
1395 RegQueryValueExA(hKey, "ScopeID", NULL, NULL, (LPBYTE)pFixedInfo->ScopeId, &size);
1396 RegCloseKey(hKey);
1399 /* FIXME: can check whether routing's enabled in /proc/sys/net/ipv4/ip_forward
1400 I suppose could also check for a listener on port 53 to set EnableDns */
1401 ret = NO_ERROR;
1402 TRACE("returning %d\n", ret);
1403 return ret;
1407 /******************************************************************
1408 * GetNumberOfInterfaces (IPHLPAPI.@)
1410 * Get the number of interfaces.
1412 * PARAMS
1413 * pdwNumIf [Out] number of interfaces
1415 * RETURNS
1416 * NO_ERROR on success, ERROR_INVALID_PARAMETER if pdwNumIf is NULL.
1418 DWORD WINAPI GetNumberOfInterfaces(PDWORD pdwNumIf)
1420 DWORD ret;
1422 TRACE("pdwNumIf %p\n", pdwNumIf);
1423 if (!pdwNumIf)
1424 ret = ERROR_INVALID_PARAMETER;
1425 else {
1426 *pdwNumIf = getNumInterfaces();
1427 ret = NO_ERROR;
1429 TRACE("returning %d\n", ret);
1430 return ret;
1434 /******************************************************************
1435 * GetPerAdapterInfo (IPHLPAPI.@)
1437 * Get information about an adapter corresponding to an interface.
1439 * PARAMS
1440 * IfIndex [In] interface info
1441 * pPerAdapterInfo [Out] buffer for per adapter info
1442 * pOutBufLen [In/Out] length of output buffer
1444 * RETURNS
1445 * Success: NO_ERROR
1446 * Failure: error code from winerror.h
1448 * FIXME
1449 * Stub, returns ERROR_NOT_SUPPORTED.
1451 DWORD WINAPI GetPerAdapterInfo(ULONG IfIndex, PIP_PER_ADAPTER_INFO pPerAdapterInfo, PULONG pOutBufLen)
1453 TRACE("(IfIndex %d, pPerAdapterInfo %p, pOutBufLen %p)\n", IfIndex,
1454 pPerAdapterInfo, pOutBufLen);
1455 return ERROR_NOT_SUPPORTED;
1459 /******************************************************************
1460 * GetRTTAndHopCount (IPHLPAPI.@)
1462 * Get round-trip time (RTT) and hop count.
1464 * PARAMS
1466 * DestIpAddress [In] destination address to get the info for
1467 * HopCount [Out] retrieved hop count
1468 * MaxHops [In] maximum hops to search for the destination
1469 * RTT [Out] RTT in milliseconds
1471 * RETURNS
1472 * Success: TRUE
1473 * Failure: FALSE
1475 * FIXME
1476 * Stub, returns FALSE.
1478 BOOL WINAPI GetRTTAndHopCount(IPAddr DestIpAddress, PULONG HopCount, ULONG MaxHops, PULONG RTT)
1480 FIXME("(DestIpAddress 0x%08lx, HopCount %p, MaxHops %d, RTT %p): stub\n",
1481 DestIpAddress, HopCount, MaxHops, RTT);
1482 return FALSE;
1486 /******************************************************************
1487 * GetTcpStatistics (IPHLPAPI.@)
1489 * Get the TCP statistics for the local computer.
1491 * PARAMS
1492 * pStats [Out] buffer for TCP statistics
1494 * RETURNS
1495 * Success: NO_ERROR
1496 * Failure: error code from winerror.h
1498 DWORD WINAPI GetTcpStatistics(PMIB_TCPSTATS pStats)
1500 DWORD ret;
1502 TRACE("pStats %p\n", pStats);
1503 ret = getTCPStats(pStats);
1504 TRACE("returning %d\n", ret);
1505 return ret;
1509 /******************************************************************
1510 * GetTcpTable (IPHLPAPI.@)
1512 * Get the table of active TCP connections.
1514 * PARAMS
1515 * pTcpTable [Out] buffer for TCP connections table
1516 * pdwSize [In/Out] length of output buffer
1517 * bOrder [In] whether to order the table
1519 * RETURNS
1520 * Success: NO_ERROR
1521 * Failure: error code from winerror.h
1523 * NOTES
1524 * If pdwSize is less than required, the function will return
1525 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to
1526 * the required byte size.
1527 * If bOrder is true, the returned table will be sorted, first by
1528 * local address and port number, then by remote address and port
1529 * number.
1531 DWORD WINAPI GetTcpTable(PMIB_TCPTABLE pTcpTable, PDWORD pdwSize, BOOL bOrder)
1533 DWORD ret;
1535 TRACE("pTcpTable %p, pdwSize %p, bOrder %d\n", pTcpTable, pdwSize,
1536 (DWORD)bOrder);
1537 if (!pdwSize)
1538 ret = ERROR_INVALID_PARAMETER;
1539 else {
1540 DWORD numEntries = getNumTcpEntries();
1541 DWORD size = sizeof(MIB_TCPTABLE) + (numEntries - 1) * sizeof(MIB_TCPROW);
1543 if (!pTcpTable || *pdwSize < size) {
1544 *pdwSize = size;
1545 ret = ERROR_INSUFFICIENT_BUFFER;
1547 else {
1548 PMIB_TCPTABLE table;
1550 ret = getTcpTable(&table, GetProcessHeap(), 0);
1551 if (!ret) {
1552 size = sizeof(MIB_TCPTABLE) + (table->dwNumEntries - 1) *
1553 sizeof(MIB_TCPROW);
1554 if (*pdwSize < size) {
1555 *pdwSize = size;
1556 ret = ERROR_INSUFFICIENT_BUFFER;
1558 else {
1559 *pdwSize = size;
1560 memcpy(pTcpTable, table, size);
1561 if (bOrder)
1562 qsort(pTcpTable->table, pTcpTable->dwNumEntries,
1563 sizeof(MIB_TCPROW), TcpTableSorter);
1564 ret = NO_ERROR;
1566 HeapFree(GetProcessHeap(), 0, table);
1568 else
1569 ret = ERROR_OUTOFMEMORY;
1572 TRACE("returning %d\n", ret);
1573 return ret;
1577 /******************************************************************
1578 * GetUdpStatistics (IPHLPAPI.@)
1580 * Get the UDP statistics for the local computer.
1582 * PARAMS
1583 * pStats [Out] buffer for UDP statistics
1585 * RETURNS
1586 * Success: NO_ERROR
1587 * Failure: error code from winerror.h
1589 DWORD WINAPI GetUdpStatistics(PMIB_UDPSTATS pStats)
1591 DWORD ret;
1593 TRACE("pStats %p\n", pStats);
1594 ret = getUDPStats(pStats);
1595 TRACE("returning %d\n", ret);
1596 return ret;
1600 /******************************************************************
1601 * GetUdpTable (IPHLPAPI.@)
1603 * Get a table of active UDP connections.
1605 * PARAMS
1606 * pUdpTable [Out] buffer for UDP connections table
1607 * pdwSize [In/Out] length of output buffer
1608 * bOrder [In] whether to order the table
1610 * RETURNS
1611 * Success: NO_ERROR
1612 * Failure: error code from winerror.h
1614 * NOTES
1615 * If pdwSize is less than required, the function will return
1616 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the
1617 * required byte size.
1618 * If bOrder is true, the returned table will be sorted, first by
1619 * local address, then by local port number.
1621 DWORD WINAPI GetUdpTable(PMIB_UDPTABLE pUdpTable, PDWORD pdwSize, BOOL bOrder)
1623 DWORD ret;
1625 TRACE("pUdpTable %p, pdwSize %p, bOrder %d\n", pUdpTable, pdwSize,
1626 (DWORD)bOrder);
1627 if (!pdwSize)
1628 ret = ERROR_INVALID_PARAMETER;
1629 else {
1630 DWORD numEntries = getNumUdpEntries();
1631 DWORD size = sizeof(MIB_UDPTABLE) + (numEntries - 1) * sizeof(MIB_UDPROW);
1633 if (!pUdpTable || *pdwSize < size) {
1634 *pdwSize = size;
1635 ret = ERROR_INSUFFICIENT_BUFFER;
1637 else {
1638 PMIB_UDPTABLE table;
1640 ret = getUdpTable(&table, GetProcessHeap(), 0);
1641 if (!ret) {
1642 size = sizeof(MIB_UDPTABLE) + (table->dwNumEntries - 1) *
1643 sizeof(MIB_UDPROW);
1644 if (*pdwSize < size) {
1645 *pdwSize = size;
1646 ret = ERROR_INSUFFICIENT_BUFFER;
1648 else {
1649 *pdwSize = size;
1650 memcpy(pUdpTable, table, size);
1651 if (bOrder)
1652 qsort(pUdpTable->table, pUdpTable->dwNumEntries,
1653 sizeof(MIB_UDPROW), UdpTableSorter);
1654 ret = NO_ERROR;
1656 HeapFree(GetProcessHeap(), 0, table);
1658 else
1659 ret = ERROR_OUTOFMEMORY;
1662 TRACE("returning %d\n", ret);
1663 return ret;
1667 /******************************************************************
1668 * GetUniDirectionalAdapterInfo (IPHLPAPI.@)
1670 * This is a Win98-only function to get information on "unidirectional"
1671 * adapters. Since this is pretty nonsensical in other contexts, it
1672 * never returns anything.
1674 * PARAMS
1675 * pIPIfInfo [Out] buffer for adapter infos
1676 * dwOutBufLen [Out] length of the output buffer
1678 * RETURNS
1679 * Success: NO_ERROR
1680 * Failure: error code from winerror.h
1682 * FIXME
1683 * Stub, returns ERROR_NOT_SUPPORTED.
1685 DWORD WINAPI GetUniDirectionalAdapterInfo(PIP_UNIDIRECTIONAL_ADAPTER_ADDRESS pIPIfInfo, PULONG dwOutBufLen)
1687 TRACE("pIPIfInfo %p, dwOutBufLen %p\n", pIPIfInfo, dwOutBufLen);
1688 /* a unidirectional adapter?? not bloody likely! */
1689 return ERROR_NOT_SUPPORTED;
1693 /******************************************************************
1694 * IpReleaseAddress (IPHLPAPI.@)
1696 * Release an IP optained through DHCP,
1698 * PARAMS
1699 * AdapterInfo [In] adapter to release IP address
1701 * RETURNS
1702 * Success: NO_ERROR
1703 * Failure: error code from winerror.h
1705 * NOTES
1706 * Since GetAdaptersInfo never returns adapters that have DHCP enabled,
1707 * this function does nothing.
1709 * FIXME
1710 * Stub, returns ERROR_NOT_SUPPORTED.
1712 DWORD WINAPI IpReleaseAddress(PIP_ADAPTER_INDEX_MAP AdapterInfo)
1714 TRACE("AdapterInfo %p\n", AdapterInfo);
1715 /* not a stub, never going to support this (and I never mark an adapter as
1716 DHCP enabled, see GetAdaptersInfo, so this should never get called) */
1717 return ERROR_NOT_SUPPORTED;
1721 /******************************************************************
1722 * IpRenewAddress (IPHLPAPI.@)
1724 * Renew an IP optained through DHCP.
1726 * PARAMS
1727 * AdapterInfo [In] adapter to renew IP address
1729 * RETURNS
1730 * Success: NO_ERROR
1731 * Failure: error code from winerror.h
1733 * NOTES
1734 * Since GetAdaptersInfo never returns adapters that have DHCP enabled,
1735 * this function does nothing.
1737 * FIXME
1738 * Stub, returns ERROR_NOT_SUPPORTED.
1740 DWORD WINAPI IpRenewAddress(PIP_ADAPTER_INDEX_MAP AdapterInfo)
1742 TRACE("AdapterInfo %p\n", AdapterInfo);
1743 /* not a stub, never going to support this (and I never mark an adapter as
1744 DHCP enabled, see GetAdaptersInfo, so this should never get called) */
1745 return ERROR_NOT_SUPPORTED;
1749 /******************************************************************
1750 * NotifyAddrChange (IPHLPAPI.@)
1752 * Notify caller whenever the ip-interface map is changed.
1754 * PARAMS
1755 * Handle [Out] handle useable in asynchronus notification
1756 * overlapped [In] overlapped structure that notifies the caller
1758 * RETURNS
1759 * Success: NO_ERROR
1760 * Failure: error code from winerror.h
1762 * FIXME
1763 * Stub, returns ERROR_NOT_SUPPORTED.
1765 DWORD WINAPI NotifyAddrChange(PHANDLE Handle, LPOVERLAPPED overlapped)
1767 FIXME("(Handle %p, overlapped %p): stub\n", Handle, overlapped);
1768 return ERROR_NOT_SUPPORTED;
1772 /******************************************************************
1773 * NotifyRouteChange (IPHLPAPI.@)
1775 * Notify caller whenever the ip routing table is changed.
1777 * PARAMS
1778 * Handle [Out] handle useable in asynchronus notification
1779 * overlapped [In] overlapped structure that notifies the caller
1781 * RETURNS
1782 * Success: NO_ERROR
1783 * Failure: error code from winerror.h
1785 * FIXME
1786 * Stub, returns ERROR_NOT_SUPPORTED.
1788 DWORD WINAPI NotifyRouteChange(PHANDLE Handle, LPOVERLAPPED overlapped)
1790 FIXME("(Handle %p, overlapped %p): stub\n", Handle, overlapped);
1791 return ERROR_NOT_SUPPORTED;
1795 /******************************************************************
1796 * SendARP (IPHLPAPI.@)
1798 * Send an ARP request.
1800 * PARAMS
1801 * DestIP [In] attempt to obtain this IP
1802 * SrcIP [In] optional sender IP address
1803 * pMacAddr [Out] buffer for the mac address
1804 * PhyAddrLen [In/Out] length of the output buffer
1806 * RETURNS
1807 * Success: NO_ERROR
1808 * Failure: error code from winerror.h
1810 * FIXME
1811 * Stub, returns ERROR_NOT_SUPPORTED.
1813 DWORD WINAPI SendARP(IPAddr DestIP, IPAddr SrcIP, PULONG pMacAddr, PULONG PhyAddrLen)
1815 FIXME("(DestIP 0x%08lx, SrcIP 0x%08lx, pMacAddr %p, PhyAddrLen %p): stub\n",
1816 DestIP, SrcIP, pMacAddr, PhyAddrLen);
1817 return ERROR_NOT_SUPPORTED;
1821 /******************************************************************
1822 * SetIfEntry (IPHLPAPI.@)
1824 * Set the administrative status of an interface.
1826 * PARAMS
1827 * pIfRow [In] dwAdminStatus member specifies the new status.
1829 * RETURNS
1830 * Success: NO_ERROR
1831 * Failure: error code from winerror.h
1833 * FIXME
1834 * Stub, returns ERROR_NOT_SUPPORTED.
1836 DWORD WINAPI SetIfEntry(PMIB_IFROW pIfRow)
1838 FIXME("(pIfRow %p): stub\n", pIfRow);
1839 /* this is supposed to set an interface administratively up or down.
1840 Could do SIOCSIFFLAGS and set/clear IFF_UP, but, not sure I want to, and
1841 this sort of down is indistinguishable from other sorts of down (e.g. no
1842 link). */
1843 return ERROR_NOT_SUPPORTED;
1847 /******************************************************************
1848 * SetIpForwardEntry (IPHLPAPI.@)
1850 * Modify an existing route.
1852 * PARAMS
1853 * pRoute [In] route with the new information
1855 * RETURNS
1856 * Success: NO_ERROR
1857 * Failure: error code from winerror.h
1859 * FIXME
1860 * Stub, returns NO_ERROR.
1862 DWORD WINAPI SetIpForwardEntry(PMIB_IPFORWARDROW pRoute)
1864 FIXME("(pRoute %p): stub\n", pRoute);
1865 /* this is to add a route entry, how's it distinguishable from
1866 CreateIpForwardEntry?
1867 could use SIOCADDRT, not sure I want to */
1868 return (DWORD) 0;
1872 /******************************************************************
1873 * SetIpNetEntry (IPHLPAPI.@)
1875 * Modify an existing ARP entry.
1877 * PARAMS
1878 * pArpEntry [In] ARP entry with the new information
1880 * RETURNS
1881 * Success: NO_ERROR
1882 * Failure: error code from winerror.h
1884 * FIXME
1885 * Stub, returns NO_ERROR.
1887 DWORD WINAPI SetIpNetEntry(PMIB_IPNETROW pArpEntry)
1889 FIXME("(pArpEntry %p): stub\n", pArpEntry);
1890 /* same as CreateIpNetEntry here, could use SIOCSARP, not sure I want to */
1891 return (DWORD) 0;
1895 /******************************************************************
1896 * SetIpStatistics (IPHLPAPI.@)
1898 * Toggle IP forwarding and det the default TTL value.
1900 * PARAMS
1901 * pIpStats [In] IP statistics with the new information
1903 * RETURNS
1904 * Success: NO_ERROR
1905 * Failure: error code from winerror.h
1907 * FIXME
1908 * Stub, returns NO_ERROR.
1910 DWORD WINAPI SetIpStatistics(PMIB_IPSTATS pIpStats)
1912 FIXME("(pIpStats %p): stub\n", pIpStats);
1913 return (DWORD) 0;
1917 /******************************************************************
1918 * SetIpTTL (IPHLPAPI.@)
1920 * Set the default TTL value.
1922 * PARAMS
1923 * nTTL [In] new TTL value
1925 * RETURNS
1926 * Success: NO_ERROR
1927 * Failure: error code from winerror.h
1929 * FIXME
1930 * Stub, returns NO_ERROR.
1932 DWORD WINAPI SetIpTTL(UINT nTTL)
1934 FIXME("(nTTL %d): stub\n", nTTL);
1935 /* could echo nTTL > /proc/net/sys/net/ipv4/ip_default_ttl, not sure I
1936 want to. Could map EACCESS to ERROR_ACCESS_DENIED, I suppose */
1937 return (DWORD) 0;
1941 /******************************************************************
1942 * SetTcpEntry (IPHLPAPI.@)
1944 * Set the state of a TCP connection.
1946 * PARAMS
1947 * pTcpRow [In] specifies connection with new state
1949 * RETURNS
1950 * Success: NO_ERROR
1951 * Failure: error code from winerror.h
1953 * FIXME
1954 * Stub, returns NO_ERROR.
1956 DWORD WINAPI SetTcpEntry(PMIB_TCPROW pTcpRow)
1958 FIXME("(pTcpRow %p): stub\n", pTcpRow);
1959 return (DWORD) 0;
1963 /******************************************************************
1964 * UnenableRouter (IPHLPAPI.@)
1966 * Decrement the IP-forwarding reference count. Turn off IP-forwarding
1967 * if it reaches zero.
1969 * PARAMS
1970 * pOverlapped [In/Out] should be the same as in EnableRouter()
1971 * lpdwEnableCount [Out] optional, receives reference count
1973 * RETURNS
1974 * Success: NO_ERROR
1975 * Failure: error code from winerror.h
1977 * FIXME
1978 * Stub, returns ERROR_NOT_SUPPORTED.
1980 DWORD WINAPI UnenableRouter(OVERLAPPED * pOverlapped, LPDWORD lpdwEnableCount)
1982 FIXME("(pOverlapped %p, lpdwEnableCount %p): stub\n", pOverlapped,
1983 lpdwEnableCount);
1984 /* could echo "0" > /proc/net/sys/net/ipv4/ip_forward, not sure I want to
1985 could map EACCESS to ERROR_ACCESS_DENIED, I suppose
1987 return ERROR_NOT_SUPPORTED;