interface_plan9.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. // Copyright 2016 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 (
  6. "errors"
  7. "internal/itoa"
  8. "os"
  9. )
  10. // If the ifindex is zero, interfaceTable returns mappings of all
  11. // network interfaces. Otherwise it returns a mapping of a specific
  12. // interface.
  13. func interfaceTable(ifindex int) ([]Interface, error) {
  14. if ifindex == 0 {
  15. n, err := interfaceCount()
  16. if err != nil {
  17. return nil, err
  18. }
  19. ifcs := make([]Interface, n)
  20. for i := range ifcs {
  21. ifc, err := readInterface(i)
  22. if err != nil {
  23. return nil, err
  24. }
  25. ifcs[i] = *ifc
  26. }
  27. return ifcs, nil
  28. }
  29. ifc, err := readInterface(ifindex - 1)
  30. if err != nil {
  31. return nil, err
  32. }
  33. return []Interface{*ifc}, nil
  34. }
  35. func readInterface(i int) (*Interface, error) {
  36. ifc := &Interface{
  37. Index: i + 1, // Offset the index by one to suit the contract
  38. Name: netdir + "/ipifc/" + itoa.Itoa(i), // Name is the full path to the interface path in plan9
  39. }
  40. ifcstat := ifc.Name + "/status"
  41. ifcstatf, err := open(ifcstat)
  42. if err != nil {
  43. return nil, err
  44. }
  45. defer ifcstatf.close()
  46. line, ok := ifcstatf.readLine()
  47. if !ok {
  48. return nil, errors.New("invalid interface status file: " + ifcstat)
  49. }
  50. fields := getFields(line)
  51. if len(fields) < 4 {
  52. return nil, errors.New("invalid interface status file: " + ifcstat)
  53. }
  54. device := fields[1]
  55. mtustr := fields[3]
  56. mtu, _, ok := dtoi(mtustr)
  57. if !ok {
  58. return nil, errors.New("invalid status file of interface: " + ifcstat)
  59. }
  60. ifc.MTU = mtu
  61. // Not a loopback device ("/dev/null") or packet interface (e.g. "pkt2")
  62. if stringsHasPrefix(device, netdir+"/") {
  63. deviceaddrf, err := open(device + "/addr")
  64. if err != nil {
  65. return nil, err
  66. }
  67. defer deviceaddrf.close()
  68. line, ok = deviceaddrf.readLine()
  69. if !ok {
  70. return nil, errors.New("invalid address file for interface: " + device + "/addr")
  71. }
  72. if len(line) > 0 && len(line)%2 == 0 {
  73. ifc.HardwareAddr = make([]byte, len(line)/2)
  74. var ok bool
  75. for i := range ifc.HardwareAddr {
  76. j := (i + 1) * 2
  77. ifc.HardwareAddr[i], ok = xtoi2(line[i*2:j], 0)
  78. if !ok {
  79. ifc.HardwareAddr = ifc.HardwareAddr[:i]
  80. break
  81. }
  82. }
  83. }
  84. ifc.Flags = FlagUp | FlagBroadcast | FlagMulticast
  85. } else {
  86. ifc.Flags = FlagUp | FlagMulticast | FlagLoopback
  87. }
  88. return ifc, nil
  89. }
  90. func interfaceCount() (int, error) {
  91. d, err := os.Open(netdir + "/ipifc")
  92. if err != nil {
  93. return -1, err
  94. }
  95. defer d.Close()
  96. names, err := d.Readdirnames(0)
  97. if err != nil {
  98. return -1, err
  99. }
  100. // Assumes that numbered files in ipifc are strictly
  101. // the incrementing numbered directories for the
  102. // interfaces
  103. c := 0
  104. for _, name := range names {
  105. if _, _, ok := dtoi(name); !ok {
  106. continue
  107. }
  108. c++
  109. }
  110. return c, nil
  111. }
  112. // If the ifi is nil, interfaceAddrTable returns addresses for all
  113. // network interfaces. Otherwise it returns addresses for a specific
  114. // interface.
  115. func interfaceAddrTable(ifi *Interface) ([]Addr, error) {
  116. var ifcs []Interface
  117. if ifi == nil {
  118. var err error
  119. ifcs, err = interfaceTable(0)
  120. if err != nil {
  121. return nil, err
  122. }
  123. } else {
  124. ifcs = []Interface{*ifi}
  125. }
  126. var addrs []Addr
  127. for _, ifc := range ifcs {
  128. status := ifc.Name + "/status"
  129. statusf, err := open(status)
  130. if err != nil {
  131. return nil, err
  132. }
  133. defer statusf.close()
  134. // Read but ignore first line as it only contains the table header.
  135. // See https://9p.io/magic/man2html/3/ip
  136. if _, ok := statusf.readLine(); !ok {
  137. return nil, errors.New("cannot read header line for interface: " + status)
  138. }
  139. for line, ok := statusf.readLine(); ok; line, ok = statusf.readLine() {
  140. fields := getFields(line)
  141. if len(fields) < 1 {
  142. return nil, errors.New("cannot parse IP address for interface: " + status)
  143. }
  144. addr := fields[0]
  145. ip := ParseIP(addr)
  146. if ip == nil {
  147. return nil, errors.New("cannot parse IP address for interface: " + status)
  148. }
  149. // The mask is represented as CIDR relative to the IPv6 address.
  150. // Plan 9 internal representation is always IPv6.
  151. maskfld := fields[1]
  152. maskfld = maskfld[1:]
  153. pfxlen, _, ok := dtoi(maskfld)
  154. if !ok {
  155. return nil, errors.New("cannot parse network mask for interface: " + status)
  156. }
  157. var mask IPMask
  158. if ip.To4() != nil { // IPv4 or IPv6 IPv4-mapped address
  159. mask = CIDRMask(pfxlen-8*len(v4InV6Prefix), 8*IPv4len)
  160. }
  161. if ip.To16() != nil && ip.To4() == nil { // IPv6 address
  162. mask = CIDRMask(pfxlen, 8*IPv6len)
  163. }
  164. addrs = append(addrs, &IPNet{IP: ip, Mask: mask})
  165. }
  166. }
  167. return addrs, nil
  168. }
  169. // interfaceMulticastAddrTable returns addresses for a specific
  170. // interface.
  171. func interfaceMulticastAddrTable(ifi *Interface) ([]Addr, error) {
  172. return nil, nil
  173. }