Source File
interface.go
Belonging Package
net
package net
import (
)
var (
errInvalidInterface = errors.New("invalid network interface")
errInvalidInterfaceIndex = errors.New("invalid network interface index")
errInvalidInterfaceName = errors.New("invalid network interface name")
errNoSuchInterface = errors.New("no such network interface")
errNoSuchMulticastInterface = errors.New("no such multicast network interface")
)
type Interface struct {
Index int // positive integer that starts at one, zero is never used
MTU int // maximum transmission unit
Name string // e.g., "en0", "lo0", "eth0.100"
HardwareAddr HardwareAddr // IEEE MAC-48, EUI-48 and EUI-64 form
Flags Flags // e.g., FlagUp, FlagLoopback, FlagMulticast
}
type Flags uint
const (
FlagUp Flags = 1 << iota // interface is up
FlagBroadcast // interface supports broadcast access capability
FlagLoopback // interface is a loopback interface
FlagPointToPoint // interface belongs to a point-to-point link
FlagMulticast // interface supports multicast access capability
)
var flagNames = []string{
"up",
"broadcast",
"loopback",
"pointtopoint",
"multicast",
}
func ( Flags) () string {
:= ""
for , := range flagNames {
if &(1<<uint()) != 0 {
if != "" {
+= "|"
}
+=
}
}
if == "" {
= "0"
}
return
}
func ( int) (*Interface, error) {
if <= 0 {
return nil, &OpError{Op: "route", Net: "ip+net", Source: nil, Addr: nil, Err: errInvalidInterfaceIndex}
}
, := interfaceTable()
if != nil {
return nil, &OpError{Op: "route", Net: "ip+net", Source: nil, Addr: nil, Err: }
}
, := interfaceByIndex(, )
if != nil {
= &OpError{Op: "route", Net: "ip+net", Source: nil, Addr: nil, Err: }
}
return ,
}
func ( []Interface, int) (*Interface, error) {
for , := range {
if == .Index {
return &, nil
}
}
return nil, errNoSuchInterface
}
func ( string) (*Interface, error) {
if == "" {
return nil, &OpError{Op: "route", Net: "ip+net", Source: nil, Addr: nil, Err: errInvalidInterfaceName}
}
, := interfaceTable(0)
if != nil {
return nil, &OpError{Op: "route", Net: "ip+net", Source: nil, Addr: nil, Err: }
}
if len() != 0 {
zoneCache.update(, false)
}
for , := range {
if == .Name {
return &, nil
}
}
return nil, &OpError{Op: "route", Net: "ip+net", Source: nil, Addr: nil, Err: errNoSuchInterface}
}
type ipv6ZoneCache struct {
sync.RWMutex // guard the following
lastFetched time.Time // last time routing information was fetched
toIndex map[string]int // interface name to its index
toName map[int]string // interface index to its name
}
var zoneCache = ipv6ZoneCache{
toIndex: make(map[string]int),
toName: make(map[int]string),
}
func ( *ipv6ZoneCache) ( []Interface, bool) ( bool) {
.Lock()
defer .Unlock()
:= time.Now()
if ! && .lastFetched.After(.Add(-60*time.Second)) {
return false
}
.lastFetched =
if len() == 0 {
var error
if , = interfaceTable(0); != nil {
return false
}
}
.toIndex = make(map[string]int, len())
.toName = make(map[int]string, len())
for , := range {
.toIndex[.Name] = .Index
if , := .toName[.Index]; ! {
.toName[.Index] = .Name
}
}
return true
}
func ( *ipv6ZoneCache) ( int) string {
if == 0 {
return ""
}
:= zoneCache.update(nil, false)
zoneCache.RLock()
, := zoneCache.toName[]
zoneCache.RUnlock()
if ! && ! {
zoneCache.update(nil, true)
zoneCache.RLock()
, = zoneCache.toName[]
zoneCache.RUnlock()
}
if ! { // last resort
= uitoa(uint())
}
return
}
func ( *ipv6ZoneCache) ( string) int {
if == "" {
return 0
}
:= zoneCache.update(nil, false)
zoneCache.RLock()
, := zoneCache.toIndex[]
zoneCache.RUnlock()
if ! && ! {
zoneCache.update(nil, true)
zoneCache.RLock()
, = zoneCache.toIndex[]
zoneCache.RUnlock()
}
if ! { // last resort
, _, _ = dtoi()
}
return
![]() |
The pages are generated with Golds v0.3.2-preview. (GOOS=darwin GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |