compiler: Do not declare type switch variable outside case statements.
[official-gcc.git] / libgo / go / net / interface.go
blob2e9f1ebc6791f9ab7b4cbbab3b694ef4e43b4414
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.
5 package net
7 import "errors"
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")
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
28 type Flags uint
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
38 var flagNames = []string{
39 "up",
40 "broadcast",
41 "loopback",
42 "pointtopoint",
43 "multicast",
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 += "|"
53 s += name
56 if s == "" {
57 s = "0"
59 return s
62 // Addrs returns interface addresses for a specific interface.
63 func (ifi *Interface) Addrs() ([]Addr, error) {
64 if ifi == nil {
65 return nil, errInvalidInterface
67 return interfaceAddrTable(ifi)
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
76 return interfaceMulticastAddrTable(ifi)
79 // Interfaces returns a list of the system's network interfaces.
80 func Interfaces() ([]Interface, error) {
81 return interfaceTable(0)
84 // InterfaceAddrs returns a list of the system's network interface
85 // addresses.
86 func InterfaceAddrs() ([]Addr, error) {
87 return interfaceAddrTable(nil)
90 // InterfaceByIndex returns the interface specified by index.
91 func InterfaceByIndex(index int) (*Interface, error) {
92 if index <= 0 {
93 return nil, errInvalidInterfaceIndex
95 ift, err := interfaceTable(index)
96 if err != nil {
97 return nil, err
99 return interfaceByIndex(ift, index)
102 func interfaceByIndex(ift []Interface, index int) (*Interface, error) {
103 for _, ifi := range ift {
104 if index == ifi.Index {
105 return &ifi, nil
108 return nil, errNoSuchInterface
111 // InterfaceByName returns the interface specified by name.
112 func InterfaceByName(name string) (*Interface, error) {
113 if name == "" {
114 return nil, errInvalidInterfaceName
116 ift, err := interfaceTable(0)
117 if err != nil {
118 return nil, err
120 for _, ifi := range ift {
121 if name == ifi.Name {
122 return &ifi, nil
125 return nil, errNoSuchInterface