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
|
// Copyright 2016 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package net
import (
"errors"
"internal/itoa"
"os"
)
// If the ifindex is zero, interfaceTable returns mappings of all
// network interfaces. Otherwise it returns a mapping of a specific
// interface.
func interfaceTable(ifindex int) ([]Interface, error) {
if ifindex == 0 {
n, err := interfaceCount()
if err != nil {
return nil, err
}
ifcs := make([]Interface, n)
for i := range ifcs {
ifc, err := readInterface(i)
if err != nil {
return nil, err
}
ifcs[i] = *ifc
}
return ifcs, nil
}
ifc, err := readInterface(ifindex - 1)
if err != nil {
return nil, err
}
return []Interface{*ifc}, nil
}
func readInterface(i int) (*Interface, error) {
ifc := &Interface{
Index: i + 1, // Offset the index by one to suit the contract
Name: netdir + "/ipifc/" + itoa.Itoa(i), // Name is the full path to the interface path in plan9
}
ifcstat := ifc.Name + "/status"
ifcstatf, err := open(ifcstat)
if err != nil {
return nil, err
}
defer ifcstatf.close()
line, ok := ifcstatf.readLine()
if !ok {
return nil, errors.New("invalid interface status file: " + ifcstat)
}
fields := getFields(line)
if len(fields) < 4 {
return nil, errors.New("invalid interface status file: " + ifcstat)
}
device := fields[1]
mtustr := fields[3]
mtu, _, ok := dtoi(mtustr)
if !ok {
return nil, errors.New("invalid status file of interface: " + ifcstat)
}
ifc.MTU = mtu
// Not a loopback device ("/dev/null") or packet interface (e.g. "pkt2")
if stringsHasPrefix(device, netdir+"/") {
deviceaddrf, err := open(device + "/addr")
if err != nil {
return nil, err
}
defer deviceaddrf.close()
line, ok = deviceaddrf.readLine()
if !ok {
return nil, errors.New("invalid address file for interface: " + device + "/addr")
}
if len(line) > 0 && len(line)%2 == 0 {
ifc.HardwareAddr = make([]byte, len(line)/2)
var ok bool
for i := range ifc.HardwareAddr {
j := (i + 1) * 2
ifc.HardwareAddr[i], ok = xtoi2(line[i*2:j], 0)
if !ok {
ifc.HardwareAddr = ifc.HardwareAddr[:i]
break
}
}
}
ifc.Flags = FlagUp | FlagRunning | FlagBroadcast | FlagMulticast
} else {
ifc.Flags = FlagUp | FlagRunning | FlagMulticast | FlagLoopback
}
return ifc, nil
}
func interfaceCount() (int, error) {
d, err := os.Open(netdir + "/ipifc")
if err != nil {
return -1, err
}
defer d.Close()
names, err := d.Readdirnames(0)
if err != nil {
return -1, err
}
// Assumes that numbered files in ipifc are strictly
// the incrementing numbered directories for the
// interfaces
c := 0
for _, name := range names {
if _, _, ok := dtoi(name); !ok {
continue
}
c++
}
return c, nil
}
// If the ifi is nil, interfaceAddrTable returns addresses for all
// network interfaces. Otherwise it returns addresses for a specific
// interface.
func interfaceAddrTable(ifi *Interface) ([]Addr, error) {
var ifcs []Interface
if ifi == nil {
var err error
ifcs, err = interfaceTable(0)
if err != nil {
return nil, err
}
} else {
ifcs = []Interface{*ifi}
}
var addrs []Addr
for _, ifc := range ifcs {
status := ifc.Name + "/status"
statusf, err := open(status)
if err != nil {
return nil, err
}
defer statusf.close()
// Read but ignore first line as it only contains the table header.
// See https://9p.io/magic/man2html/3/ip
if _, ok := statusf.readLine(); !ok {
return nil, errors.New("cannot read header line for interface: " + status)
}
for line, ok := statusf.readLine(); ok; line, ok = statusf.readLine() {
fields := getFields(line)
if len(fields) < 1 {
return nil, errors.New("cannot parse IP address for interface: " + status)
}
addr := fields[0]
ip := ParseIP(addr)
if ip == nil {
return nil, errors.New("cannot parse IP address for interface: " + status)
}
// The mask is represented as CIDR relative to the IPv6 address.
// Plan 9 internal representation is always IPv6.
maskfld := fields[1]
maskfld = maskfld[1:]
pfxlen, _, ok := dtoi(maskfld)
if !ok {
return nil, errors.New("cannot parse network mask for interface: " + status)
}
var mask IPMask
if ip.To4() != nil { // IPv4 or IPv6 IPv4-mapped address
mask = CIDRMask(pfxlen-8*len(v4InV6Prefix), 8*IPv4len)
}
if ip.To16() != nil && ip.To4() == nil { // IPv6 address
mask = CIDRMask(pfxlen, 8*IPv6len)
}
addrs = append(addrs, &IPNet{IP: ip, Mask: mask})
}
}
return addrs, nil
}
// interfaceMulticastAddrTable returns addresses for a specific
// interface.
func interfaceMulticastAddrTable(ifi *Interface) ([]Addr, error) {
return nil, nil
}
|