Skip to content

Commit fa9dfdb

Browse files
Merge pull request #1178 from dbwiddis/tcpudp
Add GetTcpStatistics and GetUdpStatistics
2 parents 6c4e8c9 + e2b8986 commit fa9dfdb

File tree

3 files changed

+185
-43
lines changed

3 files changed

+185
-43
lines changed

CHANGES.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ Next Release (5.6.0)
88
Features
99
--------
1010
* [#1160](https://github.com/java-native-access/jna/issues/1160): Make FileUtils#moveToTrash a varargs method - [@matthiasblaesing](https://github.com/matthiasblaesing).
11-
* [#1167](https://github.com/java-native-access/jna/pull/1167): Add `c.s.j.p.win32.Kernel32.GetProcessAffinityMask` - [@dbwiddis](https://github.com/dbwiddis).
12-
* [#1168](https://github.com/java-native-access/jna/pull/1168): Add `c.s.j.p.win32.Kernel32.SetProcessAffinityMask` - [@dbwiddis](https://github.com/dbwiddis).
11+
* [#1167](https://github.com/java-native-access/jna/pull/1167): Add `c.s.j.p.win32.Kernel32#GetProcessAffinityMask` - [@dbwiddis](https://github.com/dbwiddis).
12+
* [#1168](https://github.com/java-native-access/jna/pull/1168): Add `c.s.j.p.win32.Kernel32#SetProcessAffinityMask` - [@dbwiddis](https://github.com/dbwiddis).
1313
* [#1169](https://github.com/java-native-access/jna/issues/1169): Wait for process in getLinuxLdPaths - [@rdesgroppes](https://github.com/rdesgroppes).
14+
* [#1178](https://github.com/java-native-access/jna/pull/1178): Add `c.s.j.p.win32.IPHlpAPI#GetTcpStatistics`, `c.s.j.p.win32.IPHlpAPI#GetUdpStatistics`, `c.s.j.p.win32.IPHlpAPI#GetTcpStatisticsEx` and `c.s.j.p.win32.IPHlpAPI#GetUdpStatisticsEx` - [@dbwiddis](https://github.com/dbwiddis).
1415

1516
Bug Fixes
1617
---------

contrib/platform/src/com/sun/jna/platform/win32/IPHlpAPI.java

+147-40
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright (c) 2018 Daniel Widdis, All Rights Reserved
1+
/* Copyright (c) 2018,2020 Daniel Widdis, All Rights Reserved
22
*
33
* The contents of this file is dual-licensed under 2
44
* alternative Open Source/Free licenses: LGPL 2.1 or later and
@@ -51,6 +51,10 @@ public interface IPHlpAPI extends Library {
5151
int MAX_DOMAIN_NAME_LEN = 128;
5252
int MAX_SCOPE_ID_LEN = 256;
5353

54+
// Source: Winsock2.h
55+
int AF_INET = 2; // The Internet Protocol version 4 (IPv4) address family.
56+
int AF_INET6 = 23; // The Internet Protocol version 6 (IPv6) address family.
57+
5458
/**
5559
* The MIB_IFROW structure stores information about a particular interface.
5660
*
@@ -89,8 +93,7 @@ class MIB_IFROW extends Structure {
8993
}
9094

9195
/**
92-
* The MIB_IF_ROW2 structure stores information about a particular
93-
* interface.
96+
* The MIB_IF_ROW2 structure stores information about a particular interface.
9497
*
9598
* @see <A HREF=
9699
* "https://msdn.microsoft.com/library/windows/hardware/ff559214">MIB_IF_ROW2</A>
@@ -184,8 +187,8 @@ public static class ByReference extends IP_ADDR_STRING implements Structure.ByRe
184187
}
185188

186189
/**
187-
* The FIXED_INFO structure contains information that is the same across all
188-
* the interfaces on a computer.
190+
* The FIXED_INFO structure contains information that is the same across all the
191+
* interfaces on a computer.
189192
*
190193
* @see <A HREF=
191194
* "https://docs.microsoft.com/en-us/windows/desktop/api/iptypes/ns-iptypes-fixed_info_w2ksp1">FIXED_INFO</A>
@@ -214,45 +217,90 @@ public FIXED_INFO() {
214217
}
215218

216219
/**
217-
* The GetIfEntry function retrieves information for the specified interface
220+
* The MIB_TCPSTATS structure contains statistics for the TCP protocol running
218221
* on the local computer.
219-
*
220-
* The dwIndex member in the MIB_IFROW structure pointed to by the pIfRow
221-
* parameter must be initialized to a valid network interface index
222-
* retrieved by a previous call to the GetIfTable, GetIfTable2, or
223-
* GetIfTable2Ex function. The GetIfEntry function will fail if the dwIndex
224-
* member of the MIB_IFROW pointed to by the pIfRow parameter does not match
225-
* an existing interface index on the local computer.
222+
* <p>
223+
* In the Windows SDK, the version of the structure for use on Windows Vista and
224+
* later is defined as {@code MIB_TCPSTATS_LH}. In the Windows SDK, the version
225+
* of this structure to be used on earlier systems including Windows 2000 and
226+
* later is defined as {@code MIB_TCPSTATS_W2K}.
227+
*/
228+
@FieldOrder({ "dwRtoAlgorithm", "dwRtoMin", "dwRtoMax", "dwMaxConn", "dwActiveOpens", "dwPassiveOpens",
229+
"dwAttemptFails", "dwEstabResets", "dwCurrEstab", "dwInSegs", "dwOutSegs", "dwRetransSegs", "dwInErrs",
230+
"dwOutRsts", "dwNumConns" })
231+
class MIB_TCPSTATS extends Structure {
232+
public int dwRtoAlgorithm; // Union for _W2K version, doesn't change mapping
233+
public int dwRtoMin;
234+
public int dwRtoMax;
235+
public int dwMaxConn;
236+
public int dwActiveOpens;
237+
public int dwPassiveOpens;
238+
public int dwAttemptFails;
239+
public int dwEstabResets;
240+
public int dwCurrEstab;
241+
public int dwInSegs;
242+
public int dwOutSegs;
243+
public int dwRetransSegs;
244+
public int dwInErrs;
245+
public int dwOutRsts;
246+
public int dwNumConns;
247+
}
248+
249+
/**
250+
* The MIB_UDPSTATS structure contains statistics for the User Datagram Protocol
251+
* (UDP) running on the local computer.
252+
*/
253+
@FieldOrder({ "dwInDatagrams", "dwNoPorts", "dwInErrors", "dwOutDatagrams", "dwNumAddrs" })
254+
class MIB_UDPSTATS extends Structure {
255+
public int dwInDatagrams;
256+
public int dwNoPorts;
257+
public int dwInErrors;
258+
public int dwOutDatagrams;
259+
public int dwNumAddrs;
260+
}
261+
262+
/**
263+
* The GetIfEntry function retrieves information for the specified interface on
264+
* the local computer.
265+
* <p>
266+
* The {@code dwIndex} member in the {@link MIB_IFROW} structure pointed to by
267+
* the pIfRow parameter must be initialized to a valid network interface index
268+
* retrieved by a previous call to the GetIfTable, GetIfTable2, or GetIfTable2Ex
269+
* function. The GetIfEntry function will fail if the dwIndex member of the
270+
* {@link MIB_IFROW} pointed to by the pIfRow parameter does not match an
271+
* existing interface index on the local computer.
226272
*
227273
* @param pIfRow
228274
* A pointer to a MIB_IFROW structure that, on successful return,
229-
* receives information for an interface on the local computer.
230-
* On input, set the dwIndex member of MIB_IFROW to the index of
275+
* receives information for an interface on the local computer. On
276+
* input, set the dwIndex member of {@link MIB_IFROW} to the index of
231277
* the interface for which to retrieve information.
232-
* @return If the function succeeds, the return value is NO_ERROR.
278+
* @return If the function succeeds, the return value is
279+
* {@link WinError#NO_ERROR}.
233280
*/
234281
int GetIfEntry(MIB_IFROW pIfRow);
235282

236283
/**
237-
* The GetIfEntry2 function retrieves information for the specified
238-
* interface on the local computer.
239-
*
240-
* On input, at least one of the following members in the MIB_IF_ROW2
241-
* structure passed in the Row parameter must be initialized: InterfaceLuid
242-
* or InterfaceIndex. The fields are used in the order listed above. So if
243-
* the InterfaceLuid is specified, then this member is used to determine the
284+
* The GetIfEntry2 function retrieves information for the specified interface on
285+
* the local computer.
286+
* <p>
287+
* On input, at least one of the following members in the {@link MIB_IF_ROW2}
288+
* structure passed in the Row parameter must be initialized: InterfaceLuid or
289+
* InterfaceIndex. The fields are used in the order listed above. So if the
290+
* InterfaceLuid is specified, then this member is used to determine the
244291
* interface. If no value was set for the InterfaceLuid member (the value of
245-
* this member was set to zero), then the InterfaceIndex member is next used
246-
* to determine the interface. On output, the remaining fields of the
247-
* MIB_IF_ROW2 structure pointed to by the Row parameter are filled in.
292+
* this member was set to zero), then the InterfaceIndex member is next used to
293+
* determine the interface. On output, the remaining fields of the
294+
* {@link MIB_IF_ROW2} structure pointed to by the Row parameter are filled in.
248295
*
249296
* @param pIfRow2
250-
* A pointer to a MIB_IF_ROW2 structure that, on successful
297+
* A pointer to a {@link MIB_IF_ROW2} structure that, on successful
251298
* return, receives information for an interface on the local
252-
* computer. On input, the InterfaceLuid or the InterfaceIndex
253-
* member of the MIB_IF_ROW2 must be set to the interface for
254-
* which to retrieve information.
255-
* @return If the function succeeds, the return value is NO_ERROR.
299+
* computer. On input, the InterfaceLuid or the InterfaceIndex member
300+
* of the {@link MIB_IF_ROW2} must be set to the interface for which
301+
* to retrieve information.
302+
* @return If the function succeeds, the return value is
303+
* {@link WinError#NO_ERROR}.
256304
*/
257305
int GetIfEntry2(MIB_IF_ROW2 pIfRow2);
258306

@@ -261,18 +309,77 @@ public FIXED_INFO() {
261309
* computer.
262310
*
263311
* @param pFixedInfo
264-
* A pointer to a buffer that contains a FIXED_INFO structure
265-
* that receives the network parameters for the local computer,
266-
* if the function was successful. This buffer must be allocated
267-
* by the caller prior to calling the GetNetworkParams function.
312+
* A pointer to a buffer that contains a {@link FIXED_INFO} structure
313+
* that receives the network parameters for the local computer, if
314+
* the function was successful. This buffer must be allocated by the
315+
* caller prior to calling the GetNetworkParams function.
268316
* @param pOutBufLen
269317
* A pointer to a ULONG variable that specifies the size of the
270-
* FIXED_INFO structure. If this size is insufficient to hold the
271-
* information, GetNetworkParams fills in this variable with the
318+
* {@link FIXED_INFO} structure. If this size is insufficient to hold
319+
* the information, GetNetworkParams fills in this variable with the
272320
* required size, and returns an error code of
273-
* ERROR_BUFFER_OVERFLOW.
274-
* @return If the function succeeds, the return value is ERROR_SUCCESS.
321+
* {@link WinError#ERROR_BUFFER_OVERFLOW}.
322+
* @return If the function succeeds, the return value is
323+
* {@link WinError#ERROR_SUCCESS}.
275324
*/
276325
int GetNetworkParams(Pointer pFixedInfo, IntByReference pOutBufLen);
277-
}
278326

327+
/**
328+
* The GetTcpStatistics function retrieves the TCP statistics for the local
329+
* computer.
330+
*
331+
* @param Statistics
332+
* A {@link MIB_TCPSTATS} structure that receives the TCP statistics
333+
* for the local computer.
334+
* @return If the function succeeds, the return value is
335+
* {@link WinError#NO_ERROR}.
336+
*/
337+
int GetTcpStatistics(MIB_TCPSTATS Statistics);
338+
339+
/**
340+
* The GetTcpStatisticsEx function retrieves the Transmission Control Protocol
341+
* (TCP) statistics for the current computer. The GetTcpStatisticsEx function
342+
* differs from the {@link #GetTcpStatistics} function in that
343+
* GetTcpStatisticsEx also supports the Internet Protocol version 6 (IPv6)
344+
* protocol family.
345+
*
346+
* @param Statistics
347+
* A {@link MIB_TCPSTATS} structure that receives the TCP statistics
348+
* for the local computer.
349+
* @param Family
350+
* The protocol family for which to retrieve statistics. This
351+
* parameter must be {@link #AF_INET} or {@link #AF_INET6}.
352+
* @return If the function succeeds, the return value is
353+
* {@link WinError#NO_ERROR}.
354+
*/
355+
int GetTcpStatisticsEx(MIB_TCPSTATS Statistics, int Family);
356+
357+
/**
358+
* The GetUdpStatistics function retrieves the User Datagram Protocol (UDP)
359+
* statistics for the local computer.
360+
*
361+
* @param Statistics
362+
* A {@link MIB_UDPSTATS} structure that receives the UDP statistics
363+
* for the local computer.
364+
* @return If the function succeeds, the return value is
365+
* {@link WinError#NO_ERROR}.
366+
*/
367+
int GetUdpStatistics(MIB_UDPSTATS Statistics);
368+
369+
/**
370+
* The GetUdpStatisticsEx function retrieves the User Datagram Protocol (UDP)
371+
* statistics for the current computer. The GetUdpStatisticsEx function differs
372+
* from the {@link #GetUdpStatistics} function in that GetUdpStatisticsEx also
373+
* supports the Internet Protocol version 6 (IPv6) protocol family.
374+
*
375+
* @param Statistics
376+
* A {@link MIB_UDPSTATS} structure that receives the UDP statistics
377+
* for the local computer.
378+
* @param Family
379+
* The protocol family for which to retrieve statistics. This
380+
* parameter must be {@link #AF_INET} or {@link #AF_INET6}.
381+
* @return If the function succeeds, the return value is
382+
* {@link WinError#NO_ERROR}.
383+
*/
384+
int GetUdpStatisticsEx(MIB_UDPSTATS Statistics, int Family);
385+
}

contrib/platform/test/com/sun/jna/platform/win32/IPHlpAPITest.java

+35-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright (c) 2018 Daniel Widdis, All Rights Reserved
1+
/* Copyright (c) 2018,2020 Daniel Widdis, All Rights Reserved
22
*
33
* The contents of this file is dual-licensed under 2
44
* alternative Open Source/Free licenses: LGPL 2.1 or later and
@@ -39,6 +39,8 @@
3939
import com.sun.jna.platform.win32.IPHlpAPI.FIXED_INFO;
4040
import com.sun.jna.platform.win32.IPHlpAPI.MIB_IFROW;
4141
import com.sun.jna.platform.win32.IPHlpAPI.MIB_IF_ROW2;
42+
import com.sun.jna.platform.win32.IPHlpAPI.MIB_TCPSTATS;
43+
import com.sun.jna.platform.win32.IPHlpAPI.MIB_UDPSTATS;
4244
import com.sun.jna.ptr.IntByReference;
4345

4446
public class IPHlpAPITest {
@@ -135,4 +137,36 @@ public void testGetNetworkParams() {
135137
dns = dns.Next;
136138
}
137139
}
140+
141+
@Test
142+
public void testGetTcpStatistics() {
143+
MIB_TCPSTATS stats = new MIB_TCPSTATS();
144+
assertEquals(WinError.NO_ERROR, IPHlpAPI.INSTANCE.GetTcpStatistics(stats));
145+
assertTrue(stats.dwRtoAlgorithm >= 1);
146+
assertTrue(stats.dwRtoAlgorithm <= 4);
147+
assertTrue(stats.dwEstabResets <= stats.dwCurrEstab);
148+
149+
// Above should roughly match IPv4 stats with Ex version
150+
MIB_TCPSTATS stats4 = new MIB_TCPSTATS();
151+
assertEquals(WinError.NO_ERROR, IPHlpAPI.INSTANCE.GetTcpStatisticsEx(stats4, IPHlpAPI.AF_INET));
152+
assertEquals(stats.dwRtoAlgorithm, stats4.dwRtoAlgorithm);
153+
assertTrue(stats4.dwEstabResets <= stats4.dwCurrEstab);
154+
assertTrue(stats.dwActiveOpens <= stats4.dwActiveOpens);
155+
assertTrue(stats.dwPassiveOpens <= stats4.dwPassiveOpens);
156+
}
157+
158+
@Test
159+
public void testGetUdpStatistics() {
160+
MIB_UDPSTATS stats = new MIB_UDPSTATS();
161+
assertEquals(WinError.NO_ERROR, IPHlpAPI.INSTANCE.GetUdpStatistics(stats));
162+
assertTrue(stats.dwNoPorts + stats.dwInErrors <= stats.dwInDatagrams);
163+
164+
// Above should roughly match IPv4 stats with Ex version
165+
MIB_UDPSTATS stats4 = new MIB_UDPSTATS();
166+
assertEquals(WinError.NO_ERROR, IPHlpAPI.INSTANCE.GetUdpStatisticsEx(stats4, IPHlpAPI.AF_INET));
167+
assertTrue(stats.dwNoPorts <= stats4.dwNoPorts);
168+
assertTrue(stats.dwInErrors <= stats4.dwInErrors);
169+
assertTrue(stats.dwInDatagrams <= stats4.dwInDatagrams);
170+
assertTrue(stats4.dwNoPorts + stats4.dwInErrors <= stats4.dwInDatagrams);
171+
}
138172
}

0 commit comments

Comments
 (0)