1 /* 2 * Copyright (C) 2024 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.VisibleForTesting; 21 22 import com.android.net.module.util.Struct; 23 import com.android.net.module.util.Struct.Field; 24 import com.android.net.module.util.Struct.Type; 25 26 import java.nio.ByteBuffer; 27 28 /** 29 * struct prefixmsg { 30 * unsigned char prefix_family; 31 * unsigned char prefix_pad1; 32 * unsigned short prefix_pad2; 33 * int prefix_ifindex; 34 * unsigned char prefix_type; 35 * unsigned char prefix_len; 36 * unsigned char prefix_flags; 37 * unsigned char prefix_pad3; 38 * } 39 * 40 * see also: 41 * 42 * include/uapi/linux/rtnetlink.h 43 * 44 * @hide 45 */ 46 public class StructPrefixMsg extends Struct { 47 // Already aligned. 48 public static final int STRUCT_SIZE = 12; 49 50 @Field(order = 0, type = Type.U8, padding = 3) 51 public final short prefix_family; 52 @Field(order = 1, type = Type.S32) 53 public final int prefix_ifindex; 54 @Field(order = 2, type = Type.U8) 55 public final short prefix_type; 56 @Field(order = 3, type = Type.U8) 57 public final short prefix_len; 58 @Field(order = 4, type = Type.U8, padding = 1) 59 public final short prefix_flags; 60 61 @VisibleForTesting StructPrefixMsg(short family, int ifindex, short type, short len, short flags)62 public StructPrefixMsg(short family, int ifindex, short type, short len, short flags) { 63 this.prefix_family = family; 64 this.prefix_ifindex = ifindex; 65 this.prefix_type = type; 66 this.prefix_len = len; 67 this.prefix_flags = flags; 68 } 69 70 /** 71 * Parse a prefixmsg struct from a {@link ByteBuffer}. 72 * 73 * @param byteBuffer The buffer from which to parse the prefixmsg. 74 * @return the parsed prefixmsg struct, or throw IllegalArgumentException if the prefixmsg 75 * struct could not be parsed successfully (for example, if it was truncated). 76 */ parse(@onNull final ByteBuffer byteBuffer)77 public static StructPrefixMsg parse(@NonNull final ByteBuffer byteBuffer) { 78 if (byteBuffer.remaining() < STRUCT_SIZE) { 79 throw new IllegalArgumentException("Invalid bytebuffer remaining size " 80 + byteBuffer.remaining() + "for prefix_msg struct."); 81 } 82 83 // The ByteOrder must already have been set to native order. 84 return Struct.parse(StructPrefixMsg.class, byteBuffer); 85 } 86 87 /** 88 * Write a prefixmsg struct to {@link ByteBuffer}. 89 */ pack(@onNull final ByteBuffer byteBuffer)90 public void pack(@NonNull final ByteBuffer byteBuffer) { 91 // The ByteOrder must already have been set to native order. 92 writeToByteBuffer(byteBuffer); 93 } 94 } 95