• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 java.nio.ByteBuffer;
20 
21 /**
22  * struct nlmsgerr
23  *
24  * see <linux_src>/include/uapi/linux/netlink.h
25  *
26  * @hide
27  */
28 public class StructNlMsgErr {
29     public static final int STRUCT_SIZE = Integer.BYTES + StructNlMsgHdr.STRUCT_SIZE;
30 
hasAvailableSpace(ByteBuffer byteBuffer)31     private static boolean hasAvailableSpace(ByteBuffer byteBuffer) {
32         return byteBuffer != null && byteBuffer.remaining() >= STRUCT_SIZE;
33     }
34 
35     /**
36      * Parse a netlink error message payload from a {@link ByteBuffer}.
37      *
38      * @param byteBuffer The buffer from which to parse the netlink error message payload.
39      * @return the parsed netlink error message payload, or {@code null} if the netlink error
40      *         message payload could not be parsed successfully (for example, if it was truncated).
41      */
parse(ByteBuffer byteBuffer)42     public static StructNlMsgErr parse(ByteBuffer byteBuffer) {
43         if (!hasAvailableSpace(byteBuffer)) return null;
44 
45         // The ByteOrder must have already been set by the caller.  In most
46         // cases ByteOrder.nativeOrder() is correct, with the exception
47         // of usage within unittests.
48         final StructNlMsgErr struct = new StructNlMsgErr();
49         struct.error = byteBuffer.getInt();
50         struct.msg = StructNlMsgHdr.parse(byteBuffer);
51         return struct;
52     }
53 
54     public int error;
55     public StructNlMsgHdr msg;
56 
57     /**
58      * Write the netlink error message payload to {@link ByteBuffer}.
59      */
pack(ByteBuffer byteBuffer)60     public void pack(ByteBuffer byteBuffer) {
61         // The ByteOrder must have already been set by the caller.  In most
62         // cases ByteOrder.nativeOrder() is correct, with the possible
63         // exception of usage within unittests.
64         byteBuffer.putInt(error);
65         if (msg != null) {
66             msg.pack(byteBuffer);
67         }
68     }
69 
70     @Override
toString()71     public String toString() {
72         return "StructNlMsgErr{ "
73                 + "error{" + error + "}, "
74                 + "msg{" + (msg == null ? "" : msg.toString()) + "} "
75                 + "}";
76     }
77 }
78