2011-09-16 17:47:21 +02:00
|
|
|
// Copyright 2011 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
|
|
|
|
|
2012-03-06 18:57:23 +01:00
|
|
|
import "errors"
|
2011-09-16 17:47:21 +02:00
|
|
|
|
2012-01-25 21:56:26 +01:00
|
|
|
var (
|
2014-07-19 10:53:52 +02:00
|
|
|
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")
|
2012-01-25 21:56:26 +01:00
|
|
|
)
|
|
|
|
|
2011-09-16 17:47:21 +02:00
|
|
|
// Interface represents a mapping between network interface name
|
|
|
|
// and index. It also represents network interface facility
|
|
|
|
// information.
|
|
|
|
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 (f Flags) String() string {
|
|
|
|
s := ""
|
|
|
|
for i, name := range flagNames {
|
|
|
|
if f&(1<<uint(i)) != 0 {
|
|
|
|
if s != "" {
|
|
|
|
s += "|"
|
|
|
|
}
|
|
|
|
s += name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if s == "" {
|
|
|
|
s = "0"
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
// Addrs returns interface addresses for a specific interface.
|
2011-12-03 03:17:34 +01:00
|
|
|
func (ifi *Interface) Addrs() ([]Addr, error) {
|
2011-09-16 17:47:21 +02:00
|
|
|
if ifi == nil {
|
2015-10-31 01:59:47 +01:00
|
|
|
return nil, &OpError{Op: "route", Net: "ip+net", Source: nil, Addr: nil, Err: errInvalidInterface}
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
2015-10-31 01:59:47 +01:00
|
|
|
ifat, err := interfaceAddrTable(ifi)
|
|
|
|
if err != nil {
|
|
|
|
err = &OpError{Op: "route", Net: "ip+net", Source: nil, Addr: nil, Err: err}
|
|
|
|
}
|
|
|
|
return ifat, err
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// MulticastAddrs returns multicast, joined group addresses for
|
|
|
|
// a specific interface.
|
2011-12-03 03:17:34 +01:00
|
|
|
func (ifi *Interface) MulticastAddrs() ([]Addr, error) {
|
2011-09-16 17:47:21 +02:00
|
|
|
if ifi == nil {
|
2015-10-31 01:59:47 +01:00
|
|
|
return nil, &OpError{Op: "route", Net: "ip+net", Source: nil, Addr: nil, Err: errInvalidInterface}
|
|
|
|
}
|
|
|
|
ifat, err := interfaceMulticastAddrTable(ifi)
|
|
|
|
if err != nil {
|
|
|
|
err = &OpError{Op: "route", Net: "ip+net", Source: nil, Addr: nil, Err: err}
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
2015-10-31 01:59:47 +01:00
|
|
|
return ifat, err
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
|
2012-03-30 23:27:11 +02:00
|
|
|
// Interfaces returns a list of the system's network interfaces.
|
2011-12-03 03:17:34 +01:00
|
|
|
func Interfaces() ([]Interface, error) {
|
2015-10-31 01:59:47 +01:00
|
|
|
ift, err := interfaceTable(0)
|
|
|
|
if err != nil {
|
|
|
|
err = &OpError{Op: "route", Net: "ip+net", Source: nil, Addr: nil, Err: err}
|
|
|
|
}
|
|
|
|
return ift, err
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// InterfaceAddrs returns a list of the system's network interface
|
|
|
|
// addresses.
|
2011-12-03 03:17:34 +01:00
|
|
|
func InterfaceAddrs() ([]Addr, error) {
|
2015-10-31 01:59:47 +01:00
|
|
|
ifat, err := interfaceAddrTable(nil)
|
|
|
|
if err != nil {
|
|
|
|
err = &OpError{Op: "route", Net: "ip+net", Source: nil, Addr: nil, Err: err}
|
|
|
|
}
|
|
|
|
return ifat, err
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// InterfaceByIndex returns the interface specified by index.
|
2011-12-03 03:17:34 +01:00
|
|
|
func InterfaceByIndex(index int) (*Interface, error) {
|
2011-09-16 17:47:21 +02:00
|
|
|
if index <= 0 {
|
2015-10-31 01:59:47 +01:00
|
|
|
return nil, &OpError{Op: "route", Net: "ip+net", Source: nil, Addr: nil, Err: errInvalidInterfaceIndex}
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
ift, err := interfaceTable(index)
|
|
|
|
if err != nil {
|
2015-10-31 01:59:47 +01:00
|
|
|
return nil, &OpError{Op: "route", Net: "ip+net", Source: nil, Addr: nil, Err: err}
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
2015-10-31 01:59:47 +01:00
|
|
|
ifi, err := interfaceByIndex(ift, index)
|
|
|
|
if err != nil {
|
|
|
|
err = &OpError{Op: "route", Net: "ip+net", Source: nil, Addr: nil, Err: err}
|
|
|
|
}
|
|
|
|
return ifi, err
|
2013-07-16 08:54:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func interfaceByIndex(ift []Interface, index int) (*Interface, error) {
|
2011-09-16 17:47:21 +02:00
|
|
|
for _, ifi := range ift {
|
2013-07-16 08:54:42 +02:00
|
|
|
if index == ifi.Index {
|
|
|
|
return &ifi, nil
|
|
|
|
}
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
2012-01-25 21:56:26 +01:00
|
|
|
return nil, errNoSuchInterface
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// InterfaceByName returns the interface specified by name.
|
2011-12-03 03:17:34 +01:00
|
|
|
func InterfaceByName(name string) (*Interface, error) {
|
2011-09-16 17:47:21 +02:00
|
|
|
if name == "" {
|
2015-10-31 01:59:47 +01:00
|
|
|
return nil, &OpError{Op: "route", Net: "ip+net", Source: nil, Addr: nil, Err: errInvalidInterfaceName}
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
ift, err := interfaceTable(0)
|
|
|
|
if err != nil {
|
2015-10-31 01:59:47 +01:00
|
|
|
return nil, &OpError{Op: "route", Net: "ip+net", Source: nil, Addr: nil, Err: err}
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
for _, ifi := range ift {
|
|
|
|
if name == ifi.Name {
|
|
|
|
return &ifi, nil
|
|
|
|
}
|
|
|
|
}
|
2015-10-31 01:59:47 +01:00
|
|
|
return nil, &OpError{Op: "route", Net: "ip+net", Source: nil, Addr: nil, Err: errNoSuchInterface}
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|