src/pkg/net/interface.go - The Go Programming Language

Golang

Source file src/pkg/net/interface.go

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