1 /* 2 * Copyright (C) 2018 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 static java.nio.ByteOrder.BIG_ENDIAN; 20 21 import java.net.Inet4Address; 22 import java.net.InetSocketAddress; 23 import java.nio.ByteBuffer; 24 import java.nio.ByteOrder; 25 26 /** 27 * struct inet_diag_req_v2 28 * 29 * see <linux_src>/include/uapi/linux/inet_diag.h 30 * 31 * struct inet_diag_sockid { 32 * __be16 idiag_sport; 33 * __be16 idiag_dport; 34 * __be32 idiag_src[4]; 35 * __be32 idiag_dst[4]; 36 * __u32 idiag_if; 37 * __u32 idiag_cookie[2]; 38 * #define INET_DIAG_NOCOOKIE (~0U) 39 * }; 40 * 41 * @hide 42 */ 43 public class StructInetDiagSockId { 44 public static final int STRUCT_SIZE = 48; 45 46 private final InetSocketAddress mLocSocketAddress; 47 private final InetSocketAddress mRemSocketAddress; 48 private final byte[] INET_DIAG_NOCOOKIE = new byte[]{ 49 (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, 50 (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff}; 51 private final byte[] IPV4_PADDING = new byte[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; 52 StructInetDiagSockId(InetSocketAddress loc, InetSocketAddress rem)53 public StructInetDiagSockId(InetSocketAddress loc, InetSocketAddress rem) { 54 mLocSocketAddress = loc; 55 mRemSocketAddress = rem; 56 } 57 pack(ByteBuffer byteBuffer)58 public void pack(ByteBuffer byteBuffer) { 59 byteBuffer.order(BIG_ENDIAN); 60 byteBuffer.putShort((short) mLocSocketAddress.getPort()); 61 byteBuffer.putShort((short) mRemSocketAddress.getPort()); 62 byteBuffer.put(mLocSocketAddress.getAddress().getAddress()); 63 if (mLocSocketAddress.getAddress() instanceof Inet4Address) { 64 byteBuffer.put(IPV4_PADDING); 65 } 66 byteBuffer.put(mRemSocketAddress.getAddress().getAddress()); 67 if (mRemSocketAddress.getAddress() instanceof Inet4Address) { 68 byteBuffer.put(IPV4_PADDING); 69 } 70 byteBuffer.order(ByteOrder.nativeOrder()); 71 byteBuffer.putInt(0); 72 byteBuffer.put(INET_DIAG_NOCOOKIE); 73 } 74 75 @Override toString()76 public String toString() { 77 return "StructInetDiagSockId{ " 78 + "idiag_sport{" + mLocSocketAddress.getPort() + "}, " 79 + "idiag_dport{" + mRemSocketAddress.getPort() + "}, " 80 + "idiag_src{" + mLocSocketAddress.getAddress().getHostAddress() + "}, " 81 + "idiag_dst{" + mRemSocketAddress.getAddress().getHostAddress() + "}, " 82 + "idiag_if{" + 0 + "} " 83 + "idiag_cookie{INET_DIAG_NOCOOKIE}" 84 + "}"; 85 } 86 } 87