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 com.android.net.module.util.netlink;
18 
19 import androidx.annotation.Nullable;
20 
21 import java.nio.ByteBuffer;
22 
23 /**
24  * struct inet_diag_req_v2
25  *
26  * see <linux_src>/include/uapi/linux/inet_diag.h
27  *
28  *      struct inet_diag_req_v2 {
29  *          __u8    sdiag_family;
30  *          __u8    sdiag_protocol;
31  *          __u8    idiag_ext;
32  *          __u8    pad;
33  *          __u32   idiag_states;
34  *          struct  inet_diag_sockid id;
35  *      };
36  *
37  * @hide
38  */
39 public class StructInetDiagReqV2 {
40     public static final int STRUCT_SIZE = 8 + StructInetDiagSockId.STRUCT_SIZE;
41 
42     private final byte mSdiagFamily;
43     private final byte mSdiagProtocol;
44     private final byte mIdiagExt;
45     private final byte mPad;
46     private final StructInetDiagSockId mId;
47     private final int mState;
48     public static final int INET_DIAG_REQ_V2_ALL_STATES = (int) 0xffffffff;
49 
StructInetDiagReqV2(int protocol, @Nullable StructInetDiagSockId id, int family, int pad, int extension, int state)50     public StructInetDiagReqV2(int protocol, @Nullable StructInetDiagSockId id, int family, int pad,
51             int extension, int state) {
52         mSdiagFamily = (byte) family;
53         mSdiagProtocol = (byte) protocol;
54         mId = id;
55         mPad = (byte) pad;
56         mIdiagExt = (byte) extension;
57         mState = state;
58     }
59 
60     /**
61      * Write the int diag request v2 message to ByteBuffer.
62      */
pack(ByteBuffer byteBuffer)63     public void pack(ByteBuffer byteBuffer) {
64         // The ByteOrder must have already been set by the caller.
65         byteBuffer.put((byte) mSdiagFamily);
66         byteBuffer.put((byte) mSdiagProtocol);
67         byteBuffer.put((byte) mIdiagExt);
68         byteBuffer.put((byte) mPad);
69         byteBuffer.putInt(mState);
70         if (mId != null) mId.pack(byteBuffer);
71     }
72 
73     @Override
toString()74     public String toString() {
75         final String familyStr = NetlinkConstants.stringForAddressFamily(mSdiagFamily);
76         final String protocolStr = NetlinkConstants.stringForAddressFamily(mSdiagProtocol);
77 
78         return "StructInetDiagReqV2{ "
79                 + "sdiag_family{" + familyStr + "}, "
80                 + "sdiag_protocol{" + protocolStr + "}, "
81                 + "idiag_ext{" + mIdiagExt + ")}, "
82                 + "pad{" + mPad + "}, "
83                 + "idiag_states{" + Integer.toHexString(mState) + "}, "
84                 + ((mId != null) ? mId.toString() : "inet_diag_sockid=null")
85                 + "}";
86     }
87 }
88