push 22b3e00525a9a3743634eb8f21ffe1bf98bf885e
[wine/hacks.git] / dlls / iphlpapi / iphlpapi_main.c
blob8490c67990c23443f70677816b576b98c2aca3d4
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 #include "windef.h"
46 #include "winbase.h"
47 #include "winreg.h"
48 #define USE_WS_PREFIX
49 #include "winsock2.h"
50 #include "iphlpapi.h"
51 #include "ifenum.h"
52 #include "ipstats.h"
53 #include "ipifcons.h"
55 #include "wine/debug.h"
57 WINE_DEFAULT_DEBUG_CHANNEL(iphlpapi);
59 #ifndef IF_NAMESIZE
60 #define IF_NAMESIZE 16
61 #endif
63 #ifndef INADDR_NONE
64 #define INADDR_NONE ~0UL
65 #endif
67 static int resolver_initialised;
69 /* call res_init() just once because of a bug in Mac OS X 10.4 */
70 static void initialise_resolver(void)
72 if (!resolver_initialised)
74 res_init();
75 resolver_initialised = 1;
79 BOOL WINAPI DllMain (HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
81 switch (fdwReason) {
82 case DLL_PROCESS_ATTACH:
83 DisableThreadLibraryCalls( hinstDLL );
84 break;
86 case DLL_PROCESS_DETACH:
87 break;
89 return TRUE;
92 /******************************************************************
93 * AddIPAddress (IPHLPAPI.@)
95 * Add an IP address to an adapter.
97 * PARAMS
98 * Address [In] IP address to add to the adapter
99 * IpMask [In] subnet mask for the IP address
100 * IfIndex [In] adapter index to add the address
101 * NTEContext [Out] Net Table Entry (NTE) context for the IP address
102 * NTEInstance [Out] NTE instance for the IP address
104 * RETURNS
105 * Success: NO_ERROR
106 * Failure: error code from winerror.h
108 * FIXME
109 * Stub. Currently returns ERROR_NOT_SUPPORTED.
111 DWORD WINAPI AddIPAddress(IPAddr Address, IPMask IpMask, DWORD IfIndex, PULONG NTEContext, PULONG NTEInstance)
113 FIXME(":stub\n");
114 return ERROR_NOT_SUPPORTED;
118 /******************************************************************
119 * AllocateAndGetIfTableFromStack (IPHLPAPI.@)
121 * Get table of local interfaces.
122 * Like GetIfTable(), but allocate the returned table from heap.
124 * PARAMS
125 * ppIfTable [Out] pointer into which the MIB_IFTABLE is
126 * allocated and returned.
127 * bOrder [In] whether to sort the table
128 * heap [In] heap from which the table is allocated
129 * flags [In] flags to HeapAlloc
131 * RETURNS
132 * ERROR_INVALID_PARAMETER if ppIfTable is NULL, whatever
133 * GetIfTable() returns otherwise.
135 DWORD WINAPI AllocateAndGetIfTableFromStack(PMIB_IFTABLE *ppIfTable,
136 BOOL bOrder, HANDLE heap, DWORD flags)
138 DWORD ret;
140 TRACE("ppIfTable %p, bOrder %d, heap %p, flags 0x%08x\n", ppIfTable,
141 bOrder, heap, flags);
142 if (!ppIfTable)
143 ret = ERROR_INVALID_PARAMETER;
144 else {
145 DWORD dwSize = 0;
147 ret = GetIfTable(*ppIfTable, &dwSize, bOrder);
148 if (ret == ERROR_INSUFFICIENT_BUFFER) {
149 *ppIfTable = HeapAlloc(heap, flags, dwSize);
150 ret = GetIfTable(*ppIfTable, &dwSize, bOrder);
153 TRACE("returning %d\n", ret);
154 return ret;
158 static int IpAddrTableSorter(const void *a, const void *b)
160 int ret;
162 if (a && b)
163 ret = ((const MIB_IPADDRROW*)a)->dwAddr - ((const MIB_IPADDRROW*)b)->dwAddr;
164 else
165 ret = 0;
166 return ret;
170 /******************************************************************
171 * AllocateAndGetIpAddrTableFromStack (IPHLPAPI.@)
173 * Get interface-to-IP address mapping table.
174 * Like GetIpAddrTable(), but allocate the returned table from heap.
176 * PARAMS
177 * ppIpAddrTable [Out] pointer into which the MIB_IPADDRTABLE is
178 * allocated and returned.
179 * bOrder [In] whether to sort the table
180 * heap [In] heap from which the table is allocated
181 * flags [In] flags to HeapAlloc
183 * RETURNS
184 * ERROR_INVALID_PARAMETER if ppIpAddrTable is NULL, other error codes on
185 * failure, NO_ERROR on success.
187 DWORD WINAPI AllocateAndGetIpAddrTableFromStack(PMIB_IPADDRTABLE *ppIpAddrTable,
188 BOOL bOrder, HANDLE heap, DWORD flags)
190 DWORD ret;
192 TRACE("ppIpAddrTable %p, bOrder %d, heap %p, flags 0x%08x\n",
193 ppIpAddrTable, bOrder, heap, flags);
194 ret = getIPAddrTable(ppIpAddrTable, heap, flags);
195 if (!ret && bOrder)
196 qsort((*ppIpAddrTable)->table, (*ppIpAddrTable)->dwNumEntries,
197 sizeof(MIB_IPADDRROW), IpAddrTableSorter);
198 TRACE("returning %d\n", ret);
199 return ret;
203 /******************************************************************
204 * CreateIpForwardEntry (IPHLPAPI.@)
206 * Create a route in the local computer's IP table.
208 * PARAMS
209 * pRoute [In] new route information
211 * RETURNS
212 * Success: NO_ERROR
213 * Failure: error code from winerror.h
215 * FIXME
216 * Stub, always returns NO_ERROR.
218 DWORD WINAPI CreateIpForwardEntry(PMIB_IPFORWARDROW pRoute)
220 FIXME("(pRoute %p): stub\n", pRoute);
221 /* could use SIOCADDRT, not sure I want to */
222 return 0;
226 /******************************************************************
227 * CreateIpNetEntry (IPHLPAPI.@)
229 * Create entry in the ARP table.
231 * PARAMS
232 * pArpEntry [In] new ARP entry
234 * RETURNS
235 * Success: NO_ERROR
236 * Failure: error code from winerror.h
238 * FIXME
239 * Stub, always returns NO_ERROR.
241 DWORD WINAPI CreateIpNetEntry(PMIB_IPNETROW pArpEntry)
243 FIXME("(pArpEntry %p)\n", pArpEntry);
244 /* could use SIOCSARP on systems that support it, not sure I want to */
245 return 0;
249 /******************************************************************
250 * CreateProxyArpEntry (IPHLPAPI.@)
252 * Create a Proxy ARP (PARP) entry for an IP address.
254 * PARAMS
255 * dwAddress [In] IP address for which this computer acts as a proxy.
256 * dwMask [In] subnet mask for dwAddress
257 * dwIfIndex [In] interface index
259 * RETURNS
260 * Success: NO_ERROR
261 * Failure: error code from winerror.h
263 * FIXME
264 * Stub, returns ERROR_NOT_SUPPORTED.
266 DWORD WINAPI CreateProxyArpEntry(DWORD dwAddress, DWORD dwMask, DWORD dwIfIndex)
268 FIXME("(dwAddress 0x%08x, dwMask 0x%08x, dwIfIndex 0x%08x): stub\n",
269 dwAddress, dwMask, dwIfIndex);
270 return ERROR_NOT_SUPPORTED;
274 /******************************************************************
275 * DeleteIPAddress (IPHLPAPI.@)
277 * Delete an IP address added with AddIPAddress().
279 * PARAMS
280 * NTEContext [In] NTE context from AddIPAddress();
282 * RETURNS
283 * Success: NO_ERROR
284 * Failure: error code from winerror.h
286 * FIXME
287 * Stub, returns ERROR_NOT_SUPPORTED.
289 DWORD WINAPI DeleteIPAddress(ULONG NTEContext)
291 FIXME("(NTEContext %d): stub\n", NTEContext);
292 return ERROR_NOT_SUPPORTED;
296 /******************************************************************
297 * DeleteIpForwardEntry (IPHLPAPI.@)
299 * Delete a route.
301 * PARAMS
302 * pRoute [In] route to delete
304 * RETURNS
305 * Success: NO_ERROR
306 * Failure: error code from winerror.h
308 * FIXME
309 * Stub, returns NO_ERROR.
311 DWORD WINAPI DeleteIpForwardEntry(PMIB_IPFORWARDROW pRoute)
313 FIXME("(pRoute %p): stub\n", pRoute);
314 /* could use SIOCDELRT, not sure I want to */
315 return 0;
319 /******************************************************************
320 * DeleteIpNetEntry (IPHLPAPI.@)
322 * Delete an ARP entry.
324 * PARAMS
325 * pArpEntry [In] ARP entry to delete
327 * RETURNS
328 * Success: NO_ERROR
329 * Failure: error code from winerror.h
331 * FIXME
332 * Stub, returns NO_ERROR.
334 DWORD WINAPI DeleteIpNetEntry(PMIB_IPNETROW pArpEntry)
336 FIXME("(pArpEntry %p): stub\n", pArpEntry);
337 /* could use SIOCDARP on systems that support it, not sure I want to */
338 return 0;
342 /******************************************************************
343 * DeleteProxyArpEntry (IPHLPAPI.@)
345 * Delete a Proxy ARP entry.
347 * PARAMS
348 * dwAddress [In] IP address for which this computer acts as a proxy.
349 * dwMask [In] subnet mask for dwAddress
350 * dwIfIndex [In] interface index
352 * RETURNS
353 * Success: NO_ERROR
354 * Failure: error code from winerror.h
356 * FIXME
357 * Stub, returns ERROR_NOT_SUPPORTED.
359 DWORD WINAPI DeleteProxyArpEntry(DWORD dwAddress, DWORD dwMask, DWORD dwIfIndex)
361 FIXME("(dwAddress 0x%08x, dwMask 0x%08x, dwIfIndex 0x%08x): stub\n",
362 dwAddress, dwMask, dwIfIndex);
363 return ERROR_NOT_SUPPORTED;
367 /******************************************************************
368 * EnableRouter (IPHLPAPI.@)
370 * Turn on ip forwarding.
372 * PARAMS
373 * pHandle [In/Out]
374 * pOverlapped [In/Out] hEvent member should contain a valid handle.
376 * RETURNS
377 * Success: ERROR_IO_PENDING
378 * Failure: error code from winerror.h
380 * FIXME
381 * Stub, returns ERROR_NOT_SUPPORTED.
383 DWORD WINAPI EnableRouter(HANDLE * pHandle, OVERLAPPED * pOverlapped)
385 FIXME("(pHandle %p, pOverlapped %p): stub\n", pHandle, pOverlapped);
386 /* could echo "1" > /proc/net/sys/net/ipv4/ip_forward, not sure I want to
387 could map EACCESS to ERROR_ACCESS_DENIED, I suppose
389 return ERROR_NOT_SUPPORTED;
393 /******************************************************************
394 * FlushIpNetTable (IPHLPAPI.@)
396 * Delete all ARP entries of an interface
398 * PARAMS
399 * dwIfIndex [In] interface index
401 * RETURNS
402 * Success: NO_ERROR
403 * Failure: error code from winerror.h
405 * FIXME
406 * Stub, returns ERROR_NOT_SUPPORTED.
408 DWORD WINAPI FlushIpNetTable(DWORD dwIfIndex)
410 FIXME("(dwIfIndex 0x%08x): stub\n", dwIfIndex);
411 /* this flushes the arp cache of the given index */
412 return ERROR_NOT_SUPPORTED;
416 /******************************************************************
417 * GetAdapterIndex (IPHLPAPI.@)
419 * Get interface index from its name.
421 * PARAMS
422 * AdapterName [In] unicode string with the adapter name
423 * IfIndex [Out] returns found interface index
425 * RETURNS
426 * Success: NO_ERROR
427 * Failure: error code from winerror.h
429 DWORD WINAPI GetAdapterIndex(LPWSTR AdapterName, PULONG IfIndex)
431 char adapterName[MAX_ADAPTER_NAME];
432 unsigned int i;
433 DWORD ret;
435 TRACE("(AdapterName %p, IfIndex %p)\n", AdapterName, IfIndex);
436 /* The adapter name is guaranteed not to have any unicode characters, so
437 * this translation is never lossy */
438 for (i = 0; i < sizeof(adapterName) - 1 && AdapterName[i]; i++)
439 adapterName[i] = (char)AdapterName[i];
440 adapterName[i] = '\0';
441 ret = getInterfaceIndexByName(adapterName, IfIndex);
442 TRACE("returning %d\n", ret);
443 return ret;
447 /******************************************************************
448 * GetAdaptersInfo (IPHLPAPI.@)
450 * Get information about adapters.
452 * PARAMS
453 * pAdapterInfo [Out] buffer for adapter infos
454 * pOutBufLen [In] length of output buffer
456 * RETURNS
457 * Success: NO_ERROR
458 * Failure: error code from winerror.h
460 DWORD WINAPI GetAdaptersInfo(PIP_ADAPTER_INFO pAdapterInfo, PULONG pOutBufLen)
462 DWORD ret;
464 TRACE("pAdapterInfo %p, pOutBufLen %p\n", pAdapterInfo, pOutBufLen);
465 if (!pOutBufLen)
466 ret = ERROR_INVALID_PARAMETER;
467 else {
468 DWORD numNonLoopbackInterfaces = getNumNonLoopbackInterfaces();
470 if (numNonLoopbackInterfaces > 0) {
471 DWORD numIPAddresses = getNumIPAddresses();
472 ULONG size;
474 /* This may slightly overestimate the amount of space needed, because
475 * the IP addresses include the loopback address, but it's easier
476 * to make sure there's more than enough space than to make sure there's
477 * precisely enough space.
479 size = sizeof(IP_ADAPTER_INFO) * numNonLoopbackInterfaces;
480 size += numIPAddresses * sizeof(IP_ADDR_STRING);
481 if (!pAdapterInfo || *pOutBufLen < size) {
482 *pOutBufLen = size;
483 ret = ERROR_BUFFER_OVERFLOW;
485 else {
486 InterfaceIndexTable *table = NULL;
487 PMIB_IPADDRTABLE ipAddrTable = NULL;
488 PMIB_IPFORWARDTABLE routeTable = NULL;
490 ret = getIPAddrTable(&ipAddrTable, GetProcessHeap(), 0);
491 if (!ret)
492 ret = AllocateAndGetIpForwardTableFromStack(&routeTable, FALSE, GetProcessHeap(), 0);
493 if (!ret)
494 table = getNonLoopbackInterfaceIndexTable();
495 if (table) {
496 size = sizeof(IP_ADAPTER_INFO) * table->numIndexes;
497 size += ipAddrTable->dwNumEntries * sizeof(IP_ADDR_STRING);
498 if (*pOutBufLen < size) {
499 *pOutBufLen = size;
500 ret = ERROR_INSUFFICIENT_BUFFER;
502 else {
503 DWORD ndx;
504 HKEY hKey;
505 BOOL winsEnabled = FALSE;
506 IP_ADDRESS_STRING primaryWINS, secondaryWINS;
507 PIP_ADDR_STRING nextIPAddr = (PIP_ADDR_STRING)((LPBYTE)pAdapterInfo
508 + numNonLoopbackInterfaces * sizeof(IP_ADAPTER_INFO));
510 memset(pAdapterInfo, 0, size);
511 /* @@ Wine registry key: HKCU\Software\Wine\Network */
512 if (RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Network",
513 &hKey) == ERROR_SUCCESS) {
514 DWORD size = sizeof(primaryWINS.String);
515 unsigned long addr;
517 RegQueryValueExA(hKey, "WinsServer", NULL, NULL,
518 (LPBYTE)primaryWINS.String, &size);
519 addr = inet_addr(primaryWINS.String);
520 if (addr != INADDR_NONE && addr != INADDR_ANY)
521 winsEnabled = TRUE;
522 size = sizeof(secondaryWINS.String);
523 RegQueryValueExA(hKey, "BackupWinsServer", NULL, NULL,
524 (LPBYTE)secondaryWINS.String, &size);
525 addr = inet_addr(secondaryWINS.String);
526 if (addr != INADDR_NONE && addr != INADDR_ANY)
527 winsEnabled = TRUE;
528 RegCloseKey(hKey);
530 for (ndx = 0; ndx < table->numIndexes; ndx++) {
531 PIP_ADAPTER_INFO ptr = &pAdapterInfo[ndx];
532 DWORD i;
533 PIP_ADDR_STRING currentIPAddr = &ptr->IpAddressList;
534 BOOL firstIPAddr = TRUE;
536 /* on Win98 this is left empty, but whatever */
537 getInterfaceNameByIndex(table->indexes[ndx], ptr->AdapterName);
538 getInterfaceNameByIndex(table->indexes[ndx], ptr->Description);
539 ptr->AddressLength = sizeof(ptr->Address);
540 getInterfacePhysicalByIndex(table->indexes[ndx],
541 &ptr->AddressLength, ptr->Address, &ptr->Type);
542 ptr->Index = table->indexes[ndx];
543 for (i = 0; i < ipAddrTable->dwNumEntries; i++) {
544 if (ipAddrTable->table[i].dwIndex == ptr->Index) {
545 if (firstIPAddr) {
546 toIPAddressString(ipAddrTable->table[i].dwAddr,
547 ptr->IpAddressList.IpAddress.String);
548 toIPAddressString(ipAddrTable->table[i].dwMask,
549 ptr->IpAddressList.IpMask.String);
550 firstIPAddr = FALSE;
552 else {
553 currentIPAddr->Next = nextIPAddr;
554 currentIPAddr = nextIPAddr;
555 toIPAddressString(ipAddrTable->table[i].dwAddr,
556 currentIPAddr->IpAddress.String);
557 toIPAddressString(ipAddrTable->table[i].dwMask,
558 currentIPAddr->IpMask.String);
559 nextIPAddr++;
563 /* Find first router through this interface, which we'll assume
564 * is the default gateway for this adapter */
565 for (i = 0; i < routeTable->dwNumEntries; i++)
566 if (routeTable->table[i].dwForwardIfIndex == ptr->Index
567 && routeTable->table[i].dwForwardType ==
568 MIB_IPROUTE_TYPE_INDIRECT)
569 toIPAddressString(routeTable->table[i].dwForwardNextHop,
570 ptr->GatewayList.IpAddress.String);
571 if (winsEnabled) {
572 ptr->HaveWins = TRUE;
573 memcpy(ptr->PrimaryWinsServer.IpAddress.String,
574 primaryWINS.String, sizeof(primaryWINS.String));
575 memcpy(ptr->SecondaryWinsServer.IpAddress.String,
576 secondaryWINS.String, sizeof(secondaryWINS.String));
578 if (ndx < table->numIndexes - 1)
579 ptr->Next = &pAdapterInfo[ndx + 1];
580 else
581 ptr->Next = NULL;
583 ret = NO_ERROR;
585 HeapFree(GetProcessHeap(), 0, table);
587 else
588 ret = ERROR_OUTOFMEMORY;
589 HeapFree(GetProcessHeap(), 0, routeTable);
590 HeapFree(GetProcessHeap(), 0, ipAddrTable);
593 else
594 ret = ERROR_NO_DATA;
596 TRACE("returning %d\n", ret);
597 return ret;
600 static DWORD typeFromMibType(DWORD mib_type)
602 switch (mib_type)
604 case MIB_IF_TYPE_ETHERNET: return IF_TYPE_ETHERNET_CSMACD;
605 case MIB_IF_TYPE_TOKENRING: return IF_TYPE_ISO88025_TOKENRING;
606 case MIB_IF_TYPE_PPP: return IF_TYPE_PPP;
607 case MIB_IF_TYPE_LOOPBACK: return IF_TYPE_SOFTWARE_LOOPBACK;
608 default: return IF_TYPE_OTHER;
612 static ULONG addressesFromIndex(DWORD index, DWORD **addrs, ULONG *num_addrs)
614 ULONG ret, i;
615 MIB_IPADDRTABLE *at;
617 *num_addrs = 0;
618 if ((ret = getIPAddrTable(&at, GetProcessHeap(), 0))) return ret;
619 for (i = 0; i < at->dwNumEntries; i++)
621 if (at->table[i].dwIndex == index) (*num_addrs)++;
623 if (!(*addrs = HeapAlloc(GetProcessHeap(), 0, *num_addrs * sizeof(DWORD))))
625 HeapFree(GetProcessHeap(), 0, at);
626 return ERROR_OUTOFMEMORY;
628 for (i = 0; i < at->dwNumEntries; i++)
630 if (at->table[i].dwIndex == index) (*addrs)[i] = at->table[i].dwAddr;
632 HeapFree(GetProcessHeap(), 0, at);
633 return ERROR_SUCCESS;
636 static ULONG adapterAddressesFromIndex(DWORD index, IP_ADAPTER_ADDRESSES *aa, ULONG *size)
638 ULONG ret, i, num_addrs, total_size;
639 DWORD *addrs;
641 if ((ret = addressesFromIndex(index, &addrs, &num_addrs))) return ret;
643 total_size = sizeof(IP_ADAPTER_ADDRESSES);
644 total_size += IF_NAMESIZE;
645 total_size += sizeof(IP_ADAPTER_UNICAST_ADDRESS) * num_addrs;
646 total_size += sizeof(struct sockaddr_in) * num_addrs;
648 if (aa && *size >= total_size)
650 char name[IF_NAMESIZE], *ptr = (char *)aa + sizeof(IP_ADAPTER_ADDRESSES);
651 DWORD buflen, type, status;
653 memset(aa, 0, sizeof(IP_ADAPTER_ADDRESSES));
654 aa->Length = sizeof(IP_ADAPTER_ADDRESSES);
655 aa->IfIndex = index;
657 getInterfaceNameByIndex(index, name);
658 memcpy(ptr, name, IF_NAMESIZE);
659 aa->AdapterName = ptr;
660 ptr += IF_NAMESIZE;
662 if (num_addrs)
664 IP_ADAPTER_UNICAST_ADDRESS *ua;
665 struct sockaddr_in *sa;
667 ua = aa->FirstUnicastAddress = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
668 for (i = 0; i < num_addrs; i++)
670 memset(ua, 0, sizeof(IP_ADAPTER_UNICAST_ADDRESS));
671 ua->Length = sizeof(IP_ADAPTER_UNICAST_ADDRESS);
672 ua->Address.iSockaddrLength = sizeof(struct sockaddr_in);
673 ua->Address.lpSockaddr = (SOCKADDR *)((char *)ua + ua->Length);
675 sa = (struct sockaddr_in *)ua->Address.lpSockaddr;
676 sa->sin_family = AF_INET;
677 sa->sin_addr.s_addr = addrs[i];
678 sa->sin_port = 0;
680 ptr += ua->Length + ua->Address.iSockaddrLength;
681 if (i < num_addrs - 1)
683 ua->Next = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
684 ua = ua->Next;
689 buflen = MAX_INTERFACE_PHYSADDR;
690 getInterfacePhysicalByIndex(index, &buflen, aa->PhysicalAddress, &type);
691 aa->PhysicalAddressLength = buflen;
692 aa->IfType = typeFromMibType(type);
694 getInterfaceMtuByName(name, &aa->Mtu);
696 getInterfaceStatusByName(name, &status);
697 if (status == MIB_IF_OPER_STATUS_OPERATIONAL) aa->OperStatus = IfOperStatusUp;
698 else if (status == MIB_IF_OPER_STATUS_NON_OPERATIONAL) aa->OperStatus = IfOperStatusDown;
699 else aa->OperStatus = IfOperStatusUnknown;
701 *size = total_size;
702 HeapFree(GetProcessHeap(), 0, addrs);
703 return ERROR_SUCCESS;
706 ULONG WINAPI GetAdaptersAddresses(ULONG family, ULONG flags, PVOID reserved,
707 PIP_ADAPTER_ADDRESSES aa, PULONG buflen)
709 InterfaceIndexTable *table;
710 ULONG i, size, total_size, ret = ERROR_NO_DATA;
712 if (!buflen) return ERROR_INVALID_PARAMETER;
714 if (family == AF_INET6 || family == AF_UNSPEC)
715 FIXME("no support for IPv6 addresses\n");
717 if (family != AF_INET && family != AF_UNSPEC) return ERROR_NO_DATA;
719 table = getInterfaceIndexTable();
720 if (!table || !table->numIndexes)
722 HeapFree(GetProcessHeap(), 0, table);
723 return ERROR_NO_DATA;
725 total_size = 0;
726 for (i = 0; i < table->numIndexes; i++)
728 size = 0;
729 if ((ret = adapterAddressesFromIndex(table->indexes[i], NULL, &size)))
731 HeapFree(GetProcessHeap(), 0, table);
732 return ret;
734 total_size += size;
736 if (aa && *buflen >= total_size)
738 ULONG bytes_left = size = total_size;
739 for (i = 0; i < table->numIndexes; i++)
741 if ((ret = adapterAddressesFromIndex(table->indexes[i], aa, &size)))
743 HeapFree(GetProcessHeap(), 0, table);
744 return ret;
746 if (i < table->numIndexes - 1)
748 aa->Next = (IP_ADAPTER_ADDRESSES *)((char *)aa + size);
749 aa = aa->Next;
750 size = bytes_left -= size;
753 ret = ERROR_SUCCESS;
755 if (*buflen < total_size) ret = ERROR_BUFFER_OVERFLOW;
756 *buflen = total_size;
758 TRACE("num adapters %u\n", table->numIndexes);
759 HeapFree(GetProcessHeap(), 0, table);
760 return ret;
763 /******************************************************************
764 * GetBestInterface (IPHLPAPI.@)
766 * Get the interface, with the best route for the given IP address.
768 * PARAMS
769 * dwDestAddr [In] IP address to search the interface for
770 * pdwBestIfIndex [Out] found best interface
772 * RETURNS
773 * Success: NO_ERROR
774 * Failure: error code from winerror.h
776 DWORD WINAPI GetBestInterface(IPAddr dwDestAddr, PDWORD pdwBestIfIndex)
778 struct WS_sockaddr_in sa_in;
779 memset(&sa_in, 0, sizeof(sa_in));
780 sa_in.sin_family = AF_INET;
781 sa_in.sin_addr.S_un.S_addr = dwDestAddr;
782 return GetBestInterfaceEx((struct WS_sockaddr *)&sa_in, pdwBestIfIndex);
785 /******************************************************************
786 * GetBestInterfaceEx (IPHLPAPI.@)
788 * Get the interface, with the best route for the given IP address.
790 * PARAMS
791 * dwDestAddr [In] IP address to search the interface for
792 * pdwBestIfIndex [Out] found best interface
794 * RETURNS
795 * Success: NO_ERROR
796 * Failure: error code from winerror.h
798 DWORD WINAPI GetBestInterfaceEx(struct WS_sockaddr *pDestAddr, PDWORD pdwBestIfIndex)
800 DWORD ret;
802 TRACE("pDestAddr %p, pdwBestIfIndex %p\n", pDestAddr, pdwBestIfIndex);
803 if (!pDestAddr || !pdwBestIfIndex)
804 ret = ERROR_INVALID_PARAMETER;
805 else {
806 MIB_IPFORWARDROW ipRow;
808 if (pDestAddr->sa_family == AF_INET) {
809 ret = GetBestRoute(((struct WS_sockaddr_in *)pDestAddr)->sin_addr.S_un.S_addr, 0, &ipRow);
810 if (ret == ERROR_SUCCESS)
811 *pdwBestIfIndex = ipRow.dwForwardIfIndex;
812 } else {
813 FIXME("address family %d not supported\n", pDestAddr->sa_family);
814 ret = ERROR_NOT_SUPPORTED;
817 TRACE("returning %d\n", ret);
818 return ret;
822 /******************************************************************
823 * GetBestRoute (IPHLPAPI.@)
825 * Get the best route for the given IP address.
827 * PARAMS
828 * dwDestAddr [In] IP address to search the best route for
829 * dwSourceAddr [In] optional source IP address
830 * pBestRoute [Out] found best route
832 * RETURNS
833 * Success: NO_ERROR
834 * Failure: error code from winerror.h
836 DWORD WINAPI GetBestRoute(DWORD dwDestAddr, DWORD dwSourceAddr, PMIB_IPFORWARDROW pBestRoute)
838 PMIB_IPFORWARDTABLE table;
839 DWORD ret;
841 TRACE("dwDestAddr 0x%08x, dwSourceAddr 0x%08x, pBestRoute %p\n", dwDestAddr,
842 dwSourceAddr, pBestRoute);
843 if (!pBestRoute)
844 return ERROR_INVALID_PARAMETER;
846 ret = AllocateAndGetIpForwardTableFromStack(&table, FALSE, GetProcessHeap(), 0);
847 if (!ret) {
848 DWORD ndx, matchedBits, matchedNdx = table->dwNumEntries;
850 for (ndx = 0, matchedBits = 0; ndx < table->dwNumEntries; ndx++) {
851 if (table->table[ndx].dwForwardType != MIB_IPROUTE_TYPE_INVALID &&
852 (dwDestAddr & table->table[ndx].dwForwardMask) ==
853 (table->table[ndx].dwForwardDest & table->table[ndx].dwForwardMask)) {
854 DWORD numShifts, mask;
856 for (numShifts = 0, mask = table->table[ndx].dwForwardMask;
857 mask && !(mask & 1); mask >>= 1, numShifts++)
859 if (numShifts > matchedBits) {
860 matchedBits = numShifts;
861 matchedNdx = ndx;
863 else if (!matchedBits && table->table[ndx].dwForwardType ==
864 MIB_IPROUTE_TYPE_INDIRECT) {
865 /* default to a default gateway */
866 matchedNdx = ndx;
870 if (matchedNdx < table->dwNumEntries) {
871 memcpy(pBestRoute, &table->table[matchedNdx], sizeof(MIB_IPFORWARDROW));
872 ret = ERROR_SUCCESS;
874 else {
875 /* No route matches, which can happen if there's no default route. */
876 ret = ERROR_HOST_UNREACHABLE;
878 HeapFree(GetProcessHeap(), 0, table);
880 TRACE("returning %d\n", ret);
881 return ret;
885 /******************************************************************
886 * GetFriendlyIfIndex (IPHLPAPI.@)
888 * Get a "friendly" version of IfIndex, which is one that doesn't
889 * have the top byte set. Doesn't validate whether IfIndex is a valid
890 * adapter index.
892 * PARAMS
893 * IfIndex [In] interface index to get the friendly one for
895 * RETURNS
896 * A friendly version of IfIndex.
898 DWORD WINAPI GetFriendlyIfIndex(DWORD IfIndex)
900 /* windows doesn't validate these, either, just makes sure the top byte is
901 cleared. I assume my ifenum module never gives an index with the top
902 byte set. */
903 TRACE("returning %d\n", IfIndex);
904 return IfIndex;
908 /******************************************************************
909 * GetIfEntry (IPHLPAPI.@)
911 * Get information about an interface.
913 * PARAMS
914 * pIfRow [In/Out] In: dwIndex of MIB_IFROW selects the interface.
915 * Out: interface information
917 * RETURNS
918 * Success: NO_ERROR
919 * Failure: error code from winerror.h
921 DWORD WINAPI GetIfEntry(PMIB_IFROW pIfRow)
923 DWORD ret;
924 char nameBuf[MAX_ADAPTER_NAME];
925 char *name;
927 TRACE("pIfRow %p\n", pIfRow);
928 if (!pIfRow)
929 return ERROR_INVALID_PARAMETER;
931 name = getInterfaceNameByIndex(pIfRow->dwIndex, nameBuf);
932 if (name) {
933 ret = getInterfaceEntryByName(name, pIfRow);
934 if (ret == NO_ERROR)
935 ret = getInterfaceStatsByName(name, pIfRow);
937 else
938 ret = ERROR_INVALID_DATA;
939 TRACE("returning %d\n", ret);
940 return ret;
944 static int IfTableSorter(const void *a, const void *b)
946 int ret;
948 if (a && b)
949 ret = ((const MIB_IFROW*)a)->dwIndex - ((const MIB_IFROW*)b)->dwIndex;
950 else
951 ret = 0;
952 return ret;
956 /******************************************************************
957 * GetIfTable (IPHLPAPI.@)
959 * Get a table of local interfaces.
961 * PARAMS
962 * pIfTable [Out] buffer for local interfaces table
963 * pdwSize [In/Out] length of output buffer
964 * bOrder [In] whether to sort the table
966 * RETURNS
967 * Success: NO_ERROR
968 * Failure: error code from winerror.h
970 * NOTES
971 * If pdwSize is less than required, the function will return
972 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the required byte
973 * size.
974 * If bOrder is true, the returned table will be sorted by interface index.
976 DWORD WINAPI GetIfTable(PMIB_IFTABLE pIfTable, PULONG pdwSize, BOOL bOrder)
978 DWORD ret;
980 TRACE("pIfTable %p, pdwSize %p, bOrder %d\n", pdwSize, pdwSize,
981 (DWORD)bOrder);
982 if (!pdwSize)
983 ret = ERROR_INVALID_PARAMETER;
984 else {
985 DWORD numInterfaces = getNumInterfaces();
986 ULONG size = sizeof(MIB_IFTABLE);
988 if (numInterfaces > 1)
989 size += (numInterfaces - 1) * sizeof(MIB_IFROW);
990 if (!pIfTable || *pdwSize < size) {
991 *pdwSize = size;
992 ret = ERROR_INSUFFICIENT_BUFFER;
994 else {
995 InterfaceIndexTable *table = getInterfaceIndexTable();
997 if (table) {
998 size = sizeof(MIB_IFTABLE);
999 if (table->numIndexes > 1)
1000 size += (table->numIndexes - 1) * sizeof(MIB_IFROW);
1001 if (*pdwSize < size) {
1002 *pdwSize = size;
1003 ret = ERROR_INSUFFICIENT_BUFFER;
1005 else {
1006 DWORD ndx;
1008 *pdwSize = size;
1009 pIfTable->dwNumEntries = 0;
1010 for (ndx = 0; ndx < table->numIndexes; ndx++) {
1011 pIfTable->table[ndx].dwIndex = table->indexes[ndx];
1012 GetIfEntry(&pIfTable->table[ndx]);
1013 pIfTable->dwNumEntries++;
1015 if (bOrder)
1016 qsort(pIfTable->table, pIfTable->dwNumEntries, sizeof(MIB_IFROW),
1017 IfTableSorter);
1018 ret = NO_ERROR;
1020 HeapFree(GetProcessHeap(), 0, table);
1022 else
1023 ret = ERROR_OUTOFMEMORY;
1026 TRACE("returning %d\n", ret);
1027 return ret;
1031 /******************************************************************
1032 * GetInterfaceInfo (IPHLPAPI.@)
1034 * Get a list of network interface adapters.
1036 * PARAMS
1037 * pIfTable [Out] buffer for interface adapters
1038 * dwOutBufLen [Out] if buffer is too small, returns required size
1040 * RETURNS
1041 * Success: NO_ERROR
1042 * Failure: error code from winerror.h
1044 * BUGS
1045 * MSDN states this should return non-loopback interfaces only.
1047 DWORD WINAPI GetInterfaceInfo(PIP_INTERFACE_INFO pIfTable, PULONG dwOutBufLen)
1049 DWORD ret;
1051 TRACE("pIfTable %p, dwOutBufLen %p\n", pIfTable, dwOutBufLen);
1052 if (!dwOutBufLen)
1053 ret = ERROR_INVALID_PARAMETER;
1054 else {
1055 DWORD numInterfaces = getNumInterfaces();
1056 ULONG size = sizeof(IP_INTERFACE_INFO);
1058 if (numInterfaces > 1)
1059 size += (numInterfaces - 1) * sizeof(IP_ADAPTER_INDEX_MAP);
1060 if (!pIfTable || *dwOutBufLen < size) {
1061 *dwOutBufLen = size;
1062 ret = ERROR_INSUFFICIENT_BUFFER;
1064 else {
1065 InterfaceIndexTable *table = getInterfaceIndexTable();
1067 if (table) {
1068 size = sizeof(IP_INTERFACE_INFO);
1069 if (table->numIndexes > 1)
1070 size += (table->numIndexes - 1) * sizeof(IP_ADAPTER_INDEX_MAP);
1071 if (*dwOutBufLen < size) {
1072 *dwOutBufLen = size;
1073 ret = ERROR_INSUFFICIENT_BUFFER;
1075 else {
1076 DWORD ndx;
1077 char nameBuf[MAX_ADAPTER_NAME];
1079 *dwOutBufLen = size;
1080 pIfTable->NumAdapters = 0;
1081 for (ndx = 0; ndx < table->numIndexes; ndx++) {
1082 const char *walker, *name;
1083 WCHAR *assigner;
1085 pIfTable->Adapter[ndx].Index = table->indexes[ndx];
1086 name = getInterfaceNameByIndex(table->indexes[ndx], nameBuf);
1087 for (walker = name, assigner = pIfTable->Adapter[ndx].Name;
1088 walker && *walker &&
1089 assigner - pIfTable->Adapter[ndx].Name < MAX_ADAPTER_NAME - 1;
1090 walker++, assigner++)
1091 *assigner = *walker;
1092 *assigner = 0;
1093 pIfTable->NumAdapters++;
1095 ret = NO_ERROR;
1097 HeapFree(GetProcessHeap(), 0, table);
1099 else
1100 ret = ERROR_OUTOFMEMORY;
1103 TRACE("returning %d\n", ret);
1104 return ret;
1108 /******************************************************************
1109 * GetIpAddrTable (IPHLPAPI.@)
1111 * Get interface-to-IP address mapping table.
1113 * PARAMS
1114 * pIpAddrTable [Out] buffer for mapping table
1115 * pdwSize [In/Out] length of output buffer
1116 * bOrder [In] whether to sort the table
1118 * RETURNS
1119 * Success: NO_ERROR
1120 * Failure: error code from winerror.h
1122 * NOTES
1123 * If pdwSize is less than required, the function will return
1124 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the required byte
1125 * size.
1126 * If bOrder is true, the returned table will be sorted by the next hop and
1127 * an assortment of arbitrary parameters.
1129 DWORD WINAPI GetIpAddrTable(PMIB_IPADDRTABLE pIpAddrTable, PULONG pdwSize, BOOL bOrder)
1131 DWORD ret;
1133 TRACE("pIpAddrTable %p, pdwSize %p, bOrder %d\n", pIpAddrTable, pdwSize,
1134 (DWORD)bOrder);
1135 if (!pdwSize)
1136 ret = ERROR_INVALID_PARAMETER;
1137 else {
1138 PMIB_IPADDRTABLE table;
1140 ret = getIPAddrTable(&table, GetProcessHeap(), 0);
1141 if (ret == NO_ERROR)
1143 ULONG size = sizeof(MIB_IPADDRTABLE);
1145 if (table->dwNumEntries > 1)
1146 size += (table->dwNumEntries - 1) * sizeof(MIB_IPADDRROW);
1147 if (!pIpAddrTable || *pdwSize < size) {
1148 *pdwSize = size;
1149 ret = ERROR_INSUFFICIENT_BUFFER;
1151 else {
1152 *pdwSize = size;
1153 memcpy(pIpAddrTable, table, size);
1154 if (bOrder)
1155 qsort(pIpAddrTable->table, pIpAddrTable->dwNumEntries,
1156 sizeof(MIB_IPADDRROW), IpAddrTableSorter);
1157 ret = NO_ERROR;
1159 HeapFree(GetProcessHeap(), 0, table);
1162 TRACE("returning %d\n", ret);
1163 return ret;
1167 /******************************************************************
1168 * GetIpForwardTable (IPHLPAPI.@)
1170 * Get the route table.
1172 * PARAMS
1173 * pIpForwardTable [Out] buffer for route table
1174 * pdwSize [In/Out] length of output buffer
1175 * bOrder [In] whether to sort the table
1177 * RETURNS
1178 * Success: NO_ERROR
1179 * Failure: error code from winerror.h
1181 * NOTES
1182 * If pdwSize is less than required, the function will return
1183 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the required byte
1184 * size.
1185 * If bOrder is true, the returned table will be sorted by the next hop and
1186 * an assortment of arbitrary parameters.
1188 DWORD WINAPI GetIpForwardTable(PMIB_IPFORWARDTABLE pIpForwardTable, PULONG pdwSize, BOOL bOrder)
1190 DWORD ret;
1191 PMIB_IPFORWARDTABLE table;
1193 TRACE("pIpForwardTable %p, pdwSize %p, bOrder %d\n", pIpForwardTable, pdwSize, bOrder);
1195 if (!pdwSize) return ERROR_INVALID_PARAMETER;
1197 ret = AllocateAndGetIpForwardTableFromStack(&table, bOrder, GetProcessHeap(), 0);
1198 if (!ret) {
1199 DWORD size = FIELD_OFFSET( MIB_IPFORWARDTABLE, table[table->dwNumEntries] );
1200 if (!pIpForwardTable || *pdwSize < size) {
1201 *pdwSize = size;
1202 ret = ERROR_INSUFFICIENT_BUFFER;
1204 else {
1205 *pdwSize = size;
1206 memcpy(pIpForwardTable, table, size);
1208 HeapFree(GetProcessHeap(), 0, table);
1210 TRACE("returning %d\n", ret);
1211 return ret;
1215 /******************************************************************
1216 * GetIpNetTable (IPHLPAPI.@)
1218 * Get the IP-to-physical address mapping table.
1220 * PARAMS
1221 * pIpNetTable [Out] buffer for mapping table
1222 * pdwSize [In/Out] length of output buffer
1223 * bOrder [In] whether to sort the table
1225 * RETURNS
1226 * Success: NO_ERROR
1227 * Failure: error code from winerror.h
1229 * NOTES
1230 * If pdwSize is less than required, the function will return
1231 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the required byte
1232 * size.
1233 * If bOrder is true, the returned table will be sorted by IP address.
1235 DWORD WINAPI GetIpNetTable(PMIB_IPNETTABLE pIpNetTable, PULONG pdwSize, BOOL bOrder)
1237 DWORD ret;
1238 PMIB_IPNETTABLE table;
1240 TRACE("pIpNetTable %p, pdwSize %p, bOrder %d\n", pIpNetTable, pdwSize, bOrder);
1242 if (!pdwSize) return ERROR_INVALID_PARAMETER;
1244 ret = AllocateAndGetIpNetTableFromStack( &table, bOrder, GetProcessHeap(), 0 );
1245 if (!ret) {
1246 DWORD size = FIELD_OFFSET( MIB_IPNETTABLE, table[table->dwNumEntries] );
1247 if (!pIpNetTable || *pdwSize < size) {
1248 *pdwSize = size;
1249 ret = ERROR_INSUFFICIENT_BUFFER;
1251 else {
1252 *pdwSize = size;
1253 memcpy(pIpNetTable, table, size);
1255 HeapFree(GetProcessHeap(), 0, table);
1257 TRACE("returning %d\n", ret);
1258 return ret;
1262 /******************************************************************
1263 * GetNetworkParams (IPHLPAPI.@)
1265 * Get the network parameters for the local computer.
1267 * PARAMS
1268 * pFixedInfo [Out] buffer for network parameters
1269 * pOutBufLen [In/Out] length of output buffer
1271 * RETURNS
1272 * Success: NO_ERROR
1273 * Failure: error code from winerror.h
1275 * NOTES
1276 * If pOutBufLen is less than required, the function will return
1277 * ERROR_INSUFFICIENT_BUFFER, and pOutBufLen will be set to the required byte
1278 * size.
1280 DWORD WINAPI GetNetworkParams(PFIXED_INFO pFixedInfo, PULONG pOutBufLen)
1282 DWORD ret, size;
1283 LONG regReturn;
1284 HKEY hKey;
1286 TRACE("pFixedInfo %p, pOutBufLen %p\n", pFixedInfo, pOutBufLen);
1287 if (!pOutBufLen)
1288 return ERROR_INVALID_PARAMETER;
1290 initialise_resolver();
1291 size = sizeof(FIXED_INFO) + (_res.nscount > 0 ? (_res.nscount - 1) *
1292 sizeof(IP_ADDR_STRING) : 0);
1293 if (!pFixedInfo || *pOutBufLen < size) {
1294 *pOutBufLen = size;
1295 return ERROR_BUFFER_OVERFLOW;
1298 memset(pFixedInfo, 0, size);
1299 size = sizeof(pFixedInfo->HostName);
1300 GetComputerNameExA(ComputerNameDnsHostname, pFixedInfo->HostName, &size);
1301 size = sizeof(pFixedInfo->DomainName);
1302 GetComputerNameExA(ComputerNameDnsDomain, pFixedInfo->DomainName, &size);
1303 if (_res.nscount > 0) {
1304 PIP_ADDR_STRING ptr;
1305 int i;
1307 for (i = 0, ptr = &pFixedInfo->DnsServerList; i < _res.nscount && ptr;
1308 i++, ptr = ptr->Next) {
1309 toIPAddressString(_res.nsaddr_list[i].sin_addr.s_addr,
1310 ptr->IpAddress.String);
1311 if (i == _res.nscount - 1)
1312 ptr->Next = NULL;
1313 else if (i == 0)
1314 ptr->Next = (PIP_ADDR_STRING)((LPBYTE)pFixedInfo + sizeof(FIXED_INFO));
1315 else
1316 ptr->Next = (PIP_ADDR_STRING)((PBYTE)ptr + sizeof(IP_ADDR_STRING));
1319 pFixedInfo->NodeType = HYBRID_NODETYPE;
1320 regReturn = RegOpenKeyExA(HKEY_LOCAL_MACHINE,
1321 "SYSTEM\\CurrentControlSet\\Services\\VxD\\MSTCP", 0, KEY_READ, &hKey);
1322 if (regReturn != ERROR_SUCCESS)
1323 regReturn = RegOpenKeyExA(HKEY_LOCAL_MACHINE,
1324 "SYSTEM\\CurrentControlSet\\Services\\NetBT\\Parameters", 0, KEY_READ,
1325 &hKey);
1326 if (regReturn == ERROR_SUCCESS)
1328 DWORD size = sizeof(pFixedInfo->ScopeId);
1330 RegQueryValueExA(hKey, "ScopeID", NULL, NULL, (LPBYTE)pFixedInfo->ScopeId, &size);
1331 RegCloseKey(hKey);
1334 /* FIXME: can check whether routing's enabled in /proc/sys/net/ipv4/ip_forward
1335 I suppose could also check for a listener on port 53 to set EnableDns */
1336 ret = NO_ERROR;
1337 TRACE("returning %d\n", ret);
1338 return ret;
1342 /******************************************************************
1343 * GetNumberOfInterfaces (IPHLPAPI.@)
1345 * Get the number of interfaces.
1347 * PARAMS
1348 * pdwNumIf [Out] number of interfaces
1350 * RETURNS
1351 * NO_ERROR on success, ERROR_INVALID_PARAMETER if pdwNumIf is NULL.
1353 DWORD WINAPI GetNumberOfInterfaces(PDWORD pdwNumIf)
1355 DWORD ret;
1357 TRACE("pdwNumIf %p\n", pdwNumIf);
1358 if (!pdwNumIf)
1359 ret = ERROR_INVALID_PARAMETER;
1360 else {
1361 *pdwNumIf = getNumInterfaces();
1362 ret = NO_ERROR;
1364 TRACE("returning %d\n", ret);
1365 return ret;
1369 /******************************************************************
1370 * GetPerAdapterInfo (IPHLPAPI.@)
1372 * Get information about an adapter corresponding to an interface.
1374 * PARAMS
1375 * IfIndex [In] interface info
1376 * pPerAdapterInfo [Out] buffer for per adapter info
1377 * pOutBufLen [In/Out] length of output buffer
1379 * RETURNS
1380 * Success: NO_ERROR
1381 * Failure: error code from winerror.h
1383 * FIXME
1384 * Stub, returns empty IP_PER_ADAPTER_INFO in every case.
1386 DWORD WINAPI GetPerAdapterInfo(ULONG IfIndex, PIP_PER_ADAPTER_INFO pPerAdapterInfo, PULONG pOutBufLen)
1388 ULONG bytesNeeded = sizeof(IP_PER_ADAPTER_INFO);
1390 TRACE("(IfIndex %d, pPerAdapterInfo %p, pOutBufLen %p)\n", IfIndex, pPerAdapterInfo, pOutBufLen);
1392 if (!pOutBufLen) return ERROR_INVALID_PARAMETER;
1394 if (!pPerAdapterInfo || *pOutBufLen < bytesNeeded)
1396 *pOutBufLen = bytesNeeded;
1397 return ERROR_BUFFER_OVERFLOW;
1400 memset(pPerAdapterInfo, 0, bytesNeeded);
1401 return NO_ERROR;
1405 /******************************************************************
1406 * GetRTTAndHopCount (IPHLPAPI.@)
1408 * Get round-trip time (RTT) and hop count.
1410 * PARAMS
1412 * DestIpAddress [In] destination address to get the info for
1413 * HopCount [Out] retrieved hop count
1414 * MaxHops [In] maximum hops to search for the destination
1415 * RTT [Out] RTT in milliseconds
1417 * RETURNS
1418 * Success: TRUE
1419 * Failure: FALSE
1421 * FIXME
1422 * Stub, returns FALSE.
1424 BOOL WINAPI GetRTTAndHopCount(IPAddr DestIpAddress, PULONG HopCount, ULONG MaxHops, PULONG RTT)
1426 FIXME("(DestIpAddress 0x%08x, HopCount %p, MaxHops %d, RTT %p): stub\n",
1427 DestIpAddress, HopCount, MaxHops, RTT);
1428 return FALSE;
1432 /******************************************************************
1433 * GetTcpTable (IPHLPAPI.@)
1435 * Get the table of active TCP connections.
1437 * PARAMS
1438 * pTcpTable [Out] buffer for TCP connections table
1439 * pdwSize [In/Out] length of output buffer
1440 * bOrder [In] whether to order the table
1442 * RETURNS
1443 * Success: NO_ERROR
1444 * Failure: error code from winerror.h
1446 * NOTES
1447 * If pdwSize is less than required, the function will return
1448 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to
1449 * the required byte size.
1450 * If bOrder is true, the returned table will be sorted, first by
1451 * local address and port number, then by remote address and port
1452 * number.
1454 DWORD WINAPI GetTcpTable(PMIB_TCPTABLE pTcpTable, PDWORD pdwSize, BOOL bOrder)
1456 DWORD ret;
1457 PMIB_TCPTABLE table;
1459 TRACE("pTcpTable %p, pdwSize %p, bOrder %d\n", pTcpTable, pdwSize, bOrder);
1461 if (!pdwSize) return ERROR_INVALID_PARAMETER;
1463 ret = AllocateAndGetTcpTableFromStack(&table, bOrder, GetProcessHeap(), 0);
1464 if (!ret) {
1465 DWORD size = FIELD_OFFSET( MIB_TCPTABLE, table[table->dwNumEntries] );
1466 if (!pTcpTable || *pdwSize < size) {
1467 *pdwSize = size;
1468 ret = ERROR_INSUFFICIENT_BUFFER;
1470 else {
1471 *pdwSize = size;
1472 memcpy(pTcpTable, table, size);
1474 HeapFree(GetProcessHeap(), 0, table);
1476 TRACE("returning %d\n", ret);
1477 return ret;
1481 /******************************************************************
1482 * GetUdpTable (IPHLPAPI.@)
1484 * Get a table of active UDP connections.
1486 * PARAMS
1487 * pUdpTable [Out] buffer for UDP connections table
1488 * pdwSize [In/Out] length of output buffer
1489 * bOrder [In] whether to order the table
1491 * RETURNS
1492 * Success: NO_ERROR
1493 * Failure: error code from winerror.h
1495 * NOTES
1496 * If pdwSize is less than required, the function will return
1497 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the
1498 * required byte size.
1499 * If bOrder is true, the returned table will be sorted, first by
1500 * local address, then by local port number.
1502 DWORD WINAPI GetUdpTable(PMIB_UDPTABLE pUdpTable, PDWORD pdwSize, BOOL bOrder)
1504 DWORD ret;
1505 PMIB_UDPTABLE table;
1507 TRACE("pUdpTable %p, pdwSize %p, bOrder %d\n", pUdpTable, pdwSize, bOrder);
1509 if (!pdwSize) return ERROR_INVALID_PARAMETER;
1511 ret = AllocateAndGetUdpTableFromStack( &table, bOrder, GetProcessHeap(), 0 );
1512 if (!ret) {
1513 DWORD size = FIELD_OFFSET( MIB_UDPTABLE, table[table->dwNumEntries] );
1514 if (!pUdpTable || *pdwSize < size) {
1515 *pdwSize = size;
1516 ret = ERROR_INSUFFICIENT_BUFFER;
1518 else {
1519 *pdwSize = size;
1520 memcpy(pUdpTable, table, size);
1522 HeapFree(GetProcessHeap(), 0, table);
1524 TRACE("returning %d\n", ret);
1525 return ret;
1529 /******************************************************************
1530 * GetUniDirectionalAdapterInfo (IPHLPAPI.@)
1532 * This is a Win98-only function to get information on "unidirectional"
1533 * adapters. Since this is pretty nonsensical in other contexts, it
1534 * never returns anything.
1536 * PARAMS
1537 * pIPIfInfo [Out] buffer for adapter infos
1538 * dwOutBufLen [Out] length of the output buffer
1540 * RETURNS
1541 * Success: NO_ERROR
1542 * Failure: error code from winerror.h
1544 * FIXME
1545 * Stub, returns ERROR_NOT_SUPPORTED.
1547 DWORD WINAPI GetUniDirectionalAdapterInfo(PIP_UNIDIRECTIONAL_ADAPTER_ADDRESS pIPIfInfo, PULONG dwOutBufLen)
1549 TRACE("pIPIfInfo %p, dwOutBufLen %p\n", pIPIfInfo, dwOutBufLen);
1550 /* a unidirectional adapter?? not bloody likely! */
1551 return ERROR_NOT_SUPPORTED;
1555 /******************************************************************
1556 * IpReleaseAddress (IPHLPAPI.@)
1558 * Release an IP obtained through DHCP,
1560 * PARAMS
1561 * AdapterInfo [In] adapter to release IP address
1563 * RETURNS
1564 * Success: NO_ERROR
1565 * Failure: error code from winerror.h
1567 * NOTES
1568 * Since GetAdaptersInfo never returns adapters that have DHCP enabled,
1569 * this function does nothing.
1571 * FIXME
1572 * Stub, returns ERROR_NOT_SUPPORTED.
1574 DWORD WINAPI IpReleaseAddress(PIP_ADAPTER_INDEX_MAP AdapterInfo)
1576 TRACE("AdapterInfo %p\n", AdapterInfo);
1577 /* not a stub, never going to support this (and I never mark an adapter as
1578 DHCP enabled, see GetAdaptersInfo, so this should never get called) */
1579 return ERROR_NOT_SUPPORTED;
1583 /******************************************************************
1584 * IpRenewAddress (IPHLPAPI.@)
1586 * Renew an IP obtained through DHCP.
1588 * PARAMS
1589 * AdapterInfo [In] adapter to renew IP address
1591 * RETURNS
1592 * Success: NO_ERROR
1593 * Failure: error code from winerror.h
1595 * NOTES
1596 * Since GetAdaptersInfo never returns adapters that have DHCP enabled,
1597 * this function does nothing.
1599 * FIXME
1600 * Stub, returns ERROR_NOT_SUPPORTED.
1602 DWORD WINAPI IpRenewAddress(PIP_ADAPTER_INDEX_MAP AdapterInfo)
1604 TRACE("AdapterInfo %p\n", AdapterInfo);
1605 /* not a stub, never going to support this (and I never mark an adapter as
1606 DHCP enabled, see GetAdaptersInfo, so this should never get called) */
1607 return ERROR_NOT_SUPPORTED;
1611 /******************************************************************
1612 * NotifyAddrChange (IPHLPAPI.@)
1614 * Notify caller whenever the ip-interface map is changed.
1616 * PARAMS
1617 * Handle [Out] handle usable in asynchronous notification
1618 * overlapped [In] overlapped structure that notifies the caller
1620 * RETURNS
1621 * Success: NO_ERROR
1622 * Failure: error code from winerror.h
1624 * FIXME
1625 * Stub, returns ERROR_NOT_SUPPORTED.
1627 DWORD WINAPI NotifyAddrChange(PHANDLE Handle, LPOVERLAPPED overlapped)
1629 FIXME("(Handle %p, overlapped %p): stub\n", Handle, overlapped);
1630 return ERROR_NOT_SUPPORTED;
1634 /******************************************************************
1635 * NotifyRouteChange (IPHLPAPI.@)
1637 * Notify caller whenever the ip routing table is changed.
1639 * PARAMS
1640 * Handle [Out] handle usable in asynchronous notification
1641 * overlapped [In] overlapped structure that notifies the caller
1643 * RETURNS
1644 * Success: NO_ERROR
1645 * Failure: error code from winerror.h
1647 * FIXME
1648 * Stub, returns ERROR_NOT_SUPPORTED.
1650 DWORD WINAPI NotifyRouteChange(PHANDLE Handle, LPOVERLAPPED overlapped)
1652 FIXME("(Handle %p, overlapped %p): stub\n", Handle, overlapped);
1653 return ERROR_NOT_SUPPORTED;
1657 /******************************************************************
1658 * SendARP (IPHLPAPI.@)
1660 * Send an ARP request.
1662 * PARAMS
1663 * DestIP [In] attempt to obtain this IP
1664 * SrcIP [In] optional sender IP address
1665 * pMacAddr [Out] buffer for the mac address
1666 * PhyAddrLen [In/Out] length of the output buffer
1668 * RETURNS
1669 * Success: NO_ERROR
1670 * Failure: error code from winerror.h
1672 * FIXME
1673 * Stub, returns ERROR_NOT_SUPPORTED.
1675 DWORD WINAPI SendARP(IPAddr DestIP, IPAddr SrcIP, PULONG pMacAddr, PULONG PhyAddrLen)
1677 FIXME("(DestIP 0x%08x, SrcIP 0x%08x, pMacAddr %p, PhyAddrLen %p): stub\n",
1678 DestIP, SrcIP, pMacAddr, PhyAddrLen);
1679 return ERROR_NOT_SUPPORTED;
1683 /******************************************************************
1684 * SetIfEntry (IPHLPAPI.@)
1686 * Set the administrative status of an interface.
1688 * PARAMS
1689 * pIfRow [In] dwAdminStatus member specifies the new status.
1691 * RETURNS
1692 * Success: NO_ERROR
1693 * Failure: error code from winerror.h
1695 * FIXME
1696 * Stub, returns ERROR_NOT_SUPPORTED.
1698 DWORD WINAPI SetIfEntry(PMIB_IFROW pIfRow)
1700 FIXME("(pIfRow %p): stub\n", pIfRow);
1701 /* this is supposed to set an interface administratively up or down.
1702 Could do SIOCSIFFLAGS and set/clear IFF_UP, but, not sure I want to, and
1703 this sort of down is indistinguishable from other sorts of down (e.g. no
1704 link). */
1705 return ERROR_NOT_SUPPORTED;
1709 /******************************************************************
1710 * SetIpForwardEntry (IPHLPAPI.@)
1712 * Modify an existing route.
1714 * PARAMS
1715 * pRoute [In] route with the new information
1717 * RETURNS
1718 * Success: NO_ERROR
1719 * Failure: error code from winerror.h
1721 * FIXME
1722 * Stub, returns NO_ERROR.
1724 DWORD WINAPI SetIpForwardEntry(PMIB_IPFORWARDROW pRoute)
1726 FIXME("(pRoute %p): stub\n", pRoute);
1727 /* this is to add a route entry, how's it distinguishable from
1728 CreateIpForwardEntry?
1729 could use SIOCADDRT, not sure I want to */
1730 return 0;
1734 /******************************************************************
1735 * SetIpNetEntry (IPHLPAPI.@)
1737 * Modify an existing ARP entry.
1739 * PARAMS
1740 * pArpEntry [In] ARP entry with the new information
1742 * RETURNS
1743 * Success: NO_ERROR
1744 * Failure: error code from winerror.h
1746 * FIXME
1747 * Stub, returns NO_ERROR.
1749 DWORD WINAPI SetIpNetEntry(PMIB_IPNETROW pArpEntry)
1751 FIXME("(pArpEntry %p): stub\n", pArpEntry);
1752 /* same as CreateIpNetEntry here, could use SIOCSARP, not sure I want to */
1753 return 0;
1757 /******************************************************************
1758 * SetIpStatistics (IPHLPAPI.@)
1760 * Toggle IP forwarding and det the default TTL value.
1762 * PARAMS
1763 * pIpStats [In] IP statistics with the new information
1765 * RETURNS
1766 * Success: NO_ERROR
1767 * Failure: error code from winerror.h
1769 * FIXME
1770 * Stub, returns NO_ERROR.
1772 DWORD WINAPI SetIpStatistics(PMIB_IPSTATS pIpStats)
1774 FIXME("(pIpStats %p): stub\n", pIpStats);
1775 return 0;
1779 /******************************************************************
1780 * SetIpTTL (IPHLPAPI.@)
1782 * Set the default TTL value.
1784 * PARAMS
1785 * nTTL [In] new TTL value
1787 * RETURNS
1788 * Success: NO_ERROR
1789 * Failure: error code from winerror.h
1791 * FIXME
1792 * Stub, returns NO_ERROR.
1794 DWORD WINAPI SetIpTTL(UINT nTTL)
1796 FIXME("(nTTL %d): stub\n", nTTL);
1797 /* could echo nTTL > /proc/net/sys/net/ipv4/ip_default_ttl, not sure I
1798 want to. Could map EACCESS to ERROR_ACCESS_DENIED, I suppose */
1799 return 0;
1803 /******************************************************************
1804 * SetTcpEntry (IPHLPAPI.@)
1806 * Set the state of a TCP connection.
1808 * PARAMS
1809 * pTcpRow [In] specifies connection with new state
1811 * RETURNS
1812 * Success: NO_ERROR
1813 * Failure: error code from winerror.h
1815 * FIXME
1816 * Stub, returns NO_ERROR.
1818 DWORD WINAPI SetTcpEntry(PMIB_TCPROW pTcpRow)
1820 FIXME("(pTcpRow %p): stub\n", pTcpRow);
1821 return 0;
1825 /******************************************************************
1826 * UnenableRouter (IPHLPAPI.@)
1828 * Decrement the IP-forwarding reference count. Turn off IP-forwarding
1829 * if it reaches zero.
1831 * PARAMS
1832 * pOverlapped [In/Out] should be the same as in EnableRouter()
1833 * lpdwEnableCount [Out] optional, receives reference count
1835 * RETURNS
1836 * Success: NO_ERROR
1837 * Failure: error code from winerror.h
1839 * FIXME
1840 * Stub, returns ERROR_NOT_SUPPORTED.
1842 DWORD WINAPI UnenableRouter(OVERLAPPED * pOverlapped, LPDWORD lpdwEnableCount)
1844 FIXME("(pOverlapped %p, lpdwEnableCount %p): stub\n", pOverlapped,
1845 lpdwEnableCount);
1846 /* could echo "0" > /proc/net/sys/net/ipv4/ip_forward, not sure I want to
1847 could map EACCESS to ERROR_ACCESS_DENIED, I suppose
1849 return ERROR_NOT_SUPPORTED;