This repository has been archived by the owner on Mar 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 106
/
interface_linux.go
141 lines (125 loc) · 3.36 KB
/
interface_linux.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
package netLayer
import (
"bufio"
"bytes"
"encoding/binary"
"errors"
"fmt"
"io/ioutil"
"net"
"os"
"strconv"
"strings"
)
// https://github.com/jackpal/gateway/blob/master/gateway_parsers.go
func GetGateway() (ip net.IP, ifName string, err error) {
// See http://man7.org/linux/man-pages/man8/route.8.html
const file = "/proc/net/route"
var f *os.File
f, err = os.Open(file)
if err != nil {
err = fmt.Errorf("Can't access %s", file)
return
}
defer f.Close()
bytes, err := ioutil.ReadAll(f)
if err != nil {
err = fmt.Errorf("Can't read %s", file)
return
}
parsedStruct, err := parseToLinuxRouteStruct(bytes)
if err != nil {
return
}
ifName = parsedStruct.Iface
destinationHex := "0x" + parsedStruct.Destination
gatewayHex := "0x" + parsedStruct.Gateway
// cast hex address to uint32
d, err := strconv.ParseInt(gatewayHex, 0, 64)
if err != nil {
err = fmt.Errorf(
"parsing default interface address field hex '%s': %w",
destinationHex,
err,
)
return
}
// make net.IP address from uint32
ipd32 := make(net.IP, 4)
binary.LittleEndian.PutUint32(ipd32, uint32(d))
// format net.IP to dotted ipV4 string
ip = net.IP(ipd32)
return
}
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")
}
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
}