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 21 import com.android.net.module.util.Struct; 22 import com.android.net.module.util.Struct.Field; 23 import com.android.net.module.util.Struct.Type; 24 25 import java.nio.ByteBuffer; 26 27 /** 28 * struct prefix_cacheinfo { 29 * __u32 preferred_time; 30 * __u32 valid_time; 31 * } 32 * 33 * see also: 34 * 35 * include/uapi/linux/if_addr.h 36 * 37 * @hide 38 */ 39 public class StructPrefixCacheInfo extends Struct { 40 public static final int STRUCT_SIZE = 8; 41 42 @Field(order = 0, type = Type.U32) 43 public final long preferred_time; 44 @Field(order = 1, type = Type.U32) 45 public final long valid_time; 46 StructPrefixCacheInfo(long preferred, long valid)47 StructPrefixCacheInfo(long preferred, long valid) { 48 this.preferred_time = preferred; 49 this.valid_time = valid; 50 } 51 52 /** 53 * Parse a prefix_cacheinfo struct from a {@link ByteBuffer}. 54 * 55 * @param byteBuffer The buffer from which to parse the prefix_cacheinfo. 56 * @return the parsed prefix_cacheinfo struct, or throw IllegalArgumentException if the 57 * prefix_cacheinfo struct could not be parsed successfully(for example, if it was 58 * truncated). 59 */ parse(@onNull final ByteBuffer byteBuffer)60 public static StructPrefixCacheInfo parse(@NonNull final ByteBuffer byteBuffer) { 61 if (byteBuffer.remaining() < STRUCT_SIZE) { 62 throw new IllegalArgumentException("Invalid bytebuffer remaining size " 63 + byteBuffer.remaining() + " for prefix_cacheinfo attribute"); 64 } 65 66 // The ByteOrder must already have been set to native order. 67 return Struct.parse(StructPrefixCacheInfo.class, byteBuffer); 68 } 69 70 /** 71 * Write a prefix_cacheinfo struct to {@link ByteBuffer}. 72 */ pack(@onNull final ByteBuffer byteBuffer)73 public void pack(@NonNull final ByteBuffer byteBuffer) { 74 // The ByteOrder must already have been set to native order. 75 writeToByteBuffer(byteBuffer); 76 } 77 } 78