-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiproute2.go
135 lines (110 loc) · 3.32 KB
/
iproute2.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
iproute2 JSON parser
Copyright (C) 2025 SUSE LLC <[email protected]>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package main
import (
"encoding/json"
"fmt"
"strings"
)
type iproute2AddrInfo struct {
Family string `json:"family"`
Local string `json:"local"`
Prefixlen int `json:"prefixlen"`
Label string `json:"label"`
}
type iproute2SlaveData struct {
State string `json:"state"`
MiiStatus string `json:"mii_status"`
PermHwAddr string `json:"perm_hwaddr"`
}
type iproute2LinkInfo struct {
Kind string `json:"info_kind"`
DataRaw json.RawMessage `json:"info_data"`
Data interface{}
}
type iproute2LinkInfoDataBond struct {
Mode string `json:"mode"`
}
type iproute2LinkInfoDataBridge struct {
VlanProtocol string `json:"vlan_protocol"`
BridgeId string `json:"bridge_id"`
}
type iproute2LinkInfoDataVlan struct {
Protocol string `json:"protocol"`
Id int32 `json:"id"`
}
type ipRoute2Interface struct {
IfName string `json:"ifname"`
Mtu int32 `json:"mtu"`
OperState string `json:"operstate"`
LinkType string `json:"link_type"`
Address string `json:"address"`
AddrInfo []iproute2AddrInfo `json:"addr_info"`
LinkInfo iproute2LinkInfo `json:"linkinfo"`
}
type ipRoute2Interfaces []*ipRoute2Interface
func (infs ipRoute2Interfaces) String() string {
var out []string
for _, inf := range infs {
out = append(out, fmt.Sprintf("%s", *inf))
}
return strings.Join(out[:], "")
}
func parseIpRoute2AddressData(raw string) ipRoute2Interfaces {
if raw == "" {
return nil
}
// too old iproute2
if raw == "Option \"-j\" is unknown, try \"ip -help\"." {
return nil
}
data := ipRoute2Interfaces{}
err := json.Unmarshal([]byte(raw), &data)
handleError("Parsing interface JSON", err)
for _, inf := range data {
switch inf.LinkInfo.Kind {
case "bond":
inf.LinkInfo.Data = iproute2LinkInfoDataBond{}
case "bridge":
inf.LinkInfo.Data = iproute2LinkInfoDataBridge{}
case "vlan":
inf.LinkInfo.Data = iproute2LinkInfoDataVlan{}
case "":
continue
default:
Error("Unhandled kind %s", inf.LinkInfo.Kind)
continue
}
err = json.Unmarshal(inf.LinkInfo.DataRaw, &inf.LinkInfo.Data)
handleError("Parsing link data JSON", err)
}
Debug("Got data %+v", data)
return data
}
func convertInterfaces(in ipRoute2Interfaces) []linuxInterface {
out := make([]linuxInterface, len(in))
for _, iinf := range in {
linf := linuxInterface{}
linf.Name = iinf.IfName
if iinf.LinkType == "ether" {
linf.Type = 1
}
for _, addr := range iinf.AddrInfo {
linf.Addresses = append(linf.Addresses, fmt.Sprintf("%s/%d", addr.Local, addr.Prefixlen))
}
out = append(out, linf)
}
return out
}