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.
10 errInvalidInterface
= errors
.New("invalid network interface")
11 errInvalidInterfaceIndex
= errors
.New("invalid network interface index")
12 errInvalidInterfaceName
= errors
.New("invalid network interface name")
13 errNoSuchInterface
= errors
.New("no such network interface")
14 errNoSuchMulticastInterface
= errors
.New("no such multicast network interface")
17 // Interface represents a mapping between network interface name
18 // and index. It also represents network interface facility
20 type Interface
struct {
21 Index
int // positive integer that starts at one, zero is never used
22 MTU
int // maximum transmission unit
23 Name
string // e.g., "en0", "lo0", "eth0.100"
24 HardwareAddr HardwareAddr
// IEEE MAC-48, EUI-48 and EUI-64 form
25 Flags Flags
// e.g., FlagUp, FlagLoopback, FlagMulticast
31 FlagUp Flags
= 1 << iota // interface is up
32 FlagBroadcast
// interface supports broadcast access capability
33 FlagLoopback
// interface is a loopback interface
34 FlagPointToPoint
// interface belongs to a point-to-point link
35 FlagMulticast
// interface supports multicast access capability
38 var flagNames
= []string{
46 func (f Flags
) String() string {
48 for i
, name
:= range flagNames
{
49 if f
&(1<<uint(i
)) != 0 {
62 // Addrs returns interface addresses for a specific interface.
63 func (ifi
*Interface
) Addrs() ([]Addr
, error
) {
65 return nil, errInvalidInterface
67 return interfaceAddrTable(ifi
)
70 // MulticastAddrs returns multicast, joined group addresses for
71 // a specific interface.
72 func (ifi
*Interface
) MulticastAddrs() ([]Addr
, error
) {
74 return nil, errInvalidInterface
76 return interfaceMulticastAddrTable(ifi
)
79 // Interfaces returns a list of the system's network interfaces.
80 func Interfaces() ([]Interface
, error
) {
81 return interfaceTable(0)
84 // InterfaceAddrs returns a list of the system's network interface
86 func InterfaceAddrs() ([]Addr
, error
) {
87 return interfaceAddrTable(nil)
90 // InterfaceByIndex returns the interface specified by index.
91 func InterfaceByIndex(index
int) (*Interface
, error
) {
93 return nil, errInvalidInterfaceIndex
95 ift
, err
:= interfaceTable(index
)
99 return interfaceByIndex(ift
, index
)
102 func interfaceByIndex(ift
[]Interface
, index
int) (*Interface
, error
) {
103 for _
, ifi
:= range ift
{
104 if index
== ifi
.Index
{
108 return nil, errNoSuchInterface
111 // InterfaceByName returns the interface specified by name.
112 func InterfaceByName(name
string) (*Interface
, error
) {
114 return nil, errInvalidInterfaceName
116 ift
, err
:= interfaceTable(0)
120 for _
, ifi
:= range ift
{
121 if name
== ifi
.Name
{
125 return nil, errNoSuchInterface