github.com/bgentry/go@v0.0.0-20150121062915-6cf5a733d54d/src/net/interface.go (about)

     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.
     4  
     5  package net
     6  
     7  import "errors"
     8  
     9  var (
    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")
    15  )
    16  
    17  // Interface represents a mapping between network interface name
    18  // and index.  It also represents network interface facility
    19  // information.
    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
    26  }
    27  
    28  type Flags uint
    29  
    30  const (
    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
    36  )
    37  
    38  var flagNames = []string{
    39  	"up",
    40  	"broadcast",
    41  	"loopback",
    42  	"pointtopoint",
    43  	"multicast",
    44  }
    45  
    46  func (f Flags) String() string {
    47  	s := ""
    48  	for i, name := range flagNames {
    49  		if f&(1<<uint(i)) != 0 {
    50  			if s != "" {
    51  				s += "|"
    52  			}
    53  			s += name
    54  		}
    55  	}
    56  	if s == "" {
    57  		s = "0"
    58  	}
    59  	return s
    60  }
    61  
    62  // Addrs returns interface addresses for a specific interface.
    63  func (ifi *Interface) Addrs() ([]Addr, error) {
    64  	if ifi == nil {
    65  		return nil, errInvalidInterface
    66  	}
    67  	return interfaceAddrTable(ifi)
    68  }
    69  
    70  // MulticastAddrs returns multicast, joined group addresses for
    71  // a specific interface.
    72  func (ifi *Interface) MulticastAddrs() ([]Addr, error) {
    73  	if ifi == nil {
    74  		return nil, errInvalidInterface
    75  	}
    76  	return interfaceMulticastAddrTable(ifi)
    77  }
    78  
    79  // Interfaces returns a list of the system's network interfaces.
    80  func Interfaces() ([]Interface, error) {
    81  	return interfaceTable(0)
    82  }
    83  
    84  // InterfaceAddrs returns a list of the system's network interface
    85  // addresses.
    86  func InterfaceAddrs() ([]Addr, error) {
    87  	return interfaceAddrTable(nil)
    88  }
    89  
    90  // InterfaceByIndex returns the interface specified by index.
    91  func InterfaceByIndex(index int) (*Interface, error) {
    92  	if index <= 0 {
    93  		return nil, errInvalidInterfaceIndex
    94  	}
    95  	ift, err := interfaceTable(index)
    96  	if err != nil {
    97  		return nil, err
    98  	}
    99  	return interfaceByIndex(ift, index)
   100  }
   101  
   102  func interfaceByIndex(ift []Interface, index int) (*Interface, error) {
   103  	for _, ifi := range ift {
   104  		if index == ifi.Index {
   105  			return &ifi, nil
   106  		}
   107  	}
   108  	return nil, errNoSuchInterface
   109  }
   110  
   111  // InterfaceByName returns the interface specified by name.
   112  func InterfaceByName(name string) (*Interface, error) {
   113  	if name == "" {
   114  		return nil, errInvalidInterfaceName
   115  	}
   116  	ift, err := interfaceTable(0)
   117  	if err != nil {
   118  		return nil, err
   119  	}
   120  	for _, ifi := range ift {
   121  		if name == ifi.Name {
   122  			return &ifi, nil
   123  		}
   124  	}
   125  	return nil, errNoSuchInterface
   126  }