diff options
author | Xiao Ma <xiaom@google.com> | 2021-07-01 13:57:02 +0000 |
---|---|---|
committer | Xiao Ma <xiaom@google.com> | 2021-07-29 03:52:09 +0000 |
commit | a5abbe652945803803a0fe1985a7cb9712392302 (patch) | |
tree | 66172ef37e240fb91aa7c93a525b155688f46eea /common/device/com/android/net/module/util/netlink/StructNlMsgErr.java | |
parent | 5d39cb6ea74958c60b72e81ea5052e8a82928177 (diff) | |
download | net-a5abbe652945803803a0fe1985a7cb9712392302.tar.gz |
Migrate netlink-client to net-utils-device-common-netlink.
Move netlink stuff to frameworks/libs/net/common/device, and build the
source files as an individual libraray. NetworkStack module just depends
on the net-utils-device-common-netlink.
Besides, also fix the incorrect format detected by checkstyle_hook script
such as missing java doc and make some public function as private, rename
the variable and etc.
Bug: 192535368
Test: atest NetworkStaticlibTests
Change-Id: I00e7f30be1bc9ebc2e24d7cd53efc403d6ba3daa
Diffstat (limited to 'common/device/com/android/net/module/util/netlink/StructNlMsgErr.java')
-rw-r--r-- | common/device/com/android/net/module/util/netlink/StructNlMsgErr.java | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/common/device/com/android/net/module/util/netlink/StructNlMsgErr.java b/common/device/com/android/net/module/util/netlink/StructNlMsgErr.java new file mode 100644 index 00000000..b6620f3b --- /dev/null +++ b/common/device/com/android/net/module/util/netlink/StructNlMsgErr.java @@ -0,0 +1,77 @@ +/* + * Copyright (C) 2019 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.net.module.util.netlink; + +import java.nio.ByteBuffer; + +/** + * struct nlmsgerr + * + * see <linux_src>/include/uapi/linux/netlink.h + * + * @hide + */ +public class StructNlMsgErr { + public static final int STRUCT_SIZE = Integer.BYTES + StructNlMsgHdr.STRUCT_SIZE; + + private static boolean hasAvailableSpace(ByteBuffer byteBuffer) { + return byteBuffer != null && byteBuffer.remaining() >= STRUCT_SIZE; + } + + /** + * Parse a netlink error message payload from a {@link ByteBuffer}. + * + * @param byteBuffer The buffer from which to parse the netlink error message payload. + * @return the parsed netlink error message payload, or {@code null} if the netlink error + * message payload could not be parsed successfully (for example, if it was truncated). + */ + public static StructNlMsgErr parse(ByteBuffer byteBuffer) { + if (!hasAvailableSpace(byteBuffer)) return null; + + // The ByteOrder must have already been set by the caller. In most + // cases ByteOrder.nativeOrder() is correct, with the exception + // of usage within unittests. + final StructNlMsgErr struct = new StructNlMsgErr(); + struct.error = byteBuffer.getInt(); + struct.msg = StructNlMsgHdr.parse(byteBuffer); + return struct; + } + + public int error; + public StructNlMsgHdr msg; + + /** + * Write the netlink error message payload to {@link ByteBuffer}. + */ + public void pack(ByteBuffer byteBuffer) { + // The ByteOrder must have already been set by the caller. In most + // cases ByteOrder.nativeOrder() is correct, with the possible + // exception of usage within unittests. + byteBuffer.putInt(error); + if (msg != null) { + msg.pack(byteBuffer); + } + } + + @Override + public String toString() { + return "StructNlMsgErr{ " + + "error{" + error + "}, " + + "msg{" + (msg == null ? "" : msg.toString()) + "} " + + "}"; + } +} |