1package nl
2
3import (
4	"unsafe"
5
6	"golang.org/x/sys/unix"
7)
8
9type IfAddrmsg struct {
10	unix.IfAddrmsg
11}
12
13func 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
38func DeserializeIfAddrmsg(b []byte) *IfAddrmsg {
39	return (*IfAddrmsg)(unsafe.Pointer(&b[0:unix.SizeofIfAddrmsg][0]))
40}
41
42func (msg *IfAddrmsg) Serialize() []byte {
43	return (*(*[unix.SizeofIfAddrmsg]byte)(unsafe.Pointer(msg)))[:]
44}
45
46func (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
57const IFA_CACHEINFO = 6
58const SizeofIfaCacheInfo = 0x10
59
60type IfaCacheInfo struct {
61	IfaPrefered uint32
62	IfaValid    uint32
63	Cstamp      uint32
64	Tstamp      uint32
65}
66
67func (msg *IfaCacheInfo) Len() int {
68	return SizeofIfaCacheInfo
69}
70
71func DeserializeIfaCacheInfo(b []byte) *IfaCacheInfo {
72	return (*IfaCacheInfo)(unsafe.Pointer(&b[0:SizeofIfaCacheInfo][0]))
73}
74
75func (msg *IfaCacheInfo) Serialize() []byte {
76	return (*(*[SizeofIfaCacheInfo]byte)(unsafe.Pointer(msg)))[:]
77}
78