Ian Lance Taylor | 98607ac | 2011-09-16 08:09:31 -0700 | [diff] [blame] | 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 | |
Ian Lance Taylor | ba69295 | 2012-03-06 09:48:39 -0800 | [diff] [blame] | 9 | import "errors" |
Ian Lance Taylor | 98607ac | 2011-09-16 08:09:31 -0700 | [diff] [blame] | 10 | |
Ian Lance Taylor | 1fe0e9a | 2012-01-25 11:55:19 -0800 | [diff] [blame] | 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 | |
Ian Lance Taylor | 98607ac | 2011-09-16 08:09:31 -0700 | [diff] [blame] | 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. |
Ian Lance Taylor | 9d08a45 | 2011-12-02 18:16:25 -0800 | [diff] [blame] | 65 | func (ifi *Interface) Addrs() ([]Addr, error) { |
Ian Lance Taylor | 98607ac | 2011-09-16 08:09:31 -0700 | [diff] [blame] | 66 | if ifi == nil { |
Ian Lance Taylor | 1fe0e9a | 2012-01-25 11:55:19 -0800 | [diff] [blame] | 67 | return nil, errInvalidInterface |
Ian Lance Taylor | 98607ac | 2011-09-16 08:09:31 -0700 | [diff] [blame] | 68 | } |
| 69 | return interfaceAddrTable(ifi.Index) |
| 70 | } |
| 71 | |
| 72 | // MulticastAddrs returns multicast, joined group addresses for |
| 73 | // a specific interface. |
Ian Lance Taylor | 9d08a45 | 2011-12-02 18:16:25 -0800 | [diff] [blame] | 74 | func (ifi *Interface) MulticastAddrs() ([]Addr, error) { |
Ian Lance Taylor | 98607ac | 2011-09-16 08:09:31 -0700 | [diff] [blame] | 75 | if ifi == nil { |
Ian Lance Taylor | 1fe0e9a | 2012-01-25 11:55:19 -0800 | [diff] [blame] | 76 | return nil, errInvalidInterface |
Ian Lance Taylor | 98607ac | 2011-09-16 08:09:31 -0700 | [diff] [blame] | 77 | } |
| 78 | return interfaceMulticastAddrTable(ifi.Index) |
| 79 | } |
| 80 | |
| 81 | // Interfaces returns a list of the systems's network interfaces. |
Ian Lance Taylor | 9d08a45 | 2011-12-02 18:16:25 -0800 | [diff] [blame] | 82 | func Interfaces() ([]Interface, error) { |
Ian Lance Taylor | 98607ac | 2011-09-16 08:09:31 -0700 | [diff] [blame] | 83 | return interfaceTable(0) |
| 84 | } |
| 85 | |
| 86 | // InterfaceAddrs returns a list of the system's network interface |
| 87 | // addresses. |
Ian Lance Taylor | 9d08a45 | 2011-12-02 18:16:25 -0800 | [diff] [blame] | 88 | func InterfaceAddrs() ([]Addr, error) { |
Ian Lance Taylor | 98607ac | 2011-09-16 08:09:31 -0700 | [diff] [blame] | 89 | return interfaceAddrTable(0) |
| 90 | } |
| 91 | |
| 92 | // InterfaceByIndex returns the interface specified by index. |
Ian Lance Taylor | 9d08a45 | 2011-12-02 18:16:25 -0800 | [diff] [blame] | 93 | func InterfaceByIndex(index int) (*Interface, error) { |
Ian Lance Taylor | 98607ac | 2011-09-16 08:09:31 -0700 | [diff] [blame] | 94 | if index <= 0 { |
Ian Lance Taylor | 1fe0e9a | 2012-01-25 11:55:19 -0800 | [diff] [blame] | 95 | return nil, errInvalidInterfaceIndex |
Ian Lance Taylor | 98607ac | 2011-09-16 08:09:31 -0700 | [diff] [blame] | 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 | } |
Ian Lance Taylor | 1fe0e9a | 2012-01-25 11:55:19 -0800 | [diff] [blame] | 104 | return nil, errNoSuchInterface |
Ian Lance Taylor | 98607ac | 2011-09-16 08:09:31 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | // InterfaceByName returns the interface specified by name. |
Ian Lance Taylor | 9d08a45 | 2011-12-02 18:16:25 -0800 | [diff] [blame] | 108 | func InterfaceByName(name string) (*Interface, error) { |
Ian Lance Taylor | 98607ac | 2011-09-16 08:09:31 -0700 | [diff] [blame] | 109 | if name == "" { |
Ian Lance Taylor | 1fe0e9a | 2012-01-25 11:55:19 -0800 | [diff] [blame] | 110 | return nil, errInvalidInterfaceName |
Ian Lance Taylor | 98607ac | 2011-09-16 08:09:31 -0700 | [diff] [blame] | 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 | } |
Ian Lance Taylor | 1fe0e9a | 2012-01-25 11:55:19 -0800 | [diff] [blame] | 121 | return nil, errNoSuchInterface |
Ian Lance Taylor | 98607ac | 2011-09-16 08:09:31 -0700 | [diff] [blame] | 122 | } |