hlink/tests: Add a trailing '\n' to an ok() call.
[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 /* FIXME: should do same fix in dnsapi (or use dnsapi here?) */
73 static void initialise_resolver(void)
74 {
75     if ((_res.options & RES_INIT) == 0)
76         res_init();
77 }
78
79 BOOL WINAPI DllMain (HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
80 {
81   switch (fdwReason) {
82     case DLL_PROCESS_ATTACH:
83       DisableThreadLibraryCalls( hinstDLL );
84       break;
85
86     case DLL_PROCESS_DETACH:
87       break;
88   }
89   return TRUE;
90 }
91
92 /******************************************************************
93  *    AddIPAddress (IPHLPAPI.@)
94  *
95  * Add an IP address to an adapter.
96  *
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
103  *
104  * RETURNS
105  *  Success: NO_ERROR
106  *  Failure: error code from winerror.h
107  *
108  * FIXME
109  *  Stub. Currently returns ERROR_NOT_SUPPORTED.
110  */
111 DWORD WINAPI AddIPAddress(IPAddr Address, IPMask IpMask, DWORD IfIndex, PULONG NTEContext, PULONG NTEInstance)
112 {
113   FIXME(":stub\n");
114   return ERROR_NOT_SUPPORTED;
115 }
116
117
118 /******************************************************************
119  *    AllocateAndGetIfTableFromStack (IPHLPAPI.@)
120  *
121  * Get table of local interfaces.
122  * Like GetIfTable(), but allocate the returned table from heap.
123  *
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
130  *
131  * RETURNS
132  *  ERROR_INVALID_PARAMETER if ppIfTable is NULL, whatever
133  *  GetIfTable() returns otherwise.
134  */
135 DWORD WINAPI AllocateAndGetIfTableFromStack(PMIB_IFTABLE *ppIfTable,
136  BOOL bOrder, HANDLE heap, DWORD flags)
137 {
138   DWORD ret;
139
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;
146
147     ret = GetIfTable(*ppIfTable, &dwSize, bOrder);
148     if (ret == ERROR_INSUFFICIENT_BUFFER) {
149       *ppIfTable = HeapAlloc(heap, flags, dwSize);
150       ret = GetIfTable(*ppIfTable, &dwSize, bOrder);
151     }
152   }
153   TRACE("returning %d\n", ret);
154   return ret;
155 }
156
157
158 static int IpAddrTableSorter(const void *a, const void *b)
159 {
160   int ret;
161
162   if (a && b)
163     ret = ((const MIB_IPADDRROW*)a)->dwAddr - ((const MIB_IPADDRROW*)b)->dwAddr;
164   else
165     ret = 0;
166   return ret;
167 }
168
169
170 /******************************************************************
171  *    AllocateAndGetIpAddrTableFromStack (IPHLPAPI.@)
172  *
173  * Get interface-to-IP address mapping table. 
174  * Like GetIpAddrTable(), but allocate the returned table from heap.
175  *
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
182  *
183  * RETURNS
184  *  ERROR_INVALID_PARAMETER if ppIpAddrTable is NULL, other error codes on
185  *  failure, NO_ERROR on success.
186  */
187 DWORD WINAPI AllocateAndGetIpAddrTableFromStack(PMIB_IPADDRTABLE *ppIpAddrTable,
188  BOOL bOrder, HANDLE heap, DWORD flags)
189 {
190   DWORD ret;
191
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;
200 }
201
202
203 /******************************************************************
204  *    CreateIpForwardEntry (IPHLPAPI.@)
205  *
206  * Create a route in the local computer's IP table.
207  *
208  * PARAMS
209  *  pRoute [In] new route information
210  *
211  * RETURNS
212  *  Success: NO_ERROR
213  *  Failure: error code from winerror.h
214  *
215  * FIXME
216  *  Stub, always returns NO_ERROR.
217  */
218 DWORD WINAPI CreateIpForwardEntry(PMIB_IPFORWARDROW pRoute)
219 {
220   FIXME("(pRoute %p): stub\n", pRoute);
221   /* could use SIOCADDRT, not sure I want to */
222   return 0;
223 }
224
225
226 /******************************************************************
227  *    CreateIpNetEntry (IPHLPAPI.@)
228  *
229  * Create entry in the ARP table.
230  *
231  * PARAMS
232  *  pArpEntry [In] new ARP entry
233  *
234  * RETURNS
235  *  Success: NO_ERROR
236  *  Failure: error code from winerror.h
237  *
238  * FIXME
239  *  Stub, always returns NO_ERROR.
240  */
241 DWORD WINAPI CreateIpNetEntry(PMIB_IPNETROW pArpEntry)
242 {
243   FIXME("(pArpEntry %p)\n", pArpEntry);
244   /* could use SIOCSARP on systems that support it, not sure I want to */
245   return 0;
246 }
247
248
249 /******************************************************************
250  *    CreateProxyArpEntry (IPHLPAPI.@)
251  *
252  * Create a Proxy ARP (PARP) entry for an IP address.
253  *
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
258  *
259  * RETURNS
260  *  Success: NO_ERROR
261  *  Failure: error code from winerror.h
262  *
263  * FIXME
264  *  Stub, returns ERROR_NOT_SUPPORTED.
265  */
266 DWORD WINAPI CreateProxyArpEntry(DWORD dwAddress, DWORD dwMask, DWORD dwIfIndex)
267 {
268   FIXME("(dwAddress 0x%08x, dwMask 0x%08x, dwIfIndex 0x%08x): stub\n",
269    dwAddress, dwMask, dwIfIndex);
270   return ERROR_NOT_SUPPORTED;
271 }
272
273
274 /******************************************************************
275  *    DeleteIPAddress (IPHLPAPI.@)
276  *
277  * Delete an IP address added with AddIPAddress().
278  *
279  * PARAMS
280  *  NTEContext [In] NTE context from AddIPAddress();
281  *
282  * RETURNS
283  *  Success: NO_ERROR
284  *  Failure: error code from winerror.h
285  *
286  * FIXME
287  *  Stub, returns ERROR_NOT_SUPPORTED.
288  */
289 DWORD WINAPI DeleteIPAddress(ULONG NTEContext)
290 {
291   FIXME("(NTEContext %d): stub\n", NTEContext);
292   return ERROR_NOT_SUPPORTED;
293 }
294
295
296 /******************************************************************
297  *    DeleteIpForwardEntry (IPHLPAPI.@)
298  *
299  * Delete a route.
300  *
301  * PARAMS
302  *  pRoute [In] route to delete
303  *
304  * RETURNS
305  *  Success: NO_ERROR
306  *  Failure: error code from winerror.h
307  *
308  * FIXME
309  *  Stub, returns NO_ERROR.
310  */
311 DWORD WINAPI DeleteIpForwardEntry(PMIB_IPFORWARDROW pRoute)
312 {
313   FIXME("(pRoute %p): stub\n", pRoute);
314   /* could use SIOCDELRT, not sure I want to */
315   return 0;
316 }
317
318
319 /******************************************************************
320  *    DeleteIpNetEntry (IPHLPAPI.@)
321  *
322  * Delete an ARP entry.
323  *
324  * PARAMS
325  *  pArpEntry [In] ARP entry to delete
326  *
327  * RETURNS
328  *  Success: NO_ERROR
329  *  Failure: error code from winerror.h
330  *
331  * FIXME
332  *  Stub, returns NO_ERROR.
333  */
334 DWORD WINAPI DeleteIpNetEntry(PMIB_IPNETROW pArpEntry)
335 {
336   FIXME("(pArpEntry %p): stub\n", pArpEntry);
337   /* could use SIOCDARP on systems that support it, not sure I want to */
338   return 0;
339 }
340
341
342 /******************************************************************
343  *    DeleteProxyArpEntry (IPHLPAPI.@)
344  *
345  * Delete a Proxy ARP entry.
346  *
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
351  *
352  * RETURNS
353  *  Success: NO_ERROR
354  *  Failure: error code from winerror.h
355  *
356  * FIXME
357  *  Stub, returns ERROR_NOT_SUPPORTED.
358  */
359 DWORD WINAPI DeleteProxyArpEntry(DWORD dwAddress, DWORD dwMask, DWORD dwIfIndex)
360 {
361   FIXME("(dwAddress 0x%08x, dwMask 0x%08x, dwIfIndex 0x%08x): stub\n",
362    dwAddress, dwMask, dwIfIndex);
363   return ERROR_NOT_SUPPORTED;
364 }
365
366
367 /******************************************************************
368  *    EnableRouter (IPHLPAPI.@)
369  *
370  * Turn on ip forwarding.
371  *
372  * PARAMS
373  *  pHandle     [In/Out]
374  *  pOverlapped [In/Out] hEvent member should contain a valid handle.
375  *
376  * RETURNS
377  *  Success: ERROR_IO_PENDING
378  *  Failure: error code from winerror.h
379  *
380  * FIXME
381  *  Stub, returns ERROR_NOT_SUPPORTED.
382  */
383 DWORD WINAPI EnableRouter(HANDLE * pHandle, OVERLAPPED * pOverlapped)
384 {
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
388    */
389   return ERROR_NOT_SUPPORTED;
390 }
391
392
393 /******************************************************************
394  *    FlushIpNetTable (IPHLPAPI.@)
395  *
396  * Delete all ARP entries of an interface
397  *
398  * PARAMS
399  *  dwIfIndex [In] interface index
400  *
401  * RETURNS
402  *  Success: NO_ERROR
403  *  Failure: error code from winerror.h
404  *
405  * FIXME
406  *  Stub, returns ERROR_NOT_SUPPORTED.
407  */
408 DWORD WINAPI FlushIpNetTable(DWORD dwIfIndex)
409 {
410   FIXME("(dwIfIndex 0x%08x): stub\n", dwIfIndex);
411   /* this flushes the arp cache of the given index */
412   return ERROR_NOT_SUPPORTED;
413 }
414
415
416 /******************************************************************
417  *    GetAdapterIndex (IPHLPAPI.@)
418  *
419  * Get interface index from its name.
420  *
421  * PARAMS
422  *  AdapterName [In]  unicode string with the adapter name
423  *  IfIndex     [Out] returns found interface index
424  *
425  * RETURNS
426  *  Success: NO_ERROR
427  *  Failure: error code from winerror.h
428  */
429 DWORD WINAPI GetAdapterIndex(LPWSTR AdapterName, PULONG IfIndex)
430 {
431   char adapterName[MAX_ADAPTER_NAME];
432   unsigned int i;
433   DWORD ret;
434
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;
444 }
445
446
447 /******************************************************************
448  *    GetAdaptersInfo (IPHLPAPI.@)
449  *
450  * Get information about adapters.
451  *
452  * PARAMS
453  *  pAdapterInfo [Out] buffer for adapter infos
454  *  pOutBufLen   [In]  length of output buffer
455  *
456  * RETURNS
457  *  Success: NO_ERROR
458  *  Failure: error code from winerror.h
459  */
460 DWORD WINAPI GetAdaptersInfo(PIP_ADAPTER_INFO pAdapterInfo, PULONG pOutBufLen)
461 {
462   DWORD ret;
463
464   TRACE("pAdapterInfo %p, pOutBufLen %p\n", pAdapterInfo, pOutBufLen);
465   if (!pOutBufLen)
466     ret = ERROR_INVALID_PARAMETER;
467   else {
468     DWORD numNonLoopbackInterfaces = getNumNonLoopbackInterfaces();
469
470     if (numNonLoopbackInterfaces > 0) {
471       DWORD numIPAddresses = getNumIPAddresses();
472       ULONG size;
473
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.
478        */
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;
484       }
485       else {
486         InterfaceIndexTable *table = NULL;
487         PMIB_IPADDRTABLE ipAddrTable = NULL;
488         PMIB_IPFORWARDTABLE routeTable = NULL;
489
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;
501           }
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));
509
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;
516
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);
529             }
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;
535
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;
551                   }
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++;
560                   }
561                 }
562               }
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));
577               }
578               if (ndx < table->numIndexes - 1)
579                 ptr->Next = &pAdapterInfo[ndx + 1];
580               else
581                 ptr->Next = NULL;
582             }
583             ret = NO_ERROR;
584           }
585           HeapFree(GetProcessHeap(), 0, table);
586         }
587         else
588           ret = ERROR_OUTOFMEMORY;
589         HeapFree(GetProcessHeap(), 0, routeTable);
590         HeapFree(GetProcessHeap(), 0, ipAddrTable);
591       }
592     }
593     else
594       ret = ERROR_NO_DATA;
595   }
596   TRACE("returning %d\n", ret);
597   return ret;
598 }
599
600 static DWORD typeFromMibType(DWORD mib_type)
601 {
602     switch (mib_type)
603     {
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;
609     }
610 }
611
612 static ULONG v4addressesFromIndex(DWORD index, DWORD **addrs, ULONG *num_addrs)
613 {
614     ULONG ret, i, j;
615     MIB_IPADDRTABLE *at;
616
617     *num_addrs = 0;
618     if ((ret = getIPAddrTable(&at, GetProcessHeap(), 0))) return ret;
619     for (i = 0; i < at->dwNumEntries; i++)
620     {
621         if (at->table[i].dwIndex == index) (*num_addrs)++;
622     }
623     if (!(*addrs = HeapAlloc(GetProcessHeap(), 0, *num_addrs * sizeof(DWORD))))
624     {
625         HeapFree(GetProcessHeap(), 0, at);
626         return ERROR_OUTOFMEMORY;
627     }
628     for (i = 0, j = 0; i < at->dwNumEntries; i++)
629     {
630         if (at->table[i].dwIndex == index) (*addrs)[j++] = at->table[i].dwAddr;
631     }
632     HeapFree(GetProcessHeap(), 0, at);
633     return ERROR_SUCCESS;
634 }
635
636 static ULONG adapterAddressesFromIndex(ULONG family, DWORD index, IP_ADAPTER_ADDRESSES *aa, ULONG *size)
637 {
638     ULONG ret, i, num_v4addrs = 0, num_v6addrs = 0, total_size;
639     DWORD *v4addrs = NULL;
640     SOCKET_ADDRESS *v6addrs = NULL;
641
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)
647     {
648         ret = v4addressesFromIndex(index, &v4addrs, &num_v4addrs);
649         if (!ret)
650             ret = v6addressesFromIndex(index, &v6addrs, &num_v6addrs);
651     }
652     else
653     {
654         FIXME("address family %u unsupported\n", family);
655         ret = ERROR_NO_DATA;
656     }
657     if (ret) return ret;
658
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;
668
669     if (aa && *size >= total_size)
670     {
671         char name[IF_NAMESIZE], *ptr = (char *)aa + sizeof(IP_ADAPTER_ADDRESSES), *src;
672         WCHAR *dst;
673         DWORD buflen, type, status;
674
675         memset(aa, 0, sizeof(IP_ADAPTER_ADDRESSES));
676         aa->u.s.Length  = sizeof(IP_ADAPTER_ADDRESSES);
677         aa->u.s.IfIndex = index;
678
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;
688
689         if (num_v4addrs)
690         {
691             IP_ADAPTER_UNICAST_ADDRESS *ua;
692             struct sockaddr_in *sa;
693
694             ua = aa->FirstUnicastAddress = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
695             for (i = 0; i < num_v4addrs; i++)
696             {
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);
701
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;
706
707                 ptr += ua->u.s.Length + ua->Address.iSockaddrLength;
708                 if (i < num_v4addrs - 1)
709                 {
710                     ua->Next = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
711                     ua = ua->Next;
712                 }
713             }
714         }
715         if (num_v6addrs)
716         {
717             IP_ADAPTER_UNICAST_ADDRESS *ua;
718             struct WS_sockaddr_in6 *sa;
719
720             if (aa->FirstUnicastAddress)
721             {
722                 for (ua = aa->FirstUnicastAddress; ua->Next; ua = ua->Next)
723                     ;
724                 ua->Next = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
725             }
726             else
727                 ua = aa->FirstUnicastAddress = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
728             for (i = 0; i < num_v6addrs; i++)
729             {
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);
734
735                 sa = (struct WS_sockaddr_in6 *)ua->Address.lpSockaddr;
736                 memcpy(sa, v6addrs[i].lpSockaddr, sizeof(*sa));
737
738                 ptr += ua->u.s.Length + ua->Address.iSockaddrLength;
739                 if (i < num_v6addrs - 1)
740                 {
741                     ua->Next = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
742                     ua = ua->Next;
743                 }
744             }
745         }
746
747         buflen = MAX_INTERFACE_PHYSADDR;
748         getInterfacePhysicalByIndex(index, &buflen, aa->PhysicalAddress, &type);
749         aa->PhysicalAddressLength = buflen;
750         aa->IfType = typeFromMibType(type);
751
752         getInterfaceMtuByName(name, &aa->Mtu);
753
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;
758     }
759     *size = total_size;
760     HeapFree(GetProcessHeap(), 0, v6addrs);
761     HeapFree(GetProcessHeap(), 0, v4addrs);
762     return ERROR_SUCCESS;
763 }
764
765 ULONG WINAPI GetAdaptersAddresses(ULONG family, ULONG flags, PVOID reserved,
766                                   PIP_ADAPTER_ADDRESSES aa, PULONG buflen)
767 {
768     InterfaceIndexTable *table;
769     ULONG i, size, total_size, ret = ERROR_NO_DATA;
770
771     if (!buflen) return ERROR_INVALID_PARAMETER;
772
773     table = getInterfaceIndexTable();
774     if (!table || !table->numIndexes)
775     {
776         HeapFree(GetProcessHeap(), 0, table);
777         return ERROR_NO_DATA;
778     }
779     total_size = 0;
780     for (i = 0; i < table->numIndexes; i++)
781     {
782         size = 0;
783         if ((ret = adapterAddressesFromIndex(family, table->indexes[i], NULL, &size)))
784         {
785             HeapFree(GetProcessHeap(), 0, table);
786             return ret;
787         }
788         total_size += size;
789     }
790     if (aa && *buflen >= total_size)
791     {
792         ULONG bytes_left = size = total_size;
793         for (i = 0; i < table->numIndexes; i++)
794         {
795             if ((ret = adapterAddressesFromIndex(family, table->indexes[i], aa, &size)))
796             {
797                 HeapFree(GetProcessHeap(), 0, table);
798                 return ret;
799             }
800             if (i < table->numIndexes - 1)
801             {
802                 aa->Next = (IP_ADAPTER_ADDRESSES *)((char *)aa + size);
803                 aa = aa->Next;
804                 size = bytes_left -= size;
805             }
806         }
807         ret = ERROR_SUCCESS;
808     }
809     if (*buflen < total_size) ret = ERROR_BUFFER_OVERFLOW;
810     *buflen = total_size;
811
812     TRACE("num adapters %u\n", table->numIndexes);
813     HeapFree(GetProcessHeap(), 0, table);
814     return ret;
815 }
816
817 /******************************************************************
818  *    GetBestInterface (IPHLPAPI.@)
819  *
820  * Get the interface, with the best route for the given IP address.
821  *
822  * PARAMS
823  *  dwDestAddr     [In]  IP address to search the interface for
824  *  pdwBestIfIndex [Out] found best interface
825  *
826  * RETURNS
827  *  Success: NO_ERROR
828  *  Failure: error code from winerror.h
829  */
830 DWORD WINAPI GetBestInterface(IPAddr dwDestAddr, PDWORD pdwBestIfIndex)
831 {
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);
837 }
838
839 /******************************************************************
840  *    GetBestInterfaceEx (IPHLPAPI.@)
841  *
842  * Get the interface, with the best route for the given IP address.
843  *
844  * PARAMS
845  *  dwDestAddr     [In]  IP address to search the interface for
846  *  pdwBestIfIndex [Out] found best interface
847  *
848  * RETURNS
849  *  Success: NO_ERROR
850  *  Failure: error code from winerror.h
851  */
852 DWORD WINAPI GetBestInterfaceEx(struct WS_sockaddr *pDestAddr, PDWORD pdwBestIfIndex)
853 {
854   DWORD ret;
855
856   TRACE("pDestAddr %p, pdwBestIfIndex %p\n", pDestAddr, pdwBestIfIndex);
857   if (!pDestAddr || !pdwBestIfIndex)
858     ret = ERROR_INVALID_PARAMETER;
859   else {
860     MIB_IPFORWARDROW ipRow;
861
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;
869     }
870   }
871   TRACE("returning %d\n", ret);
872   return ret;
873 }
874
875
876 /******************************************************************
877  *    GetBestRoute (IPHLPAPI.@)
878  *
879  * Get the best route for the given IP address.
880  *
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
885  *
886  * RETURNS
887  *  Success: NO_ERROR
888  *  Failure: error code from winerror.h
889  */
890 DWORD WINAPI GetBestRoute(DWORD dwDestAddr, DWORD dwSourceAddr, PMIB_IPFORWARDROW pBestRoute)
891 {
892   PMIB_IPFORWARDTABLE table;
893   DWORD ret;
894
895   TRACE("dwDestAddr 0x%08x, dwSourceAddr 0x%08x, pBestRoute %p\n", dwDestAddr,
896    dwSourceAddr, pBestRoute);
897   if (!pBestRoute)
898     return ERROR_INVALID_PARAMETER;
899
900   ret = AllocateAndGetIpForwardTableFromStack(&table, FALSE, GetProcessHeap(), 0);
901   if (!ret) {
902     DWORD ndx, matchedBits, matchedNdx = table->dwNumEntries;
903
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;
909
910         for (numShifts = 0, mask = table->table[ndx].dwForwardMask;
911          mask && !(mask & 1); mask >>= 1, numShifts++)
912           ;
913         if (numShifts > matchedBits) {
914           matchedBits = numShifts;
915           matchedNdx = ndx;
916         }
917         else if (!matchedBits) {
918           matchedNdx = ndx;
919         }
920       }
921     }
922     if (matchedNdx < table->dwNumEntries) {
923       memcpy(pBestRoute, &table->table[matchedNdx], sizeof(MIB_IPFORWARDROW));
924       ret = ERROR_SUCCESS;
925     }
926     else {
927       /* No route matches, which can happen if there's no default route. */
928       ret = ERROR_HOST_UNREACHABLE;
929     }
930     HeapFree(GetProcessHeap(), 0, table);
931   }
932   TRACE("returning %d\n", ret);
933   return ret;
934 }
935
936
937 /******************************************************************
938  *    GetFriendlyIfIndex (IPHLPAPI.@)
939  *
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.
943  *
944  * PARAMS
945  *  IfIndex [In] interface index to get the friendly one for
946  *
947  * RETURNS
948  *  A friendly version of IfIndex.
949  */
950 DWORD WINAPI GetFriendlyIfIndex(DWORD IfIndex)
951 {
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;
957 }
958
959
960 /******************************************************************
961  *    GetIfEntry (IPHLPAPI.@)
962  *
963  * Get information about an interface.
964  *
965  * PARAMS
966  *  pIfRow [In/Out] In:  dwIndex of MIB_IFROW selects the interface.
967  *                  Out: interface information
968  *
969  * RETURNS
970  *  Success: NO_ERROR
971  *  Failure: error code from winerror.h
972  */
973 DWORD WINAPI GetIfEntry(PMIB_IFROW pIfRow)
974 {
975   DWORD ret;
976   char nameBuf[MAX_ADAPTER_NAME];
977   char *name;
978
979   TRACE("pIfRow %p\n", pIfRow);
980   if (!pIfRow)
981     return ERROR_INVALID_PARAMETER;
982
983   name = getInterfaceNameByIndex(pIfRow->dwIndex, nameBuf);
984   if (name) {
985     ret = getInterfaceEntryByName(name, pIfRow);
986     if (ret == NO_ERROR)
987       ret = getInterfaceStatsByName(name, pIfRow);
988   }
989   else
990     ret = ERROR_INVALID_DATA;
991   TRACE("returning %d\n", ret);
992   return ret;
993 }
994
995
996 static int IfTableSorter(const void *a, const void *b)
997 {
998   int ret;
999
1000   if (a && b)
1001     ret = ((const MIB_IFROW*)a)->dwIndex - ((const MIB_IFROW*)b)->dwIndex;
1002   else
1003     ret = 0;
1004   return ret;
1005 }
1006
1007
1008 /******************************************************************
1009  *    GetIfTable (IPHLPAPI.@)
1010  *
1011  * Get a table of local interfaces.
1012  *
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
1017  *
1018  * RETURNS
1019  *  Success: NO_ERROR
1020  *  Failure: error code from winerror.h
1021  *
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.
1027  */
1028 DWORD WINAPI GetIfTable(PMIB_IFTABLE pIfTable, PULONG pdwSize, BOOL bOrder)
1029 {
1030   DWORD ret;
1031
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);
1039
1040     if (numInterfaces > 1)
1041       size += (numInterfaces - 1) * sizeof(MIB_IFROW);
1042     if (!pIfTable || *pdwSize < size) {
1043       *pdwSize = size;
1044       ret = ERROR_INSUFFICIENT_BUFFER;
1045     }
1046     else {
1047       InterfaceIndexTable *table = getInterfaceIndexTable();
1048
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;
1056         }
1057         else {
1058           DWORD ndx;
1059
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++;
1066           }
1067           if (bOrder)
1068             qsort(pIfTable->table, pIfTable->dwNumEntries, sizeof(MIB_IFROW),
1069              IfTableSorter);
1070           ret = NO_ERROR;
1071         }
1072         HeapFree(GetProcessHeap(), 0, table);
1073       }
1074       else
1075         ret = ERROR_OUTOFMEMORY;
1076     }
1077   }
1078   TRACE("returning %d\n", ret);
1079   return ret;
1080 }
1081
1082
1083 /******************************************************************
1084  *    GetInterfaceInfo (IPHLPAPI.@)
1085  *
1086  * Get a list of network interface adapters.
1087  *
1088  * PARAMS
1089  *  pIfTable    [Out] buffer for interface adapters
1090  *  dwOutBufLen [Out] if buffer is too small, returns required size
1091  *
1092  * RETURNS
1093  *  Success: NO_ERROR
1094  *  Failure: error code from winerror.h
1095  *
1096  * BUGS
1097  *  MSDN states this should return non-loopback interfaces only.
1098  */
1099 DWORD WINAPI GetInterfaceInfo(PIP_INTERFACE_INFO pIfTable, PULONG dwOutBufLen)
1100 {
1101   DWORD ret;
1102
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);
1109
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;
1115     }
1116     else {
1117       InterfaceIndexTable *table = getInterfaceIndexTable();
1118
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;
1126         }
1127         else {
1128           DWORD ndx;
1129           char nameBuf[MAX_ADAPTER_NAME];
1130
1131           *dwOutBufLen = size;
1132           pIfTable->NumAdapters = 0;
1133           for (ndx = 0; ndx < table->numIndexes; ndx++) {
1134             const char *walker, *name;
1135             WCHAR *assigner;
1136
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++;
1146           }
1147           ret = NO_ERROR;
1148         }
1149         HeapFree(GetProcessHeap(), 0, table);
1150       }
1151       else
1152         ret = ERROR_OUTOFMEMORY;
1153     }
1154   }
1155   TRACE("returning %d\n", ret);
1156   return ret;
1157 }
1158
1159
1160 /******************************************************************
1161  *    GetIpAddrTable (IPHLPAPI.@)
1162  *
1163  * Get interface-to-IP address mapping table. 
1164  *
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
1169  *
1170  * RETURNS
1171  *  Success: NO_ERROR
1172  *  Failure: error code from winerror.h
1173  *
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.
1180  */
1181 DWORD WINAPI GetIpAddrTable(PMIB_IPADDRTABLE pIpAddrTable, PULONG pdwSize, BOOL bOrder)
1182 {
1183   DWORD ret;
1184
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;
1191
1192     ret = getIPAddrTable(&table, GetProcessHeap(), 0);
1193     if (ret == NO_ERROR)
1194     {
1195       ULONG size = sizeof(MIB_IPADDRTABLE);
1196
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;
1202       }
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;
1210       }
1211       HeapFree(GetProcessHeap(), 0, table);
1212     }
1213   }
1214   TRACE("returning %d\n", ret);
1215   return ret;
1216 }
1217
1218
1219 /******************************************************************
1220  *    GetIpForwardTable (IPHLPAPI.@)
1221  *
1222  * Get the route table.
1223  *
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
1228  *
1229  * RETURNS
1230  *  Success: NO_ERROR
1231  *  Failure: error code from winerror.h
1232  *
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.
1239  */
1240 DWORD WINAPI GetIpForwardTable(PMIB_IPFORWARDTABLE pIpForwardTable, PULONG pdwSize, BOOL bOrder)
1241 {
1242     DWORD ret;
1243     PMIB_IPFORWARDTABLE table;
1244
1245     TRACE("pIpForwardTable %p, pdwSize %p, bOrder %d\n", pIpForwardTable, pdwSize, bOrder);
1246
1247     if (!pdwSize) return ERROR_INVALID_PARAMETER;
1248
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;
1255         }
1256         else {
1257           *pdwSize = size;
1258           memcpy(pIpForwardTable, table, size);
1259         }
1260         HeapFree(GetProcessHeap(), 0, table);
1261     }
1262     TRACE("returning %d\n", ret);
1263     return ret;
1264 }
1265
1266
1267 /******************************************************************
1268  *    GetIpNetTable (IPHLPAPI.@)
1269  *
1270  * Get the IP-to-physical address mapping table.
1271  *
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
1276  *
1277  * RETURNS
1278  *  Success: NO_ERROR
1279  *  Failure: error code from winerror.h
1280  *
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.
1286  */
1287 DWORD WINAPI GetIpNetTable(PMIB_IPNETTABLE pIpNetTable, PULONG pdwSize, BOOL bOrder)
1288 {
1289     DWORD ret;
1290     PMIB_IPNETTABLE table;
1291
1292     TRACE("pIpNetTable %p, pdwSize %p, bOrder %d\n", pIpNetTable, pdwSize, bOrder);
1293
1294     if (!pdwSize) return ERROR_INVALID_PARAMETER;
1295
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;
1302         }
1303         else {
1304           *pdwSize = size;
1305           memcpy(pIpNetTable, table, size);
1306         }
1307         HeapFree(GetProcessHeap(), 0, table);
1308     }
1309     TRACE("returning %d\n", ret);
1310     return ret;
1311 }
1312
1313
1314 /******************************************************************
1315  *    GetNetworkParams (IPHLPAPI.@)
1316  *
1317  * Get the network parameters for the local computer.
1318  *
1319  * PARAMS
1320  *  pFixedInfo [Out]    buffer for network parameters
1321  *  pOutBufLen [In/Out] length of output buffer
1322  *
1323  * RETURNS
1324  *  Success: NO_ERROR
1325  *  Failure: error code from winerror.h
1326  *
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.
1331  */
1332 DWORD WINAPI GetNetworkParams(PFIXED_INFO pFixedInfo, PULONG pOutBufLen)
1333 {
1334   DWORD ret, size;
1335   LONG regReturn;
1336   HKEY hKey;
1337
1338   TRACE("pFixedInfo %p, pOutBufLen %p\n", pFixedInfo, pOutBufLen);
1339   if (!pOutBufLen)
1340     return ERROR_INVALID_PARAMETER;
1341
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;
1348   }
1349
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;
1358
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));
1369     }
1370   }
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)
1379   {
1380     DWORD size = sizeof(pFixedInfo->ScopeId);
1381
1382     RegQueryValueExA(hKey, "ScopeID", NULL, NULL, (LPBYTE)pFixedInfo->ScopeId, &size);
1383     RegCloseKey(hKey);
1384   }
1385
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;
1391 }
1392
1393
1394 /******************************************************************
1395  *    GetNumberOfInterfaces (IPHLPAPI.@)
1396  *
1397  * Get the number of interfaces.
1398  *
1399  * PARAMS
1400  *  pdwNumIf [Out] number of interfaces
1401  *
1402  * RETURNS
1403  *  NO_ERROR on success, ERROR_INVALID_PARAMETER if pdwNumIf is NULL.
1404  */
1405 DWORD WINAPI GetNumberOfInterfaces(PDWORD pdwNumIf)
1406 {
1407   DWORD ret;
1408
1409   TRACE("pdwNumIf %p\n", pdwNumIf);
1410   if (!pdwNumIf)
1411     ret = ERROR_INVALID_PARAMETER;
1412   else {
1413     *pdwNumIf = getNumInterfaces();
1414     ret = NO_ERROR;
1415   }
1416   TRACE("returning %d\n", ret);
1417   return ret;
1418 }
1419
1420
1421 /******************************************************************
1422  *    GetPerAdapterInfo (IPHLPAPI.@)
1423  *
1424  * Get information about an adapter corresponding to an interface.
1425  *
1426  * PARAMS
1427  *  IfIndex         [In]     interface info
1428  *  pPerAdapterInfo [Out]    buffer for per adapter info
1429  *  pOutBufLen      [In/Out] length of output buffer
1430  *
1431  * RETURNS
1432  *  Success: NO_ERROR
1433  *  Failure: error code from winerror.h
1434  *
1435  * FIXME
1436  *  Stub, returns empty IP_PER_ADAPTER_INFO in every case.
1437  */
1438 DWORD WINAPI GetPerAdapterInfo(ULONG IfIndex, PIP_PER_ADAPTER_INFO pPerAdapterInfo, PULONG pOutBufLen)
1439 {
1440   ULONG bytesNeeded = sizeof(IP_PER_ADAPTER_INFO);
1441
1442   TRACE("(IfIndex %d, pPerAdapterInfo %p, pOutBufLen %p)\n", IfIndex, pPerAdapterInfo, pOutBufLen);
1443
1444   if (!pOutBufLen) return ERROR_INVALID_PARAMETER;
1445
1446   if (!pPerAdapterInfo || *pOutBufLen < bytesNeeded)
1447   {
1448     *pOutBufLen = bytesNeeded;
1449     return ERROR_BUFFER_OVERFLOW;
1450   }
1451
1452   memset(pPerAdapterInfo, 0, bytesNeeded);
1453   return NO_ERROR;
1454 }
1455
1456
1457 /******************************************************************
1458  *    GetRTTAndHopCount (IPHLPAPI.@)
1459  *
1460  * Get round-trip time (RTT) and hop count.
1461  *
1462  * PARAMS
1463  *
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
1468  *
1469  * RETURNS
1470  *  Success: TRUE
1471  *  Failure: FALSE
1472  *
1473  * FIXME
1474  *  Stub, returns FALSE.
1475  */
1476 BOOL WINAPI GetRTTAndHopCount(IPAddr DestIpAddress, PULONG HopCount, ULONG MaxHops, PULONG RTT)
1477 {
1478   FIXME("(DestIpAddress 0x%08x, HopCount %p, MaxHops %d, RTT %p): stub\n",
1479    DestIpAddress, HopCount, MaxHops, RTT);
1480   return FALSE;
1481 }
1482
1483
1484 /******************************************************************
1485  *    GetTcpTable (IPHLPAPI.@)
1486  *
1487  * Get the table of active TCP connections.
1488  *
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
1493  *
1494  * RETURNS
1495  *  Success: NO_ERROR
1496  *  Failure: error code from winerror.h
1497  *
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.
1505  */
1506 DWORD WINAPI GetTcpTable(PMIB_TCPTABLE pTcpTable, PDWORD pdwSize, BOOL bOrder)
1507 {
1508     DWORD ret;
1509     PMIB_TCPTABLE table;
1510
1511     TRACE("pTcpTable %p, pdwSize %p, bOrder %d\n", pTcpTable, pdwSize, bOrder);
1512
1513     if (!pdwSize) return ERROR_INVALID_PARAMETER;
1514
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;
1521         }
1522         else {
1523           *pdwSize = size;
1524           memcpy(pTcpTable, table, size);
1525         }
1526         HeapFree(GetProcessHeap(), 0, table);
1527     }
1528     TRACE("returning %d\n", ret);
1529     return ret;
1530 }
1531
1532
1533 /******************************************************************
1534  *    GetUdpTable (IPHLPAPI.@)
1535  *
1536  * Get a table of active UDP connections.
1537  *
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
1542  *
1543  * RETURNS
1544  *  Success: NO_ERROR
1545  *  Failure: error code from winerror.h
1546  *
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.
1553  */
1554 DWORD WINAPI GetUdpTable(PMIB_UDPTABLE pUdpTable, PDWORD pdwSize, BOOL bOrder)
1555 {
1556     DWORD ret;
1557     PMIB_UDPTABLE table;
1558
1559     TRACE("pUdpTable %p, pdwSize %p, bOrder %d\n", pUdpTable, pdwSize, bOrder);
1560
1561     if (!pdwSize) return ERROR_INVALID_PARAMETER;
1562
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;
1569         }
1570         else {
1571           *pdwSize = size;
1572           memcpy(pUdpTable, table, size);
1573         }
1574         HeapFree(GetProcessHeap(), 0, table);
1575     }
1576     TRACE("returning %d\n", ret);
1577     return ret;
1578 }
1579
1580
1581 /******************************************************************
1582  *    GetUniDirectionalAdapterInfo (IPHLPAPI.@)
1583  *
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.
1587  *
1588  * PARAMS
1589  *  pIPIfInfo   [Out] buffer for adapter infos
1590  *  dwOutBufLen [Out] length of the output buffer
1591  *
1592  * RETURNS
1593  *  Success: NO_ERROR
1594  *  Failure: error code from winerror.h
1595  *
1596  * FIXME
1597  *  Stub, returns ERROR_NOT_SUPPORTED.
1598  */
1599 DWORD WINAPI GetUniDirectionalAdapterInfo(PIP_UNIDIRECTIONAL_ADAPTER_ADDRESS pIPIfInfo, PULONG dwOutBufLen)
1600 {
1601   TRACE("pIPIfInfo %p, dwOutBufLen %p\n", pIPIfInfo, dwOutBufLen);
1602   /* a unidirectional adapter?? not bloody likely! */
1603   return ERROR_NOT_SUPPORTED;
1604 }
1605
1606
1607 /******************************************************************
1608  *    IpReleaseAddress (IPHLPAPI.@)
1609  *
1610  * Release an IP obtained through DHCP,
1611  *
1612  * PARAMS
1613  *  AdapterInfo [In] adapter to release IP address
1614  *
1615  * RETURNS
1616  *  Success: NO_ERROR
1617  *  Failure: error code from winerror.h
1618  *
1619  * NOTES
1620  *  Since GetAdaptersInfo never returns adapters that have DHCP enabled,
1621  *  this function does nothing.
1622  *
1623  * FIXME
1624  *  Stub, returns ERROR_NOT_SUPPORTED.
1625  */
1626 DWORD WINAPI IpReleaseAddress(PIP_ADAPTER_INDEX_MAP AdapterInfo)
1627 {
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;
1632 }
1633
1634
1635 /******************************************************************
1636  *    IpRenewAddress (IPHLPAPI.@)
1637  *
1638  * Renew an IP obtained through DHCP.
1639  *
1640  * PARAMS
1641  *  AdapterInfo [In] adapter to renew IP address
1642  *
1643  * RETURNS
1644  *  Success: NO_ERROR
1645  *  Failure: error code from winerror.h
1646  *
1647  * NOTES
1648  *  Since GetAdaptersInfo never returns adapters that have DHCP enabled,
1649  *  this function does nothing.
1650  *
1651  * FIXME
1652  *  Stub, returns ERROR_NOT_SUPPORTED.
1653  */
1654 DWORD WINAPI IpRenewAddress(PIP_ADAPTER_INDEX_MAP AdapterInfo)
1655 {
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;
1660 }
1661
1662
1663 /******************************************************************
1664  *    NotifyAddrChange (IPHLPAPI.@)
1665  *
1666  * Notify caller whenever the ip-interface map is changed.
1667  *
1668  * PARAMS
1669  *  Handle     [Out] handle usable in asynchronous notification
1670  *  overlapped [In]  overlapped structure that notifies the caller
1671  *
1672  * RETURNS
1673  *  Success: NO_ERROR
1674  *  Failure: error code from winerror.h
1675  *
1676  * FIXME
1677  *  Stub, returns ERROR_NOT_SUPPORTED.
1678  */
1679 DWORD WINAPI NotifyAddrChange(PHANDLE Handle, LPOVERLAPPED overlapped)
1680 {
1681   FIXME("(Handle %p, overlapped %p): stub\n", Handle, overlapped);
1682   return ERROR_NOT_SUPPORTED;
1683 }
1684
1685
1686 /******************************************************************
1687  *    NotifyRouteChange (IPHLPAPI.@)
1688  *
1689  * Notify caller whenever the ip routing table is changed.
1690  *
1691  * PARAMS
1692  *  Handle     [Out] handle usable in asynchronous notification
1693  *  overlapped [In]  overlapped structure that notifies the caller
1694  *
1695  * RETURNS
1696  *  Success: NO_ERROR
1697  *  Failure: error code from winerror.h
1698  *
1699  * FIXME
1700  *  Stub, returns ERROR_NOT_SUPPORTED.
1701  */
1702 DWORD WINAPI NotifyRouteChange(PHANDLE Handle, LPOVERLAPPED overlapped)
1703 {
1704   FIXME("(Handle %p, overlapped %p): stub\n", Handle, overlapped);
1705   return ERROR_NOT_SUPPORTED;
1706 }
1707
1708
1709 /******************************************************************
1710  *    SendARP (IPHLPAPI.@)
1711  *
1712  * Send an ARP request.
1713  *
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
1719  *
1720  * RETURNS
1721  *  Success: NO_ERROR
1722  *  Failure: error code from winerror.h
1723  *
1724  * FIXME
1725  *  Stub, returns ERROR_NOT_SUPPORTED.
1726  */
1727 DWORD WINAPI SendARP(IPAddr DestIP, IPAddr SrcIP, PULONG pMacAddr, PULONG PhyAddrLen)
1728 {
1729   FIXME("(DestIP 0x%08x, SrcIP 0x%08x, pMacAddr %p, PhyAddrLen %p): stub\n",
1730    DestIP, SrcIP, pMacAddr, PhyAddrLen);
1731   return ERROR_NOT_SUPPORTED;
1732 }
1733
1734
1735 /******************************************************************
1736  *    SetIfEntry (IPHLPAPI.@)
1737  *
1738  * Set the administrative status of an interface.
1739  *
1740  * PARAMS
1741  *  pIfRow [In] dwAdminStatus member specifies the new status.
1742  *
1743  * RETURNS
1744  *  Success: NO_ERROR
1745  *  Failure: error code from winerror.h
1746  *
1747  * FIXME
1748  *  Stub, returns ERROR_NOT_SUPPORTED.
1749  */
1750 DWORD WINAPI SetIfEntry(PMIB_IFROW pIfRow)
1751 {
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;
1758 }
1759
1760
1761 /******************************************************************
1762  *    SetIpForwardEntry (IPHLPAPI.@)
1763  *
1764  * Modify an existing route.
1765  *
1766  * PARAMS
1767  *  pRoute [In] route with the new information
1768  *
1769  * RETURNS
1770  *  Success: NO_ERROR
1771  *  Failure: error code from winerror.h
1772  *
1773  * FIXME
1774  *  Stub, returns NO_ERROR.
1775  */
1776 DWORD WINAPI SetIpForwardEntry(PMIB_IPFORWARDROW pRoute)
1777 {
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;
1783 }
1784
1785
1786 /******************************************************************
1787  *    SetIpNetEntry (IPHLPAPI.@)
1788  *
1789  * Modify an existing ARP entry.
1790  *
1791  * PARAMS
1792  *  pArpEntry [In] ARP entry with the new information
1793  *
1794  * RETURNS
1795  *  Success: NO_ERROR
1796  *  Failure: error code from winerror.h
1797  *
1798  * FIXME
1799  *  Stub, returns NO_ERROR.
1800  */
1801 DWORD WINAPI SetIpNetEntry(PMIB_IPNETROW pArpEntry)
1802 {
1803   FIXME("(pArpEntry %p): stub\n", pArpEntry);
1804   /* same as CreateIpNetEntry here, could use SIOCSARP, not sure I want to */
1805   return 0;
1806 }
1807
1808
1809 /******************************************************************
1810  *    SetIpStatistics (IPHLPAPI.@)
1811  *
1812  * Toggle IP forwarding and det the default TTL value.
1813  *
1814  * PARAMS
1815  *  pIpStats [In] IP statistics with the new information
1816  *
1817  * RETURNS
1818  *  Success: NO_ERROR
1819  *  Failure: error code from winerror.h
1820  *
1821  * FIXME
1822  *  Stub, returns NO_ERROR.
1823  */
1824 DWORD WINAPI SetIpStatistics(PMIB_IPSTATS pIpStats)
1825 {
1826   FIXME("(pIpStats %p): stub\n", pIpStats);
1827   return 0;
1828 }
1829
1830
1831 /******************************************************************
1832  *    SetIpTTL (IPHLPAPI.@)
1833  *
1834  * Set the default TTL value.
1835  *
1836  * PARAMS
1837  *  nTTL [In] new TTL value
1838  *
1839  * RETURNS
1840  *  Success: NO_ERROR
1841  *  Failure: error code from winerror.h
1842  *
1843  * FIXME
1844  *  Stub, returns NO_ERROR.
1845  */
1846 DWORD WINAPI SetIpTTL(UINT nTTL)
1847 {
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;
1852 }
1853
1854
1855 /******************************************************************
1856  *    SetTcpEntry (IPHLPAPI.@)
1857  *
1858  * Set the state of a TCP connection.
1859  *
1860  * PARAMS
1861  *  pTcpRow [In] specifies connection with new state
1862  *
1863  * RETURNS
1864  *  Success: NO_ERROR
1865  *  Failure: error code from winerror.h
1866  *
1867  * FIXME
1868  *  Stub, returns NO_ERROR.
1869  */
1870 DWORD WINAPI SetTcpEntry(PMIB_TCPROW pTcpRow)
1871 {
1872   FIXME("(pTcpRow %p): stub\n", pTcpRow);
1873   return 0;
1874 }
1875
1876
1877 /******************************************************************
1878  *    UnenableRouter (IPHLPAPI.@)
1879  *
1880  * Decrement the IP-forwarding reference count. Turn off IP-forwarding
1881  * if it reaches zero.
1882  *
1883  * PARAMS
1884  *  pOverlapped     [In/Out] should be the same as in EnableRouter()
1885  *  lpdwEnableCount [Out]    optional, receives reference count
1886  *
1887  * RETURNS
1888  *  Success: NO_ERROR
1889  *  Failure: error code from winerror.h
1890  *
1891  * FIXME
1892  *  Stub, returns ERROR_NOT_SUPPORTED.
1893  */
1894 DWORD WINAPI UnenableRouter(OVERLAPPED * pOverlapped, LPDWORD lpdwEnableCount)
1895 {
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
1900    */
1901   return ERROR_NOT_SUPPORTED;
1902 }