blob: 5da19a2e7f49a9b4de40713936497d697916dac1 [file] [log] [blame]
Erik Kline6193aa32015-01-20 12:28:26 +09001/*
2 * Copyright (C) 2015 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
17package android.net.netlink;
18
19import android.net.netlink.NetlinkConstants;
20import android.net.netlink.StructNlMsgHdr;
21import libcore.io.SizeOf;
22
23import java.nio.ByteBuffer;
24
25
26/**
27 * struct nlmsgerr
28 *
29 * see <linux_src>/include/uapi/linux/netlink.h
30 *
31 * @hide
32 */
33public class StructNlMsgErr {
34 public static final int STRUCT_SIZE = SizeOf.INT + StructNlMsgHdr.STRUCT_SIZE;
35
36 public static boolean hasAvailableSpace(ByteBuffer byteBuffer) {
37 return byteBuffer != null && byteBuffer.remaining() >= STRUCT_SIZE;
38 }
39
40 public static StructNlMsgErr parse(ByteBuffer byteBuffer) {
41 if (!hasAvailableSpace(byteBuffer)) { return null; }
42
43 // The ByteOrder must have already been set by the caller. In most
44 // cases ByteOrder.nativeOrder() is correct, with the exception
45 // of usage within unittests.
46 final StructNlMsgErr struct = new StructNlMsgErr();
47 struct.error = byteBuffer.getInt();
48 struct.msg = StructNlMsgHdr.parse(byteBuffer);
49 return struct;
50 }
51
52 public int error;
53 public StructNlMsgHdr msg;
54
55 public StructNlMsgErr() {
56 error = 0;
57 msg = null;
58 }
59
60 public boolean pack(ByteBuffer byteBuffer) {
61 if (!hasAvailableSpace(byteBuffer)) { return false; }
62
63 // The ByteOrder must have already been set by the caller. In most
64 // cases ByteOrder.nativeOrder() is correct, with the possible
65 // exception of usage within unittests.
66 byteBuffer.putInt(error);
67 if (msg != null) {
68 msg.pack(byteBuffer);
69 }
70 return true;
71 }
72
73 @Override
74 public String toString() {
75 return "StructNlMsgErr{ "
76 + "error{" + error + "}, "
77 + "msg{" + (msg == null ? "" : msg.toString()) + "} "
78 + "}";
79 }
80}