gdi.exe16: Constify a variable.
[wine] / dlls / iphlpapi / iphlpapi_main.c
1 /*
2  * iphlpapi dll implementation
3  *
4  * Copyright (C) 2003,2006 Juan Lang
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "config.h"
22
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
44
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"
57
58 #include "wine/debug.h"
59
60 WINE_DEFAULT_DEBUG_CHANNEL(iphlpapi);
61
62 #ifndef IF_NAMESIZE
63 #define IF_NAMESIZE 16
64 #endif
65
66 #ifndef INADDR_NONE
67 #define INADDR_NONE ~0UL
68 #endif
69
70 /* call res_init() just once because of a bug in Mac OS X 10.4 */
71 /* Call once per thread on systems that have per-thread _res. */
72 static void initialise_resolver(void)
73 {
74     if ((_res.options & RES_INIT) == 0)
75         res_init();
76 }
77
78 BOOL WINAPI DllMain (HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
79 {
80   switch (fdwReason) {
81     case DLL_PROCESS_ATTACH:
82       DisableThreadLibraryCalls( hinstDLL );
83       break;
84
85     case DLL_PROCESS_DETACH:
86       break;
87   }
88   return TRUE;
89 }
90
91 /******************************************************************
92  *    AddIPAddress (IPHLPAPI.@)
93  *
94  * Add an IP address to an adapter.
95  *
96  * PARAMS
97  *  Address     [In]  IP address to add to the adapter
98  *  IpMask      [In]  subnet mask for the IP address
99  *  IfIndex     [In]  adapter index to add the address
100  *  NTEContext  [Out] Net Table Entry (NTE) context for the IP address
101  *  NTEInstance [Out] NTE instance for the IP address
102  *
103  * RETURNS
104  *  Success: NO_ERROR
105  *  Failure: error code from winerror.h
106  *
107  * FIXME
108  *  Stub. Currently returns ERROR_NOT_SUPPORTED.
109  */
110 DWORD WINAPI AddIPAddress(IPAddr Address, IPMask IpMask, DWORD IfIndex, PULONG NTEContext, PULONG NTEInstance)
111 {
112   FIXME(":stub\n");
113   return ERROR_NOT_SUPPORTED;
114 }
115
116
117 /******************************************************************
118  *    AllocateAndGetIfTableFromStack (IPHLPAPI.@)
119  *
120  * Get table of local interfaces.
121  * Like GetIfTable(), but allocate the returned table from heap.
122  *
123  * PARAMS
124  *  ppIfTable [Out] pointer into which the MIB_IFTABLE is
125  *                  allocated and returned.
126  *  bOrder    [In]  whether to sort the table
127  *  heap      [In]  heap from which the table is allocated
128  *  flags     [In]  flags to HeapAlloc
129  *
130  * RETURNS
131  *  ERROR_INVALID_PARAMETER if ppIfTable is NULL, whatever
132  *  GetIfTable() returns otherwise.
133  */
134 DWORD WINAPI AllocateAndGetIfTableFromStack(PMIB_IFTABLE *ppIfTable,
135  BOOL bOrder, HANDLE heap, DWORD flags)
136 {
137   DWORD ret;
138
139   TRACE("ppIfTable %p, bOrder %d, heap %p, flags 0x%08x\n", ppIfTable,
140         bOrder, heap, flags);
141   if (!ppIfTable)
142     ret = ERROR_INVALID_PARAMETER;
143   else {
144     DWORD dwSize = 0;
145
146     ret = GetIfTable(*ppIfTable, &dwSize, bOrder);
147     if (ret == ERROR_INSUFFICIENT_BUFFER) {
148       *ppIfTable = HeapAlloc(heap, flags, dwSize);
149       ret = GetIfTable(*ppIfTable, &dwSize, bOrder);
150     }
151   }
152   TRACE("returning %d\n", ret);
153   return ret;
154 }
155
156
157 static int IpAddrTableSorter(const void *a, const void *b)
158 {
159   int ret;
160
161   if (a && b)
162     ret = ((const MIB_IPADDRROW*)a)->dwAddr - ((const MIB_IPADDRROW*)b)->dwAddr;
163   else
164     ret = 0;
165   return ret;
166 }
167
168
169 /******************************************************************
170  *    AllocateAndGetIpAddrTableFromStack (IPHLPAPI.@)
171  *
172  * Get interface-to-IP address mapping table. 
173  * Like GetIpAddrTable(), but allocate the returned table from heap.
174  *
175  * PARAMS
176  *  ppIpAddrTable [Out] pointer into which the MIB_IPADDRTABLE is
177  *                      allocated and returned.
178  *  bOrder        [In]  whether to sort the table
179  *  heap          [In]  heap from which the table is allocated
180  *  flags         [In]  flags to HeapAlloc
181  *
182  * RETURNS
183  *  ERROR_INVALID_PARAMETER if ppIpAddrTable is NULL, other error codes on
184  *  failure, NO_ERROR on success.
185  */
186 DWORD WINAPI AllocateAndGetIpAddrTableFromStack(PMIB_IPADDRTABLE *ppIpAddrTable,
187  BOOL bOrder, HANDLE heap, DWORD flags)
188 {
189   DWORD ret;
190
191   TRACE("ppIpAddrTable %p, bOrder %d, heap %p, flags 0x%08x\n",
192    ppIpAddrTable, bOrder, heap, flags);
193   ret = getIPAddrTable(ppIpAddrTable, heap, flags);
194   if (!ret && bOrder)
195     qsort((*ppIpAddrTable)->table, (*ppIpAddrTable)->dwNumEntries,
196      sizeof(MIB_IPADDRROW), IpAddrTableSorter);
197   TRACE("returning %d\n", ret);
198   return ret;
199 }
200
201
202 /******************************************************************
203  *    CreateIpForwardEntry (IPHLPAPI.@)
204  *
205  * Create a route in the local computer's IP table.
206  *
207  * PARAMS
208  *  pRoute [In] new route information
209  *
210  * RETURNS
211  *  Success: NO_ERROR
212  *  Failure: error code from winerror.h
213  *
214  * FIXME
215  *  Stub, always returns NO_ERROR.
216  */
217 DWORD WINAPI CreateIpForwardEntry(PMIB_IPFORWARDROW pRoute)
218 {
219   FIXME("(pRoute %p): stub\n", pRoute);
220   /* could use SIOCADDRT, not sure I want to */
221   return 0;
222 }
223
224
225 /******************************************************************
226  *    CreateIpNetEntry (IPHLPAPI.@)
227  *
228  * Create entry in the ARP table.
229  *
230  * PARAMS
231  *  pArpEntry [In] new ARP entry
232  *
233  * RETURNS
234  *  Success: NO_ERROR
235  *  Failure: error code from winerror.h
236  *
237  * FIXME
238  *  Stub, always returns NO_ERROR.
239  */
240 DWORD WINAPI CreateIpNetEntry(PMIB_IPNETROW pArpEntry)
241 {
242   FIXME("(pArpEntry %p)\n", pArpEntry);
243   /* could use SIOCSARP on systems that support it, not sure I want to */
244   return 0;
245 }
246
247
248 /******************************************************************
249  *    CreateProxyArpEntry (IPHLPAPI.@)
250  *
251  * Create a Proxy ARP (PARP) entry for an IP address.
252  *
253  * PARAMS
254  *  dwAddress [In] IP address for which this computer acts as a proxy. 
255  *  dwMask    [In] subnet mask for dwAddress
256  *  dwIfIndex [In] interface index
257  *
258  * RETURNS
259  *  Success: NO_ERROR
260  *  Failure: error code from winerror.h
261  *
262  * FIXME
263  *  Stub, returns ERROR_NOT_SUPPORTED.
264  */
265 DWORD WINAPI CreateProxyArpEntry(DWORD dwAddress, DWORD dwMask, DWORD dwIfIndex)
266 {
267   FIXME("(dwAddress 0x%08x, dwMask 0x%08x, dwIfIndex 0x%08x): stub\n",
268    dwAddress, dwMask, dwIfIndex);
269   return ERROR_NOT_SUPPORTED;
270 }
271
272
273 /******************************************************************
274  *    DeleteIPAddress (IPHLPAPI.@)
275  *
276  * Delete an IP address added with AddIPAddress().
277  *
278  * PARAMS
279  *  NTEContext [In] NTE context from AddIPAddress();
280  *
281  * RETURNS
282  *  Success: NO_ERROR
283  *  Failure: error code from winerror.h
284  *
285  * FIXME
286  *  Stub, returns ERROR_NOT_SUPPORTED.
287  */
288 DWORD WINAPI DeleteIPAddress(ULONG NTEContext)
289 {
290   FIXME("(NTEContext %d): stub\n", NTEContext);
291   return ERROR_NOT_SUPPORTED;
292 }
293
294
295 /******************************************************************
296  *    DeleteIpForwardEntry (IPHLPAPI.@)
297  *
298  * Delete a route.
299  *
300  * PARAMS
301  *  pRoute [In] route to delete
302  *
303  * RETURNS
304  *  Success: NO_ERROR
305  *  Failure: error code from winerror.h
306  *
307  * FIXME
308  *  Stub, returns NO_ERROR.
309  */
310 DWORD WINAPI DeleteIpForwardEntry(PMIB_IPFORWARDROW pRoute)
311 {
312   FIXME("(pRoute %p): stub\n", pRoute);
313   /* could use SIOCDELRT, not sure I want to */
314   return 0;
315 }
316
317
318 /******************************************************************
319  *    DeleteIpNetEntry (IPHLPAPI.@)
320  *
321  * Delete an ARP entry.
322  *
323  * PARAMS
324  *  pArpEntry [In] ARP entry to delete
325  *
326  * RETURNS
327  *  Success: NO_ERROR
328  *  Failure: error code from winerror.h
329  *
330  * FIXME
331  *  Stub, returns NO_ERROR.
332  */
333 DWORD WINAPI DeleteIpNetEntry(PMIB_IPNETROW pArpEntry)
334 {
335   FIXME("(pArpEntry %p): stub\n", pArpEntry);
336   /* could use SIOCDARP on systems that support it, not sure I want to */
337   return 0;
338 }
339
340
341 /******************************************************************
342  *    DeleteProxyArpEntry (IPHLPAPI.@)
343  *
344  * Delete a Proxy ARP entry.
345  *
346  * PARAMS
347  *  dwAddress [In] IP address for which this computer acts as a proxy. 
348  *  dwMask    [In] subnet mask for dwAddress
349  *  dwIfIndex [In] interface index
350  *
351  * RETURNS
352  *  Success: NO_ERROR
353  *  Failure: error code from winerror.h
354  *
355  * FIXME
356  *  Stub, returns ERROR_NOT_SUPPORTED.
357  */
358 DWORD WINAPI DeleteProxyArpEntry(DWORD dwAddress, DWORD dwMask, DWORD dwIfIndex)
359 {
360   FIXME("(dwAddress 0x%08x, dwMask 0x%08x, dwIfIndex 0x%08x): stub\n",
361    dwAddress, dwMask, dwIfIndex);
362   return ERROR_NOT_SUPPORTED;
363 }
364
365
366 /******************************************************************
367  *    EnableRouter (IPHLPAPI.@)
368  *
369  * Turn on ip forwarding.
370  *
371  * PARAMS
372  *  pHandle     [In/Out]
373  *  pOverlapped [In/Out] hEvent member should contain a valid handle.
374  *
375  * RETURNS
376  *  Success: ERROR_IO_PENDING
377  *  Failure: error code from winerror.h
378  *
379  * FIXME
380  *  Stub, returns ERROR_NOT_SUPPORTED.
381  */
382 DWORD WINAPI EnableRouter(HANDLE * pHandle, OVERLAPPED * pOverlapped)
383 {
384   FIXME("(pHandle %p, pOverlapped %p): stub\n", pHandle, pOverlapped);
385   /* could echo "1" > /proc/net/sys/net/ipv4/ip_forward, not sure I want to
386      could map EACCESS to ERROR_ACCESS_DENIED, I suppose
387    */
388   return ERROR_NOT_SUPPORTED;
389 }
390
391
392 /******************************************************************
393  *    FlushIpNetTable (IPHLPAPI.@)
394  *
395  * Delete all ARP entries of an interface
396  *
397  * PARAMS
398  *  dwIfIndex [In] interface index
399  *
400  * RETURNS
401  *  Success: NO_ERROR
402  *  Failure: error code from winerror.h
403  *
404  * FIXME
405  *  Stub, returns ERROR_NOT_SUPPORTED.
406  */
407 DWORD WINAPI FlushIpNetTable(DWORD dwIfIndex)
408 {
409   FIXME("(dwIfIndex 0x%08x): stub\n", dwIfIndex);
410   /* this flushes the arp cache of the given index */
411   return ERROR_NOT_SUPPORTED;
412 }
413
414
415 /******************************************************************
416  *    GetAdapterIndex (IPHLPAPI.@)
417  *
418  * Get interface index from its name.
419  *
420  * PARAMS
421  *  AdapterName [In]  unicode string with the adapter name
422  *  IfIndex     [Out] returns found interface index
423  *
424  * RETURNS
425  *  Success: NO_ERROR
426  *  Failure: error code from winerror.h
427  */
428 DWORD WINAPI GetAdapterIndex(LPWSTR AdapterName, PULONG IfIndex)
429 {
430   char adapterName[MAX_ADAPTER_NAME];
431   unsigned int i;
432   DWORD ret;
433
434   TRACE("(AdapterName %p, IfIndex %p)\n", AdapterName, IfIndex);
435   /* The adapter name is guaranteed not to have any unicode characters, so
436    * this translation is never lossy */
437   for (i = 0; i < sizeof(adapterName) - 1 && AdapterName[i]; i++)
438     adapterName[i] = (char)AdapterName[i];
439   adapterName[i] = '\0';
440   ret = getInterfaceIndexByName(adapterName, IfIndex);
441   TRACE("returning %d\n", ret);
442   return ret;
443 }
444
445
446 /******************************************************************
447  *    GetAdaptersInfo (IPHLPAPI.@)
448  *
449  * Get information about adapters.
450  *
451  * PARAMS
452  *  pAdapterInfo [Out] buffer for adapter infos
453  *  pOutBufLen   [In]  length of output buffer
454  *
455  * RETURNS
456  *  Success: NO_ERROR
457  *  Failure: error code from winerror.h
458  */
459 DWORD WINAPI GetAdaptersInfo(PIP_ADAPTER_INFO pAdapterInfo, PULONG pOutBufLen)
460 {
461   DWORD ret;
462
463   TRACE("pAdapterInfo %p, pOutBufLen %p\n", pAdapterInfo, pOutBufLen);
464   if (!pOutBufLen)
465     ret = ERROR_INVALID_PARAMETER;
466   else {
467     DWORD numNonLoopbackInterfaces = getNumNonLoopbackInterfaces();
468
469     if (numNonLoopbackInterfaces > 0) {
470       DWORD numIPAddresses = getNumIPAddresses();
471       ULONG size;
472
473       /* This may slightly overestimate the amount of space needed, because
474        * the IP addresses include the loopback address, but it's easier
475        * to make sure there's more than enough space than to make sure there's
476        * precisely enough space.
477        */
478       size = sizeof(IP_ADAPTER_INFO) * numNonLoopbackInterfaces;
479       size += numIPAddresses  * sizeof(IP_ADDR_STRING); 
480       if (!pAdapterInfo || *pOutBufLen < size) {
481         *pOutBufLen = size;
482         ret = ERROR_BUFFER_OVERFLOW;
483       }
484       else {
485         InterfaceIndexTable *table = NULL;
486         PMIB_IPADDRTABLE ipAddrTable = NULL;
487         PMIB_IPFORWARDTABLE routeTable = NULL;
488
489         ret = getIPAddrTable(&ipAddrTable, GetProcessHeap(), 0);
490         if (!ret)
491           ret = AllocateAndGetIpForwardTableFromStack(&routeTable, FALSE, GetProcessHeap(), 0);
492         if (!ret)
493           table = getNonLoopbackInterfaceIndexTable();
494         if (table) {
495           size = sizeof(IP_ADAPTER_INFO) * table->numIndexes;
496           size += ipAddrTable->dwNumEntries * sizeof(IP_ADDR_STRING); 
497           if (*pOutBufLen < size) {
498             *pOutBufLen = size;
499             ret = ERROR_INSUFFICIENT_BUFFER;
500           }
501           else {
502             DWORD ndx;
503             HKEY hKey;
504             BOOL winsEnabled = FALSE;
505             IP_ADDRESS_STRING primaryWINS, secondaryWINS;
506             PIP_ADDR_STRING nextIPAddr = (PIP_ADDR_STRING)((LPBYTE)pAdapterInfo
507              + numNonLoopbackInterfaces * sizeof(IP_ADAPTER_INFO));
508
509             memset(pAdapterInfo, 0, size);
510             /* @@ Wine registry key: HKCU\Software\Wine\Network */
511             if (RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Network",
512              &hKey) == ERROR_SUCCESS) {
513               DWORD size = sizeof(primaryWINS.String);
514               unsigned long addr;
515
516               RegQueryValueExA(hKey, "WinsServer", NULL, NULL,
517                (LPBYTE)primaryWINS.String, &size);
518               addr = inet_addr(primaryWINS.String);
519               if (addr != INADDR_NONE && addr != INADDR_ANY)
520                 winsEnabled = TRUE;
521               size = sizeof(secondaryWINS.String);
522               RegQueryValueExA(hKey, "BackupWinsServer", NULL, NULL,
523                (LPBYTE)secondaryWINS.String, &size);
524               addr = inet_addr(secondaryWINS.String);
525               if (addr != INADDR_NONE && addr != INADDR_ANY)
526                 winsEnabled = TRUE;
527               RegCloseKey(hKey);
528             }
529             for (ndx = 0; ndx < table->numIndexes; ndx++) {
530               PIP_ADAPTER_INFO ptr = &pAdapterInfo[ndx];
531               DWORD i;
532               PIP_ADDR_STRING currentIPAddr = &ptr->IpAddressList;
533               BOOL firstIPAddr = TRUE;
534
535               /* on Win98 this is left empty, but whatever */
536               getInterfaceNameByIndex(table->indexes[ndx], ptr->AdapterName);
537               getInterfaceNameByIndex(table->indexes[ndx], ptr->Description);
538               ptr->AddressLength = sizeof(ptr->Address);
539               getInterfacePhysicalByIndex(table->indexes[ndx],
540                &ptr->AddressLength, ptr->Address, &ptr->Type);
541               ptr->Index = table->indexes[ndx];
542               for (i = 0; i < ipAddrTable->dwNumEntries; i++) {
543                 if (ipAddrTable->table[i].dwIndex == ptr->Index) {
544                   if (firstIPAddr) {
545                     toIPAddressString(ipAddrTable->table[i].dwAddr,
546                      ptr->IpAddressList.IpAddress.String);
547                     toIPAddressString(ipAddrTable->table[i].dwMask,
548                      ptr->IpAddressList.IpMask.String);
549                     firstIPAddr = FALSE;
550                   }
551                   else {
552                     currentIPAddr->Next = nextIPAddr;
553                     currentIPAddr = nextIPAddr;
554                     toIPAddressString(ipAddrTable->table[i].dwAddr,
555                      currentIPAddr->IpAddress.String);
556                     toIPAddressString(ipAddrTable->table[i].dwMask,
557                      currentIPAddr->IpMask.String);
558                     nextIPAddr++;
559                   }
560                 }
561               }
562               /* Find first router through this interface, which we'll assume
563                * is the default gateway for this adapter */
564               for (i = 0; i < routeTable->dwNumEntries; i++)
565                 if (routeTable->table[i].dwForwardIfIndex == ptr->Index
566                  && routeTable->table[i].dwForwardType ==
567                  MIB_IPROUTE_TYPE_INDIRECT)
568                   toIPAddressString(routeTable->table[i].dwForwardNextHop,
569                    ptr->GatewayList.IpAddress.String);
570               if (winsEnabled) {
571                 ptr->HaveWins = TRUE;
572                 memcpy(ptr->PrimaryWinsServer.IpAddress.String,
573                  primaryWINS.String, sizeof(primaryWINS.String));
574                 memcpy(ptr->SecondaryWinsServer.IpAddress.String,
575                  secondaryWINS.String, sizeof(secondaryWINS.String));
576               }
577               if (ndx < table->numIndexes - 1)
578                 ptr->Next = &pAdapterInfo[ndx + 1];
579               else
580                 ptr->Next = NULL;
581             }
582             ret = NO_ERROR;
583           }
584           HeapFree(GetProcessHeap(), 0, table);
585         }
586         else
587           ret = ERROR_OUTOFMEMORY;
588         HeapFree(GetProcessHeap(), 0, routeTable);
589         HeapFree(GetProcessHeap(), 0, ipAddrTable);
590       }
591     }
592     else
593       ret = ERROR_NO_DATA;
594   }
595   TRACE("returning %d\n", ret);
596   return ret;
597 }
598
599 static DWORD typeFromMibType(DWORD mib_type)
600 {
601     switch (mib_type)
602     {
603     case MIB_IF_TYPE_ETHERNET:  return IF_TYPE_ETHERNET_CSMACD;
604     case MIB_IF_TYPE_TOKENRING: return IF_TYPE_ISO88025_TOKENRING;
605     case MIB_IF_TYPE_PPP:       return IF_TYPE_PPP;
606     case MIB_IF_TYPE_LOOPBACK:  return IF_TYPE_SOFTWARE_LOOPBACK;
607     default:                    return IF_TYPE_OTHER;
608     }
609 }
610
611 static DWORD connectionTypeFromMibType(DWORD mib_type)
612 {
613     switch (mib_type)
614     {
615     case MIB_IF_TYPE_PPP:       return NET_IF_CONNECTION_DEMAND;
616     case MIB_IF_TYPE_SLIP:      return NET_IF_CONNECTION_DEMAND;
617     default:                    return NET_IF_CONNECTION_DEDICATED;
618     }
619 }
620
621 static ULONG v4addressesFromIndex(DWORD index, DWORD **addrs, ULONG *num_addrs)
622 {
623     ULONG ret, i, j;
624     MIB_IPADDRTABLE *at;
625
626     *num_addrs = 0;
627     if ((ret = getIPAddrTable(&at, GetProcessHeap(), 0))) return ret;
628     for (i = 0; i < at->dwNumEntries; i++)
629     {
630         if (at->table[i].dwIndex == index) (*num_addrs)++;
631     }
632     if (!(*addrs = HeapAlloc(GetProcessHeap(), 0, *num_addrs * sizeof(DWORD))))
633     {
634         HeapFree(GetProcessHeap(), 0, at);
635         return ERROR_OUTOFMEMORY;
636     }
637     for (i = 0, j = 0; i < at->dwNumEntries; i++)
638     {
639         if (at->table[i].dwIndex == index) (*addrs)[j++] = at->table[i].dwAddr;
640     }
641     HeapFree(GetProcessHeap(), 0, at);
642     return ERROR_SUCCESS;
643 }
644
645 static char *debugstr_ipv4(const in_addr_t *in_addr, char *buf)
646 {
647     const BYTE *addrp;
648     char *p = buf;
649
650     for (addrp = (const BYTE *)in_addr;
651      addrp - (const BYTE *)in_addr < sizeof(*in_addr);
652      addrp++)
653     {
654         if (addrp == (const BYTE *)in_addr + sizeof(*in_addr) - 1)
655             sprintf(p, "%d", *addrp);
656         else
657         {
658             int n;
659
660             sprintf(p, "%d.%n", *addrp, &n);
661             p += n;
662         }
663     }
664     return buf;
665 }
666
667 static char *debugstr_ipv6(const struct WS_sockaddr_in6 *sin, char *buf)
668 {
669     const IN6_ADDR *addr = &sin->sin6_addr;
670     char *p = buf;
671     int i;
672     BOOL in_zero = FALSE;
673
674     for (i = 0; i < 7; i++)
675     {
676         if (!addr->u.Word[i])
677         {
678             if (i == 0)
679                 *p++ = ':';
680             if (!in_zero)
681             {
682                 *p++ = ':';
683                 in_zero = TRUE;
684             }
685         }
686         else
687         {
688             int n;
689
690             sprintf(p, "%x:%n", ntohs(addr->u.Word[i]), &n);
691             p += n;
692             in_zero = FALSE;
693         }
694     }
695     sprintf(p, "%x", ntohs(addr->u.Word[7]));
696     return buf;
697 }
698
699 static ULONG count_v4_gateways(DWORD index, PMIB_IPFORWARDTABLE routeTable)
700 {
701     DWORD i, num_gateways = 0;
702
703     for (i = 0; i < routeTable->dwNumEntries; i++)
704     {
705         if (routeTable->table[i].dwForwardIfIndex == index &&
706             routeTable->table[i].dwForwardType == MIB_IPROUTE_TYPE_INDIRECT)
707             num_gateways++;
708     }
709     return num_gateways;
710 }
711
712 static PMIB_IPFORWARDROW findIPv4Gateway(DWORD index,
713                                          PMIB_IPFORWARDTABLE routeTable)
714 {
715     DWORD i;
716     PMIB_IPFORWARDROW row = NULL;
717
718     for (i = 0; !row && i < routeTable->dwNumEntries; i++)
719     {
720         if (routeTable->table[i].dwForwardIfIndex == index &&
721             routeTable->table[i].dwForwardType == MIB_IPROUTE_TYPE_INDIRECT)
722             row = &routeTable->table[i];
723     }
724     return row;
725 }
726
727 static ULONG adapterAddressesFromIndex(ULONG family, DWORD index, IP_ADAPTER_ADDRESSES *aa, ULONG *size)
728 {
729     ULONG ret, i, num_v4addrs = 0, num_v4_gateways = 0, num_v6addrs = 0, total_size;
730     DWORD *v4addrs = NULL;
731     SOCKET_ADDRESS *v6addrs = NULL;
732     PMIB_IPFORWARDTABLE routeTable = NULL;
733
734     if (family == AF_INET)
735     {
736         ret = AllocateAndGetIpForwardTableFromStack(&routeTable, FALSE,
737                                                     GetProcessHeap(), 0);
738         if (!ret)
739         {
740             ret = v4addressesFromIndex(index, &v4addrs, &num_v4addrs);
741             num_v4_gateways = count_v4_gateways(index, routeTable);
742         }
743     }
744     else if (family == AF_INET6)
745         ret = v6addressesFromIndex(index, &v6addrs, &num_v6addrs);
746     else if (family == AF_UNSPEC)
747     {
748         ret = AllocateAndGetIpForwardTableFromStack(&routeTable, FALSE,
749                                                     GetProcessHeap(), 0);
750         if (!ret)
751         {
752             ret = v4addressesFromIndex(index, &v4addrs, &num_v4addrs);
753             num_v4_gateways = count_v4_gateways(index, routeTable);
754             if (!ret)
755                 ret = v6addressesFromIndex(index, &v6addrs, &num_v6addrs);
756         }
757     }
758     else
759     {
760         FIXME("address family %u unsupported\n", family);
761         ret = ERROR_NO_DATA;
762     }
763     if (ret)
764     {
765         HeapFree(GetProcessHeap(), 0, routeTable);
766         return ret;
767     }
768
769     total_size = sizeof(IP_ADAPTER_ADDRESSES);
770     total_size += IF_NAMESIZE;
771     total_size += 2 * IF_NAMESIZE * sizeof(WCHAR);
772     total_size += sizeof(IP_ADAPTER_UNICAST_ADDRESS) * num_v4addrs;
773     total_size += sizeof(struct sockaddr_in) * num_v4addrs;
774     total_size += (sizeof(IP_ADAPTER_GATEWAY_ADDRESS) + sizeof(SOCKADDR_IN)) * num_v4_gateways;
775     total_size += sizeof(IP_ADAPTER_UNICAST_ADDRESS) * num_v6addrs;
776     total_size += sizeof(SOCKET_ADDRESS) * num_v6addrs;
777     for (i = 0; i < num_v6addrs; i++)
778         total_size += v6addrs[i].iSockaddrLength;
779
780     if (aa && *size >= total_size)
781     {
782         char name[IF_NAMESIZE], *ptr = (char *)aa + sizeof(IP_ADAPTER_ADDRESSES), *src;
783         WCHAR *dst;
784         DWORD buflen, type, status;
785
786         memset(aa, 0, sizeof(IP_ADAPTER_ADDRESSES));
787         aa->u.s.Length  = sizeof(IP_ADAPTER_ADDRESSES);
788         aa->u.s.IfIndex = index;
789
790         getInterfaceNameByIndex(index, name);
791         memcpy(ptr, name, IF_NAMESIZE);
792         aa->AdapterName = ptr;
793         ptr += IF_NAMESIZE;
794         aa->FriendlyName = (WCHAR *)ptr;
795         for (src = name, dst = (WCHAR *)ptr; *src; src++, dst++)
796             *dst = *src;
797         *dst++ = 0;
798         ptr = (char *)dst;
799         aa->Description = (WCHAR *)ptr;
800         for (src = name, dst = (WCHAR *)ptr; *src; src++, dst++)
801             *dst = *src;
802         *dst++ = 0;
803         ptr = (char *)dst;
804
805         TRACE("%s: %d IPv4 addresses, %d IPv6 addresses:\n", name, num_v4addrs,
806               num_v6addrs);
807         if (num_v4_gateways)
808         {
809             PMIB_IPFORWARDROW adapterRow;
810
811             if ((adapterRow = findIPv4Gateway(index, routeTable)))
812             {
813                 PIP_ADAPTER_GATEWAY_ADDRESS gw;
814                 PSOCKADDR_IN sin;
815
816                 gw = (PIP_ADAPTER_GATEWAY_ADDRESS)ptr;
817                 aa->FirstGatewayAddress = gw;
818
819                 gw->u.s.Length = sizeof(IP_ADAPTER_GATEWAY_ADDRESS);
820                 ptr += sizeof(IP_ADAPTER_GATEWAY_ADDRESS);
821                 sin = (PSOCKADDR_IN)ptr;
822                 sin->sin_family = AF_INET;
823                 sin->sin_port = 0;
824                 memcpy(&sin->sin_addr, &adapterRow->dwForwardNextHop,
825                        sizeof(DWORD));
826                 gw->Address.lpSockaddr = (LPSOCKADDR)sin;
827                 gw->Address.iSockaddrLength = sizeof(SOCKADDR_IN);
828                 gw->Next = NULL;
829                 ptr += sizeof(SOCKADDR_IN);
830             }
831         }
832         if (num_v4addrs)
833         {
834             IP_ADAPTER_UNICAST_ADDRESS *ua;
835             struct sockaddr_in *sa;
836             aa->Flags |= IP_ADAPTER_IPV4_ENABLED;
837             ua = aa->FirstUnicastAddress = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
838             for (i = 0; i < num_v4addrs; i++)
839             {
840                 char addr_buf[16];
841
842                 memset(ua, 0, sizeof(IP_ADAPTER_UNICAST_ADDRESS));
843                 ua->u.s.Length              = sizeof(IP_ADAPTER_UNICAST_ADDRESS);
844                 ua->Address.iSockaddrLength = sizeof(struct sockaddr_in);
845                 ua->Address.lpSockaddr      = (SOCKADDR *)((char *)ua + ua->u.s.Length);
846
847                 sa = (struct sockaddr_in *)ua->Address.lpSockaddr;
848                 sa->sin_family      = AF_INET;
849                 sa->sin_addr.s_addr = v4addrs[i];
850                 sa->sin_port        = 0;
851                 TRACE("IPv4 %d/%d: %s\n", i + 1, num_v4addrs,
852                       debugstr_ipv4(&sa->sin_addr.s_addr, addr_buf));
853
854                 ptr += ua->u.s.Length + ua->Address.iSockaddrLength;
855                 if (i < num_v4addrs - 1)
856                 {
857                     ua->Next = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
858                     ua = ua->Next;
859                 }
860             }
861         }
862         if (num_v6addrs)
863         {
864             IP_ADAPTER_UNICAST_ADDRESS *ua;
865             struct WS_sockaddr_in6 *sa;
866
867             aa->Flags |= IP_ADAPTER_IPV6_ENABLED;
868             if (aa->FirstUnicastAddress)
869             {
870                 for (ua = aa->FirstUnicastAddress; ua->Next; ua = ua->Next)
871                     ;
872                 ua->Next = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
873                 ua = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
874             }
875             else
876                 ua = aa->FirstUnicastAddress = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
877             for (i = 0; i < num_v6addrs; i++)
878             {
879                 char addr_buf[46];
880
881                 memset(ua, 0, sizeof(IP_ADAPTER_UNICAST_ADDRESS));
882                 ua->u.s.Length              = sizeof(IP_ADAPTER_UNICAST_ADDRESS);
883                 ua->Address.iSockaddrLength = v6addrs[i].iSockaddrLength;
884                 ua->Address.lpSockaddr      = (SOCKADDR *)((char *)ua + ua->u.s.Length);
885
886                 sa = (struct WS_sockaddr_in6 *)ua->Address.lpSockaddr;
887                 memcpy(sa, v6addrs[i].lpSockaddr, sizeof(*sa));
888                 TRACE("IPv6 %d/%d: %s\n", i + 1, num_v6addrs,
889                       debugstr_ipv6(sa, addr_buf));
890
891                 ptr += ua->u.s.Length + ua->Address.iSockaddrLength;
892                 if (i < num_v6addrs - 1)
893                 {
894                     ua->Next = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
895                     ua = ua->Next;
896                 }
897             }
898         }
899
900         buflen = MAX_INTERFACE_PHYSADDR;
901         getInterfacePhysicalByIndex(index, &buflen, aa->PhysicalAddress, &type);
902         aa->PhysicalAddressLength = buflen;
903         aa->IfType = typeFromMibType(type);
904         aa->ConnectionType = connectionTypeFromMibType(type);
905
906         getInterfaceMtuByName(name, &aa->Mtu);
907
908         getInterfaceStatusByName(name, &status);
909         if (status == MIB_IF_OPER_STATUS_OPERATIONAL) aa->OperStatus = IfOperStatusUp;
910         else if (status == MIB_IF_OPER_STATUS_NON_OPERATIONAL) aa->OperStatus = IfOperStatusDown;
911         else aa->OperStatus = IfOperStatusUnknown;
912     }
913     *size = total_size;
914     HeapFree(GetProcessHeap(), 0, routeTable);
915     HeapFree(GetProcessHeap(), 0, v6addrs);
916     HeapFree(GetProcessHeap(), 0, v4addrs);
917     return ERROR_SUCCESS;
918 }
919
920 ULONG WINAPI GetAdaptersAddresses(ULONG family, ULONG flags, PVOID reserved,
921                                   PIP_ADAPTER_ADDRESSES aa, PULONG buflen)
922 {
923     InterfaceIndexTable *table;
924     ULONG i, size, total_size, ret = ERROR_NO_DATA;
925
926     TRACE("(%d, %08x, %p, %p, %p)\n", family, flags, reserved, aa, buflen);
927
928     if (!buflen) return ERROR_INVALID_PARAMETER;
929
930     table = getInterfaceIndexTable();
931     if (!table || !table->numIndexes)
932     {
933         HeapFree(GetProcessHeap(), 0, table);
934         return ERROR_NO_DATA;
935     }
936     total_size = 0;
937     for (i = 0; i < table->numIndexes; i++)
938     {
939         size = 0;
940         if ((ret = adapterAddressesFromIndex(family, table->indexes[i], NULL, &size)))
941         {
942             HeapFree(GetProcessHeap(), 0, table);
943             return ret;
944         }
945         total_size += size;
946     }
947     if (aa && *buflen >= total_size)
948     {
949         ULONG bytes_left = size = total_size;
950         for (i = 0; i < table->numIndexes; i++)
951         {
952             if ((ret = adapterAddressesFromIndex(family, table->indexes[i], aa, &size)))
953             {
954                 HeapFree(GetProcessHeap(), 0, table);
955                 return ret;
956             }
957             if (i < table->numIndexes - 1)
958             {
959                 aa->Next = (IP_ADAPTER_ADDRESSES *)((char *)aa + size);
960                 aa = aa->Next;
961                 size = bytes_left -= size;
962             }
963         }
964         ret = ERROR_SUCCESS;
965     }
966     if (*buflen < total_size) ret = ERROR_BUFFER_OVERFLOW;
967     *buflen = total_size;
968
969     TRACE("num adapters %u\n", table->numIndexes);
970     HeapFree(GetProcessHeap(), 0, table);
971     return ret;
972 }
973
974 /******************************************************************
975  *    GetBestInterface (IPHLPAPI.@)
976  *
977  * Get the interface, with the best route for the given IP address.
978  *
979  * PARAMS
980  *  dwDestAddr     [In]  IP address to search the interface for
981  *  pdwBestIfIndex [Out] found best interface
982  *
983  * RETURNS
984  *  Success: NO_ERROR
985  *  Failure: error code from winerror.h
986  */
987 DWORD WINAPI GetBestInterface(IPAddr dwDestAddr, PDWORD pdwBestIfIndex)
988 {
989     struct WS_sockaddr_in sa_in;
990     memset(&sa_in, 0, sizeof(sa_in));
991     sa_in.sin_family = AF_INET;
992     sa_in.sin_addr.S_un.S_addr = dwDestAddr;
993     return GetBestInterfaceEx((struct WS_sockaddr *)&sa_in, pdwBestIfIndex);
994 }
995
996 /******************************************************************
997  *    GetBestInterfaceEx (IPHLPAPI.@)
998  *
999  * Get the interface, with the best route for the given IP address.
1000  *
1001  * PARAMS
1002  *  dwDestAddr     [In]  IP address to search the interface for
1003  *  pdwBestIfIndex [Out] found best interface
1004  *
1005  * RETURNS
1006  *  Success: NO_ERROR
1007  *  Failure: error code from winerror.h
1008  */
1009 DWORD WINAPI GetBestInterfaceEx(struct WS_sockaddr *pDestAddr, PDWORD pdwBestIfIndex)
1010 {
1011   DWORD ret;
1012
1013   TRACE("pDestAddr %p, pdwBestIfIndex %p\n", pDestAddr, pdwBestIfIndex);
1014   if (!pDestAddr || !pdwBestIfIndex)
1015     ret = ERROR_INVALID_PARAMETER;
1016   else {
1017     MIB_IPFORWARDROW ipRow;
1018
1019     if (pDestAddr->sa_family == AF_INET) {
1020       ret = GetBestRoute(((struct WS_sockaddr_in *)pDestAddr)->sin_addr.S_un.S_addr, 0, &ipRow);
1021       if (ret == ERROR_SUCCESS)
1022         *pdwBestIfIndex = ipRow.dwForwardIfIndex;
1023     } else {
1024       FIXME("address family %d not supported\n", pDestAddr->sa_family);
1025       ret = ERROR_NOT_SUPPORTED;
1026     }
1027   }
1028   TRACE("returning %d\n", ret);
1029   return ret;
1030 }
1031
1032
1033 /******************************************************************
1034  *    GetBestRoute (IPHLPAPI.@)
1035  *
1036  * Get the best route for the given IP address.
1037  *
1038  * PARAMS
1039  *  dwDestAddr   [In]  IP address to search the best route for
1040  *  dwSourceAddr [In]  optional source IP address
1041  *  pBestRoute   [Out] found best route
1042  *
1043  * RETURNS
1044  *  Success: NO_ERROR
1045  *  Failure: error code from winerror.h
1046  */
1047 DWORD WINAPI GetBestRoute(DWORD dwDestAddr, DWORD dwSourceAddr, PMIB_IPFORWARDROW pBestRoute)
1048 {
1049   PMIB_IPFORWARDTABLE table;
1050   DWORD ret;
1051
1052   TRACE("dwDestAddr 0x%08x, dwSourceAddr 0x%08x, pBestRoute %p\n", dwDestAddr,
1053    dwSourceAddr, pBestRoute);
1054   if (!pBestRoute)
1055     return ERROR_INVALID_PARAMETER;
1056
1057   ret = AllocateAndGetIpForwardTableFromStack(&table, FALSE, GetProcessHeap(), 0);
1058   if (!ret) {
1059     DWORD ndx, matchedBits, matchedNdx = table->dwNumEntries;
1060
1061     for (ndx = 0, matchedBits = 0; ndx < table->dwNumEntries; ndx++) {
1062       if (table->table[ndx].dwForwardType != MIB_IPROUTE_TYPE_INVALID &&
1063        (dwDestAddr & table->table[ndx].dwForwardMask) ==
1064        (table->table[ndx].dwForwardDest & table->table[ndx].dwForwardMask)) {
1065         DWORD numShifts, mask;
1066
1067         for (numShifts = 0, mask = table->table[ndx].dwForwardMask;
1068          mask && mask & 1; mask >>= 1, numShifts++)
1069           ;
1070         if (numShifts > matchedBits) {
1071           matchedBits = numShifts;
1072           matchedNdx = ndx;
1073         }
1074         else if (!matchedBits) {
1075           matchedNdx = ndx;
1076         }
1077       }
1078     }
1079     if (matchedNdx < table->dwNumEntries) {
1080       memcpy(pBestRoute, &table->table[matchedNdx], sizeof(MIB_IPFORWARDROW));
1081       ret = ERROR_SUCCESS;
1082     }
1083     else {
1084       /* No route matches, which can happen if there's no default route. */
1085       ret = ERROR_HOST_UNREACHABLE;
1086     }
1087     HeapFree(GetProcessHeap(), 0, table);
1088   }
1089   TRACE("returning %d\n", ret);
1090   return ret;
1091 }
1092
1093
1094 /******************************************************************
1095  *    GetFriendlyIfIndex (IPHLPAPI.@)
1096  *
1097  * Get a "friendly" version of IfIndex, which is one that doesn't
1098  * have the top byte set.  Doesn't validate whether IfIndex is a valid
1099  * adapter index.
1100  *
1101  * PARAMS
1102  *  IfIndex [In] interface index to get the friendly one for
1103  *
1104  * RETURNS
1105  *  A friendly version of IfIndex.
1106  */
1107 DWORD WINAPI GetFriendlyIfIndex(DWORD IfIndex)
1108 {
1109   /* windows doesn't validate these, either, just makes sure the top byte is
1110      cleared.  I assume my ifenum module never gives an index with the top
1111      byte set. */
1112   TRACE("returning %d\n", IfIndex);
1113   return IfIndex;
1114 }
1115
1116
1117 /******************************************************************
1118  *    GetIfEntry (IPHLPAPI.@)
1119  *
1120  * Get information about an interface.
1121  *
1122  * PARAMS
1123  *  pIfRow [In/Out] In:  dwIndex of MIB_IFROW selects the interface.
1124  *                  Out: interface information
1125  *
1126  * RETURNS
1127  *  Success: NO_ERROR
1128  *  Failure: error code from winerror.h
1129  */
1130 DWORD WINAPI GetIfEntry(PMIB_IFROW pIfRow)
1131 {
1132   DWORD ret;
1133   char nameBuf[MAX_ADAPTER_NAME];
1134   char *name;
1135
1136   TRACE("pIfRow %p\n", pIfRow);
1137   if (!pIfRow)
1138     return ERROR_INVALID_PARAMETER;
1139
1140   name = getInterfaceNameByIndex(pIfRow->dwIndex, nameBuf);
1141   if (name) {
1142     ret = getInterfaceEntryByName(name, pIfRow);
1143     if (ret == NO_ERROR)
1144       ret = getInterfaceStatsByName(name, pIfRow);
1145   }
1146   else
1147     ret = ERROR_INVALID_DATA;
1148   TRACE("returning %d\n", ret);
1149   return ret;
1150 }
1151
1152
1153 static int IfTableSorter(const void *a, const void *b)
1154 {
1155   int ret;
1156
1157   if (a && b)
1158     ret = ((const MIB_IFROW*)a)->dwIndex - ((const MIB_IFROW*)b)->dwIndex;
1159   else
1160     ret = 0;
1161   return ret;
1162 }
1163
1164
1165 /******************************************************************
1166  *    GetIfTable (IPHLPAPI.@)
1167  *
1168  * Get a table of local interfaces.
1169  *
1170  * PARAMS
1171  *  pIfTable [Out]    buffer for local interfaces table
1172  *  pdwSize  [In/Out] length of output buffer
1173  *  bOrder   [In]     whether to sort the table
1174  *
1175  * RETURNS
1176  *  Success: NO_ERROR
1177  *  Failure: error code from winerror.h
1178  *
1179  * NOTES
1180  *  If pdwSize is less than required, the function will return
1181  *  ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the required byte
1182  *  size.
1183  *  If bOrder is true, the returned table will be sorted by interface index.
1184  */
1185 DWORD WINAPI GetIfTable(PMIB_IFTABLE pIfTable, PULONG pdwSize, BOOL bOrder)
1186 {
1187   DWORD ret;
1188
1189   TRACE("pIfTable %p, pdwSize %p, bOrder %d\n", pdwSize, pdwSize,
1190    (DWORD)bOrder);
1191   if (!pdwSize)
1192     ret = ERROR_INVALID_PARAMETER;
1193   else {
1194     DWORD numInterfaces = getNumInterfaces();
1195     ULONG size = sizeof(MIB_IFTABLE);
1196
1197     if (numInterfaces > 1)
1198       size += (numInterfaces - 1) * sizeof(MIB_IFROW);
1199     if (!pIfTable || *pdwSize < size) {
1200       *pdwSize = size;
1201       ret = ERROR_INSUFFICIENT_BUFFER;
1202     }
1203     else {
1204       InterfaceIndexTable *table = getInterfaceIndexTable();
1205
1206       if (table) {
1207         size = sizeof(MIB_IFTABLE);
1208         if (table->numIndexes > 1)
1209           size += (table->numIndexes - 1) * sizeof(MIB_IFROW);
1210         if (*pdwSize < size) {
1211           *pdwSize = size;
1212           ret = ERROR_INSUFFICIENT_BUFFER;
1213         }
1214         else {
1215           DWORD ndx;
1216
1217           *pdwSize = size;
1218           pIfTable->dwNumEntries = 0;
1219           for (ndx = 0; ndx < table->numIndexes; ndx++) {
1220             pIfTable->table[ndx].dwIndex = table->indexes[ndx];
1221             GetIfEntry(&pIfTable->table[ndx]);
1222             pIfTable->dwNumEntries++;
1223           }
1224           if (bOrder)
1225             qsort(pIfTable->table, pIfTable->dwNumEntries, sizeof(MIB_IFROW),
1226              IfTableSorter);
1227           ret = NO_ERROR;
1228         }
1229         HeapFree(GetProcessHeap(), 0, table);
1230       }
1231       else
1232         ret = ERROR_OUTOFMEMORY;
1233     }
1234   }
1235   TRACE("returning %d\n", ret);
1236   return ret;
1237 }
1238
1239
1240 /******************************************************************
1241  *    GetInterfaceInfo (IPHLPAPI.@)
1242  *
1243  * Get a list of network interface adapters.
1244  *
1245  * PARAMS
1246  *  pIfTable    [Out] buffer for interface adapters
1247  *  dwOutBufLen [Out] if buffer is too small, returns required size
1248  *
1249  * RETURNS
1250  *  Success: NO_ERROR
1251  *  Failure: error code from winerror.h
1252  *
1253  * BUGS
1254  *  MSDN states this should return non-loopback interfaces only.
1255  */
1256 DWORD WINAPI GetInterfaceInfo(PIP_INTERFACE_INFO pIfTable, PULONG dwOutBufLen)
1257 {
1258   DWORD ret;
1259
1260   TRACE("pIfTable %p, dwOutBufLen %p\n", pIfTable, dwOutBufLen);
1261   if (!dwOutBufLen)
1262     ret = ERROR_INVALID_PARAMETER;
1263   else {
1264     DWORD numInterfaces = getNumInterfaces();
1265     ULONG size = sizeof(IP_INTERFACE_INFO);
1266
1267     if (numInterfaces > 1)
1268       size += (numInterfaces - 1) * sizeof(IP_ADAPTER_INDEX_MAP);
1269     if (!pIfTable || *dwOutBufLen < size) {
1270       *dwOutBufLen = size;
1271       ret = ERROR_INSUFFICIENT_BUFFER;
1272     }
1273     else {
1274       InterfaceIndexTable *table = getInterfaceIndexTable();
1275
1276       if (table) {
1277         size = sizeof(IP_INTERFACE_INFO);
1278         if (table->numIndexes > 1)
1279           size += (table->numIndexes - 1) * sizeof(IP_ADAPTER_INDEX_MAP);
1280         if (*dwOutBufLen < size) {
1281           *dwOutBufLen = size;
1282           ret = ERROR_INSUFFICIENT_BUFFER;
1283         }
1284         else {
1285           DWORD ndx;
1286           char nameBuf[MAX_ADAPTER_NAME];
1287
1288           *dwOutBufLen = size;
1289           pIfTable->NumAdapters = 0;
1290           for (ndx = 0; ndx < table->numIndexes; ndx++) {
1291             const char *walker, *name;
1292             WCHAR *assigner;
1293
1294             pIfTable->Adapter[ndx].Index = table->indexes[ndx];
1295             name = getInterfaceNameByIndex(table->indexes[ndx], nameBuf);
1296             for (walker = name, assigner = pIfTable->Adapter[ndx].Name;
1297              walker && *walker &&
1298              assigner - pIfTable->Adapter[ndx].Name < MAX_ADAPTER_NAME - 1;
1299              walker++, assigner++)
1300               *assigner = *walker;
1301             *assigner = 0;
1302             pIfTable->NumAdapters++;
1303           }
1304           ret = NO_ERROR;
1305         }
1306         HeapFree(GetProcessHeap(), 0, table);
1307       }
1308       else
1309         ret = ERROR_OUTOFMEMORY;
1310     }
1311   }
1312   TRACE("returning %d\n", ret);
1313   return ret;
1314 }
1315
1316
1317 /******************************************************************
1318  *    GetIpAddrTable (IPHLPAPI.@)
1319  *
1320  * Get interface-to-IP address mapping table. 
1321  *
1322  * PARAMS
1323  *  pIpAddrTable [Out]    buffer for mapping table
1324  *  pdwSize      [In/Out] length of output buffer
1325  *  bOrder       [In]     whether to sort the table
1326  *
1327  * RETURNS
1328  *  Success: NO_ERROR
1329  *  Failure: error code from winerror.h
1330  *
1331  * NOTES
1332  *  If pdwSize is less than required, the function will return
1333  *  ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the required byte
1334  *  size.
1335  *  If bOrder is true, the returned table will be sorted by the next hop and
1336  *  an assortment of arbitrary parameters.
1337  */
1338 DWORD WINAPI GetIpAddrTable(PMIB_IPADDRTABLE pIpAddrTable, PULONG pdwSize, BOOL bOrder)
1339 {
1340   DWORD ret;
1341
1342   TRACE("pIpAddrTable %p, pdwSize %p, bOrder %d\n", pIpAddrTable, pdwSize,
1343    (DWORD)bOrder);
1344   if (!pdwSize)
1345     ret = ERROR_INVALID_PARAMETER;
1346   else {
1347     PMIB_IPADDRTABLE table;
1348
1349     ret = getIPAddrTable(&table, GetProcessHeap(), 0);
1350     if (ret == NO_ERROR)
1351     {
1352       ULONG size = sizeof(MIB_IPADDRTABLE);
1353
1354       if (table->dwNumEntries > 1)
1355         size += (table->dwNumEntries - 1) * sizeof(MIB_IPADDRROW);
1356       if (!pIpAddrTable || *pdwSize < size) {
1357         *pdwSize = size;
1358         ret = ERROR_INSUFFICIENT_BUFFER;
1359       }
1360       else {
1361         *pdwSize = size;
1362         memcpy(pIpAddrTable, table, size);
1363         if (bOrder)
1364           qsort(pIpAddrTable->table, pIpAddrTable->dwNumEntries,
1365            sizeof(MIB_IPADDRROW), IpAddrTableSorter);
1366         ret = NO_ERROR;
1367       }
1368       HeapFree(GetProcessHeap(), 0, table);
1369     }
1370   }
1371   TRACE("returning %d\n", ret);
1372   return ret;
1373 }
1374
1375
1376 /******************************************************************
1377  *    GetIpForwardTable (IPHLPAPI.@)
1378  *
1379  * Get the route table.
1380  *
1381  * PARAMS
1382  *  pIpForwardTable [Out]    buffer for route table
1383  *  pdwSize         [In/Out] length of output buffer
1384  *  bOrder          [In]     whether to sort the table
1385  *
1386  * RETURNS
1387  *  Success: NO_ERROR
1388  *  Failure: error code from winerror.h
1389  *
1390  * NOTES
1391  *  If pdwSize is less than required, the function will return
1392  *  ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the required byte
1393  *  size.
1394  *  If bOrder is true, the returned table will be sorted by the next hop and
1395  *  an assortment of arbitrary parameters.
1396  */
1397 DWORD WINAPI GetIpForwardTable(PMIB_IPFORWARDTABLE pIpForwardTable, PULONG pdwSize, BOOL bOrder)
1398 {
1399     DWORD ret;
1400     PMIB_IPFORWARDTABLE table;
1401
1402     TRACE("pIpForwardTable %p, pdwSize %p, bOrder %d\n", pIpForwardTable, pdwSize, bOrder);
1403
1404     if (!pdwSize) return ERROR_INVALID_PARAMETER;
1405
1406     ret = AllocateAndGetIpForwardTableFromStack(&table, bOrder, GetProcessHeap(), 0);
1407     if (!ret) {
1408         DWORD size = FIELD_OFFSET( MIB_IPFORWARDTABLE, table[table->dwNumEntries] );
1409         if (!pIpForwardTable || *pdwSize < size) {
1410           *pdwSize = size;
1411           ret = ERROR_INSUFFICIENT_BUFFER;
1412         }
1413         else {
1414           *pdwSize = size;
1415           memcpy(pIpForwardTable, table, size);
1416         }
1417         HeapFree(GetProcessHeap(), 0, table);
1418     }
1419     TRACE("returning %d\n", ret);
1420     return ret;
1421 }
1422
1423
1424 /******************************************************************
1425  *    GetIpNetTable (IPHLPAPI.@)
1426  *
1427  * Get the IP-to-physical address mapping table.
1428  *
1429  * PARAMS
1430  *  pIpNetTable [Out]    buffer for mapping table
1431  *  pdwSize     [In/Out] length of output buffer
1432  *  bOrder      [In]     whether to sort the table
1433  *
1434  * RETURNS
1435  *  Success: NO_ERROR
1436  *  Failure: error code from winerror.h
1437  *
1438  * NOTES
1439  *  If pdwSize is less than required, the function will return
1440  *  ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the required byte
1441  *  size.
1442  *  If bOrder is true, the returned table will be sorted by IP address.
1443  */
1444 DWORD WINAPI GetIpNetTable(PMIB_IPNETTABLE pIpNetTable, PULONG pdwSize, BOOL bOrder)
1445 {
1446     DWORD ret;
1447     PMIB_IPNETTABLE table;
1448
1449     TRACE("pIpNetTable %p, pdwSize %p, bOrder %d\n", pIpNetTable, pdwSize, bOrder);
1450
1451     if (!pdwSize) return ERROR_INVALID_PARAMETER;
1452
1453     ret = AllocateAndGetIpNetTableFromStack( &table, bOrder, GetProcessHeap(), 0 );
1454     if (!ret) {
1455         DWORD size = FIELD_OFFSET( MIB_IPNETTABLE, table[table->dwNumEntries] );
1456         if (!pIpNetTable || *pdwSize < size) {
1457           *pdwSize = size;
1458           ret = ERROR_INSUFFICIENT_BUFFER;
1459         }
1460         else {
1461           *pdwSize = size;
1462           memcpy(pIpNetTable, table, size);
1463         }
1464         HeapFree(GetProcessHeap(), 0, table);
1465     }
1466     TRACE("returning %d\n", ret);
1467     return ret;
1468 }
1469
1470
1471 /******************************************************************
1472  *    GetNetworkParams (IPHLPAPI.@)
1473  *
1474  * Get the network parameters for the local computer.
1475  *
1476  * PARAMS
1477  *  pFixedInfo [Out]    buffer for network parameters
1478  *  pOutBufLen [In/Out] length of output buffer
1479  *
1480  * RETURNS
1481  *  Success: NO_ERROR
1482  *  Failure: error code from winerror.h
1483  *
1484  * NOTES
1485  *  If pOutBufLen is less than required, the function will return
1486  *  ERROR_INSUFFICIENT_BUFFER, and pOutBufLen will be set to the required byte
1487  *  size.
1488  */
1489 DWORD WINAPI GetNetworkParams(PFIXED_INFO pFixedInfo, PULONG pOutBufLen)
1490 {
1491   DWORD ret, size;
1492   LONG regReturn;
1493   HKEY hKey;
1494
1495   TRACE("pFixedInfo %p, pOutBufLen %p\n", pFixedInfo, pOutBufLen);
1496   if (!pOutBufLen)
1497     return ERROR_INVALID_PARAMETER;
1498
1499   initialise_resolver();
1500   size = sizeof(FIXED_INFO) + (_res.nscount > 0 ? (_res.nscount  - 1) *
1501    sizeof(IP_ADDR_STRING) : 0);
1502   if (!pFixedInfo || *pOutBufLen < size) {
1503     *pOutBufLen = size;
1504     return ERROR_BUFFER_OVERFLOW;
1505   }
1506
1507   memset(pFixedInfo, 0, size);
1508   size = sizeof(pFixedInfo->HostName);
1509   GetComputerNameExA(ComputerNameDnsHostname, pFixedInfo->HostName, &size);
1510   size = sizeof(pFixedInfo->DomainName);
1511   GetComputerNameExA(ComputerNameDnsDomain, pFixedInfo->DomainName, &size);
1512   if (_res.nscount > 0) {
1513     PIP_ADDR_STRING ptr;
1514     int i;
1515
1516     for (i = 0, ptr = &pFixedInfo->DnsServerList; i < _res.nscount && ptr;
1517      i++, ptr = ptr->Next) {
1518       toIPAddressString(_res.nsaddr_list[i].sin_addr.s_addr,
1519        ptr->IpAddress.String);
1520       if (i == _res.nscount - 1)
1521         ptr->Next = NULL;
1522       else if (i == 0)
1523         ptr->Next = (PIP_ADDR_STRING)((LPBYTE)pFixedInfo + sizeof(FIXED_INFO));
1524       else
1525         ptr->Next = (PIP_ADDR_STRING)((PBYTE)ptr + sizeof(IP_ADDR_STRING));
1526     }
1527   }
1528   pFixedInfo->NodeType = HYBRID_NODETYPE;
1529   regReturn = RegOpenKeyExA(HKEY_LOCAL_MACHINE,
1530    "SYSTEM\\CurrentControlSet\\Services\\VxD\\MSTCP", 0, KEY_READ, &hKey);
1531   if (regReturn != ERROR_SUCCESS)
1532     regReturn = RegOpenKeyExA(HKEY_LOCAL_MACHINE,
1533      "SYSTEM\\CurrentControlSet\\Services\\NetBT\\Parameters", 0, KEY_READ,
1534      &hKey);
1535   if (regReturn == ERROR_SUCCESS)
1536   {
1537     DWORD size = sizeof(pFixedInfo->ScopeId);
1538
1539     RegQueryValueExA(hKey, "ScopeID", NULL, NULL, (LPBYTE)pFixedInfo->ScopeId, &size);
1540     RegCloseKey(hKey);
1541   }
1542
1543   /* FIXME: can check whether routing's enabled in /proc/sys/net/ipv4/ip_forward
1544      I suppose could also check for a listener on port 53 to set EnableDns */
1545   ret = NO_ERROR;
1546   TRACE("returning %d\n", ret);
1547   return ret;
1548 }
1549
1550
1551 /******************************************************************
1552  *    GetNumberOfInterfaces (IPHLPAPI.@)
1553  *
1554  * Get the number of interfaces.
1555  *
1556  * PARAMS
1557  *  pdwNumIf [Out] number of interfaces
1558  *
1559  * RETURNS
1560  *  NO_ERROR on success, ERROR_INVALID_PARAMETER if pdwNumIf is NULL.
1561  */
1562 DWORD WINAPI GetNumberOfInterfaces(PDWORD pdwNumIf)
1563 {
1564   DWORD ret;
1565
1566   TRACE("pdwNumIf %p\n", pdwNumIf);
1567   if (!pdwNumIf)
1568     ret = ERROR_INVALID_PARAMETER;
1569   else {
1570     *pdwNumIf = getNumInterfaces();
1571     ret = NO_ERROR;
1572   }
1573   TRACE("returning %d\n", ret);
1574   return ret;
1575 }
1576
1577
1578 /******************************************************************
1579  *    GetPerAdapterInfo (IPHLPAPI.@)
1580  *
1581  * Get information about an adapter corresponding to an interface.
1582  *
1583  * PARAMS
1584  *  IfIndex         [In]     interface info
1585  *  pPerAdapterInfo [Out]    buffer for per adapter info
1586  *  pOutBufLen      [In/Out] length of output buffer
1587  *
1588  * RETURNS
1589  *  Success: NO_ERROR
1590  *  Failure: error code from winerror.h
1591  *
1592  * FIXME
1593  *  Stub, returns empty IP_PER_ADAPTER_INFO in every case.
1594  */
1595 DWORD WINAPI GetPerAdapterInfo(ULONG IfIndex, PIP_PER_ADAPTER_INFO pPerAdapterInfo, PULONG pOutBufLen)
1596 {
1597   ULONG bytesNeeded = sizeof(IP_PER_ADAPTER_INFO);
1598
1599   TRACE("(IfIndex %d, pPerAdapterInfo %p, pOutBufLen %p)\n", IfIndex, pPerAdapterInfo, pOutBufLen);
1600
1601   if (!pOutBufLen) return ERROR_INVALID_PARAMETER;
1602
1603   if (!pPerAdapterInfo || *pOutBufLen < bytesNeeded)
1604   {
1605     *pOutBufLen = bytesNeeded;
1606     return ERROR_BUFFER_OVERFLOW;
1607   }
1608
1609   memset(pPerAdapterInfo, 0, bytesNeeded);
1610   return NO_ERROR;
1611 }
1612
1613
1614 /******************************************************************
1615  *    GetRTTAndHopCount (IPHLPAPI.@)
1616  *
1617  * Get round-trip time (RTT) and hop count.
1618  *
1619  * PARAMS
1620  *
1621  *  DestIpAddress [In]  destination address to get the info for
1622  *  HopCount      [Out] retrieved hop count
1623  *  MaxHops       [In]  maximum hops to search for the destination
1624  *  RTT           [Out] RTT in milliseconds
1625  *
1626  * RETURNS
1627  *  Success: TRUE
1628  *  Failure: FALSE
1629  *
1630  * FIXME
1631  *  Stub, returns FALSE.
1632  */
1633 BOOL WINAPI GetRTTAndHopCount(IPAddr DestIpAddress, PULONG HopCount, ULONG MaxHops, PULONG RTT)
1634 {
1635   FIXME("(DestIpAddress 0x%08x, HopCount %p, MaxHops %d, RTT %p): stub\n",
1636    DestIpAddress, HopCount, MaxHops, RTT);
1637   return FALSE;
1638 }
1639
1640
1641 /******************************************************************
1642  *    GetTcpTable (IPHLPAPI.@)
1643  *
1644  * Get the table of active TCP connections.
1645  *
1646  * PARAMS
1647  *  pTcpTable [Out]    buffer for TCP connections table
1648  *  pdwSize   [In/Out] length of output buffer
1649  *  bOrder    [In]     whether to order the table
1650  *
1651  * RETURNS
1652  *  Success: NO_ERROR
1653  *  Failure: error code from winerror.h
1654  *
1655  * NOTES
1656  *  If pdwSize is less than required, the function will return 
1657  *  ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to 
1658  *  the required byte size.
1659  *  If bOrder is true, the returned table will be sorted, first by
1660  *  local address and port number, then by remote address and port
1661  *  number.
1662  */
1663 DWORD WINAPI GetTcpTable(PMIB_TCPTABLE pTcpTable, PDWORD pdwSize, BOOL bOrder)
1664 {
1665     DWORD ret;
1666     PMIB_TCPTABLE table;
1667
1668     TRACE("pTcpTable %p, pdwSize %p, bOrder %d\n", pTcpTable, pdwSize, bOrder);
1669
1670     if (!pdwSize) return ERROR_INVALID_PARAMETER;
1671
1672     ret = AllocateAndGetTcpTableFromStack(&table, bOrder, GetProcessHeap(), 0);
1673     if (!ret) {
1674         DWORD size = FIELD_OFFSET( MIB_TCPTABLE, table[table->dwNumEntries] );
1675         if (!pTcpTable || *pdwSize < size) {
1676           *pdwSize = size;
1677           ret = ERROR_INSUFFICIENT_BUFFER;
1678         }
1679         else {
1680           *pdwSize = size;
1681           memcpy(pTcpTable, table, size);
1682         }
1683         HeapFree(GetProcessHeap(), 0, table);
1684     }
1685     TRACE("returning %d\n", ret);
1686     return ret;
1687 }
1688
1689
1690 /******************************************************************
1691  *    GetUdpTable (IPHLPAPI.@)
1692  *
1693  * Get a table of active UDP connections.
1694  *
1695  * PARAMS
1696  *  pUdpTable [Out]    buffer for UDP connections table
1697  *  pdwSize   [In/Out] length of output buffer
1698  *  bOrder    [In]     whether to order the table
1699  *
1700  * RETURNS
1701  *  Success: NO_ERROR
1702  *  Failure: error code from winerror.h
1703  *
1704  * NOTES
1705  *  If pdwSize is less than required, the function will return 
1706  *  ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the
1707  *  required byte size.
1708  *  If bOrder is true, the returned table will be sorted, first by
1709  *  local address, then by local port number.
1710  */
1711 DWORD WINAPI GetUdpTable(PMIB_UDPTABLE pUdpTable, PDWORD pdwSize, BOOL bOrder)
1712 {
1713     DWORD ret;
1714     PMIB_UDPTABLE table;
1715
1716     TRACE("pUdpTable %p, pdwSize %p, bOrder %d\n", pUdpTable, pdwSize, bOrder);
1717
1718     if (!pdwSize) return ERROR_INVALID_PARAMETER;
1719
1720     ret = AllocateAndGetUdpTableFromStack( &table, bOrder, GetProcessHeap(), 0 );
1721     if (!ret) {
1722         DWORD size = FIELD_OFFSET( MIB_UDPTABLE, table[table->dwNumEntries] );
1723         if (!pUdpTable || *pdwSize < size) {
1724           *pdwSize = size;
1725           ret = ERROR_INSUFFICIENT_BUFFER;
1726         }
1727         else {
1728           *pdwSize = size;
1729           memcpy(pUdpTable, table, size);
1730         }
1731         HeapFree(GetProcessHeap(), 0, table);
1732     }
1733     TRACE("returning %d\n", ret);
1734     return ret;
1735 }
1736
1737
1738 /******************************************************************
1739  *    GetUniDirectionalAdapterInfo (IPHLPAPI.@)
1740  *
1741  * This is a Win98-only function to get information on "unidirectional"
1742  * adapters.  Since this is pretty nonsensical in other contexts, it
1743  * never returns anything.
1744  *
1745  * PARAMS
1746  *  pIPIfInfo   [Out] buffer for adapter infos
1747  *  dwOutBufLen [Out] length of the output buffer
1748  *
1749  * RETURNS
1750  *  Success: NO_ERROR
1751  *  Failure: error code from winerror.h
1752  *
1753  * FIXME
1754  *  Stub, returns ERROR_NOT_SUPPORTED.
1755  */
1756 DWORD WINAPI GetUniDirectionalAdapterInfo(PIP_UNIDIRECTIONAL_ADAPTER_ADDRESS pIPIfInfo, PULONG dwOutBufLen)
1757 {
1758   TRACE("pIPIfInfo %p, dwOutBufLen %p\n", pIPIfInfo, dwOutBufLen);
1759   /* a unidirectional adapter?? not bloody likely! */
1760   return ERROR_NOT_SUPPORTED;
1761 }
1762
1763
1764 /******************************************************************
1765  *    IpReleaseAddress (IPHLPAPI.@)
1766  *
1767  * Release an IP obtained through DHCP,
1768  *
1769  * PARAMS
1770  *  AdapterInfo [In] adapter to release IP address
1771  *
1772  * RETURNS
1773  *  Success: NO_ERROR
1774  *  Failure: error code from winerror.h
1775  *
1776  * NOTES
1777  *  Since GetAdaptersInfo never returns adapters that have DHCP enabled,
1778  *  this function does nothing.
1779  *
1780  * FIXME
1781  *  Stub, returns ERROR_NOT_SUPPORTED.
1782  */
1783 DWORD WINAPI IpReleaseAddress(PIP_ADAPTER_INDEX_MAP AdapterInfo)
1784 {
1785   TRACE("AdapterInfo %p\n", AdapterInfo);
1786   /* not a stub, never going to support this (and I never mark an adapter as
1787      DHCP enabled, see GetAdaptersInfo, so this should never get called) */
1788   return ERROR_NOT_SUPPORTED;
1789 }
1790
1791
1792 /******************************************************************
1793  *    IpRenewAddress (IPHLPAPI.@)
1794  *
1795  * Renew an IP obtained through DHCP.
1796  *
1797  * PARAMS
1798  *  AdapterInfo [In] adapter to renew IP address
1799  *
1800  * RETURNS
1801  *  Success: NO_ERROR
1802  *  Failure: error code from winerror.h
1803  *
1804  * NOTES
1805  *  Since GetAdaptersInfo never returns adapters that have DHCP enabled,
1806  *  this function does nothing.
1807  *
1808  * FIXME
1809  *  Stub, returns ERROR_NOT_SUPPORTED.
1810  */
1811 DWORD WINAPI IpRenewAddress(PIP_ADAPTER_INDEX_MAP AdapterInfo)
1812 {
1813   TRACE("AdapterInfo %p\n", AdapterInfo);
1814   /* not a stub, never going to support this (and I never mark an adapter as
1815      DHCP enabled, see GetAdaptersInfo, so this should never get called) */
1816   return ERROR_NOT_SUPPORTED;
1817 }
1818
1819
1820 /******************************************************************
1821  *    NotifyAddrChange (IPHLPAPI.@)
1822  *
1823  * Notify caller whenever the ip-interface map is changed.
1824  *
1825  * PARAMS
1826  *  Handle     [Out] handle usable in asynchronous notification
1827  *  overlapped [In]  overlapped structure that notifies the caller
1828  *
1829  * RETURNS
1830  *  Success: NO_ERROR
1831  *  Failure: error code from winerror.h
1832  *
1833  * FIXME
1834  *  Stub, returns ERROR_NOT_SUPPORTED.
1835  */
1836 DWORD WINAPI NotifyAddrChange(PHANDLE Handle, LPOVERLAPPED overlapped)
1837 {
1838   FIXME("(Handle %p, overlapped %p): stub\n", Handle, overlapped);
1839   return ERROR_NOT_SUPPORTED;
1840 }
1841
1842
1843 /******************************************************************
1844  *    NotifyRouteChange (IPHLPAPI.@)
1845  *
1846  * Notify caller whenever the ip routing table is changed.
1847  *
1848  * PARAMS
1849  *  Handle     [Out] handle usable in asynchronous notification
1850  *  overlapped [In]  overlapped structure that notifies the caller
1851  *
1852  * RETURNS
1853  *  Success: NO_ERROR
1854  *  Failure: error code from winerror.h
1855  *
1856  * FIXME
1857  *  Stub, returns ERROR_NOT_SUPPORTED.
1858  */
1859 DWORD WINAPI NotifyRouteChange(PHANDLE Handle, LPOVERLAPPED overlapped)
1860 {
1861   FIXME("(Handle %p, overlapped %p): stub\n", Handle, overlapped);
1862   return ERROR_NOT_SUPPORTED;
1863 }
1864
1865
1866 /******************************************************************
1867  *    SendARP (IPHLPAPI.@)
1868  *
1869  * Send an ARP request.
1870  *
1871  * PARAMS
1872  *  DestIP     [In]     attempt to obtain this IP
1873  *  SrcIP      [In]     optional sender IP address
1874  *  pMacAddr   [Out]    buffer for the mac address
1875  *  PhyAddrLen [In/Out] length of the output buffer
1876  *
1877  * RETURNS
1878  *  Success: NO_ERROR
1879  *  Failure: error code from winerror.h
1880  *
1881  * FIXME
1882  *  Stub, returns ERROR_NOT_SUPPORTED.
1883  */
1884 DWORD WINAPI SendARP(IPAddr DestIP, IPAddr SrcIP, PULONG pMacAddr, PULONG PhyAddrLen)
1885 {
1886   FIXME("(DestIP 0x%08x, SrcIP 0x%08x, pMacAddr %p, PhyAddrLen %p): stub\n",
1887    DestIP, SrcIP, pMacAddr, PhyAddrLen);
1888   return ERROR_NOT_SUPPORTED;
1889 }
1890
1891
1892 /******************************************************************
1893  *    SetIfEntry (IPHLPAPI.@)
1894  *
1895  * Set the administrative status of an interface.
1896  *
1897  * PARAMS
1898  *  pIfRow [In] dwAdminStatus member specifies the new status.
1899  *
1900  * RETURNS
1901  *  Success: NO_ERROR
1902  *  Failure: error code from winerror.h
1903  *
1904  * FIXME
1905  *  Stub, returns ERROR_NOT_SUPPORTED.
1906  */
1907 DWORD WINAPI SetIfEntry(PMIB_IFROW pIfRow)
1908 {
1909   FIXME("(pIfRow %p): stub\n", pIfRow);
1910   /* this is supposed to set an interface administratively up or down.
1911      Could do SIOCSIFFLAGS and set/clear IFF_UP, but, not sure I want to, and
1912      this sort of down is indistinguishable from other sorts of down (e.g. no
1913      link). */
1914   return ERROR_NOT_SUPPORTED;
1915 }
1916
1917
1918 /******************************************************************
1919  *    SetIpForwardEntry (IPHLPAPI.@)
1920  *
1921  * Modify an existing route.
1922  *
1923  * PARAMS
1924  *  pRoute [In] route with the new information
1925  *
1926  * RETURNS
1927  *  Success: NO_ERROR
1928  *  Failure: error code from winerror.h
1929  *
1930  * FIXME
1931  *  Stub, returns NO_ERROR.
1932  */
1933 DWORD WINAPI SetIpForwardEntry(PMIB_IPFORWARDROW pRoute)
1934 {
1935   FIXME("(pRoute %p): stub\n", pRoute);
1936   /* this is to add a route entry, how's it distinguishable from
1937      CreateIpForwardEntry?
1938      could use SIOCADDRT, not sure I want to */
1939   return 0;
1940 }
1941
1942
1943 /******************************************************************
1944  *    SetIpNetEntry (IPHLPAPI.@)
1945  *
1946  * Modify an existing ARP entry.
1947  *
1948  * PARAMS
1949  *  pArpEntry [In] ARP entry with the new information
1950  *
1951  * RETURNS
1952  *  Success: NO_ERROR
1953  *  Failure: error code from winerror.h
1954  *
1955  * FIXME
1956  *  Stub, returns NO_ERROR.
1957  */
1958 DWORD WINAPI SetIpNetEntry(PMIB_IPNETROW pArpEntry)
1959 {
1960   FIXME("(pArpEntry %p): stub\n", pArpEntry);
1961   /* same as CreateIpNetEntry here, could use SIOCSARP, not sure I want to */
1962   return 0;
1963 }
1964
1965
1966 /******************************************************************
1967  *    SetIpStatistics (IPHLPAPI.@)
1968  *
1969  * Toggle IP forwarding and det the default TTL value.
1970  *
1971  * PARAMS
1972  *  pIpStats [In] IP statistics with the new information
1973  *
1974  * RETURNS
1975  *  Success: NO_ERROR
1976  *  Failure: error code from winerror.h
1977  *
1978  * FIXME
1979  *  Stub, returns NO_ERROR.
1980  */
1981 DWORD WINAPI SetIpStatistics(PMIB_IPSTATS pIpStats)
1982 {
1983   FIXME("(pIpStats %p): stub\n", pIpStats);
1984   return 0;
1985 }
1986
1987
1988 /******************************************************************
1989  *    SetIpTTL (IPHLPAPI.@)
1990  *
1991  * Set the default TTL value.
1992  *
1993  * PARAMS
1994  *  nTTL [In] new TTL value
1995  *
1996  * RETURNS
1997  *  Success: NO_ERROR
1998  *  Failure: error code from winerror.h
1999  *
2000  * FIXME
2001  *  Stub, returns NO_ERROR.
2002  */
2003 DWORD WINAPI SetIpTTL(UINT nTTL)
2004 {
2005   FIXME("(nTTL %d): stub\n", nTTL);
2006   /* could echo nTTL > /proc/net/sys/net/ipv4/ip_default_ttl, not sure I
2007      want to.  Could map EACCESS to ERROR_ACCESS_DENIED, I suppose */
2008   return 0;
2009 }
2010
2011
2012 /******************************************************************
2013  *    SetTcpEntry (IPHLPAPI.@)
2014  *
2015  * Set the state of a TCP connection.
2016  *
2017  * PARAMS
2018  *  pTcpRow [In] specifies connection with new state
2019  *
2020  * RETURNS
2021  *  Success: NO_ERROR
2022  *  Failure: error code from winerror.h
2023  *
2024  * FIXME
2025  *  Stub, returns NO_ERROR.
2026  */
2027 DWORD WINAPI SetTcpEntry(PMIB_TCPROW pTcpRow)
2028 {
2029   FIXME("(pTcpRow %p): stub\n", pTcpRow);
2030   return 0;
2031 }
2032
2033
2034 /******************************************************************
2035  *    UnenableRouter (IPHLPAPI.@)
2036  *
2037  * Decrement the IP-forwarding reference count. Turn off IP-forwarding
2038  * if it reaches zero.
2039  *
2040  * PARAMS
2041  *  pOverlapped     [In/Out] should be the same as in EnableRouter()
2042  *  lpdwEnableCount [Out]    optional, receives reference count
2043  *
2044  * RETURNS
2045  *  Success: NO_ERROR
2046  *  Failure: error code from winerror.h
2047  *
2048  * FIXME
2049  *  Stub, returns ERROR_NOT_SUPPORTED.
2050  */
2051 DWORD WINAPI UnenableRouter(OVERLAPPED * pOverlapped, LPDWORD lpdwEnableCount)
2052 {
2053   FIXME("(pOverlapped %p, lpdwEnableCount %p): stub\n", pOverlapped,
2054    lpdwEnableCount);
2055   /* could echo "0" > /proc/net/sys/net/ipv4/ip_forward, not sure I want to
2056      could map EACCESS to ERROR_ACCESS_DENIED, I suppose
2057    */
2058   return ERROR_NOT_SUPPORTED;
2059 }