Source file
src/net/interface.go
1
2
3
4
5 package net
6
7 import (
8 "errors"
9 "internal/itoa"
10 "sync"
11 "time"
12 _ "unsafe"
13 )
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 var (
32 errInvalidInterface = errors.New("invalid network interface")
33 errInvalidInterfaceIndex = errors.New("invalid network interface index")
34 errInvalidInterfaceName = errors.New("invalid network interface name")
35 errNoSuchInterface = errors.New("no such network interface")
36 errNoSuchMulticastInterface = errors.New("no such multicast network interface")
37 )
38
39
40
41
42 type Interface struct {
43 Index int
44 MTU int
45 Name string
46 HardwareAddr HardwareAddr
47 Flags Flags
48 }
49
50 type Flags uint
51
52 const (
53 FlagUp Flags = 1 << iota
54 FlagBroadcast
55 FlagLoopback
56 FlagPointToPoint
57 FlagMulticast
58 FlagRunning
59 )
60
61 var flagNames = []string{
62 "up",
63 "broadcast",
64 "loopback",
65 "pointtopoint",
66 "multicast",
67 "running",
68 }
69
70 func (f Flags) String() string {
71 s := ""
72 for i, name := range flagNames {
73 if f&(1<<uint(i)) != 0 {
74 if s != "" {
75 s += "|"
76 }
77 s += name
78 }
79 }
80 if s == "" {
81 s = "0"
82 }
83 return s
84 }
85
86
87
88 func (ifi *Interface) Addrs() ([]Addr, error) {
89 if ifi == nil {
90 return nil, &OpError{Op: "route", Net: "ip+net", Source: nil, Addr: nil, Err: errInvalidInterface}
91 }
92 ifat, err := interfaceAddrTable(ifi)
93 if err != nil {
94 err = &OpError{Op: "route", Net: "ip+net", Source: nil, Addr: nil, Err: err}
95 }
96 return ifat, err
97 }
98
99
100
101 func (ifi *Interface) MulticastAddrs() ([]Addr, error) {
102 if ifi == nil {
103 return nil, &OpError{Op: "route", Net: "ip+net", Source: nil, Addr: nil, Err: errInvalidInterface}
104 }
105 ifat, err := interfaceMulticastAddrTable(ifi)
106 if err != nil {
107 err = &OpError{Op: "route", Net: "ip+net", Source: nil, Addr: nil, Err: err}
108 }
109 return ifat, err
110 }
111
112
113 func Interfaces() ([]Interface, error) {
114 ift, err := interfaceTable(0)
115 if err != nil {
116 return nil, &OpError{Op: "route", Net: "ip+net", Source: nil, Addr: nil, Err: err}
117 }
118 if len(ift) != 0 {
119 zoneCache.update(ift, false)
120 }
121 return ift, nil
122 }
123
124
125
126
127
128
129 func InterfaceAddrs() ([]Addr, error) {
130 ifat, err := interfaceAddrTable(nil)
131 if err != nil {
132 err = &OpError{Op: "route", Net: "ip+net", Source: nil, Addr: nil, Err: err}
133 }
134 return ifat, err
135 }
136
137
138
139
140
141
142 func InterfaceByIndex(index int) (*Interface, error) {
143 if index <= 0 {
144 return nil, &OpError{Op: "route", Net: "ip+net", Source: nil, Addr: nil, Err: errInvalidInterfaceIndex}
145 }
146 ift, err := interfaceTable(index)
147 if err != nil {
148 return nil, &OpError{Op: "route", Net: "ip+net", Source: nil, Addr: nil, Err: err}
149 }
150 ifi, err := interfaceByIndex(ift, index)
151 if err != nil {
152 err = &OpError{Op: "route", Net: "ip+net", Source: nil, Addr: nil, Err: err}
153 }
154 return ifi, err
155 }
156
157 func interfaceByIndex(ift []Interface, index int) (*Interface, error) {
158 for _, ifi := range ift {
159 if index == ifi.Index {
160 return &ifi, nil
161 }
162 }
163 return nil, errNoSuchInterface
164 }
165
166
167 func InterfaceByName(name string) (*Interface, error) {
168 if name == "" {
169 return nil, &OpError{Op: "route", Net: "ip+net", Source: nil, Addr: nil, Err: errInvalidInterfaceName}
170 }
171 ift, err := interfaceTable(0)
172 if err != nil {
173 return nil, &OpError{Op: "route", Net: "ip+net", Source: nil, Addr: nil, Err: err}
174 }
175 if len(ift) != 0 {
176 zoneCache.update(ift, false)
177 }
178 for _, ifi := range ift {
179 if name == ifi.Name {
180 return &ifi, nil
181 }
182 }
183 return nil, &OpError{Op: "route", Net: "ip+net", Source: nil, Addr: nil, Err: errNoSuchInterface}
184 }
185
186
187
188
189
190
191
192 type ipv6ZoneCache struct {
193 sync.RWMutex
194 lastFetched time.Time
195 toIndex map[string]int
196 toName map[int]string
197 }
198
199 var zoneCache = ipv6ZoneCache{
200 toIndex: make(map[string]int),
201 toName: make(map[int]string),
202 }
203
204
205
206
207 func (zc *ipv6ZoneCache) update(ift []Interface, force bool) (updated bool) {
208 zc.Lock()
209 defer zc.Unlock()
210 now := time.Now()
211 if !force && zc.lastFetched.After(now.Add(-60*time.Second)) {
212 return false
213 }
214 zc.lastFetched = now
215 if len(ift) == 0 {
216 var err error
217 if ift, err = interfaceTable(0); err != nil {
218 return false
219 }
220 }
221 zc.toIndex = make(map[string]int, len(ift))
222 zc.toName = make(map[int]string, len(ift))
223 for _, ifi := range ift {
224 zc.toIndex[ifi.Name] = ifi.Index
225 if _, ok := zc.toName[ifi.Index]; !ok {
226 zc.toName[ifi.Index] = ifi.Name
227 }
228 }
229 return true
230 }
231
232 func (zc *ipv6ZoneCache) name(index int) string {
233 if index == 0 {
234 return ""
235 }
236 updated := zoneCache.update(nil, false)
237 zoneCache.RLock()
238 name, ok := zoneCache.toName[index]
239 zoneCache.RUnlock()
240 if !ok && !updated {
241 zoneCache.update(nil, true)
242 zoneCache.RLock()
243 name, ok = zoneCache.toName[index]
244 zoneCache.RUnlock()
245 }
246 if !ok {
247 name = itoa.Uitoa(uint(index))
248 }
249 return name
250 }
251
252 func (zc *ipv6ZoneCache) index(name string) int {
253 if name == "" {
254 return 0
255 }
256 updated := zoneCache.update(nil, false)
257 zoneCache.RLock()
258 index, ok := zoneCache.toIndex[name]
259 zoneCache.RUnlock()
260 if !ok && !updated {
261 zoneCache.update(nil, true)
262 zoneCache.RLock()
263 index, ok = zoneCache.toIndex[name]
264 zoneCache.RUnlock()
265 }
266 if !ok {
267 index, _, _ = dtoi(name)
268 }
269 return index
270 }
271
View as plain text