Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(135)

Delta Between Two Patch Sets: src/pkg/net/interface.go

Issue 4437087: code review 4437087: net: add network interface identification API (Closed)
Left Patch Set: diff -r 01b6a240e0b7 https://go.googlecode.com/hg/ Created 12 years, 10 months ago
Right Patch Set: diff -r 596c547538d6 https://go.googlecode.com/hg/ Created 12 years, 10 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « src/pkg/net/Makefile ('k') | src/pkg/net/interface_bsd.go » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 // Copyright 2011 The Go Authors. All rights reserved. 1 // Copyright 2011 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style 2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file. 3 // license that can be found in the LICENSE file.
4 4
5 // Network interface identification 5 // Network interface identification
6 6
7 package net 7 package net
8 8
9 import ( 9 import (
10 "bytes"
10 "fmt" 11 "fmt"
11 "os" 12 "os"
13 "syscall"
12 ) 14 )
13 15
14 // HardwareAddr represents the hardware address of the interface. 16 // A HardwareAddr represents a physical hardware address.
15 type HardwareAddr []byte 17 type HardwareAddr []byte
16 18
17 // String returns the string form of the hardware address. 19 func (a HardwareAddr) String() string {
18 func (hwa HardwareAddr) String() string { 20 » var buf bytes.Buffer
19 » if hwa == nil { 21 » for i, b := range a {
r 2011/05/27 05:07:38 if len(hwa) == 0 {
mikio 2011/05/27 05:59:59 Done.
20 » » return "" 22 » » if i > 0 {
23 » » » buf.WriteByte(':')
24 » » }
25 » » fmt.Fprintf(&buf, "%02x", b)
21 } 26 }
22 » var s string 27 » return buf.String()
23 » for i := 0; i < len(hwa); i++ {
24 » » s += fmt.Sprintf("%02x", hwa[i])
25 » » if i < len(hwa)-1 {
r 2011/05/27 05:07:38 drop the test, always add : and then return s[:len
mikio 2011/05/27 05:59:59 Done.
26 » » » s += fmt.Sprintf(":")
27 » » }
28 » }
29 » return s
30 } 28 }
31 29
32 // Interface represents a mapping between network interface 30 // Interface represents a mapping between network interface name
33 // name and index. 31 // and index. It also represents network interface facility
32 // information.
34 type Interface struct { 33 type Interface struct {
35 » Index int 34 » Index int // positive integer that starts at one, zero i s never used
36 » MTU int 35 » MTU int // maximum transmission unit
37 » Name string 36 » Name string // e.g., "en0", "lo0", "eth0.100"
38 » Type string 37 » HardwareAddr HardwareAddr // IEEE MAC-48, EUI-48 and EUI-64 form
39 » HardwareAddr HardwareAddr 38 » rawFlags int
40 } 39 }
41 40
42 func (ifi *Interface) Addr() ([]IP, os.Error) { 41 // IsUp returns true if ifi is up.
42 func (ifi *Interface) IsUp() bool {
43 » if ifi == nil {
44 » » return false
45 » }
46 » return ifi.rawFlags&syscall.IFF_UP != 0
47 }
48
49 // IsLoopback returns true if ifi is a loopback interface.
50 func (ifi *Interface) IsLoopback() bool {
51 » if ifi == nil {
52 » » return false
53 » }
54 » return ifi.rawFlags&syscall.IFF_LOOPBACK != 0
55 }
56
57 // CanBroadcast returns true if ifi supports a broadcast access
58 // capability.
59 func (ifi *Interface) CanBroadcast() bool {
60 » if ifi == nil {
61 » » return false
62 » }
63 » return ifi.rawFlags&syscall.IFF_BROADCAST != 0
64 }
65
66 // IsPointToPoint returns true if ifi belongs to a point-to-point
67 // link.
68 func (ifi *Interface) IsPointToPoint() bool {
69 » if ifi == nil {
70 » » return false
71 » }
72 » return ifi.rawFlags&syscall.IFF_POINTOPOINT != 0
73 }
74
75 // CanMulticast returns true if ifi supports a multicast access
76 // capability.
77 func (ifi *Interface) CanMulticast() bool {
78 » if ifi == nil {
79 » » return false
80 » }
81 » return ifi.rawFlags&syscall.IFF_MULTICAST != 0
82 }
83
84 // Addrs returns interface addresses for a specific interface.
85 func (ifi *Interface) Addrs() ([]Addr, os.Error) {
86 » if ifi == nil {
87 » » return nil, os.NewError("net: invalid interface")
88 » }
43 return interfaceAddrTable(ifi.Index) 89 return interfaceAddrTable(ifi.Index)
44 } 90 }
45 91
46 // EnumerateInterface tries to enumerate network interfaces. 92 // Interfaces returns a list of the systems's network interfaces.
47 func EnumerateInterface() ([]Interface, os.Error) { 93 func Interfaces() ([]Interface, os.Error) {
48 return interfaceTable(0) 94 return interfaceTable(0)
49 } 95 }
50 96
51 // EnumerateInterfaceAddr tries to enumerate network interface 97 // InterfaceAddrs returns a list of the system's network interface
52 // addresses. 98 // addresses.
53 func EnumerateInterfaceAddr() ([]IP, os.Error) { 99 func InterfaceAddrs() ([]Addr, os.Error) {
54 return interfaceAddrTable(0) 100 return interfaceAddrTable(0)
55 } 101 }
56 102
57 // InterfaceIndex maps the network interface name specified in 103 // InterfaceByIndex returns the interface specified by index.
58 // name to its corresponding index. 104 func InterfaceByIndex(index int) (*Interface, os.Error) {
59 func InterfaceIndex(name string) (int, os.Error) { 105 » if index <= 0 {
106 » » return nil, os.NewError("net: invalid interface index")
107 » }
108 » ift, err := interfaceTable(index)
109 » if err != nil {
110 » » return nil, err
111 » }
112 » for _, ifi := range ift {
113 » » return &ifi, nil
114 » }
115 » return nil, os.NewError("net: no such interface")
116 }
117
118 // InterfaceByName returns the interface specified by name.
119 func InterfaceByName(name string) (*Interface, os.Error) {
60 if name == "" { 120 if name == "" {
61 » » return 0, os.EINVAL 121 » » return nil, os.NewError("net: invalid interface name")
62 } 122 }
63 ift, err := interfaceTable(0) 123 ift, err := interfaceTable(0)
64 if err != nil { 124 if err != nil {
65 » » return 0, err 125 » » return nil, err
66 } 126 }
67 err = os.ENOENT
68 var index int
69 for _, ifi := range ift { 127 for _, ifi := range ift {
70 if name == ifi.Name { 128 if name == ifi.Name {
71 » » » index = ifi.Index 129 » » » return &ifi, nil
72 » » » err = nil
73 » » » break
74 } 130 }
75 } 131 }
76 » return index, err 132 » return nil, os.NewError("net: no such interface")
r 2011/05/27 05:07:38 for ... { if name == ifi.Name { return ifi.I
mikio 2011/05/27 05:59:59 Done.
77 } 133 }
78
79 // InterfaceName maps the network interface index speficied in
80 // index to its corresponding name.
81 func InterfaceName(index int) (string, os.Error) {
82 if index <= 0 {
83 return "", os.EINVAL
84 }
85 ift, err := interfaceTable(index)
86 if err != nil {
87 return "", err
88 }
89 err = os.ENOENT
90 var name string
91 for _, ifi := range ift {
92 name = ifi.Name
93 err = nil
94 break
95 }
96 return name, err
r 2011/05/27 05:07:38 ditto
mikio 2011/05/27 05:59:59 Done.
97 }
LEFTRIGHT

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b