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