diff options
author | Chiachang Wang <chiachangwang@google.com> | 2019-10-24 07:31:53 +0000 |
---|---|---|
committer | Gerrit Code Review <noreply-gerritcodereview@google.com> | 2019-10-24 07:31:53 +0000 |
commit | 0825500b27279489e1c3a34c2994e4af8921055d (patch) | |
tree | 966555e3b68996cc2103b6cda42733b59c926eba /services/net/java | |
parent | 78b20d0d5854ccc1737b515e65f188ba66d910e7 (diff) | |
parent | 5d62167efc4b58f7d319bd11e85c34b42d7dc6ac (diff) |
Merge "Extend netlink class to fit the data structure"
Diffstat (limited to 'services/net/java')
-rw-r--r-- | services/net/java/android/net/netlink/InetDiagMessage.java | 35 | ||||
-rw-r--r-- | services/net/java/android/net/netlink/StructInetDiagReqV2.java | 63 |
2 files changed, 72 insertions, 26 deletions
diff --git a/services/net/java/android/net/netlink/InetDiagMessage.java b/services/net/java/android/net/netlink/InetDiagMessage.java index 31a2556f2041..ca07630d3e63 100644 --- a/services/net/java/android/net/netlink/InetDiagMessage.java +++ b/services/net/java/android/net/netlink/InetDiagMessage.java @@ -26,6 +26,7 @@ import static android.system.OsConstants.AF_INET6; import static android.system.OsConstants.IPPROTO_UDP; import static android.system.OsConstants.NETLINK_INET_DIAG; +import android.annotation.Nullable; import android.net.util.SocketUtils; import android.system.ErrnoException; import android.util.Log; @@ -53,7 +54,35 @@ public class InetDiagMessage extends NetlinkMessage { private static final int TIMEOUT_MS = 500; public static byte[] InetDiagReqV2(int protocol, InetSocketAddress local, - InetSocketAddress remote, int family, short flags) { + InetSocketAddress remote, int family, short flags) { + return InetDiagReqV2(protocol, local, remote, family, flags, 0 /* pad */, + 0 /* idiagExt */, StructInetDiagReqV2.INET_DIAG_REQ_V2_ALL_STATES); + } + + /** + * Construct an inet_diag_req_v2 message. This method will throw {@code NullPointerException} + * if local and remote are not both null or both non-null. + * + * @param protocol the request protocol type. This should be set to one of IPPROTO_TCP, + * IPPROTO_UDP, or IPPROTO_UDPLITE. + * @param local local socket address of the target socket. This will be packed into a + * {@Code StructInetDiagSockId}. Request to diagnose for all sockets if both of + * local or remote address is null. + * @param remote remote socket address of the target socket. This will be packed into a + * {@Code StructInetDiagSockId}. Request to diagnose for all sockets if both of + * local or remote address is null. + * @param family the ip family of the request message. This should be set to either AF_INET or + * AF_INET6 for IPv4 or IPv6 sockets respectively. + * @param flags message flags. See <linux_src>/include/uapi/linux/netlink.h. + * @param pad for raw socket protocol specification. + * @param idiagExt a set of flags defining what kind of extended information to report. + * @param state a bit mask that defines a filter of socket states. + * + * @return bytes array representation of the message + **/ + public static byte[] InetDiagReqV2(int protocol, @Nullable InetSocketAddress local, + @Nullable InetSocketAddress remote, int family, short flags, int pad, int idiagExt, + int state) throws NullPointerException { final byte[] bytes = new byte[StructNlMsgHdr.STRUCT_SIZE + StructInetDiagReqV2.STRUCT_SIZE]; final ByteBuffer byteBuffer = ByteBuffer.wrap(bytes); byteBuffer.order(ByteOrder.nativeOrder()); @@ -63,9 +92,9 @@ public class InetDiagMessage extends NetlinkMessage { nlMsgHdr.nlmsg_type = SOCK_DIAG_BY_FAMILY; nlMsgHdr.nlmsg_flags = flags; nlMsgHdr.pack(byteBuffer); + final StructInetDiagReqV2 inetDiagReqV2 = + new StructInetDiagReqV2(protocol, local, remote, family, pad, idiagExt, state); - final StructInetDiagReqV2 inetDiagReqV2 = new StructInetDiagReqV2(protocol, local, remote, - family); inetDiagReqV2.pack(byteBuffer); return bytes; } diff --git a/services/net/java/android/net/netlink/StructInetDiagReqV2.java b/services/net/java/android/net/netlink/StructInetDiagReqV2.java index 49a93258e714..2268113044d5 100644 --- a/services/net/java/android/net/netlink/StructInetDiagReqV2.java +++ b/services/net/java/android/net/netlink/StructInetDiagReqV2.java @@ -16,10 +16,10 @@ package android.net.netlink; -import static java.nio.ByteOrder.BIG_ENDIAN; +import android.annotation.Nullable; + import java.net.InetSocketAddress; import java.nio.ByteBuffer; -import java.nio.ByteOrder; /** * struct inet_diag_req_v2 @@ -40,41 +40,58 @@ import java.nio.ByteOrder; public class StructInetDiagReqV2 { public static final int STRUCT_SIZE = 8 + StructInetDiagSockId.STRUCT_SIZE; - private final byte sdiag_family; - private final byte sdiag_protocol; - private final StructInetDiagSockId id; - private final int INET_DIAG_REQ_V2_ALL_STATES = (int) 0xffffffff; - + private final byte mSdiagFamily; + private final byte mSdiagProtocol; + private final byte mIdiagExt; + private final byte mPad; + private final StructInetDiagSockId mId; + private final int mState; + public static final int INET_DIAG_REQ_V2_ALL_STATES = (int) 0xffffffff; public StructInetDiagReqV2(int protocol, InetSocketAddress local, InetSocketAddress remote, - int family) { - sdiag_family = (byte) family; - sdiag_protocol = (byte) protocol; - id = new StructInetDiagSockId(local, remote); + int family) { + this(protocol, local, remote, family, 0 /* pad */, 0 /* extension */, + INET_DIAG_REQ_V2_ALL_STATES); + } + + public StructInetDiagReqV2(int protocol, @Nullable InetSocketAddress local, + @Nullable InetSocketAddress remote, int family, int pad, int extension, int state) + throws NullPointerException { + mSdiagFamily = (byte) family; + mSdiagProtocol = (byte) protocol; + // Request for all sockets if no specific socket is requested. Specify the local and remote + // socket address information for target request socket. + if ((local == null) != (remote == null)) { + throw new NullPointerException("Local and remote must be both null or both non-null"); + } + mId = ((local != null && remote != null) ? new StructInetDiagSockId(local, remote) : null); + mPad = (byte) pad; + mIdiagExt = (byte) extension; + mState = state; } public void pack(ByteBuffer byteBuffer) { // The ByteOrder must have already been set by the caller. - byteBuffer.put((byte) sdiag_family); - byteBuffer.put((byte) sdiag_protocol); - byteBuffer.put((byte) 0); - byteBuffer.put((byte) 0); - byteBuffer.putInt(INET_DIAG_REQ_V2_ALL_STATES); - id.pack(byteBuffer); + byteBuffer.put((byte) mSdiagFamily); + byteBuffer.put((byte) mSdiagProtocol); + byteBuffer.put((byte) mIdiagExt); + byteBuffer.put((byte) mPad); + byteBuffer.putInt(mState); + if (mId != null) mId.pack(byteBuffer); } @Override public String toString() { - final String familyStr = NetlinkConstants.stringForAddressFamily(sdiag_family); - final String protocolStr = NetlinkConstants.stringForAddressFamily(sdiag_protocol); + final String familyStr = NetlinkConstants.stringForAddressFamily(mSdiagFamily); + final String protocolStr = NetlinkConstants.stringForAddressFamily(mSdiagProtocol); return "StructInetDiagReqV2{ " + "sdiag_family{" + familyStr + "}, " + "sdiag_protocol{" + protocolStr + "}, " - + "idiag_ext{" + 0 + ")}, " - + "pad{" + 0 + "}, " - + "idiag_states{" + Integer.toHexString(INET_DIAG_REQ_V2_ALL_STATES) + "}, " - + id.toString() + + "idiag_ext{" + mIdiagExt + ")}, " + + "pad{" + mPad + "}, " + + "idiag_states{" + Integer.toHexString(mState) + "}, " + + ((mId != null) ? mId.toString() : "inet_diag_sockid=null") + "}"; } } |