This repository has been archived by the owner on Nov 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathinterfacesReader.go
213 lines (176 loc) · 4.56 KB
/
interfacesReader.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package netif
import (
"bufio"
"os"
"strings"
"github.com/n-marshall/fn"
)
// TODO get rid of interfaceReader
type InterfacesReader struct {
filePath string
adapters []*NetworkAdapter
autoList []string
hotplugList []string
context int
}
func Parse(opts ...fn.Option) *InterfaceSet {
fnConfig := fn.MakeConfig(
fn.Defaults{"path": "/etc/network/interfaces"},
opts,
)
path := fnConfig.GetString("path")
is := &InterfaceSet{
InterfacesPath: path,
}
is.Adapters = NewInterfacesReader(is.InterfacesPath).ParseInterfaces()
return is
}
func NewInterfacesReader(filePath string) *InterfacesReader {
ir := InterfacesReader{filePath: filePath}
ir.reset()
return &ir
}
func (ir *InterfacesReader) ParseInterfaces() []*NetworkAdapter {
// Reset this object in case is not new
ir.reset()
// Try to open the file
f, err := os.Open(ir.filePath)
if err != nil {
return nil
}
defer f.Close()
// Treat each line from the file
ir.readLinesFromFile(f)
return ir.parseInterfacesImplementation()
}
func (ir *InterfacesReader) parseInterfacesFromString(data string) {
// Reset this object in case is not new
ir.reset()
}
func (ir *InterfacesReader) parseInterfacesImplementation() []*NetworkAdapter {
// Save adapters and return them
// foreach iface in the auto list
for _, autoName := range ir.autoList {
for naIdx, _ := range ir.adapters {
if ir.adapters[naIdx].Name == autoName {
ir.adapters[naIdx].Auto = true
}
}
}
// foreach iface in the hotplug list
for _, hotplugName := range ir.hotplugList {
for naIdx, _ := range ir.adapters {
if ir.adapters[naIdx].Name == hotplugName {
ir.adapters[naIdx].Hotplug = true
}
}
}
return ir.adapters
}
func (ir *InterfacesReader) readLinesFromFile(file *os.File) bool {
s := bufio.NewScanner(file)
//var a Adapter
for s.Scan() {
line := s.Text()
// Identify the clauses by analyzing the first word of each line.
// Go to the next line if the current line is a comment.
if strings.HasPrefix(strings.TrimSpace(line), "#") {
continue
}
// Continue if line is empty
if len(strings.TrimSpace(line)) == 0 {
continue
}
// Parse the line
ir.parseIface(line)
ir.parseDetails(line)
ir.readAuto(line)
ir.readHotplug(line)
}
return false
}
func (ir *InterfacesReader) parseIface(line string) {
if !strings.HasPrefix(line, "iface") {
return
}
sline := strings.Split(strings.TrimSpace(line), " ")
ir.adapters = append(ir.adapters, &NetworkAdapter{Name: sline[1]})
ir.context++
// Parse and set the address source
src, err := ir.adapters[ir.context].ParseAddressSource(sline[len(sline)-1])
if err != nil {
panic(err)
}
ir.adapters[ir.context].AddrSource = src
// Parse and set the address family
fam, err := ir.adapters[ir.context].ParseAddressFamily(sline[2])
if err == nil {
ir.adapters[ir.context].AddrFamily = fam
}
}
func (ir *InterfacesReader) parseDetails(line string) {
// If line begins with a space, it's a interface attribute
if strings.TrimSpace(line)[0] == line[0] {
// Doesn't begin with space, pass
return
}
sline := strings.Split(strings.TrimSpace(line), " ")
na := ir.adapters[ir.context]
switch sline[0] {
case "address":
if na.SetAddress(sline[1]) != nil {
return
}
case "netmask":
if na.SetNetmask(sline[1]) != nil {
return
}
case "gateway":
if na.SetGateway(sline[1]) != nil {
return
}
case "broadcast":
if na.SetBroadcast(sline[1]) != nil {
return
}
case "network":
if na.SetNetwork(sline[1]) != nil {
return
}
default:
}
}
func (ir *InterfacesReader) readWord(line string, word string) (bool, string) {
// Isolate the second value after a matching word on the given line
if strings.HasPrefix(line, word) {
sline := strings.Split(strings.TrimSpace(line), " ")
for _, s := range sline {
if s != word {
return true, s
}
}
}
return false, ""
}
func (ir *InterfacesReader) readAuto(line string) {
// Identify which adapters are flagged auto
if ok, iface := ir.readWord(line, "auto"); ok {
ir.autoList = append(ir.autoList, iface)
}
}
func (ir *InterfacesReader) readHotplug(line string) {
// Identify which adapters are flagged allow-hotplug
if ok, iface := ir.readWord(line, "allow-hotplug"); ok {
ir.hotplugList = append(ir.hotplugList, iface)
}
}
func (ir *InterfacesReader) reset() {
// Initialize a place to store create NetworkAdapter objects
ir.adapters = nil
// Keep a list of adapters that have the auto or allow-hotplug flags set.
ir.autoList = nil
ir.hotplugList = nil
// Store the interface context.
// This is the index of the adapters collection.
ir.context = -1
}