1 /* 2 * Copyright (C) 2019 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.net.netlink; 18 19 import android.annotation.Nullable; 20 21 import java.net.InetSocketAddress; 22 import java.nio.ByteBuffer; 23 24 /** 25 * struct inet_diag_req_v2 26 * 27 * see <linux_src>/include/uapi/linux/inet_diag.h 28 * 29 * struct inet_diag_req_v2 { 30 * __u8 sdiag_family; 31 * __u8 sdiag_protocol; 32 * __u8 idiag_ext; 33 * __u8 pad; 34 * __u32 idiag_states; 35 * struct inet_diag_sockid id; 36 * }; 37 * 38 * @hide 39 */ 40 public class StructInetDiagReqV2 { 41 public static final int STRUCT_SIZE = 8 + StructInetDiagSockId.STRUCT_SIZE; 42 43 private final byte mSdiagFamily; 44 private final byte mSdiagProtocol; 45 private final byte mIdiagExt; 46 private final byte mPad; 47 private final StructInetDiagSockId mId; 48 private final int mState; 49 public static final int INET_DIAG_REQ_V2_ALL_STATES = (int) 0xffffffff; 50 StructInetDiagReqV2(int protocol, InetSocketAddress local, InetSocketAddress remote, int family)51 public StructInetDiagReqV2(int protocol, InetSocketAddress local, InetSocketAddress remote, 52 int family) { 53 this(protocol, local, remote, family, 0 /* pad */, 0 /* extension */, 54 INET_DIAG_REQ_V2_ALL_STATES); 55 } 56 StructInetDiagReqV2(int protocol, @Nullable InetSocketAddress local, @Nullable InetSocketAddress remote, int family, int pad, int extension, int state)57 public StructInetDiagReqV2(int protocol, @Nullable InetSocketAddress local, 58 @Nullable InetSocketAddress remote, int family, int pad, int extension, int state) 59 throws NullPointerException { 60 mSdiagFamily = (byte) family; 61 mSdiagProtocol = (byte) protocol; 62 // Request for all sockets if no specific socket is requested. Specify the local and remote 63 // socket address information for target request socket. 64 if ((local == null) != (remote == null)) { 65 throw new NullPointerException("Local and remote must be both null or both non-null"); 66 } 67 mId = ((local != null && remote != null) ? new StructInetDiagSockId(local, remote) : null); 68 mPad = (byte) pad; 69 mIdiagExt = (byte) extension; 70 mState = state; 71 } 72 pack(ByteBuffer byteBuffer)73 public void pack(ByteBuffer byteBuffer) { 74 // The ByteOrder must have already been set by the caller. 75 byteBuffer.put((byte) mSdiagFamily); 76 byteBuffer.put((byte) mSdiagProtocol); 77 byteBuffer.put((byte) mIdiagExt); 78 byteBuffer.put((byte) mPad); 79 byteBuffer.putInt(mState); 80 if (mId != null) mId.pack(byteBuffer); 81 } 82 83 @Override toString()84 public String toString() { 85 final String familyStr = NetlinkConstants.stringForAddressFamily(mSdiagFamily); 86 final String protocolStr = NetlinkConstants.stringForAddressFamily(mSdiagProtocol); 87 88 return "StructInetDiagReqV2{ " 89 + "sdiag_family{" + familyStr + "}, " 90 + "sdiag_protocol{" + protocolStr + "}, " 91 + "idiag_ext{" + mIdiagExt + ")}, " 92 + "pad{" + mPad + "}, " 93 + "idiag_states{" + Integer.toHexString(mState) + "}, " 94 + ((mId != null) ? mId.toString() : "inet_diag_sockid=null") 95 + "}"; 96 } 97 } 98