Skip to content

Commit ed3bae9

Browse files
committed
Add IpHlpApi GetExtendedTcpTable and GetExtendedUdpTable
1 parent 91f139e commit ed3bae9

File tree

3 files changed

+408
-4
lines changed

3 files changed

+408
-4
lines changed

CHANGES.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ Features
1818
* [#1239](https://github.com/java-native-access/jna/pull/1239): Improve performance of allocation of `c.s.j.Memory` objects - [@joerg1985](https://github.com/joerg1985).
1919
* [#1246](https://github.com/java-native-access/jna/pull/1246): Improve performance of `c.s.j.Structure#read` and `c.s.j.Structure#write` - [@joerg1985](https://github.com/joerg1985).
2020
* [#1260](https://github.com/java-native-access/jna/pull/1260): Add mapping for X11 generic events - [@lafoletc](https://github.com/lafoletc).
21+
* [#1263](https://github.com/java-native-access/jna/pull/1263): Add LowLevelMouseProc - [@nordiakt](https://github.com/nordiakt)
2122
* [#1265](https://github.com/java-native-access/jna/pull/1265): Add mapping for XQueryExtension - [@lafoletc](https://github.com/lafoletc).
22-
* [#1263](https://github.com/java-native-access/jna/pull/1263): Add LowLevelMouseProc - [@nordiakt](https://github.com/nordiakt).
23+
* [#1299](https://github.com/java-native-access/jna/pull/1299): Add `c.s.j.p.win32.IPHlpApi#GetExtendedTcpTable`, `c.s.j.p.win32.IPHlpApi#GetExtendedUdpTable`, and supporting structures. - [@dbwiddis](https://github.com/dbwiddis).
2324

2425
Bug Fixes
2526
---------

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

+274-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright (c) 2018,2020 Daniel Widdis, All Rights Reserved
1+
/* Copyright (c) 2018,2020,2021 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
@@ -60,6 +60,50 @@ public interface IPHlpAPI extends Library {
6060
int AF_IRDA = 26; // The Infrared Data Association (IrDA) address family.
6161
int AF_BTH = 32; // The Bluetooth address family.
6262

63+
/**
64+
* Defines the set of values used to indicate the type of table returned by
65+
* calls to {@link #GetExtendedTcpTable}
66+
*/
67+
interface TCP_TABLE_CLASS {
68+
int TCP_TABLE_BASIC_LISTENER = 0;
69+
int TCP_TABLE_BASIC_CONNECTIONS = 1;
70+
int TCP_TABLE_BASIC_ALL = 2;
71+
int TCP_TABLE_OWNER_PID_LISTENER = 3;
72+
int TCP_TABLE_OWNER_PID_CONNECTIONS = 4;
73+
int TCP_TABLE_OWNER_PID_ALL = 5;
74+
int TCP_TABLE_OWNER_MODULE_LISTENER = 6;
75+
int TCP_TABLE_OWNER_MODULE_CONNECTIONS = 7;
76+
int TCP_TABLE_OWNER_MODULE_ALL = 8;
77+
}
78+
79+
/**
80+
* Defines the set of values used to indicate the type of table returned by
81+
* calls to {@link #GetExtendedUdpTable}.
82+
*/
83+
interface UDP_TABLE_CLASS {
84+
int UDP_TABLE_BASIC = 0;
85+
int UDP_TABLE_OWNER_PID = 1;
86+
int UDP_TABLE_OWNER_MODULE = 2;
87+
}
88+
89+
/**
90+
* Enumerates different possible TCP states.
91+
*/
92+
interface MIB_TCP_STATE {
93+
int MIB_TCP_STATE_CLOSED = 1;
94+
int MIB_TCP_STATE_LISTEN = 2;
95+
int MIB_TCP_STATE_SYN_SENT = 3;
96+
int MIB_TCP_STATE_SYN_RCVD = 4;
97+
int MIB_TCP_STATE_ESTAB = 5;
98+
int MIB_TCP_STATE_FIN_WAIT1 = 6;
99+
int MIB_TCP_STATE_FIN_WAIT2 = 7;
100+
int MIB_TCP_STATE_CLOSE_WAIT = 8;
101+
int MIB_TCP_STATE_CLOSING = 9;
102+
int MIB_TCP_STATE_LAST_ACK = 10;
103+
int MIB_TCP_STATE_TIME_WAIT = 11;
104+
int MIB_TCP_STATE_DELETE_TCB = 12;
105+
}
106+
63107
/**
64108
* The MIB_IFROW structure stores information about a particular interface.
65109
*
@@ -264,6 +308,160 @@ class MIB_UDPSTATS extends Structure {
264308
public int dwNumAddrs;
265309
}
266310

311+
/**
312+
* Contains information that describes an IPv4 TCP connection.
313+
*/
314+
@FieldOrder({ "dwState", "dwLocalAddr", "dwLocalPort", "dwRemoteAddr", "dwRemotePort", "dwOwningPid" })
315+
class MIB_TCPROW_OWNER_PID extends Structure {
316+
public int dwState;
317+
public int dwLocalAddr;
318+
public int dwLocalPort;
319+
public int dwRemoteAddr;
320+
public int dwRemotePort;
321+
public int dwOwningPid;
322+
}
323+
324+
/**
325+
* Contains a table of IPv4 TCP connections on the local computer.
326+
*/
327+
@FieldOrder({ "dwNumEntries", "table" })
328+
class MIB_TCPTABLE_OWNER_PID extends Structure {
329+
public int dwNumEntries;
330+
public MIB_TCPROW_OWNER_PID[] table = new MIB_TCPROW_OWNER_PID[1];
331+
332+
public MIB_TCPTABLE_OWNER_PID(Pointer buf) {
333+
super(buf);
334+
read();
335+
}
336+
337+
@Override
338+
public void read() {
339+
// First element contains array size
340+
this.dwNumEntries = getPointer().getInt(0);
341+
if (this.dwNumEntries > 0) {
342+
table = (MIB_TCPROW_OWNER_PID[]) new MIB_TCPROW_OWNER_PID().toArray(this.dwNumEntries);
343+
super.read();
344+
} else {
345+
table = new MIB_TCPROW_OWNER_PID[0];
346+
}
347+
}
348+
}
349+
350+
/**
351+
* Contains information that describes an IPv6 TCP connection.
352+
*/
353+
@FieldOrder({ "LocalAddr", "dwLocalScopeId", "dwLocalPort", "RemoteAddr", "dwRemoteScopeId", "dwRemotePort",
354+
"State", "dwOwningPid" })
355+
class MIB_TCP6ROW_OWNER_PID extends Structure {
356+
public byte[] LocalAddr = new byte[16];
357+
public int dwLocalScopeId;
358+
public int dwLocalPort;
359+
public byte[] RemoteAddr = new byte[16];
360+
public int dwRemoteScopeId;
361+
public int dwRemotePort;
362+
public int State;
363+
public int dwOwningPid;
364+
}
365+
366+
/**
367+
* Contains a table of IPv6 TCP connections on the local computer.
368+
*/
369+
@FieldOrder({ "dwNumEntries", "table" })
370+
class MIB_TCP6TABLE_OWNER_PID extends Structure {
371+
public int dwNumEntries;
372+
public MIB_TCP6ROW_OWNER_PID[] table = new MIB_TCP6ROW_OWNER_PID[1];
373+
374+
public MIB_TCP6TABLE_OWNER_PID(Pointer buf) {
375+
super(buf);
376+
read();
377+
}
378+
379+
@Override
380+
public void read() {
381+
// First element contains array size
382+
this.dwNumEntries = getPointer().getInt(0);
383+
if (this.dwNumEntries > 0) {
384+
table = (MIB_TCP6ROW_OWNER_PID[]) new MIB_TCP6ROW_OWNER_PID().toArray(this.dwNumEntries);
385+
super.read();
386+
} else {
387+
table = new MIB_TCP6ROW_OWNER_PID[0];
388+
}
389+
}
390+
}
391+
392+
/**
393+
* Contains information that describes an IPv6 UDP connection.
394+
*/
395+
@FieldOrder({ "dwLocalAddr", "dwLocalPort", "dwOwningPid" })
396+
class MIB_UDPROW_OWNER_PID extends Structure {
397+
public int dwLocalAddr;
398+
public int dwLocalPort;
399+
public int dwOwningPid;
400+
}
401+
402+
/**
403+
* Contains a table of IPv6 UDP connections on the local computer.
404+
*/
405+
@FieldOrder({ "dwNumEntries", "table" })
406+
class MIB_UDPTABLE_OWNER_PID extends Structure {
407+
public int dwNumEntries;
408+
public MIB_UDPROW_OWNER_PID[] table = new MIB_UDPROW_OWNER_PID[1];
409+
410+
public MIB_UDPTABLE_OWNER_PID(Pointer buf) {
411+
super(buf);
412+
read();
413+
}
414+
415+
@Override
416+
public void read() {
417+
// First element contains array size
418+
this.dwNumEntries = getPointer().getInt(0);
419+
if (this.dwNumEntries > 0) {
420+
table = (MIB_UDPROW_OWNER_PID[]) new MIB_UDPROW_OWNER_PID().toArray(this.dwNumEntries);
421+
super.read();
422+
} else {
423+
table = new MIB_UDPROW_OWNER_PID[0];
424+
}
425+
}
426+
}
427+
428+
/**
429+
* Contains information that describes an IPv6 UDP connection.
430+
*/
431+
@FieldOrder({ "ucLocalAddr", "dwLocalScopeId", "dwLocalPort", "dwOwningPid" })
432+
class MIB_UDP6ROW_OWNER_PID extends Structure {
433+
public byte[] ucLocalAddr = new byte[16];
434+
public int dwLocalScopeId;
435+
public int dwLocalPort;
436+
public int dwOwningPid;
437+
}
438+
439+
/**
440+
* Contains a table of IPv6 UDP connections on the local computer.
441+
*/
442+
@FieldOrder({ "dwNumEntries", "table" })
443+
class MIB_UDP6TABLE_OWNER_PID extends Structure {
444+
public int dwNumEntries;
445+
public MIB_UDP6ROW_OWNER_PID[] table = new MIB_UDP6ROW_OWNER_PID[1];
446+
447+
public MIB_UDP6TABLE_OWNER_PID(Pointer buf) {
448+
super(buf);
449+
read();
450+
}
451+
452+
@Override
453+
public void read() {
454+
// First element contains array size
455+
this.dwNumEntries = getPointer().getInt(0);
456+
if (this.dwNumEntries > 0) {
457+
table = (MIB_UDP6ROW_OWNER_PID[]) new MIB_UDP6ROW_OWNER_PID().toArray(this.dwNumEntries);
458+
super.read();
459+
} else {
460+
table = new MIB_UDP6ROW_OWNER_PID[0];
461+
}
462+
}
463+
}
464+
267465
/**
268466
* The GetIfEntry function retrieves information for the specified interface on
269467
* the local computer.
@@ -387,4 +585,79 @@ class MIB_UDPSTATS extends Structure {
387585
* {@link WinError#NO_ERROR}.
388586
*/
389587
int GetUdpStatisticsEx(MIB_UDPSTATS Statistics, int Family);
588+
589+
/**
590+
* Retrieves a table that contains a list of TCP endpoints available to the
591+
* application.
592+
*
593+
* @param pTcpTable
594+
* A pointer to the table structure that contains the filtered TCP
595+
* endpoints available to the application.
596+
* @param pdwSize
597+
* The estimated size of the structure returned in pTcpTable, in
598+
* bytes. If this value is set too small,
599+
* {@code ERROR_INSUFFICIENT_BUFFER} is returned by this function,
600+
* and this field will contain the correct size of the structure.
601+
* @param bOrder
602+
* A value that specifies whether the TCP connection table should be
603+
* sorted. If this parameter is set to TRUE, the TCP endpoints in the
604+
* table are sorted in ascending order, starting with the lowest
605+
* local IP address. If this parameter is set to FALSE, the TCP
606+
* endpoints in the table appear in the order in which they were
607+
* retrieved. The following values are compared (as listed) when
608+
* ordering the TCP endpoints: Local IP address, Local scope ID
609+
* (applicable when the ulAf parameter is set to AF_INET6), Local TCP
610+
* port, Remote IP address, Remote scope ID (applicable when the ulAf
611+
* parameter is set to AF_INET6), Remote TCP port.
612+
* @param ulAf
613+
* The version of IP used by the TCP endpoints.
614+
* @param TableClass
615+
* The type of the TCP table structure to retrieve. This parameter
616+
* can be one of the values from the {@link TCP_TABLE_CLASS}
617+
* enumeration.
618+
* @param Reserved
619+
* Reserved. This value must be zero.
620+
* @return If the function succeeds, the return value is {@code NO_ERROR}. If
621+
* the function fails, the return value is an error code.
622+
*/
623+
int GetExtendedTcpTable(Pointer pTcpTable, IntByReference pdwSize, boolean bOrder, int ulAf, int TableClass,
624+
int Reserved);
625+
626+
/**
627+
* Retrieves a table that contains a list of UDP endpoints available to the
628+
* application.
629+
*
630+
* @param pUdpTable
631+
* A pointer to the table structure that contains the filtered UDP
632+
* endpoints available to the application.
633+
* @param pdwSize
634+
* The estimated size of the structure returned in pTcpTable, in
635+
* bytes. If this value is set too small,
636+
* {@code ERROR_INSUFFICIENT_BUFFER} is returned by this function,
637+
* and this field will contain the correct size of the structure.
638+
* @param bOrder
639+
* A value that specifies whether the TCP connection table should be
640+
* sorted. If this parameter is set to TRUE, the TCP endpoints in the
641+
* table are sorted in ascending order, starting with the lowest
642+
* local IP address. If this parameter is set to FALSE, the TCP
643+
* endpoints in the table appear in the order in which they were
644+
* retrieved. The following values are compared (as listed) when
645+
* ordering the TCP endpoints: Local IP address, Local scope ID
646+
* (applicable when the ulAf parameter is set to {@code AF_INET6}),
647+
* Local TCP port, Remote IP address, Remote scope ID (applicable
648+
* when the ulAf parameter is set to {@code AF_INET6}), Remote TCP
649+
* port.
650+
* @param ulAf
651+
* The version of IP used by the UDP endpoints.
652+
* @param TableClass
653+
* The type of the TCP table structure to retrieve. This parameter
654+
* can be one of the values from the {@link TCP_TABLE_CLASS}
655+
* enumeration.
656+
* @param Reserved
657+
* Reserved. This value must be zero.
658+
* @return If the function succeeds, the return value is {@code NO_ERROR}. If
659+
* the function fails, the return value is an error code.
660+
*/
661+
int GetExtendedUdpTable(Pointer pUdpTable, IntByReference pdwSize, boolean bOrder, int ulAf, int TableClass,
662+
int Reserved);
390663
}

0 commit comments

Comments
 (0)