winmm: Fix a memory leak in mciSendStringW (valgrind).
[wine.git] / dlls / iphlpapi / iphlpapi_main.c
blobe14538632be025d671e8346fc233c5615ef87edb
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 #define NONAMELESSUNION
40 #define NONAMELESSSTRUCT
41 #include "windef.h"
42 #include "winbase.h"
43 #include "winreg.h"
44 #define USE_WS_PREFIX
45 #include "winsock2.h"
46 #include "winternl.h"
47 #include "ws2ipdef.h"
48 #include "iphlpapi.h"
49 #include "ifenum.h"
50 #include "ipstats.h"
51 #include "ipifcons.h"
52 #include "fltdefs.h"
54 #include "wine/debug.h"
56 WINE_DEFAULT_DEBUG_CHANNEL(iphlpapi);
58 #ifndef IF_NAMESIZE
59 #define IF_NAMESIZE 16
60 #endif
62 #ifndef INADDR_NONE
63 #define INADDR_NONE ~0UL
64 #endif
66 /******************************************************************
67 * AddIPAddress (IPHLPAPI.@)
69 * Add an IP address to an adapter.
71 * PARAMS
72 * Address [In] IP address to add to the adapter
73 * IpMask [In] subnet mask for the IP address
74 * IfIndex [In] adapter index to add the address
75 * NTEContext [Out] Net Table Entry (NTE) context for the IP address
76 * NTEInstance [Out] NTE instance for the IP address
78 * RETURNS
79 * Success: NO_ERROR
80 * Failure: error code from winerror.h
82 * FIXME
83 * Stub. Currently returns ERROR_NOT_SUPPORTED.
85 DWORD WINAPI AddIPAddress(IPAddr Address, IPMask IpMask, DWORD IfIndex, PULONG NTEContext, PULONG NTEInstance)
87 FIXME(":stub\n");
88 return ERROR_NOT_SUPPORTED;
92 /******************************************************************
93 * AllocateAndGetIfTableFromStack (IPHLPAPI.@)
95 * Get table of local interfaces.
96 * Like GetIfTable(), but allocate the returned table from heap.
98 * PARAMS
99 * ppIfTable [Out] pointer into which the MIB_IFTABLE is
100 * allocated and returned.
101 * bOrder [In] whether to sort the table
102 * heap [In] heap from which the table is allocated
103 * flags [In] flags to HeapAlloc
105 * RETURNS
106 * ERROR_INVALID_PARAMETER if ppIfTable is NULL, whatever
107 * GetIfTable() returns otherwise.
109 DWORD WINAPI AllocateAndGetIfTableFromStack(PMIB_IFTABLE *ppIfTable,
110 BOOL bOrder, HANDLE heap, DWORD flags)
112 DWORD ret;
114 TRACE("ppIfTable %p, bOrder %d, heap %p, flags 0x%08x\n", ppIfTable,
115 bOrder, heap, flags);
116 if (!ppIfTable)
117 ret = ERROR_INVALID_PARAMETER;
118 else {
119 DWORD dwSize = 0;
121 ret = GetIfTable(*ppIfTable, &dwSize, bOrder);
122 if (ret == ERROR_INSUFFICIENT_BUFFER) {
123 *ppIfTable = HeapAlloc(heap, flags, dwSize);
124 ret = GetIfTable(*ppIfTable, &dwSize, bOrder);
127 TRACE("returning %d\n", ret);
128 return ret;
132 static int IpAddrTableSorter(const void *a, const void *b)
134 int ret;
136 if (a && b)
137 ret = ((const MIB_IPADDRROW*)a)->dwAddr - ((const MIB_IPADDRROW*)b)->dwAddr;
138 else
139 ret = 0;
140 return ret;
144 /******************************************************************
145 * AllocateAndGetIpAddrTableFromStack (IPHLPAPI.@)
147 * Get interface-to-IP address mapping table.
148 * Like GetIpAddrTable(), but allocate the returned table from heap.
150 * PARAMS
151 * ppIpAddrTable [Out] pointer into which the MIB_IPADDRTABLE is
152 * allocated and returned.
153 * bOrder [In] whether to sort the table
154 * heap [In] heap from which the table is allocated
155 * flags [In] flags to HeapAlloc
157 * RETURNS
158 * ERROR_INVALID_PARAMETER if ppIpAddrTable is NULL, other error codes on
159 * failure, NO_ERROR on success.
161 DWORD WINAPI AllocateAndGetIpAddrTableFromStack(PMIB_IPADDRTABLE *ppIpAddrTable,
162 BOOL bOrder, HANDLE heap, DWORD flags)
164 DWORD ret;
166 TRACE("ppIpAddrTable %p, bOrder %d, heap %p, flags 0x%08x\n",
167 ppIpAddrTable, bOrder, heap, flags);
168 ret = getIPAddrTable(ppIpAddrTable, heap, flags);
169 if (!ret && bOrder)
170 qsort((*ppIpAddrTable)->table, (*ppIpAddrTable)->dwNumEntries,
171 sizeof(MIB_IPADDRROW), IpAddrTableSorter);
172 TRACE("returning %d\n", ret);
173 return ret;
177 /******************************************************************
178 * CancelIPChangeNotify (IPHLPAPI.@)
180 * Cancel a previous notification created by NotifyAddrChange or
181 * NotifyRouteChange.
183 * PARAMS
184 * overlapped [In] overlapped structure that notifies the caller
186 * RETURNS
187 * Success: TRUE
188 * Failure: FALSE
190 * FIXME
191 * Stub, returns FALSE.
193 BOOL WINAPI CancelIPChangeNotify(LPOVERLAPPED overlapped)
195 FIXME("(overlapped %p): stub\n", overlapped);
196 return FALSE;
201 /******************************************************************
202 * CreateIpForwardEntry (IPHLPAPI.@)
204 * Create a route in the local computer's IP table.
206 * PARAMS
207 * pRoute [In] new route information
209 * RETURNS
210 * Success: NO_ERROR
211 * Failure: error code from winerror.h
213 * FIXME
214 * Stub, always returns NO_ERROR.
216 DWORD WINAPI CreateIpForwardEntry(PMIB_IPFORWARDROW pRoute)
218 FIXME("(pRoute %p): stub\n", pRoute);
219 /* could use SIOCADDRT, not sure I want to */
220 return 0;
224 /******************************************************************
225 * CreateIpNetEntry (IPHLPAPI.@)
227 * Create entry in the ARP table.
229 * PARAMS
230 * pArpEntry [In] new ARP entry
232 * RETURNS
233 * Success: NO_ERROR
234 * Failure: error code from winerror.h
236 * FIXME
237 * Stub, always returns NO_ERROR.
239 DWORD WINAPI CreateIpNetEntry(PMIB_IPNETROW pArpEntry)
241 FIXME("(pArpEntry %p)\n", pArpEntry);
242 /* could use SIOCSARP on systems that support it, not sure I want to */
243 return 0;
247 /******************************************************************
248 * CreateProxyArpEntry (IPHLPAPI.@)
250 * Create a Proxy ARP (PARP) entry for an IP address.
252 * PARAMS
253 * dwAddress [In] IP address for which this computer acts as a proxy.
254 * dwMask [In] subnet mask for dwAddress
255 * dwIfIndex [In] interface index
257 * RETURNS
258 * Success: NO_ERROR
259 * Failure: error code from winerror.h
261 * FIXME
262 * Stub, returns ERROR_NOT_SUPPORTED.
264 DWORD WINAPI CreateProxyArpEntry(DWORD dwAddress, DWORD dwMask, DWORD dwIfIndex)
266 FIXME("(dwAddress 0x%08x, dwMask 0x%08x, dwIfIndex 0x%08x): stub\n",
267 dwAddress, dwMask, dwIfIndex);
268 return ERROR_NOT_SUPPORTED;
272 /******************************************************************
273 * DeleteIPAddress (IPHLPAPI.@)
275 * Delete an IP address added with AddIPAddress().
277 * PARAMS
278 * NTEContext [In] NTE context from AddIPAddress();
280 * RETURNS
281 * Success: NO_ERROR
282 * Failure: error code from winerror.h
284 * FIXME
285 * Stub, returns ERROR_NOT_SUPPORTED.
287 DWORD WINAPI DeleteIPAddress(ULONG NTEContext)
289 FIXME("(NTEContext %d): stub\n", NTEContext);
290 return ERROR_NOT_SUPPORTED;
294 /******************************************************************
295 * DeleteIpForwardEntry (IPHLPAPI.@)
297 * Delete a route.
299 * PARAMS
300 * pRoute [In] route to delete
302 * RETURNS
303 * Success: NO_ERROR
304 * Failure: error code from winerror.h
306 * FIXME
307 * Stub, returns NO_ERROR.
309 DWORD WINAPI DeleteIpForwardEntry(PMIB_IPFORWARDROW pRoute)
311 FIXME("(pRoute %p): stub\n", pRoute);
312 /* could use SIOCDELRT, not sure I want to */
313 return 0;
317 /******************************************************************
318 * DeleteIpNetEntry (IPHLPAPI.@)
320 * Delete an ARP entry.
322 * PARAMS
323 * pArpEntry [In] ARP entry to delete
325 * RETURNS
326 * Success: NO_ERROR
327 * Failure: error code from winerror.h
329 * FIXME
330 * Stub, returns NO_ERROR.
332 DWORD WINAPI DeleteIpNetEntry(PMIB_IPNETROW pArpEntry)
334 FIXME("(pArpEntry %p): stub\n", pArpEntry);
335 /* could use SIOCDARP on systems that support it, not sure I want to */
336 return 0;
340 /******************************************************************
341 * DeleteProxyArpEntry (IPHLPAPI.@)
343 * Delete a Proxy ARP entry.
345 * PARAMS
346 * dwAddress [In] IP address for which this computer acts as a proxy.
347 * dwMask [In] subnet mask for dwAddress
348 * dwIfIndex [In] interface index
350 * RETURNS
351 * Success: NO_ERROR
352 * Failure: error code from winerror.h
354 * FIXME
355 * Stub, returns ERROR_NOT_SUPPORTED.
357 DWORD WINAPI DeleteProxyArpEntry(DWORD dwAddress, DWORD dwMask, DWORD dwIfIndex)
359 FIXME("(dwAddress 0x%08x, dwMask 0x%08x, dwIfIndex 0x%08x): stub\n",
360 dwAddress, dwMask, dwIfIndex);
361 return ERROR_NOT_SUPPORTED;
365 /******************************************************************
366 * EnableRouter (IPHLPAPI.@)
368 * Turn on ip forwarding.
370 * PARAMS
371 * pHandle [In/Out]
372 * pOverlapped [In/Out] hEvent member should contain a valid handle.
374 * RETURNS
375 * Success: ERROR_IO_PENDING
376 * Failure: error code from winerror.h
378 * FIXME
379 * Stub, returns ERROR_NOT_SUPPORTED.
381 DWORD WINAPI EnableRouter(HANDLE * pHandle, OVERLAPPED * pOverlapped)
383 FIXME("(pHandle %p, pOverlapped %p): stub\n", pHandle, pOverlapped);
384 /* could echo "1" > /proc/net/sys/net/ipv4/ip_forward, not sure I want to
385 could map EACCESS to ERROR_ACCESS_DENIED, I suppose
387 return ERROR_NOT_SUPPORTED;
391 /******************************************************************
392 * FlushIpNetTable (IPHLPAPI.@)
394 * Delete all ARP entries of an interface
396 * PARAMS
397 * dwIfIndex [In] interface index
399 * RETURNS
400 * Success: NO_ERROR
401 * Failure: error code from winerror.h
403 * FIXME
404 * Stub, returns ERROR_NOT_SUPPORTED.
406 DWORD WINAPI FlushIpNetTable(DWORD dwIfIndex)
408 FIXME("(dwIfIndex 0x%08x): stub\n", dwIfIndex);
409 /* this flushes the arp cache of the given index */
410 return ERROR_NOT_SUPPORTED;
414 /******************************************************************
415 * GetAdapterIndex (IPHLPAPI.@)
417 * Get interface index from its name.
419 * PARAMS
420 * AdapterName [In] unicode string with the adapter name
421 * IfIndex [Out] returns found interface index
423 * RETURNS
424 * Success: NO_ERROR
425 * Failure: error code from winerror.h
427 DWORD WINAPI GetAdapterIndex(LPWSTR AdapterName, PULONG IfIndex)
429 char adapterName[MAX_ADAPTER_NAME];
430 unsigned int i;
431 DWORD ret;
433 TRACE("(AdapterName %p, IfIndex %p)\n", AdapterName, IfIndex);
434 /* The adapter name is guaranteed not to have any unicode characters, so
435 * this translation is never lossy */
436 for (i = 0; i < sizeof(adapterName) - 1 && AdapterName[i]; i++)
437 adapterName[i] = (char)AdapterName[i];
438 adapterName[i] = '\0';
439 ret = getInterfaceIndexByName(adapterName, IfIndex);
440 TRACE("returning %d\n", ret);
441 return ret;
445 /******************************************************************
446 * GetAdaptersInfo (IPHLPAPI.@)
448 * Get information about adapters.
450 * PARAMS
451 * pAdapterInfo [Out] buffer for adapter infos
452 * pOutBufLen [In] length of output buffer
454 * RETURNS
455 * Success: NO_ERROR
456 * Failure: error code from winerror.h
458 DWORD WINAPI GetAdaptersInfo(PIP_ADAPTER_INFO pAdapterInfo, PULONG pOutBufLen)
460 DWORD ret;
462 TRACE("pAdapterInfo %p, pOutBufLen %p\n", pAdapterInfo, pOutBufLen);
463 if (!pOutBufLen)
464 ret = ERROR_INVALID_PARAMETER;
465 else {
466 DWORD numNonLoopbackInterfaces = get_interface_indices( TRUE, NULL );
468 if (numNonLoopbackInterfaces > 0) {
469 DWORD numIPAddresses = getNumIPAddresses();
470 ULONG size;
472 /* This may slightly overestimate the amount of space needed, because
473 * the IP addresses include the loopback address, but it's easier
474 * to make sure there's more than enough space than to make sure there's
475 * precisely enough space.
477 size = sizeof(IP_ADAPTER_INFO) * numNonLoopbackInterfaces;
478 size += numIPAddresses * sizeof(IP_ADDR_STRING);
479 if (!pAdapterInfo || *pOutBufLen < size) {
480 *pOutBufLen = size;
481 ret = ERROR_BUFFER_OVERFLOW;
483 else {
484 InterfaceIndexTable *table = NULL;
485 PMIB_IPADDRTABLE ipAddrTable = NULL;
486 PMIB_IPFORWARDTABLE routeTable = NULL;
488 ret = getIPAddrTable(&ipAddrTable, GetProcessHeap(), 0);
489 if (!ret)
490 ret = AllocateAndGetIpForwardTableFromStack(&routeTable, FALSE, GetProcessHeap(), 0);
491 if (!ret)
492 get_interface_indices( TRUE, &table );
493 if (table) {
494 size = sizeof(IP_ADAPTER_INFO) * table->numIndexes;
495 size += ipAddrTable->dwNumEntries * sizeof(IP_ADDR_STRING);
496 if (*pOutBufLen < size) {
497 *pOutBufLen = size;
498 ret = ERROR_INSUFFICIENT_BUFFER;
500 else {
501 DWORD ndx;
502 HKEY hKey;
503 BOOL winsEnabled = FALSE;
504 IP_ADDRESS_STRING primaryWINS, secondaryWINS;
505 PIP_ADDR_STRING nextIPAddr = (PIP_ADDR_STRING)((LPBYTE)pAdapterInfo
506 + numNonLoopbackInterfaces * sizeof(IP_ADAPTER_INFO));
508 memset(pAdapterInfo, 0, size);
509 /* @@ Wine registry key: HKCU\Software\Wine\Network */
510 if (RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Network",
511 &hKey) == ERROR_SUCCESS) {
512 DWORD size = sizeof(primaryWINS.String);
513 unsigned long addr;
515 RegQueryValueExA(hKey, "WinsServer", NULL, NULL,
516 (LPBYTE)primaryWINS.String, &size);
517 addr = inet_addr(primaryWINS.String);
518 if (addr != INADDR_NONE && addr != INADDR_ANY)
519 winsEnabled = TRUE;
520 size = sizeof(secondaryWINS.String);
521 RegQueryValueExA(hKey, "BackupWinsServer", NULL, NULL,
522 (LPBYTE)secondaryWINS.String, &size);
523 addr = inet_addr(secondaryWINS.String);
524 if (addr != INADDR_NONE && addr != INADDR_ANY)
525 winsEnabled = TRUE;
526 RegCloseKey(hKey);
528 for (ndx = 0; ndx < table->numIndexes; ndx++) {
529 PIP_ADAPTER_INFO ptr = &pAdapterInfo[ndx];
530 DWORD i;
531 PIP_ADDR_STRING currentIPAddr = &ptr->IpAddressList;
532 BOOL firstIPAddr = TRUE;
534 /* on Win98 this is left empty, but whatever */
535 getInterfaceNameByIndex(table->indexes[ndx], ptr->AdapterName);
536 getInterfaceNameByIndex(table->indexes[ndx], ptr->Description);
537 ptr->AddressLength = sizeof(ptr->Address);
538 getInterfacePhysicalByIndex(table->indexes[ndx],
539 &ptr->AddressLength, ptr->Address, &ptr->Type);
540 ptr->Index = table->indexes[ndx];
541 for (i = 0; i < ipAddrTable->dwNumEntries; i++) {
542 if (ipAddrTable->table[i].dwIndex == ptr->Index) {
543 if (firstIPAddr) {
544 toIPAddressString(ipAddrTable->table[i].dwAddr,
545 ptr->IpAddressList.IpAddress.String);
546 toIPAddressString(ipAddrTable->table[i].dwMask,
547 ptr->IpAddressList.IpMask.String);
548 firstIPAddr = FALSE;
550 else {
551 currentIPAddr->Next = nextIPAddr;
552 currentIPAddr = nextIPAddr;
553 toIPAddressString(ipAddrTable->table[i].dwAddr,
554 currentIPAddr->IpAddress.String);
555 toIPAddressString(ipAddrTable->table[i].dwMask,
556 currentIPAddr->IpMask.String);
557 nextIPAddr++;
561 /* Find first router through this interface, which we'll assume
562 * is the default gateway for this adapter */
563 for (i = 0; i < routeTable->dwNumEntries; i++)
564 if (routeTable->table[i].dwForwardIfIndex == ptr->Index
565 && routeTable->table[i].u1.ForwardType ==
566 MIB_IPROUTE_TYPE_INDIRECT)
568 toIPAddressString(routeTable->table[i].dwForwardNextHop,
569 ptr->GatewayList.IpAddress.String);
570 toIPAddressString(routeTable->table[i].dwForwardMask,
571 ptr->GatewayList.IpMask.String);
573 if (winsEnabled) {
574 ptr->HaveWins = TRUE;
575 memcpy(ptr->PrimaryWinsServer.IpAddress.String,
576 primaryWINS.String, sizeof(primaryWINS.String));
577 memcpy(ptr->SecondaryWinsServer.IpAddress.String,
578 secondaryWINS.String, sizeof(secondaryWINS.String));
580 if (ndx < table->numIndexes - 1)
581 ptr->Next = &pAdapterInfo[ndx + 1];
582 else
583 ptr->Next = NULL;
585 ptr->DhcpEnabled = TRUE;
587 ret = NO_ERROR;
589 HeapFree(GetProcessHeap(), 0, table);
591 else
592 ret = ERROR_OUTOFMEMORY;
593 HeapFree(GetProcessHeap(), 0, routeTable);
594 HeapFree(GetProcessHeap(), 0, ipAddrTable);
597 else
598 ret = ERROR_NO_DATA;
600 TRACE("returning %d\n", ret);
601 return ret;
604 static DWORD typeFromMibType(DWORD mib_type)
606 switch (mib_type)
608 case MIB_IF_TYPE_ETHERNET: return IF_TYPE_ETHERNET_CSMACD;
609 case MIB_IF_TYPE_TOKENRING: return IF_TYPE_ISO88025_TOKENRING;
610 case MIB_IF_TYPE_PPP: return IF_TYPE_PPP;
611 case MIB_IF_TYPE_LOOPBACK: return IF_TYPE_SOFTWARE_LOOPBACK;
612 default: return IF_TYPE_OTHER;
616 static NET_IF_CONNECTION_TYPE connectionTypeFromMibType(DWORD mib_type)
618 switch (mib_type)
620 case MIB_IF_TYPE_PPP: return NET_IF_CONNECTION_DEMAND;
621 case MIB_IF_TYPE_SLIP: return NET_IF_CONNECTION_DEMAND;
622 default: return NET_IF_CONNECTION_DEDICATED;
626 static ULONG v4addressesFromIndex(IF_INDEX index, DWORD **addrs, ULONG *num_addrs, DWORD **masks)
628 ULONG ret, i, j;
629 MIB_IPADDRTABLE *at;
631 *num_addrs = 0;
632 if ((ret = getIPAddrTable(&at, GetProcessHeap(), 0))) return ret;
633 for (i = 0; i < at->dwNumEntries; i++)
635 if (at->table[i].dwIndex == index) (*num_addrs)++;
637 if (!(*addrs = HeapAlloc(GetProcessHeap(), 0, *num_addrs * sizeof(DWORD))))
639 HeapFree(GetProcessHeap(), 0, at);
640 return ERROR_OUTOFMEMORY;
642 if (!(*masks = HeapAlloc(GetProcessHeap(), 0, *num_addrs * sizeof(DWORD))))
644 HeapFree(GetProcessHeap(), 0, *addrs);
645 HeapFree(GetProcessHeap(), 0, at);
646 return ERROR_OUTOFMEMORY;
648 for (i = 0, j = 0; i < at->dwNumEntries; i++)
650 if (at->table[i].dwIndex == index)
652 (*addrs)[j] = at->table[i].dwAddr;
653 (*masks)[j] = at->table[i].dwMask;
654 j++;
657 HeapFree(GetProcessHeap(), 0, at);
658 return ERROR_SUCCESS;
661 static char *debugstr_ipv4(const in_addr_t *in_addr, char *buf)
663 const BYTE *addrp;
664 char *p = buf;
666 for (addrp = (const BYTE *)in_addr;
667 addrp - (const BYTE *)in_addr < sizeof(*in_addr);
668 addrp++)
670 if (addrp == (const BYTE *)in_addr + sizeof(*in_addr) - 1)
671 sprintf(p, "%d", *addrp);
672 else
673 p += sprintf(p, "%d.", *addrp);
675 return buf;
678 static char *debugstr_ipv6(const struct WS_sockaddr_in6 *sin, char *buf)
680 const IN6_ADDR *addr = &sin->sin6_addr;
681 char *p = buf;
682 int i;
683 BOOL in_zero = FALSE;
685 for (i = 0; i < 7; i++)
687 if (!addr->u.Word[i])
689 if (i == 0)
690 *p++ = ':';
691 if (!in_zero)
693 *p++ = ':';
694 in_zero = TRUE;
697 else
699 p += sprintf(p, "%x:", ntohs(addr->u.Word[i]));
700 in_zero = FALSE;
703 sprintf(p, "%x", ntohs(addr->u.Word[7]));
704 return buf;
707 static ULONG count_v4_gateways(DWORD index, PMIB_IPFORWARDTABLE routeTable)
709 DWORD i, num_gateways = 0;
711 for (i = 0; i < routeTable->dwNumEntries; i++)
713 if (routeTable->table[i].dwForwardIfIndex == index &&
714 routeTable->table[i].u1.ForwardType == MIB_IPROUTE_TYPE_INDIRECT)
715 num_gateways++;
717 return num_gateways;
720 static PMIB_IPFORWARDROW findIPv4Gateway(DWORD index,
721 PMIB_IPFORWARDTABLE routeTable)
723 DWORD i;
724 PMIB_IPFORWARDROW row = NULL;
726 for (i = 0; !row && i < routeTable->dwNumEntries; i++)
728 if (routeTable->table[i].dwForwardIfIndex == index &&
729 routeTable->table[i].u1.ForwardType == MIB_IPROUTE_TYPE_INDIRECT)
730 row = &routeTable->table[i];
732 return row;
735 static ULONG adapterAddressesFromIndex(ULONG family, ULONG flags, IF_INDEX index,
736 IP_ADAPTER_ADDRESSES *aa, ULONG *size)
738 ULONG ret = ERROR_SUCCESS, i, j, num_v4addrs = 0, num_v4_gateways = 0, num_v6addrs = 0, total_size;
739 DWORD *v4addrs = NULL, *v4masks = NULL;
740 SOCKET_ADDRESS *v6addrs = NULL, *v6masks = NULL;
741 PMIB_IPFORWARDTABLE routeTable = NULL;
743 if (family == WS_AF_INET)
745 ret = v4addressesFromIndex(index, &v4addrs, &num_v4addrs, &v4masks);
747 if (!ret && flags & GAA_FLAG_INCLUDE_ALL_GATEWAYS)
749 ret = AllocateAndGetIpForwardTableFromStack(&routeTable, FALSE, GetProcessHeap(), 0);
750 if (!ret) num_v4_gateways = count_v4_gateways(index, routeTable);
753 else if (family == WS_AF_INET6)
755 ret = v6addressesFromIndex(index, &v6addrs, &num_v6addrs, &v6masks);
757 else if (family == WS_AF_UNSPEC)
759 ret = v4addressesFromIndex(index, &v4addrs, &num_v4addrs, &v4masks);
761 if (!ret && flags & GAA_FLAG_INCLUDE_ALL_GATEWAYS)
763 ret = AllocateAndGetIpForwardTableFromStack(&routeTable, FALSE, GetProcessHeap(), 0);
764 if (!ret) num_v4_gateways = count_v4_gateways(index, routeTable);
766 if (!ret) ret = v6addressesFromIndex(index, &v6addrs, &num_v6addrs, &v6masks);
768 else
770 FIXME("address family %u unsupported\n", family);
771 ret = ERROR_NO_DATA;
773 if (ret)
775 HeapFree(GetProcessHeap(), 0, v4addrs);
776 HeapFree(GetProcessHeap(), 0, v4masks);
777 HeapFree(GetProcessHeap(), 0, v6addrs);
778 HeapFree(GetProcessHeap(), 0, v6masks);
779 HeapFree(GetProcessHeap(), 0, routeTable);
780 return ret;
783 total_size = sizeof(IP_ADAPTER_ADDRESSES);
784 total_size += IF_NAMESIZE;
785 total_size += IF_NAMESIZE * sizeof(WCHAR);
786 if (!(flags & GAA_FLAG_SKIP_FRIENDLY_NAME))
787 total_size += IF_NAMESIZE * sizeof(WCHAR);
788 if (flags & GAA_FLAG_INCLUDE_PREFIX)
790 total_size += sizeof(IP_ADAPTER_PREFIX) * num_v4addrs;
791 total_size += sizeof(IP_ADAPTER_PREFIX) * num_v6addrs;
792 total_size += sizeof(struct sockaddr_in) * num_v4addrs;
793 for (i = 0; i < num_v6addrs; i++)
794 total_size += v6masks[i].iSockaddrLength;
796 total_size += sizeof(IP_ADAPTER_UNICAST_ADDRESS) * num_v4addrs;
797 total_size += sizeof(struct sockaddr_in) * num_v4addrs;
798 total_size += (sizeof(IP_ADAPTER_GATEWAY_ADDRESS) + sizeof(SOCKADDR_IN)) * num_v4_gateways;
799 total_size += sizeof(IP_ADAPTER_UNICAST_ADDRESS) * num_v6addrs;
800 total_size += sizeof(SOCKET_ADDRESS) * num_v6addrs;
801 for (i = 0; i < num_v6addrs; i++)
802 total_size += v6addrs[i].iSockaddrLength;
804 if (aa && *size >= total_size)
806 char name[IF_NAMESIZE], *ptr = (char *)aa + sizeof(IP_ADAPTER_ADDRESSES), *src;
807 WCHAR *dst;
808 DWORD buflen, type;
809 INTERNAL_IF_OPER_STATUS status;
811 memset(aa, 0, sizeof(IP_ADAPTER_ADDRESSES));
812 aa->u.s.Length = sizeof(IP_ADAPTER_ADDRESSES);
813 aa->u.s.IfIndex = index;
815 getInterfaceNameByIndex(index, name);
816 memcpy(ptr, name, IF_NAMESIZE);
817 aa->AdapterName = ptr;
818 ptr += IF_NAMESIZE;
819 if (!(flags & GAA_FLAG_SKIP_FRIENDLY_NAME))
821 aa->FriendlyName = (WCHAR *)ptr;
822 for (src = name, dst = (WCHAR *)ptr; *src; src++, dst++)
823 *dst = *src;
824 *dst++ = 0;
825 ptr = (char *)dst;
827 aa->Description = (WCHAR *)ptr;
828 for (src = name, dst = (WCHAR *)ptr; *src; src++, dst++)
829 *dst = *src;
830 *dst++ = 0;
831 ptr = (char *)dst;
833 TRACE("%s: %d IPv4 addresses, %d IPv6 addresses:\n", name, num_v4addrs,
834 num_v6addrs);
835 if (num_v4_gateways)
837 PMIB_IPFORWARDROW adapterRow;
839 if ((adapterRow = findIPv4Gateway(index, routeTable)))
841 PIP_ADAPTER_GATEWAY_ADDRESS gw;
842 PSOCKADDR_IN sin;
844 gw = (PIP_ADAPTER_GATEWAY_ADDRESS)ptr;
845 aa->FirstGatewayAddress = gw;
847 gw->u.s.Length = sizeof(IP_ADAPTER_GATEWAY_ADDRESS);
848 ptr += sizeof(IP_ADAPTER_GATEWAY_ADDRESS);
849 sin = (PSOCKADDR_IN)ptr;
850 sin->sin_family = AF_INET;
851 sin->sin_port = 0;
852 memcpy(&sin->sin_addr, &adapterRow->dwForwardNextHop,
853 sizeof(DWORD));
854 gw->Address.lpSockaddr = (LPSOCKADDR)sin;
855 gw->Address.iSockaddrLength = sizeof(SOCKADDR_IN);
856 gw->Next = NULL;
857 ptr += sizeof(SOCKADDR_IN);
860 if (num_v4addrs && !(flags & GAA_FLAG_SKIP_UNICAST))
862 IP_ADAPTER_UNICAST_ADDRESS *ua;
863 struct WS_sockaddr_in *sa;
864 aa->Flags |= IP_ADAPTER_IPV4_ENABLED;
865 ua = aa->FirstUnicastAddress = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
866 for (i = 0; i < num_v4addrs; i++)
868 char addr_buf[16];
870 memset(ua, 0, sizeof(IP_ADAPTER_UNICAST_ADDRESS));
871 ua->u.s.Length = sizeof(IP_ADAPTER_UNICAST_ADDRESS);
872 ua->Address.iSockaddrLength = sizeof(struct sockaddr_in);
873 ua->Address.lpSockaddr = (SOCKADDR *)((char *)ua + ua->u.s.Length);
875 sa = (struct WS_sockaddr_in *)ua->Address.lpSockaddr;
876 sa->sin_family = WS_AF_INET;
877 sa->sin_addr.S_un.S_addr = v4addrs[i];
878 sa->sin_port = 0;
879 TRACE("IPv4 %d/%d: %s\n", i + 1, num_v4addrs,
880 debugstr_ipv4(&sa->sin_addr.S_un.S_addr, addr_buf));
882 ptr += ua->u.s.Length + ua->Address.iSockaddrLength;
883 if (i < num_v4addrs - 1)
885 ua->Next = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
886 ua = ua->Next;
890 if (num_v6addrs && !(flags & GAA_FLAG_SKIP_UNICAST))
892 IP_ADAPTER_UNICAST_ADDRESS *ua;
893 struct WS_sockaddr_in6 *sa;
895 aa->Flags |= IP_ADAPTER_IPV6_ENABLED;
896 if (aa->FirstUnicastAddress)
898 for (ua = aa->FirstUnicastAddress; ua->Next; ua = ua->Next)
900 ua->Next = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
901 ua = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
903 else
904 ua = aa->FirstUnicastAddress = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
905 for (i = 0; i < num_v6addrs; i++)
907 char addr_buf[46];
909 memset(ua, 0, sizeof(IP_ADAPTER_UNICAST_ADDRESS));
910 ua->u.s.Length = sizeof(IP_ADAPTER_UNICAST_ADDRESS);
911 ua->Address.iSockaddrLength = v6addrs[i].iSockaddrLength;
912 ua->Address.lpSockaddr = (SOCKADDR *)((char *)ua + ua->u.s.Length);
914 sa = (struct WS_sockaddr_in6 *)ua->Address.lpSockaddr;
915 memcpy(sa, v6addrs[i].lpSockaddr, sizeof(*sa));
916 TRACE("IPv6 %d/%d: %s\n", i + 1, num_v6addrs,
917 debugstr_ipv6(sa, addr_buf));
919 ptr += ua->u.s.Length + ua->Address.iSockaddrLength;
920 if (i < num_v6addrs - 1)
922 ua->Next = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
923 ua = ua->Next;
927 if (num_v4addrs && (flags & GAA_FLAG_INCLUDE_PREFIX))
929 IP_ADAPTER_PREFIX *prefix;
931 prefix = aa->FirstPrefix = (IP_ADAPTER_PREFIX *)ptr;
932 for (i = 0; i < num_v4addrs; i++)
934 char addr_buf[16];
935 struct WS_sockaddr_in *sa;
937 prefix->u.s.Length = sizeof(*prefix);
938 prefix->u.s.Flags = 0;
939 prefix->Next = NULL;
940 prefix->Address.iSockaddrLength = sizeof(struct sockaddr_in);
941 prefix->Address.lpSockaddr = (SOCKADDR *)((char *)prefix + prefix->u.s.Length);
943 sa = (struct WS_sockaddr_in *)prefix->Address.lpSockaddr;
944 sa->sin_family = WS_AF_INET;
945 sa->sin_addr.S_un.S_addr = v4addrs[i] & v4masks[i];
946 sa->sin_port = 0;
948 prefix->PrefixLength = 0;
949 for (j = 0; j < sizeof(*v4masks) * 8; j++)
951 if (v4masks[i] & 1 << j) prefix->PrefixLength++;
952 else break;
954 TRACE("IPv4 network: %s/%u\n",
955 debugstr_ipv4((const in_addr_t *)&sa->sin_addr.S_un.S_addr, addr_buf),
956 prefix->PrefixLength);
958 ptr += prefix->u.s.Length + prefix->Address.iSockaddrLength;
959 if (i < num_v4addrs - 1)
961 prefix->Next = (IP_ADAPTER_PREFIX *)ptr;
962 prefix = prefix->Next;
966 if (num_v6addrs && (flags & GAA_FLAG_INCLUDE_PREFIX))
968 IP_ADAPTER_PREFIX *prefix;
970 if (aa->FirstPrefix)
972 for (prefix = aa->FirstPrefix; prefix->Next; prefix = prefix->Next)
974 prefix->Next = (IP_ADAPTER_PREFIX *)ptr;
975 prefix = (IP_ADAPTER_PREFIX *)ptr;
977 else
978 prefix = aa->FirstPrefix = (IP_ADAPTER_PREFIX *)ptr;
979 for (i = 0; i < num_v6addrs; i++)
981 char addr_buf[46];
982 struct WS_sockaddr_in6 *sa;
983 const IN6_ADDR *addr, *mask;
984 BOOL done = FALSE;
986 prefix->u.s.Length = sizeof(*prefix);
987 prefix->u.s.Flags = 0;
988 prefix->Next = NULL;
989 prefix->Address.iSockaddrLength = sizeof(struct sockaddr_in6);
990 prefix->Address.lpSockaddr = (SOCKADDR *)((char *)prefix + prefix->u.s.Length);
992 sa = (struct WS_sockaddr_in6 *)prefix->Address.lpSockaddr;
993 sa->sin6_family = WS_AF_INET6;
994 sa->sin6_port = 0;
995 sa->sin6_flowinfo = 0;
996 addr = &((struct WS_sockaddr_in6 *)v6addrs[i].lpSockaddr)->sin6_addr;
997 mask = &((struct WS_sockaddr_in6 *)v6masks[i].lpSockaddr)->sin6_addr;
998 for (j = 0; j < 8; j++) sa->sin6_addr.u.Word[j] = addr->u.Word[j] & mask->u.Word[j];
999 sa->sin6_scope_id = 0;
1001 prefix->PrefixLength = 0;
1002 for (i = 0; i < 8 && !done; i++)
1004 for (j = 0; j < sizeof(WORD) * 8 && !done; j++)
1006 if (mask->u.Word[i] & 1 << j) prefix->PrefixLength++;
1007 else done = TRUE;
1010 TRACE("IPv6 network: %s/%u\n", debugstr_ipv6(sa, addr_buf), prefix->PrefixLength);
1012 ptr += prefix->u.s.Length + prefix->Address.iSockaddrLength;
1013 if (i < num_v6addrs - 1)
1015 prefix->Next = (IP_ADAPTER_PREFIX *)ptr;
1016 prefix = prefix->Next;
1021 buflen = MAX_INTERFACE_PHYSADDR;
1022 getInterfacePhysicalByIndex(index, &buflen, aa->PhysicalAddress, &type);
1023 aa->PhysicalAddressLength = buflen;
1024 aa->IfType = typeFromMibType(type);
1025 aa->ConnectionType = connectionTypeFromMibType(type);
1027 getInterfaceMtuByName(name, &aa->Mtu);
1029 getInterfaceStatusByName(name, &status);
1030 if (status == MIB_IF_OPER_STATUS_OPERATIONAL) aa->OperStatus = IfOperStatusUp;
1031 else if (status == MIB_IF_OPER_STATUS_NON_OPERATIONAL) aa->OperStatus = IfOperStatusDown;
1032 else aa->OperStatus = IfOperStatusUnknown;
1034 *size = total_size;
1035 HeapFree(GetProcessHeap(), 0, routeTable);
1036 HeapFree(GetProcessHeap(), 0, v6addrs);
1037 HeapFree(GetProcessHeap(), 0, v6masks);
1038 HeapFree(GetProcessHeap(), 0, v4addrs);
1039 HeapFree(GetProcessHeap(), 0, v4masks);
1040 return ERROR_SUCCESS;
1043 static void sockaddr_in_to_WS_storage( SOCKADDR_STORAGE *dst, const struct sockaddr_in *src )
1045 SOCKADDR_IN *s = (SOCKADDR_IN *)dst;
1047 s->sin_family = WS_AF_INET;
1048 s->sin_port = src->sin_port;
1049 memcpy( &s->sin_addr, &src->sin_addr, sizeof(IN_ADDR) );
1050 memset( (char *)s + FIELD_OFFSET( SOCKADDR_IN, sin_zero ), 0,
1051 sizeof(SOCKADDR_STORAGE) - FIELD_OFFSET( SOCKADDR_IN, sin_zero) );
1054 static void sockaddr_in6_to_WS_storage( SOCKADDR_STORAGE *dst, const struct sockaddr_in6 *src )
1056 SOCKADDR_IN6 *s = (SOCKADDR_IN6 *)dst;
1058 s->sin6_family = WS_AF_INET6;
1059 s->sin6_port = src->sin6_port;
1060 s->sin6_flowinfo = src->sin6_flowinfo;
1061 memcpy( &s->sin6_addr, &src->sin6_addr, sizeof(IN6_ADDR) );
1062 s->sin6_scope_id = src->sin6_scope_id;
1063 memset( (char *)s + sizeof(SOCKADDR_IN6), 0,
1064 sizeof(SOCKADDR_STORAGE) - sizeof(SOCKADDR_IN6) );
1067 #ifdef HAVE_STRUCT___RES_STATE
1068 /* call res_init() just once because of a bug in Mac OS X 10.4 */
1069 /* Call once per thread on systems that have per-thread _res. */
1070 static void initialise_resolver(void)
1072 if ((_res.options & RES_INIT) == 0)
1073 res_init();
1076 static int get_dns_servers( SOCKADDR_STORAGE *servers, int num, BOOL ip4_only )
1078 int i, ip6_count = 0;
1079 SOCKADDR_STORAGE *addr;
1081 initialise_resolver();
1083 #ifdef HAVE_STRUCT___RES_STATE__U__EXT_NSCOUNT6
1084 ip6_count = _res._u._ext.nscount6;
1085 #endif
1087 if (!servers || !num)
1089 num = _res.nscount;
1090 if (ip4_only) num -= ip6_count;
1091 return num;
1094 for (i = 0, addr = servers; addr < (servers + num) && i < _res.nscount; i++)
1096 #ifdef HAVE_STRUCT___RES_STATE__U__EXT_NSCOUNT6
1097 if (_res._u._ext.nsaddrs[i])
1099 if (ip4_only) continue;
1100 sockaddr_in6_to_WS_storage( addr, _res._u._ext.nsaddrs[i] );
1102 else
1103 #endif
1105 sockaddr_in_to_WS_storage( addr, _res.nsaddr_list + i );
1107 addr++;
1109 return addr - servers;
1111 #elif defined(HAVE___RES_GET_STATE) && defined(HAVE___RES_GETSERVERS)
1113 static int get_dns_servers( SOCKADDR_STORAGE *servers, int num, BOOL ip4_only )
1115 extern struct res_state *__res_get_state( void );
1116 extern int __res_getservers( struct res_state *, struct sockaddr_storage *, int );
1117 struct res_state *state = __res_get_state();
1118 int i, found = 0, total = __res_getservers( state, NULL, 0 );
1119 SOCKADDR_STORAGE *addr = servers;
1120 struct sockaddr_storage *buf;
1122 if ((!servers || !num) && !ip4_only) return total;
1124 buf = HeapAlloc( GetProcessHeap(), 0, total * sizeof(struct sockaddr_storage) );
1125 total = __res_getservers( state, buf, total );
1127 for (i = 0; i < total; i++)
1129 if (buf[i].ss_family == AF_INET6 && ip4_only) continue;
1130 if (buf[i].ss_family != AF_INET && buf[i].ss_family != AF_INET6) continue;
1132 found++;
1133 if (!servers || !num) continue;
1135 if (buf[i].ss_family == AF_INET6)
1137 sockaddr_in6_to_WS_storage( addr, (struct sockaddr_in6 *)(buf + i) );
1139 else
1141 sockaddr_in_to_WS_storage( addr, (struct sockaddr_in *)(buf + i) );
1143 if (++addr >= servers + num) break;
1146 HeapFree( GetProcessHeap(), 0, buf );
1147 return found;
1149 #else
1151 static int get_dns_servers( SOCKADDR_STORAGE *servers, int num, BOOL ip4_only )
1153 FIXME("Unimplemented on this system\n");
1154 return 0;
1156 #endif
1158 static ULONG get_dns_server_addresses(PIP_ADAPTER_DNS_SERVER_ADDRESS address, ULONG *len)
1160 int num = get_dns_servers( NULL, 0, FALSE );
1161 DWORD size;
1163 size = num * (sizeof(IP_ADAPTER_DNS_SERVER_ADDRESS) + sizeof(SOCKADDR_STORAGE));
1164 if (!address || *len < size)
1166 *len = size;
1167 return ERROR_BUFFER_OVERFLOW;
1169 *len = size;
1170 if (num > 0)
1172 PIP_ADAPTER_DNS_SERVER_ADDRESS addr = address;
1173 SOCKADDR_STORAGE *sock_addrs = (SOCKADDR_STORAGE *)(address + num);
1174 int i;
1176 get_dns_servers( sock_addrs, num, FALSE );
1178 for (i = 0; i < num; i++, addr = addr->Next)
1180 addr->u.s.Length = sizeof(*addr);
1181 if (sock_addrs[i].ss_family == WS_AF_INET6)
1182 addr->Address.iSockaddrLength = sizeof(SOCKADDR_IN6);
1183 else
1184 addr->Address.iSockaddrLength = sizeof(SOCKADDR_IN);
1185 addr->Address.lpSockaddr = (SOCKADDR *)(sock_addrs + i);
1186 if (i == num - 1)
1187 addr->Next = NULL;
1188 else
1189 addr->Next = addr + 1;
1192 return ERROR_SUCCESS;
1195 #ifdef HAVE_STRUCT___RES_STATE
1196 static BOOL is_ip_address_string(const char *str)
1198 struct in_addr in;
1199 int ret;
1201 ret = inet_aton(str, &in);
1202 return ret != 0;
1204 #endif
1206 static ULONG get_dns_suffix(WCHAR *suffix, ULONG *len)
1208 ULONG size;
1209 const char *found_suffix = "";
1210 /* Always return a NULL-terminated string, even if it's empty. */
1212 #ifdef HAVE_STRUCT___RES_STATE
1214 ULONG i;
1215 initialise_resolver();
1216 for (i = 0; !*found_suffix && i < MAXDNSRCH + 1 && _res.dnsrch[i]; i++)
1218 /* This uses a heuristic to select a DNS suffix:
1219 * the first, non-IP address string is selected.
1221 if (!is_ip_address_string(_res.dnsrch[i]))
1222 found_suffix = _res.dnsrch[i];
1225 #endif
1227 size = MultiByteToWideChar( CP_UNIXCP, 0, found_suffix, -1, NULL, 0 ) * sizeof(WCHAR);
1228 if (!suffix || *len < size)
1230 *len = size;
1231 return ERROR_BUFFER_OVERFLOW;
1233 *len = MultiByteToWideChar( CP_UNIXCP, 0, found_suffix, -1, suffix, *len / sizeof(WCHAR) ) * sizeof(WCHAR);
1234 return ERROR_SUCCESS;
1237 ULONG WINAPI DECLSPEC_HOTPATCH GetAdaptersAddresses(ULONG family, ULONG flags, PVOID reserved,
1238 PIP_ADAPTER_ADDRESSES aa, PULONG buflen)
1240 InterfaceIndexTable *table;
1241 ULONG i, size, dns_server_size, dns_suffix_size, total_size, ret = ERROR_NO_DATA;
1243 TRACE("(%d, %08x, %p, %p, %p)\n", family, flags, reserved, aa, buflen);
1245 if (!buflen) return ERROR_INVALID_PARAMETER;
1247 get_interface_indices( FALSE, &table );
1248 if (!table || !table->numIndexes)
1250 HeapFree(GetProcessHeap(), 0, table);
1251 return ERROR_NO_DATA;
1253 total_size = 0;
1254 for (i = 0; i < table->numIndexes; i++)
1256 size = 0;
1257 if ((ret = adapterAddressesFromIndex(family, flags, table->indexes[i], NULL, &size)))
1259 HeapFree(GetProcessHeap(), 0, table);
1260 return ret;
1262 total_size += size;
1264 if (!(flags & GAA_FLAG_SKIP_DNS_SERVER))
1266 /* Since DNS servers aren't really per adapter, get enough space for a
1267 * single copy of them.
1269 get_dns_server_addresses(NULL, &dns_server_size);
1270 total_size += dns_server_size;
1272 /* Since DNS suffix also isn't really per adapter, get enough space for a
1273 * single copy of it.
1275 get_dns_suffix(NULL, &dns_suffix_size);
1276 total_size += dns_suffix_size;
1277 if (aa && *buflen >= total_size)
1279 ULONG bytes_left = size = total_size;
1280 PIP_ADAPTER_ADDRESSES first_aa = aa;
1281 PIP_ADAPTER_DNS_SERVER_ADDRESS firstDns;
1282 WCHAR *dnsSuffix;
1284 for (i = 0; i < table->numIndexes; i++)
1286 if ((ret = adapterAddressesFromIndex(family, flags, table->indexes[i], aa, &size)))
1288 HeapFree(GetProcessHeap(), 0, table);
1289 return ret;
1291 if (i < table->numIndexes - 1)
1293 aa->Next = (IP_ADAPTER_ADDRESSES *)((char *)aa + size);
1294 aa = aa->Next;
1295 size = bytes_left -= size;
1298 if (!(flags & GAA_FLAG_SKIP_DNS_SERVER) && dns_server_size)
1300 firstDns = (PIP_ADAPTER_DNS_SERVER_ADDRESS)((BYTE *)first_aa + total_size - dns_server_size - dns_suffix_size);
1301 get_dns_server_addresses(firstDns, &dns_server_size);
1302 for (aa = first_aa; aa; aa = aa->Next)
1304 if (aa->IfType != IF_TYPE_SOFTWARE_LOOPBACK && aa->OperStatus == IfOperStatusUp)
1305 aa->FirstDnsServerAddress = firstDns;
1308 aa = first_aa;
1309 dnsSuffix = (WCHAR *)((BYTE *)aa + total_size - dns_suffix_size);
1310 get_dns_suffix(dnsSuffix, &dns_suffix_size);
1311 for (; aa; aa = aa->Next)
1313 if (aa->IfType != IF_TYPE_SOFTWARE_LOOPBACK && aa->OperStatus == IfOperStatusUp)
1314 aa->DnsSuffix = dnsSuffix;
1315 else
1316 aa->DnsSuffix = dnsSuffix + dns_suffix_size / sizeof(WCHAR) - 1;
1318 ret = ERROR_SUCCESS;
1320 else
1321 ret = ERROR_BUFFER_OVERFLOW;
1322 *buflen = total_size;
1324 TRACE("num adapters %u\n", table->numIndexes);
1325 HeapFree(GetProcessHeap(), 0, table);
1326 return ret;
1329 /******************************************************************
1330 * GetBestInterface (IPHLPAPI.@)
1332 * Get the interface, with the best route for the given IP address.
1334 * PARAMS
1335 * dwDestAddr [In] IP address to search the interface for
1336 * pdwBestIfIndex [Out] found best interface
1338 * RETURNS
1339 * Success: NO_ERROR
1340 * Failure: error code from winerror.h
1342 DWORD WINAPI GetBestInterface(IPAddr dwDestAddr, PDWORD pdwBestIfIndex)
1344 struct WS_sockaddr_in sa_in;
1345 memset(&sa_in, 0, sizeof(sa_in));
1346 sa_in.sin_family = AF_INET;
1347 sa_in.sin_addr.S_un.S_addr = dwDestAddr;
1348 return GetBestInterfaceEx((struct WS_sockaddr *)&sa_in, pdwBestIfIndex);
1351 /******************************************************************
1352 * GetBestInterfaceEx (IPHLPAPI.@)
1354 * Get the interface, with the best route for the given IP address.
1356 * PARAMS
1357 * dwDestAddr [In] IP address to search the interface for
1358 * pdwBestIfIndex [Out] found best interface
1360 * RETURNS
1361 * Success: NO_ERROR
1362 * Failure: error code from winerror.h
1364 DWORD WINAPI GetBestInterfaceEx(struct WS_sockaddr *pDestAddr, PDWORD pdwBestIfIndex)
1366 DWORD ret;
1368 TRACE("pDestAddr %p, pdwBestIfIndex %p\n", pDestAddr, pdwBestIfIndex);
1369 if (!pDestAddr || !pdwBestIfIndex)
1370 ret = ERROR_INVALID_PARAMETER;
1371 else {
1372 MIB_IPFORWARDROW ipRow;
1374 if (pDestAddr->sa_family == AF_INET) {
1375 ret = GetBestRoute(((struct WS_sockaddr_in *)pDestAddr)->sin_addr.S_un.S_addr, 0, &ipRow);
1376 if (ret == ERROR_SUCCESS)
1377 *pdwBestIfIndex = ipRow.dwForwardIfIndex;
1378 } else {
1379 FIXME("address family %d not supported\n", pDestAddr->sa_family);
1380 ret = ERROR_NOT_SUPPORTED;
1383 TRACE("returning %d\n", ret);
1384 return ret;
1388 /******************************************************************
1389 * GetBestRoute (IPHLPAPI.@)
1391 * Get the best route for the given IP address.
1393 * PARAMS
1394 * dwDestAddr [In] IP address to search the best route for
1395 * dwSourceAddr [In] optional source IP address
1396 * pBestRoute [Out] found best route
1398 * RETURNS
1399 * Success: NO_ERROR
1400 * Failure: error code from winerror.h
1402 DWORD WINAPI GetBestRoute(DWORD dwDestAddr, DWORD dwSourceAddr, PMIB_IPFORWARDROW pBestRoute)
1404 PMIB_IPFORWARDTABLE table;
1405 DWORD ret;
1407 TRACE("dwDestAddr 0x%08x, dwSourceAddr 0x%08x, pBestRoute %p\n", dwDestAddr,
1408 dwSourceAddr, pBestRoute);
1409 if (!pBestRoute)
1410 return ERROR_INVALID_PARAMETER;
1412 ret = AllocateAndGetIpForwardTableFromStack(&table, FALSE, GetProcessHeap(), 0);
1413 if (!ret) {
1414 DWORD ndx, matchedBits, matchedNdx = table->dwNumEntries;
1416 for (ndx = 0, matchedBits = 0; ndx < table->dwNumEntries; ndx++) {
1417 if (table->table[ndx].u1.ForwardType != MIB_IPROUTE_TYPE_INVALID &&
1418 (dwDestAddr & table->table[ndx].dwForwardMask) ==
1419 (table->table[ndx].dwForwardDest & table->table[ndx].dwForwardMask)) {
1420 DWORD numShifts, mask;
1422 for (numShifts = 0, mask = table->table[ndx].dwForwardMask;
1423 mask && mask & 1; mask >>= 1, numShifts++)
1425 if (numShifts > matchedBits) {
1426 matchedBits = numShifts;
1427 matchedNdx = ndx;
1429 else if (!matchedBits) {
1430 matchedNdx = ndx;
1434 if (matchedNdx < table->dwNumEntries) {
1435 memcpy(pBestRoute, &table->table[matchedNdx], sizeof(MIB_IPFORWARDROW));
1436 ret = ERROR_SUCCESS;
1438 else {
1439 /* No route matches, which can happen if there's no default route. */
1440 ret = ERROR_HOST_UNREACHABLE;
1442 HeapFree(GetProcessHeap(), 0, table);
1444 TRACE("returning %d\n", ret);
1445 return ret;
1449 /******************************************************************
1450 * GetFriendlyIfIndex (IPHLPAPI.@)
1452 * Get a "friendly" version of IfIndex, which is one that doesn't
1453 * have the top byte set. Doesn't validate whether IfIndex is a valid
1454 * adapter index.
1456 * PARAMS
1457 * IfIndex [In] interface index to get the friendly one for
1459 * RETURNS
1460 * A friendly version of IfIndex.
1462 DWORD WINAPI GetFriendlyIfIndex(DWORD IfIndex)
1464 /* windows doesn't validate these, either, just makes sure the top byte is
1465 cleared. I assume my ifenum module never gives an index with the top
1466 byte set. */
1467 TRACE("returning %d\n", IfIndex);
1468 return IfIndex;
1472 /******************************************************************
1473 * GetIfEntry (IPHLPAPI.@)
1475 * Get information about an interface.
1477 * PARAMS
1478 * pIfRow [In/Out] In: dwIndex of MIB_IFROW selects the interface.
1479 * Out: interface information
1481 * RETURNS
1482 * Success: NO_ERROR
1483 * Failure: error code from winerror.h
1485 DWORD WINAPI GetIfEntry(PMIB_IFROW pIfRow)
1487 DWORD ret;
1488 char nameBuf[MAX_ADAPTER_NAME];
1489 char *name;
1491 TRACE("pIfRow %p\n", pIfRow);
1492 if (!pIfRow)
1493 return ERROR_INVALID_PARAMETER;
1495 name = getInterfaceNameByIndex(pIfRow->dwIndex, nameBuf);
1496 if (name) {
1497 ret = getInterfaceEntryByName(name, pIfRow);
1498 if (ret == NO_ERROR)
1499 ret = getInterfaceStatsByName(name, pIfRow);
1501 else
1502 ret = ERROR_INVALID_DATA;
1503 TRACE("returning %d\n", ret);
1504 return ret;
1508 static int IfTableSorter(const void *a, const void *b)
1510 int ret;
1512 if (a && b)
1513 ret = ((const MIB_IFROW*)a)->dwIndex - ((const MIB_IFROW*)b)->dwIndex;
1514 else
1515 ret = 0;
1516 return ret;
1520 /******************************************************************
1521 * GetIfTable (IPHLPAPI.@)
1523 * Get a table of local interfaces.
1525 * PARAMS
1526 * pIfTable [Out] buffer for local interfaces table
1527 * pdwSize [In/Out] length of output buffer
1528 * bOrder [In] whether to sort the table
1530 * RETURNS
1531 * Success: NO_ERROR
1532 * Failure: error code from winerror.h
1534 * NOTES
1535 * If pdwSize is less than required, the function will return
1536 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the required byte
1537 * size.
1538 * If bOrder is true, the returned table will be sorted by interface index.
1540 DWORD WINAPI GetIfTable(PMIB_IFTABLE pIfTable, PULONG pdwSize, BOOL bOrder)
1542 DWORD ret;
1544 TRACE("pIfTable %p, pdwSize %p, bOrder %d\n", pdwSize, pdwSize,
1545 (DWORD)bOrder);
1546 if (!pdwSize)
1547 ret = ERROR_INVALID_PARAMETER;
1548 else {
1549 DWORD numInterfaces = get_interface_indices( FALSE, NULL );
1550 ULONG size = sizeof(MIB_IFTABLE);
1552 if (numInterfaces > 1)
1553 size += (numInterfaces - 1) * sizeof(MIB_IFROW);
1554 if (!pIfTable || *pdwSize < size) {
1555 *pdwSize = size;
1556 ret = ERROR_INSUFFICIENT_BUFFER;
1558 else {
1559 InterfaceIndexTable *table;
1560 get_interface_indices( FALSE, &table );
1562 if (table) {
1563 size = sizeof(MIB_IFTABLE);
1564 if (table->numIndexes > 1)
1565 size += (table->numIndexes - 1) * sizeof(MIB_IFROW);
1566 if (*pdwSize < size) {
1567 *pdwSize = size;
1568 ret = ERROR_INSUFFICIENT_BUFFER;
1570 else {
1571 DWORD ndx;
1573 *pdwSize = size;
1574 pIfTable->dwNumEntries = 0;
1575 for (ndx = 0; ndx < table->numIndexes; ndx++) {
1576 pIfTable->table[ndx].dwIndex = table->indexes[ndx];
1577 GetIfEntry(&pIfTable->table[ndx]);
1578 pIfTable->dwNumEntries++;
1580 if (bOrder)
1581 qsort(pIfTable->table, pIfTable->dwNumEntries, sizeof(MIB_IFROW),
1582 IfTableSorter);
1583 ret = NO_ERROR;
1585 HeapFree(GetProcessHeap(), 0, table);
1587 else
1588 ret = ERROR_OUTOFMEMORY;
1591 TRACE("returning %d\n", ret);
1592 return ret;
1596 /******************************************************************
1597 * GetInterfaceInfo (IPHLPAPI.@)
1599 * Get a list of network interface adapters.
1601 * PARAMS
1602 * pIfTable [Out] buffer for interface adapters
1603 * dwOutBufLen [Out] if buffer is too small, returns required size
1605 * RETURNS
1606 * Success: NO_ERROR
1607 * Failure: error code from winerror.h
1609 * BUGS
1610 * MSDN states this should return non-loopback interfaces only.
1612 DWORD WINAPI GetInterfaceInfo(PIP_INTERFACE_INFO pIfTable, PULONG dwOutBufLen)
1614 DWORD ret;
1616 TRACE("pIfTable %p, dwOutBufLen %p\n", pIfTable, dwOutBufLen);
1617 if (!dwOutBufLen)
1618 ret = ERROR_INVALID_PARAMETER;
1619 else {
1620 DWORD numInterfaces = get_interface_indices( FALSE, NULL );
1621 ULONG size = sizeof(IP_INTERFACE_INFO);
1623 if (numInterfaces > 1)
1624 size += (numInterfaces - 1) * sizeof(IP_ADAPTER_INDEX_MAP);
1625 if (!pIfTable || *dwOutBufLen < size) {
1626 *dwOutBufLen = size;
1627 ret = ERROR_INSUFFICIENT_BUFFER;
1629 else {
1630 InterfaceIndexTable *table;
1631 get_interface_indices( FALSE, &table );
1633 if (table) {
1634 size = sizeof(IP_INTERFACE_INFO);
1635 if (table->numIndexes > 1)
1636 size += (table->numIndexes - 1) * sizeof(IP_ADAPTER_INDEX_MAP);
1637 if (*dwOutBufLen < size) {
1638 *dwOutBufLen = size;
1639 ret = ERROR_INSUFFICIENT_BUFFER;
1641 else {
1642 DWORD ndx;
1643 char nameBuf[MAX_ADAPTER_NAME];
1645 *dwOutBufLen = size;
1646 pIfTable->NumAdapters = 0;
1647 for (ndx = 0; ndx < table->numIndexes; ndx++) {
1648 const char *walker, *name;
1649 WCHAR *assigner;
1651 pIfTable->Adapter[ndx].Index = table->indexes[ndx];
1652 name = getInterfaceNameByIndex(table->indexes[ndx], nameBuf);
1653 for (walker = name, assigner = pIfTable->Adapter[ndx].Name;
1654 walker && *walker &&
1655 assigner - pIfTable->Adapter[ndx].Name < MAX_ADAPTER_NAME - 1;
1656 walker++, assigner++)
1657 *assigner = *walker;
1658 *assigner = 0;
1659 pIfTable->NumAdapters++;
1661 ret = NO_ERROR;
1663 HeapFree(GetProcessHeap(), 0, table);
1665 else
1666 ret = ERROR_OUTOFMEMORY;
1669 TRACE("returning %d\n", ret);
1670 return ret;
1674 /******************************************************************
1675 * GetIpAddrTable (IPHLPAPI.@)
1677 * Get interface-to-IP address mapping table.
1679 * PARAMS
1680 * pIpAddrTable [Out] buffer for mapping table
1681 * pdwSize [In/Out] length of output buffer
1682 * bOrder [In] whether to sort the table
1684 * RETURNS
1685 * Success: NO_ERROR
1686 * Failure: error code from winerror.h
1688 * NOTES
1689 * If pdwSize is less than required, the function will return
1690 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the required byte
1691 * size.
1692 * If bOrder is true, the returned table will be sorted by the next hop and
1693 * an assortment of arbitrary parameters.
1695 DWORD WINAPI GetIpAddrTable(PMIB_IPADDRTABLE pIpAddrTable, PULONG pdwSize, BOOL bOrder)
1697 DWORD ret;
1699 TRACE("pIpAddrTable %p, pdwSize %p, bOrder %d\n", pIpAddrTable, pdwSize,
1700 (DWORD)bOrder);
1701 if (!pdwSize)
1702 ret = ERROR_INVALID_PARAMETER;
1703 else {
1704 PMIB_IPADDRTABLE table;
1706 ret = getIPAddrTable(&table, GetProcessHeap(), 0);
1707 if (ret == NO_ERROR)
1709 ULONG size = FIELD_OFFSET(MIB_IPADDRTABLE, table[table->dwNumEntries]);
1711 if (!pIpAddrTable || *pdwSize < size) {
1712 *pdwSize = size;
1713 ret = ERROR_INSUFFICIENT_BUFFER;
1715 else {
1716 *pdwSize = size;
1717 memcpy(pIpAddrTable, table, size);
1718 if (bOrder)
1719 qsort(pIpAddrTable->table, pIpAddrTable->dwNumEntries,
1720 sizeof(MIB_IPADDRROW), IpAddrTableSorter);
1721 ret = NO_ERROR;
1723 HeapFree(GetProcessHeap(), 0, table);
1726 TRACE("returning %d\n", ret);
1727 return ret;
1731 /******************************************************************
1732 * GetIpForwardTable (IPHLPAPI.@)
1734 * Get the route table.
1736 * PARAMS
1737 * pIpForwardTable [Out] buffer for route table
1738 * pdwSize [In/Out] length of output buffer
1739 * bOrder [In] whether to sort the table
1741 * RETURNS
1742 * Success: NO_ERROR
1743 * Failure: error code from winerror.h
1745 * NOTES
1746 * If pdwSize is less than required, the function will return
1747 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the required byte
1748 * size.
1749 * If bOrder is true, the returned table will be sorted by the next hop and
1750 * an assortment of arbitrary parameters.
1752 DWORD WINAPI GetIpForwardTable(PMIB_IPFORWARDTABLE pIpForwardTable, PULONG pdwSize, BOOL bOrder)
1754 DWORD ret;
1755 PMIB_IPFORWARDTABLE table;
1757 TRACE("pIpForwardTable %p, pdwSize %p, bOrder %d\n", pIpForwardTable, pdwSize, bOrder);
1759 if (!pdwSize) return ERROR_INVALID_PARAMETER;
1761 ret = AllocateAndGetIpForwardTableFromStack(&table, bOrder, GetProcessHeap(), 0);
1762 if (!ret) {
1763 DWORD size = FIELD_OFFSET( MIB_IPFORWARDTABLE, table[table->dwNumEntries] );
1764 if (!pIpForwardTable || *pdwSize < size) {
1765 *pdwSize = size;
1766 ret = ERROR_INSUFFICIENT_BUFFER;
1768 else {
1769 *pdwSize = size;
1770 memcpy(pIpForwardTable, table, size);
1772 HeapFree(GetProcessHeap(), 0, table);
1774 TRACE("returning %d\n", ret);
1775 return ret;
1779 /******************************************************************
1780 * GetIpNetTable (IPHLPAPI.@)
1782 * Get the IP-to-physical address mapping table.
1784 * PARAMS
1785 * pIpNetTable [Out] buffer for mapping table
1786 * pdwSize [In/Out] length of output buffer
1787 * bOrder [In] whether to sort the table
1789 * RETURNS
1790 * Success: NO_ERROR
1791 * Failure: error code from winerror.h
1793 * NOTES
1794 * If pdwSize is less than required, the function will return
1795 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the required byte
1796 * size.
1797 * If bOrder is true, the returned table will be sorted by IP address.
1799 DWORD WINAPI GetIpNetTable(PMIB_IPNETTABLE pIpNetTable, PULONG pdwSize, BOOL bOrder)
1801 DWORD ret;
1802 PMIB_IPNETTABLE table;
1804 TRACE("pIpNetTable %p, pdwSize %p, bOrder %d\n", pIpNetTable, pdwSize, bOrder);
1806 if (!pdwSize) return ERROR_INVALID_PARAMETER;
1808 ret = AllocateAndGetIpNetTableFromStack( &table, bOrder, GetProcessHeap(), 0 );
1809 if (!ret) {
1810 DWORD size = FIELD_OFFSET( MIB_IPNETTABLE, table[table->dwNumEntries] );
1811 if (!pIpNetTable || *pdwSize < size) {
1812 *pdwSize = size;
1813 ret = ERROR_INSUFFICIENT_BUFFER;
1815 else {
1816 *pdwSize = size;
1817 memcpy(pIpNetTable, table, size);
1819 HeapFree(GetProcessHeap(), 0, table);
1821 TRACE("returning %d\n", ret);
1822 return ret;
1825 /* Gets the DNS server list into the list beginning at list. Assumes that
1826 * a single server address may be placed at list if *len is at least
1827 * sizeof(IP_ADDR_STRING) long. Otherwise, list->Next is set to firstDynamic,
1828 * and assumes that all remaining DNS servers are contiguously located
1829 * beginning at firstDynamic. On input, *len is assumed to be the total number
1830 * of bytes available for all DNS servers, and is ignored if list is NULL.
1831 * On return, *len is set to the total number of bytes required for all DNS
1832 * servers.
1833 * Returns ERROR_BUFFER_OVERFLOW if *len is insufficient,
1834 * ERROR_SUCCESS otherwise.
1836 static DWORD get_dns_server_list(PIP_ADDR_STRING list,
1837 PIP_ADDR_STRING firstDynamic, DWORD *len)
1839 DWORD size;
1840 int num = get_dns_servers( NULL, 0, TRUE );
1842 size = num * sizeof(IP_ADDR_STRING);
1843 if (!list || *len < size) {
1844 *len = size;
1845 return ERROR_BUFFER_OVERFLOW;
1847 *len = size;
1848 if (num > 0) {
1849 PIP_ADDR_STRING ptr;
1850 int i;
1851 SOCKADDR_STORAGE *addr = HeapAlloc( GetProcessHeap(), 0, num * sizeof(SOCKADDR_STORAGE) );
1853 get_dns_servers( addr, num, TRUE );
1855 for (i = 0, ptr = list; i < num; i++, ptr = ptr->Next) {
1856 toIPAddressString(((struct sockaddr_in *)(addr + i))->sin_addr.s_addr,
1857 ptr->IpAddress.String);
1858 if (i == num - 1)
1859 ptr->Next = NULL;
1860 else if (i == 0)
1861 ptr->Next = firstDynamic;
1862 else
1863 ptr->Next = (PIP_ADDR_STRING)((PBYTE)ptr + sizeof(IP_ADDR_STRING));
1865 HeapFree( GetProcessHeap(), 0, addr );
1867 return ERROR_SUCCESS;
1870 /******************************************************************
1871 * GetNetworkParams (IPHLPAPI.@)
1873 * Get the network parameters for the local computer.
1875 * PARAMS
1876 * pFixedInfo [Out] buffer for network parameters
1877 * pOutBufLen [In/Out] length of output buffer
1879 * RETURNS
1880 * Success: NO_ERROR
1881 * Failure: error code from winerror.h
1883 * NOTES
1884 * If pOutBufLen is less than required, the function will return
1885 * ERROR_INSUFFICIENT_BUFFER, and pOutBufLen will be set to the required byte
1886 * size.
1888 DWORD WINAPI GetNetworkParams(PFIXED_INFO pFixedInfo, PULONG pOutBufLen)
1890 DWORD ret, size, serverListSize;
1891 LONG regReturn;
1892 HKEY hKey;
1894 TRACE("pFixedInfo %p, pOutBufLen %p\n", pFixedInfo, pOutBufLen);
1895 if (!pOutBufLen)
1896 return ERROR_INVALID_PARAMETER;
1898 get_dns_server_list(NULL, NULL, &serverListSize);
1899 size = sizeof(FIXED_INFO) + serverListSize - sizeof(IP_ADDR_STRING);
1900 if (!pFixedInfo || *pOutBufLen < size) {
1901 *pOutBufLen = size;
1902 return ERROR_BUFFER_OVERFLOW;
1905 memset(pFixedInfo, 0, size);
1906 size = sizeof(pFixedInfo->HostName);
1907 GetComputerNameExA(ComputerNameDnsHostname, pFixedInfo->HostName, &size);
1908 size = sizeof(pFixedInfo->DomainName);
1909 GetComputerNameExA(ComputerNameDnsDomain, pFixedInfo->DomainName, &size);
1910 get_dns_server_list(&pFixedInfo->DnsServerList,
1911 (PIP_ADDR_STRING)((BYTE *)pFixedInfo + sizeof(FIXED_INFO)),
1912 &serverListSize);
1913 /* Assume the first DNS server in the list is the "current" DNS server: */
1914 pFixedInfo->CurrentDnsServer = &pFixedInfo->DnsServerList;
1915 pFixedInfo->NodeType = HYBRID_NODETYPE;
1916 regReturn = RegOpenKeyExA(HKEY_LOCAL_MACHINE,
1917 "SYSTEM\\CurrentControlSet\\Services\\VxD\\MSTCP", 0, KEY_READ, &hKey);
1918 if (regReturn != ERROR_SUCCESS)
1919 regReturn = RegOpenKeyExA(HKEY_LOCAL_MACHINE,
1920 "SYSTEM\\CurrentControlSet\\Services\\NetBT\\Parameters", 0, KEY_READ,
1921 &hKey);
1922 if (regReturn == ERROR_SUCCESS)
1924 DWORD size = sizeof(pFixedInfo->ScopeId);
1926 RegQueryValueExA(hKey, "ScopeID", NULL, NULL, (LPBYTE)pFixedInfo->ScopeId, &size);
1927 RegCloseKey(hKey);
1930 /* FIXME: can check whether routing's enabled in /proc/sys/net/ipv4/ip_forward
1931 I suppose could also check for a listener on port 53 to set EnableDns */
1932 ret = NO_ERROR;
1933 TRACE("returning %d\n", ret);
1934 return ret;
1938 /******************************************************************
1939 * GetNumberOfInterfaces (IPHLPAPI.@)
1941 * Get the number of interfaces.
1943 * PARAMS
1944 * pdwNumIf [Out] number of interfaces
1946 * RETURNS
1947 * NO_ERROR on success, ERROR_INVALID_PARAMETER if pdwNumIf is NULL.
1949 DWORD WINAPI GetNumberOfInterfaces(PDWORD pdwNumIf)
1951 DWORD ret;
1953 TRACE("pdwNumIf %p\n", pdwNumIf);
1954 if (!pdwNumIf)
1955 ret = ERROR_INVALID_PARAMETER;
1956 else {
1957 *pdwNumIf = get_interface_indices( FALSE, NULL );
1958 ret = NO_ERROR;
1960 TRACE("returning %d\n", ret);
1961 return ret;
1965 /******************************************************************
1966 * GetPerAdapterInfo (IPHLPAPI.@)
1968 * Get information about an adapter corresponding to an interface.
1970 * PARAMS
1971 * IfIndex [In] interface info
1972 * pPerAdapterInfo [Out] buffer for per adapter info
1973 * pOutBufLen [In/Out] length of output buffer
1975 * RETURNS
1976 * Success: NO_ERROR
1977 * Failure: error code from winerror.h
1979 DWORD WINAPI GetPerAdapterInfo(ULONG IfIndex, PIP_PER_ADAPTER_INFO pPerAdapterInfo, PULONG pOutBufLen)
1981 ULONG bytesNeeded = sizeof(IP_PER_ADAPTER_INFO), serverListSize = 0;
1982 DWORD ret = NO_ERROR;
1984 TRACE("(IfIndex %d, pPerAdapterInfo %p, pOutBufLen %p)\n", IfIndex, pPerAdapterInfo, pOutBufLen);
1986 if (!pOutBufLen) return ERROR_INVALID_PARAMETER;
1988 if (!isIfIndexLoopback(IfIndex)) {
1989 get_dns_server_list(NULL, NULL, &serverListSize);
1990 if (serverListSize > sizeof(IP_ADDR_STRING))
1991 bytesNeeded += serverListSize - sizeof(IP_ADDR_STRING);
1993 if (!pPerAdapterInfo || *pOutBufLen < bytesNeeded)
1995 *pOutBufLen = bytesNeeded;
1996 return ERROR_BUFFER_OVERFLOW;
1999 memset(pPerAdapterInfo, 0, bytesNeeded);
2000 if (!isIfIndexLoopback(IfIndex)) {
2001 ret = get_dns_server_list(&pPerAdapterInfo->DnsServerList,
2002 (PIP_ADDR_STRING)((PBYTE)pPerAdapterInfo + sizeof(IP_PER_ADAPTER_INFO)),
2003 &serverListSize);
2004 /* Assume the first DNS server in the list is the "current" DNS server: */
2005 pPerAdapterInfo->CurrentDnsServer = &pPerAdapterInfo->DnsServerList;
2007 return ret;
2011 /******************************************************************
2012 * GetRTTAndHopCount (IPHLPAPI.@)
2014 * Get round-trip time (RTT) and hop count.
2016 * PARAMS
2018 * DestIpAddress [In] destination address to get the info for
2019 * HopCount [Out] retrieved hop count
2020 * MaxHops [In] maximum hops to search for the destination
2021 * RTT [Out] RTT in milliseconds
2023 * RETURNS
2024 * Success: TRUE
2025 * Failure: FALSE
2027 * FIXME
2028 * Stub, returns FALSE.
2030 BOOL WINAPI GetRTTAndHopCount(IPAddr DestIpAddress, PULONG HopCount, ULONG MaxHops, PULONG RTT)
2032 FIXME("(DestIpAddress 0x%08x, HopCount %p, MaxHops %d, RTT %p): stub\n",
2033 DestIpAddress, HopCount, MaxHops, RTT);
2034 return FALSE;
2038 /******************************************************************
2039 * GetTcpTable (IPHLPAPI.@)
2041 * Get the table of active TCP connections.
2043 * PARAMS
2044 * pTcpTable [Out] buffer for TCP connections table
2045 * pdwSize [In/Out] length of output buffer
2046 * bOrder [In] whether to order the table
2048 * RETURNS
2049 * Success: NO_ERROR
2050 * Failure: error code from winerror.h
2052 * NOTES
2053 * If pdwSize is less than required, the function will return
2054 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to
2055 * the required byte size.
2056 * If bOrder is true, the returned table will be sorted, first by
2057 * local address and port number, then by remote address and port
2058 * number.
2060 DWORD WINAPI GetTcpTable(PMIB_TCPTABLE pTcpTable, PDWORD pdwSize, BOOL bOrder)
2062 TRACE("pTcpTable %p, pdwSize %p, bOrder %d\n", pTcpTable, pdwSize, bOrder);
2063 return GetExtendedTcpTable(pTcpTable, pdwSize, bOrder, AF_INET, TCP_TABLE_BASIC_ALL, 0);
2066 /******************************************************************
2067 * GetExtendedTcpTable (IPHLPAPI.@)
2069 DWORD WINAPI GetExtendedTcpTable(PVOID pTcpTable, PDWORD pdwSize, BOOL bOrder,
2070 ULONG ulAf, TCP_TABLE_CLASS TableClass, ULONG Reserved)
2072 DWORD ret, size;
2073 void *table;
2075 TRACE("pTcpTable %p, pdwSize %p, bOrder %d, ulAf %u, TableClass %u, Reserved %u\n",
2076 pTcpTable, pdwSize, bOrder, ulAf, TableClass, Reserved);
2078 if (!pdwSize) return ERROR_INVALID_PARAMETER;
2080 if (ulAf != AF_INET)
2082 FIXME("ulAf = %u not supported\n", ulAf);
2083 return ERROR_NOT_SUPPORTED;
2085 if (TableClass >= TCP_TABLE_OWNER_MODULE_LISTENER)
2086 FIXME("module classes not fully supported\n");
2088 if ((ret = build_tcp_table(TableClass, &table, bOrder, GetProcessHeap(), 0, &size)))
2089 return ret;
2091 if (!pTcpTable || *pdwSize < size)
2093 *pdwSize = size;
2094 ret = ERROR_INSUFFICIENT_BUFFER;
2096 else
2098 *pdwSize = size;
2099 memcpy(pTcpTable, table, size);
2101 HeapFree(GetProcessHeap(), 0, table);
2102 return ret;
2105 /******************************************************************
2106 * GetUdpTable (IPHLPAPI.@)
2108 * Get a table of active UDP connections.
2110 * PARAMS
2111 * pUdpTable [Out] buffer for UDP connections table
2112 * pdwSize [In/Out] length of output buffer
2113 * bOrder [In] whether to order the table
2115 * RETURNS
2116 * Success: NO_ERROR
2117 * Failure: error code from winerror.h
2119 * NOTES
2120 * If pdwSize is less than required, the function will return
2121 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the
2122 * required byte size.
2123 * If bOrder is true, the returned table will be sorted, first by
2124 * local address, then by local port number.
2126 DWORD WINAPI GetUdpTable(PMIB_UDPTABLE pUdpTable, PDWORD pdwSize, BOOL bOrder)
2128 return GetExtendedUdpTable(pUdpTable, pdwSize, bOrder, AF_INET, UDP_TABLE_BASIC, 0);
2131 /******************************************************************
2132 * GetExtendedUdpTable (IPHLPAPI.@)
2134 DWORD WINAPI GetExtendedUdpTable(PVOID pUdpTable, PDWORD pdwSize, BOOL bOrder,
2135 ULONG ulAf, UDP_TABLE_CLASS TableClass, ULONG Reserved)
2137 DWORD ret, size;
2138 void *table;
2140 TRACE("pUdpTable %p, pdwSize %p, bOrder %d, ulAf %u, TableClass %u, Reserved %u\n",
2141 pUdpTable, pdwSize, bOrder, ulAf, TableClass, Reserved);
2143 if (!pdwSize) return ERROR_INVALID_PARAMETER;
2145 if (ulAf != AF_INET)
2147 FIXME("ulAf = %u not supported\n", ulAf);
2148 return ERROR_NOT_SUPPORTED;
2150 if (TableClass == UDP_TABLE_OWNER_MODULE)
2151 FIXME("UDP_TABLE_OWNER_MODULE not fully supported\n");
2153 if ((ret = build_udp_table(TableClass, &table, bOrder, GetProcessHeap(), 0, &size)))
2154 return ret;
2156 if (!pUdpTable || *pdwSize < size)
2158 *pdwSize = size;
2159 ret = ERROR_INSUFFICIENT_BUFFER;
2161 else
2163 *pdwSize = size;
2164 memcpy(pUdpTable, table, size);
2166 HeapFree(GetProcessHeap(), 0, table);
2167 return ret;
2170 /******************************************************************
2171 * GetUniDirectionalAdapterInfo (IPHLPAPI.@)
2173 * This is a Win98-only function to get information on "unidirectional"
2174 * adapters. Since this is pretty nonsensical in other contexts, it
2175 * never returns anything.
2177 * PARAMS
2178 * pIPIfInfo [Out] buffer for adapter infos
2179 * dwOutBufLen [Out] length of the output buffer
2181 * RETURNS
2182 * Success: NO_ERROR
2183 * Failure: error code from winerror.h
2185 * FIXME
2186 * Stub, returns ERROR_NOT_SUPPORTED.
2188 DWORD WINAPI GetUniDirectionalAdapterInfo(PIP_UNIDIRECTIONAL_ADAPTER_ADDRESS pIPIfInfo, PULONG dwOutBufLen)
2190 TRACE("pIPIfInfo %p, dwOutBufLen %p\n", pIPIfInfo, dwOutBufLen);
2191 /* a unidirectional adapter?? not bloody likely! */
2192 return ERROR_NOT_SUPPORTED;
2196 /******************************************************************
2197 * IpReleaseAddress (IPHLPAPI.@)
2199 * Release an IP obtained through DHCP,
2201 * PARAMS
2202 * AdapterInfo [In] adapter to release IP address
2204 * RETURNS
2205 * Success: NO_ERROR
2206 * Failure: error code from winerror.h
2208 * NOTES
2209 * Since GetAdaptersInfo never returns adapters that have DHCP enabled,
2210 * this function does nothing.
2212 * FIXME
2213 * Stub, returns ERROR_NOT_SUPPORTED.
2215 DWORD WINAPI IpReleaseAddress(PIP_ADAPTER_INDEX_MAP AdapterInfo)
2217 FIXME("Stub AdapterInfo %p\n", AdapterInfo);
2218 return ERROR_NOT_SUPPORTED;
2222 /******************************************************************
2223 * IpRenewAddress (IPHLPAPI.@)
2225 * Renew an IP obtained through DHCP.
2227 * PARAMS
2228 * AdapterInfo [In] adapter to renew IP address
2230 * RETURNS
2231 * Success: NO_ERROR
2232 * Failure: error code from winerror.h
2234 * NOTES
2235 * Since GetAdaptersInfo never returns adapters that have DHCP enabled,
2236 * this function does nothing.
2238 * FIXME
2239 * Stub, returns ERROR_NOT_SUPPORTED.
2241 DWORD WINAPI IpRenewAddress(PIP_ADAPTER_INDEX_MAP AdapterInfo)
2243 FIXME("Stub AdapterInfo %p\n", AdapterInfo);
2244 return ERROR_NOT_SUPPORTED;
2248 /******************************************************************
2249 * NotifyAddrChange (IPHLPAPI.@)
2251 * Notify caller whenever the ip-interface map is changed.
2253 * PARAMS
2254 * Handle [Out] handle usable in asynchronous notification
2255 * overlapped [In] overlapped structure that notifies the caller
2257 * RETURNS
2258 * Success: NO_ERROR
2259 * Failure: error code from winerror.h
2261 * FIXME
2262 * Stub, returns ERROR_NOT_SUPPORTED.
2264 DWORD WINAPI NotifyAddrChange(PHANDLE Handle, LPOVERLAPPED overlapped)
2266 FIXME("(Handle %p, overlapped %p): stub\n", Handle, overlapped);
2267 if (Handle) *Handle = INVALID_HANDLE_VALUE;
2268 if (overlapped) ((IO_STATUS_BLOCK *) overlapped)->u.Status = STATUS_PENDING;
2269 return ERROR_IO_PENDING;
2273 /******************************************************************
2274 * NotifyRouteChange (IPHLPAPI.@)
2276 * Notify caller whenever the ip routing table is changed.
2278 * PARAMS
2279 * Handle [Out] handle usable in asynchronous notification
2280 * overlapped [In] overlapped structure that notifies the caller
2282 * RETURNS
2283 * Success: NO_ERROR
2284 * Failure: error code from winerror.h
2286 * FIXME
2287 * Stub, returns ERROR_NOT_SUPPORTED.
2289 DWORD WINAPI NotifyRouteChange(PHANDLE Handle, LPOVERLAPPED overlapped)
2291 FIXME("(Handle %p, overlapped %p): stub\n", Handle, overlapped);
2292 return ERROR_NOT_SUPPORTED;
2296 /******************************************************************
2297 * SendARP (IPHLPAPI.@)
2299 * Send an ARP request.
2301 * PARAMS
2302 * DestIP [In] attempt to obtain this IP
2303 * SrcIP [In] optional sender IP address
2304 * pMacAddr [Out] buffer for the mac address
2305 * PhyAddrLen [In/Out] length of the output buffer
2307 * RETURNS
2308 * Success: NO_ERROR
2309 * Failure: error code from winerror.h
2311 * FIXME
2312 * Stub, returns ERROR_NOT_SUPPORTED.
2314 DWORD WINAPI SendARP(IPAddr DestIP, IPAddr SrcIP, PULONG pMacAddr, PULONG PhyAddrLen)
2316 FIXME("(DestIP 0x%08x, SrcIP 0x%08x, pMacAddr %p, PhyAddrLen %p): stub\n",
2317 DestIP, SrcIP, pMacAddr, PhyAddrLen);
2318 return ERROR_NOT_SUPPORTED;
2322 /******************************************************************
2323 * SetIfEntry (IPHLPAPI.@)
2325 * Set the administrative status of an interface.
2327 * PARAMS
2328 * pIfRow [In] dwAdminStatus member specifies the new status.
2330 * RETURNS
2331 * Success: NO_ERROR
2332 * Failure: error code from winerror.h
2334 * FIXME
2335 * Stub, returns ERROR_NOT_SUPPORTED.
2337 DWORD WINAPI SetIfEntry(PMIB_IFROW pIfRow)
2339 FIXME("(pIfRow %p): stub\n", pIfRow);
2340 /* this is supposed to set an interface administratively up or down.
2341 Could do SIOCSIFFLAGS and set/clear IFF_UP, but, not sure I want to, and
2342 this sort of down is indistinguishable from other sorts of down (e.g. no
2343 link). */
2344 return ERROR_NOT_SUPPORTED;
2348 /******************************************************************
2349 * SetIpForwardEntry (IPHLPAPI.@)
2351 * Modify an existing route.
2353 * PARAMS
2354 * pRoute [In] route with the new information
2356 * RETURNS
2357 * Success: NO_ERROR
2358 * Failure: error code from winerror.h
2360 * FIXME
2361 * Stub, returns NO_ERROR.
2363 DWORD WINAPI SetIpForwardEntry(PMIB_IPFORWARDROW pRoute)
2365 FIXME("(pRoute %p): stub\n", pRoute);
2366 /* this is to add a route entry, how's it distinguishable from
2367 CreateIpForwardEntry?
2368 could use SIOCADDRT, not sure I want to */
2369 return 0;
2373 /******************************************************************
2374 * SetIpNetEntry (IPHLPAPI.@)
2376 * Modify an existing ARP entry.
2378 * PARAMS
2379 * pArpEntry [In] ARP entry with the new information
2381 * RETURNS
2382 * Success: NO_ERROR
2383 * Failure: error code from winerror.h
2385 * FIXME
2386 * Stub, returns NO_ERROR.
2388 DWORD WINAPI SetIpNetEntry(PMIB_IPNETROW pArpEntry)
2390 FIXME("(pArpEntry %p): stub\n", pArpEntry);
2391 /* same as CreateIpNetEntry here, could use SIOCSARP, not sure I want to */
2392 return 0;
2396 /******************************************************************
2397 * SetIpStatistics (IPHLPAPI.@)
2399 * Toggle IP forwarding and det the default TTL value.
2401 * PARAMS
2402 * pIpStats [In] IP statistics with the new information
2404 * RETURNS
2405 * Success: NO_ERROR
2406 * Failure: error code from winerror.h
2408 * FIXME
2409 * Stub, returns NO_ERROR.
2411 DWORD WINAPI SetIpStatistics(PMIB_IPSTATS pIpStats)
2413 FIXME("(pIpStats %p): stub\n", pIpStats);
2414 return 0;
2418 /******************************************************************
2419 * SetIpTTL (IPHLPAPI.@)
2421 * Set the default TTL value.
2423 * PARAMS
2424 * nTTL [In] new TTL value
2426 * RETURNS
2427 * Success: NO_ERROR
2428 * Failure: error code from winerror.h
2430 * FIXME
2431 * Stub, returns NO_ERROR.
2433 DWORD WINAPI SetIpTTL(UINT nTTL)
2435 FIXME("(nTTL %d): stub\n", nTTL);
2436 /* could echo nTTL > /proc/net/sys/net/ipv4/ip_default_ttl, not sure I
2437 want to. Could map EACCESS to ERROR_ACCESS_DENIED, I suppose */
2438 return 0;
2442 /******************************************************************
2443 * SetTcpEntry (IPHLPAPI.@)
2445 * Set the state of a TCP connection.
2447 * PARAMS
2448 * pTcpRow [In] specifies connection with new state
2450 * RETURNS
2451 * Success: NO_ERROR
2452 * Failure: error code from winerror.h
2454 * FIXME
2455 * Stub, returns NO_ERROR.
2457 DWORD WINAPI SetTcpEntry(PMIB_TCPROW pTcpRow)
2459 FIXME("(pTcpRow %p): stub\n", pTcpRow);
2460 return 0;
2464 /******************************************************************
2465 * UnenableRouter (IPHLPAPI.@)
2467 * Decrement the IP-forwarding reference count. Turn off IP-forwarding
2468 * if it reaches zero.
2470 * PARAMS
2471 * pOverlapped [In/Out] should be the same as in EnableRouter()
2472 * lpdwEnableCount [Out] optional, receives reference count
2474 * RETURNS
2475 * Success: NO_ERROR
2476 * Failure: error code from winerror.h
2478 * FIXME
2479 * Stub, returns ERROR_NOT_SUPPORTED.
2481 DWORD WINAPI UnenableRouter(OVERLAPPED * pOverlapped, LPDWORD lpdwEnableCount)
2483 FIXME("(pOverlapped %p, lpdwEnableCount %p): stub\n", pOverlapped,
2484 lpdwEnableCount);
2485 /* could echo "0" > /proc/net/sys/net/ipv4/ip_forward, not sure I want to
2486 could map EACCESS to ERROR_ACCESS_DENIED, I suppose
2488 return ERROR_NOT_SUPPORTED;
2491 /******************************************************************
2492 * PfCreateInterface (IPHLPAPI.@)
2494 DWORD WINAPI PfCreateInterface(DWORD dwName, PFFORWARD_ACTION inAction, PFFORWARD_ACTION outAction,
2495 BOOL bUseLog, BOOL bMustBeUnique, INTERFACE_HANDLE *ppInterface)
2497 FIXME("(%d %d %d %x %x %p) stub\n", dwName, inAction, outAction, bUseLog, bMustBeUnique, ppInterface);
2498 return ERROR_CALL_NOT_IMPLEMENTED;
2501 /******************************************************************
2502 * GetTcpTable2 (IPHLPAPI.@)
2504 ULONG WINAPI GetTcpTable2(PMIB_TCPTABLE2 table, PULONG size, BOOL order)
2506 FIXME("pTcpTable2 %p, pdwSize %p, bOrder %d: stub\n", table, size, order);
2507 return ERROR_NOT_SUPPORTED;
2510 /******************************************************************
2511 * GetTcp6Table (IPHLPAPI.@)
2513 ULONG WINAPI GetTcp6Table(PMIB_TCP6TABLE table, PULONG size, BOOL order)
2515 FIXME("pTcp6Table %p, size %p, order %d: stub\n", table, size, order);
2516 return ERROR_NOT_SUPPORTED;
2519 /******************************************************************
2520 * GetTcp6Table2 (IPHLPAPI.@)
2522 ULONG WINAPI GetTcp6Table2(PMIB_TCP6TABLE2 table, PULONG size, BOOL order)
2524 FIXME("pTcp6Table2 %p, size %p, order %d: stub\n", table, size, order);
2525 return ERROR_NOT_SUPPORTED;