-
Notifications
You must be signed in to change notification settings - Fork 4
/
show_ifaddr.go
99 lines (90 loc) · 2.23 KB
/
show_ifaddr.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Copyright © 2015-2016 Platina Systems, Inc. All rights reserved.
// Use of this source code is governed by the GPL-2 license described in the
// LICENSE file.
package options
import (
"fmt"
"net"
"github.com/platinasystems/goes/internal/nl"
"github.com/platinasystems/goes/internal/nl/rtnl"
)
func (opt *Options) ShowIfAddr(b []byte, withCacheInfo bool) {
var ifa rtnl.Ifa
var ifaf uint32
ifa.Write(b)
msg := rtnl.IfAddrMsgPtr(b)
if val := ifa[rtnl.IFA_FLAGS]; len(val) > 0 {
ifaf = nl.Uint32(val)
} else {
ifaf = uint32(msg.Flags)
}
opt.Print(rtnl.AfName(msg.Family), " ")
ip := net.IP(ifa[rtnl.IFA_ADDRESS])
opt.Print(ip, "/", msg.Prefixlen)
opt.Print(" scope ", rtnl.RtScopeName[msg.Scope])
if (ifaf & uint32(rtnl.IFA_F_SECONDARY)) ==
uint32(rtnl.IFA_F_SECONDARY) {
if msg.Family == rtnl.AF_INET {
opt.Print(" secondary")
} else {
opt.Print(" temporary")
}
}
for _, x := range []struct {
not bool
flag uint32
name string
}{
{false, uint32(rtnl.IFA_F_TENTATIVE), "tentative"},
{false, uint32(rtnl.IFA_F_DEPRECATED), "deprecated"},
{false, uint32(rtnl.IFA_F_HOMEADDRESS), "home"},
{false, uint32(rtnl.IFA_F_NODAD), "nodad"},
{false, uint32(rtnl.IFA_F_MANAGETEMPADDR), "mngtmpaddr"},
{false, uint32(rtnl.IFA_F_NOPREFIXROUTE), "noprefixroute"},
{false, uint32(rtnl.IFA_F_MCAUTOJOIN), "autojoin"},
{true, uint32(rtnl.IFA_F_PERMANENT), "dynamic"},
{false, uint32(rtnl.IFA_F_DADFAILED), "dadfailed"},
} {
if x.not {
if (ifaf & x.flag) != x.flag {
opt.Print(" ", x.name)
}
} else if (ifaf & x.flag) == x.flag {
opt.Print(" ", x.name)
}
ifaf &= ^x.flag
}
if ifaf != 0 {
fmt.Printf(" flags %#x", ifaf)
}
if val := ifa[rtnl.IFA_LABEL]; len(val) > 0 {
opt.Print(" ", nl.Kstring(val))
}
if withCacheInfo {
ci := rtnl.IfaCacheInfoPtr(ifa[rtnl.IFA_CACHEINFO])
if ci != nil {
opt.Println()
opt.Nprint(7)
opt.showIfaCacheInfo(ci)
}
}
}
func (opt *Options) showIfaCacheInfo(ci *rtnl.IfaCacheInfo) {
for i, x := range []struct {
name string
lft uint32
}{
{"valid", ci.Valid},
{"preferred", ci.Prefered},
} {
if i > 0 {
opt.Print(" ")
}
opt.Print(x.name, "_lft ")
if x.lft == ^uint32(0) {
opt.Print("forever")
} else {
opt.Print(x.lft)
}
}
}