1 // Copyright 2011 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // Network interface identification
12 errInvalidInterface = errors.New("net: invalid interface")
13 errInvalidInterfaceIndex = errors.New("net: invalid interface index")
14 errInvalidInterfaceName = errors.New("net: invalid interface name")
15 errNoSuchInterface = errors.New("net: no such interface")
16 errNoSuchMulticastInterface = errors.New("net: no such multicast interface")
19 // Interface represents a mapping between network interface name
20 // and index. It also represents network interface facility
22 type Interface struct {
23 Index int // positive integer that starts at one, zero is never used
24 MTU int // maximum transmission unit
25 Name string // e.g., "en0", "lo0", "eth0.100"
26 HardwareAddr HardwareAddr // IEEE MAC-48, EUI-48 and EUI-64 form
27 Flags Flags // e.g., FlagUp, FlagLoopback, FlagMulticast
33 FlagUp Flags = 1 << iota // interface is up
34 FlagBroadcast // interface supports broadcast access capability
35 FlagLoopback // interface is a loopback interface
36 FlagPointToPoint // interface belongs to a point-to-point link
37 FlagMulticast // interface supports multicast access capability
40 var flagNames = []string{
48 func (f Flags) String() string {
50 for i, name := range flagNames {
51 if f&(1<<uint(i)) != 0 {
64 // Addrs returns interface addresses for a specific interface.
65 func (ifi *Interface) Addrs() ([]Addr, error) {
67 return nil, errInvalidInterface
69 return interfaceAddrTable(ifi.Index)
72 // MulticastAddrs returns multicast, joined group addresses for
73 // a specific interface.
74 func (ifi *Interface) MulticastAddrs() ([]Addr, error) {
76 return nil, errInvalidInterface
78 return interfaceMulticastAddrTable(ifi.Index)
81 // Interfaces returns a list of the system's network interfaces.
82 func Interfaces() ([]Interface, error) {
83 return interfaceTable(0)
86 // InterfaceAddrs returns a list of the system's network interface
88 func InterfaceAddrs() ([]Addr, error) {
89 return interfaceAddrTable(0)
92 // InterfaceByIndex returns the interface specified by index.
93 func InterfaceByIndex(index int) (*Interface, error) {
95 return nil, errInvalidInterfaceIndex
97 ift, err := interfaceTable(index)
101 for _, ifi := range ift {
104 return nil, errNoSuchInterface
107 // InterfaceByName returns the interface specified by name.
108 func InterfaceByName(name string) (*Interface, error) {
110 return nil, errInvalidInterfaceName
112 ift, err := interfaceTable(0)
116 for _, ifi := range ift {
117 if name == ifi.Name {
121 return nil, errNoSuchInterface