OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libgo/] [go/] [net/] [interface.go] - Blame information for rev 747

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 747 jeremybenn
// 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 (
10
        "bytes"
11
        "errors"
12
        "fmt"
13
)
14
 
15
var (
16
        errInvalidInterface         = errors.New("net: invalid interface")
17
        errInvalidInterfaceIndex    = errors.New("net: invalid interface index")
18
        errInvalidInterfaceName     = errors.New("net: invalid interface name")
19
        errNoSuchInterface          = errors.New("net: no such interface")
20
        errNoSuchMulticastInterface = errors.New("net: no such multicast interface")
21
)
22
 
23
// A HardwareAddr represents a physical hardware address.
24
type HardwareAddr []byte
25
 
26
func (a HardwareAddr) String() string {
27
        var buf bytes.Buffer
28
        for i, b := range a {
29
                if i > 0 {
30
                        buf.WriteByte(':')
31
                }
32
                fmt.Fprintf(&buf, "%02x", b)
33
        }
34
        return buf.String()
35
}
36
 
37
// ParseMAC parses s as an IEEE 802 MAC-48, EUI-48, or EUI-64 using one of the
38
// following formats:
39
//   01:23:45:67:89:ab
40
//   01:23:45:67:89:ab:cd:ef
41
//   01-23-45-67-89-ab
42
//   01-23-45-67-89-ab-cd-ef
43
//   0123.4567.89ab
44
//   0123.4567.89ab.cdef
45
func ParseMAC(s string) (hw HardwareAddr, err error) {
46
        if len(s) < 14 {
47
                goto error
48
        }
49
 
50
        if s[2] == ':' || s[2] == '-' {
51
                if (len(s)+1)%3 != 0 {
52
                        goto error
53
                }
54
                n := (len(s) + 1) / 3
55
                if n != 6 && n != 8 {
56
                        goto error
57
                }
58
                hw = make(HardwareAddr, n)
59
                for x, i := 0, 0; i < n; i++ {
60
                        var ok bool
61
                        if hw[i], ok = xtoi2(s[x:], s[2]); !ok {
62
                                goto error
63
                        }
64
                        x += 3
65
                }
66
        } else if s[4] == '.' {
67
                if (len(s)+1)%5 != 0 {
68
                        goto error
69
                }
70
                n := 2 * (len(s) + 1) / 5
71
                if n != 6 && n != 8 {
72
                        goto error
73
                }
74
                hw = make(HardwareAddr, n)
75
                for x, i := 0, 0; i < n; i += 2 {
76
                        var ok bool
77
                        if hw[i], ok = xtoi2(s[x:x+2], 0); !ok {
78
                                goto error
79
                        }
80
                        if hw[i+1], ok = xtoi2(s[x+2:], s[4]); !ok {
81
                                goto error
82
                        }
83
                        x += 5
84
                }
85
        } else {
86
                goto error
87
        }
88
        return hw, nil
89
 
90
error:
91
        return nil, errors.New("invalid MAC address: " + s)
92
}
93
 
94
// Interface represents a mapping between network interface name
95
// and index.  It also represents network interface facility
96
// information.
97
type Interface struct {
98
        Index        int          // positive integer that starts at one, zero is never used
99
        MTU          int          // maximum transmission unit
100
        Name         string       // e.g., "en0", "lo0", "eth0.100"
101
        HardwareAddr HardwareAddr // IEEE MAC-48, EUI-48 and EUI-64 form
102
        Flags        Flags        // e.g., FlagUp, FlagLoopback, FlagMulticast
103
}
104
 
105
type Flags uint
106
 
107
const (
108
        FlagUp           Flags = 1 << iota // interface is up
109
        FlagBroadcast                      // interface supports broadcast access capability
110
        FlagLoopback                       // interface is a loopback interface
111
        FlagPointToPoint                   // interface belongs to a point-to-point link
112
        FlagMulticast                      // interface supports multicast access capability
113
)
114
 
115
var flagNames = []string{
116
        "up",
117
        "broadcast",
118
        "loopback",
119
        "pointtopoint",
120
        "multicast",
121
}
122
 
123
func (f Flags) String() string {
124
        s := ""
125
        for i, name := range flagNames {
126
                if f&(1<
127
                        if s != "" {
128
                                s += "|"
129
                        }
130
                        s += name
131
                }
132
        }
133
        if s == "" {
134
                s = "0"
135
        }
136
        return s
137
}
138
 
139
// Addrs returns interface addresses for a specific interface.
140
func (ifi *Interface) Addrs() ([]Addr, error) {
141
        if ifi == nil {
142
                return nil, errInvalidInterface
143
        }
144
        return interfaceAddrTable(ifi.Index)
145
}
146
 
147
// MulticastAddrs returns multicast, joined group addresses for
148
// a specific interface.
149
func (ifi *Interface) MulticastAddrs() ([]Addr, error) {
150
        if ifi == nil {
151
                return nil, errInvalidInterface
152
        }
153
        return interfaceMulticastAddrTable(ifi.Index)
154
}
155
 
156
// Interfaces returns a list of the systems's network interfaces.
157
func Interfaces() ([]Interface, error) {
158
        return interfaceTable(0)
159
}
160
 
161
// InterfaceAddrs returns a list of the system's network interface
162
// addresses.
163
func InterfaceAddrs() ([]Addr, error) {
164
        return interfaceAddrTable(0)
165
}
166
 
167
// InterfaceByIndex returns the interface specified by index.
168
func InterfaceByIndex(index int) (*Interface, error) {
169
        if index <= 0 {
170
                return nil, errInvalidInterfaceIndex
171
        }
172
        ift, err := interfaceTable(index)
173
        if err != nil {
174
                return nil, err
175
        }
176
        for _, ifi := range ift {
177
                return &ifi, nil
178
        }
179
        return nil, errNoSuchInterface
180
}
181
 
182
// InterfaceByName returns the interface specified by name.
183
func InterfaceByName(name string) (*Interface, error) {
184
        if name == "" {
185
                return nil, errInvalidInterfaceName
186
        }
187
        ift, err := interfaceTable(0)
188
        if err != nil {
189
                return nil, err
190
        }
191
        for _, ifi := range ift {
192
                if name == ifi.Name {
193
                        return &ifi, nil
194
                }
195
        }
196
        return nil, errNoSuchInterface
197
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.