interface.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. package net
  5. import "errors"
  6. var (
  7. errInvalidInterface = errors.New("invalid network interface")
  8. errInvalidInterfaceIndex = errors.New("invalid network interface index")
  9. errInvalidInterfaceName = errors.New("invalid network interface name")
  10. errNoSuchInterface = errors.New("no such network interface")
  11. errNoSuchMulticastInterface = errors.New("no such multicast network interface")
  12. )
  13. // Interface represents a mapping between network interface name
  14. // and index. It also represents network interface facility
  15. // information.
  16. type Interface struct {
  17. Index int // positive integer that starts at one, zero is never used
  18. MTU int // maximum transmission unit
  19. Name string // e.g., "en0", "lo0", "eth0.100"
  20. HardwareAddr HardwareAddr // IEEE MAC-48, EUI-48 and EUI-64 form
  21. Flags Flags // e.g., FlagUp, FlagLoopback, FlagMulticast
  22. }
  23. type Flags uint
  24. const (
  25. FlagUp Flags = 1 << iota // interface is up
  26. FlagBroadcast // interface supports broadcast access capability
  27. FlagLoopback // interface is a loopback interface
  28. FlagPointToPoint // interface belongs to a point-to-point link
  29. FlagMulticast // interface supports multicast access capability
  30. )
  31. var flagNames = []string{
  32. "up",
  33. "broadcast",
  34. "loopback",
  35. "pointtopoint",
  36. "multicast",
  37. }
  38. func (f Flags) String() string {
  39. s := ""
  40. for i, name := range flagNames {
  41. if f&(1<<uint(i)) != 0 {
  42. if s != "" {
  43. s += "|"
  44. }
  45. s += name
  46. }
  47. }
  48. if s == "" {
  49. s = "0"
  50. }
  51. return s
  52. }
  53. // Addrs returns interface addresses for a specific interface.
  54. func (ifi *Interface) Addrs() ([]Addr, error) {
  55. if ifi == nil {
  56. return nil, errInvalidInterface
  57. }
  58. return interfaceAddrTable(ifi)
  59. }
  60. // MulticastAddrs returns multicast, joined group addresses for
  61. // a specific interface.
  62. func (ifi *Interface) MulticastAddrs() ([]Addr, error) {
  63. if ifi == nil {
  64. return nil, errInvalidInterface
  65. }
  66. return interfaceMulticastAddrTable(ifi)
  67. }
  68. // Interfaces returns a list of the system's network interfaces.
  69. func Interfaces() ([]Interface, error) {
  70. return interfaceTable(0)
  71. }
  72. // InterfaceAddrs returns a list of the system's network interface
  73. // addresses.
  74. func InterfaceAddrs() ([]Addr, error) {
  75. return interfaceAddrTable(nil)
  76. }
  77. // InterfaceByIndex returns the interface specified by index.
  78. func InterfaceByIndex(index int) (*Interface, error) {
  79. if index <= 0 {
  80. return nil, errInvalidInterfaceIndex
  81. }
  82. ift, err := interfaceTable(index)
  83. if err != nil {
  84. return nil, err
  85. }
  86. return interfaceByIndex(ift, index)
  87. }
  88. func interfaceByIndex(ift []Interface, index int) (*Interface, error) {
  89. for _, ifi := range ift {
  90. if index == ifi.Index {
  91. return &ifi, nil
  92. }
  93. }
  94. return nil, errNoSuchInterface
  95. }
  96. // InterfaceByName returns the interface specified by name.
  97. func InterfaceByName(name string) (*Interface, error) {
  98. if name == "" {
  99. return nil, errInvalidInterfaceName
  100. }
  101. ift, err := interfaceTable(0)
  102. if err != nil {
  103. return nil, err
  104. }
  105. for _, ifi := range ift {
  106. if name == ifi.Name {
  107. return &ifi, nil
  108. }
  109. }
  110. return nil, errNoSuchInterface
  111. }