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 androidx.annotation.NonNull;
20 import androidx.annotation.Nullable;
21 
22 import java.nio.ByteBuffer;
23 
24 /**
25  * A NetlinkMessage subclass for netlink error messages.
26  *
27  * @hide
28  */
29 public class NetlinkErrorMessage extends NetlinkMessage {
30 
31     /**
32      * Parse a netlink error message from a {@link ByteBuffer}.
33      *
34      * @param byteBuffer The buffer from which to parse the netlink error message.
35      * @return the parsed netlink error message, or {@code null} if the netlink error message
36      *         could not be parsed successfully (for example, if it was truncated).
37      */
38     @Nullable
parse(@onNull StructNlMsgHdr header, @NonNull ByteBuffer byteBuffer)39     public static NetlinkErrorMessage parse(@NonNull StructNlMsgHdr header,
40             @NonNull ByteBuffer byteBuffer) {
41         final NetlinkErrorMessage errorMsg = new NetlinkErrorMessage(header);
42 
43         errorMsg.mNlMsgErr = StructNlMsgErr.parse(byteBuffer);
44         if (errorMsg.mNlMsgErr == null) {
45             return null;
46         }
47 
48         return errorMsg;
49     }
50 
51     private StructNlMsgErr mNlMsgErr;
52 
NetlinkErrorMessage(@onNull StructNlMsgHdr header)53     NetlinkErrorMessage(@NonNull StructNlMsgHdr header) {
54         super(header);
55         mNlMsgErr = null;
56     }
57 
getNlMsgError()58     public StructNlMsgErr getNlMsgError() {
59         return mNlMsgErr;
60     }
61 
62     @Override
toString()63     public String toString() {
64         return "NetlinkErrorMessage{ "
65                 + "nlmsghdr{" + (mHeader == null ? "" : mHeader.toString()) + "}, "
66                 + "nlmsgerr{" + (mNlMsgErr == null ? "" : mNlMsgErr.toString()) + "} "
67                 + "}";
68     }
69 }
70