iphlpapi: Implement GetIpStatisticsEx on Linux.
[wine] / dlls / iphlpapi / tests / iphlpapi.c
1 /*
2  * iphlpapi dll test
3  *
4  * Copyright (C) 2003 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 /*
22  * Some observations that an automated test can't produce:
23  * An adapter index is a key for an adapter.  That is, if an index is returned
24  * from one API, that same index may be used successfully in another API, as
25  * long as the adapter remains present.
26  * If the adapter is removed and reinserted, however, the index may change (and
27  * indeed it does change on Win2K).
28  *
29  * The Name field of the IP_ADAPTER_INDEX_MAP entries returned by
30  * GetInterfaceInfo is declared as a wide string, but the bytes are actually
31  * an ASCII string on some versions of the IP helper API under Win9x.  This was
32  * apparently an MS bug, it's corrected in later versions.
33  *
34  * The DomainName field of FIXED_INFO isn't NULL-terminated on Win98.
35  */
36
37 #include <stdarg.h>
38 #include "winsock2.h"
39 #include "windef.h"
40 #include "winbase.h"
41 #include "iphlpapi.h"
42 #include "iprtrmib.h"
43 #include "wine/test.h"
44 #include <stdio.h>
45 #include <stdlib.h>
46
47 static HMODULE hLibrary = NULL;
48
49 static DWORD (WINAPI *pGetNumberOfInterfaces)(PDWORD);
50 static DWORD (WINAPI *pGetIpAddrTable)(PMIB_IPADDRTABLE,PULONG,BOOL);
51 static DWORD (WINAPI *pGetIfEntry)(PMIB_IFROW);
52 static DWORD (WINAPI *pGetFriendlyIfIndex)(DWORD);
53 static DWORD (WINAPI *pGetIfTable)(PMIB_IFTABLE,PULONG,BOOL);
54 static DWORD (WINAPI *pGetIpForwardTable)(PMIB_IPFORWARDTABLE,PULONG,BOOL);
55 static DWORD (WINAPI *pGetIpNetTable)(PMIB_IPNETTABLE,PULONG,BOOL);
56 static DWORD (WINAPI *pGetInterfaceInfo)(PIP_INTERFACE_INFO,PULONG);
57 static DWORD (WINAPI *pGetAdaptersInfo)(PIP_ADAPTER_INFO,PULONG);
58 static DWORD (WINAPI *pGetNetworkParams)(PFIXED_INFO,PULONG);
59 static DWORD (WINAPI *pGetIcmpStatistics)(PMIB_ICMP);
60 static DWORD (WINAPI *pGetIpStatistics)(PMIB_IPSTATS);
61 static DWORD (WINAPI *pGetTcpStatistics)(PMIB_TCPSTATS);
62 static DWORD (WINAPI *pGetUdpStatistics)(PMIB_UDPSTATS);
63 static DWORD (WINAPI *pGetIcmpStatisticsEx)(PMIB_ICMP_EX,DWORD);
64 static DWORD (WINAPI *pGetIpStatisticsEx)(PMIB_IPSTATS,DWORD);
65 static DWORD (WINAPI *pGetTcpStatisticsEx)(PMIB_TCPSTATS,DWORD);
66 static DWORD (WINAPI *pGetUdpStatisticsEx)(PMIB_UDPSTATS,DWORD);
67 static DWORD (WINAPI *pGetTcpTable)(PMIB_TCPTABLE,PDWORD,BOOL);
68 static DWORD (WINAPI *pGetUdpTable)(PMIB_UDPTABLE,PDWORD,BOOL);
69 static DWORD (WINAPI *pGetPerAdapterInfo)(ULONG,PIP_PER_ADAPTER_INFO,PULONG);
70 static DWORD (WINAPI *pGetAdaptersAddresses)(ULONG,ULONG,PVOID,PIP_ADAPTER_ADDRESSES,PULONG);
71 static DWORD (WINAPI *pNotifyAddrChange)(PHANDLE,LPOVERLAPPED);
72 static BOOL  (WINAPI *pCancelIPChangeNotify)(LPOVERLAPPED);
73 static DWORD (WINAPI *pGetExtendedTcpTable)(PVOID,PDWORD,BOOL,ULONG,TCP_TABLE_CLASS,ULONG);
74 static DWORD (WINAPI *pGetExtendedUdpTable)(PVOID,PDWORD,BOOL,ULONG,UDP_TABLE_CLASS,ULONG);
75 static DWORD (WINAPI *pSetTcpEntry)(PMIB_TCPROW);
76
77 static void loadIPHlpApi(void)
78 {
79   hLibrary = LoadLibraryA("iphlpapi.dll");
80   if (hLibrary) {
81     pGetNumberOfInterfaces = (void *)GetProcAddress(hLibrary, "GetNumberOfInterfaces");
82     pGetIpAddrTable = (void *)GetProcAddress(hLibrary, "GetIpAddrTable");
83     pGetIfEntry = (void *)GetProcAddress(hLibrary, "GetIfEntry");
84     pGetFriendlyIfIndex = (void *)GetProcAddress(hLibrary, "GetFriendlyIfIndex");
85     pGetIfTable = (void *)GetProcAddress(hLibrary, "GetIfTable");
86     pGetIpForwardTable = (void *)GetProcAddress(hLibrary, "GetIpForwardTable");
87     pGetIpNetTable = (void *)GetProcAddress(hLibrary, "GetIpNetTable");
88     pGetInterfaceInfo = (void *)GetProcAddress(hLibrary, "GetInterfaceInfo");
89     pGetAdaptersInfo = (void *)GetProcAddress(hLibrary, "GetAdaptersInfo");
90     pGetNetworkParams = (void *)GetProcAddress(hLibrary, "GetNetworkParams");
91     pGetIcmpStatistics = (void *)GetProcAddress(hLibrary, "GetIcmpStatistics");
92     pGetIpStatistics = (void *)GetProcAddress(hLibrary, "GetIpStatistics");
93     pGetTcpStatistics = (void *)GetProcAddress(hLibrary, "GetTcpStatistics");
94     pGetUdpStatistics = (void *)GetProcAddress(hLibrary, "GetUdpStatistics");
95     pGetIcmpStatisticsEx = (void *)GetProcAddress(hLibrary, "GetIcmpStatisticsEx");
96     pGetIpStatisticsEx = (void *)GetProcAddress(hLibrary, "GetIpStatisticsEx");
97     pGetTcpStatisticsEx = (void *)GetProcAddress(hLibrary, "GetTcpStatisticsEx");
98     pGetUdpStatisticsEx = (void *)GetProcAddress(hLibrary, "GetUdpStatisticsEx");
99     pGetTcpTable = (void *)GetProcAddress(hLibrary, "GetTcpTable");
100     pGetUdpTable = (void *)GetProcAddress(hLibrary, "GetUdpTable");
101     pGetPerAdapterInfo = (void *)GetProcAddress(hLibrary, "GetPerAdapterInfo");
102     pGetAdaptersAddresses = (void *)GetProcAddress(hLibrary, "GetAdaptersAddresses");
103     pNotifyAddrChange = (void *)GetProcAddress(hLibrary, "NotifyAddrChange");
104     pCancelIPChangeNotify = (void *)GetProcAddress(hLibrary, "CancelIPChangeNotify");
105     pGetExtendedTcpTable = (void *)GetProcAddress(hLibrary, "GetExtendedTcpTable");
106     pGetExtendedUdpTable = (void *)GetProcAddress(hLibrary, "GetExtendedUdpTable");
107     pSetTcpEntry = (void *)GetProcAddress(hLibrary, "SetTcpEntry");
108   }
109 }
110
111 static void freeIPHlpApi(void)
112 {
113     FreeLibrary(hLibrary);
114 }
115
116 /* replacement for inet_ntoa */
117 static const char *ntoa( DWORD ip )
118 {
119     static char buffer[40];
120
121     ip = htonl(ip);
122     sprintf( buffer, "%u.%u.%u.%u", (ip >> 24) & 0xff, (ip >> 16) & 0xff, (ip >> 8) & 0xff, ip & 0xff );
123     return buffer;
124 }
125
126 /*
127 still-to-be-tested 98-only functions:
128 GetUniDirectionalAdapterInfo
129 */
130 static void testWin98OnlyFunctions(void)
131 {
132 }
133
134 static void testGetNumberOfInterfaces(void)
135 {
136   if (pGetNumberOfInterfaces) {
137     DWORD apiReturn, numInterfaces;
138
139     /* Crashes on Vista */
140     if (0) {
141       apiReturn = pGetNumberOfInterfaces(NULL);
142       if (apiReturn == ERROR_NOT_SUPPORTED)
143         return;
144       ok(apiReturn == ERROR_INVALID_PARAMETER,
145        "GetNumberOfInterfaces(NULL) returned %d, expected ERROR_INVALID_PARAMETER\n",
146        apiReturn);
147     }
148
149     apiReturn = pGetNumberOfInterfaces(&numInterfaces);
150     if (apiReturn == ERROR_NOT_SUPPORTED) {
151       skip("GetNumberOfInterfaces is not supported\n");
152       return;
153     }
154     ok(apiReturn == NO_ERROR,
155      "GetNumberOfInterfaces returned %d, expected 0\n", apiReturn);
156   }
157 }
158
159 static void testGetIfEntry(DWORD index)
160 {
161   if (pGetIfEntry) {
162     DWORD apiReturn;
163     MIB_IFROW row;
164
165     memset(&row, 0, sizeof(row));
166     apiReturn = pGetIfEntry(NULL);
167     if (apiReturn == ERROR_NOT_SUPPORTED) {
168       skip("GetIfEntry is not supported\n");
169       return;
170     }
171     ok(apiReturn == ERROR_INVALID_PARAMETER,
172      "GetIfEntry(NULL) returned %d, expected ERROR_INVALID_PARAMETER\n",
173      apiReturn);
174     row.dwIndex = -1; /* hope that's always bogus! */
175     apiReturn = pGetIfEntry(&row);
176     ok(apiReturn == ERROR_INVALID_DATA ||
177      apiReturn == ERROR_FILE_NOT_FOUND /* Vista */,
178      "GetIfEntry(bogus row) returned %d, expected ERROR_INVALID_DATA or ERROR_FILE_NOT_FOUND\n",
179      apiReturn);
180     row.dwIndex = index;
181     apiReturn = pGetIfEntry(&row);
182     ok(apiReturn == NO_ERROR, 
183      "GetIfEntry returned %d, expected NO_ERROR\n", apiReturn);
184   }
185 }
186
187 static void testGetIpAddrTable(void)
188 {
189   if (pGetIpAddrTable) {
190     DWORD apiReturn;
191     ULONG dwSize = 0;
192
193     apiReturn = pGetIpAddrTable(NULL, NULL, FALSE);
194     if (apiReturn == ERROR_NOT_SUPPORTED) {
195       skip("GetIpAddrTable is not supported\n");
196       return;
197     }
198     ok(apiReturn == ERROR_INVALID_PARAMETER,
199      "GetIpAddrTable(NULL, NULL, FALSE) returned %d, expected ERROR_INVALID_PARAMETER\n",
200      apiReturn);
201     apiReturn = pGetIpAddrTable(NULL, &dwSize, FALSE);
202     ok(apiReturn == ERROR_INSUFFICIENT_BUFFER,
203      "GetIpAddrTable(NULL, &dwSize, FALSE) returned %d, expected ERROR_INSUFFICIENT_BUFFER\n",
204      apiReturn);
205     if (apiReturn == ERROR_INSUFFICIENT_BUFFER) {
206       PMIB_IPADDRTABLE buf = HeapAlloc(GetProcessHeap(), 0, dwSize);
207
208       apiReturn = pGetIpAddrTable(buf, &dwSize, FALSE);
209       ok(apiReturn == NO_ERROR,
210        "GetIpAddrTable(buf, &dwSize, FALSE) returned %d, expected NO_ERROR\n",
211        apiReturn);
212       if (apiReturn == NO_ERROR && buf->dwNumEntries)
213         testGetIfEntry(buf->table[0].dwIndex);
214       HeapFree(GetProcessHeap(), 0, buf);
215     }
216   }
217 }
218
219 static void testGetIfTable(void)
220 {
221   if (pGetIfTable) {
222     DWORD apiReturn;
223     ULONG dwSize = 0;
224
225     apiReturn = pGetIfTable(NULL, NULL, FALSE);
226     if (apiReturn == ERROR_NOT_SUPPORTED) {
227       skip("GetIfTable is not supported\n");
228       return;
229     }
230     ok(apiReturn == ERROR_INVALID_PARAMETER,
231      "GetIfTable(NULL, NULL, FALSE) returned %d, expected ERROR_INVALID_PARAMETER\n",
232      apiReturn);
233     apiReturn = pGetIfTable(NULL, &dwSize, FALSE);
234     ok(apiReturn == ERROR_INSUFFICIENT_BUFFER,
235      "GetIfTable(NULL, &dwSize, FALSE) returned %d, expected ERROR_INSUFFICIENT_BUFFER\n",
236      apiReturn);
237     if (apiReturn == ERROR_INSUFFICIENT_BUFFER) {
238       PMIB_IFTABLE buf = HeapAlloc(GetProcessHeap(), 0, dwSize);
239
240       apiReturn = pGetIfTable(buf, &dwSize, FALSE);
241       ok(apiReturn == NO_ERROR,
242        "GetIfTable(buf, &dwSize, FALSE) returned %d, expected NO_ERROR\n\n",
243        apiReturn);
244
245       if (apiReturn == NO_ERROR && winetest_debug > 1)
246       {
247           DWORD i, j;
248           char name[MAX_INTERFACE_NAME_LEN];
249
250           trace( "interface table: %u entries\n", buf->dwNumEntries );
251           for (i = 0; i < buf->dwNumEntries; i++)
252           {
253               MIB_IFROW *row = &buf->table[i];
254               WideCharToMultiByte( CP_ACP, 0, row->wszName, -1, name, MAX_INTERFACE_NAME_LEN, NULL, NULL );
255               trace( "%u: '%s' type %u mtu %u speed %u phys",
256                      row->dwIndex, name, row->dwType, row->dwMtu, row->dwSpeed );
257               for (j = 0; j < row->dwPhysAddrLen; j++)
258                   printf( " %02x", row->bPhysAddr[j] );
259               printf( "\n" );
260               trace( "        in: bytes %u upkts %u nupkts %u disc %u err %u unk %u\n",
261                      row->dwInOctets, row->dwInUcastPkts, row->dwInNUcastPkts,
262                      row->dwInDiscards, row->dwInErrors, row->dwInUnknownProtos );
263               trace( "        out: bytes %u upkts %u nupkts %u disc %u err %u\n",
264                      row->dwOutOctets, row->dwOutUcastPkts, row->dwOutNUcastPkts,
265                      row->dwOutDiscards, row->dwOutErrors );
266           }
267       }
268       HeapFree(GetProcessHeap(), 0, buf);
269     }
270   }
271 }
272
273 static void testGetIpForwardTable(void)
274 {
275   if (pGetIpForwardTable) {
276     DWORD apiReturn;
277     ULONG dwSize = 0;
278
279     apiReturn = pGetIpForwardTable(NULL, NULL, FALSE);
280     if (apiReturn == ERROR_NOT_SUPPORTED) {
281       skip("GetIpForwardTable is not supported\n");
282       return;
283     }
284     ok(apiReturn == ERROR_INVALID_PARAMETER,
285      "GetIpForwardTable(NULL, NULL, FALSE) returned %d, expected ERROR_INVALID_PARAMETER\n",
286      apiReturn);
287     apiReturn = pGetIpForwardTable(NULL, &dwSize, FALSE);
288     ok(apiReturn == ERROR_INSUFFICIENT_BUFFER,
289      "GetIpForwardTable(NULL, &dwSize, FALSE) returned %d, expected ERROR_INSUFFICIENT_BUFFER\n",
290      apiReturn);
291     if (apiReturn == ERROR_INSUFFICIENT_BUFFER) {
292       PMIB_IPFORWARDTABLE buf = HeapAlloc(GetProcessHeap(), 0, dwSize);
293
294       apiReturn = pGetIpForwardTable(buf, &dwSize, FALSE);
295       ok(apiReturn == NO_ERROR,
296        "GetIpForwardTable(buf, &dwSize, FALSE) returned %d, expected NO_ERROR\n",
297        apiReturn);
298
299       if (apiReturn == NO_ERROR && winetest_debug > 1)
300       {
301           DWORD i;
302
303           trace( "IP forward table: %u entries\n", buf->dwNumEntries );
304           for (i = 0; i < buf->dwNumEntries; i++)
305           {
306               char buffer[40];
307               sprintf( buffer, "dest %s", ntoa( buf->table[i].dwForwardDest ));
308               sprintf( buffer + strlen(buffer), " mask %s", ntoa( buf->table[i].dwForwardMask ));
309               trace( "%u: %s gw %s if %u type %u\n", i, buffer,
310                      ntoa( buf->table[i].dwForwardNextHop ),
311                      buf->table[i].dwForwardIfIndex, U1(buf->table[i]).dwForwardType );
312           }
313       }
314       HeapFree(GetProcessHeap(), 0, buf);
315     }
316   }
317 }
318
319 static void testGetIpNetTable(void)
320 {
321   if (pGetIpNetTable) {
322     DWORD apiReturn;
323     ULONG dwSize = 0;
324
325     apiReturn = pGetIpNetTable(NULL, NULL, FALSE);
326     if (apiReturn == ERROR_NOT_SUPPORTED) {
327       skip("GetIpNetTable is not supported\n");
328       return;
329     }
330     ok(apiReturn == ERROR_INVALID_PARAMETER,
331      "GetIpNetTable(NULL, NULL, FALSE) returned %d, expected ERROR_INVALID_PARAMETER\n",
332      apiReturn);
333     apiReturn = pGetIpNetTable(NULL, &dwSize, FALSE);
334     ok(apiReturn == ERROR_NO_DATA || apiReturn == ERROR_INSUFFICIENT_BUFFER,
335      "GetIpNetTable(NULL, &dwSize, FALSE) returned %d, expected ERROR_NO_DATA or ERROR_INSUFFICIENT_BUFFER\n",
336      apiReturn);
337     if (apiReturn == ERROR_NO_DATA)
338       ; /* empty ARP table's okay */
339     else if (apiReturn == ERROR_INSUFFICIENT_BUFFER) {
340       PMIB_IPNETTABLE buf = HeapAlloc(GetProcessHeap(), 0, dwSize);
341
342       apiReturn = pGetIpNetTable(buf, &dwSize, FALSE);
343       ok(apiReturn == NO_ERROR ||
344          apiReturn == ERROR_NO_DATA, /* empty ARP table's okay */
345        "GetIpNetTable(buf, &dwSize, FALSE) returned %d, expected NO_ERROR\n",
346        apiReturn);
347
348       if (apiReturn == NO_ERROR && winetest_debug > 1)
349       {
350           DWORD i, j;
351
352           trace( "IP net table: %u entries\n", buf->dwNumEntries );
353           for (i = 0; i < buf->dwNumEntries; i++)
354           {
355               trace( "%u: idx %u type %u addr %s phys",
356                      i, buf->table[i].dwIndex, U(buf->table[i]).dwType, ntoa( buf->table[i].dwAddr ));
357               for (j = 0; j < buf->table[i].dwPhysAddrLen; j++)
358                   printf( " %02x", buf->table[i].bPhysAddr[j] );
359               printf( "\n" );
360           }
361       }
362       HeapFree(GetProcessHeap(), 0, buf);
363     }
364   }
365 }
366
367 static void testGetIcmpStatistics(void)
368 {
369   if (pGetIcmpStatistics) {
370     DWORD apiReturn;
371     MIB_ICMP stats;
372
373     /* Crashes on Vista */
374     if (0) {
375       apiReturn = pGetIcmpStatistics(NULL);
376       if (apiReturn == ERROR_NOT_SUPPORTED)
377         return;
378       ok(apiReturn == ERROR_INVALID_PARAMETER,
379        "GetIcmpStatistics(NULL) returned %d, expected ERROR_INVALID_PARAMETER\n",
380        apiReturn);
381     }
382
383     apiReturn = pGetIcmpStatistics(&stats);
384     if (apiReturn == ERROR_NOT_SUPPORTED)
385     {
386       skip("GetIcmpStatistics is not supported\n");
387       return;
388     }
389     ok(apiReturn == NO_ERROR,
390      "GetIcmpStatistics returned %d, expected NO_ERROR\n", apiReturn);
391     if (apiReturn == NO_ERROR && winetest_debug > 1)
392     {
393         trace( "ICMP stats:          %8s %8s\n", "in", "out" );
394         trace( "    dwMsgs:          %8u %8u\n", stats.stats.icmpInStats.dwMsgs, stats.stats.icmpOutStats.dwMsgs );
395         trace( "    dwErrors:        %8u %8u\n", stats.stats.icmpInStats.dwErrors, stats.stats.icmpOutStats.dwErrors );
396         trace( "    dwDestUnreachs:  %8u %8u\n", stats.stats.icmpInStats.dwDestUnreachs, stats.stats.icmpOutStats.dwDestUnreachs );
397         trace( "    dwTimeExcds:     %8u %8u\n", stats.stats.icmpInStats.dwTimeExcds, stats.stats.icmpOutStats.dwTimeExcds );
398         trace( "    dwParmProbs:     %8u %8u\n", stats.stats.icmpInStats.dwParmProbs, stats.stats.icmpOutStats.dwParmProbs );
399         trace( "    dwSrcQuenchs:    %8u %8u\n", stats.stats.icmpInStats.dwSrcQuenchs, stats.stats.icmpOutStats.dwSrcQuenchs );
400         trace( "    dwRedirects:     %8u %8u\n", stats.stats.icmpInStats.dwRedirects, stats.stats.icmpOutStats.dwRedirects );
401         trace( "    dwEchos:         %8u %8u\n", stats.stats.icmpInStats.dwEchos, stats.stats.icmpOutStats.dwEchos );
402         trace( "    dwEchoReps:      %8u %8u\n", stats.stats.icmpInStats.dwEchoReps, stats.stats.icmpOutStats.dwEchoReps );
403         trace( "    dwTimestamps:    %8u %8u\n", stats.stats.icmpInStats.dwTimestamps, stats.stats.icmpOutStats.dwTimestamps );
404         trace( "    dwTimestampReps: %8u %8u\n", stats.stats.icmpInStats.dwTimestampReps, stats.stats.icmpOutStats.dwTimestampReps );
405         trace( "    dwAddrMasks:     %8u %8u\n", stats.stats.icmpInStats.dwAddrMasks, stats.stats.icmpOutStats.dwAddrMasks );
406         trace( "    dwAddrMaskReps:  %8u %8u\n", stats.stats.icmpInStats.dwAddrMaskReps, stats.stats.icmpOutStats.dwAddrMaskReps );
407     }
408   }
409 }
410
411 static void testGetIpStatistics(void)
412 {
413   if (pGetIpStatistics) {
414     DWORD apiReturn;
415     MIB_IPSTATS stats;
416
417     apiReturn = pGetIpStatistics(NULL);
418     if (apiReturn == ERROR_NOT_SUPPORTED) {
419       skip("GetIpStatistics is not supported\n");
420       return;
421     }
422     ok(apiReturn == ERROR_INVALID_PARAMETER,
423      "GetIpStatistics(NULL) returned %d, expected ERROR_INVALID_PARAMETER\n",
424      apiReturn);
425     apiReturn = pGetIpStatistics(&stats);
426     ok(apiReturn == NO_ERROR,
427       "GetIpStatistics returned %d, expected NO_ERROR\n", apiReturn);
428     if (apiReturn == NO_ERROR && winetest_debug > 1)
429     {
430         trace( "IP stats:\n" );
431         trace( "    dwForwarding:      %u\n", U(stats).dwForwarding );
432         trace( "    dwDefaultTTL:      %u\n", stats.dwDefaultTTL );
433         trace( "    dwInReceives:      %u\n", stats.dwInReceives );
434         trace( "    dwInHdrErrors:     %u\n", stats.dwInHdrErrors );
435         trace( "    dwInAddrErrors:    %u\n", stats.dwInAddrErrors );
436         trace( "    dwForwDatagrams:   %u\n", stats.dwForwDatagrams );
437         trace( "    dwInUnknownProtos: %u\n", stats.dwInUnknownProtos );
438         trace( "    dwInDiscards:      %u\n", stats.dwInDiscards );
439         trace( "    dwInDelivers:      %u\n", stats.dwInDelivers );
440         trace( "    dwOutRequests:     %u\n", stats.dwOutRequests );
441         trace( "    dwRoutingDiscards: %u\n", stats.dwRoutingDiscards );
442         trace( "    dwOutDiscards:     %u\n", stats.dwOutDiscards );
443         trace( "    dwOutNoRoutes:     %u\n", stats.dwOutNoRoutes );
444         trace( "    dwReasmTimeout:    %u\n", stats.dwReasmTimeout );
445         trace( "    dwReasmReqds:      %u\n", stats.dwReasmReqds );
446         trace( "    dwReasmOks:        %u\n", stats.dwReasmOks );
447         trace( "    dwReasmFails:      %u\n", stats.dwReasmFails );
448         trace( "    dwFragOks:         %u\n", stats.dwFragOks );
449         trace( "    dwFragFails:       %u\n", stats.dwFragFails );
450         trace( "    dwFragCreates:     %u\n", stats.dwFragCreates );
451         trace( "    dwNumIf:           %u\n", stats.dwNumIf );
452         trace( "    dwNumAddr:         %u\n", stats.dwNumAddr );
453         trace( "    dwNumRoutes:       %u\n", stats.dwNumRoutes );
454     }
455   }
456 }
457
458 static void testGetTcpStatistics(void)
459 {
460   if (pGetTcpStatistics) {
461     DWORD apiReturn;
462     MIB_TCPSTATS stats;
463
464     apiReturn = pGetTcpStatistics(NULL);
465     if (apiReturn == ERROR_NOT_SUPPORTED) {
466       skip("GetTcpStatistics is not supported\n");
467       return;
468     }
469     ok(apiReturn == ERROR_INVALID_PARAMETER,
470      "GetTcpStatistics(NULL) returned %d, expected ERROR_INVALID_PARAMETER\n",
471      apiReturn);
472     apiReturn = pGetTcpStatistics(&stats);
473     ok(apiReturn == NO_ERROR,
474       "GetTcpStatistics returned %d, expected NO_ERROR\n", apiReturn);
475     if (apiReturn == NO_ERROR && winetest_debug > 1)
476     {
477         trace( "TCP stats:\n" );
478         trace( "    dwRtoAlgorithm: %u\n", U(stats).dwRtoAlgorithm );
479         trace( "    dwRtoMin:       %u\n", stats.dwRtoMin );
480         trace( "    dwRtoMax:       %u\n", stats.dwRtoMax );
481         trace( "    dwMaxConn:      %u\n", stats.dwMaxConn );
482         trace( "    dwActiveOpens:  %u\n", stats.dwActiveOpens );
483         trace( "    dwPassiveOpens: %u\n", stats.dwPassiveOpens );
484         trace( "    dwAttemptFails: %u\n", stats.dwAttemptFails );
485         trace( "    dwEstabResets:  %u\n", stats.dwEstabResets );
486         trace( "    dwCurrEstab:    %u\n", stats.dwCurrEstab );
487         trace( "    dwInSegs:       %u\n", stats.dwInSegs );
488         trace( "    dwOutSegs:      %u\n", stats.dwOutSegs );
489         trace( "    dwRetransSegs:  %u\n", stats.dwRetransSegs );
490         trace( "    dwInErrs:       %u\n", stats.dwInErrs );
491         trace( "    dwOutRsts:      %u\n", stats.dwOutRsts );
492         trace( "    dwNumConns:     %u\n", stats.dwNumConns );
493     }
494   }
495 }
496
497 static void testGetUdpStatistics(void)
498 {
499   if (pGetUdpStatistics) {
500     DWORD apiReturn;
501     MIB_UDPSTATS stats;
502
503     apiReturn = pGetUdpStatistics(NULL);
504     if (apiReturn == ERROR_NOT_SUPPORTED) {
505       skip("GetUdpStatistics is not supported\n");
506       return;
507     }
508     ok(apiReturn == ERROR_INVALID_PARAMETER,
509      "GetUdpStatistics(NULL) returned %d, expected ERROR_INVALID_PARAMETER\n",
510      apiReturn);
511     apiReturn = pGetUdpStatistics(&stats);
512     ok(apiReturn == NO_ERROR,
513      "GetUdpStatistics returned %d, expected NO_ERROR\n", apiReturn);
514     if (apiReturn == NO_ERROR && winetest_debug > 1)
515     {
516         trace( "UDP stats:\n" );
517         trace( "    dwInDatagrams:  %u\n", stats.dwInDatagrams );
518         trace( "    dwNoPorts:      %u\n", stats.dwNoPorts );
519         trace( "    dwInErrors:     %u\n", stats.dwInErrors );
520         trace( "    dwOutDatagrams: %u\n", stats.dwOutDatagrams );
521         trace( "    dwNumAddrs:     %u\n", stats.dwNumAddrs );
522     }
523   }
524 }
525
526 static void testGetIcmpStatisticsEx(void)
527 {
528     DWORD apiReturn;
529     MIB_ICMP_EX stats;
530
531     if (!pGetIcmpStatisticsEx)
532     {
533         skip( "GetIcmpStatisticsEx not available\n" );
534         return;
535     }
536
537     /* Crashes on Vista */
538     if (1) {
539         apiReturn = pGetIcmpStatisticsEx(NULL, AF_INET);
540         ok(apiReturn == ERROR_INVALID_PARAMETER,
541          "GetIcmpStatisticsEx(NULL, AF_INET) returned %d, expected ERROR_INVALID_PARAMETER\n", apiReturn);
542     }
543
544     apiReturn = pGetIcmpStatisticsEx(&stats, AF_INET);
545     ok(apiReturn == NO_ERROR, "GetIcmpStatisticsEx returned %d, expected NO_ERROR\n", apiReturn);
546     if (apiReturn == NO_ERROR && winetest_debug > 1)
547     {
548         INT i;
549         trace( "ICMP Ex stats:           %8s %8s\n", "in", "out" );
550         trace( "    dwMsgs:              %8u %8u\n", stats.icmpInStats.dwMsgs, stats.icmpOutStats.dwMsgs );
551         trace( "    dwErrors:            %8u %8u\n", stats.icmpInStats.dwErrors, stats.icmpOutStats.dwErrors );
552         for (i = 0; i < 256; i++)
553             trace( "    rgdwTypeCount[%3i]: %8u %8u\n", i, stats.icmpInStats.rgdwTypeCount[i], stats.icmpOutStats.rgdwTypeCount[i] );
554     }
555
556     apiReturn = pGetIcmpStatisticsEx(&stats, AF_INET6);
557     ok(apiReturn == NO_ERROR || broken(apiReturn == ERROR_NOT_SUPPORTED),
558        "GetIcmpStatisticsEx returned %d, expected NO_ERROR\n", apiReturn);
559     if (apiReturn == NO_ERROR && winetest_debug > 1)
560     {
561         INT i;
562         trace( "ICMP Ex stats:           %8s %8s\n", "in", "out" );
563         trace( "    dwMsgs:              %8u %8u\n", stats.icmpInStats.dwMsgs, stats.icmpOutStats.dwMsgs );
564         trace( "    dwErrors:            %8u %8u\n", stats.icmpInStats.dwErrors, stats.icmpOutStats.dwErrors );
565         for (i = 0; i < 256; i++)
566             trace( "    rgdwTypeCount[%3i]: %8u %8u\n", i, stats.icmpInStats.rgdwTypeCount[i], stats.icmpOutStats.rgdwTypeCount[i] );
567     }
568 }
569
570 static void testGetIpStatisticsEx(void)
571 {
572     DWORD apiReturn;
573     MIB_IPSTATS stats;
574
575     if (!pGetIpStatisticsEx)
576     {
577         win_skip( "GetIpStatisticsEx not available\n" );
578         return;
579     }
580
581     apiReturn = pGetIpStatisticsEx(NULL, AF_INET);
582     ok(apiReturn == ERROR_INVALID_PARAMETER,
583        "GetIpStatisticsEx(NULL, AF_INET) returned %d, expected ERROR_INVALID_PARAMETER\n", apiReturn);
584
585     apiReturn = pGetIpStatisticsEx(&stats, AF_BAN);
586     ok(apiReturn == ERROR_INVALID_PARAMETER,
587        "GetIpStatisticsEx(&stats, AF_BAN) returned %d, expected ERROR_INVALID_PARAMETER\n", apiReturn);
588
589     apiReturn = pGetIpStatisticsEx(&stats, AF_INET);
590     ok(apiReturn == NO_ERROR, "GetIpStatisticsEx returned %d, expected NO_ERROR\n", apiReturn);
591     if (apiReturn == NO_ERROR && winetest_debug > 1)
592     {
593         trace( "IP Ex stats:\n" );
594         trace( "    dwForwarding:      %u\n", U(stats).dwForwarding );
595         trace( "    dwDefaultTTL:      %u\n", stats.dwDefaultTTL );
596         trace( "    dwInReceives:      %u\n", stats.dwInReceives );
597         trace( "    dwInHdrErrors:     %u\n", stats.dwInHdrErrors );
598         trace( "    dwInAddrErrors:    %u\n", stats.dwInAddrErrors );
599         trace( "    dwForwDatagrams:   %u\n", stats.dwForwDatagrams );
600         trace( "    dwInUnknownProtos: %u\n", stats.dwInUnknownProtos );
601         trace( "    dwInDiscards:      %u\n", stats.dwInDiscards );
602         trace( "    dwInDelivers:      %u\n", stats.dwInDelivers );
603         trace( "    dwOutRequests:     %u\n", stats.dwOutRequests );
604         trace( "    dwRoutingDiscards: %u\n", stats.dwRoutingDiscards );
605         trace( "    dwOutDiscards:     %u\n", stats.dwOutDiscards );
606         trace( "    dwOutNoRoutes:     %u\n", stats.dwOutNoRoutes );
607         trace( "    dwReasmTimeout:    %u\n", stats.dwReasmTimeout );
608         trace( "    dwReasmReqds:      %u\n", stats.dwReasmReqds );
609         trace( "    dwReasmOks:        %u\n", stats.dwReasmOks );
610         trace( "    dwReasmFails:      %u\n", stats.dwReasmFails );
611         trace( "    dwFragOks:         %u\n", stats.dwFragOks );
612         trace( "    dwFragFails:       %u\n", stats.dwFragFails );
613         trace( "    dwFragCreates:     %u\n", stats.dwFragCreates );
614         trace( "    dwNumIf:           %u\n", stats.dwNumIf );
615         trace( "    dwNumAddr:         %u\n", stats.dwNumAddr );
616         trace( "    dwNumRoutes:       %u\n", stats.dwNumRoutes );
617     }
618
619     apiReturn = pGetIpStatisticsEx(&stats, AF_INET6);
620     ok(apiReturn == NO_ERROR || broken(apiReturn == ERROR_NOT_SUPPORTED),
621        "GetIpStatisticsEx returned %d, expected NO_ERROR\n", apiReturn);
622     if (apiReturn == NO_ERROR && winetest_debug > 1)
623     {
624         trace( "IP Ex stats:\n" );
625         trace( "    dwForwarding:      %u\n", U(stats).dwForwarding );
626         trace( "    dwDefaultTTL:      %u\n", stats.dwDefaultTTL );
627         trace( "    dwInReceives:      %u\n", stats.dwInReceives );
628         trace( "    dwInHdrErrors:     %u\n", stats.dwInHdrErrors );
629         trace( "    dwInAddrErrors:    %u\n", stats.dwInAddrErrors );
630         trace( "    dwForwDatagrams:   %u\n", stats.dwForwDatagrams );
631         trace( "    dwInUnknownProtos: %u\n", stats.dwInUnknownProtos );
632         trace( "    dwInDiscards:      %u\n", stats.dwInDiscards );
633         trace( "    dwInDelivers:      %u\n", stats.dwInDelivers );
634         trace( "    dwOutRequests:     %u\n", stats.dwOutRequests );
635         trace( "    dwRoutingDiscards: %u\n", stats.dwRoutingDiscards );
636         trace( "    dwOutDiscards:     %u\n", stats.dwOutDiscards );
637         trace( "    dwOutNoRoutes:     %u\n", stats.dwOutNoRoutes );
638         trace( "    dwReasmTimeout:    %u\n", stats.dwReasmTimeout );
639         trace( "    dwReasmReqds:      %u\n", stats.dwReasmReqds );
640         trace( "    dwReasmOks:        %u\n", stats.dwReasmOks );
641         trace( "    dwReasmFails:      %u\n", stats.dwReasmFails );
642         trace( "    dwFragOks:         %u\n", stats.dwFragOks );
643         trace( "    dwFragFails:       %u\n", stats.dwFragFails );
644         trace( "    dwFragCreates:     %u\n", stats.dwFragCreates );
645         trace( "    dwNumIf:           %u\n", stats.dwNumIf );
646         trace( "    dwNumAddr:         %u\n", stats.dwNumAddr );
647         trace( "    dwNumRoutes:       %u\n", stats.dwNumRoutes );
648     }
649 }
650
651 static void testGetTcpStatisticsEx(void)
652 {
653     DWORD apiReturn;
654     MIB_TCPSTATS stats;
655
656     if (!pGetTcpStatisticsEx)
657     {
658         skip( "GetTcpStatisticsEx not available\n" );
659         return;
660     }
661
662     apiReturn = pGetTcpStatisticsEx(NULL, AF_INET);
663     ok(apiReturn == ERROR_INVALID_PARAMETER,
664        "GetTcpStatisticsEx(NULL, AF_INET); returned %d, expected ERROR_INVALID_PARAMETER\n", apiReturn);
665
666     apiReturn = pGetTcpStatisticsEx(&stats, AF_INET);
667     ok(apiReturn == NO_ERROR, "GetTcpStatisticsEx returned %d, expected NO_ERROR\n", apiReturn);
668     if (apiReturn == NO_ERROR && winetest_debug > 1)
669     {
670         trace( "TCP stats:\n" );
671         trace( "    dwRtoAlgorithm: %u\n", U(stats).dwRtoAlgorithm );
672         trace( "    dwRtoMin:       %u\n", stats.dwRtoMin );
673         trace( "    dwRtoMax:       %u\n", stats.dwRtoMax );
674         trace( "    dwMaxConn:      %u\n", stats.dwMaxConn );
675         trace( "    dwActiveOpens:  %u\n", stats.dwActiveOpens );
676         trace( "    dwPassiveOpens: %u\n", stats.dwPassiveOpens );
677         trace( "    dwAttemptFails: %u\n", stats.dwAttemptFails );
678         trace( "    dwEstabResets:  %u\n", stats.dwEstabResets );
679         trace( "    dwCurrEstab:    %u\n", stats.dwCurrEstab );
680         trace( "    dwInSegs:       %u\n", stats.dwInSegs );
681         trace( "    dwOutSegs:      %u\n", stats.dwOutSegs );
682         trace( "    dwRetransSegs:  %u\n", stats.dwRetransSegs );
683         trace( "    dwInErrs:       %u\n", stats.dwInErrs );
684         trace( "    dwOutRsts:      %u\n", stats.dwOutRsts );
685         trace( "    dwNumConns:     %u\n", stats.dwNumConns );
686     }
687
688     apiReturn = pGetTcpStatisticsEx(&stats, AF_INET6);
689     ok(apiReturn == NO_ERROR || broken(apiReturn == ERROR_NOT_SUPPORTED),
690        "GetTcpStatisticsEx returned %d, expected NO_ERROR\n", apiReturn);
691     if (apiReturn == NO_ERROR && winetest_debug > 1)
692     {
693         trace( "TCP stats:\n" );
694         trace( "    dwRtoAlgorithm: %u\n", U(stats).dwRtoAlgorithm );
695         trace( "    dwRtoMin:       %u\n", stats.dwRtoMin );
696         trace( "    dwRtoMax:       %u\n", stats.dwRtoMax );
697         trace( "    dwMaxConn:      %u\n", stats.dwMaxConn );
698         trace( "    dwActiveOpens:  %u\n", stats.dwActiveOpens );
699         trace( "    dwPassiveOpens: %u\n", stats.dwPassiveOpens );
700         trace( "    dwAttemptFails: %u\n", stats.dwAttemptFails );
701         trace( "    dwEstabResets:  %u\n", stats.dwEstabResets );
702         trace( "    dwCurrEstab:    %u\n", stats.dwCurrEstab );
703         trace( "    dwInSegs:       %u\n", stats.dwInSegs );
704         trace( "    dwOutSegs:      %u\n", stats.dwOutSegs );
705         trace( "    dwRetransSegs:  %u\n", stats.dwRetransSegs );
706         trace( "    dwInErrs:       %u\n", stats.dwInErrs );
707         trace( "    dwOutRsts:      %u\n", stats.dwOutRsts );
708         trace( "    dwNumConns:     %u\n", stats.dwNumConns );
709     }
710 }
711
712 static void testGetUdpStatisticsEx(void)
713 {
714     DWORD apiReturn;
715     MIB_UDPSTATS stats;
716
717     if (!pGetUdpStatisticsEx)
718     {
719         skip( "GetUdpStatisticsEx not available\n" );
720         return;
721     }
722
723     apiReturn = pGetUdpStatisticsEx(NULL, AF_INET);
724     ok(apiReturn == ERROR_INVALID_PARAMETER,
725        "GetUdpStatisticsEx(NULL, AF_INET); returned %d, expected ERROR_INVALID_PARAMETER\n", apiReturn);
726
727     apiReturn = pGetUdpStatisticsEx(&stats, AF_INET);
728     ok(apiReturn == NO_ERROR, "GetUdpStatisticsEx returned %d, expected NO_ERROR\n", apiReturn);
729     if (apiReturn == NO_ERROR && winetest_debug > 1)
730     {
731         trace( "UDP stats:\n" );
732         trace( "    dwInDatagrams:  %u\n", stats.dwInDatagrams );
733         trace( "    dwNoPorts:      %u\n", stats.dwNoPorts );
734         trace( "    dwInErrors:     %u\n", stats.dwInErrors );
735         trace( "    dwOutDatagrams: %u\n", stats.dwOutDatagrams );
736         trace( "    dwNumAddrs:     %u\n", stats.dwNumAddrs );
737     }
738
739     apiReturn = pGetUdpStatisticsEx(&stats, AF_INET6);
740     ok(apiReturn == NO_ERROR || broken(apiReturn == ERROR_NOT_SUPPORTED),
741        "GetUdpStatisticsEx returned %d, expected NO_ERROR\n", apiReturn);
742     if (apiReturn == NO_ERROR && winetest_debug > 1)
743     {
744         trace( "UDP stats:\n" );
745         trace( "    dwInDatagrams:  %u\n", stats.dwInDatagrams );
746         trace( "    dwNoPorts:      %u\n", stats.dwNoPorts );
747         trace( "    dwInErrors:     %u\n", stats.dwInErrors );
748         trace( "    dwOutDatagrams: %u\n", stats.dwOutDatagrams );
749         trace( "    dwNumAddrs:     %u\n", stats.dwNumAddrs );
750     }
751 }
752
753 static void testGetTcpTable(void)
754 {
755   if (pGetTcpTable) {
756     DWORD apiReturn;
757     ULONG dwSize = 0;
758
759     apiReturn = pGetTcpTable(NULL, &dwSize, FALSE);
760     if (apiReturn == ERROR_NOT_SUPPORTED) {
761       skip("GetTcpTable is not supported\n");
762       return;
763     }
764     ok(apiReturn == ERROR_INSUFFICIENT_BUFFER ||
765        broken(apiReturn == ERROR_NO_DATA), /* win95 */
766      "GetTcpTable(NULL, &dwSize, FALSE) returned %d, expected ERROR_INSUFFICIENT_BUFFER\n",
767      apiReturn);
768     if (apiReturn == ERROR_INSUFFICIENT_BUFFER) {
769       PMIB_TCPTABLE buf = HeapAlloc(GetProcessHeap(), 0, dwSize);
770
771       apiReturn = pGetTcpTable(buf, &dwSize, FALSE);
772       ok(apiReturn == NO_ERROR,
773        "GetTcpTable(buf, &dwSize, FALSE) returned %d, expected NO_ERROR\n",
774        apiReturn);
775
776       if (apiReturn == NO_ERROR && winetest_debug > 1)
777       {
778           DWORD i;
779           trace( "TCP table: %u entries\n", buf->dwNumEntries );
780           for (i = 0; i < buf->dwNumEntries; i++)
781           {
782               char buffer[40];
783               sprintf( buffer, "local %s:%u",
784                        ntoa(buf->table[i].dwLocalAddr), ntohs(buf->table[i].dwLocalPort) );
785               trace( "%u: %s remote %s:%u state %u\n",
786                      i, buffer, ntoa( buf->table[i].dwRemoteAddr ),
787                      ntohs(buf->table[i].dwRemotePort), U(buf->table[i]).dwState );
788           }
789       }
790       HeapFree(GetProcessHeap(), 0, buf);
791     }
792   }
793 }
794
795 static void testGetUdpTable(void)
796 {
797   if (pGetUdpTable) {
798     DWORD apiReturn;
799     ULONG dwSize = 0;
800
801     apiReturn = pGetUdpTable(NULL, &dwSize, FALSE);
802     if (apiReturn == ERROR_NOT_SUPPORTED) {
803       skip("GetUdpTable is not supported\n");
804       return;
805     }
806     ok(apiReturn == ERROR_INSUFFICIENT_BUFFER,
807      "GetUdpTable(NULL, &dwSize, FALSE) returned %d, expected ERROR_INSUFFICIENT_BUFFER\n",
808      apiReturn);
809     if (apiReturn == ERROR_INSUFFICIENT_BUFFER) {
810       PMIB_UDPTABLE buf = HeapAlloc(GetProcessHeap(), 0, dwSize);
811
812       apiReturn = pGetUdpTable(buf, &dwSize, FALSE);
813       ok(apiReturn == NO_ERROR,
814        "GetUdpTable(buf, &dwSize, FALSE) returned %d, expected NO_ERROR\n",
815        apiReturn);
816
817       if (apiReturn == NO_ERROR && winetest_debug > 1)
818       {
819           DWORD i;
820           trace( "UDP table: %u entries\n", buf->dwNumEntries );
821           for (i = 0; i < buf->dwNumEntries; i++)
822               trace( "%u: %s:%u\n",
823                      i, ntoa( buf->table[i].dwLocalAddr ), ntohs(buf->table[i].dwLocalPort) );
824       }
825       HeapFree(GetProcessHeap(), 0, buf);
826     }
827   }
828 }
829
830 static void testSetTcpEntry(void)
831 {
832     DWORD ret;
833     MIB_TCPROW row;
834
835     memset(&row, 0, sizeof(row));
836     if(0) /* This test crashes in OS >= VISTA */
837     {
838         ret = pSetTcpEntry(NULL);
839         ok( ret == ERROR_INVALID_PARAMETER, "got %u, expected %u\n", ret, ERROR_INVALID_PARAMETER);
840     }
841
842     ret = pSetTcpEntry(&row);
843     todo_wine ok( ret == ERROR_INVALID_PARAMETER, "got %u, expected %u\n", ret, ERROR_INVALID_PARAMETER);
844
845     U(row).dwState = MIB_TCP_STATE_DELETE_TCB;
846     ret = pSetTcpEntry(&row);
847     todo_wine ok( ret == ERROR_MR_MID_NOT_FOUND || broken(ret == ERROR_INVALID_PARAMETER),
848        "got %u, expected %u\n", ret, ERROR_MR_MID_NOT_FOUND);
849 }
850
851 /*
852 still-to-be-tested NT4-onward functions:
853 CreateIpForwardEntry
854 DeleteIpForwardEntry
855 CreateIpNetEntry
856 DeleteIpNetEntry
857 GetFriendlyIfIndex
858 GetRTTAndHopCount
859 SetIfEntry
860 SetIpForwardEntry
861 SetIpNetEntry
862 SetIpStatistics
863 SetIpTTL
864 */
865 static void testWinNT4Functions(void)
866 {
867   testGetNumberOfInterfaces();
868   testGetIpAddrTable();
869   testGetIfTable();
870   testGetIpForwardTable();
871   testGetIpNetTable();
872   testGetIcmpStatistics();
873   testGetIpStatistics();
874   testGetTcpStatistics();
875   testGetUdpStatistics();
876   testGetIcmpStatisticsEx();
877   testGetIpStatisticsEx();
878   testGetTcpStatisticsEx();
879   testGetUdpStatisticsEx();
880   testGetTcpTable();
881   testGetUdpTable();
882   testSetTcpEntry();
883 }
884
885 static void testGetInterfaceInfo(void)
886 {
887   if (pGetInterfaceInfo) {
888     DWORD apiReturn;
889     ULONG len = 0;
890
891     apiReturn = pGetInterfaceInfo(NULL, NULL);
892     if (apiReturn == ERROR_NOT_SUPPORTED) {
893       skip("GetInterfaceInfo is not supported\n");
894       return;
895     }
896     ok(apiReturn == ERROR_INVALID_PARAMETER,
897      "GetInterfaceInfo returned %d, expected ERROR_INVALID_PARAMETER\n",
898      apiReturn);
899     apiReturn = pGetInterfaceInfo(NULL, &len);
900     ok(apiReturn == ERROR_INSUFFICIENT_BUFFER,
901      "GetInterfaceInfo returned %d, expected ERROR_INSUFFICIENT_BUFFER\n",
902      apiReturn);
903     if (apiReturn == ERROR_INSUFFICIENT_BUFFER) {
904       PIP_INTERFACE_INFO buf = HeapAlloc(GetProcessHeap(), 0, len);
905
906       apiReturn = pGetInterfaceInfo(buf, &len);
907       ok(apiReturn == NO_ERROR,
908        "GetInterfaceInfo(buf, &dwSize) returned %d, expected NO_ERROR\n",
909        apiReturn);
910       HeapFree(GetProcessHeap(), 0, buf);
911     }
912   }
913 }
914
915 static void testGetAdaptersInfo(void)
916 {
917   if (pGetAdaptersInfo) {
918     DWORD apiReturn;
919     ULONG len = 0;
920
921     apiReturn = pGetAdaptersInfo(NULL, NULL);
922     if (apiReturn == ERROR_NOT_SUPPORTED) {
923       skip("GetAdaptersInfo is not supported\n");
924       return;
925     }
926     ok(apiReturn == ERROR_INVALID_PARAMETER,
927      "GetAdaptersInfo returned %d, expected ERROR_INVALID_PARAMETER\n",
928      apiReturn);
929     apiReturn = pGetAdaptersInfo(NULL, &len);
930     ok(apiReturn == ERROR_NO_DATA || apiReturn == ERROR_BUFFER_OVERFLOW,
931      "GetAdaptersInfo returned %d, expected ERROR_NO_DATA or ERROR_BUFFER_OVERFLOW\n",
932      apiReturn);
933     if (apiReturn == ERROR_NO_DATA)
934       ; /* no adapter's, that's okay */
935     else if (apiReturn == ERROR_BUFFER_OVERFLOW) {
936       PIP_ADAPTER_INFO buf = HeapAlloc(GetProcessHeap(), 0, len);
937
938       apiReturn = pGetAdaptersInfo(buf, &len);
939       ok(apiReturn == NO_ERROR,
940        "GetAdaptersInfo(buf, &dwSize) returned %d, expected NO_ERROR\n",
941        apiReturn);
942       HeapFree(GetProcessHeap(), 0, buf);
943     }
944   }
945 }
946
947 static void testGetNetworkParams(void)
948 {
949   if (pGetNetworkParams) {
950     DWORD apiReturn;
951     ULONG len = 0;
952
953     apiReturn = pGetNetworkParams(NULL, NULL);
954     if (apiReturn == ERROR_NOT_SUPPORTED) {
955       skip("GetNetworkParams is not supported\n");
956       return;
957     }
958     ok(apiReturn == ERROR_INVALID_PARAMETER,
959      "GetNetworkParams returned %d, expected ERROR_INVALID_PARAMETER\n",
960      apiReturn);
961     apiReturn = pGetNetworkParams(NULL, &len);
962     ok(apiReturn == ERROR_BUFFER_OVERFLOW,
963      "GetNetworkParams returned %d, expected ERROR_BUFFER_OVERFLOW\n",
964      apiReturn);
965     if (apiReturn == ERROR_BUFFER_OVERFLOW) {
966       PFIXED_INFO buf = HeapAlloc(GetProcessHeap(), 0, len);
967
968       apiReturn = pGetNetworkParams(buf, &len);
969       ok(apiReturn == NO_ERROR,
970        "GetNetworkParams(buf, &dwSize) returned %d, expected NO_ERROR\n",
971        apiReturn);
972       HeapFree(GetProcessHeap(), 0, buf);
973     }
974   }
975 }
976
977 /*
978 still-to-be-tested 98-onward functions:
979 GetBestInterface
980 GetBestRoute
981 IpReleaseAddress
982 IpRenewAddress
983 */
984 static DWORD CALLBACK testWin98Functions(void *p)
985 {
986   testGetInterfaceInfo();
987   testGetAdaptersInfo();
988   testGetNetworkParams();
989   return 0;
990 }
991
992 static void testGetPerAdapterInfo(void)
993 {
994     DWORD ret, needed;
995     void *buffer;
996
997     if (!pGetPerAdapterInfo) return;
998     ret = pGetPerAdapterInfo(1, NULL, NULL);
999     if (ret == ERROR_NOT_SUPPORTED) {
1000       skip("GetPerAdapterInfo is not supported\n");
1001       return;
1002     }
1003     ok( ret == ERROR_INVALID_PARAMETER, "got %u instead of ERROR_INVALID_PARAMETER\n", ret );
1004     needed = 0xdeadbeef;
1005     ret = pGetPerAdapterInfo(1, NULL, &needed);
1006     if (ret == ERROR_NO_DATA) return;  /* no such adapter */
1007     ok( ret == ERROR_BUFFER_OVERFLOW, "got %u instead of ERROR_BUFFER_OVERFLOW\n", ret );
1008     ok( needed != 0xdeadbeef, "needed not set\n" );
1009     buffer = HeapAlloc( GetProcessHeap(), 0, needed );
1010     ret = pGetPerAdapterInfo(1, buffer, &needed);
1011     ok( ret == NO_ERROR, "got %u instead of NO_ERROR\n", ret );
1012     HeapFree( GetProcessHeap(), 0, buffer );
1013 }
1014
1015 static void testNotifyAddrChange(void)
1016 {
1017     DWORD ret, bytes;
1018     OVERLAPPED overlapped;
1019     HANDLE handle;
1020     BOOL success;
1021
1022     if (!pNotifyAddrChange)
1023     {
1024         win_skip("NotifyAddrChange not present\n");
1025         return;
1026     }
1027     if (!pCancelIPChangeNotify)
1028     {
1029         win_skip("CancelIPChangeNotify not present\n");
1030         return;
1031     }
1032
1033     handle = NULL;
1034     ZeroMemory(&overlapped, sizeof(overlapped));
1035     ret = pNotifyAddrChange(&handle, &overlapped);
1036     if (ret == ERROR_NOT_SUPPORTED)
1037     {
1038         win_skip("NotifyAddrChange is not supported\n");
1039         return;
1040     }
1041     ok(ret == ERROR_IO_PENDING, "NotifyAddrChange returned %d, expected ERROR_IO_PENDING\n", ret);
1042     ret = GetLastError();
1043     todo_wine ok(ret == ERROR_IO_PENDING, "GetLastError returned %d, expected ERROR_IO_PENDING\n", ret);
1044     success = pCancelIPChangeNotify(&overlapped);
1045     todo_wine ok(success == TRUE, "CancelIPChangeNotify returned FALSE, expected TRUE\n");
1046
1047     ZeroMemory(&overlapped, sizeof(overlapped));
1048     success = pCancelIPChangeNotify(&overlapped);
1049     ok(success == FALSE, "CancelIPChangeNotify returned TRUE, expected FALSE\n");
1050
1051     handle = NULL;
1052     ZeroMemory(&overlapped, sizeof(overlapped));
1053     overlapped.hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
1054     ret = pNotifyAddrChange(&handle, &overlapped);
1055     ok(ret == ERROR_IO_PENDING, "NotifyAddrChange returned %d, expected ERROR_IO_PENDING\n", ret);
1056     todo_wine ok(handle != INVALID_HANDLE_VALUE, "NotifyAddrChange returned invalid file handle\n");
1057     success = GetOverlappedResult(handle, &overlapped, &bytes, FALSE);
1058     ok(success == FALSE, "GetOverlappedResult returned TRUE, expected FALSE\n");
1059     ret = GetLastError();
1060     ok(ret == ERROR_IO_INCOMPLETE, "GetLastError returned %d, expected ERROR_IO_INCOMPLETE\n", ret);
1061     success = pCancelIPChangeNotify(&overlapped);
1062     todo_wine ok(success == TRUE, "CancelIPChangeNotify returned FALSE, expected TRUE\n");
1063
1064     if (winetest_interactive)
1065     {
1066         handle = NULL;
1067         ZeroMemory(&overlapped, sizeof(overlapped));
1068         overlapped.hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
1069         trace("Testing asynchronous ipv4 address change notification. Please "
1070               "change the ipv4 address of one of your network interfaces\n");
1071         ret = pNotifyAddrChange(&handle, &overlapped);
1072         ok(ret == ERROR_IO_PENDING, "NotifyAddrChange returned %d, expected NO_ERROR\n", ret);
1073         success = GetOverlappedResult(handle, &overlapped, &bytes, TRUE);
1074         ok(success == TRUE, "GetOverlappedResult returned FALSE, expected TRUE\n");
1075     }
1076
1077     /* test synchronous functionality */
1078     if (winetest_interactive)
1079     {
1080         trace("Testing synchronous ipv4 address change notification. Please "
1081               "change the ipv4 address of one of your network interfaces\n");
1082         ret = pNotifyAddrChange(NULL, NULL);
1083         todo_wine ok(ret == NO_ERROR, "NotifyAddrChange returned %d, expected NO_ERROR\n", ret);
1084     }
1085 }
1086
1087 /*
1088 still-to-be-tested 2K-onward functions:
1089 AddIPAddress
1090 CreateProxyArpEntry
1091 DeleteIPAddress
1092 DeleteProxyArpEntry
1093 EnableRouter
1094 FlushIpNetTable
1095 GetAdapterIndex
1096 NotifyRouteChange + CancelIPChangeNotify
1097 SendARP
1098 UnenableRouter
1099 */
1100 static void testWin2KFunctions(void)
1101 {
1102     testGetPerAdapterInfo();
1103     testNotifyAddrChange();
1104 }
1105
1106 static void test_GetAdaptersAddresses(void)
1107 {
1108     ULONG ret, size;
1109     IP_ADAPTER_ADDRESSES *aa, *ptr;
1110     IP_ADAPTER_UNICAST_ADDRESS *ua;
1111
1112     if (!pGetAdaptersAddresses)
1113     {
1114         win_skip("GetAdaptersAddresses not present\n");
1115         return;
1116     }
1117
1118     ret = pGetAdaptersAddresses(AF_UNSPEC, 0, NULL, NULL, NULL);
1119     ok(ret == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER got %u\n", ret);
1120
1121     /* size should be ignored and overwritten if buffer is NULL */
1122     size = 0x7fffffff;
1123     ret = pGetAdaptersAddresses(AF_UNSPEC, 0, NULL, NULL, &size);
1124     ok(ret == ERROR_BUFFER_OVERFLOW, "expected ERROR_BUFFER_OVERFLOW, got %u\n", ret);
1125     if (ret != ERROR_BUFFER_OVERFLOW) return;
1126
1127     ptr = HeapAlloc(GetProcessHeap(), 0, size);
1128     ret = pGetAdaptersAddresses(AF_UNSPEC, 0, NULL, ptr, &size);
1129     ok(!ret, "expected ERROR_SUCCESS got %u\n", ret);
1130
1131     for (aa = ptr; !ret && aa; aa = aa->Next)
1132     {
1133         ok(aa->DnsSuffix != NULL, "DnsSuffix is not a valid pointer\n");
1134         ok(aa->Description != NULL, "Description is not a valid pointer\n");
1135         ok(aa->FriendlyName != NULL, "FriendlyName is not a valid pointer\n");
1136
1137         if (winetest_debug <= 1)
1138             continue;
1139
1140         trace("Length:                %u\n", S(U(*aa)).Length);
1141         trace("IfIndex:               %u\n", S(U(*aa)).IfIndex);
1142         trace("Next:                  %p\n", aa->Next);
1143         trace("AdapterName:           %s\n", aa->AdapterName);
1144         trace("FirstUnicastAddress:   %p\n", aa->FirstUnicastAddress);
1145         ua = aa->FirstUnicastAddress;
1146         while (ua)
1147         {
1148             trace("\tLength:                  %u\n", S(U(*ua)).Length);
1149             trace("\tFlags:                   0x%08x\n", S(U(*ua)).Flags);
1150             trace("\tNext:                    %p\n", ua->Next);
1151             trace("\tAddress.lpSockaddr:      %p\n", ua->Address.lpSockaddr);
1152             trace("\tAddress.iSockaddrLength: %d\n", ua->Address.iSockaddrLength);
1153             trace("\tPrefixOrigin:            %u\n", ua->PrefixOrigin);
1154             trace("\tSuffixOrigin:            %u\n", ua->SuffixOrigin);
1155             trace("\tDadState:                %u\n", ua->DadState);
1156             trace("\tValidLifetime:           0x%08x\n", ua->ValidLifetime);
1157             trace("\tPreferredLifetime:       0x%08x\n", ua->PreferredLifetime);
1158             trace("\tLeaseLifetime:           0x%08x\n", ua->LeaseLifetime);
1159             trace("\n");
1160             ua = ua->Next;
1161         }
1162         trace("FirstAnycastAddress:   %p\n", aa->FirstAnycastAddress);
1163         trace("FirstMulticastAddress: %p\n", aa->FirstMulticastAddress);
1164         trace("FirstDnsServerAddress: %p\n", aa->FirstDnsServerAddress);
1165         trace("DnsSuffix:             %p\n", aa->DnsSuffix);
1166         trace("Description:           %p\n", aa->Description);
1167         trace("FriendlyName:          %p\n", aa->FriendlyName);
1168         trace("PhysicalAddress:       %02x\n", aa->PhysicalAddress[0]);
1169         trace("PhysicalAddressLength: %u\n", aa->PhysicalAddressLength);
1170         trace("Flags:                 0x%08x\n", aa->Flags);
1171         trace("Mtu:                   %u\n", aa->Mtu);
1172         trace("IfType:                %u\n", aa->IfType);
1173         trace("OperStatus:            %u\n", aa->OperStatus);
1174         trace("\n");
1175     }
1176     HeapFree(GetProcessHeap(), 0, ptr);
1177 }
1178
1179 static void test_GetExtendedTcpTable(void)
1180 {
1181     DWORD ret, size;
1182     MIB_TCPTABLE *table;
1183     MIB_TCPTABLE_OWNER_PID *table_pid;
1184
1185     if (!pGetExtendedTcpTable)
1186     {
1187         win_skip("GetExtendedTcpTable not available\n");
1188         return;
1189     }
1190     ret = pGetExtendedTcpTable( NULL, NULL, TRUE, AF_INET, TCP_TABLE_BASIC_ALL, 0 );
1191     ok( ret == ERROR_INVALID_PARAMETER, "got %u\n", ret );
1192
1193     size = 0;
1194     ret = pGetExtendedTcpTable( NULL, &size, TRUE, AF_INET, TCP_TABLE_BASIC_ALL, 0 );
1195     ok( ret == ERROR_INSUFFICIENT_BUFFER, "got %u\n", ret );
1196
1197     table = HeapAlloc( GetProcessHeap(), 0, size );
1198     ret = pGetExtendedTcpTable( table, &size, TRUE, AF_INET, TCP_TABLE_BASIC_ALL, 0 );
1199     ok( ret == ERROR_SUCCESS, "got %u\n", ret );
1200     HeapFree( GetProcessHeap(), 0, table );
1201
1202     size = 0;
1203     ret = pGetExtendedTcpTable( NULL, &size, TRUE, AF_INET, TCP_TABLE_OWNER_PID_ALL, 0 );
1204     ok( ret == ERROR_INSUFFICIENT_BUFFER, "got %u\n", ret );
1205
1206     table_pid = HeapAlloc( GetProcessHeap(), 0, size );
1207     ret = pGetExtendedTcpTable( table_pid, &size, TRUE, AF_INET, TCP_TABLE_OWNER_PID_ALL, 0 );
1208     ok( ret == ERROR_SUCCESS, "got %u\n", ret );
1209     HeapFree( GetProcessHeap(), 0, table_pid );
1210 }
1211
1212 static void test_GetExtendedUdpTable(void)
1213 {
1214     DWORD ret, size;
1215     MIB_UDPTABLE *table;
1216     MIB_UDPTABLE_OWNER_PID *table_pid;
1217
1218     if (!pGetExtendedUdpTable)
1219     {
1220         win_skip("GetExtendedUdpTable not available\n");
1221         return;
1222     }
1223     ret = pGetExtendedUdpTable( NULL, NULL, TRUE, AF_INET, UDP_TABLE_BASIC, 0 );
1224     ok( ret == ERROR_INVALID_PARAMETER, "got %u\n", ret );
1225
1226     size = 0;
1227     ret = pGetExtendedUdpTable( NULL, &size, TRUE, AF_INET, UDP_TABLE_BASIC, 0 );
1228     ok( ret == ERROR_INSUFFICIENT_BUFFER, "got %u\n", ret );
1229
1230     table = HeapAlloc( GetProcessHeap(), 0, size );
1231     ret = pGetExtendedUdpTable( table, &size, TRUE, AF_INET, UDP_TABLE_BASIC, 0 );
1232     ok( ret == ERROR_SUCCESS, "got %u\n", ret );
1233     HeapFree( GetProcessHeap(), 0, table );
1234
1235     size = 0;
1236     ret = pGetExtendedUdpTable( NULL, &size, TRUE, AF_INET, UDP_TABLE_OWNER_PID, 0 );
1237     ok( ret == ERROR_INSUFFICIENT_BUFFER, "got %u\n", ret );
1238
1239     table_pid = HeapAlloc( GetProcessHeap(), 0, size );
1240     ret = pGetExtendedUdpTable( table_pid, &size, TRUE, AF_INET, UDP_TABLE_OWNER_PID, 0 );
1241     ok( ret == ERROR_SUCCESS, "got %u\n", ret );
1242     HeapFree( GetProcessHeap(), 0, table_pid );
1243 }
1244
1245 START_TEST(iphlpapi)
1246 {
1247
1248   loadIPHlpApi();
1249   if (hLibrary) {
1250     HANDLE thread;
1251
1252     testWin98OnlyFunctions();
1253     testWinNT4Functions();
1254
1255     /* run testGetXXXX in two threads at once to make sure we don't crash in that case */
1256     thread = CreateThread(NULL, 0, testWin98Functions, NULL, 0, NULL);
1257     testWin98Functions(NULL);
1258     WaitForSingleObject(thread, INFINITE);
1259
1260     testWin2KFunctions();
1261     test_GetAdaptersAddresses();
1262     test_GetExtendedTcpTable();
1263     test_GetExtendedUdpTable();
1264     freeIPHlpApi();
1265   }
1266 }