dplayx: WIP Starting to set dplay Send up
[wine/gsoc_dplay.git] / dlls / iphlpapi / iphlpapi_main.c
blob522d1d5fac40b56ff81db9b5ae2c62465ccc8042
1 /*
2 * iphlpapi dll implementation
4 * Copyright (C) 2003,2006 Juan Lang
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
23 #include <stdarg.h>
24 #include <stdlib.h>
25 #include <sys/types.h>
26 #ifdef HAVE_SYS_SOCKET_H
27 #include <sys/socket.h>
28 #endif
29 #ifdef HAVE_NET_IF_H
30 #include <net/if.h>
31 #endif
32 #ifdef HAVE_NETINET_IN_H
33 # include <netinet/in.h>
34 #endif
35 #ifdef HAVE_ARPA_INET_H
36 # include <arpa/inet.h>
37 #endif
38 #ifdef HAVE_ARPA_NAMESER_H
39 # include <arpa/nameser.h>
40 #endif
41 #ifdef HAVE_RESOLV_H
42 # include <resolv.h>
43 #endif
45 #define NONAMELESSUNION
46 #define NONAMELESSSTRUCT
47 #include "windef.h"
48 #include "winbase.h"
49 #include "winreg.h"
50 #define USE_WS_PREFIX
51 #include "winsock2.h"
52 #include "iphlpapi.h"
53 #include "ifenum.h"
54 #include "ipstats.h"
55 #include "ipifcons.h"
57 #include "wine/debug.h"
59 WINE_DEFAULT_DEBUG_CHANNEL(iphlpapi);
61 #ifndef IF_NAMESIZE
62 #define IF_NAMESIZE 16
63 #endif
65 #ifndef INADDR_NONE
66 #define INADDR_NONE ~0UL
67 #endif
69 static int resolver_initialised;
71 /* call res_init() just once because of a bug in Mac OS X 10.4 */
72 static void initialise_resolver(void)
74 if (!resolver_initialised)
76 res_init();
77 resolver_initialised = 1;
81 BOOL WINAPI DllMain (HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
83 switch (fdwReason) {
84 case DLL_PROCESS_ATTACH:
85 DisableThreadLibraryCalls( hinstDLL );
86 break;
88 case DLL_PROCESS_DETACH:
89 break;
91 return TRUE;
94 /******************************************************************
95 * AddIPAddress (IPHLPAPI.@)
97 * Add an IP address to an adapter.
99 * PARAMS
100 * Address [In] IP address to add to the adapter
101 * IpMask [In] subnet mask for the IP address
102 * IfIndex [In] adapter index to add the address
103 * NTEContext [Out] Net Table Entry (NTE) context for the IP address
104 * NTEInstance [Out] NTE instance for the IP address
106 * RETURNS
107 * Success: NO_ERROR
108 * Failure: error code from winerror.h
110 * FIXME
111 * Stub. Currently returns ERROR_NOT_SUPPORTED.
113 DWORD WINAPI AddIPAddress(IPAddr Address, IPMask IpMask, DWORD IfIndex, PULONG NTEContext, PULONG NTEInstance)
115 FIXME(":stub\n");
116 return ERROR_NOT_SUPPORTED;
120 /******************************************************************
121 * AllocateAndGetIfTableFromStack (IPHLPAPI.@)
123 * Get table of local interfaces.
124 * Like GetIfTable(), but allocate the returned table from heap.
126 * PARAMS
127 * ppIfTable [Out] pointer into which the MIB_IFTABLE is
128 * allocated and returned.
129 * bOrder [In] whether to sort the table
130 * heap [In] heap from which the table is allocated
131 * flags [In] flags to HeapAlloc
133 * RETURNS
134 * ERROR_INVALID_PARAMETER if ppIfTable is NULL, whatever
135 * GetIfTable() returns otherwise.
137 DWORD WINAPI AllocateAndGetIfTableFromStack(PMIB_IFTABLE *ppIfTable,
138 BOOL bOrder, HANDLE heap, DWORD flags)
140 DWORD ret;
142 TRACE("ppIfTable %p, bOrder %d, heap %p, flags 0x%08x\n", ppIfTable,
143 bOrder, heap, flags);
144 if (!ppIfTable)
145 ret = ERROR_INVALID_PARAMETER;
146 else {
147 DWORD dwSize = 0;
149 ret = GetIfTable(*ppIfTable, &dwSize, bOrder);
150 if (ret == ERROR_INSUFFICIENT_BUFFER) {
151 *ppIfTable = HeapAlloc(heap, flags, dwSize);
152 ret = GetIfTable(*ppIfTable, &dwSize, bOrder);
155 TRACE("returning %d\n", ret);
156 return ret;
160 static int IpAddrTableSorter(const void *a, const void *b)
162 int ret;
164 if (a && b)
165 ret = ((const MIB_IPADDRROW*)a)->dwAddr - ((const MIB_IPADDRROW*)b)->dwAddr;
166 else
167 ret = 0;
168 return ret;
172 /******************************************************************
173 * AllocateAndGetIpAddrTableFromStack (IPHLPAPI.@)
175 * Get interface-to-IP address mapping table.
176 * Like GetIpAddrTable(), but allocate the returned table from heap.
178 * PARAMS
179 * ppIpAddrTable [Out] pointer into which the MIB_IPADDRTABLE is
180 * allocated and returned.
181 * bOrder [In] whether to sort the table
182 * heap [In] heap from which the table is allocated
183 * flags [In] flags to HeapAlloc
185 * RETURNS
186 * ERROR_INVALID_PARAMETER if ppIpAddrTable is NULL, other error codes on
187 * failure, NO_ERROR on success.
189 DWORD WINAPI AllocateAndGetIpAddrTableFromStack(PMIB_IPADDRTABLE *ppIpAddrTable,
190 BOOL bOrder, HANDLE heap, DWORD flags)
192 DWORD ret;
194 TRACE("ppIpAddrTable %p, bOrder %d, heap %p, flags 0x%08x\n",
195 ppIpAddrTable, bOrder, heap, flags);
196 ret = getIPAddrTable(ppIpAddrTable, heap, flags);
197 if (!ret && bOrder)
198 qsort((*ppIpAddrTable)->table, (*ppIpAddrTable)->dwNumEntries,
199 sizeof(MIB_IPADDRROW), IpAddrTableSorter);
200 TRACE("returning %d\n", ret);
201 return ret;
205 /******************************************************************
206 * CreateIpForwardEntry (IPHLPAPI.@)
208 * Create a route in the local computer's IP table.
210 * PARAMS
211 * pRoute [In] new route information
213 * RETURNS
214 * Success: NO_ERROR
215 * Failure: error code from winerror.h
217 * FIXME
218 * Stub, always returns NO_ERROR.
220 DWORD WINAPI CreateIpForwardEntry(PMIB_IPFORWARDROW pRoute)
222 FIXME("(pRoute %p): stub\n", pRoute);
223 /* could use SIOCADDRT, not sure I want to */
224 return 0;
228 /******************************************************************
229 * CreateIpNetEntry (IPHLPAPI.@)
231 * Create entry in the ARP table.
233 * PARAMS
234 * pArpEntry [In] new ARP entry
236 * RETURNS
237 * Success: NO_ERROR
238 * Failure: error code from winerror.h
240 * FIXME
241 * Stub, always returns NO_ERROR.
243 DWORD WINAPI CreateIpNetEntry(PMIB_IPNETROW pArpEntry)
245 FIXME("(pArpEntry %p)\n", pArpEntry);
246 /* could use SIOCSARP on systems that support it, not sure I want to */
247 return 0;
251 /******************************************************************
252 * CreateProxyArpEntry (IPHLPAPI.@)
254 * Create a Proxy ARP (PARP) entry for an IP address.
256 * PARAMS
257 * dwAddress [In] IP address for which this computer acts as a proxy.
258 * dwMask [In] subnet mask for dwAddress
259 * dwIfIndex [In] interface index
261 * RETURNS
262 * Success: NO_ERROR
263 * Failure: error code from winerror.h
265 * FIXME
266 * Stub, returns ERROR_NOT_SUPPORTED.
268 DWORD WINAPI CreateProxyArpEntry(DWORD dwAddress, DWORD dwMask, DWORD dwIfIndex)
270 FIXME("(dwAddress 0x%08x, dwMask 0x%08x, dwIfIndex 0x%08x): stub\n",
271 dwAddress, dwMask, dwIfIndex);
272 return ERROR_NOT_SUPPORTED;
276 /******************************************************************
277 * DeleteIPAddress (IPHLPAPI.@)
279 * Delete an IP address added with AddIPAddress().
281 * PARAMS
282 * NTEContext [In] NTE context from AddIPAddress();
284 * RETURNS
285 * Success: NO_ERROR
286 * Failure: error code from winerror.h
288 * FIXME
289 * Stub, returns ERROR_NOT_SUPPORTED.
291 DWORD WINAPI DeleteIPAddress(ULONG NTEContext)
293 FIXME("(NTEContext %d): stub\n", NTEContext);
294 return ERROR_NOT_SUPPORTED;
298 /******************************************************************
299 * DeleteIpForwardEntry (IPHLPAPI.@)
301 * Delete a route.
303 * PARAMS
304 * pRoute [In] route to delete
306 * RETURNS
307 * Success: NO_ERROR
308 * Failure: error code from winerror.h
310 * FIXME
311 * Stub, returns NO_ERROR.
313 DWORD WINAPI DeleteIpForwardEntry(PMIB_IPFORWARDROW pRoute)
315 FIXME("(pRoute %p): stub\n", pRoute);
316 /* could use SIOCDELRT, not sure I want to */
317 return 0;
321 /******************************************************************
322 * DeleteIpNetEntry (IPHLPAPI.@)
324 * Delete an ARP entry.
326 * PARAMS
327 * pArpEntry [In] ARP entry to delete
329 * RETURNS
330 * Success: NO_ERROR
331 * Failure: error code from winerror.h
333 * FIXME
334 * Stub, returns NO_ERROR.
336 DWORD WINAPI DeleteIpNetEntry(PMIB_IPNETROW pArpEntry)
338 FIXME("(pArpEntry %p): stub\n", pArpEntry);
339 /* could use SIOCDARP on systems that support it, not sure I want to */
340 return 0;
344 /******************************************************************
345 * DeleteProxyArpEntry (IPHLPAPI.@)
347 * Delete a Proxy ARP entry.
349 * PARAMS
350 * dwAddress [In] IP address for which this computer acts as a proxy.
351 * dwMask [In] subnet mask for dwAddress
352 * dwIfIndex [In] interface index
354 * RETURNS
355 * Success: NO_ERROR
356 * Failure: error code from winerror.h
358 * FIXME
359 * Stub, returns ERROR_NOT_SUPPORTED.
361 DWORD WINAPI DeleteProxyArpEntry(DWORD dwAddress, DWORD dwMask, DWORD dwIfIndex)
363 FIXME("(dwAddress 0x%08x, dwMask 0x%08x, dwIfIndex 0x%08x): stub\n",
364 dwAddress, dwMask, dwIfIndex);
365 return ERROR_NOT_SUPPORTED;
369 /******************************************************************
370 * EnableRouter (IPHLPAPI.@)
372 * Turn on ip forwarding.
374 * PARAMS
375 * pHandle [In/Out]
376 * pOverlapped [In/Out] hEvent member should contain a valid handle.
378 * RETURNS
379 * Success: ERROR_IO_PENDING
380 * Failure: error code from winerror.h
382 * FIXME
383 * Stub, returns ERROR_NOT_SUPPORTED.
385 DWORD WINAPI EnableRouter(HANDLE * pHandle, OVERLAPPED * pOverlapped)
387 FIXME("(pHandle %p, pOverlapped %p): stub\n", pHandle, pOverlapped);
388 /* could echo "1" > /proc/net/sys/net/ipv4/ip_forward, not sure I want to
389 could map EACCESS to ERROR_ACCESS_DENIED, I suppose
391 return ERROR_NOT_SUPPORTED;
395 /******************************************************************
396 * FlushIpNetTable (IPHLPAPI.@)
398 * Delete all ARP entries of an interface
400 * PARAMS
401 * dwIfIndex [In] interface index
403 * RETURNS
404 * Success: NO_ERROR
405 * Failure: error code from winerror.h
407 * FIXME
408 * Stub, returns ERROR_NOT_SUPPORTED.
410 DWORD WINAPI FlushIpNetTable(DWORD dwIfIndex)
412 FIXME("(dwIfIndex 0x%08x): stub\n", dwIfIndex);
413 /* this flushes the arp cache of the given index */
414 return ERROR_NOT_SUPPORTED;
418 /******************************************************************
419 * GetAdapterIndex (IPHLPAPI.@)
421 * Get interface index from its name.
423 * PARAMS
424 * AdapterName [In] unicode string with the adapter name
425 * IfIndex [Out] returns found interface index
427 * RETURNS
428 * Success: NO_ERROR
429 * Failure: error code from winerror.h
431 DWORD WINAPI GetAdapterIndex(LPWSTR AdapterName, PULONG IfIndex)
433 char adapterName[MAX_ADAPTER_NAME];
434 unsigned int i;
435 DWORD ret;
437 TRACE("(AdapterName %p, IfIndex %p)\n", AdapterName, IfIndex);
438 /* The adapter name is guaranteed not to have any unicode characters, so
439 * this translation is never lossy */
440 for (i = 0; i < sizeof(adapterName) - 1 && AdapterName[i]; i++)
441 adapterName[i] = (char)AdapterName[i];
442 adapterName[i] = '\0';
443 ret = getInterfaceIndexByName(adapterName, IfIndex);
444 TRACE("returning %d\n", ret);
445 return ret;
449 /******************************************************************
450 * GetAdaptersInfo (IPHLPAPI.@)
452 * Get information about adapters.
454 * PARAMS
455 * pAdapterInfo [Out] buffer for adapter infos
456 * pOutBufLen [In] length of output buffer
458 * RETURNS
459 * Success: NO_ERROR
460 * Failure: error code from winerror.h
462 DWORD WINAPI GetAdaptersInfo(PIP_ADAPTER_INFO pAdapterInfo, PULONG pOutBufLen)
464 DWORD ret;
466 TRACE("pAdapterInfo %p, pOutBufLen %p\n", pAdapterInfo, pOutBufLen);
467 if (!pOutBufLen)
468 ret = ERROR_INVALID_PARAMETER;
469 else {
470 DWORD numNonLoopbackInterfaces = getNumNonLoopbackInterfaces();
472 if (numNonLoopbackInterfaces > 0) {
473 DWORD numIPAddresses = getNumIPAddresses();
474 ULONG size;
476 /* This may slightly overestimate the amount of space needed, because
477 * the IP addresses include the loopback address, but it's easier
478 * to make sure there's more than enough space than to make sure there's
479 * precisely enough space.
481 size = sizeof(IP_ADAPTER_INFO) * numNonLoopbackInterfaces;
482 size += numIPAddresses * sizeof(IP_ADDR_STRING);
483 if (!pAdapterInfo || *pOutBufLen < size) {
484 *pOutBufLen = size;
485 ret = ERROR_BUFFER_OVERFLOW;
487 else {
488 InterfaceIndexTable *table = NULL;
489 PMIB_IPADDRTABLE ipAddrTable = NULL;
490 PMIB_IPFORWARDTABLE routeTable = NULL;
492 ret = getIPAddrTable(&ipAddrTable, GetProcessHeap(), 0);
493 if (!ret)
494 ret = AllocateAndGetIpForwardTableFromStack(&routeTable, FALSE, GetProcessHeap(), 0);
495 if (!ret)
496 table = getNonLoopbackInterfaceIndexTable();
497 if (table) {
498 size = sizeof(IP_ADAPTER_INFO) * table->numIndexes;
499 size += ipAddrTable->dwNumEntries * sizeof(IP_ADDR_STRING);
500 if (*pOutBufLen < size) {
501 *pOutBufLen = size;
502 ret = ERROR_INSUFFICIENT_BUFFER;
504 else {
505 DWORD ndx;
506 HKEY hKey;
507 BOOL winsEnabled = FALSE;
508 IP_ADDRESS_STRING primaryWINS, secondaryWINS;
509 PIP_ADDR_STRING nextIPAddr = (PIP_ADDR_STRING)((LPBYTE)pAdapterInfo
510 + numNonLoopbackInterfaces * sizeof(IP_ADAPTER_INFO));
512 memset(pAdapterInfo, 0, size);
513 /* @@ Wine registry key: HKCU\Software\Wine\Network */
514 if (RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Network",
515 &hKey) == ERROR_SUCCESS) {
516 DWORD size = sizeof(primaryWINS.String);
517 unsigned long addr;
519 RegQueryValueExA(hKey, "WinsServer", NULL, NULL,
520 (LPBYTE)primaryWINS.String, &size);
521 addr = inet_addr(primaryWINS.String);
522 if (addr != INADDR_NONE && addr != INADDR_ANY)
523 winsEnabled = TRUE;
524 size = sizeof(secondaryWINS.String);
525 RegQueryValueExA(hKey, "BackupWinsServer", NULL, NULL,
526 (LPBYTE)secondaryWINS.String, &size);
527 addr = inet_addr(secondaryWINS.String);
528 if (addr != INADDR_NONE && addr != INADDR_ANY)
529 winsEnabled = TRUE;
530 RegCloseKey(hKey);
532 for (ndx = 0; ndx < table->numIndexes; ndx++) {
533 PIP_ADAPTER_INFO ptr = &pAdapterInfo[ndx];
534 DWORD i;
535 PIP_ADDR_STRING currentIPAddr = &ptr->IpAddressList;
536 BOOL firstIPAddr = TRUE;
538 /* on Win98 this is left empty, but whatever */
539 getInterfaceNameByIndex(table->indexes[ndx], ptr->AdapterName);
540 getInterfaceNameByIndex(table->indexes[ndx], ptr->Description);
541 ptr->AddressLength = sizeof(ptr->Address);
542 getInterfacePhysicalByIndex(table->indexes[ndx],
543 &ptr->AddressLength, ptr->Address, &ptr->Type);
544 ptr->Index = table->indexes[ndx];
545 for (i = 0; i < ipAddrTable->dwNumEntries; i++) {
546 if (ipAddrTable->table[i].dwIndex == ptr->Index) {
547 if (firstIPAddr) {
548 toIPAddressString(ipAddrTable->table[i].dwAddr,
549 ptr->IpAddressList.IpAddress.String);
550 toIPAddressString(ipAddrTable->table[i].dwMask,
551 ptr->IpAddressList.IpMask.String);
552 firstIPAddr = FALSE;
554 else {
555 currentIPAddr->Next = nextIPAddr;
556 currentIPAddr = nextIPAddr;
557 toIPAddressString(ipAddrTable->table[i].dwAddr,
558 currentIPAddr->IpAddress.String);
559 toIPAddressString(ipAddrTable->table[i].dwMask,
560 currentIPAddr->IpMask.String);
561 nextIPAddr++;
565 /* Find first router through this interface, which we'll assume
566 * is the default gateway for this adapter */
567 for (i = 0; i < routeTable->dwNumEntries; i++)
568 if (routeTable->table[i].dwForwardIfIndex == ptr->Index
569 && routeTable->table[i].dwForwardType ==
570 MIB_IPROUTE_TYPE_INDIRECT)
571 toIPAddressString(routeTable->table[i].dwForwardNextHop,
572 ptr->GatewayList.IpAddress.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 ret = NO_ERROR;
587 HeapFree(GetProcessHeap(), 0, table);
589 else
590 ret = ERROR_OUTOFMEMORY;
591 HeapFree(GetProcessHeap(), 0, routeTable);
592 HeapFree(GetProcessHeap(), 0, ipAddrTable);
595 else
596 ret = ERROR_NO_DATA;
598 TRACE("returning %d\n", ret);
599 return ret;
602 static DWORD typeFromMibType(DWORD mib_type)
604 switch (mib_type)
606 case MIB_IF_TYPE_ETHERNET: return IF_TYPE_ETHERNET_CSMACD;
607 case MIB_IF_TYPE_TOKENRING: return IF_TYPE_ISO88025_TOKENRING;
608 case MIB_IF_TYPE_PPP: return IF_TYPE_PPP;
609 case MIB_IF_TYPE_LOOPBACK: return IF_TYPE_SOFTWARE_LOOPBACK;
610 default: return IF_TYPE_OTHER;
614 static ULONG addressesFromIndex(DWORD index, DWORD **addrs, ULONG *num_addrs)
616 ULONG ret, i, j;
617 MIB_IPADDRTABLE *at;
619 *num_addrs = 0;
620 if ((ret = getIPAddrTable(&at, GetProcessHeap(), 0))) return ret;
621 for (i = 0; i < at->dwNumEntries; i++)
623 if (at->table[i].dwIndex == index) (*num_addrs)++;
625 if (!(*addrs = HeapAlloc(GetProcessHeap(), 0, *num_addrs * sizeof(DWORD))))
627 HeapFree(GetProcessHeap(), 0, at);
628 return ERROR_OUTOFMEMORY;
630 for (i = 0, j = 0; i < at->dwNumEntries; i++)
632 if (at->table[i].dwIndex == index) (*addrs)[j++] = at->table[i].dwAddr;
634 HeapFree(GetProcessHeap(), 0, at);
635 return ERROR_SUCCESS;
638 static ULONG adapterAddressesFromIndex(DWORD index, IP_ADAPTER_ADDRESSES *aa, ULONG *size)
640 ULONG ret, i, num_addrs, total_size;
641 DWORD *addrs;
643 if ((ret = addressesFromIndex(index, &addrs, &num_addrs))) return ret;
645 total_size = sizeof(IP_ADAPTER_ADDRESSES);
646 total_size += IF_NAMESIZE;
647 total_size += IF_NAMESIZE * sizeof(WCHAR);
648 total_size += sizeof(IP_ADAPTER_UNICAST_ADDRESS) * num_addrs;
649 total_size += sizeof(struct sockaddr_in) * num_addrs;
651 if (aa && *size >= total_size)
653 char name[IF_NAMESIZE], *ptr = (char *)aa + sizeof(IP_ADAPTER_ADDRESSES), *src;
654 WCHAR *dst;
655 DWORD buflen, type, status;
657 memset(aa, 0, sizeof(IP_ADAPTER_ADDRESSES));
658 aa->u.s.Length = sizeof(IP_ADAPTER_ADDRESSES);
659 aa->u.s.IfIndex = index;
661 getInterfaceNameByIndex(index, name);
662 memcpy(ptr, name, IF_NAMESIZE);
663 aa->AdapterName = ptr;
664 ptr += IF_NAMESIZE;
665 aa->FriendlyName = (WCHAR *)ptr;
666 for (src = name, dst = (WCHAR *)ptr; *src; src++, dst++)
667 *dst = *src;
668 *dst++ = 0;
669 ptr = (char *)dst;
671 if (num_addrs)
673 IP_ADAPTER_UNICAST_ADDRESS *ua;
674 struct sockaddr_in *sa;
676 ua = aa->FirstUnicastAddress = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
677 for (i = 0; i < num_addrs; i++)
679 memset(ua, 0, sizeof(IP_ADAPTER_UNICAST_ADDRESS));
680 ua->u.s.Length = sizeof(IP_ADAPTER_UNICAST_ADDRESS);
681 ua->Address.iSockaddrLength = sizeof(struct sockaddr_in);
682 ua->Address.lpSockaddr = (SOCKADDR *)((char *)ua + ua->u.s.Length);
684 sa = (struct sockaddr_in *)ua->Address.lpSockaddr;
685 sa->sin_family = AF_INET;
686 sa->sin_addr.s_addr = addrs[i];
687 sa->sin_port = 0;
689 ptr += ua->u.s.Length + ua->Address.iSockaddrLength;
690 if (i < num_addrs - 1)
692 ua->Next = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
693 ua = ua->Next;
698 buflen = MAX_INTERFACE_PHYSADDR;
699 getInterfacePhysicalByIndex(index, &buflen, aa->PhysicalAddress, &type);
700 aa->PhysicalAddressLength = buflen;
701 aa->IfType = typeFromMibType(type);
703 getInterfaceMtuByName(name, &aa->Mtu);
705 getInterfaceStatusByName(name, &status);
706 if (status == MIB_IF_OPER_STATUS_OPERATIONAL) aa->OperStatus = IfOperStatusUp;
707 else if (status == MIB_IF_OPER_STATUS_NON_OPERATIONAL) aa->OperStatus = IfOperStatusDown;
708 else aa->OperStatus = IfOperStatusUnknown;
710 *size = total_size;
711 HeapFree(GetProcessHeap(), 0, addrs);
712 return ERROR_SUCCESS;
715 ULONG WINAPI GetAdaptersAddresses(ULONG family, ULONG flags, PVOID reserved,
716 PIP_ADAPTER_ADDRESSES aa, PULONG buflen)
718 InterfaceIndexTable *table;
719 ULONG i, size, total_size, ret = ERROR_NO_DATA;
721 if (!buflen) return ERROR_INVALID_PARAMETER;
723 if (family == AF_INET6 || family == AF_UNSPEC)
724 FIXME("no support for IPv6 addresses\n");
726 if (family != AF_INET && family != AF_UNSPEC) return ERROR_NO_DATA;
728 table = getInterfaceIndexTable();
729 if (!table || !table->numIndexes)
731 HeapFree(GetProcessHeap(), 0, table);
732 return ERROR_NO_DATA;
734 total_size = 0;
735 for (i = 0; i < table->numIndexes; i++)
737 size = 0;
738 if ((ret = adapterAddressesFromIndex(table->indexes[i], NULL, &size)))
740 HeapFree(GetProcessHeap(), 0, table);
741 return ret;
743 total_size += size;
745 if (aa && *buflen >= total_size)
747 ULONG bytes_left = size = total_size;
748 for (i = 0; i < table->numIndexes; i++)
750 if ((ret = adapterAddressesFromIndex(table->indexes[i], aa, &size)))
752 HeapFree(GetProcessHeap(), 0, table);
753 return ret;
755 if (i < table->numIndexes - 1)
757 aa->Next = (IP_ADAPTER_ADDRESSES *)((char *)aa + size);
758 aa = aa->Next;
759 size = bytes_left -= size;
762 ret = ERROR_SUCCESS;
764 if (*buflen < total_size) ret = ERROR_BUFFER_OVERFLOW;
765 *buflen = total_size;
767 TRACE("num adapters %u\n", table->numIndexes);
768 HeapFree(GetProcessHeap(), 0, table);
769 return ret;
772 /******************************************************************
773 * GetBestInterface (IPHLPAPI.@)
775 * Get the interface, with the best route for the given IP address.
777 * PARAMS
778 * dwDestAddr [In] IP address to search the interface for
779 * pdwBestIfIndex [Out] found best interface
781 * RETURNS
782 * Success: NO_ERROR
783 * Failure: error code from winerror.h
785 DWORD WINAPI GetBestInterface(IPAddr dwDestAddr, PDWORD pdwBestIfIndex)
787 struct WS_sockaddr_in sa_in;
788 memset(&sa_in, 0, sizeof(sa_in));
789 sa_in.sin_family = AF_INET;
790 sa_in.sin_addr.S_un.S_addr = dwDestAddr;
791 return GetBestInterfaceEx((struct WS_sockaddr *)&sa_in, pdwBestIfIndex);
794 /******************************************************************
795 * GetBestInterfaceEx (IPHLPAPI.@)
797 * Get the interface, with the best route for the given IP address.
799 * PARAMS
800 * dwDestAddr [In] IP address to search the interface for
801 * pdwBestIfIndex [Out] found best interface
803 * RETURNS
804 * Success: NO_ERROR
805 * Failure: error code from winerror.h
807 DWORD WINAPI GetBestInterfaceEx(struct WS_sockaddr *pDestAddr, PDWORD pdwBestIfIndex)
809 DWORD ret;
811 TRACE("pDestAddr %p, pdwBestIfIndex %p\n", pDestAddr, pdwBestIfIndex);
812 if (!pDestAddr || !pdwBestIfIndex)
813 ret = ERROR_INVALID_PARAMETER;
814 else {
815 MIB_IPFORWARDROW ipRow;
817 if (pDestAddr->sa_family == AF_INET) {
818 ret = GetBestRoute(((struct WS_sockaddr_in *)pDestAddr)->sin_addr.S_un.S_addr, 0, &ipRow);
819 if (ret == ERROR_SUCCESS)
820 *pdwBestIfIndex = ipRow.dwForwardIfIndex;
821 } else {
822 FIXME("address family %d not supported\n", pDestAddr->sa_family);
823 ret = ERROR_NOT_SUPPORTED;
826 TRACE("returning %d\n", ret);
827 return ret;
831 /******************************************************************
832 * GetBestRoute (IPHLPAPI.@)
834 * Get the best route for the given IP address.
836 * PARAMS
837 * dwDestAddr [In] IP address to search the best route for
838 * dwSourceAddr [In] optional source IP address
839 * pBestRoute [Out] found best route
841 * RETURNS
842 * Success: NO_ERROR
843 * Failure: error code from winerror.h
845 DWORD WINAPI GetBestRoute(DWORD dwDestAddr, DWORD dwSourceAddr, PMIB_IPFORWARDROW pBestRoute)
847 PMIB_IPFORWARDTABLE table;
848 DWORD ret;
850 TRACE("dwDestAddr 0x%08x, dwSourceAddr 0x%08x, pBestRoute %p\n", dwDestAddr,
851 dwSourceAddr, pBestRoute);
852 if (!pBestRoute)
853 return ERROR_INVALID_PARAMETER;
855 ret = AllocateAndGetIpForwardTableFromStack(&table, FALSE, GetProcessHeap(), 0);
856 if (!ret) {
857 DWORD ndx, matchedBits, matchedNdx = table->dwNumEntries;
859 for (ndx = 0, matchedBits = 0; ndx < table->dwNumEntries; ndx++) {
860 if (table->table[ndx].dwForwardType != MIB_IPROUTE_TYPE_INVALID &&
861 (dwDestAddr & table->table[ndx].dwForwardMask) ==
862 (table->table[ndx].dwForwardDest & table->table[ndx].dwForwardMask)) {
863 DWORD numShifts, mask;
865 for (numShifts = 0, mask = table->table[ndx].dwForwardMask;
866 mask && !(mask & 1); mask >>= 1, numShifts++)
868 if (numShifts > matchedBits) {
869 matchedBits = numShifts;
870 matchedNdx = ndx;
872 else if (!matchedBits && table->table[ndx].dwForwardType ==
873 MIB_IPROUTE_TYPE_INDIRECT) {
874 /* default to a default gateway */
875 matchedNdx = ndx;
879 if (matchedNdx < table->dwNumEntries) {
880 memcpy(pBestRoute, &table->table[matchedNdx], sizeof(MIB_IPFORWARDROW));
881 ret = ERROR_SUCCESS;
883 else {
884 /* No route matches, which can happen if there's no default route. */
885 ret = ERROR_HOST_UNREACHABLE;
887 HeapFree(GetProcessHeap(), 0, table);
889 TRACE("returning %d\n", ret);
890 return ret;
894 /******************************************************************
895 * GetFriendlyIfIndex (IPHLPAPI.@)
897 * Get a "friendly" version of IfIndex, which is one that doesn't
898 * have the top byte set. Doesn't validate whether IfIndex is a valid
899 * adapter index.
901 * PARAMS
902 * IfIndex [In] interface index to get the friendly one for
904 * RETURNS
905 * A friendly version of IfIndex.
907 DWORD WINAPI GetFriendlyIfIndex(DWORD IfIndex)
909 /* windows doesn't validate these, either, just makes sure the top byte is
910 cleared. I assume my ifenum module never gives an index with the top
911 byte set. */
912 TRACE("returning %d\n", IfIndex);
913 return IfIndex;
917 /******************************************************************
918 * GetIfEntry (IPHLPAPI.@)
920 * Get information about an interface.
922 * PARAMS
923 * pIfRow [In/Out] In: dwIndex of MIB_IFROW selects the interface.
924 * Out: interface information
926 * RETURNS
927 * Success: NO_ERROR
928 * Failure: error code from winerror.h
930 DWORD WINAPI GetIfEntry(PMIB_IFROW pIfRow)
932 DWORD ret;
933 char nameBuf[MAX_ADAPTER_NAME];
934 char *name;
936 TRACE("pIfRow %p\n", pIfRow);
937 if (!pIfRow)
938 return ERROR_INVALID_PARAMETER;
940 name = getInterfaceNameByIndex(pIfRow->dwIndex, nameBuf);
941 if (name) {
942 ret = getInterfaceEntryByName(name, pIfRow);
943 if (ret == NO_ERROR)
944 ret = getInterfaceStatsByName(name, pIfRow);
946 else
947 ret = ERROR_INVALID_DATA;
948 TRACE("returning %d\n", ret);
949 return ret;
953 static int IfTableSorter(const void *a, const void *b)
955 int ret;
957 if (a && b)
958 ret = ((const MIB_IFROW*)a)->dwIndex - ((const MIB_IFROW*)b)->dwIndex;
959 else
960 ret = 0;
961 return ret;
965 /******************************************************************
966 * GetIfTable (IPHLPAPI.@)
968 * Get a table of local interfaces.
970 * PARAMS
971 * pIfTable [Out] buffer for local interfaces table
972 * pdwSize [In/Out] length of output buffer
973 * bOrder [In] whether to sort the table
975 * RETURNS
976 * Success: NO_ERROR
977 * Failure: error code from winerror.h
979 * NOTES
980 * If pdwSize is less than required, the function will return
981 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the required byte
982 * size.
983 * If bOrder is true, the returned table will be sorted by interface index.
985 DWORD WINAPI GetIfTable(PMIB_IFTABLE pIfTable, PULONG pdwSize, BOOL bOrder)
987 DWORD ret;
989 TRACE("pIfTable %p, pdwSize %p, bOrder %d\n", pdwSize, pdwSize,
990 (DWORD)bOrder);
991 if (!pdwSize)
992 ret = ERROR_INVALID_PARAMETER;
993 else {
994 DWORD numInterfaces = getNumInterfaces();
995 ULONG size = sizeof(MIB_IFTABLE);
997 if (numInterfaces > 1)
998 size += (numInterfaces - 1) * sizeof(MIB_IFROW);
999 if (!pIfTable || *pdwSize < size) {
1000 *pdwSize = size;
1001 ret = ERROR_INSUFFICIENT_BUFFER;
1003 else {
1004 InterfaceIndexTable *table = getInterfaceIndexTable();
1006 if (table) {
1007 size = sizeof(MIB_IFTABLE);
1008 if (table->numIndexes > 1)
1009 size += (table->numIndexes - 1) * sizeof(MIB_IFROW);
1010 if (*pdwSize < size) {
1011 *pdwSize = size;
1012 ret = ERROR_INSUFFICIENT_BUFFER;
1014 else {
1015 DWORD ndx;
1017 *pdwSize = size;
1018 pIfTable->dwNumEntries = 0;
1019 for (ndx = 0; ndx < table->numIndexes; ndx++) {
1020 pIfTable->table[ndx].dwIndex = table->indexes[ndx];
1021 GetIfEntry(&pIfTable->table[ndx]);
1022 pIfTable->dwNumEntries++;
1024 if (bOrder)
1025 qsort(pIfTable->table, pIfTable->dwNumEntries, sizeof(MIB_IFROW),
1026 IfTableSorter);
1027 ret = NO_ERROR;
1029 HeapFree(GetProcessHeap(), 0, table);
1031 else
1032 ret = ERROR_OUTOFMEMORY;
1035 TRACE("returning %d\n", ret);
1036 return ret;
1040 /******************************************************************
1041 * GetInterfaceInfo (IPHLPAPI.@)
1043 * Get a list of network interface adapters.
1045 * PARAMS
1046 * pIfTable [Out] buffer for interface adapters
1047 * dwOutBufLen [Out] if buffer is too small, returns required size
1049 * RETURNS
1050 * Success: NO_ERROR
1051 * Failure: error code from winerror.h
1053 * BUGS
1054 * MSDN states this should return non-loopback interfaces only.
1056 DWORD WINAPI GetInterfaceInfo(PIP_INTERFACE_INFO pIfTable, PULONG dwOutBufLen)
1058 DWORD ret;
1060 TRACE("pIfTable %p, dwOutBufLen %p\n", pIfTable, dwOutBufLen);
1061 if (!dwOutBufLen)
1062 ret = ERROR_INVALID_PARAMETER;
1063 else {
1064 DWORD numInterfaces = getNumInterfaces();
1065 ULONG size = sizeof(IP_INTERFACE_INFO);
1067 if (numInterfaces > 1)
1068 size += (numInterfaces - 1) * sizeof(IP_ADAPTER_INDEX_MAP);
1069 if (!pIfTable || *dwOutBufLen < size) {
1070 *dwOutBufLen = size;
1071 ret = ERROR_INSUFFICIENT_BUFFER;
1073 else {
1074 InterfaceIndexTable *table = getInterfaceIndexTable();
1076 if (table) {
1077 size = sizeof(IP_INTERFACE_INFO);
1078 if (table->numIndexes > 1)
1079 size += (table->numIndexes - 1) * sizeof(IP_ADAPTER_INDEX_MAP);
1080 if (*dwOutBufLen < size) {
1081 *dwOutBufLen = size;
1082 ret = ERROR_INSUFFICIENT_BUFFER;
1084 else {
1085 DWORD ndx;
1086 char nameBuf[MAX_ADAPTER_NAME];
1088 *dwOutBufLen = size;
1089 pIfTable->NumAdapters = 0;
1090 for (ndx = 0; ndx < table->numIndexes; ndx++) {
1091 const char *walker, *name;
1092 WCHAR *assigner;
1094 pIfTable->Adapter[ndx].Index = table->indexes[ndx];
1095 name = getInterfaceNameByIndex(table->indexes[ndx], nameBuf);
1096 for (walker = name, assigner = pIfTable->Adapter[ndx].Name;
1097 walker && *walker &&
1098 assigner - pIfTable->Adapter[ndx].Name < MAX_ADAPTER_NAME - 1;
1099 walker++, assigner++)
1100 *assigner = *walker;
1101 *assigner = 0;
1102 pIfTable->NumAdapters++;
1104 ret = NO_ERROR;
1106 HeapFree(GetProcessHeap(), 0, table);
1108 else
1109 ret = ERROR_OUTOFMEMORY;
1112 TRACE("returning %d\n", ret);
1113 return ret;
1117 /******************************************************************
1118 * GetIpAddrTable (IPHLPAPI.@)
1120 * Get interface-to-IP address mapping table.
1122 * PARAMS
1123 * pIpAddrTable [Out] buffer for mapping table
1124 * pdwSize [In/Out] length of output buffer
1125 * bOrder [In] whether to sort the table
1127 * RETURNS
1128 * Success: NO_ERROR
1129 * Failure: error code from winerror.h
1131 * NOTES
1132 * If pdwSize is less than required, the function will return
1133 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the required byte
1134 * size.
1135 * If bOrder is true, the returned table will be sorted by the next hop and
1136 * an assortment of arbitrary parameters.
1138 DWORD WINAPI GetIpAddrTable(PMIB_IPADDRTABLE pIpAddrTable, PULONG pdwSize, BOOL bOrder)
1140 DWORD ret;
1142 TRACE("pIpAddrTable %p, pdwSize %p, bOrder %d\n", pIpAddrTable, pdwSize,
1143 (DWORD)bOrder);
1144 if (!pdwSize)
1145 ret = ERROR_INVALID_PARAMETER;
1146 else {
1147 PMIB_IPADDRTABLE table;
1149 ret = getIPAddrTable(&table, GetProcessHeap(), 0);
1150 if (ret == NO_ERROR)
1152 ULONG size = sizeof(MIB_IPADDRTABLE);
1154 if (table->dwNumEntries > 1)
1155 size += (table->dwNumEntries - 1) * sizeof(MIB_IPADDRROW);
1156 if (!pIpAddrTable || *pdwSize < size) {
1157 *pdwSize = size;
1158 ret = ERROR_INSUFFICIENT_BUFFER;
1160 else {
1161 *pdwSize = size;
1162 memcpy(pIpAddrTable, table, size);
1163 if (bOrder)
1164 qsort(pIpAddrTable->table, pIpAddrTable->dwNumEntries,
1165 sizeof(MIB_IPADDRROW), IpAddrTableSorter);
1166 ret = NO_ERROR;
1168 HeapFree(GetProcessHeap(), 0, table);
1171 TRACE("returning %d\n", ret);
1172 return ret;
1176 /******************************************************************
1177 * GetIpForwardTable (IPHLPAPI.@)
1179 * Get the route table.
1181 * PARAMS
1182 * pIpForwardTable [Out] buffer for route table
1183 * pdwSize [In/Out] length of output buffer
1184 * bOrder [In] whether to sort the table
1186 * RETURNS
1187 * Success: NO_ERROR
1188 * Failure: error code from winerror.h
1190 * NOTES
1191 * If pdwSize is less than required, the function will return
1192 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the required byte
1193 * size.
1194 * If bOrder is true, the returned table will be sorted by the next hop and
1195 * an assortment of arbitrary parameters.
1197 DWORD WINAPI GetIpForwardTable(PMIB_IPFORWARDTABLE pIpForwardTable, PULONG pdwSize, BOOL bOrder)
1199 DWORD ret;
1200 PMIB_IPFORWARDTABLE table;
1202 TRACE("pIpForwardTable %p, pdwSize %p, bOrder %d\n", pIpForwardTable, pdwSize, bOrder);
1204 if (!pdwSize) return ERROR_INVALID_PARAMETER;
1206 ret = AllocateAndGetIpForwardTableFromStack(&table, bOrder, GetProcessHeap(), 0);
1207 if (!ret) {
1208 DWORD size = FIELD_OFFSET( MIB_IPFORWARDTABLE, table[table->dwNumEntries] );
1209 if (!pIpForwardTable || *pdwSize < size) {
1210 *pdwSize = size;
1211 ret = ERROR_INSUFFICIENT_BUFFER;
1213 else {
1214 *pdwSize = size;
1215 memcpy(pIpForwardTable, table, size);
1217 HeapFree(GetProcessHeap(), 0, table);
1219 TRACE("returning %d\n", ret);
1220 return ret;
1224 /******************************************************************
1225 * GetIpNetTable (IPHLPAPI.@)
1227 * Get the IP-to-physical address mapping table.
1229 * PARAMS
1230 * pIpNetTable [Out] buffer for mapping table
1231 * pdwSize [In/Out] length of output buffer
1232 * bOrder [In] whether to sort the table
1234 * RETURNS
1235 * Success: NO_ERROR
1236 * Failure: error code from winerror.h
1238 * NOTES
1239 * If pdwSize is less than required, the function will return
1240 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the required byte
1241 * size.
1242 * If bOrder is true, the returned table will be sorted by IP address.
1244 DWORD WINAPI GetIpNetTable(PMIB_IPNETTABLE pIpNetTable, PULONG pdwSize, BOOL bOrder)
1246 DWORD ret;
1247 PMIB_IPNETTABLE table;
1249 TRACE("pIpNetTable %p, pdwSize %p, bOrder %d\n", pIpNetTable, pdwSize, bOrder);
1251 if (!pdwSize) return ERROR_INVALID_PARAMETER;
1253 ret = AllocateAndGetIpNetTableFromStack( &table, bOrder, GetProcessHeap(), 0 );
1254 if (!ret) {
1255 DWORD size = FIELD_OFFSET( MIB_IPNETTABLE, table[table->dwNumEntries] );
1256 if (!pIpNetTable || *pdwSize < size) {
1257 *pdwSize = size;
1258 ret = ERROR_INSUFFICIENT_BUFFER;
1260 else {
1261 *pdwSize = size;
1262 memcpy(pIpNetTable, table, size);
1264 HeapFree(GetProcessHeap(), 0, table);
1266 TRACE("returning %d\n", ret);
1267 return ret;
1271 /******************************************************************
1272 * GetNetworkParams (IPHLPAPI.@)
1274 * Get the network parameters for the local computer.
1276 * PARAMS
1277 * pFixedInfo [Out] buffer for network parameters
1278 * pOutBufLen [In/Out] length of output buffer
1280 * RETURNS
1281 * Success: NO_ERROR
1282 * Failure: error code from winerror.h
1284 * NOTES
1285 * If pOutBufLen is less than required, the function will return
1286 * ERROR_INSUFFICIENT_BUFFER, and pOutBufLen will be set to the required byte
1287 * size.
1289 DWORD WINAPI GetNetworkParams(PFIXED_INFO pFixedInfo, PULONG pOutBufLen)
1291 DWORD ret, size;
1292 LONG regReturn;
1293 HKEY hKey;
1295 TRACE("pFixedInfo %p, pOutBufLen %p\n", pFixedInfo, pOutBufLen);
1296 if (!pOutBufLen)
1297 return ERROR_INVALID_PARAMETER;
1299 initialise_resolver();
1300 size = sizeof(FIXED_INFO) + (_res.nscount > 0 ? (_res.nscount - 1) *
1301 sizeof(IP_ADDR_STRING) : 0);
1302 if (!pFixedInfo || *pOutBufLen < size) {
1303 *pOutBufLen = size;
1304 return ERROR_BUFFER_OVERFLOW;
1307 memset(pFixedInfo, 0, size);
1308 size = sizeof(pFixedInfo->HostName);
1309 GetComputerNameExA(ComputerNameDnsHostname, pFixedInfo->HostName, &size);
1310 size = sizeof(pFixedInfo->DomainName);
1311 GetComputerNameExA(ComputerNameDnsDomain, pFixedInfo->DomainName, &size);
1312 if (_res.nscount > 0) {
1313 PIP_ADDR_STRING ptr;
1314 int i;
1316 for (i = 0, ptr = &pFixedInfo->DnsServerList; i < _res.nscount && ptr;
1317 i++, ptr = ptr->Next) {
1318 toIPAddressString(_res.nsaddr_list[i].sin_addr.s_addr,
1319 ptr->IpAddress.String);
1320 if (i == _res.nscount - 1)
1321 ptr->Next = NULL;
1322 else if (i == 0)
1323 ptr->Next = (PIP_ADDR_STRING)((LPBYTE)pFixedInfo + sizeof(FIXED_INFO));
1324 else
1325 ptr->Next = (PIP_ADDR_STRING)((PBYTE)ptr + sizeof(IP_ADDR_STRING));
1328 pFixedInfo->NodeType = HYBRID_NODETYPE;
1329 regReturn = RegOpenKeyExA(HKEY_LOCAL_MACHINE,
1330 "SYSTEM\\CurrentControlSet\\Services\\VxD\\MSTCP", 0, KEY_READ, &hKey);
1331 if (regReturn != ERROR_SUCCESS)
1332 regReturn = RegOpenKeyExA(HKEY_LOCAL_MACHINE,
1333 "SYSTEM\\CurrentControlSet\\Services\\NetBT\\Parameters", 0, KEY_READ,
1334 &hKey);
1335 if (regReturn == ERROR_SUCCESS)
1337 DWORD size = sizeof(pFixedInfo->ScopeId);
1339 RegQueryValueExA(hKey, "ScopeID", NULL, NULL, (LPBYTE)pFixedInfo->ScopeId, &size);
1340 RegCloseKey(hKey);
1343 /* FIXME: can check whether routing's enabled in /proc/sys/net/ipv4/ip_forward
1344 I suppose could also check for a listener on port 53 to set EnableDns */
1345 ret = NO_ERROR;
1346 TRACE("returning %d\n", ret);
1347 return ret;
1351 /******************************************************************
1352 * GetNumberOfInterfaces (IPHLPAPI.@)
1354 * Get the number of interfaces.
1356 * PARAMS
1357 * pdwNumIf [Out] number of interfaces
1359 * RETURNS
1360 * NO_ERROR on success, ERROR_INVALID_PARAMETER if pdwNumIf is NULL.
1362 DWORD WINAPI GetNumberOfInterfaces(PDWORD pdwNumIf)
1364 DWORD ret;
1366 TRACE("pdwNumIf %p\n", pdwNumIf);
1367 if (!pdwNumIf)
1368 ret = ERROR_INVALID_PARAMETER;
1369 else {
1370 *pdwNumIf = getNumInterfaces();
1371 ret = NO_ERROR;
1373 TRACE("returning %d\n", ret);
1374 return ret;
1378 /******************************************************************
1379 * GetPerAdapterInfo (IPHLPAPI.@)
1381 * Get information about an adapter corresponding to an interface.
1383 * PARAMS
1384 * IfIndex [In] interface info
1385 * pPerAdapterInfo [Out] buffer for per adapter info
1386 * pOutBufLen [In/Out] length of output buffer
1388 * RETURNS
1389 * Success: NO_ERROR
1390 * Failure: error code from winerror.h
1392 * FIXME
1393 * Stub, returns empty IP_PER_ADAPTER_INFO in every case.
1395 DWORD WINAPI GetPerAdapterInfo(ULONG IfIndex, PIP_PER_ADAPTER_INFO pPerAdapterInfo, PULONG pOutBufLen)
1397 ULONG bytesNeeded = sizeof(IP_PER_ADAPTER_INFO);
1399 TRACE("(IfIndex %d, pPerAdapterInfo %p, pOutBufLen %p)\n", IfIndex, pPerAdapterInfo, pOutBufLen);
1401 if (!pOutBufLen) return ERROR_INVALID_PARAMETER;
1403 if (!pPerAdapterInfo || *pOutBufLen < bytesNeeded)
1405 *pOutBufLen = bytesNeeded;
1406 return ERROR_BUFFER_OVERFLOW;
1409 memset(pPerAdapterInfo, 0, bytesNeeded);
1410 return NO_ERROR;
1414 /******************************************************************
1415 * GetRTTAndHopCount (IPHLPAPI.@)
1417 * Get round-trip time (RTT) and hop count.
1419 * PARAMS
1421 * DestIpAddress [In] destination address to get the info for
1422 * HopCount [Out] retrieved hop count
1423 * MaxHops [In] maximum hops to search for the destination
1424 * RTT [Out] RTT in milliseconds
1426 * RETURNS
1427 * Success: TRUE
1428 * Failure: FALSE
1430 * FIXME
1431 * Stub, returns FALSE.
1433 BOOL WINAPI GetRTTAndHopCount(IPAddr DestIpAddress, PULONG HopCount, ULONG MaxHops, PULONG RTT)
1435 FIXME("(DestIpAddress 0x%08x, HopCount %p, MaxHops %d, RTT %p): stub\n",
1436 DestIpAddress, HopCount, MaxHops, RTT);
1437 return FALSE;
1441 /******************************************************************
1442 * GetTcpTable (IPHLPAPI.@)
1444 * Get the table of active TCP connections.
1446 * PARAMS
1447 * pTcpTable [Out] buffer for TCP connections table
1448 * pdwSize [In/Out] length of output buffer
1449 * bOrder [In] whether to order the table
1451 * RETURNS
1452 * Success: NO_ERROR
1453 * Failure: error code from winerror.h
1455 * NOTES
1456 * If pdwSize is less than required, the function will return
1457 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to
1458 * the required byte size.
1459 * If bOrder is true, the returned table will be sorted, first by
1460 * local address and port number, then by remote address and port
1461 * number.
1463 DWORD WINAPI GetTcpTable(PMIB_TCPTABLE pTcpTable, PDWORD pdwSize, BOOL bOrder)
1465 DWORD ret;
1466 PMIB_TCPTABLE table;
1468 TRACE("pTcpTable %p, pdwSize %p, bOrder %d\n", pTcpTable, pdwSize, bOrder);
1470 if (!pdwSize) return ERROR_INVALID_PARAMETER;
1472 ret = AllocateAndGetTcpTableFromStack(&table, bOrder, GetProcessHeap(), 0);
1473 if (!ret) {
1474 DWORD size = FIELD_OFFSET( MIB_TCPTABLE, table[table->dwNumEntries] );
1475 if (!pTcpTable || *pdwSize < size) {
1476 *pdwSize = size;
1477 ret = ERROR_INSUFFICIENT_BUFFER;
1479 else {
1480 *pdwSize = size;
1481 memcpy(pTcpTable, table, size);
1483 HeapFree(GetProcessHeap(), 0, table);
1485 TRACE("returning %d\n", ret);
1486 return ret;
1490 /******************************************************************
1491 * GetUdpTable (IPHLPAPI.@)
1493 * Get a table of active UDP connections.
1495 * PARAMS
1496 * pUdpTable [Out] buffer for UDP connections table
1497 * pdwSize [In/Out] length of output buffer
1498 * bOrder [In] whether to order the table
1500 * RETURNS
1501 * Success: NO_ERROR
1502 * Failure: error code from winerror.h
1504 * NOTES
1505 * If pdwSize is less than required, the function will return
1506 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the
1507 * required byte size.
1508 * If bOrder is true, the returned table will be sorted, first by
1509 * local address, then by local port number.
1511 DWORD WINAPI GetUdpTable(PMIB_UDPTABLE pUdpTable, PDWORD pdwSize, BOOL bOrder)
1513 DWORD ret;
1514 PMIB_UDPTABLE table;
1516 TRACE("pUdpTable %p, pdwSize %p, bOrder %d\n", pUdpTable, pdwSize, bOrder);
1518 if (!pdwSize) return ERROR_INVALID_PARAMETER;
1520 ret = AllocateAndGetUdpTableFromStack( &table, bOrder, GetProcessHeap(), 0 );
1521 if (!ret) {
1522 DWORD size = FIELD_OFFSET( MIB_UDPTABLE, table[table->dwNumEntries] );
1523 if (!pUdpTable || *pdwSize < size) {
1524 *pdwSize = size;
1525 ret = ERROR_INSUFFICIENT_BUFFER;
1527 else {
1528 *pdwSize = size;
1529 memcpy(pUdpTable, table, size);
1531 HeapFree(GetProcessHeap(), 0, table);
1533 TRACE("returning %d\n", ret);
1534 return ret;
1538 /******************************************************************
1539 * GetUniDirectionalAdapterInfo (IPHLPAPI.@)
1541 * This is a Win98-only function to get information on "unidirectional"
1542 * adapters. Since this is pretty nonsensical in other contexts, it
1543 * never returns anything.
1545 * PARAMS
1546 * pIPIfInfo [Out] buffer for adapter infos
1547 * dwOutBufLen [Out] length of the output buffer
1549 * RETURNS
1550 * Success: NO_ERROR
1551 * Failure: error code from winerror.h
1553 * FIXME
1554 * Stub, returns ERROR_NOT_SUPPORTED.
1556 DWORD WINAPI GetUniDirectionalAdapterInfo(PIP_UNIDIRECTIONAL_ADAPTER_ADDRESS pIPIfInfo, PULONG dwOutBufLen)
1558 TRACE("pIPIfInfo %p, dwOutBufLen %p\n", pIPIfInfo, dwOutBufLen);
1559 /* a unidirectional adapter?? not bloody likely! */
1560 return ERROR_NOT_SUPPORTED;
1564 /******************************************************************
1565 * IpReleaseAddress (IPHLPAPI.@)
1567 * Release an IP obtained through DHCP,
1569 * PARAMS
1570 * AdapterInfo [In] adapter to release IP address
1572 * RETURNS
1573 * Success: NO_ERROR
1574 * Failure: error code from winerror.h
1576 * NOTES
1577 * Since GetAdaptersInfo never returns adapters that have DHCP enabled,
1578 * this function does nothing.
1580 * FIXME
1581 * Stub, returns ERROR_NOT_SUPPORTED.
1583 DWORD WINAPI IpReleaseAddress(PIP_ADAPTER_INDEX_MAP AdapterInfo)
1585 TRACE("AdapterInfo %p\n", AdapterInfo);
1586 /* not a stub, never going to support this (and I never mark an adapter as
1587 DHCP enabled, see GetAdaptersInfo, so this should never get called) */
1588 return ERROR_NOT_SUPPORTED;
1592 /******************************************************************
1593 * IpRenewAddress (IPHLPAPI.@)
1595 * Renew an IP obtained through DHCP.
1597 * PARAMS
1598 * AdapterInfo [In] adapter to renew IP address
1600 * RETURNS
1601 * Success: NO_ERROR
1602 * Failure: error code from winerror.h
1604 * NOTES
1605 * Since GetAdaptersInfo never returns adapters that have DHCP enabled,
1606 * this function does nothing.
1608 * FIXME
1609 * Stub, returns ERROR_NOT_SUPPORTED.
1611 DWORD WINAPI IpRenewAddress(PIP_ADAPTER_INDEX_MAP AdapterInfo)
1613 TRACE("AdapterInfo %p\n", AdapterInfo);
1614 /* not a stub, never going to support this (and I never mark an adapter as
1615 DHCP enabled, see GetAdaptersInfo, so this should never get called) */
1616 return ERROR_NOT_SUPPORTED;
1620 /******************************************************************
1621 * NotifyAddrChange (IPHLPAPI.@)
1623 * Notify caller whenever the ip-interface map is changed.
1625 * PARAMS
1626 * Handle [Out] handle usable in asynchronous notification
1627 * overlapped [In] overlapped structure that notifies the caller
1629 * RETURNS
1630 * Success: NO_ERROR
1631 * Failure: error code from winerror.h
1633 * FIXME
1634 * Stub, returns ERROR_NOT_SUPPORTED.
1636 DWORD WINAPI NotifyAddrChange(PHANDLE Handle, LPOVERLAPPED overlapped)
1638 FIXME("(Handle %p, overlapped %p): stub\n", Handle, overlapped);
1639 return ERROR_NOT_SUPPORTED;
1643 /******************************************************************
1644 * NotifyRouteChange (IPHLPAPI.@)
1646 * Notify caller whenever the ip routing table is changed.
1648 * PARAMS
1649 * Handle [Out] handle usable in asynchronous notification
1650 * overlapped [In] overlapped structure that notifies the caller
1652 * RETURNS
1653 * Success: NO_ERROR
1654 * Failure: error code from winerror.h
1656 * FIXME
1657 * Stub, returns ERROR_NOT_SUPPORTED.
1659 DWORD WINAPI NotifyRouteChange(PHANDLE Handle, LPOVERLAPPED overlapped)
1661 FIXME("(Handle %p, overlapped %p): stub\n", Handle, overlapped);
1662 return ERROR_NOT_SUPPORTED;
1666 /******************************************************************
1667 * SendARP (IPHLPAPI.@)
1669 * Send an ARP request.
1671 * PARAMS
1672 * DestIP [In] attempt to obtain this IP
1673 * SrcIP [In] optional sender IP address
1674 * pMacAddr [Out] buffer for the mac address
1675 * PhyAddrLen [In/Out] length of the output buffer
1677 * RETURNS
1678 * Success: NO_ERROR
1679 * Failure: error code from winerror.h
1681 * FIXME
1682 * Stub, returns ERROR_NOT_SUPPORTED.
1684 DWORD WINAPI SendARP(IPAddr DestIP, IPAddr SrcIP, PULONG pMacAddr, PULONG PhyAddrLen)
1686 FIXME("(DestIP 0x%08x, SrcIP 0x%08x, pMacAddr %p, PhyAddrLen %p): stub\n",
1687 DestIP, SrcIP, pMacAddr, PhyAddrLen);
1688 return ERROR_NOT_SUPPORTED;
1692 /******************************************************************
1693 * SetIfEntry (IPHLPAPI.@)
1695 * Set the administrative status of an interface.
1697 * PARAMS
1698 * pIfRow [In] dwAdminStatus member specifies the new status.
1700 * RETURNS
1701 * Success: NO_ERROR
1702 * Failure: error code from winerror.h
1704 * FIXME
1705 * Stub, returns ERROR_NOT_SUPPORTED.
1707 DWORD WINAPI SetIfEntry(PMIB_IFROW pIfRow)
1709 FIXME("(pIfRow %p): stub\n", pIfRow);
1710 /* this is supposed to set an interface administratively up or down.
1711 Could do SIOCSIFFLAGS and set/clear IFF_UP, but, not sure I want to, and
1712 this sort of down is indistinguishable from other sorts of down (e.g. no
1713 link). */
1714 return ERROR_NOT_SUPPORTED;
1718 /******************************************************************
1719 * SetIpForwardEntry (IPHLPAPI.@)
1721 * Modify an existing route.
1723 * PARAMS
1724 * pRoute [In] route with the new information
1726 * RETURNS
1727 * Success: NO_ERROR
1728 * Failure: error code from winerror.h
1730 * FIXME
1731 * Stub, returns NO_ERROR.
1733 DWORD WINAPI SetIpForwardEntry(PMIB_IPFORWARDROW pRoute)
1735 FIXME("(pRoute %p): stub\n", pRoute);
1736 /* this is to add a route entry, how's it distinguishable from
1737 CreateIpForwardEntry?
1738 could use SIOCADDRT, not sure I want to */
1739 return 0;
1743 /******************************************************************
1744 * SetIpNetEntry (IPHLPAPI.@)
1746 * Modify an existing ARP entry.
1748 * PARAMS
1749 * pArpEntry [In] ARP entry with the new information
1751 * RETURNS
1752 * Success: NO_ERROR
1753 * Failure: error code from winerror.h
1755 * FIXME
1756 * Stub, returns NO_ERROR.
1758 DWORD WINAPI SetIpNetEntry(PMIB_IPNETROW pArpEntry)
1760 FIXME("(pArpEntry %p): stub\n", pArpEntry);
1761 /* same as CreateIpNetEntry here, could use SIOCSARP, not sure I want to */
1762 return 0;
1766 /******************************************************************
1767 * SetIpStatistics (IPHLPAPI.@)
1769 * Toggle IP forwarding and det the default TTL value.
1771 * PARAMS
1772 * pIpStats [In] IP statistics with the new information
1774 * RETURNS
1775 * Success: NO_ERROR
1776 * Failure: error code from winerror.h
1778 * FIXME
1779 * Stub, returns NO_ERROR.
1781 DWORD WINAPI SetIpStatistics(PMIB_IPSTATS pIpStats)
1783 FIXME("(pIpStats %p): stub\n", pIpStats);
1784 return 0;
1788 /******************************************************************
1789 * SetIpTTL (IPHLPAPI.@)
1791 * Set the default TTL value.
1793 * PARAMS
1794 * nTTL [In] new TTL value
1796 * RETURNS
1797 * Success: NO_ERROR
1798 * Failure: error code from winerror.h
1800 * FIXME
1801 * Stub, returns NO_ERROR.
1803 DWORD WINAPI SetIpTTL(UINT nTTL)
1805 FIXME("(nTTL %d): stub\n", nTTL);
1806 /* could echo nTTL > /proc/net/sys/net/ipv4/ip_default_ttl, not sure I
1807 want to. Could map EACCESS to ERROR_ACCESS_DENIED, I suppose */
1808 return 0;
1812 /******************************************************************
1813 * SetTcpEntry (IPHLPAPI.@)
1815 * Set the state of a TCP connection.
1817 * PARAMS
1818 * pTcpRow [In] specifies connection with new state
1820 * RETURNS
1821 * Success: NO_ERROR
1822 * Failure: error code from winerror.h
1824 * FIXME
1825 * Stub, returns NO_ERROR.
1827 DWORD WINAPI SetTcpEntry(PMIB_TCPROW pTcpRow)
1829 FIXME("(pTcpRow %p): stub\n", pTcpRow);
1830 return 0;
1834 /******************************************************************
1835 * UnenableRouter (IPHLPAPI.@)
1837 * Decrement the IP-forwarding reference count. Turn off IP-forwarding
1838 * if it reaches zero.
1840 * PARAMS
1841 * pOverlapped [In/Out] should be the same as in EnableRouter()
1842 * lpdwEnableCount [Out] optional, receives reference count
1844 * RETURNS
1845 * Success: NO_ERROR
1846 * Failure: error code from winerror.h
1848 * FIXME
1849 * Stub, returns ERROR_NOT_SUPPORTED.
1851 DWORD WINAPI UnenableRouter(OVERLAPPED * pOverlapped, LPDWORD lpdwEnableCount)
1853 FIXME("(pOverlapped %p, lpdwEnableCount %p): stub\n", pOverlapped,
1854 lpdwEnableCount);
1855 /* could echo "0" > /proc/net/sys/net/ipv4/ip_forward, not sure I want to
1856 could map EACCESS to ERROR_ACCESS_DENIED, I suppose
1858 return ERROR_NOT_SUPPORTED;