forked from jackpal/gateway
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gateway_parsers.go
276 lines (247 loc) · 7.35 KB
/
gateway_parsers.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package gateway
import (
"bufio"
"bytes"
"encoding/binary"
"errors"
"fmt"
"net"
"strconv"
"strings"
)
type windowsRouteStruct struct {
Destination string
Netmask string
Gateway string
Interface string
Metric string
}
type linuxRouteStruct struct {
Iface string
Destination string
Gateway string
Flags string
RefCnt string
Use string
Metric string
Mask string
MTU string
Window string
IRTT string
}
func parseToWindowsRouteStruct(output []byte) (windowsRouteStruct, error) {
// Windows route output format is always like this:
// ===========================================================================
// Interface List
// 8 ...00 12 3f a7 17 ba ...... Intel(R) PRO/100 VE Network Connection
// 1 ........................... Software Loopback Interface 1
// ===========================================================================
// IPv4 Route Table
// ===========================================================================
// Active Routes:
// Network Destination Netmask Gateway Interface Metric
// 0.0.0.0 0.0.0.0 192.168.1.1 192.168.1.100 20
// ===========================================================================
//
// Windows commands are localized, so we can't just look for "Active Routes:" string
// I'm trying to pick the active route,
// then jump 2 lines and get the row
// Not using regex because output is quite standard from Windows XP to 8 (NEEDS TESTING)
lines := strings.Split(string(output), "\n")
sep := 0
for idx, line := range lines {
if sep == 3 {
// We just entered the 2nd section containing "Active Routes:"
if len(lines) <= idx+2 {
return windowsRouteStruct{}, errNoGateway
}
fields := strings.Fields(lines[idx+2])
if len(fields) < 5 {
return windowsRouteStruct{}, errCantParse
}
return windowsRouteStruct{
Destination: fields[0],
Netmask: fields[1],
Gateway: fields[2],
Interface: fields[3],
Metric: fields[4],
}, nil
}
if strings.HasPrefix(line, "=======") {
sep++
continue
}
}
return windowsRouteStruct{}, errNoGateway
}
func parseToLinuxRouteStruct(output []byte) (linuxRouteStruct, error) {
// parseLinuxProcNetRoute parses the route file located at /proc/net/route
// and returns the IP address of the default gateway. The default gateway
// is the one with Destination value of 0.0.0.0.
//
// The Linux route file has the following format:
//
// $ cat /proc/net/route
//
// Iface Destination Gateway Flags RefCnt Use Metric Mask
// eno1 00000000 C900A8C0 0003 0 0 100 00000000 0 00
// eno1 0000A8C0 00000000 0001 0 0 100 00FFFFFF 0 00
const (
sep = "\t" // field separator
destinationField = 1 // field containing hex destination address
gatewayField = 2 // field containing hex gateway address
)
scanner := bufio.NewScanner(bytes.NewReader(output))
// Skip header line
if !scanner.Scan() {
return linuxRouteStruct{}, errors.New("Invalid linux route file")
}
for scanner.Scan() {
row := scanner.Text()
tokens := strings.Split(row, sep)
if len(tokens) < 11 {
return linuxRouteStruct{}, fmt.Errorf("invalid row '%s' in route file: doesn't have 11 fields", row)
}
// Cast hex destination address to int
destinationHex := "0x" + tokens[destinationField]
destination, err := strconv.ParseInt(destinationHex, 0, 64)
if err != nil {
return linuxRouteStruct{}, fmt.Errorf(
"parsing destination field hex '%s' in row '%s': %w",
destinationHex,
row,
err,
)
}
// The default interface is the one that's 0
if destination != 0 {
continue
}
return linuxRouteStruct{
Iface: tokens[0],
Destination: tokens[1],
Gateway: tokens[2],
Flags: tokens[3],
RefCnt: tokens[4],
Use: tokens[5],
Metric: tokens[6],
Mask: tokens[7],
MTU: tokens[8],
Window: tokens[9],
IRTT: tokens[10],
}, nil
}
return linuxRouteStruct{}, errors.New("interface with default destination not found")
}
func parseWindowsGatewayIP(output []byte) (net.IP, error) {
parsedOutput, err := parseToWindowsRouteStruct(output)
if err != nil {
return nil, err
}
ip := net.ParseIP(parsedOutput.Gateway)
if ip == nil {
return nil, errCantParse
}
return ip, nil
}
func parseWindowsInterfaceIP(output []byte) (net.IP, error) {
parsedOutput, err := parseToWindowsRouteStruct(output)
if err != nil {
return nil, err
}
ip := net.ParseIP(parsedOutput.Interface)
if ip == nil {
return nil, errCantParse
}
return ip, nil
}
func parseLinuxGatewayIP(output []byte) (net.IP, error) {
parsedStruct, err := parseToLinuxRouteStruct(output)
if err != nil {
return nil, err
}
destinationHex := "0x" + parsedStruct.Destination
gatewayHex := "0x" + parsedStruct.Gateway
// cast hex address to uint32
d, err := strconv.ParseInt(gatewayHex, 0, 64)
if err != nil {
return nil, fmt.Errorf(
"parsing default interface address field hex '%s': %w",
destinationHex,
err,
)
}
// make net.IP address from uint32
ipd32 := make(net.IP, 4)
binary.LittleEndian.PutUint32(ipd32, uint32(d))
// format net.IP to dotted ipV4 string
return net.IP(ipd32), nil
}
func parseLinuxInterfaceIP(output []byte) (*net.IPNet, error) {
parsedStruct, err := parseToLinuxRouteStruct(output)
if err != nil {
return nil, err
}
iface, err := net.InterfaceByName(parsedStruct.Iface)
if err != nil {
return nil, err
}
addrs, err := iface.Addrs()
if err != nil {
return nil, err
}
// split when its 192.168.8.8/24
_, ipNet, err := net.ParseCIDR(addrs[0].String())
if err != nil {
return nil, err
}
return ipNet, nil
}
func parseDarwinRouteGet(output []byte) (net.IP, error) {
// Darwin route out format is always like this:
// route to: default
// destination: default
// mask: default
// gateway: 192.168.1.1
lines := strings.Split(string(output), "\n")
for _, line := range lines {
fields := strings.Fields(line)
if len(fields) >= 2 && fields[0] == "gateway:" {
ip := net.ParseIP(fields[1])
if ip != nil {
return ip, nil
}
}
}
return nil, errNoGateway
}
func parseBSDSolarisNetstat(output []byte) (net.IP, error) {
// netstat -rn produces the following on FreeBSD:
// Routing tables
//
// Internet:
// Destination Gateway Flags Netif Expire
// default 10.88.88.2 UGS em0
// 10.88.88.0/24 link#1 U em0
// 10.88.88.148 link#1 UHS lo0
// 127.0.0.1 link#2 UH lo0
//
// Internet6:
// Destination Gateway Flags Netif Expire
// ::/96 ::1 UGRS lo0
// ::1 link#2 UH lo0
// ::ffff:0.0.0.0/96 ::1 UGRS lo0
// fe80::/10 ::1 UGRS lo0
// ...
outputLines := strings.Split(string(output), "\n")
for _, line := range outputLines {
fields := strings.Fields(line)
if len(fields) >= 2 && fields[0] == "default" {
ip := net.ParseIP(fields[1])
if ip != nil {
return ip, nil
}
}
}
return nil, errNoGateway
}