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 java.nio.ByteBuffer; 20 21 22 /** 23 * struct nlmsgerr 24 * 25 * see <linux_src>/include/uapi/linux/netlink.h 26 * 27 * @hide 28 */ 29 public class StructNlMsgErr { 30 public static final int STRUCT_SIZE = Integer.BYTES + StructNlMsgHdr.STRUCT_SIZE; 31 hasAvailableSpace(ByteBuffer byteBuffer)32 public static boolean hasAvailableSpace(ByteBuffer byteBuffer) { 33 return byteBuffer != null && byteBuffer.remaining() >= STRUCT_SIZE; 34 } 35 parse(ByteBuffer byteBuffer)36 public static StructNlMsgErr parse(ByteBuffer byteBuffer) { 37 if (!hasAvailableSpace(byteBuffer)) { return null; } 38 39 // The ByteOrder must have already been set by the caller. In most 40 // cases ByteOrder.nativeOrder() is correct, with the exception 41 // of usage within unittests. 42 final StructNlMsgErr struct = new StructNlMsgErr(); 43 struct.error = byteBuffer.getInt(); 44 struct.msg = StructNlMsgHdr.parse(byteBuffer); 45 return struct; 46 } 47 48 public int error; 49 public StructNlMsgHdr msg; 50 pack(ByteBuffer byteBuffer)51 public void pack(ByteBuffer byteBuffer) { 52 // The ByteOrder must have already been set by the caller. In most 53 // cases ByteOrder.nativeOrder() is correct, with the possible 54 // exception of usage within unittests. 55 byteBuffer.putInt(error); 56 if (msg != null) { 57 msg.pack(byteBuffer); 58 } 59 } 60 61 @Override toString()62 public String toString() { 63 return "StructNlMsgErr{ " 64 + "error{" + error + "}, " 65 + "msg{" + (msg == null ? "" : msg.toString()) + "} " 66 + "}"; 67 } 68 } 69