pop b0283e26815279d45d201d5585820bb1d1997663
[wine/hacks.git] / dlls / iphlpapi / iphlpapi_main.c
blob86008493611ada77798864414fce1edc87666d84
1 /*
2 * iphlpapi dll implementation
4 * Copyright (C) 2003,2006 Juan Lang
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
23 #include <stdarg.h>
24 #include <stdlib.h>
25 #include <sys/types.h>
26 #ifdef HAVE_SYS_SOCKET_H
27 #include <sys/socket.h>
28 #endif
29 #ifdef HAVE_NET_IF_H
30 #include <net/if.h>
31 #endif
32 #ifdef HAVE_NETINET_IN_H
33 # include <netinet/in.h>
34 #endif
35 #ifdef HAVE_ARPA_INET_H
36 # include <arpa/inet.h>
37 #endif
38 #ifdef HAVE_ARPA_NAMESER_H
39 # include <arpa/nameser.h>
40 #endif
41 #ifdef HAVE_RESOLV_H
42 # include <resolv.h>
43 #endif
45 #define NONAMELESSUNION
46 #define NONAMELESSSTRUCT
47 #include "windef.h"
48 #include "winbase.h"
49 #include "winreg.h"
50 #define USE_WS_PREFIX
51 #include "winsock2.h"
52 #include "ws2ipdef.h"
53 #include "iphlpapi.h"
54 #include "ifenum.h"
55 #include "ipstats.h"
56 #include "ipifcons.h"
58 #include "wine/debug.h"
60 WINE_DEFAULT_DEBUG_CHANNEL(iphlpapi);
62 #ifndef IF_NAMESIZE
63 #define IF_NAMESIZE 16
64 #endif
66 #ifndef INADDR_NONE
67 #define INADDR_NONE ~0UL
68 #endif
70 /* call res_init() just once because of a bug in Mac OS X 10.4 */
71 /* Call once per thread on systems that have per-thread _res. */
72 /* FIXME: should do same fix in dnsapi (or use dnsapi here?) */
73 static void initialise_resolver(void)
75 if ((_res.options & RES_INIT) == 0)
76 res_init();
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 v4addressesFromIndex(DWORD index, DWORD **addrs, ULONG *num_addrs)
614 ULONG ret, i, j;
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, j = 0; i < at->dwNumEntries; i++)
630 if (at->table[i].dwIndex == index) (*addrs)[j++] = at->table[i].dwAddr;
632 HeapFree(GetProcessHeap(), 0, at);
633 return ERROR_SUCCESS;
636 static ULONG adapterAddressesFromIndex(ULONG family, DWORD index, IP_ADAPTER_ADDRESSES *aa, ULONG *size)
638 ULONG ret, i, num_v4addrs = 0, num_v6addrs = 0, total_size;
639 DWORD *v4addrs = NULL;
640 SOCKET_ADDRESS *v6addrs = NULL;
642 if (family == AF_INET)
643 ret = v4addressesFromIndex(index, &v4addrs, &num_v4addrs);
644 else if (family == AF_INET6)
645 ret = v6addressesFromIndex(index, &v6addrs, &num_v6addrs);
646 else if (family == AF_UNSPEC)
648 ret = v4addressesFromIndex(index, &v4addrs, &num_v4addrs);
649 if (!ret)
650 ret = v6addressesFromIndex(index, &v6addrs, &num_v6addrs);
652 else
654 FIXME("address family %u unsupported\n", family);
655 ret = ERROR_NO_DATA;
657 if (ret) return ret;
659 total_size = sizeof(IP_ADAPTER_ADDRESSES);
660 total_size += IF_NAMESIZE;
661 total_size += IF_NAMESIZE * sizeof(WCHAR);
662 total_size += sizeof(IP_ADAPTER_UNICAST_ADDRESS) * num_v4addrs;
663 total_size += sizeof(struct sockaddr_in) * num_v4addrs;
664 total_size += sizeof(IP_ADAPTER_UNICAST_ADDRESS) * num_v6addrs;
665 total_size += sizeof(SOCKET_ADDRESS) * num_v6addrs;
666 for (i = 0; i < num_v6addrs; i++)
667 total_size += v6addrs[i].iSockaddrLength;
669 if (aa && *size >= total_size)
671 char name[IF_NAMESIZE], *ptr = (char *)aa + sizeof(IP_ADAPTER_ADDRESSES), *src;
672 WCHAR *dst;
673 DWORD buflen, type, status;
675 memset(aa, 0, sizeof(IP_ADAPTER_ADDRESSES));
676 aa->u.s.Length = sizeof(IP_ADAPTER_ADDRESSES);
677 aa->u.s.IfIndex = index;
679 getInterfaceNameByIndex(index, name);
680 memcpy(ptr, name, IF_NAMESIZE);
681 aa->AdapterName = ptr;
682 ptr += IF_NAMESIZE;
683 aa->FriendlyName = (WCHAR *)ptr;
684 for (src = name, dst = (WCHAR *)ptr; *src; src++, dst++)
685 *dst = *src;
686 *dst++ = 0;
687 ptr = (char *)dst;
689 if (num_v4addrs)
691 IP_ADAPTER_UNICAST_ADDRESS *ua;
692 struct sockaddr_in *sa;
694 ua = aa->FirstUnicastAddress = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
695 for (i = 0; i < num_v4addrs; i++)
697 memset(ua, 0, sizeof(IP_ADAPTER_UNICAST_ADDRESS));
698 ua->u.s.Length = sizeof(IP_ADAPTER_UNICAST_ADDRESS);
699 ua->Address.iSockaddrLength = sizeof(struct sockaddr_in);
700 ua->Address.lpSockaddr = (SOCKADDR *)((char *)ua + ua->u.s.Length);
702 sa = (struct sockaddr_in *)ua->Address.lpSockaddr;
703 sa->sin_family = AF_INET;
704 sa->sin_addr.s_addr = v4addrs[i];
705 sa->sin_port = 0;
707 ptr += ua->u.s.Length + ua->Address.iSockaddrLength;
708 if (i < num_v4addrs - 1)
710 ua->Next = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
711 ua = ua->Next;
715 if (num_v6addrs)
717 IP_ADAPTER_UNICAST_ADDRESS *ua;
718 struct WS_sockaddr_in6 *sa;
720 if (aa->FirstUnicastAddress)
722 for (ua = aa->FirstUnicastAddress; ua->Next; ua = ua->Next)
724 ua->Next = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
726 else
727 ua = aa->FirstUnicastAddress = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
728 for (i = 0; i < num_v6addrs; i++)
730 memset(ua, 0, sizeof(IP_ADAPTER_UNICAST_ADDRESS));
731 ua->u.s.Length = sizeof(IP_ADAPTER_UNICAST_ADDRESS);
732 ua->Address.iSockaddrLength = v6addrs[i].iSockaddrLength;
733 ua->Address.lpSockaddr = (SOCKADDR *)((char *)ua + ua->u.s.Length);
735 sa = (struct WS_sockaddr_in6 *)ua->Address.lpSockaddr;
736 memcpy(sa, v6addrs[i].lpSockaddr, sizeof(*sa));
738 ptr += ua->u.s.Length + ua->Address.iSockaddrLength;
739 if (i < num_v6addrs - 1)
741 ua->Next = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
742 ua = ua->Next;
747 buflen = MAX_INTERFACE_PHYSADDR;
748 getInterfacePhysicalByIndex(index, &buflen, aa->PhysicalAddress, &type);
749 aa->PhysicalAddressLength = buflen;
750 aa->IfType = typeFromMibType(type);
752 getInterfaceMtuByName(name, &aa->Mtu);
754 getInterfaceStatusByName(name, &status);
755 if (status == MIB_IF_OPER_STATUS_OPERATIONAL) aa->OperStatus = IfOperStatusUp;
756 else if (status == MIB_IF_OPER_STATUS_NON_OPERATIONAL) aa->OperStatus = IfOperStatusDown;
757 else aa->OperStatus = IfOperStatusUnknown;
759 *size = total_size;
760 HeapFree(GetProcessHeap(), 0, v6addrs);
761 HeapFree(GetProcessHeap(), 0, v4addrs);
762 return ERROR_SUCCESS;
765 ULONG WINAPI GetAdaptersAddresses(ULONG family, ULONG flags, PVOID reserved,
766 PIP_ADAPTER_ADDRESSES aa, PULONG buflen)
768 InterfaceIndexTable *table;
769 ULONG i, size, total_size, ret = ERROR_NO_DATA;
771 if (!buflen) return ERROR_INVALID_PARAMETER;
773 table = getInterfaceIndexTable();
774 if (!table || !table->numIndexes)
776 HeapFree(GetProcessHeap(), 0, table);
777 return ERROR_NO_DATA;
779 total_size = 0;
780 for (i = 0; i < table->numIndexes; i++)
782 size = 0;
783 if ((ret = adapterAddressesFromIndex(family, table->indexes[i], NULL, &size)))
785 HeapFree(GetProcessHeap(), 0, table);
786 return ret;
788 total_size += size;
790 if (aa && *buflen >= total_size)
792 ULONG bytes_left = size = total_size;
793 for (i = 0; i < table->numIndexes; i++)
795 if ((ret = adapterAddressesFromIndex(family, table->indexes[i], aa, &size)))
797 HeapFree(GetProcessHeap(), 0, table);
798 return ret;
800 if (i < table->numIndexes - 1)
802 aa->Next = (IP_ADAPTER_ADDRESSES *)((char *)aa + size);
803 aa = aa->Next;
804 size = bytes_left -= size;
807 ret = ERROR_SUCCESS;
809 if (*buflen < total_size) ret = ERROR_BUFFER_OVERFLOW;
810 *buflen = total_size;
812 TRACE("num adapters %u\n", table->numIndexes);
813 HeapFree(GetProcessHeap(), 0, table);
814 return ret;
817 /******************************************************************
818 * GetBestInterface (IPHLPAPI.@)
820 * Get the interface, with the best route for the given IP address.
822 * PARAMS
823 * dwDestAddr [In] IP address to search the interface for
824 * pdwBestIfIndex [Out] found best interface
826 * RETURNS
827 * Success: NO_ERROR
828 * Failure: error code from winerror.h
830 DWORD WINAPI GetBestInterface(IPAddr dwDestAddr, PDWORD pdwBestIfIndex)
832 struct WS_sockaddr_in sa_in;
833 memset(&sa_in, 0, sizeof(sa_in));
834 sa_in.sin_family = AF_INET;
835 sa_in.sin_addr.S_un.S_addr = dwDestAddr;
836 return GetBestInterfaceEx((struct WS_sockaddr *)&sa_in, pdwBestIfIndex);
839 /******************************************************************
840 * GetBestInterfaceEx (IPHLPAPI.@)
842 * Get the interface, with the best route for the given IP address.
844 * PARAMS
845 * dwDestAddr [In] IP address to search the interface for
846 * pdwBestIfIndex [Out] found best interface
848 * RETURNS
849 * Success: NO_ERROR
850 * Failure: error code from winerror.h
852 DWORD WINAPI GetBestInterfaceEx(struct WS_sockaddr *pDestAddr, PDWORD pdwBestIfIndex)
854 DWORD ret;
856 TRACE("pDestAddr %p, pdwBestIfIndex %p\n", pDestAddr, pdwBestIfIndex);
857 if (!pDestAddr || !pdwBestIfIndex)
858 ret = ERROR_INVALID_PARAMETER;
859 else {
860 MIB_IPFORWARDROW ipRow;
862 if (pDestAddr->sa_family == AF_INET) {
863 ret = GetBestRoute(((struct WS_sockaddr_in *)pDestAddr)->sin_addr.S_un.S_addr, 0, &ipRow);
864 if (ret == ERROR_SUCCESS)
865 *pdwBestIfIndex = ipRow.dwForwardIfIndex;
866 } else {
867 FIXME("address family %d not supported\n", pDestAddr->sa_family);
868 ret = ERROR_NOT_SUPPORTED;
871 TRACE("returning %d\n", ret);
872 return ret;
876 /******************************************************************
877 * GetBestRoute (IPHLPAPI.@)
879 * Get the best route for the given IP address.
881 * PARAMS
882 * dwDestAddr [In] IP address to search the best route for
883 * dwSourceAddr [In] optional source IP address
884 * pBestRoute [Out] found best route
886 * RETURNS
887 * Success: NO_ERROR
888 * Failure: error code from winerror.h
890 DWORD WINAPI GetBestRoute(DWORD dwDestAddr, DWORD dwSourceAddr, PMIB_IPFORWARDROW pBestRoute)
892 PMIB_IPFORWARDTABLE table;
893 DWORD ret;
895 TRACE("dwDestAddr 0x%08x, dwSourceAddr 0x%08x, pBestRoute %p\n", dwDestAddr,
896 dwSourceAddr, pBestRoute);
897 if (!pBestRoute)
898 return ERROR_INVALID_PARAMETER;
900 ret = AllocateAndGetIpForwardTableFromStack(&table, FALSE, GetProcessHeap(), 0);
901 if (!ret) {
902 DWORD ndx, matchedBits, matchedNdx = table->dwNumEntries;
904 for (ndx = 0, matchedBits = 0; ndx < table->dwNumEntries; ndx++) {
905 if (table->table[ndx].dwForwardType != MIB_IPROUTE_TYPE_INVALID &&
906 (dwDestAddr & table->table[ndx].dwForwardMask) ==
907 (table->table[ndx].dwForwardDest & table->table[ndx].dwForwardMask)) {
908 DWORD numShifts, mask;
910 for (numShifts = 0, mask = table->table[ndx].dwForwardMask;
911 mask && !(mask & 1); mask >>= 1, numShifts++)
913 if (numShifts > matchedBits) {
914 matchedBits = numShifts;
915 matchedNdx = ndx;
917 else if (!matchedBits) {
918 matchedNdx = ndx;
922 if (matchedNdx < table->dwNumEntries) {
923 memcpy(pBestRoute, &table->table[matchedNdx], sizeof(MIB_IPFORWARDROW));
924 ret = ERROR_SUCCESS;
926 else {
927 /* No route matches, which can happen if there's no default route. */
928 ret = ERROR_HOST_UNREACHABLE;
930 HeapFree(GetProcessHeap(), 0, table);
932 TRACE("returning %d\n", ret);
933 return ret;
937 /******************************************************************
938 * GetFriendlyIfIndex (IPHLPAPI.@)
940 * Get a "friendly" version of IfIndex, which is one that doesn't
941 * have the top byte set. Doesn't validate whether IfIndex is a valid
942 * adapter index.
944 * PARAMS
945 * IfIndex [In] interface index to get the friendly one for
947 * RETURNS
948 * A friendly version of IfIndex.
950 DWORD WINAPI GetFriendlyIfIndex(DWORD IfIndex)
952 /* windows doesn't validate these, either, just makes sure the top byte is
953 cleared. I assume my ifenum module never gives an index with the top
954 byte set. */
955 TRACE("returning %d\n", IfIndex);
956 return IfIndex;
960 /******************************************************************
961 * GetIfEntry (IPHLPAPI.@)
963 * Get information about an interface.
965 * PARAMS
966 * pIfRow [In/Out] In: dwIndex of MIB_IFROW selects the interface.
967 * Out: interface information
969 * RETURNS
970 * Success: NO_ERROR
971 * Failure: error code from winerror.h
973 DWORD WINAPI GetIfEntry(PMIB_IFROW pIfRow)
975 DWORD ret;
976 char nameBuf[MAX_ADAPTER_NAME];
977 char *name;
979 TRACE("pIfRow %p\n", pIfRow);
980 if (!pIfRow)
981 return ERROR_INVALID_PARAMETER;
983 name = getInterfaceNameByIndex(pIfRow->dwIndex, nameBuf);
984 if (name) {
985 ret = getInterfaceEntryByName(name, pIfRow);
986 if (ret == NO_ERROR)
987 ret = getInterfaceStatsByName(name, pIfRow);
989 else
990 ret = ERROR_INVALID_DATA;
991 TRACE("returning %d\n", ret);
992 return ret;
996 static int IfTableSorter(const void *a, const void *b)
998 int ret;
1000 if (a && b)
1001 ret = ((const MIB_IFROW*)a)->dwIndex - ((const MIB_IFROW*)b)->dwIndex;
1002 else
1003 ret = 0;
1004 return ret;
1008 /******************************************************************
1009 * GetIfTable (IPHLPAPI.@)
1011 * Get a table of local interfaces.
1013 * PARAMS
1014 * pIfTable [Out] buffer for local interfaces table
1015 * pdwSize [In/Out] length of output buffer
1016 * bOrder [In] whether to sort the table
1018 * RETURNS
1019 * Success: NO_ERROR
1020 * Failure: error code from winerror.h
1022 * NOTES
1023 * If pdwSize is less than required, the function will return
1024 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the required byte
1025 * size.
1026 * If bOrder is true, the returned table will be sorted by interface index.
1028 DWORD WINAPI GetIfTable(PMIB_IFTABLE pIfTable, PULONG pdwSize, BOOL bOrder)
1030 DWORD ret;
1032 TRACE("pIfTable %p, pdwSize %p, bOrder %d\n", pdwSize, pdwSize,
1033 (DWORD)bOrder);
1034 if (!pdwSize)
1035 ret = ERROR_INVALID_PARAMETER;
1036 else {
1037 DWORD numInterfaces = getNumInterfaces();
1038 ULONG size = sizeof(MIB_IFTABLE);
1040 if (numInterfaces > 1)
1041 size += (numInterfaces - 1) * sizeof(MIB_IFROW);
1042 if (!pIfTable || *pdwSize < size) {
1043 *pdwSize = size;
1044 ret = ERROR_INSUFFICIENT_BUFFER;
1046 else {
1047 InterfaceIndexTable *table = getInterfaceIndexTable();
1049 if (table) {
1050 size = sizeof(MIB_IFTABLE);
1051 if (table->numIndexes > 1)
1052 size += (table->numIndexes - 1) * sizeof(MIB_IFROW);
1053 if (*pdwSize < size) {
1054 *pdwSize = size;
1055 ret = ERROR_INSUFFICIENT_BUFFER;
1057 else {
1058 DWORD ndx;
1060 *pdwSize = size;
1061 pIfTable->dwNumEntries = 0;
1062 for (ndx = 0; ndx < table->numIndexes; ndx++) {
1063 pIfTable->table[ndx].dwIndex = table->indexes[ndx];
1064 GetIfEntry(&pIfTable->table[ndx]);
1065 pIfTable->dwNumEntries++;
1067 if (bOrder)
1068 qsort(pIfTable->table, pIfTable->dwNumEntries, sizeof(MIB_IFROW),
1069 IfTableSorter);
1070 ret = NO_ERROR;
1072 HeapFree(GetProcessHeap(), 0, table);
1074 else
1075 ret = ERROR_OUTOFMEMORY;
1078 TRACE("returning %d\n", ret);
1079 return ret;
1083 /******************************************************************
1084 * GetInterfaceInfo (IPHLPAPI.@)
1086 * Get a list of network interface adapters.
1088 * PARAMS
1089 * pIfTable [Out] buffer for interface adapters
1090 * dwOutBufLen [Out] if buffer is too small, returns required size
1092 * RETURNS
1093 * Success: NO_ERROR
1094 * Failure: error code from winerror.h
1096 * BUGS
1097 * MSDN states this should return non-loopback interfaces only.
1099 DWORD WINAPI GetInterfaceInfo(PIP_INTERFACE_INFO pIfTable, PULONG dwOutBufLen)
1101 DWORD ret;
1103 TRACE("pIfTable %p, dwOutBufLen %p\n", pIfTable, dwOutBufLen);
1104 if (!dwOutBufLen)
1105 ret = ERROR_INVALID_PARAMETER;
1106 else {
1107 DWORD numInterfaces = getNumInterfaces();
1108 ULONG size = sizeof(IP_INTERFACE_INFO);
1110 if (numInterfaces > 1)
1111 size += (numInterfaces - 1) * sizeof(IP_ADAPTER_INDEX_MAP);
1112 if (!pIfTable || *dwOutBufLen < size) {
1113 *dwOutBufLen = size;
1114 ret = ERROR_INSUFFICIENT_BUFFER;
1116 else {
1117 InterfaceIndexTable *table = getInterfaceIndexTable();
1119 if (table) {
1120 size = sizeof(IP_INTERFACE_INFO);
1121 if (table->numIndexes > 1)
1122 size += (table->numIndexes - 1) * sizeof(IP_ADAPTER_INDEX_MAP);
1123 if (*dwOutBufLen < size) {
1124 *dwOutBufLen = size;
1125 ret = ERROR_INSUFFICIENT_BUFFER;
1127 else {
1128 DWORD ndx;
1129 char nameBuf[MAX_ADAPTER_NAME];
1131 *dwOutBufLen = size;
1132 pIfTable->NumAdapters = 0;
1133 for (ndx = 0; ndx < table->numIndexes; ndx++) {
1134 const char *walker, *name;
1135 WCHAR *assigner;
1137 pIfTable->Adapter[ndx].Index = table->indexes[ndx];
1138 name = getInterfaceNameByIndex(table->indexes[ndx], nameBuf);
1139 for (walker = name, assigner = pIfTable->Adapter[ndx].Name;
1140 walker && *walker &&
1141 assigner - pIfTable->Adapter[ndx].Name < MAX_ADAPTER_NAME - 1;
1142 walker++, assigner++)
1143 *assigner = *walker;
1144 *assigner = 0;
1145 pIfTable->NumAdapters++;
1147 ret = NO_ERROR;
1149 HeapFree(GetProcessHeap(), 0, table);
1151 else
1152 ret = ERROR_OUTOFMEMORY;
1155 TRACE("returning %d\n", ret);
1156 return ret;
1160 /******************************************************************
1161 * GetIpAddrTable (IPHLPAPI.@)
1163 * Get interface-to-IP address mapping table.
1165 * PARAMS
1166 * pIpAddrTable [Out] buffer for mapping table
1167 * pdwSize [In/Out] length of output buffer
1168 * bOrder [In] whether to sort the table
1170 * RETURNS
1171 * Success: NO_ERROR
1172 * Failure: error code from winerror.h
1174 * NOTES
1175 * If pdwSize is less than required, the function will return
1176 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the required byte
1177 * size.
1178 * If bOrder is true, the returned table will be sorted by the next hop and
1179 * an assortment of arbitrary parameters.
1181 DWORD WINAPI GetIpAddrTable(PMIB_IPADDRTABLE pIpAddrTable, PULONG pdwSize, BOOL bOrder)
1183 DWORD ret;
1185 TRACE("pIpAddrTable %p, pdwSize %p, bOrder %d\n", pIpAddrTable, pdwSize,
1186 (DWORD)bOrder);
1187 if (!pdwSize)
1188 ret = ERROR_INVALID_PARAMETER;
1189 else {
1190 PMIB_IPADDRTABLE table;
1192 ret = getIPAddrTable(&table, GetProcessHeap(), 0);
1193 if (ret == NO_ERROR)
1195 ULONG size = sizeof(MIB_IPADDRTABLE);
1197 if (table->dwNumEntries > 1)
1198 size += (table->dwNumEntries - 1) * sizeof(MIB_IPADDRROW);
1199 if (!pIpAddrTable || *pdwSize < size) {
1200 *pdwSize = size;
1201 ret = ERROR_INSUFFICIENT_BUFFER;
1203 else {
1204 *pdwSize = size;
1205 memcpy(pIpAddrTable, table, size);
1206 if (bOrder)
1207 qsort(pIpAddrTable->table, pIpAddrTable->dwNumEntries,
1208 sizeof(MIB_IPADDRROW), IpAddrTableSorter);
1209 ret = NO_ERROR;
1211 HeapFree(GetProcessHeap(), 0, table);
1214 TRACE("returning %d\n", ret);
1215 return ret;
1219 /******************************************************************
1220 * GetIpForwardTable (IPHLPAPI.@)
1222 * Get the route table.
1224 * PARAMS
1225 * pIpForwardTable [Out] buffer for route table
1226 * pdwSize [In/Out] length of output buffer
1227 * bOrder [In] whether to sort the table
1229 * RETURNS
1230 * Success: NO_ERROR
1231 * Failure: error code from winerror.h
1233 * NOTES
1234 * If pdwSize is less than required, the function will return
1235 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the required byte
1236 * size.
1237 * If bOrder is true, the returned table will be sorted by the next hop and
1238 * an assortment of arbitrary parameters.
1240 DWORD WINAPI GetIpForwardTable(PMIB_IPFORWARDTABLE pIpForwardTable, PULONG pdwSize, BOOL bOrder)
1242 DWORD ret;
1243 PMIB_IPFORWARDTABLE table;
1245 TRACE("pIpForwardTable %p, pdwSize %p, bOrder %d\n", pIpForwardTable, pdwSize, bOrder);
1247 if (!pdwSize) return ERROR_INVALID_PARAMETER;
1249 ret = AllocateAndGetIpForwardTableFromStack(&table, bOrder, GetProcessHeap(), 0);
1250 if (!ret) {
1251 DWORD size = FIELD_OFFSET( MIB_IPFORWARDTABLE, table[table->dwNumEntries] );
1252 if (!pIpForwardTable || *pdwSize < size) {
1253 *pdwSize = size;
1254 ret = ERROR_INSUFFICIENT_BUFFER;
1256 else {
1257 *pdwSize = size;
1258 memcpy(pIpForwardTable, table, size);
1260 HeapFree(GetProcessHeap(), 0, table);
1262 TRACE("returning %d\n", ret);
1263 return ret;
1267 /******************************************************************
1268 * GetIpNetTable (IPHLPAPI.@)
1270 * Get the IP-to-physical address mapping table.
1272 * PARAMS
1273 * pIpNetTable [Out] buffer for mapping table
1274 * pdwSize [In/Out] length of output buffer
1275 * bOrder [In] whether to sort the table
1277 * RETURNS
1278 * Success: NO_ERROR
1279 * Failure: error code from winerror.h
1281 * NOTES
1282 * If pdwSize is less than required, the function will return
1283 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the required byte
1284 * size.
1285 * If bOrder is true, the returned table will be sorted by IP address.
1287 DWORD WINAPI GetIpNetTable(PMIB_IPNETTABLE pIpNetTable, PULONG pdwSize, BOOL bOrder)
1289 DWORD ret;
1290 PMIB_IPNETTABLE table;
1292 TRACE("pIpNetTable %p, pdwSize %p, bOrder %d\n", pIpNetTable, pdwSize, bOrder);
1294 if (!pdwSize) return ERROR_INVALID_PARAMETER;
1296 ret = AllocateAndGetIpNetTableFromStack( &table, bOrder, GetProcessHeap(), 0 );
1297 if (!ret) {
1298 DWORD size = FIELD_OFFSET( MIB_IPNETTABLE, table[table->dwNumEntries] );
1299 if (!pIpNetTable || *pdwSize < size) {
1300 *pdwSize = size;
1301 ret = ERROR_INSUFFICIENT_BUFFER;
1303 else {
1304 *pdwSize = size;
1305 memcpy(pIpNetTable, table, size);
1307 HeapFree(GetProcessHeap(), 0, table);
1309 TRACE("returning %d\n", ret);
1310 return ret;
1314 /******************************************************************
1315 * GetNetworkParams (IPHLPAPI.@)
1317 * Get the network parameters for the local computer.
1319 * PARAMS
1320 * pFixedInfo [Out] buffer for network parameters
1321 * pOutBufLen [In/Out] length of output buffer
1323 * RETURNS
1324 * Success: NO_ERROR
1325 * Failure: error code from winerror.h
1327 * NOTES
1328 * If pOutBufLen is less than required, the function will return
1329 * ERROR_INSUFFICIENT_BUFFER, and pOutBufLen will be set to the required byte
1330 * size.
1332 DWORD WINAPI GetNetworkParams(PFIXED_INFO pFixedInfo, PULONG pOutBufLen)
1334 DWORD ret, size;
1335 LONG regReturn;
1336 HKEY hKey;
1338 TRACE("pFixedInfo %p, pOutBufLen %p\n", pFixedInfo, pOutBufLen);
1339 if (!pOutBufLen)
1340 return ERROR_INVALID_PARAMETER;
1342 initialise_resolver();
1343 size = sizeof(FIXED_INFO) + (_res.nscount > 0 ? (_res.nscount - 1) *
1344 sizeof(IP_ADDR_STRING) : 0);
1345 if (!pFixedInfo || *pOutBufLen < size) {
1346 *pOutBufLen = size;
1347 return ERROR_BUFFER_OVERFLOW;
1350 memset(pFixedInfo, 0, size);
1351 size = sizeof(pFixedInfo->HostName);
1352 GetComputerNameExA(ComputerNameDnsHostname, pFixedInfo->HostName, &size);
1353 size = sizeof(pFixedInfo->DomainName);
1354 GetComputerNameExA(ComputerNameDnsDomain, pFixedInfo->DomainName, &size);
1355 if (_res.nscount > 0) {
1356 PIP_ADDR_STRING ptr;
1357 int i;
1359 for (i = 0, ptr = &pFixedInfo->DnsServerList; i < _res.nscount && ptr;
1360 i++, ptr = ptr->Next) {
1361 toIPAddressString(_res.nsaddr_list[i].sin_addr.s_addr,
1362 ptr->IpAddress.String);
1363 if (i == _res.nscount - 1)
1364 ptr->Next = NULL;
1365 else if (i == 0)
1366 ptr->Next = (PIP_ADDR_STRING)((LPBYTE)pFixedInfo + sizeof(FIXED_INFO));
1367 else
1368 ptr->Next = (PIP_ADDR_STRING)((PBYTE)ptr + sizeof(IP_ADDR_STRING));
1371 pFixedInfo->NodeType = HYBRID_NODETYPE;
1372 regReturn = RegOpenKeyExA(HKEY_LOCAL_MACHINE,
1373 "SYSTEM\\CurrentControlSet\\Services\\VxD\\MSTCP", 0, KEY_READ, &hKey);
1374 if (regReturn != ERROR_SUCCESS)
1375 regReturn = RegOpenKeyExA(HKEY_LOCAL_MACHINE,
1376 "SYSTEM\\CurrentControlSet\\Services\\NetBT\\Parameters", 0, KEY_READ,
1377 &hKey);
1378 if (regReturn == ERROR_SUCCESS)
1380 DWORD size = sizeof(pFixedInfo->ScopeId);
1382 RegQueryValueExA(hKey, "ScopeID", NULL, NULL, (LPBYTE)pFixedInfo->ScopeId, &size);
1383 RegCloseKey(hKey);
1386 /* FIXME: can check whether routing's enabled in /proc/sys/net/ipv4/ip_forward
1387 I suppose could also check for a listener on port 53 to set EnableDns */
1388 ret = NO_ERROR;
1389 TRACE("returning %d\n", ret);
1390 return ret;
1394 /******************************************************************
1395 * GetNumberOfInterfaces (IPHLPAPI.@)
1397 * Get the number of interfaces.
1399 * PARAMS
1400 * pdwNumIf [Out] number of interfaces
1402 * RETURNS
1403 * NO_ERROR on success, ERROR_INVALID_PARAMETER if pdwNumIf is NULL.
1405 DWORD WINAPI GetNumberOfInterfaces(PDWORD pdwNumIf)
1407 DWORD ret;
1409 TRACE("pdwNumIf %p\n", pdwNumIf);
1410 if (!pdwNumIf)
1411 ret = ERROR_INVALID_PARAMETER;
1412 else {
1413 *pdwNumIf = getNumInterfaces();
1414 ret = NO_ERROR;
1416 TRACE("returning %d\n", ret);
1417 return ret;
1421 /******************************************************************
1422 * GetPerAdapterInfo (IPHLPAPI.@)
1424 * Get information about an adapter corresponding to an interface.
1426 * PARAMS
1427 * IfIndex [In] interface info
1428 * pPerAdapterInfo [Out] buffer for per adapter info
1429 * pOutBufLen [In/Out] length of output buffer
1431 * RETURNS
1432 * Success: NO_ERROR
1433 * Failure: error code from winerror.h
1435 * FIXME
1436 * Stub, returns empty IP_PER_ADAPTER_INFO in every case.
1438 DWORD WINAPI GetPerAdapterInfo(ULONG IfIndex, PIP_PER_ADAPTER_INFO pPerAdapterInfo, PULONG pOutBufLen)
1440 ULONG bytesNeeded = sizeof(IP_PER_ADAPTER_INFO);
1442 TRACE("(IfIndex %d, pPerAdapterInfo %p, pOutBufLen %p)\n", IfIndex, pPerAdapterInfo, pOutBufLen);
1444 if (!pOutBufLen) return ERROR_INVALID_PARAMETER;
1446 if (!pPerAdapterInfo || *pOutBufLen < bytesNeeded)
1448 *pOutBufLen = bytesNeeded;
1449 return ERROR_BUFFER_OVERFLOW;
1452 memset(pPerAdapterInfo, 0, bytesNeeded);
1453 return NO_ERROR;
1457 /******************************************************************
1458 * GetRTTAndHopCount (IPHLPAPI.@)
1460 * Get round-trip time (RTT) and hop count.
1462 * PARAMS
1464 * DestIpAddress [In] destination address to get the info for
1465 * HopCount [Out] retrieved hop count
1466 * MaxHops [In] maximum hops to search for the destination
1467 * RTT [Out] RTT in milliseconds
1469 * RETURNS
1470 * Success: TRUE
1471 * Failure: FALSE
1473 * FIXME
1474 * Stub, returns FALSE.
1476 BOOL WINAPI GetRTTAndHopCount(IPAddr DestIpAddress, PULONG HopCount, ULONG MaxHops, PULONG RTT)
1478 FIXME("(DestIpAddress 0x%08x, HopCount %p, MaxHops %d, RTT %p): stub\n",
1479 DestIpAddress, HopCount, MaxHops, RTT);
1480 return FALSE;
1484 /******************************************************************
1485 * GetTcpTable (IPHLPAPI.@)
1487 * Get the table of active TCP connections.
1489 * PARAMS
1490 * pTcpTable [Out] buffer for TCP connections table
1491 * pdwSize [In/Out] length of output buffer
1492 * bOrder [In] whether to order the table
1494 * RETURNS
1495 * Success: NO_ERROR
1496 * Failure: error code from winerror.h
1498 * NOTES
1499 * If pdwSize is less than required, the function will return
1500 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to
1501 * the required byte size.
1502 * If bOrder is true, the returned table will be sorted, first by
1503 * local address and port number, then by remote address and port
1504 * number.
1506 DWORD WINAPI GetTcpTable(PMIB_TCPTABLE pTcpTable, PDWORD pdwSize, BOOL bOrder)
1508 DWORD ret;
1509 PMIB_TCPTABLE table;
1511 TRACE("pTcpTable %p, pdwSize %p, bOrder %d\n", pTcpTable, pdwSize, bOrder);
1513 if (!pdwSize) return ERROR_INVALID_PARAMETER;
1515 ret = AllocateAndGetTcpTableFromStack(&table, bOrder, GetProcessHeap(), 0);
1516 if (!ret) {
1517 DWORD size = FIELD_OFFSET( MIB_TCPTABLE, table[table->dwNumEntries] );
1518 if (!pTcpTable || *pdwSize < size) {
1519 *pdwSize = size;
1520 ret = ERROR_INSUFFICIENT_BUFFER;
1522 else {
1523 *pdwSize = size;
1524 memcpy(pTcpTable, table, size);
1526 HeapFree(GetProcessHeap(), 0, table);
1528 TRACE("returning %d\n", ret);
1529 return ret;
1533 /******************************************************************
1534 * GetUdpTable (IPHLPAPI.@)
1536 * Get a table of active UDP connections.
1538 * PARAMS
1539 * pUdpTable [Out] buffer for UDP connections table
1540 * pdwSize [In/Out] length of output buffer
1541 * bOrder [In] whether to order the table
1543 * RETURNS
1544 * Success: NO_ERROR
1545 * Failure: error code from winerror.h
1547 * NOTES
1548 * If pdwSize is less than required, the function will return
1549 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the
1550 * required byte size.
1551 * If bOrder is true, the returned table will be sorted, first by
1552 * local address, then by local port number.
1554 DWORD WINAPI GetUdpTable(PMIB_UDPTABLE pUdpTable, PDWORD pdwSize, BOOL bOrder)
1556 DWORD ret;
1557 PMIB_UDPTABLE table;
1559 TRACE("pUdpTable %p, pdwSize %p, bOrder %d\n", pUdpTable, pdwSize, bOrder);
1561 if (!pdwSize) return ERROR_INVALID_PARAMETER;
1563 ret = AllocateAndGetUdpTableFromStack( &table, bOrder, GetProcessHeap(), 0 );
1564 if (!ret) {
1565 DWORD size = FIELD_OFFSET( MIB_UDPTABLE, table[table->dwNumEntries] );
1566 if (!pUdpTable || *pdwSize < size) {
1567 *pdwSize = size;
1568 ret = ERROR_INSUFFICIENT_BUFFER;
1570 else {
1571 *pdwSize = size;
1572 memcpy(pUdpTable, table, size);
1574 HeapFree(GetProcessHeap(), 0, table);
1576 TRACE("returning %d\n", ret);
1577 return ret;
1581 /******************************************************************
1582 * GetUniDirectionalAdapterInfo (IPHLPAPI.@)
1584 * This is a Win98-only function to get information on "unidirectional"
1585 * adapters. Since this is pretty nonsensical in other contexts, it
1586 * never returns anything.
1588 * PARAMS
1589 * pIPIfInfo [Out] buffer for adapter infos
1590 * dwOutBufLen [Out] length of the output buffer
1592 * RETURNS
1593 * Success: NO_ERROR
1594 * Failure: error code from winerror.h
1596 * FIXME
1597 * Stub, returns ERROR_NOT_SUPPORTED.
1599 DWORD WINAPI GetUniDirectionalAdapterInfo(PIP_UNIDIRECTIONAL_ADAPTER_ADDRESS pIPIfInfo, PULONG dwOutBufLen)
1601 TRACE("pIPIfInfo %p, dwOutBufLen %p\n", pIPIfInfo, dwOutBufLen);
1602 /* a unidirectional adapter?? not bloody likely! */
1603 return ERROR_NOT_SUPPORTED;
1607 /******************************************************************
1608 * IpReleaseAddress (IPHLPAPI.@)
1610 * Release an IP obtained through DHCP,
1612 * PARAMS
1613 * AdapterInfo [In] adapter to release IP address
1615 * RETURNS
1616 * Success: NO_ERROR
1617 * Failure: error code from winerror.h
1619 * NOTES
1620 * Since GetAdaptersInfo never returns adapters that have DHCP enabled,
1621 * this function does nothing.
1623 * FIXME
1624 * Stub, returns ERROR_NOT_SUPPORTED.
1626 DWORD WINAPI IpReleaseAddress(PIP_ADAPTER_INDEX_MAP AdapterInfo)
1628 TRACE("AdapterInfo %p\n", AdapterInfo);
1629 /* not a stub, never going to support this (and I never mark an adapter as
1630 DHCP enabled, see GetAdaptersInfo, so this should never get called) */
1631 return ERROR_NOT_SUPPORTED;
1635 /******************************************************************
1636 * IpRenewAddress (IPHLPAPI.@)
1638 * Renew an IP obtained through DHCP.
1640 * PARAMS
1641 * AdapterInfo [In] adapter to renew IP address
1643 * RETURNS
1644 * Success: NO_ERROR
1645 * Failure: error code from winerror.h
1647 * NOTES
1648 * Since GetAdaptersInfo never returns adapters that have DHCP enabled,
1649 * this function does nothing.
1651 * FIXME
1652 * Stub, returns ERROR_NOT_SUPPORTED.
1654 DWORD WINAPI IpRenewAddress(PIP_ADAPTER_INDEX_MAP AdapterInfo)
1656 TRACE("AdapterInfo %p\n", AdapterInfo);
1657 /* not a stub, never going to support this (and I never mark an adapter as
1658 DHCP enabled, see GetAdaptersInfo, so this should never get called) */
1659 return ERROR_NOT_SUPPORTED;
1663 /******************************************************************
1664 * NotifyAddrChange (IPHLPAPI.@)
1666 * Notify caller whenever the ip-interface map is changed.
1668 * PARAMS
1669 * Handle [Out] handle usable in asynchronous notification
1670 * overlapped [In] overlapped structure that notifies the caller
1672 * RETURNS
1673 * Success: NO_ERROR
1674 * Failure: error code from winerror.h
1676 * FIXME
1677 * Stub, returns ERROR_NOT_SUPPORTED.
1679 DWORD WINAPI NotifyAddrChange(PHANDLE Handle, LPOVERLAPPED overlapped)
1681 FIXME("(Handle %p, overlapped %p): stub\n", Handle, overlapped);
1682 return ERROR_NOT_SUPPORTED;
1686 /******************************************************************
1687 * NotifyRouteChange (IPHLPAPI.@)
1689 * Notify caller whenever the ip routing table is changed.
1691 * PARAMS
1692 * Handle [Out] handle usable in asynchronous notification
1693 * overlapped [In] overlapped structure that notifies the caller
1695 * RETURNS
1696 * Success: NO_ERROR
1697 * Failure: error code from winerror.h
1699 * FIXME
1700 * Stub, returns ERROR_NOT_SUPPORTED.
1702 DWORD WINAPI NotifyRouteChange(PHANDLE Handle, LPOVERLAPPED overlapped)
1704 FIXME("(Handle %p, overlapped %p): stub\n", Handle, overlapped);
1705 return ERROR_NOT_SUPPORTED;
1709 /******************************************************************
1710 * SendARP (IPHLPAPI.@)
1712 * Send an ARP request.
1714 * PARAMS
1715 * DestIP [In] attempt to obtain this IP
1716 * SrcIP [In] optional sender IP address
1717 * pMacAddr [Out] buffer for the mac address
1718 * PhyAddrLen [In/Out] length of the output buffer
1720 * RETURNS
1721 * Success: NO_ERROR
1722 * Failure: error code from winerror.h
1724 * FIXME
1725 * Stub, returns ERROR_NOT_SUPPORTED.
1727 DWORD WINAPI SendARP(IPAddr DestIP, IPAddr SrcIP, PULONG pMacAddr, PULONG PhyAddrLen)
1729 FIXME("(DestIP 0x%08x, SrcIP 0x%08x, pMacAddr %p, PhyAddrLen %p): stub\n",
1730 DestIP, SrcIP, pMacAddr, PhyAddrLen);
1731 return ERROR_NOT_SUPPORTED;
1735 /******************************************************************
1736 * SetIfEntry (IPHLPAPI.@)
1738 * Set the administrative status of an interface.
1740 * PARAMS
1741 * pIfRow [In] dwAdminStatus member specifies the new status.
1743 * RETURNS
1744 * Success: NO_ERROR
1745 * Failure: error code from winerror.h
1747 * FIXME
1748 * Stub, returns ERROR_NOT_SUPPORTED.
1750 DWORD WINAPI SetIfEntry(PMIB_IFROW pIfRow)
1752 FIXME("(pIfRow %p): stub\n", pIfRow);
1753 /* this is supposed to set an interface administratively up or down.
1754 Could do SIOCSIFFLAGS and set/clear IFF_UP, but, not sure I want to, and
1755 this sort of down is indistinguishable from other sorts of down (e.g. no
1756 link). */
1757 return ERROR_NOT_SUPPORTED;
1761 /******************************************************************
1762 * SetIpForwardEntry (IPHLPAPI.@)
1764 * Modify an existing route.
1766 * PARAMS
1767 * pRoute [In] route with the new information
1769 * RETURNS
1770 * Success: NO_ERROR
1771 * Failure: error code from winerror.h
1773 * FIXME
1774 * Stub, returns NO_ERROR.
1776 DWORD WINAPI SetIpForwardEntry(PMIB_IPFORWARDROW pRoute)
1778 FIXME("(pRoute %p): stub\n", pRoute);
1779 /* this is to add a route entry, how's it distinguishable from
1780 CreateIpForwardEntry?
1781 could use SIOCADDRT, not sure I want to */
1782 return 0;
1786 /******************************************************************
1787 * SetIpNetEntry (IPHLPAPI.@)
1789 * Modify an existing ARP entry.
1791 * PARAMS
1792 * pArpEntry [In] ARP entry with the new information
1794 * RETURNS
1795 * Success: NO_ERROR
1796 * Failure: error code from winerror.h
1798 * FIXME
1799 * Stub, returns NO_ERROR.
1801 DWORD WINAPI SetIpNetEntry(PMIB_IPNETROW pArpEntry)
1803 FIXME("(pArpEntry %p): stub\n", pArpEntry);
1804 /* same as CreateIpNetEntry here, could use SIOCSARP, not sure I want to */
1805 return 0;
1809 /******************************************************************
1810 * SetIpStatistics (IPHLPAPI.@)
1812 * Toggle IP forwarding and det the default TTL value.
1814 * PARAMS
1815 * pIpStats [In] IP statistics with the new information
1817 * RETURNS
1818 * Success: NO_ERROR
1819 * Failure: error code from winerror.h
1821 * FIXME
1822 * Stub, returns NO_ERROR.
1824 DWORD WINAPI SetIpStatistics(PMIB_IPSTATS pIpStats)
1826 FIXME("(pIpStats %p): stub\n", pIpStats);
1827 return 0;
1831 /******************************************************************
1832 * SetIpTTL (IPHLPAPI.@)
1834 * Set the default TTL value.
1836 * PARAMS
1837 * nTTL [In] new TTL value
1839 * RETURNS
1840 * Success: NO_ERROR
1841 * Failure: error code from winerror.h
1843 * FIXME
1844 * Stub, returns NO_ERROR.
1846 DWORD WINAPI SetIpTTL(UINT nTTL)
1848 FIXME("(nTTL %d): stub\n", nTTL);
1849 /* could echo nTTL > /proc/net/sys/net/ipv4/ip_default_ttl, not sure I
1850 want to. Could map EACCESS to ERROR_ACCESS_DENIED, I suppose */
1851 return 0;
1855 /******************************************************************
1856 * SetTcpEntry (IPHLPAPI.@)
1858 * Set the state of a TCP connection.
1860 * PARAMS
1861 * pTcpRow [In] specifies connection with new state
1863 * RETURNS
1864 * Success: NO_ERROR
1865 * Failure: error code from winerror.h
1867 * FIXME
1868 * Stub, returns NO_ERROR.
1870 DWORD WINAPI SetTcpEntry(PMIB_TCPROW pTcpRow)
1872 FIXME("(pTcpRow %p): stub\n", pTcpRow);
1873 return 0;
1877 /******************************************************************
1878 * UnenableRouter (IPHLPAPI.@)
1880 * Decrement the IP-forwarding reference count. Turn off IP-forwarding
1881 * if it reaches zero.
1883 * PARAMS
1884 * pOverlapped [In/Out] should be the same as in EnableRouter()
1885 * lpdwEnableCount [Out] optional, receives reference count
1887 * RETURNS
1888 * Success: NO_ERROR
1889 * Failure: error code from winerror.h
1891 * FIXME
1892 * Stub, returns ERROR_NOT_SUPPORTED.
1894 DWORD WINAPI UnenableRouter(OVERLAPPED * pOverlapped, LPDWORD lpdwEnableCount)
1896 FIXME("(pOverlapped %p, lpdwEnableCount %p): stub\n", pOverlapped,
1897 lpdwEnableCount);
1898 /* could echo "0" > /proc/net/sys/net/ipv4/ip_forward, not sure I want to
1899 could map EACCESS to ERROR_ACCESS_DENIED, I suppose
1901 return ERROR_NOT_SUPPORTED;