crypt32: Fix German translation.
[wine.git] / dlls / iphlpapi / iphlpapi_main.c
blob13294660a5c90a54eb3c83763a194bde2e99fe33
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_SYS_SOCKET_H
27 #include <sys/socket.h>
28 #endif
29 #ifdef HAVE_NET_IF_H
30 #include <net/if.h>
31 #endif
32 #ifdef HAVE_NETINET_IN_H
33 # include <netinet/in.h>
34 #endif
35 #ifdef HAVE_ARPA_INET_H
36 # include <arpa/inet.h>
37 #endif
38 #ifdef HAVE_ARPA_NAMESER_H
39 # include <arpa/nameser.h>
40 #endif
41 #ifdef HAVE_RESOLV_H
42 # include <resolv.h>
43 #endif
45 #define NONAMELESSUNION
46 #define NONAMELESSSTRUCT
47 #include "windef.h"
48 #include "winbase.h"
49 #include "winreg.h"
50 #define USE_WS_PREFIX
51 #include "winsock2.h"
52 #include "ws2ipdef.h"
53 #include "iphlpapi.h"
54 #include "ifenum.h"
55 #include "ipstats.h"
56 #include "ipifcons.h"
58 #include "wine/debug.h"
60 WINE_DEFAULT_DEBUG_CHANNEL(iphlpapi);
62 #ifndef IF_NAMESIZE
63 #define IF_NAMESIZE 16
64 #endif
66 #ifndef INADDR_NONE
67 #define INADDR_NONE ~0UL
68 #endif
70 /* call res_init() just once because of a bug in Mac OS X 10.4 */
71 /* Call once per thread on systems that have per-thread _res. */
72 static void initialise_resolver(void)
74 if ((_res.options & RES_INIT) == 0)
75 res_init();
78 BOOL WINAPI DllMain (HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
80 switch (fdwReason) {
81 case DLL_PROCESS_ATTACH:
82 DisableThreadLibraryCalls( hinstDLL );
83 break;
85 case DLL_PROCESS_DETACH:
86 break;
88 return TRUE;
91 /******************************************************************
92 * AddIPAddress (IPHLPAPI.@)
94 * Add an IP address to an adapter.
96 * PARAMS
97 * Address [In] IP address to add to the adapter
98 * IpMask [In] subnet mask for the IP address
99 * IfIndex [In] adapter index to add the address
100 * NTEContext [Out] Net Table Entry (NTE) context for the IP address
101 * NTEInstance [Out] NTE instance for the IP address
103 * RETURNS
104 * Success: NO_ERROR
105 * Failure: error code from winerror.h
107 * FIXME
108 * Stub. Currently returns ERROR_NOT_SUPPORTED.
110 DWORD WINAPI AddIPAddress(IPAddr Address, IPMask IpMask, DWORD IfIndex, PULONG NTEContext, PULONG NTEInstance)
112 FIXME(":stub\n");
113 return ERROR_NOT_SUPPORTED;
117 /******************************************************************
118 * AllocateAndGetIfTableFromStack (IPHLPAPI.@)
120 * Get table of local interfaces.
121 * Like GetIfTable(), but allocate the returned table from heap.
123 * PARAMS
124 * ppIfTable [Out] pointer into which the MIB_IFTABLE is
125 * allocated and returned.
126 * bOrder [In] whether to sort the table
127 * heap [In] heap from which the table is allocated
128 * flags [In] flags to HeapAlloc
130 * RETURNS
131 * ERROR_INVALID_PARAMETER if ppIfTable is NULL, whatever
132 * GetIfTable() returns otherwise.
134 DWORD WINAPI AllocateAndGetIfTableFromStack(PMIB_IFTABLE *ppIfTable,
135 BOOL bOrder, HANDLE heap, DWORD flags)
137 DWORD ret;
139 TRACE("ppIfTable %p, bOrder %d, heap %p, flags 0x%08x\n", ppIfTable,
140 bOrder, heap, flags);
141 if (!ppIfTable)
142 ret = ERROR_INVALID_PARAMETER;
143 else {
144 DWORD dwSize = 0;
146 ret = GetIfTable(*ppIfTable, &dwSize, bOrder);
147 if (ret == ERROR_INSUFFICIENT_BUFFER) {
148 *ppIfTable = HeapAlloc(heap, flags, dwSize);
149 ret = GetIfTable(*ppIfTable, &dwSize, bOrder);
152 TRACE("returning %d\n", ret);
153 return ret;
157 static int IpAddrTableSorter(const void *a, const void *b)
159 int ret;
161 if (a && b)
162 ret = ((const MIB_IPADDRROW*)a)->dwAddr - ((const MIB_IPADDRROW*)b)->dwAddr;
163 else
164 ret = 0;
165 return ret;
169 /******************************************************************
170 * AllocateAndGetIpAddrTableFromStack (IPHLPAPI.@)
172 * Get interface-to-IP address mapping table.
173 * Like GetIpAddrTable(), but allocate the returned table from heap.
175 * PARAMS
176 * ppIpAddrTable [Out] pointer into which the MIB_IPADDRTABLE is
177 * allocated and returned.
178 * bOrder [In] whether to sort the table
179 * heap [In] heap from which the table is allocated
180 * flags [In] flags to HeapAlloc
182 * RETURNS
183 * ERROR_INVALID_PARAMETER if ppIpAddrTable is NULL, other error codes on
184 * failure, NO_ERROR on success.
186 DWORD WINAPI AllocateAndGetIpAddrTableFromStack(PMIB_IPADDRTABLE *ppIpAddrTable,
187 BOOL bOrder, HANDLE heap, DWORD flags)
189 DWORD ret;
191 TRACE("ppIpAddrTable %p, bOrder %d, heap %p, flags 0x%08x\n",
192 ppIpAddrTable, bOrder, heap, flags);
193 ret = getIPAddrTable(ppIpAddrTable, heap, flags);
194 if (!ret && bOrder)
195 qsort((*ppIpAddrTable)->table, (*ppIpAddrTable)->dwNumEntries,
196 sizeof(MIB_IPADDRROW), IpAddrTableSorter);
197 TRACE("returning %d\n", ret);
198 return ret;
202 /******************************************************************
203 * CreateIpForwardEntry (IPHLPAPI.@)
205 * Create a route in the local computer's IP table.
207 * PARAMS
208 * pRoute [In] new route information
210 * RETURNS
211 * Success: NO_ERROR
212 * Failure: error code from winerror.h
214 * FIXME
215 * Stub, always returns NO_ERROR.
217 DWORD WINAPI CreateIpForwardEntry(PMIB_IPFORWARDROW pRoute)
219 FIXME("(pRoute %p): stub\n", pRoute);
220 /* could use SIOCADDRT, not sure I want to */
221 return 0;
225 /******************************************************************
226 * CreateIpNetEntry (IPHLPAPI.@)
228 * Create entry in the ARP table.
230 * PARAMS
231 * pArpEntry [In] new ARP entry
233 * RETURNS
234 * Success: NO_ERROR
235 * Failure: error code from winerror.h
237 * FIXME
238 * Stub, always returns NO_ERROR.
240 DWORD WINAPI CreateIpNetEntry(PMIB_IPNETROW pArpEntry)
242 FIXME("(pArpEntry %p)\n", pArpEntry);
243 /* could use SIOCSARP on systems that support it, not sure I want to */
244 return 0;
248 /******************************************************************
249 * CreateProxyArpEntry (IPHLPAPI.@)
251 * Create a Proxy ARP (PARP) entry for an IP address.
253 * PARAMS
254 * dwAddress [In] IP address for which this computer acts as a proxy.
255 * dwMask [In] subnet mask for dwAddress
256 * dwIfIndex [In] interface index
258 * RETURNS
259 * Success: NO_ERROR
260 * Failure: error code from winerror.h
262 * FIXME
263 * Stub, returns ERROR_NOT_SUPPORTED.
265 DWORD WINAPI CreateProxyArpEntry(DWORD dwAddress, DWORD dwMask, DWORD dwIfIndex)
267 FIXME("(dwAddress 0x%08x, dwMask 0x%08x, dwIfIndex 0x%08x): stub\n",
268 dwAddress, dwMask, dwIfIndex);
269 return ERROR_NOT_SUPPORTED;
273 /******************************************************************
274 * DeleteIPAddress (IPHLPAPI.@)
276 * Delete an IP address added with AddIPAddress().
278 * PARAMS
279 * NTEContext [In] NTE context from AddIPAddress();
281 * RETURNS
282 * Success: NO_ERROR
283 * Failure: error code from winerror.h
285 * FIXME
286 * Stub, returns ERROR_NOT_SUPPORTED.
288 DWORD WINAPI DeleteIPAddress(ULONG NTEContext)
290 FIXME("(NTEContext %d): stub\n", NTEContext);
291 return ERROR_NOT_SUPPORTED;
295 /******************************************************************
296 * DeleteIpForwardEntry (IPHLPAPI.@)
298 * Delete a route.
300 * PARAMS
301 * pRoute [In] route to delete
303 * RETURNS
304 * Success: NO_ERROR
305 * Failure: error code from winerror.h
307 * FIXME
308 * Stub, returns NO_ERROR.
310 DWORD WINAPI DeleteIpForwardEntry(PMIB_IPFORWARDROW pRoute)
312 FIXME("(pRoute %p): stub\n", pRoute);
313 /* could use SIOCDELRT, not sure I want to */
314 return 0;
318 /******************************************************************
319 * DeleteIpNetEntry (IPHLPAPI.@)
321 * Delete an ARP entry.
323 * PARAMS
324 * pArpEntry [In] ARP entry to delete
326 * RETURNS
327 * Success: NO_ERROR
328 * Failure: error code from winerror.h
330 * FIXME
331 * Stub, returns NO_ERROR.
333 DWORD WINAPI DeleteIpNetEntry(PMIB_IPNETROW pArpEntry)
335 FIXME("(pArpEntry %p): stub\n", pArpEntry);
336 /* could use SIOCDARP on systems that support it, not sure I want to */
337 return 0;
341 /******************************************************************
342 * DeleteProxyArpEntry (IPHLPAPI.@)
344 * Delete a Proxy ARP entry.
346 * PARAMS
347 * dwAddress [In] IP address for which this computer acts as a proxy.
348 * dwMask [In] subnet mask for dwAddress
349 * dwIfIndex [In] interface index
351 * RETURNS
352 * Success: NO_ERROR
353 * Failure: error code from winerror.h
355 * FIXME
356 * Stub, returns ERROR_NOT_SUPPORTED.
358 DWORD WINAPI DeleteProxyArpEntry(DWORD dwAddress, DWORD dwMask, DWORD dwIfIndex)
360 FIXME("(dwAddress 0x%08x, dwMask 0x%08x, dwIfIndex 0x%08x): stub\n",
361 dwAddress, dwMask, dwIfIndex);
362 return ERROR_NOT_SUPPORTED;
366 /******************************************************************
367 * EnableRouter (IPHLPAPI.@)
369 * Turn on ip forwarding.
371 * PARAMS
372 * pHandle [In/Out]
373 * pOverlapped [In/Out] hEvent member should contain a valid handle.
375 * RETURNS
376 * Success: ERROR_IO_PENDING
377 * Failure: error code from winerror.h
379 * FIXME
380 * Stub, returns ERROR_NOT_SUPPORTED.
382 DWORD WINAPI EnableRouter(HANDLE * pHandle, OVERLAPPED * pOverlapped)
384 FIXME("(pHandle %p, pOverlapped %p): stub\n", pHandle, pOverlapped);
385 /* could echo "1" > /proc/net/sys/net/ipv4/ip_forward, not sure I want to
386 could map EACCESS to ERROR_ACCESS_DENIED, I suppose
388 return ERROR_NOT_SUPPORTED;
392 /******************************************************************
393 * FlushIpNetTable (IPHLPAPI.@)
395 * Delete all ARP entries of an interface
397 * PARAMS
398 * dwIfIndex [In] interface index
400 * RETURNS
401 * Success: NO_ERROR
402 * Failure: error code from winerror.h
404 * FIXME
405 * Stub, returns ERROR_NOT_SUPPORTED.
407 DWORD WINAPI FlushIpNetTable(DWORD dwIfIndex)
409 FIXME("(dwIfIndex 0x%08x): stub\n", dwIfIndex);
410 /* this flushes the arp cache of the given index */
411 return ERROR_NOT_SUPPORTED;
415 /******************************************************************
416 * GetAdapterIndex (IPHLPAPI.@)
418 * Get interface index from its name.
420 * PARAMS
421 * AdapterName [In] unicode string with the adapter name
422 * IfIndex [Out] returns found interface index
424 * RETURNS
425 * Success: NO_ERROR
426 * Failure: error code from winerror.h
428 DWORD WINAPI GetAdapterIndex(LPWSTR AdapterName, PULONG IfIndex)
430 char adapterName[MAX_ADAPTER_NAME];
431 unsigned int i;
432 DWORD ret;
434 TRACE("(AdapterName %p, IfIndex %p)\n", AdapterName, IfIndex);
435 /* The adapter name is guaranteed not to have any unicode characters, so
436 * this translation is never lossy */
437 for (i = 0; i < sizeof(adapterName) - 1 && AdapterName[i]; i++)
438 adapterName[i] = (char)AdapterName[i];
439 adapterName[i] = '\0';
440 ret = getInterfaceIndexByName(adapterName, IfIndex);
441 TRACE("returning %d\n", ret);
442 return ret;
446 /******************************************************************
447 * GetAdaptersInfo (IPHLPAPI.@)
449 * Get information about adapters.
451 * PARAMS
452 * pAdapterInfo [Out] buffer for adapter infos
453 * pOutBufLen [In] length of output buffer
455 * RETURNS
456 * Success: NO_ERROR
457 * Failure: error code from winerror.h
459 DWORD WINAPI GetAdaptersInfo(PIP_ADAPTER_INFO pAdapterInfo, PULONG pOutBufLen)
461 DWORD ret;
463 TRACE("pAdapterInfo %p, pOutBufLen %p\n", pAdapterInfo, pOutBufLen);
464 if (!pOutBufLen)
465 ret = ERROR_INVALID_PARAMETER;
466 else {
467 DWORD numNonLoopbackInterfaces = getNumNonLoopbackInterfaces();
469 if (numNonLoopbackInterfaces > 0) {
470 DWORD numIPAddresses = getNumIPAddresses();
471 ULONG size;
473 /* This may slightly overestimate the amount of space needed, because
474 * the IP addresses include the loopback address, but it's easier
475 * to make sure there's more than enough space than to make sure there's
476 * precisely enough space.
478 size = sizeof(IP_ADAPTER_INFO) * numNonLoopbackInterfaces;
479 size += numIPAddresses * sizeof(IP_ADDR_STRING);
480 if (!pAdapterInfo || *pOutBufLen < size) {
481 *pOutBufLen = size;
482 ret = ERROR_BUFFER_OVERFLOW;
484 else {
485 InterfaceIndexTable *table = NULL;
486 PMIB_IPADDRTABLE ipAddrTable = NULL;
487 PMIB_IPFORWARDTABLE routeTable = NULL;
489 ret = getIPAddrTable(&ipAddrTable, GetProcessHeap(), 0);
490 if (!ret)
491 ret = AllocateAndGetIpForwardTableFromStack(&routeTable, FALSE, GetProcessHeap(), 0);
492 if (!ret)
493 table = getNonLoopbackInterfaceIndexTable();
494 if (table) {
495 size = sizeof(IP_ADAPTER_INFO) * table->numIndexes;
496 size += ipAddrTable->dwNumEntries * sizeof(IP_ADDR_STRING);
497 if (*pOutBufLen < size) {
498 *pOutBufLen = size;
499 ret = ERROR_INSUFFICIENT_BUFFER;
501 else {
502 DWORD ndx;
503 HKEY hKey;
504 BOOL winsEnabled = FALSE;
505 IP_ADDRESS_STRING primaryWINS, secondaryWINS;
506 PIP_ADDR_STRING nextIPAddr = (PIP_ADDR_STRING)((LPBYTE)pAdapterInfo
507 + numNonLoopbackInterfaces * sizeof(IP_ADAPTER_INFO));
509 memset(pAdapterInfo, 0, size);
510 /* @@ Wine registry key: HKCU\Software\Wine\Network */
511 if (RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Network",
512 &hKey) == ERROR_SUCCESS) {
513 DWORD size = sizeof(primaryWINS.String);
514 unsigned long addr;
516 RegQueryValueExA(hKey, "WinsServer", NULL, NULL,
517 (LPBYTE)primaryWINS.String, &size);
518 addr = inet_addr(primaryWINS.String);
519 if (addr != INADDR_NONE && addr != INADDR_ANY)
520 winsEnabled = TRUE;
521 size = sizeof(secondaryWINS.String);
522 RegQueryValueExA(hKey, "BackupWinsServer", NULL, NULL,
523 (LPBYTE)secondaryWINS.String, &size);
524 addr = inet_addr(secondaryWINS.String);
525 if (addr != INADDR_NONE && addr != INADDR_ANY)
526 winsEnabled = TRUE;
527 RegCloseKey(hKey);
529 for (ndx = 0; ndx < table->numIndexes; ndx++) {
530 PIP_ADAPTER_INFO ptr = &pAdapterInfo[ndx];
531 DWORD i;
532 PIP_ADDR_STRING currentIPAddr = &ptr->IpAddressList;
533 BOOL firstIPAddr = TRUE;
535 /* on Win98 this is left empty, but whatever */
536 getInterfaceNameByIndex(table->indexes[ndx], ptr->AdapterName);
537 getInterfaceNameByIndex(table->indexes[ndx], ptr->Description);
538 ptr->AddressLength = sizeof(ptr->Address);
539 getInterfacePhysicalByIndex(table->indexes[ndx],
540 &ptr->AddressLength, ptr->Address, &ptr->Type);
541 ptr->Index = table->indexes[ndx];
542 for (i = 0; i < ipAddrTable->dwNumEntries; i++) {
543 if (ipAddrTable->table[i].dwIndex == ptr->Index) {
544 if (firstIPAddr) {
545 toIPAddressString(ipAddrTable->table[i].dwAddr,
546 ptr->IpAddressList.IpAddress.String);
547 toIPAddressString(ipAddrTable->table[i].dwMask,
548 ptr->IpAddressList.IpMask.String);
549 firstIPAddr = FALSE;
551 else {
552 currentIPAddr->Next = nextIPAddr;
553 currentIPAddr = nextIPAddr;
554 toIPAddressString(ipAddrTable->table[i].dwAddr,
555 currentIPAddr->IpAddress.String);
556 toIPAddressString(ipAddrTable->table[i].dwMask,
557 currentIPAddr->IpMask.String);
558 nextIPAddr++;
562 /* Find first router through this interface, which we'll assume
563 * is the default gateway for this adapter */
564 for (i = 0; i < routeTable->dwNumEntries; i++)
565 if (routeTable->table[i].dwForwardIfIndex == ptr->Index
566 && routeTable->table[i].dwForwardType ==
567 MIB_IPROUTE_TYPE_INDIRECT)
568 toIPAddressString(routeTable->table[i].dwForwardNextHop,
569 ptr->GatewayList.IpAddress.String);
570 if (winsEnabled) {
571 ptr->HaveWins = TRUE;
572 memcpy(ptr->PrimaryWinsServer.IpAddress.String,
573 primaryWINS.String, sizeof(primaryWINS.String));
574 memcpy(ptr->SecondaryWinsServer.IpAddress.String,
575 secondaryWINS.String, sizeof(secondaryWINS.String));
577 if (ndx < table->numIndexes - 1)
578 ptr->Next = &pAdapterInfo[ndx + 1];
579 else
580 ptr->Next = NULL;
582 ret = NO_ERROR;
584 HeapFree(GetProcessHeap(), 0, table);
586 else
587 ret = ERROR_OUTOFMEMORY;
588 HeapFree(GetProcessHeap(), 0, routeTable);
589 HeapFree(GetProcessHeap(), 0, ipAddrTable);
592 else
593 ret = ERROR_NO_DATA;
595 TRACE("returning %d\n", ret);
596 return ret;
599 static DWORD typeFromMibType(DWORD mib_type)
601 switch (mib_type)
603 case MIB_IF_TYPE_ETHERNET: return IF_TYPE_ETHERNET_CSMACD;
604 case MIB_IF_TYPE_TOKENRING: return IF_TYPE_ISO88025_TOKENRING;
605 case MIB_IF_TYPE_PPP: return IF_TYPE_PPP;
606 case MIB_IF_TYPE_LOOPBACK: return IF_TYPE_SOFTWARE_LOOPBACK;
607 default: return IF_TYPE_OTHER;
611 static DWORD connectionTypeFromMibType(DWORD mib_type)
613 switch (mib_type)
615 case MIB_IF_TYPE_PPP: return NET_IF_CONNECTION_DEMAND;
616 case MIB_IF_TYPE_SLIP: return NET_IF_CONNECTION_DEMAND;
617 default: return NET_IF_CONNECTION_DEDICATED;
621 static ULONG v4addressesFromIndex(DWORD index, DWORD **addrs, ULONG *num_addrs)
623 ULONG ret, i, j;
624 MIB_IPADDRTABLE *at;
626 *num_addrs = 0;
627 if ((ret = getIPAddrTable(&at, GetProcessHeap(), 0))) return ret;
628 for (i = 0; i < at->dwNumEntries; i++)
630 if (at->table[i].dwIndex == index) (*num_addrs)++;
632 if (!(*addrs = HeapAlloc(GetProcessHeap(), 0, *num_addrs * sizeof(DWORD))))
634 HeapFree(GetProcessHeap(), 0, at);
635 return ERROR_OUTOFMEMORY;
637 for (i = 0, j = 0; i < at->dwNumEntries; i++)
639 if (at->table[i].dwIndex == index) (*addrs)[j++] = at->table[i].dwAddr;
641 HeapFree(GetProcessHeap(), 0, at);
642 return ERROR_SUCCESS;
645 static char *debugstr_ipv4(const in_addr_t *in_addr, char *buf)
647 const BYTE *addrp;
648 char *p = buf;
650 for (addrp = (const BYTE *)in_addr;
651 addrp - (const BYTE *)in_addr < sizeof(*in_addr);
652 addrp++)
654 if (addrp == (const BYTE *)in_addr + sizeof(*in_addr) - 1)
655 sprintf(p, "%d", *addrp);
656 else
657 p += sprintf(p, "%d.", *addrp);
659 return buf;
662 static char *debugstr_ipv6(const struct WS_sockaddr_in6 *sin, char *buf)
664 const IN6_ADDR *addr = &sin->sin6_addr;
665 char *p = buf;
666 int i;
667 BOOL in_zero = FALSE;
669 for (i = 0; i < 7; i++)
671 if (!addr->u.Word[i])
673 if (i == 0)
674 *p++ = ':';
675 if (!in_zero)
677 *p++ = ':';
678 in_zero = TRUE;
681 else
683 p += sprintf(p, "%x:", ntohs(addr->u.Word[i]));
684 in_zero = FALSE;
687 sprintf(p, "%x", ntohs(addr->u.Word[7]));
688 return buf;
691 static ULONG count_v4_gateways(DWORD index, PMIB_IPFORWARDTABLE routeTable)
693 DWORD i, num_gateways = 0;
695 for (i = 0; i < routeTable->dwNumEntries; i++)
697 if (routeTable->table[i].dwForwardIfIndex == index &&
698 routeTable->table[i].dwForwardType == MIB_IPROUTE_TYPE_INDIRECT)
699 num_gateways++;
701 return num_gateways;
704 static PMIB_IPFORWARDROW findIPv4Gateway(DWORD index,
705 PMIB_IPFORWARDTABLE routeTable)
707 DWORD i;
708 PMIB_IPFORWARDROW row = NULL;
710 for (i = 0; !row && i < routeTable->dwNumEntries; i++)
712 if (routeTable->table[i].dwForwardIfIndex == index &&
713 routeTable->table[i].dwForwardType == MIB_IPROUTE_TYPE_INDIRECT)
714 row = &routeTable->table[i];
716 return row;
719 static ULONG adapterAddressesFromIndex(ULONG family, DWORD index, IP_ADAPTER_ADDRESSES *aa, ULONG *size)
721 ULONG ret, i, num_v4addrs = 0, num_v4_gateways = 0, num_v6addrs = 0, total_size;
722 DWORD *v4addrs = NULL;
723 SOCKET_ADDRESS *v6addrs = NULL;
724 PMIB_IPFORWARDTABLE routeTable = NULL;
726 if (family == AF_INET)
728 ret = AllocateAndGetIpForwardTableFromStack(&routeTable, FALSE,
729 GetProcessHeap(), 0);
730 if (!ret)
732 ret = v4addressesFromIndex(index, &v4addrs, &num_v4addrs);
733 num_v4_gateways = count_v4_gateways(index, routeTable);
736 else if (family == AF_INET6)
737 ret = v6addressesFromIndex(index, &v6addrs, &num_v6addrs);
738 else if (family == AF_UNSPEC)
740 ret = AllocateAndGetIpForwardTableFromStack(&routeTable, FALSE,
741 GetProcessHeap(), 0);
742 if (!ret)
744 ret = v4addressesFromIndex(index, &v4addrs, &num_v4addrs);
745 num_v4_gateways = count_v4_gateways(index, routeTable);
746 if (!ret)
747 ret = v6addressesFromIndex(index, &v6addrs, &num_v6addrs);
750 else
752 FIXME("address family %u unsupported\n", family);
753 ret = ERROR_NO_DATA;
755 if (ret)
757 HeapFree(GetProcessHeap(), 0, routeTable);
758 return ret;
761 total_size = sizeof(IP_ADAPTER_ADDRESSES);
762 total_size += IF_NAMESIZE;
763 total_size += 2 * IF_NAMESIZE * sizeof(WCHAR);
764 total_size += sizeof(IP_ADAPTER_UNICAST_ADDRESS) * num_v4addrs;
765 total_size += sizeof(struct sockaddr_in) * num_v4addrs;
766 total_size += (sizeof(IP_ADAPTER_GATEWAY_ADDRESS) + sizeof(SOCKADDR_IN)) * num_v4_gateways;
767 total_size += sizeof(IP_ADAPTER_UNICAST_ADDRESS) * num_v6addrs;
768 total_size += sizeof(SOCKET_ADDRESS) * num_v6addrs;
769 for (i = 0; i < num_v6addrs; i++)
770 total_size += v6addrs[i].iSockaddrLength;
772 if (aa && *size >= total_size)
774 char name[IF_NAMESIZE], *ptr = (char *)aa + sizeof(IP_ADAPTER_ADDRESSES), *src;
775 WCHAR *dst;
776 DWORD buflen, type, status;
778 memset(aa, 0, sizeof(IP_ADAPTER_ADDRESSES));
779 aa->u.s.Length = sizeof(IP_ADAPTER_ADDRESSES);
780 aa->u.s.IfIndex = index;
782 getInterfaceNameByIndex(index, name);
783 memcpy(ptr, name, IF_NAMESIZE);
784 aa->AdapterName = ptr;
785 ptr += IF_NAMESIZE;
786 aa->FriendlyName = (WCHAR *)ptr;
787 for (src = name, dst = (WCHAR *)ptr; *src; src++, dst++)
788 *dst = *src;
789 *dst++ = 0;
790 ptr = (char *)dst;
791 aa->Description = (WCHAR *)ptr;
792 for (src = name, dst = (WCHAR *)ptr; *src; src++, dst++)
793 *dst = *src;
794 *dst++ = 0;
795 ptr = (char *)dst;
797 TRACE("%s: %d IPv4 addresses, %d IPv6 addresses:\n", name, num_v4addrs,
798 num_v6addrs);
799 if (num_v4_gateways)
801 PMIB_IPFORWARDROW adapterRow;
803 if ((adapterRow = findIPv4Gateway(index, routeTable)))
805 PIP_ADAPTER_GATEWAY_ADDRESS gw;
806 PSOCKADDR_IN sin;
808 gw = (PIP_ADAPTER_GATEWAY_ADDRESS)ptr;
809 aa->FirstGatewayAddress = gw;
811 gw->u.s.Length = sizeof(IP_ADAPTER_GATEWAY_ADDRESS);
812 ptr += sizeof(IP_ADAPTER_GATEWAY_ADDRESS);
813 sin = (PSOCKADDR_IN)ptr;
814 sin->sin_family = AF_INET;
815 sin->sin_port = 0;
816 memcpy(&sin->sin_addr, &adapterRow->dwForwardNextHop,
817 sizeof(DWORD));
818 gw->Address.lpSockaddr = (LPSOCKADDR)sin;
819 gw->Address.iSockaddrLength = sizeof(SOCKADDR_IN);
820 gw->Next = NULL;
821 ptr += sizeof(SOCKADDR_IN);
824 if (num_v4addrs)
826 IP_ADAPTER_UNICAST_ADDRESS *ua;
827 struct sockaddr_in *sa;
828 aa->Flags |= IP_ADAPTER_IPV4_ENABLED;
829 ua = aa->FirstUnicastAddress = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
830 for (i = 0; i < num_v4addrs; i++)
832 char addr_buf[16];
834 memset(ua, 0, sizeof(IP_ADAPTER_UNICAST_ADDRESS));
835 ua->u.s.Length = sizeof(IP_ADAPTER_UNICAST_ADDRESS);
836 ua->Address.iSockaddrLength = sizeof(struct sockaddr_in);
837 ua->Address.lpSockaddr = (SOCKADDR *)((char *)ua + ua->u.s.Length);
839 sa = (struct sockaddr_in *)ua->Address.lpSockaddr;
840 sa->sin_family = AF_INET;
841 sa->sin_addr.s_addr = v4addrs[i];
842 sa->sin_port = 0;
843 TRACE("IPv4 %d/%d: %s\n", i + 1, num_v4addrs,
844 debugstr_ipv4(&sa->sin_addr.s_addr, addr_buf));
846 ptr += ua->u.s.Length + ua->Address.iSockaddrLength;
847 if (i < num_v4addrs - 1)
849 ua->Next = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
850 ua = ua->Next;
854 if (num_v6addrs)
856 IP_ADAPTER_UNICAST_ADDRESS *ua;
857 struct WS_sockaddr_in6 *sa;
859 aa->Flags |= IP_ADAPTER_IPV6_ENABLED;
860 if (aa->FirstUnicastAddress)
862 for (ua = aa->FirstUnicastAddress; ua->Next; ua = ua->Next)
864 ua->Next = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
865 ua = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
867 else
868 ua = aa->FirstUnicastAddress = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
869 for (i = 0; i < num_v6addrs; i++)
871 char addr_buf[46];
873 memset(ua, 0, sizeof(IP_ADAPTER_UNICAST_ADDRESS));
874 ua->u.s.Length = sizeof(IP_ADAPTER_UNICAST_ADDRESS);
875 ua->Address.iSockaddrLength = v6addrs[i].iSockaddrLength;
876 ua->Address.lpSockaddr = (SOCKADDR *)((char *)ua + ua->u.s.Length);
878 sa = (struct WS_sockaddr_in6 *)ua->Address.lpSockaddr;
879 memcpy(sa, v6addrs[i].lpSockaddr, sizeof(*sa));
880 TRACE("IPv6 %d/%d: %s\n", i + 1, num_v6addrs,
881 debugstr_ipv6(sa, addr_buf));
883 ptr += ua->u.s.Length + ua->Address.iSockaddrLength;
884 if (i < num_v6addrs - 1)
886 ua->Next = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
887 ua = ua->Next;
892 buflen = MAX_INTERFACE_PHYSADDR;
893 getInterfacePhysicalByIndex(index, &buflen, aa->PhysicalAddress, &type);
894 aa->PhysicalAddressLength = buflen;
895 aa->IfType = typeFromMibType(type);
896 aa->ConnectionType = connectionTypeFromMibType(type);
898 getInterfaceMtuByName(name, &aa->Mtu);
900 getInterfaceStatusByName(name, &status);
901 if (status == MIB_IF_OPER_STATUS_OPERATIONAL) aa->OperStatus = IfOperStatusUp;
902 else if (status == MIB_IF_OPER_STATUS_NON_OPERATIONAL) aa->OperStatus = IfOperStatusDown;
903 else aa->OperStatus = IfOperStatusUnknown;
905 *size = total_size;
906 HeapFree(GetProcessHeap(), 0, routeTable);
907 HeapFree(GetProcessHeap(), 0, v6addrs);
908 HeapFree(GetProcessHeap(), 0, v4addrs);
909 return ERROR_SUCCESS;
912 ULONG WINAPI GetAdaptersAddresses(ULONG family, ULONG flags, PVOID reserved,
913 PIP_ADAPTER_ADDRESSES aa, PULONG buflen)
915 InterfaceIndexTable *table;
916 ULONG i, size, total_size, ret = ERROR_NO_DATA;
918 TRACE("(%d, %08x, %p, %p, %p)\n", family, flags, reserved, aa, buflen);
920 if (!buflen) return ERROR_INVALID_PARAMETER;
922 table = getInterfaceIndexTable();
923 if (!table || !table->numIndexes)
925 HeapFree(GetProcessHeap(), 0, table);
926 return ERROR_NO_DATA;
928 total_size = 0;
929 for (i = 0; i < table->numIndexes; i++)
931 size = 0;
932 if ((ret = adapterAddressesFromIndex(family, table->indexes[i], NULL, &size)))
934 HeapFree(GetProcessHeap(), 0, table);
935 return ret;
937 total_size += size;
939 if (aa && *buflen >= total_size)
941 ULONG bytes_left = size = total_size;
942 for (i = 0; i < table->numIndexes; i++)
944 if ((ret = adapterAddressesFromIndex(family, table->indexes[i], aa, &size)))
946 HeapFree(GetProcessHeap(), 0, table);
947 return ret;
949 if (i < table->numIndexes - 1)
951 aa->Next = (IP_ADAPTER_ADDRESSES *)((char *)aa + size);
952 aa = aa->Next;
953 size = bytes_left -= size;
956 ret = ERROR_SUCCESS;
958 if (*buflen < total_size) ret = ERROR_BUFFER_OVERFLOW;
959 *buflen = total_size;
961 TRACE("num adapters %u\n", table->numIndexes);
962 HeapFree(GetProcessHeap(), 0, table);
963 return ret;
966 /******************************************************************
967 * GetBestInterface (IPHLPAPI.@)
969 * Get the interface, with the best route for the given IP address.
971 * PARAMS
972 * dwDestAddr [In] IP address to search the interface for
973 * pdwBestIfIndex [Out] found best interface
975 * RETURNS
976 * Success: NO_ERROR
977 * Failure: error code from winerror.h
979 DWORD WINAPI GetBestInterface(IPAddr dwDestAddr, PDWORD pdwBestIfIndex)
981 struct WS_sockaddr_in sa_in;
982 memset(&sa_in, 0, sizeof(sa_in));
983 sa_in.sin_family = AF_INET;
984 sa_in.sin_addr.S_un.S_addr = dwDestAddr;
985 return GetBestInterfaceEx((struct WS_sockaddr *)&sa_in, pdwBestIfIndex);
988 /******************************************************************
989 * GetBestInterfaceEx (IPHLPAPI.@)
991 * Get the interface, with the best route for the given IP address.
993 * PARAMS
994 * dwDestAddr [In] IP address to search the interface for
995 * pdwBestIfIndex [Out] found best interface
997 * RETURNS
998 * Success: NO_ERROR
999 * Failure: error code from winerror.h
1001 DWORD WINAPI GetBestInterfaceEx(struct WS_sockaddr *pDestAddr, PDWORD pdwBestIfIndex)
1003 DWORD ret;
1005 TRACE("pDestAddr %p, pdwBestIfIndex %p\n", pDestAddr, pdwBestIfIndex);
1006 if (!pDestAddr || !pdwBestIfIndex)
1007 ret = ERROR_INVALID_PARAMETER;
1008 else {
1009 MIB_IPFORWARDROW ipRow;
1011 if (pDestAddr->sa_family == AF_INET) {
1012 ret = GetBestRoute(((struct WS_sockaddr_in *)pDestAddr)->sin_addr.S_un.S_addr, 0, &ipRow);
1013 if (ret == ERROR_SUCCESS)
1014 *pdwBestIfIndex = ipRow.dwForwardIfIndex;
1015 } else {
1016 FIXME("address family %d not supported\n", pDestAddr->sa_family);
1017 ret = ERROR_NOT_SUPPORTED;
1020 TRACE("returning %d\n", ret);
1021 return ret;
1025 /******************************************************************
1026 * GetBestRoute (IPHLPAPI.@)
1028 * Get the best route for the given IP address.
1030 * PARAMS
1031 * dwDestAddr [In] IP address to search the best route for
1032 * dwSourceAddr [In] optional source IP address
1033 * pBestRoute [Out] found best route
1035 * RETURNS
1036 * Success: NO_ERROR
1037 * Failure: error code from winerror.h
1039 DWORD WINAPI GetBestRoute(DWORD dwDestAddr, DWORD dwSourceAddr, PMIB_IPFORWARDROW pBestRoute)
1041 PMIB_IPFORWARDTABLE table;
1042 DWORD ret;
1044 TRACE("dwDestAddr 0x%08x, dwSourceAddr 0x%08x, pBestRoute %p\n", dwDestAddr,
1045 dwSourceAddr, pBestRoute);
1046 if (!pBestRoute)
1047 return ERROR_INVALID_PARAMETER;
1049 ret = AllocateAndGetIpForwardTableFromStack(&table, FALSE, GetProcessHeap(), 0);
1050 if (!ret) {
1051 DWORD ndx, matchedBits, matchedNdx = table->dwNumEntries;
1053 for (ndx = 0, matchedBits = 0; ndx < table->dwNumEntries; ndx++) {
1054 if (table->table[ndx].dwForwardType != MIB_IPROUTE_TYPE_INVALID &&
1055 (dwDestAddr & table->table[ndx].dwForwardMask) ==
1056 (table->table[ndx].dwForwardDest & table->table[ndx].dwForwardMask)) {
1057 DWORD numShifts, mask;
1059 for (numShifts = 0, mask = table->table[ndx].dwForwardMask;
1060 mask && mask & 1; mask >>= 1, numShifts++)
1062 if (numShifts > matchedBits) {
1063 matchedBits = numShifts;
1064 matchedNdx = ndx;
1066 else if (!matchedBits) {
1067 matchedNdx = ndx;
1071 if (matchedNdx < table->dwNumEntries) {
1072 memcpy(pBestRoute, &table->table[matchedNdx], sizeof(MIB_IPFORWARDROW));
1073 ret = ERROR_SUCCESS;
1075 else {
1076 /* No route matches, which can happen if there's no default route. */
1077 ret = ERROR_HOST_UNREACHABLE;
1079 HeapFree(GetProcessHeap(), 0, table);
1081 TRACE("returning %d\n", ret);
1082 return ret;
1086 /******************************************************************
1087 * GetFriendlyIfIndex (IPHLPAPI.@)
1089 * Get a "friendly" version of IfIndex, which is one that doesn't
1090 * have the top byte set. Doesn't validate whether IfIndex is a valid
1091 * adapter index.
1093 * PARAMS
1094 * IfIndex [In] interface index to get the friendly one for
1096 * RETURNS
1097 * A friendly version of IfIndex.
1099 DWORD WINAPI GetFriendlyIfIndex(DWORD IfIndex)
1101 /* windows doesn't validate these, either, just makes sure the top byte is
1102 cleared. I assume my ifenum module never gives an index with the top
1103 byte set. */
1104 TRACE("returning %d\n", IfIndex);
1105 return IfIndex;
1109 /******************************************************************
1110 * GetIfEntry (IPHLPAPI.@)
1112 * Get information about an interface.
1114 * PARAMS
1115 * pIfRow [In/Out] In: dwIndex of MIB_IFROW selects the interface.
1116 * Out: interface information
1118 * RETURNS
1119 * Success: NO_ERROR
1120 * Failure: error code from winerror.h
1122 DWORD WINAPI GetIfEntry(PMIB_IFROW pIfRow)
1124 DWORD ret;
1125 char nameBuf[MAX_ADAPTER_NAME];
1126 char *name;
1128 TRACE("pIfRow %p\n", pIfRow);
1129 if (!pIfRow)
1130 return ERROR_INVALID_PARAMETER;
1132 name = getInterfaceNameByIndex(pIfRow->dwIndex, nameBuf);
1133 if (name) {
1134 ret = getInterfaceEntryByName(name, pIfRow);
1135 if (ret == NO_ERROR)
1136 ret = getInterfaceStatsByName(name, pIfRow);
1138 else
1139 ret = ERROR_INVALID_DATA;
1140 TRACE("returning %d\n", ret);
1141 return ret;
1145 static int IfTableSorter(const void *a, const void *b)
1147 int ret;
1149 if (a && b)
1150 ret = ((const MIB_IFROW*)a)->dwIndex - ((const MIB_IFROW*)b)->dwIndex;
1151 else
1152 ret = 0;
1153 return ret;
1157 /******************************************************************
1158 * GetIfTable (IPHLPAPI.@)
1160 * Get a table of local interfaces.
1162 * PARAMS
1163 * pIfTable [Out] buffer for local interfaces table
1164 * pdwSize [In/Out] length of output buffer
1165 * bOrder [In] whether to sort the table
1167 * RETURNS
1168 * Success: NO_ERROR
1169 * Failure: error code from winerror.h
1171 * NOTES
1172 * If pdwSize is less than required, the function will return
1173 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the required byte
1174 * size.
1175 * If bOrder is true, the returned table will be sorted by interface index.
1177 DWORD WINAPI GetIfTable(PMIB_IFTABLE pIfTable, PULONG pdwSize, BOOL bOrder)
1179 DWORD ret;
1181 TRACE("pIfTable %p, pdwSize %p, bOrder %d\n", pdwSize, pdwSize,
1182 (DWORD)bOrder);
1183 if (!pdwSize)
1184 ret = ERROR_INVALID_PARAMETER;
1185 else {
1186 DWORD numInterfaces = getNumInterfaces();
1187 ULONG size = sizeof(MIB_IFTABLE);
1189 if (numInterfaces > 1)
1190 size += (numInterfaces - 1) * sizeof(MIB_IFROW);
1191 if (!pIfTable || *pdwSize < size) {
1192 *pdwSize = size;
1193 ret = ERROR_INSUFFICIENT_BUFFER;
1195 else {
1196 InterfaceIndexTable *table = getInterfaceIndexTable();
1198 if (table) {
1199 size = sizeof(MIB_IFTABLE);
1200 if (table->numIndexes > 1)
1201 size += (table->numIndexes - 1) * sizeof(MIB_IFROW);
1202 if (*pdwSize < size) {
1203 *pdwSize = size;
1204 ret = ERROR_INSUFFICIENT_BUFFER;
1206 else {
1207 DWORD ndx;
1209 *pdwSize = size;
1210 pIfTable->dwNumEntries = 0;
1211 for (ndx = 0; ndx < table->numIndexes; ndx++) {
1212 pIfTable->table[ndx].dwIndex = table->indexes[ndx];
1213 GetIfEntry(&pIfTable->table[ndx]);
1214 pIfTable->dwNumEntries++;
1216 if (bOrder)
1217 qsort(pIfTable->table, pIfTable->dwNumEntries, sizeof(MIB_IFROW),
1218 IfTableSorter);
1219 ret = NO_ERROR;
1221 HeapFree(GetProcessHeap(), 0, table);
1223 else
1224 ret = ERROR_OUTOFMEMORY;
1227 TRACE("returning %d\n", ret);
1228 return ret;
1232 /******************************************************************
1233 * GetInterfaceInfo (IPHLPAPI.@)
1235 * Get a list of network interface adapters.
1237 * PARAMS
1238 * pIfTable [Out] buffer for interface adapters
1239 * dwOutBufLen [Out] if buffer is too small, returns required size
1241 * RETURNS
1242 * Success: NO_ERROR
1243 * Failure: error code from winerror.h
1245 * BUGS
1246 * MSDN states this should return non-loopback interfaces only.
1248 DWORD WINAPI GetInterfaceInfo(PIP_INTERFACE_INFO pIfTable, PULONG dwOutBufLen)
1250 DWORD ret;
1252 TRACE("pIfTable %p, dwOutBufLen %p\n", pIfTable, dwOutBufLen);
1253 if (!dwOutBufLen)
1254 ret = ERROR_INVALID_PARAMETER;
1255 else {
1256 DWORD numInterfaces = getNumInterfaces();
1257 ULONG size = sizeof(IP_INTERFACE_INFO);
1259 if (numInterfaces > 1)
1260 size += (numInterfaces - 1) * sizeof(IP_ADAPTER_INDEX_MAP);
1261 if (!pIfTable || *dwOutBufLen < size) {
1262 *dwOutBufLen = size;
1263 ret = ERROR_INSUFFICIENT_BUFFER;
1265 else {
1266 InterfaceIndexTable *table = getInterfaceIndexTable();
1268 if (table) {
1269 size = sizeof(IP_INTERFACE_INFO);
1270 if (table->numIndexes > 1)
1271 size += (table->numIndexes - 1) * sizeof(IP_ADAPTER_INDEX_MAP);
1272 if (*dwOutBufLen < size) {
1273 *dwOutBufLen = size;
1274 ret = ERROR_INSUFFICIENT_BUFFER;
1276 else {
1277 DWORD ndx;
1278 char nameBuf[MAX_ADAPTER_NAME];
1280 *dwOutBufLen = size;
1281 pIfTable->NumAdapters = 0;
1282 for (ndx = 0; ndx < table->numIndexes; ndx++) {
1283 const char *walker, *name;
1284 WCHAR *assigner;
1286 pIfTable->Adapter[ndx].Index = table->indexes[ndx];
1287 name = getInterfaceNameByIndex(table->indexes[ndx], nameBuf);
1288 for (walker = name, assigner = pIfTable->Adapter[ndx].Name;
1289 walker && *walker &&
1290 assigner - pIfTable->Adapter[ndx].Name < MAX_ADAPTER_NAME - 1;
1291 walker++, assigner++)
1292 *assigner = *walker;
1293 *assigner = 0;
1294 pIfTable->NumAdapters++;
1296 ret = NO_ERROR;
1298 HeapFree(GetProcessHeap(), 0, table);
1300 else
1301 ret = ERROR_OUTOFMEMORY;
1304 TRACE("returning %d\n", ret);
1305 return ret;
1309 /******************************************************************
1310 * GetIpAddrTable (IPHLPAPI.@)
1312 * Get interface-to-IP address mapping table.
1314 * PARAMS
1315 * pIpAddrTable [Out] buffer for mapping table
1316 * pdwSize [In/Out] length of output buffer
1317 * bOrder [In] whether to sort the table
1319 * RETURNS
1320 * Success: NO_ERROR
1321 * Failure: error code from winerror.h
1323 * NOTES
1324 * If pdwSize is less than required, the function will return
1325 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the required byte
1326 * size.
1327 * If bOrder is true, the returned table will be sorted by the next hop and
1328 * an assortment of arbitrary parameters.
1330 DWORD WINAPI GetIpAddrTable(PMIB_IPADDRTABLE pIpAddrTable, PULONG pdwSize, BOOL bOrder)
1332 DWORD ret;
1334 TRACE("pIpAddrTable %p, pdwSize %p, bOrder %d\n", pIpAddrTable, pdwSize,
1335 (DWORD)bOrder);
1336 if (!pdwSize)
1337 ret = ERROR_INVALID_PARAMETER;
1338 else {
1339 PMIB_IPADDRTABLE table;
1341 ret = getIPAddrTable(&table, GetProcessHeap(), 0);
1342 if (ret == NO_ERROR)
1344 ULONG size = sizeof(MIB_IPADDRTABLE);
1346 if (table->dwNumEntries > 1)
1347 size += (table->dwNumEntries - 1) * sizeof(MIB_IPADDRROW);
1348 if (!pIpAddrTable || *pdwSize < size) {
1349 *pdwSize = size;
1350 ret = ERROR_INSUFFICIENT_BUFFER;
1352 else {
1353 *pdwSize = size;
1354 memcpy(pIpAddrTable, table, size);
1355 if (bOrder)
1356 qsort(pIpAddrTable->table, pIpAddrTable->dwNumEntries,
1357 sizeof(MIB_IPADDRROW), IpAddrTableSorter);
1358 ret = NO_ERROR;
1360 HeapFree(GetProcessHeap(), 0, table);
1363 TRACE("returning %d\n", ret);
1364 return ret;
1368 /******************************************************************
1369 * GetIpForwardTable (IPHLPAPI.@)
1371 * Get the route table.
1373 * PARAMS
1374 * pIpForwardTable [Out] buffer for route table
1375 * pdwSize [In/Out] length of output buffer
1376 * bOrder [In] whether to sort the table
1378 * RETURNS
1379 * Success: NO_ERROR
1380 * Failure: error code from winerror.h
1382 * NOTES
1383 * If pdwSize is less than required, the function will return
1384 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the required byte
1385 * size.
1386 * If bOrder is true, the returned table will be sorted by the next hop and
1387 * an assortment of arbitrary parameters.
1389 DWORD WINAPI GetIpForwardTable(PMIB_IPFORWARDTABLE pIpForwardTable, PULONG pdwSize, BOOL bOrder)
1391 DWORD ret;
1392 PMIB_IPFORWARDTABLE table;
1394 TRACE("pIpForwardTable %p, pdwSize %p, bOrder %d\n", pIpForwardTable, pdwSize, bOrder);
1396 if (!pdwSize) return ERROR_INVALID_PARAMETER;
1398 ret = AllocateAndGetIpForwardTableFromStack(&table, bOrder, GetProcessHeap(), 0);
1399 if (!ret) {
1400 DWORD size = FIELD_OFFSET( MIB_IPFORWARDTABLE, table[table->dwNumEntries] );
1401 if (!pIpForwardTable || *pdwSize < size) {
1402 *pdwSize = size;
1403 ret = ERROR_INSUFFICIENT_BUFFER;
1405 else {
1406 *pdwSize = size;
1407 memcpy(pIpForwardTable, table, size);
1409 HeapFree(GetProcessHeap(), 0, table);
1411 TRACE("returning %d\n", ret);
1412 return ret;
1416 /******************************************************************
1417 * GetIpNetTable (IPHLPAPI.@)
1419 * Get the IP-to-physical address mapping table.
1421 * PARAMS
1422 * pIpNetTable [Out] buffer for mapping table
1423 * pdwSize [In/Out] length of output buffer
1424 * bOrder [In] whether to sort the table
1426 * RETURNS
1427 * Success: NO_ERROR
1428 * Failure: error code from winerror.h
1430 * NOTES
1431 * If pdwSize is less than required, the function will return
1432 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the required byte
1433 * size.
1434 * If bOrder is true, the returned table will be sorted by IP address.
1436 DWORD WINAPI GetIpNetTable(PMIB_IPNETTABLE pIpNetTable, PULONG pdwSize, BOOL bOrder)
1438 DWORD ret;
1439 PMIB_IPNETTABLE table;
1441 TRACE("pIpNetTable %p, pdwSize %p, bOrder %d\n", pIpNetTable, pdwSize, bOrder);
1443 if (!pdwSize) return ERROR_INVALID_PARAMETER;
1445 ret = AllocateAndGetIpNetTableFromStack( &table, bOrder, GetProcessHeap(), 0 );
1446 if (!ret) {
1447 DWORD size = FIELD_OFFSET( MIB_IPNETTABLE, table[table->dwNumEntries] );
1448 if (!pIpNetTable || *pdwSize < size) {
1449 *pdwSize = size;
1450 ret = ERROR_INSUFFICIENT_BUFFER;
1452 else {
1453 *pdwSize = size;
1454 memcpy(pIpNetTable, table, size);
1456 HeapFree(GetProcessHeap(), 0, table);
1458 TRACE("returning %d\n", ret);
1459 return ret;
1463 /******************************************************************
1464 * GetNetworkParams (IPHLPAPI.@)
1466 * Get the network parameters for the local computer.
1468 * PARAMS
1469 * pFixedInfo [Out] buffer for network parameters
1470 * pOutBufLen [In/Out] length of output buffer
1472 * RETURNS
1473 * Success: NO_ERROR
1474 * Failure: error code from winerror.h
1476 * NOTES
1477 * If pOutBufLen is less than required, the function will return
1478 * ERROR_INSUFFICIENT_BUFFER, and pOutBufLen will be set to the required byte
1479 * size.
1481 DWORD WINAPI GetNetworkParams(PFIXED_INFO pFixedInfo, PULONG pOutBufLen)
1483 DWORD ret, size;
1484 LONG regReturn;
1485 HKEY hKey;
1487 TRACE("pFixedInfo %p, pOutBufLen %p\n", pFixedInfo, pOutBufLen);
1488 if (!pOutBufLen)
1489 return ERROR_INVALID_PARAMETER;
1491 initialise_resolver();
1492 size = sizeof(FIXED_INFO) + (_res.nscount > 0 ? (_res.nscount - 1) *
1493 sizeof(IP_ADDR_STRING) : 0);
1494 if (!pFixedInfo || *pOutBufLen < size) {
1495 *pOutBufLen = size;
1496 return ERROR_BUFFER_OVERFLOW;
1499 memset(pFixedInfo, 0, size);
1500 size = sizeof(pFixedInfo->HostName);
1501 GetComputerNameExA(ComputerNameDnsHostname, pFixedInfo->HostName, &size);
1502 size = sizeof(pFixedInfo->DomainName);
1503 GetComputerNameExA(ComputerNameDnsDomain, pFixedInfo->DomainName, &size);
1504 if (_res.nscount > 0) {
1505 PIP_ADDR_STRING ptr;
1506 int i;
1508 for (i = 0, ptr = &pFixedInfo->DnsServerList; i < _res.nscount && ptr;
1509 i++, ptr = ptr->Next) {
1510 toIPAddressString(_res.nsaddr_list[i].sin_addr.s_addr,
1511 ptr->IpAddress.String);
1512 if (i == _res.nscount - 1)
1513 ptr->Next = NULL;
1514 else if (i == 0)
1515 ptr->Next = (PIP_ADDR_STRING)((LPBYTE)pFixedInfo + sizeof(FIXED_INFO));
1516 else
1517 ptr->Next = (PIP_ADDR_STRING)((PBYTE)ptr + sizeof(IP_ADDR_STRING));
1520 pFixedInfo->NodeType = HYBRID_NODETYPE;
1521 regReturn = RegOpenKeyExA(HKEY_LOCAL_MACHINE,
1522 "SYSTEM\\CurrentControlSet\\Services\\VxD\\MSTCP", 0, KEY_READ, &hKey);
1523 if (regReturn != ERROR_SUCCESS)
1524 regReturn = RegOpenKeyExA(HKEY_LOCAL_MACHINE,
1525 "SYSTEM\\CurrentControlSet\\Services\\NetBT\\Parameters", 0, KEY_READ,
1526 &hKey);
1527 if (regReturn == ERROR_SUCCESS)
1529 DWORD size = sizeof(pFixedInfo->ScopeId);
1531 RegQueryValueExA(hKey, "ScopeID", NULL, NULL, (LPBYTE)pFixedInfo->ScopeId, &size);
1532 RegCloseKey(hKey);
1535 /* FIXME: can check whether routing's enabled in /proc/sys/net/ipv4/ip_forward
1536 I suppose could also check for a listener on port 53 to set EnableDns */
1537 ret = NO_ERROR;
1538 TRACE("returning %d\n", ret);
1539 return ret;
1543 /******************************************************************
1544 * GetNumberOfInterfaces (IPHLPAPI.@)
1546 * Get the number of interfaces.
1548 * PARAMS
1549 * pdwNumIf [Out] number of interfaces
1551 * RETURNS
1552 * NO_ERROR on success, ERROR_INVALID_PARAMETER if pdwNumIf is NULL.
1554 DWORD WINAPI GetNumberOfInterfaces(PDWORD pdwNumIf)
1556 DWORD ret;
1558 TRACE("pdwNumIf %p\n", pdwNumIf);
1559 if (!pdwNumIf)
1560 ret = ERROR_INVALID_PARAMETER;
1561 else {
1562 *pdwNumIf = getNumInterfaces();
1563 ret = NO_ERROR;
1565 TRACE("returning %d\n", ret);
1566 return ret;
1570 /******************************************************************
1571 * GetPerAdapterInfo (IPHLPAPI.@)
1573 * Get information about an adapter corresponding to an interface.
1575 * PARAMS
1576 * IfIndex [In] interface info
1577 * pPerAdapterInfo [Out] buffer for per adapter info
1578 * pOutBufLen [In/Out] length of output buffer
1580 * RETURNS
1581 * Success: NO_ERROR
1582 * Failure: error code from winerror.h
1584 * FIXME
1585 * Stub, returns empty IP_PER_ADAPTER_INFO in every case.
1587 DWORD WINAPI GetPerAdapterInfo(ULONG IfIndex, PIP_PER_ADAPTER_INFO pPerAdapterInfo, PULONG pOutBufLen)
1589 ULONG bytesNeeded = sizeof(IP_PER_ADAPTER_INFO);
1591 TRACE("(IfIndex %d, pPerAdapterInfo %p, pOutBufLen %p)\n", IfIndex, pPerAdapterInfo, pOutBufLen);
1593 if (!pOutBufLen) return ERROR_INVALID_PARAMETER;
1595 if (!pPerAdapterInfo || *pOutBufLen < bytesNeeded)
1597 *pOutBufLen = bytesNeeded;
1598 return ERROR_BUFFER_OVERFLOW;
1601 memset(pPerAdapterInfo, 0, bytesNeeded);
1602 return NO_ERROR;
1606 /******************************************************************
1607 * GetRTTAndHopCount (IPHLPAPI.@)
1609 * Get round-trip time (RTT) and hop count.
1611 * PARAMS
1613 * DestIpAddress [In] destination address to get the info for
1614 * HopCount [Out] retrieved hop count
1615 * MaxHops [In] maximum hops to search for the destination
1616 * RTT [Out] RTT in milliseconds
1618 * RETURNS
1619 * Success: TRUE
1620 * Failure: FALSE
1622 * FIXME
1623 * Stub, returns FALSE.
1625 BOOL WINAPI GetRTTAndHopCount(IPAddr DestIpAddress, PULONG HopCount, ULONG MaxHops, PULONG RTT)
1627 FIXME("(DestIpAddress 0x%08x, HopCount %p, MaxHops %d, RTT %p): stub\n",
1628 DestIpAddress, HopCount, MaxHops, RTT);
1629 return FALSE;
1633 /******************************************************************
1634 * GetTcpTable (IPHLPAPI.@)
1636 * Get the table of active TCP connections.
1638 * PARAMS
1639 * pTcpTable [Out] buffer for TCP connections table
1640 * pdwSize [In/Out] length of output buffer
1641 * bOrder [In] whether to order the table
1643 * RETURNS
1644 * Success: NO_ERROR
1645 * Failure: error code from winerror.h
1647 * NOTES
1648 * If pdwSize is less than required, the function will return
1649 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to
1650 * the required byte size.
1651 * If bOrder is true, the returned table will be sorted, first by
1652 * local address and port number, then by remote address and port
1653 * number.
1655 DWORD WINAPI GetTcpTable(PMIB_TCPTABLE pTcpTable, PDWORD pdwSize, BOOL bOrder)
1657 DWORD ret;
1658 PMIB_TCPTABLE table;
1660 TRACE("pTcpTable %p, pdwSize %p, bOrder %d\n", pTcpTable, pdwSize, bOrder);
1662 if (!pdwSize) return ERROR_INVALID_PARAMETER;
1664 ret = AllocateAndGetTcpTableFromStack(&table, bOrder, GetProcessHeap(), 0);
1665 if (!ret) {
1666 DWORD size = FIELD_OFFSET( MIB_TCPTABLE, table[table->dwNumEntries] );
1667 if (!pTcpTable || *pdwSize < size) {
1668 *pdwSize = size;
1669 ret = ERROR_INSUFFICIENT_BUFFER;
1671 else {
1672 *pdwSize = size;
1673 memcpy(pTcpTable, table, size);
1675 HeapFree(GetProcessHeap(), 0, table);
1677 TRACE("returning %d\n", ret);
1678 return ret;
1682 /******************************************************************
1683 * GetUdpTable (IPHLPAPI.@)
1685 * Get a table of active UDP connections.
1687 * PARAMS
1688 * pUdpTable [Out] buffer for UDP connections table
1689 * pdwSize [In/Out] length of output buffer
1690 * bOrder [In] whether to order the table
1692 * RETURNS
1693 * Success: NO_ERROR
1694 * Failure: error code from winerror.h
1696 * NOTES
1697 * If pdwSize is less than required, the function will return
1698 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the
1699 * required byte size.
1700 * If bOrder is true, the returned table will be sorted, first by
1701 * local address, then by local port number.
1703 DWORD WINAPI GetUdpTable(PMIB_UDPTABLE pUdpTable, PDWORD pdwSize, BOOL bOrder)
1705 DWORD ret;
1706 PMIB_UDPTABLE table;
1708 TRACE("pUdpTable %p, pdwSize %p, bOrder %d\n", pUdpTable, pdwSize, bOrder);
1710 if (!pdwSize) return ERROR_INVALID_PARAMETER;
1712 ret = AllocateAndGetUdpTableFromStack( &table, bOrder, GetProcessHeap(), 0 );
1713 if (!ret) {
1714 DWORD size = FIELD_OFFSET( MIB_UDPTABLE, table[table->dwNumEntries] );
1715 if (!pUdpTable || *pdwSize < size) {
1716 *pdwSize = size;
1717 ret = ERROR_INSUFFICIENT_BUFFER;
1719 else {
1720 *pdwSize = size;
1721 memcpy(pUdpTable, table, size);
1723 HeapFree(GetProcessHeap(), 0, table);
1725 TRACE("returning %d\n", ret);
1726 return ret;
1730 /******************************************************************
1731 * GetUniDirectionalAdapterInfo (IPHLPAPI.@)
1733 * This is a Win98-only function to get information on "unidirectional"
1734 * adapters. Since this is pretty nonsensical in other contexts, it
1735 * never returns anything.
1737 * PARAMS
1738 * pIPIfInfo [Out] buffer for adapter infos
1739 * dwOutBufLen [Out] length of the output buffer
1741 * RETURNS
1742 * Success: NO_ERROR
1743 * Failure: error code from winerror.h
1745 * FIXME
1746 * Stub, returns ERROR_NOT_SUPPORTED.
1748 DWORD WINAPI GetUniDirectionalAdapterInfo(PIP_UNIDIRECTIONAL_ADAPTER_ADDRESS pIPIfInfo, PULONG dwOutBufLen)
1750 TRACE("pIPIfInfo %p, dwOutBufLen %p\n", pIPIfInfo, dwOutBufLen);
1751 /* a unidirectional adapter?? not bloody likely! */
1752 return ERROR_NOT_SUPPORTED;
1756 /******************************************************************
1757 * IpReleaseAddress (IPHLPAPI.@)
1759 * Release an IP obtained through DHCP,
1761 * PARAMS
1762 * AdapterInfo [In] adapter to release IP address
1764 * RETURNS
1765 * Success: NO_ERROR
1766 * Failure: error code from winerror.h
1768 * NOTES
1769 * Since GetAdaptersInfo never returns adapters that have DHCP enabled,
1770 * this function does nothing.
1772 * FIXME
1773 * Stub, returns ERROR_NOT_SUPPORTED.
1775 DWORD WINAPI IpReleaseAddress(PIP_ADAPTER_INDEX_MAP AdapterInfo)
1777 TRACE("AdapterInfo %p\n", AdapterInfo);
1778 /* not a stub, never going to support this (and I never mark an adapter as
1779 DHCP enabled, see GetAdaptersInfo, so this should never get called) */
1780 return ERROR_NOT_SUPPORTED;
1784 /******************************************************************
1785 * IpRenewAddress (IPHLPAPI.@)
1787 * Renew an IP obtained through DHCP.
1789 * PARAMS
1790 * AdapterInfo [In] adapter to renew IP address
1792 * RETURNS
1793 * Success: NO_ERROR
1794 * Failure: error code from winerror.h
1796 * NOTES
1797 * Since GetAdaptersInfo never returns adapters that have DHCP enabled,
1798 * this function does nothing.
1800 * FIXME
1801 * Stub, returns ERROR_NOT_SUPPORTED.
1803 DWORD WINAPI IpRenewAddress(PIP_ADAPTER_INDEX_MAP AdapterInfo)
1805 TRACE("AdapterInfo %p\n", AdapterInfo);
1806 /* not a stub, never going to support this (and I never mark an adapter as
1807 DHCP enabled, see GetAdaptersInfo, so this should never get called) */
1808 return ERROR_NOT_SUPPORTED;
1812 /******************************************************************
1813 * NotifyAddrChange (IPHLPAPI.@)
1815 * Notify caller whenever the ip-interface map is changed.
1817 * PARAMS
1818 * Handle [Out] handle usable in asynchronous notification
1819 * overlapped [In] overlapped structure that notifies the caller
1821 * RETURNS
1822 * Success: NO_ERROR
1823 * Failure: error code from winerror.h
1825 * FIXME
1826 * Stub, returns ERROR_NOT_SUPPORTED.
1828 DWORD WINAPI NotifyAddrChange(PHANDLE Handle, LPOVERLAPPED overlapped)
1830 FIXME("(Handle %p, overlapped %p): stub\n", Handle, overlapped);
1831 return ERROR_NOT_SUPPORTED;
1835 /******************************************************************
1836 * NotifyRouteChange (IPHLPAPI.@)
1838 * Notify caller whenever the ip routing table is changed.
1840 * PARAMS
1841 * Handle [Out] handle usable in asynchronous notification
1842 * overlapped [In] overlapped structure that notifies the caller
1844 * RETURNS
1845 * Success: NO_ERROR
1846 * Failure: error code from winerror.h
1848 * FIXME
1849 * Stub, returns ERROR_NOT_SUPPORTED.
1851 DWORD WINAPI NotifyRouteChange(PHANDLE Handle, LPOVERLAPPED overlapped)
1853 FIXME("(Handle %p, overlapped %p): stub\n", Handle, overlapped);
1854 return ERROR_NOT_SUPPORTED;
1858 /******************************************************************
1859 * SendARP (IPHLPAPI.@)
1861 * Send an ARP request.
1863 * PARAMS
1864 * DestIP [In] attempt to obtain this IP
1865 * SrcIP [In] optional sender IP address
1866 * pMacAddr [Out] buffer for the mac address
1867 * PhyAddrLen [In/Out] length of the output buffer
1869 * RETURNS
1870 * Success: NO_ERROR
1871 * Failure: error code from winerror.h
1873 * FIXME
1874 * Stub, returns ERROR_NOT_SUPPORTED.
1876 DWORD WINAPI SendARP(IPAddr DestIP, IPAddr SrcIP, PULONG pMacAddr, PULONG PhyAddrLen)
1878 FIXME("(DestIP 0x%08x, SrcIP 0x%08x, pMacAddr %p, PhyAddrLen %p): stub\n",
1879 DestIP, SrcIP, pMacAddr, PhyAddrLen);
1880 return ERROR_NOT_SUPPORTED;
1884 /******************************************************************
1885 * SetIfEntry (IPHLPAPI.@)
1887 * Set the administrative status of an interface.
1889 * PARAMS
1890 * pIfRow [In] dwAdminStatus member specifies the new status.
1892 * RETURNS
1893 * Success: NO_ERROR
1894 * Failure: error code from winerror.h
1896 * FIXME
1897 * Stub, returns ERROR_NOT_SUPPORTED.
1899 DWORD WINAPI SetIfEntry(PMIB_IFROW pIfRow)
1901 FIXME("(pIfRow %p): stub\n", pIfRow);
1902 /* this is supposed to set an interface administratively up or down.
1903 Could do SIOCSIFFLAGS and set/clear IFF_UP, but, not sure I want to, and
1904 this sort of down is indistinguishable from other sorts of down (e.g. no
1905 link). */
1906 return ERROR_NOT_SUPPORTED;
1910 /******************************************************************
1911 * SetIpForwardEntry (IPHLPAPI.@)
1913 * Modify an existing route.
1915 * PARAMS
1916 * pRoute [In] route with the new information
1918 * RETURNS
1919 * Success: NO_ERROR
1920 * Failure: error code from winerror.h
1922 * FIXME
1923 * Stub, returns NO_ERROR.
1925 DWORD WINAPI SetIpForwardEntry(PMIB_IPFORWARDROW pRoute)
1927 FIXME("(pRoute %p): stub\n", pRoute);
1928 /* this is to add a route entry, how's it distinguishable from
1929 CreateIpForwardEntry?
1930 could use SIOCADDRT, not sure I want to */
1931 return 0;
1935 /******************************************************************
1936 * SetIpNetEntry (IPHLPAPI.@)
1938 * Modify an existing ARP entry.
1940 * PARAMS
1941 * pArpEntry [In] ARP entry with the new information
1943 * RETURNS
1944 * Success: NO_ERROR
1945 * Failure: error code from winerror.h
1947 * FIXME
1948 * Stub, returns NO_ERROR.
1950 DWORD WINAPI SetIpNetEntry(PMIB_IPNETROW pArpEntry)
1952 FIXME("(pArpEntry %p): stub\n", pArpEntry);
1953 /* same as CreateIpNetEntry here, could use SIOCSARP, not sure I want to */
1954 return 0;
1958 /******************************************************************
1959 * SetIpStatistics (IPHLPAPI.@)
1961 * Toggle IP forwarding and det the default TTL value.
1963 * PARAMS
1964 * pIpStats [In] IP statistics with the new information
1966 * RETURNS
1967 * Success: NO_ERROR
1968 * Failure: error code from winerror.h
1970 * FIXME
1971 * Stub, returns NO_ERROR.
1973 DWORD WINAPI SetIpStatistics(PMIB_IPSTATS pIpStats)
1975 FIXME("(pIpStats %p): stub\n", pIpStats);
1976 return 0;
1980 /******************************************************************
1981 * SetIpTTL (IPHLPAPI.@)
1983 * Set the default TTL value.
1985 * PARAMS
1986 * nTTL [In] new TTL value
1988 * RETURNS
1989 * Success: NO_ERROR
1990 * Failure: error code from winerror.h
1992 * FIXME
1993 * Stub, returns NO_ERROR.
1995 DWORD WINAPI SetIpTTL(UINT nTTL)
1997 FIXME("(nTTL %d): stub\n", nTTL);
1998 /* could echo nTTL > /proc/net/sys/net/ipv4/ip_default_ttl, not sure I
1999 want to. Could map EACCESS to ERROR_ACCESS_DENIED, I suppose */
2000 return 0;
2004 /******************************************************************
2005 * SetTcpEntry (IPHLPAPI.@)
2007 * Set the state of a TCP connection.
2009 * PARAMS
2010 * pTcpRow [In] specifies connection with new state
2012 * RETURNS
2013 * Success: NO_ERROR
2014 * Failure: error code from winerror.h
2016 * FIXME
2017 * Stub, returns NO_ERROR.
2019 DWORD WINAPI SetTcpEntry(PMIB_TCPROW pTcpRow)
2021 FIXME("(pTcpRow %p): stub\n", pTcpRow);
2022 return 0;
2026 /******************************************************************
2027 * UnenableRouter (IPHLPAPI.@)
2029 * Decrement the IP-forwarding reference count. Turn off IP-forwarding
2030 * if it reaches zero.
2032 * PARAMS
2033 * pOverlapped [In/Out] should be the same as in EnableRouter()
2034 * lpdwEnableCount [Out] optional, receives reference count
2036 * RETURNS
2037 * Success: NO_ERROR
2038 * Failure: error code from winerror.h
2040 * FIXME
2041 * Stub, returns ERROR_NOT_SUPPORTED.
2043 DWORD WINAPI UnenableRouter(OVERLAPPED * pOverlapped, LPDWORD lpdwEnableCount)
2045 FIXME("(pOverlapped %p, lpdwEnableCount %p): stub\n", pOverlapped,
2046 lpdwEnableCount);
2047 /* could echo "0" > /proc/net/sys/net/ipv4/ip_forward, not sure I want to
2048 could map EACCESS to ERROR_ACCESS_DENIED, I suppose
2050 return ERROR_NOT_SUPPORTED;