blob: f25d046c171ea1bb4b512230b2a922726f3540b1 [file] [log] [blame]
Ian Lance Taylor98607ac2011-09-16 08:09:31 -07001// 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
7package net
8
Ian Lance Taylorba692952012-03-06 09:48:39 -08009import "errors"
Ian Lance Taylor98607ac2011-09-16 08:09:31 -070010
Ian Lance Taylor1fe0e9a2012-01-25 11:55:19 -080011var (
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 Taylor98607ac2011-09-16 08:09:31 -070019// Interface represents a mapping between network interface name
20// and index. It also represents network interface facility
21// information.
22type 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
30type Flags uint
31
32const (
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
40var flagNames = []string{
41 "up",
42 "broadcast",
43 "loopback",
44 "pointtopoint",
45 "multicast",
46}
47
48func (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 Taylor9d08a452011-12-02 18:16:25 -080065func (ifi *Interface) Addrs() ([]Addr, error) {
Ian Lance Taylor98607ac2011-09-16 08:09:31 -070066 if ifi == nil {
Ian Lance Taylor1fe0e9a2012-01-25 11:55:19 -080067 return nil, errInvalidInterface
Ian Lance Taylor98607ac2011-09-16 08:09:31 -070068 }
69 return interfaceAddrTable(ifi.Index)
70}
71
72// MulticastAddrs returns multicast, joined group addresses for
73// a specific interface.
Ian Lance Taylor9d08a452011-12-02 18:16:25 -080074func (ifi *Interface) MulticastAddrs() ([]Addr, error) {
Ian Lance Taylor98607ac2011-09-16 08:09:31 -070075 if ifi == nil {
Ian Lance Taylor1fe0e9a2012-01-25 11:55:19 -080076 return nil, errInvalidInterface
Ian Lance Taylor98607ac2011-09-16 08:09:31 -070077 }
78 return interfaceMulticastAddrTable(ifi.Index)
79}
80
81// Interfaces returns a list of the systems's network interfaces.
Ian Lance Taylor9d08a452011-12-02 18:16:25 -080082func Interfaces() ([]Interface, error) {
Ian Lance Taylor98607ac2011-09-16 08:09:31 -070083 return interfaceTable(0)
84}
85
86// InterfaceAddrs returns a list of the system's network interface
87// addresses.
Ian Lance Taylor9d08a452011-12-02 18:16:25 -080088func InterfaceAddrs() ([]Addr, error) {
Ian Lance Taylor98607ac2011-09-16 08:09:31 -070089 return interfaceAddrTable(0)
90}
91
92// InterfaceByIndex returns the interface specified by index.
Ian Lance Taylor9d08a452011-12-02 18:16:25 -080093func InterfaceByIndex(index int) (*Interface, error) {
Ian Lance Taylor98607ac2011-09-16 08:09:31 -070094 if index <= 0 {
Ian Lance Taylor1fe0e9a2012-01-25 11:55:19 -080095 return nil, errInvalidInterfaceIndex
Ian Lance Taylor98607ac2011-09-16 08:09:31 -070096 }
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 Taylor1fe0e9a2012-01-25 11:55:19 -0800104 return nil, errNoSuchInterface
Ian Lance Taylor98607ac2011-09-16 08:09:31 -0700105}
106
107// InterfaceByName returns the interface specified by name.
Ian Lance Taylor9d08a452011-12-02 18:16:25 -0800108func InterfaceByName(name string) (*Interface, error) {
Ian Lance Taylor98607ac2011-09-16 08:09:31 -0700109 if name == "" {
Ian Lance Taylor1fe0e9a2012-01-25 11:55:19 -0800110 return nil, errInvalidInterfaceName
Ian Lance Taylor98607ac2011-09-16 08:09:31 -0700111 }
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 Taylor1fe0e9a2012-01-25 11:55:19 -0800121 return nil, errNoSuchInterface
Ian Lance Taylor98607ac2011-09-16 08:09:31 -0700122}