github.com/sagernet/netlink@v0.0.0-20240612041022-b9a21c07ac6a/nl/addr_linux.go (about)

     1  package nl
     2  
     3  import (
     4  	"unsafe"
     5  
     6  	"golang.org/x/sys/unix"
     7  )
     8  
     9  type IfAddrmsg struct {
    10  	unix.IfAddrmsg
    11  }
    12  
    13  func NewIfAddrmsg(family int) *IfAddrmsg {
    14  	return &IfAddrmsg{
    15  		IfAddrmsg: unix.IfAddrmsg{
    16  			Family: uint8(family),
    17  		},
    18  	}
    19  }
    20  
    21  // struct ifaddrmsg {
    22  //   __u8    ifa_family;
    23  //   __u8    ifa_prefixlen;  /* The prefix length    */
    24  //   __u8    ifa_flags;  /* Flags      */
    25  //   __u8    ifa_scope;  /* Address scope    */
    26  //   __u32   ifa_index;  /* Link index     */
    27  // };
    28  
    29  // type IfAddrmsg struct {
    30  // 	Family    uint8
    31  // 	Prefixlen uint8
    32  // 	Flags     uint8
    33  // 	Scope     uint8
    34  // 	Index     uint32
    35  // }
    36  // SizeofIfAddrmsg     = 0x8
    37  
    38  func DeserializeIfAddrmsg(b []byte) *IfAddrmsg {
    39  	return (*IfAddrmsg)(unsafe.Pointer(&b[0:unix.SizeofIfAddrmsg][0]))
    40  }
    41  
    42  func (msg *IfAddrmsg) Serialize() []byte {
    43  	return (*(*[unix.SizeofIfAddrmsg]byte)(unsafe.Pointer(msg)))[:]
    44  }
    45  
    46  func (msg *IfAddrmsg) Len() int {
    47  	return unix.SizeofIfAddrmsg
    48  }
    49  
    50  // struct ifa_cacheinfo {
    51  // 	__u32	ifa_prefered;
    52  // 	__u32	ifa_valid;
    53  // 	__u32	cstamp; /* created timestamp, hundredths of seconds */
    54  // 	__u32	tstamp; /* updated timestamp, hundredths of seconds */
    55  // };
    56  
    57  type IfaCacheInfo struct {
    58  	unix.IfaCacheinfo
    59  }
    60  
    61  func (msg *IfaCacheInfo) Len() int {
    62  	return unix.SizeofIfaCacheinfo
    63  }
    64  
    65  func DeserializeIfaCacheInfo(b []byte) *IfaCacheInfo {
    66  	return (*IfaCacheInfo)(unsafe.Pointer(&b[0:unix.SizeofIfaCacheinfo][0]))
    67  }
    68  
    69  func (msg *IfaCacheInfo) Serialize() []byte {
    70  	return (*(*[unix.SizeofIfaCacheinfo]byte)(unsafe.Pointer(msg)))[:]
    71  }