iphlpapi: Set ConnectionType in GetAdaptersAddresses.
[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 adapterAddressesFromIndex(ULONG family, DWORD index, IP_ADAPTER_ADDRESSES *aa, ULONG *size)
700 {
701     ULONG ret, i, num_v4addrs = 0, num_v6addrs = 0, total_size;
702     DWORD *v4addrs = NULL;
703     SOCKET_ADDRESS *v6addrs = NULL;
704
705     if (family == AF_INET)
706         ret = v4addressesFromIndex(index, &v4addrs, &num_v4addrs);
707     else if (family == AF_INET6)
708         ret = v6addressesFromIndex(index, &v6addrs, &num_v6addrs);
709     else if (family == AF_UNSPEC)
710     {
711         ret = v4addressesFromIndex(index, &v4addrs, &num_v4addrs);
712         if (!ret)
713             ret = v6addressesFromIndex(index, &v6addrs, &num_v6addrs);
714     }
715     else
716     {
717         FIXME("address family %u unsupported\n", family);
718         ret = ERROR_NO_DATA;
719     }
720     if (ret) return ret;
721
722     total_size = sizeof(IP_ADAPTER_ADDRESSES);
723     total_size += IF_NAMESIZE;
724     total_size += IF_NAMESIZE * sizeof(WCHAR);
725     total_size += sizeof(IP_ADAPTER_UNICAST_ADDRESS) * num_v4addrs;
726     total_size += sizeof(struct sockaddr_in) * num_v4addrs;
727     total_size += sizeof(IP_ADAPTER_UNICAST_ADDRESS) * num_v6addrs;
728     total_size += sizeof(SOCKET_ADDRESS) * num_v6addrs;
729     for (i = 0; i < num_v6addrs; i++)
730         total_size += v6addrs[i].iSockaddrLength;
731
732     if (aa && *size >= total_size)
733     {
734         char name[IF_NAMESIZE], *ptr = (char *)aa + sizeof(IP_ADAPTER_ADDRESSES), *src;
735         WCHAR *dst;
736         DWORD buflen, type, status;
737
738         memset(aa, 0, sizeof(IP_ADAPTER_ADDRESSES));
739         aa->u.s.Length  = sizeof(IP_ADAPTER_ADDRESSES);
740         aa->u.s.IfIndex = index;
741
742         getInterfaceNameByIndex(index, name);
743         memcpy(ptr, name, IF_NAMESIZE);
744         aa->AdapterName = ptr;
745         ptr += IF_NAMESIZE;
746         aa->FriendlyName = (WCHAR *)ptr;
747         for (src = name, dst = (WCHAR *)ptr; *src; src++, dst++)
748             *dst = *src;
749         *dst++ = 0;
750         ptr = (char *)dst;
751
752         TRACE("%s: %d IPv4 addresses, %d IPv6 addresses:\n", name, num_v4addrs,
753               num_v6addrs);
754         if (num_v4addrs)
755         {
756             IP_ADAPTER_UNICAST_ADDRESS *ua;
757             struct sockaddr_in *sa;
758
759             aa->Flags |= IP_ADAPTER_IPV4_ENABLED;
760             ua = aa->FirstUnicastAddress = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
761             for (i = 0; i < num_v4addrs; i++)
762             {
763                 char addr_buf[16];
764
765                 memset(ua, 0, sizeof(IP_ADAPTER_UNICAST_ADDRESS));
766                 ua->u.s.Length              = sizeof(IP_ADAPTER_UNICAST_ADDRESS);
767                 ua->Address.iSockaddrLength = sizeof(struct sockaddr_in);
768                 ua->Address.lpSockaddr      = (SOCKADDR *)((char *)ua + ua->u.s.Length);
769
770                 sa = (struct sockaddr_in *)ua->Address.lpSockaddr;
771                 sa->sin_family      = AF_INET;
772                 sa->sin_addr.s_addr = v4addrs[i];
773                 sa->sin_port        = 0;
774                 TRACE("IPv4 %d/%d: %s\n", i + 1, num_v4addrs,
775                       debugstr_ipv4(&sa->sin_addr.s_addr, addr_buf));
776
777                 ptr += ua->u.s.Length + ua->Address.iSockaddrLength;
778                 if (i < num_v4addrs - 1)
779                 {
780                     ua->Next = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
781                     ua = ua->Next;
782                 }
783             }
784         }
785         if (num_v6addrs)
786         {
787             IP_ADAPTER_UNICAST_ADDRESS *ua;
788             struct WS_sockaddr_in6 *sa;
789
790             aa->Flags |= IP_ADAPTER_IPV6_ENABLED;
791             if (aa->FirstUnicastAddress)
792             {
793                 for (ua = aa->FirstUnicastAddress; ua->Next; ua = ua->Next)
794                     ;
795                 ua->Next = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
796                 ua = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
797             }
798             else
799                 ua = aa->FirstUnicastAddress = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
800             for (i = 0; i < num_v6addrs; i++)
801             {
802                 char addr_buf[46];
803
804                 memset(ua, 0, sizeof(IP_ADAPTER_UNICAST_ADDRESS));
805                 ua->u.s.Length              = sizeof(IP_ADAPTER_UNICAST_ADDRESS);
806                 ua->Address.iSockaddrLength = v6addrs[i].iSockaddrLength;
807                 ua->Address.lpSockaddr      = (SOCKADDR *)((char *)ua + ua->u.s.Length);
808
809                 sa = (struct WS_sockaddr_in6 *)ua->Address.lpSockaddr;
810                 memcpy(sa, v6addrs[i].lpSockaddr, sizeof(*sa));
811                 TRACE("IPv6 %d/%d: %s\n", i + 1, num_v6addrs,
812                       debugstr_ipv6(sa, addr_buf));
813
814                 ptr += ua->u.s.Length + ua->Address.iSockaddrLength;
815                 if (i < num_v6addrs - 1)
816                 {
817                     ua->Next = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
818                     ua = ua->Next;
819                 }
820             }
821         }
822
823         buflen = MAX_INTERFACE_PHYSADDR;
824         getInterfacePhysicalByIndex(index, &buflen, aa->PhysicalAddress, &type);
825         aa->PhysicalAddressLength = buflen;
826         aa->IfType = typeFromMibType(type);
827         aa->ConnectionType = connectionTypeFromMibType(type);
828
829         getInterfaceMtuByName(name, &aa->Mtu);
830
831         getInterfaceStatusByName(name, &status);
832         if (status == MIB_IF_OPER_STATUS_OPERATIONAL) aa->OperStatus = IfOperStatusUp;
833         else if (status == MIB_IF_OPER_STATUS_NON_OPERATIONAL) aa->OperStatus = IfOperStatusDown;
834         else aa->OperStatus = IfOperStatusUnknown;
835     }
836     *size = total_size;
837     HeapFree(GetProcessHeap(), 0, v6addrs);
838     HeapFree(GetProcessHeap(), 0, v4addrs);
839     return ERROR_SUCCESS;
840 }
841
842 ULONG WINAPI GetAdaptersAddresses(ULONG family, ULONG flags, PVOID reserved,
843                                   PIP_ADAPTER_ADDRESSES aa, PULONG buflen)
844 {
845     InterfaceIndexTable *table;
846     ULONG i, size, total_size, ret = ERROR_NO_DATA;
847
848     if (!buflen) return ERROR_INVALID_PARAMETER;
849
850     table = getInterfaceIndexTable();
851     if (!table || !table->numIndexes)
852     {
853         HeapFree(GetProcessHeap(), 0, table);
854         return ERROR_NO_DATA;
855     }
856     total_size = 0;
857     for (i = 0; i < table->numIndexes; i++)
858     {
859         size = 0;
860         if ((ret = adapterAddressesFromIndex(family, table->indexes[i], NULL, &size)))
861         {
862             HeapFree(GetProcessHeap(), 0, table);
863             return ret;
864         }
865         total_size += size;
866     }
867     if (aa && *buflen >= total_size)
868     {
869         ULONG bytes_left = size = total_size;
870         for (i = 0; i < table->numIndexes; i++)
871         {
872             if ((ret = adapterAddressesFromIndex(family, table->indexes[i], aa, &size)))
873             {
874                 HeapFree(GetProcessHeap(), 0, table);
875                 return ret;
876             }
877             if (i < table->numIndexes - 1)
878             {
879                 aa->Next = (IP_ADAPTER_ADDRESSES *)((char *)aa + size);
880                 aa = aa->Next;
881                 size = bytes_left -= size;
882             }
883         }
884         ret = ERROR_SUCCESS;
885     }
886     if (*buflen < total_size) ret = ERROR_BUFFER_OVERFLOW;
887     *buflen = total_size;
888
889     TRACE("num adapters %u\n", table->numIndexes);
890     HeapFree(GetProcessHeap(), 0, table);
891     return ret;
892 }
893
894 /******************************************************************
895  *    GetBestInterface (IPHLPAPI.@)
896  *
897  * Get the interface, with the best route for the given IP address.
898  *
899  * PARAMS
900  *  dwDestAddr     [In]  IP address to search the interface for
901  *  pdwBestIfIndex [Out] found best interface
902  *
903  * RETURNS
904  *  Success: NO_ERROR
905  *  Failure: error code from winerror.h
906  */
907 DWORD WINAPI GetBestInterface(IPAddr dwDestAddr, PDWORD pdwBestIfIndex)
908 {
909     struct WS_sockaddr_in sa_in;
910     memset(&sa_in, 0, sizeof(sa_in));
911     sa_in.sin_family = AF_INET;
912     sa_in.sin_addr.S_un.S_addr = dwDestAddr;
913     return GetBestInterfaceEx((struct WS_sockaddr *)&sa_in, pdwBestIfIndex);
914 }
915
916 /******************************************************************
917  *    GetBestInterfaceEx (IPHLPAPI.@)
918  *
919  * Get the interface, with the best route for the given IP address.
920  *
921  * PARAMS
922  *  dwDestAddr     [In]  IP address to search the interface for
923  *  pdwBestIfIndex [Out] found best interface
924  *
925  * RETURNS
926  *  Success: NO_ERROR
927  *  Failure: error code from winerror.h
928  */
929 DWORD WINAPI GetBestInterfaceEx(struct WS_sockaddr *pDestAddr, PDWORD pdwBestIfIndex)
930 {
931   DWORD ret;
932
933   TRACE("pDestAddr %p, pdwBestIfIndex %p\n", pDestAddr, pdwBestIfIndex);
934   if (!pDestAddr || !pdwBestIfIndex)
935     ret = ERROR_INVALID_PARAMETER;
936   else {
937     MIB_IPFORWARDROW ipRow;
938
939     if (pDestAddr->sa_family == AF_INET) {
940       ret = GetBestRoute(((struct WS_sockaddr_in *)pDestAddr)->sin_addr.S_un.S_addr, 0, &ipRow);
941       if (ret == ERROR_SUCCESS)
942         *pdwBestIfIndex = ipRow.dwForwardIfIndex;
943     } else {
944       FIXME("address family %d not supported\n", pDestAddr->sa_family);
945       ret = ERROR_NOT_SUPPORTED;
946     }
947   }
948   TRACE("returning %d\n", ret);
949   return ret;
950 }
951
952
953 /******************************************************************
954  *    GetBestRoute (IPHLPAPI.@)
955  *
956  * Get the best route for the given IP address.
957  *
958  * PARAMS
959  *  dwDestAddr   [In]  IP address to search the best route for
960  *  dwSourceAddr [In]  optional source IP address
961  *  pBestRoute   [Out] found best route
962  *
963  * RETURNS
964  *  Success: NO_ERROR
965  *  Failure: error code from winerror.h
966  */
967 DWORD WINAPI GetBestRoute(DWORD dwDestAddr, DWORD dwSourceAddr, PMIB_IPFORWARDROW pBestRoute)
968 {
969   PMIB_IPFORWARDTABLE table;
970   DWORD ret;
971
972   TRACE("dwDestAddr 0x%08x, dwSourceAddr 0x%08x, pBestRoute %p\n", dwDestAddr,
973    dwSourceAddr, pBestRoute);
974   if (!pBestRoute)
975     return ERROR_INVALID_PARAMETER;
976
977   ret = AllocateAndGetIpForwardTableFromStack(&table, FALSE, GetProcessHeap(), 0);
978   if (!ret) {
979     DWORD ndx, matchedBits, matchedNdx = table->dwNumEntries;
980
981     for (ndx = 0, matchedBits = 0; ndx < table->dwNumEntries; ndx++) {
982       if (table->table[ndx].dwForwardType != MIB_IPROUTE_TYPE_INVALID &&
983        (dwDestAddr & table->table[ndx].dwForwardMask) ==
984        (table->table[ndx].dwForwardDest & table->table[ndx].dwForwardMask)) {
985         DWORD numShifts, mask;
986
987         for (numShifts = 0, mask = table->table[ndx].dwForwardMask;
988          mask && !(mask & 1); mask >>= 1, numShifts++)
989           ;
990         if (numShifts > matchedBits) {
991           matchedBits = numShifts;
992           matchedNdx = ndx;
993         }
994         else if (!matchedBits) {
995           matchedNdx = ndx;
996         }
997       }
998     }
999     if (matchedNdx < table->dwNumEntries) {
1000       memcpy(pBestRoute, &table->table[matchedNdx], sizeof(MIB_IPFORWARDROW));
1001       ret = ERROR_SUCCESS;
1002     }
1003     else {
1004       /* No route matches, which can happen if there's no default route. */
1005       ret = ERROR_HOST_UNREACHABLE;
1006     }
1007     HeapFree(GetProcessHeap(), 0, table);
1008   }
1009   TRACE("returning %d\n", ret);
1010   return ret;
1011 }
1012
1013
1014 /******************************************************************
1015  *    GetFriendlyIfIndex (IPHLPAPI.@)
1016  *
1017  * Get a "friendly" version of IfIndex, which is one that doesn't
1018  * have the top byte set.  Doesn't validate whether IfIndex is a valid
1019  * adapter index.
1020  *
1021  * PARAMS
1022  *  IfIndex [In] interface index to get the friendly one for
1023  *
1024  * RETURNS
1025  *  A friendly version of IfIndex.
1026  */
1027 DWORD WINAPI GetFriendlyIfIndex(DWORD IfIndex)
1028 {
1029   /* windows doesn't validate these, either, just makes sure the top byte is
1030      cleared.  I assume my ifenum module never gives an index with the top
1031      byte set. */
1032   TRACE("returning %d\n", IfIndex);
1033   return IfIndex;
1034 }
1035
1036
1037 /******************************************************************
1038  *    GetIfEntry (IPHLPAPI.@)
1039  *
1040  * Get information about an interface.
1041  *
1042  * PARAMS
1043  *  pIfRow [In/Out] In:  dwIndex of MIB_IFROW selects the interface.
1044  *                  Out: interface information
1045  *
1046  * RETURNS
1047  *  Success: NO_ERROR
1048  *  Failure: error code from winerror.h
1049  */
1050 DWORD WINAPI GetIfEntry(PMIB_IFROW pIfRow)
1051 {
1052   DWORD ret;
1053   char nameBuf[MAX_ADAPTER_NAME];
1054   char *name;
1055
1056   TRACE("pIfRow %p\n", pIfRow);
1057   if (!pIfRow)
1058     return ERROR_INVALID_PARAMETER;
1059
1060   name = getInterfaceNameByIndex(pIfRow->dwIndex, nameBuf);
1061   if (name) {
1062     ret = getInterfaceEntryByName(name, pIfRow);
1063     if (ret == NO_ERROR)
1064       ret = getInterfaceStatsByName(name, pIfRow);
1065   }
1066   else
1067     ret = ERROR_INVALID_DATA;
1068   TRACE("returning %d\n", ret);
1069   return ret;
1070 }
1071
1072
1073 static int IfTableSorter(const void *a, const void *b)
1074 {
1075   int ret;
1076
1077   if (a && b)
1078     ret = ((const MIB_IFROW*)a)->dwIndex - ((const MIB_IFROW*)b)->dwIndex;
1079   else
1080     ret = 0;
1081   return ret;
1082 }
1083
1084
1085 /******************************************************************
1086  *    GetIfTable (IPHLPAPI.@)
1087  *
1088  * Get a table of local interfaces.
1089  *
1090  * PARAMS
1091  *  pIfTable [Out]    buffer for local interfaces table
1092  *  pdwSize  [In/Out] length of output buffer
1093  *  bOrder   [In]     whether to sort the table
1094  *
1095  * RETURNS
1096  *  Success: NO_ERROR
1097  *  Failure: error code from winerror.h
1098  *
1099  * NOTES
1100  *  If pdwSize is less than required, the function will return
1101  *  ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the required byte
1102  *  size.
1103  *  If bOrder is true, the returned table will be sorted by interface index.
1104  */
1105 DWORD WINAPI GetIfTable(PMIB_IFTABLE pIfTable, PULONG pdwSize, BOOL bOrder)
1106 {
1107   DWORD ret;
1108
1109   TRACE("pIfTable %p, pdwSize %p, bOrder %d\n", pdwSize, pdwSize,
1110    (DWORD)bOrder);
1111   if (!pdwSize)
1112     ret = ERROR_INVALID_PARAMETER;
1113   else {
1114     DWORD numInterfaces = getNumInterfaces();
1115     ULONG size = sizeof(MIB_IFTABLE);
1116
1117     if (numInterfaces > 1)
1118       size += (numInterfaces - 1) * sizeof(MIB_IFROW);
1119     if (!pIfTable || *pdwSize < size) {
1120       *pdwSize = size;
1121       ret = ERROR_INSUFFICIENT_BUFFER;
1122     }
1123     else {
1124       InterfaceIndexTable *table = getInterfaceIndexTable();
1125
1126       if (table) {
1127         size = sizeof(MIB_IFTABLE);
1128         if (table->numIndexes > 1)
1129           size += (table->numIndexes - 1) * sizeof(MIB_IFROW);
1130         if (*pdwSize < size) {
1131           *pdwSize = size;
1132           ret = ERROR_INSUFFICIENT_BUFFER;
1133         }
1134         else {
1135           DWORD ndx;
1136
1137           *pdwSize = size;
1138           pIfTable->dwNumEntries = 0;
1139           for (ndx = 0; ndx < table->numIndexes; ndx++) {
1140             pIfTable->table[ndx].dwIndex = table->indexes[ndx];
1141             GetIfEntry(&pIfTable->table[ndx]);
1142             pIfTable->dwNumEntries++;
1143           }
1144           if (bOrder)
1145             qsort(pIfTable->table, pIfTable->dwNumEntries, sizeof(MIB_IFROW),
1146              IfTableSorter);
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  *    GetInterfaceInfo (IPHLPAPI.@)
1162  *
1163  * Get a list of network interface adapters.
1164  *
1165  * PARAMS
1166  *  pIfTable    [Out] buffer for interface adapters
1167  *  dwOutBufLen [Out] if buffer is too small, returns required size
1168  *
1169  * RETURNS
1170  *  Success: NO_ERROR
1171  *  Failure: error code from winerror.h
1172  *
1173  * BUGS
1174  *  MSDN states this should return non-loopback interfaces only.
1175  */
1176 DWORD WINAPI GetInterfaceInfo(PIP_INTERFACE_INFO pIfTable, PULONG dwOutBufLen)
1177 {
1178   DWORD ret;
1179
1180   TRACE("pIfTable %p, dwOutBufLen %p\n", pIfTable, dwOutBufLen);
1181   if (!dwOutBufLen)
1182     ret = ERROR_INVALID_PARAMETER;
1183   else {
1184     DWORD numInterfaces = getNumInterfaces();
1185     ULONG size = sizeof(IP_INTERFACE_INFO);
1186
1187     if (numInterfaces > 1)
1188       size += (numInterfaces - 1) * sizeof(IP_ADAPTER_INDEX_MAP);
1189     if (!pIfTable || *dwOutBufLen < size) {
1190       *dwOutBufLen = size;
1191       ret = ERROR_INSUFFICIENT_BUFFER;
1192     }
1193     else {
1194       InterfaceIndexTable *table = getInterfaceIndexTable();
1195
1196       if (table) {
1197         size = sizeof(IP_INTERFACE_INFO);
1198         if (table->numIndexes > 1)
1199           size += (table->numIndexes - 1) * sizeof(IP_ADAPTER_INDEX_MAP);
1200         if (*dwOutBufLen < size) {
1201           *dwOutBufLen = size;
1202           ret = ERROR_INSUFFICIENT_BUFFER;
1203         }
1204         else {
1205           DWORD ndx;
1206           char nameBuf[MAX_ADAPTER_NAME];
1207
1208           *dwOutBufLen = size;
1209           pIfTable->NumAdapters = 0;
1210           for (ndx = 0; ndx < table->numIndexes; ndx++) {
1211             const char *walker, *name;
1212             WCHAR *assigner;
1213
1214             pIfTable->Adapter[ndx].Index = table->indexes[ndx];
1215             name = getInterfaceNameByIndex(table->indexes[ndx], nameBuf);
1216             for (walker = name, assigner = pIfTable->Adapter[ndx].Name;
1217              walker && *walker &&
1218              assigner - pIfTable->Adapter[ndx].Name < MAX_ADAPTER_NAME - 1;
1219              walker++, assigner++)
1220               *assigner = *walker;
1221             *assigner = 0;
1222             pIfTable->NumAdapters++;
1223           }
1224           ret = NO_ERROR;
1225         }
1226         HeapFree(GetProcessHeap(), 0, table);
1227       }
1228       else
1229         ret = ERROR_OUTOFMEMORY;
1230     }
1231   }
1232   TRACE("returning %d\n", ret);
1233   return ret;
1234 }
1235
1236
1237 /******************************************************************
1238  *    GetIpAddrTable (IPHLPAPI.@)
1239  *
1240  * Get interface-to-IP address mapping table. 
1241  *
1242  * PARAMS
1243  *  pIpAddrTable [Out]    buffer for mapping table
1244  *  pdwSize      [In/Out] length of output buffer
1245  *  bOrder       [In]     whether to sort the table
1246  *
1247  * RETURNS
1248  *  Success: NO_ERROR
1249  *  Failure: error code from winerror.h
1250  *
1251  * NOTES
1252  *  If pdwSize is less than required, the function will return
1253  *  ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the required byte
1254  *  size.
1255  *  If bOrder is true, the returned table will be sorted by the next hop and
1256  *  an assortment of arbitrary parameters.
1257  */
1258 DWORD WINAPI GetIpAddrTable(PMIB_IPADDRTABLE pIpAddrTable, PULONG pdwSize, BOOL bOrder)
1259 {
1260   DWORD ret;
1261
1262   TRACE("pIpAddrTable %p, pdwSize %p, bOrder %d\n", pIpAddrTable, pdwSize,
1263    (DWORD)bOrder);
1264   if (!pdwSize)
1265     ret = ERROR_INVALID_PARAMETER;
1266   else {
1267     PMIB_IPADDRTABLE table;
1268
1269     ret = getIPAddrTable(&table, GetProcessHeap(), 0);
1270     if (ret == NO_ERROR)
1271     {
1272       ULONG size = sizeof(MIB_IPADDRTABLE);
1273
1274       if (table->dwNumEntries > 1)
1275         size += (table->dwNumEntries - 1) * sizeof(MIB_IPADDRROW);
1276       if (!pIpAddrTable || *pdwSize < size) {
1277         *pdwSize = size;
1278         ret = ERROR_INSUFFICIENT_BUFFER;
1279       }
1280       else {
1281         *pdwSize = size;
1282         memcpy(pIpAddrTable, table, size);
1283         if (bOrder)
1284           qsort(pIpAddrTable->table, pIpAddrTable->dwNumEntries,
1285            sizeof(MIB_IPADDRROW), IpAddrTableSorter);
1286         ret = NO_ERROR;
1287       }
1288       HeapFree(GetProcessHeap(), 0, table);
1289     }
1290   }
1291   TRACE("returning %d\n", ret);
1292   return ret;
1293 }
1294
1295
1296 /******************************************************************
1297  *    GetIpForwardTable (IPHLPAPI.@)
1298  *
1299  * Get the route table.
1300  *
1301  * PARAMS
1302  *  pIpForwardTable [Out]    buffer for route table
1303  *  pdwSize         [In/Out] length of output buffer
1304  *  bOrder          [In]     whether to sort the table
1305  *
1306  * RETURNS
1307  *  Success: NO_ERROR
1308  *  Failure: error code from winerror.h
1309  *
1310  * NOTES
1311  *  If pdwSize is less than required, the function will return
1312  *  ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the required byte
1313  *  size.
1314  *  If bOrder is true, the returned table will be sorted by the next hop and
1315  *  an assortment of arbitrary parameters.
1316  */
1317 DWORD WINAPI GetIpForwardTable(PMIB_IPFORWARDTABLE pIpForwardTable, PULONG pdwSize, BOOL bOrder)
1318 {
1319     DWORD ret;
1320     PMIB_IPFORWARDTABLE table;
1321
1322     TRACE("pIpForwardTable %p, pdwSize %p, bOrder %d\n", pIpForwardTable, pdwSize, bOrder);
1323
1324     if (!pdwSize) return ERROR_INVALID_PARAMETER;
1325
1326     ret = AllocateAndGetIpForwardTableFromStack(&table, bOrder, GetProcessHeap(), 0);
1327     if (!ret) {
1328         DWORD size = FIELD_OFFSET( MIB_IPFORWARDTABLE, table[table->dwNumEntries] );
1329         if (!pIpForwardTable || *pdwSize < size) {
1330           *pdwSize = size;
1331           ret = ERROR_INSUFFICIENT_BUFFER;
1332         }
1333         else {
1334           *pdwSize = size;
1335           memcpy(pIpForwardTable, table, size);
1336         }
1337         HeapFree(GetProcessHeap(), 0, table);
1338     }
1339     TRACE("returning %d\n", ret);
1340     return ret;
1341 }
1342
1343
1344 /******************************************************************
1345  *    GetIpNetTable (IPHLPAPI.@)
1346  *
1347  * Get the IP-to-physical address mapping table.
1348  *
1349  * PARAMS
1350  *  pIpNetTable [Out]    buffer for mapping table
1351  *  pdwSize     [In/Out] length of output buffer
1352  *  bOrder      [In]     whether to sort the table
1353  *
1354  * RETURNS
1355  *  Success: NO_ERROR
1356  *  Failure: error code from winerror.h
1357  *
1358  * NOTES
1359  *  If pdwSize is less than required, the function will return
1360  *  ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the required byte
1361  *  size.
1362  *  If bOrder is true, the returned table will be sorted by IP address.
1363  */
1364 DWORD WINAPI GetIpNetTable(PMIB_IPNETTABLE pIpNetTable, PULONG pdwSize, BOOL bOrder)
1365 {
1366     DWORD ret;
1367     PMIB_IPNETTABLE table;
1368
1369     TRACE("pIpNetTable %p, pdwSize %p, bOrder %d\n", pIpNetTable, pdwSize, bOrder);
1370
1371     if (!pdwSize) return ERROR_INVALID_PARAMETER;
1372
1373     ret = AllocateAndGetIpNetTableFromStack( &table, bOrder, GetProcessHeap(), 0 );
1374     if (!ret) {
1375         DWORD size = FIELD_OFFSET( MIB_IPNETTABLE, table[table->dwNumEntries] );
1376         if (!pIpNetTable || *pdwSize < size) {
1377           *pdwSize = size;
1378           ret = ERROR_INSUFFICIENT_BUFFER;
1379         }
1380         else {
1381           *pdwSize = size;
1382           memcpy(pIpNetTable, table, size);
1383         }
1384         HeapFree(GetProcessHeap(), 0, table);
1385     }
1386     TRACE("returning %d\n", ret);
1387     return ret;
1388 }
1389
1390
1391 /******************************************************************
1392  *    GetNetworkParams (IPHLPAPI.@)
1393  *
1394  * Get the network parameters for the local computer.
1395  *
1396  * PARAMS
1397  *  pFixedInfo [Out]    buffer for network parameters
1398  *  pOutBufLen [In/Out] length of output buffer
1399  *
1400  * RETURNS
1401  *  Success: NO_ERROR
1402  *  Failure: error code from winerror.h
1403  *
1404  * NOTES
1405  *  If pOutBufLen is less than required, the function will return
1406  *  ERROR_INSUFFICIENT_BUFFER, and pOutBufLen will be set to the required byte
1407  *  size.
1408  */
1409 DWORD WINAPI GetNetworkParams(PFIXED_INFO pFixedInfo, PULONG pOutBufLen)
1410 {
1411   DWORD ret, size;
1412   LONG regReturn;
1413   HKEY hKey;
1414
1415   TRACE("pFixedInfo %p, pOutBufLen %p\n", pFixedInfo, pOutBufLen);
1416   if (!pOutBufLen)
1417     return ERROR_INVALID_PARAMETER;
1418
1419   initialise_resolver();
1420   size = sizeof(FIXED_INFO) + (_res.nscount > 0 ? (_res.nscount  - 1) *
1421    sizeof(IP_ADDR_STRING) : 0);
1422   if (!pFixedInfo || *pOutBufLen < size) {
1423     *pOutBufLen = size;
1424     return ERROR_BUFFER_OVERFLOW;
1425   }
1426
1427   memset(pFixedInfo, 0, size);
1428   size = sizeof(pFixedInfo->HostName);
1429   GetComputerNameExA(ComputerNameDnsHostname, pFixedInfo->HostName, &size);
1430   size = sizeof(pFixedInfo->DomainName);
1431   GetComputerNameExA(ComputerNameDnsDomain, pFixedInfo->DomainName, &size);
1432   if (_res.nscount > 0) {
1433     PIP_ADDR_STRING ptr;
1434     int i;
1435
1436     for (i = 0, ptr = &pFixedInfo->DnsServerList; i < _res.nscount && ptr;
1437      i++, ptr = ptr->Next) {
1438       toIPAddressString(_res.nsaddr_list[i].sin_addr.s_addr,
1439        ptr->IpAddress.String);
1440       if (i == _res.nscount - 1)
1441         ptr->Next = NULL;
1442       else if (i == 0)
1443         ptr->Next = (PIP_ADDR_STRING)((LPBYTE)pFixedInfo + sizeof(FIXED_INFO));
1444       else
1445         ptr->Next = (PIP_ADDR_STRING)((PBYTE)ptr + sizeof(IP_ADDR_STRING));
1446     }
1447   }
1448   pFixedInfo->NodeType = HYBRID_NODETYPE;
1449   regReturn = RegOpenKeyExA(HKEY_LOCAL_MACHINE,
1450    "SYSTEM\\CurrentControlSet\\Services\\VxD\\MSTCP", 0, KEY_READ, &hKey);
1451   if (regReturn != ERROR_SUCCESS)
1452     regReturn = RegOpenKeyExA(HKEY_LOCAL_MACHINE,
1453      "SYSTEM\\CurrentControlSet\\Services\\NetBT\\Parameters", 0, KEY_READ,
1454      &hKey);
1455   if (regReturn == ERROR_SUCCESS)
1456   {
1457     DWORD size = sizeof(pFixedInfo->ScopeId);
1458
1459     RegQueryValueExA(hKey, "ScopeID", NULL, NULL, (LPBYTE)pFixedInfo->ScopeId, &size);
1460     RegCloseKey(hKey);
1461   }
1462
1463   /* FIXME: can check whether routing's enabled in /proc/sys/net/ipv4/ip_forward
1464      I suppose could also check for a listener on port 53 to set EnableDns */
1465   ret = NO_ERROR;
1466   TRACE("returning %d\n", ret);
1467   return ret;
1468 }
1469
1470
1471 /******************************************************************
1472  *    GetNumberOfInterfaces (IPHLPAPI.@)
1473  *
1474  * Get the number of interfaces.
1475  *
1476  * PARAMS
1477  *  pdwNumIf [Out] number of interfaces
1478  *
1479  * RETURNS
1480  *  NO_ERROR on success, ERROR_INVALID_PARAMETER if pdwNumIf is NULL.
1481  */
1482 DWORD WINAPI GetNumberOfInterfaces(PDWORD pdwNumIf)
1483 {
1484   DWORD ret;
1485
1486   TRACE("pdwNumIf %p\n", pdwNumIf);
1487   if (!pdwNumIf)
1488     ret = ERROR_INVALID_PARAMETER;
1489   else {
1490     *pdwNumIf = getNumInterfaces();
1491     ret = NO_ERROR;
1492   }
1493   TRACE("returning %d\n", ret);
1494   return ret;
1495 }
1496
1497
1498 /******************************************************************
1499  *    GetPerAdapterInfo (IPHLPAPI.@)
1500  *
1501  * Get information about an adapter corresponding to an interface.
1502  *
1503  * PARAMS
1504  *  IfIndex         [In]     interface info
1505  *  pPerAdapterInfo [Out]    buffer for per adapter info
1506  *  pOutBufLen      [In/Out] length of output buffer
1507  *
1508  * RETURNS
1509  *  Success: NO_ERROR
1510  *  Failure: error code from winerror.h
1511  *
1512  * FIXME
1513  *  Stub, returns empty IP_PER_ADAPTER_INFO in every case.
1514  */
1515 DWORD WINAPI GetPerAdapterInfo(ULONG IfIndex, PIP_PER_ADAPTER_INFO pPerAdapterInfo, PULONG pOutBufLen)
1516 {
1517   ULONG bytesNeeded = sizeof(IP_PER_ADAPTER_INFO);
1518
1519   TRACE("(IfIndex %d, pPerAdapterInfo %p, pOutBufLen %p)\n", IfIndex, pPerAdapterInfo, pOutBufLen);
1520
1521   if (!pOutBufLen) return ERROR_INVALID_PARAMETER;
1522
1523   if (!pPerAdapterInfo || *pOutBufLen < bytesNeeded)
1524   {
1525     *pOutBufLen = bytesNeeded;
1526     return ERROR_BUFFER_OVERFLOW;
1527   }
1528
1529   memset(pPerAdapterInfo, 0, bytesNeeded);
1530   return NO_ERROR;
1531 }
1532
1533
1534 /******************************************************************
1535  *    GetRTTAndHopCount (IPHLPAPI.@)
1536  *
1537  * Get round-trip time (RTT) and hop count.
1538  *
1539  * PARAMS
1540  *
1541  *  DestIpAddress [In]  destination address to get the info for
1542  *  HopCount      [Out] retrieved hop count
1543  *  MaxHops       [In]  maximum hops to search for the destination
1544  *  RTT           [Out] RTT in milliseconds
1545  *
1546  * RETURNS
1547  *  Success: TRUE
1548  *  Failure: FALSE
1549  *
1550  * FIXME
1551  *  Stub, returns FALSE.
1552  */
1553 BOOL WINAPI GetRTTAndHopCount(IPAddr DestIpAddress, PULONG HopCount, ULONG MaxHops, PULONG RTT)
1554 {
1555   FIXME("(DestIpAddress 0x%08x, HopCount %p, MaxHops %d, RTT %p): stub\n",
1556    DestIpAddress, HopCount, MaxHops, RTT);
1557   return FALSE;
1558 }
1559
1560
1561 /******************************************************************
1562  *    GetTcpTable (IPHLPAPI.@)
1563  *
1564  * Get the table of active TCP connections.
1565  *
1566  * PARAMS
1567  *  pTcpTable [Out]    buffer for TCP connections table
1568  *  pdwSize   [In/Out] length of output buffer
1569  *  bOrder    [In]     whether to order the table
1570  *
1571  * RETURNS
1572  *  Success: NO_ERROR
1573  *  Failure: error code from winerror.h
1574  *
1575  * NOTES
1576  *  If pdwSize is less than required, the function will return 
1577  *  ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to 
1578  *  the required byte size.
1579  *  If bOrder is true, the returned table will be sorted, first by
1580  *  local address and port number, then by remote address and port
1581  *  number.
1582  */
1583 DWORD WINAPI GetTcpTable(PMIB_TCPTABLE pTcpTable, PDWORD pdwSize, BOOL bOrder)
1584 {
1585     DWORD ret;
1586     PMIB_TCPTABLE table;
1587
1588     TRACE("pTcpTable %p, pdwSize %p, bOrder %d\n", pTcpTable, pdwSize, bOrder);
1589
1590     if (!pdwSize) return ERROR_INVALID_PARAMETER;
1591
1592     ret = AllocateAndGetTcpTableFromStack(&table, bOrder, GetProcessHeap(), 0);
1593     if (!ret) {
1594         DWORD size = FIELD_OFFSET( MIB_TCPTABLE, table[table->dwNumEntries] );
1595         if (!pTcpTable || *pdwSize < size) {
1596           *pdwSize = size;
1597           ret = ERROR_INSUFFICIENT_BUFFER;
1598         }
1599         else {
1600           *pdwSize = size;
1601           memcpy(pTcpTable, table, size);
1602         }
1603         HeapFree(GetProcessHeap(), 0, table);
1604     }
1605     TRACE("returning %d\n", ret);
1606     return ret;
1607 }
1608
1609
1610 /******************************************************************
1611  *    GetUdpTable (IPHLPAPI.@)
1612  *
1613  * Get a table of active UDP connections.
1614  *
1615  * PARAMS
1616  *  pUdpTable [Out]    buffer for UDP connections table
1617  *  pdwSize   [In/Out] length of output buffer
1618  *  bOrder    [In]     whether to order the table
1619  *
1620  * RETURNS
1621  *  Success: NO_ERROR
1622  *  Failure: error code from winerror.h
1623  *
1624  * NOTES
1625  *  If pdwSize is less than required, the function will return 
1626  *  ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the
1627  *  required byte size.
1628  *  If bOrder is true, the returned table will be sorted, first by
1629  *  local address, then by local port number.
1630  */
1631 DWORD WINAPI GetUdpTable(PMIB_UDPTABLE pUdpTable, PDWORD pdwSize, BOOL bOrder)
1632 {
1633     DWORD ret;
1634     PMIB_UDPTABLE table;
1635
1636     TRACE("pUdpTable %p, pdwSize %p, bOrder %d\n", pUdpTable, pdwSize, bOrder);
1637
1638     if (!pdwSize) return ERROR_INVALID_PARAMETER;
1639
1640     ret = AllocateAndGetUdpTableFromStack( &table, bOrder, GetProcessHeap(), 0 );
1641     if (!ret) {
1642         DWORD size = FIELD_OFFSET( MIB_UDPTABLE, table[table->dwNumEntries] );
1643         if (!pUdpTable || *pdwSize < size) {
1644           *pdwSize = size;
1645           ret = ERROR_INSUFFICIENT_BUFFER;
1646         }
1647         else {
1648           *pdwSize = size;
1649           memcpy(pUdpTable, table, size);
1650         }
1651         HeapFree(GetProcessHeap(), 0, table);
1652     }
1653     TRACE("returning %d\n", ret);
1654     return ret;
1655 }
1656
1657
1658 /******************************************************************
1659  *    GetUniDirectionalAdapterInfo (IPHLPAPI.@)
1660  *
1661  * This is a Win98-only function to get information on "unidirectional"
1662  * adapters.  Since this is pretty nonsensical in other contexts, it
1663  * never returns anything.
1664  *
1665  * PARAMS
1666  *  pIPIfInfo   [Out] buffer for adapter infos
1667  *  dwOutBufLen [Out] length of the output buffer
1668  *
1669  * RETURNS
1670  *  Success: NO_ERROR
1671  *  Failure: error code from winerror.h
1672  *
1673  * FIXME
1674  *  Stub, returns ERROR_NOT_SUPPORTED.
1675  */
1676 DWORD WINAPI GetUniDirectionalAdapterInfo(PIP_UNIDIRECTIONAL_ADAPTER_ADDRESS pIPIfInfo, PULONG dwOutBufLen)
1677 {
1678   TRACE("pIPIfInfo %p, dwOutBufLen %p\n", pIPIfInfo, dwOutBufLen);
1679   /* a unidirectional adapter?? not bloody likely! */
1680   return ERROR_NOT_SUPPORTED;
1681 }
1682
1683
1684 /******************************************************************
1685  *    IpReleaseAddress (IPHLPAPI.@)
1686  *
1687  * Release an IP obtained through DHCP,
1688  *
1689  * PARAMS
1690  *  AdapterInfo [In] adapter to release IP address
1691  *
1692  * RETURNS
1693  *  Success: NO_ERROR
1694  *  Failure: error code from winerror.h
1695  *
1696  * NOTES
1697  *  Since GetAdaptersInfo never returns adapters that have DHCP enabled,
1698  *  this function does nothing.
1699  *
1700  * FIXME
1701  *  Stub, returns ERROR_NOT_SUPPORTED.
1702  */
1703 DWORD WINAPI IpReleaseAddress(PIP_ADAPTER_INDEX_MAP AdapterInfo)
1704 {
1705   TRACE("AdapterInfo %p\n", AdapterInfo);
1706   /* not a stub, never going to support this (and I never mark an adapter as
1707      DHCP enabled, see GetAdaptersInfo, so this should never get called) */
1708   return ERROR_NOT_SUPPORTED;
1709 }
1710
1711
1712 /******************************************************************
1713  *    IpRenewAddress (IPHLPAPI.@)
1714  *
1715  * Renew an IP obtained through DHCP.
1716  *
1717  * PARAMS
1718  *  AdapterInfo [In] adapter to renew IP address
1719  *
1720  * RETURNS
1721  *  Success: NO_ERROR
1722  *  Failure: error code from winerror.h
1723  *
1724  * NOTES
1725  *  Since GetAdaptersInfo never returns adapters that have DHCP enabled,
1726  *  this function does nothing.
1727  *
1728  * FIXME
1729  *  Stub, returns ERROR_NOT_SUPPORTED.
1730  */
1731 DWORD WINAPI IpRenewAddress(PIP_ADAPTER_INDEX_MAP AdapterInfo)
1732 {
1733   TRACE("AdapterInfo %p\n", AdapterInfo);
1734   /* not a stub, never going to support this (and I never mark an adapter as
1735      DHCP enabled, see GetAdaptersInfo, so this should never get called) */
1736   return ERROR_NOT_SUPPORTED;
1737 }
1738
1739
1740 /******************************************************************
1741  *    NotifyAddrChange (IPHLPAPI.@)
1742  *
1743  * Notify caller whenever the ip-interface map is changed.
1744  *
1745  * PARAMS
1746  *  Handle     [Out] handle usable in asynchronous notification
1747  *  overlapped [In]  overlapped structure that notifies the caller
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 NotifyAddrChange(PHANDLE Handle, LPOVERLAPPED overlapped)
1757 {
1758   FIXME("(Handle %p, overlapped %p): stub\n", Handle, overlapped);
1759   return ERROR_NOT_SUPPORTED;
1760 }
1761
1762
1763 /******************************************************************
1764  *    NotifyRouteChange (IPHLPAPI.@)
1765  *
1766  * Notify caller whenever the ip routing table is changed.
1767  *
1768  * PARAMS
1769  *  Handle     [Out] handle usable in asynchronous notification
1770  *  overlapped [In]  overlapped structure that notifies the caller
1771  *
1772  * RETURNS
1773  *  Success: NO_ERROR
1774  *  Failure: error code from winerror.h
1775  *
1776  * FIXME
1777  *  Stub, returns ERROR_NOT_SUPPORTED.
1778  */
1779 DWORD WINAPI NotifyRouteChange(PHANDLE Handle, LPOVERLAPPED overlapped)
1780 {
1781   FIXME("(Handle %p, overlapped %p): stub\n", Handle, overlapped);
1782   return ERROR_NOT_SUPPORTED;
1783 }
1784
1785
1786 /******************************************************************
1787  *    SendARP (IPHLPAPI.@)
1788  *
1789  * Send an ARP request.
1790  *
1791  * PARAMS
1792  *  DestIP     [In]     attempt to obtain this IP
1793  *  SrcIP      [In]     optional sender IP address
1794  *  pMacAddr   [Out]    buffer for the mac address
1795  *  PhyAddrLen [In/Out] length of the output buffer
1796  *
1797  * RETURNS
1798  *  Success: NO_ERROR
1799  *  Failure: error code from winerror.h
1800  *
1801  * FIXME
1802  *  Stub, returns ERROR_NOT_SUPPORTED.
1803  */
1804 DWORD WINAPI SendARP(IPAddr DestIP, IPAddr SrcIP, PULONG pMacAddr, PULONG PhyAddrLen)
1805 {
1806   FIXME("(DestIP 0x%08x, SrcIP 0x%08x, pMacAddr %p, PhyAddrLen %p): stub\n",
1807    DestIP, SrcIP, pMacAddr, PhyAddrLen);
1808   return ERROR_NOT_SUPPORTED;
1809 }
1810
1811
1812 /******************************************************************
1813  *    SetIfEntry (IPHLPAPI.@)
1814  *
1815  * Set the administrative status of an interface.
1816  *
1817  * PARAMS
1818  *  pIfRow [In] dwAdminStatus member specifies the new status.
1819  *
1820  * RETURNS
1821  *  Success: NO_ERROR
1822  *  Failure: error code from winerror.h
1823  *
1824  * FIXME
1825  *  Stub, returns ERROR_NOT_SUPPORTED.
1826  */
1827 DWORD WINAPI SetIfEntry(PMIB_IFROW pIfRow)
1828 {
1829   FIXME("(pIfRow %p): stub\n", pIfRow);
1830   /* this is supposed to set an interface administratively up or down.
1831      Could do SIOCSIFFLAGS and set/clear IFF_UP, but, not sure I want to, and
1832      this sort of down is indistinguishable from other sorts of down (e.g. no
1833      link). */
1834   return ERROR_NOT_SUPPORTED;
1835 }
1836
1837
1838 /******************************************************************
1839  *    SetIpForwardEntry (IPHLPAPI.@)
1840  *
1841  * Modify an existing route.
1842  *
1843  * PARAMS
1844  *  pRoute [In] route with the new information
1845  *
1846  * RETURNS
1847  *  Success: NO_ERROR
1848  *  Failure: error code from winerror.h
1849  *
1850  * FIXME
1851  *  Stub, returns NO_ERROR.
1852  */
1853 DWORD WINAPI SetIpForwardEntry(PMIB_IPFORWARDROW pRoute)
1854 {
1855   FIXME("(pRoute %p): stub\n", pRoute);
1856   /* this is to add a route entry, how's it distinguishable from
1857      CreateIpForwardEntry?
1858      could use SIOCADDRT, not sure I want to */
1859   return 0;
1860 }
1861
1862
1863 /******************************************************************
1864  *    SetIpNetEntry (IPHLPAPI.@)
1865  *
1866  * Modify an existing ARP entry.
1867  *
1868  * PARAMS
1869  *  pArpEntry [In] ARP entry with the new information
1870  *
1871  * RETURNS
1872  *  Success: NO_ERROR
1873  *  Failure: error code from winerror.h
1874  *
1875  * FIXME
1876  *  Stub, returns NO_ERROR.
1877  */
1878 DWORD WINAPI SetIpNetEntry(PMIB_IPNETROW pArpEntry)
1879 {
1880   FIXME("(pArpEntry %p): stub\n", pArpEntry);
1881   /* same as CreateIpNetEntry here, could use SIOCSARP, not sure I want to */
1882   return 0;
1883 }
1884
1885
1886 /******************************************************************
1887  *    SetIpStatistics (IPHLPAPI.@)
1888  *
1889  * Toggle IP forwarding and det the default TTL value.
1890  *
1891  * PARAMS
1892  *  pIpStats [In] IP statistics with the new information
1893  *
1894  * RETURNS
1895  *  Success: NO_ERROR
1896  *  Failure: error code from winerror.h
1897  *
1898  * FIXME
1899  *  Stub, returns NO_ERROR.
1900  */
1901 DWORD WINAPI SetIpStatistics(PMIB_IPSTATS pIpStats)
1902 {
1903   FIXME("(pIpStats %p): stub\n", pIpStats);
1904   return 0;
1905 }
1906
1907
1908 /******************************************************************
1909  *    SetIpTTL (IPHLPAPI.@)
1910  *
1911  * Set the default TTL value.
1912  *
1913  * PARAMS
1914  *  nTTL [In] new TTL value
1915  *
1916  * RETURNS
1917  *  Success: NO_ERROR
1918  *  Failure: error code from winerror.h
1919  *
1920  * FIXME
1921  *  Stub, returns NO_ERROR.
1922  */
1923 DWORD WINAPI SetIpTTL(UINT nTTL)
1924 {
1925   FIXME("(nTTL %d): stub\n", nTTL);
1926   /* could echo nTTL > /proc/net/sys/net/ipv4/ip_default_ttl, not sure I
1927      want to.  Could map EACCESS to ERROR_ACCESS_DENIED, I suppose */
1928   return 0;
1929 }
1930
1931
1932 /******************************************************************
1933  *    SetTcpEntry (IPHLPAPI.@)
1934  *
1935  * Set the state of a TCP connection.
1936  *
1937  * PARAMS
1938  *  pTcpRow [In] specifies connection with new state
1939  *
1940  * RETURNS
1941  *  Success: NO_ERROR
1942  *  Failure: error code from winerror.h
1943  *
1944  * FIXME
1945  *  Stub, returns NO_ERROR.
1946  */
1947 DWORD WINAPI SetTcpEntry(PMIB_TCPROW pTcpRow)
1948 {
1949   FIXME("(pTcpRow %p): stub\n", pTcpRow);
1950   return 0;
1951 }
1952
1953
1954 /******************************************************************
1955  *    UnenableRouter (IPHLPAPI.@)
1956  *
1957  * Decrement the IP-forwarding reference count. Turn off IP-forwarding
1958  * if it reaches zero.
1959  *
1960  * PARAMS
1961  *  pOverlapped     [In/Out] should be the same as in EnableRouter()
1962  *  lpdwEnableCount [Out]    optional, receives reference count
1963  *
1964  * RETURNS
1965  *  Success: NO_ERROR
1966  *  Failure: error code from winerror.h
1967  *
1968  * FIXME
1969  *  Stub, returns ERROR_NOT_SUPPORTED.
1970  */
1971 DWORD WINAPI UnenableRouter(OVERLAPPED * pOverlapped, LPDWORD lpdwEnableCount)
1972 {
1973   FIXME("(pOverlapped %p, lpdwEnableCount %p): stub\n", pOverlapped,
1974    lpdwEnableCount);
1975   /* could echo "0" > /proc/net/sys/net/ipv4/ip_forward, not sure I want to
1976      could map EACCESS to ERROR_ACCESS_DENIED, I suppose
1977    */
1978   return ERROR_NOT_SUPPORTED;
1979 }