Home | History | Annotate | Download | only in netlink
      1 /*
      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 
     17 package android.net.netlink;
     18 
     19 import android.net.netlink.NetlinkConstants;
     20 import android.net.netlink.StructNlMsgHdr;
     21 import libcore.io.SizeOf;
     22 
     23 import java.nio.ByteBuffer;
     24 
     25 
     26 /**
     27  * struct nlmsgerr
     28  *
     29  * see <linux_src>/include/uapi/linux/netlink.h
     30  *
     31  * @hide
     32  */
     33 public 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 void pack(ByteBuffer byteBuffer) {
     56         // The ByteOrder must have already been set by the caller.  In most
     57         // cases ByteOrder.nativeOrder() is correct, with the possible
     58         // exception of usage within unittests.
     59         byteBuffer.putInt(error);
     60         if (msg != null) {
     61             msg.pack(byteBuffer);
     62         }
     63     }
     64 
     65     @Override
     66     public String toString() {
     67         return "StructNlMsgErr{ "
     68                 + "error{" + error + "}, "
     69                 + "msg{" + (msg == null ? "" : msg.toString()) + "} "
     70                 + "}";
     71     }
     72 }
     73