sys_posix.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. // Copyright 2017 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. //go:build aix || darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris || windows || zos
  5. // +build aix darwin dragonfly freebsd linux netbsd openbsd solaris windows zos
  6. package socket
  7. import (
  8. "encoding/binary"
  9. "errors"
  10. "net"
  11. "runtime"
  12. "strconv"
  13. "sync"
  14. "time"
  15. )
  16. // marshalInetAddr writes a in sockaddr format into the buffer b.
  17. // The buffer must be sufficiently large (sizeofSockaddrInet4/6).
  18. // Returns the number of bytes written.
  19. func marshalInetAddr(a net.Addr, b []byte) int {
  20. switch a := a.(type) {
  21. case *net.TCPAddr:
  22. return marshalSockaddr(a.IP, a.Port, a.Zone, b)
  23. case *net.UDPAddr:
  24. return marshalSockaddr(a.IP, a.Port, a.Zone, b)
  25. case *net.IPAddr:
  26. return marshalSockaddr(a.IP, 0, a.Zone, b)
  27. default:
  28. return 0
  29. }
  30. }
  31. func marshalSockaddr(ip net.IP, port int, zone string, b []byte) int {
  32. if ip4 := ip.To4(); ip4 != nil {
  33. switch runtime.GOOS {
  34. case "android", "illumos", "linux", "solaris", "windows":
  35. NativeEndian.PutUint16(b[:2], uint16(sysAF_INET))
  36. default:
  37. b[0] = sizeofSockaddrInet4
  38. b[1] = sysAF_INET
  39. }
  40. binary.BigEndian.PutUint16(b[2:4], uint16(port))
  41. copy(b[4:8], ip4)
  42. return sizeofSockaddrInet4
  43. }
  44. if ip6 := ip.To16(); ip6 != nil && ip.To4() == nil {
  45. switch runtime.GOOS {
  46. case "android", "illumos", "linux", "solaris", "windows":
  47. NativeEndian.PutUint16(b[:2], uint16(sysAF_INET6))
  48. default:
  49. b[0] = sizeofSockaddrInet6
  50. b[1] = sysAF_INET6
  51. }
  52. binary.BigEndian.PutUint16(b[2:4], uint16(port))
  53. copy(b[8:24], ip6)
  54. if zone != "" {
  55. NativeEndian.PutUint32(b[24:28], uint32(zoneCache.index(zone)))
  56. }
  57. return sizeofSockaddrInet6
  58. }
  59. return 0
  60. }
  61. func parseInetAddr(b []byte, network string) (net.Addr, error) {
  62. if len(b) < 2 {
  63. return nil, errors.New("invalid address")
  64. }
  65. var af int
  66. switch runtime.GOOS {
  67. case "android", "illumos", "linux", "solaris", "windows":
  68. af = int(NativeEndian.Uint16(b[:2]))
  69. default:
  70. af = int(b[1])
  71. }
  72. var ip net.IP
  73. var zone string
  74. if af == sysAF_INET {
  75. if len(b) < sizeofSockaddrInet4 {
  76. return nil, errors.New("short address")
  77. }
  78. ip = make(net.IP, net.IPv4len)
  79. copy(ip, b[4:8])
  80. }
  81. if af == sysAF_INET6 {
  82. if len(b) < sizeofSockaddrInet6 {
  83. return nil, errors.New("short address")
  84. }
  85. ip = make(net.IP, net.IPv6len)
  86. copy(ip, b[8:24])
  87. if id := int(NativeEndian.Uint32(b[24:28])); id > 0 {
  88. zone = zoneCache.name(id)
  89. }
  90. }
  91. switch network {
  92. case "tcp", "tcp4", "tcp6":
  93. return &net.TCPAddr{IP: ip, Port: int(binary.BigEndian.Uint16(b[2:4])), Zone: zone}, nil
  94. case "udp", "udp4", "udp6":
  95. return &net.UDPAddr{IP: ip, Port: int(binary.BigEndian.Uint16(b[2:4])), Zone: zone}, nil
  96. default:
  97. return &net.IPAddr{IP: ip, Zone: zone}, nil
  98. }
  99. }
  100. // An ipv6ZoneCache represents a cache holding partial network
  101. // interface information. It is used for reducing the cost of IPv6
  102. // addressing scope zone resolution.
  103. //
  104. // Multiple names sharing the index are managed by first-come
  105. // first-served basis for consistency.
  106. type ipv6ZoneCache struct {
  107. sync.RWMutex // guard the following
  108. lastFetched time.Time // last time routing information was fetched
  109. toIndex map[string]int // interface name to its index
  110. toName map[int]string // interface index to its name
  111. }
  112. var zoneCache = ipv6ZoneCache{
  113. toIndex: make(map[string]int),
  114. toName: make(map[int]string),
  115. }
  116. // update refreshes the network interface information if the cache was last
  117. // updated more than 1 minute ago, or if force is set. It returns whether the
  118. // cache was updated.
  119. func (zc *ipv6ZoneCache) update(ift []net.Interface, force bool) (updated bool) {
  120. zc.Lock()
  121. defer zc.Unlock()
  122. now := time.Now()
  123. if !force && zc.lastFetched.After(now.Add(-60*time.Second)) {
  124. return false
  125. }
  126. zc.lastFetched = now
  127. if len(ift) == 0 {
  128. var err error
  129. if ift, err = net.Interfaces(); err != nil {
  130. return false
  131. }
  132. }
  133. zc.toIndex = make(map[string]int, len(ift))
  134. zc.toName = make(map[int]string, len(ift))
  135. for _, ifi := range ift {
  136. zc.toIndex[ifi.Name] = ifi.Index
  137. if _, ok := zc.toName[ifi.Index]; !ok {
  138. zc.toName[ifi.Index] = ifi.Name
  139. }
  140. }
  141. return true
  142. }
  143. func (zc *ipv6ZoneCache) name(zone int) string {
  144. updated := zoneCache.update(nil, false)
  145. zoneCache.RLock()
  146. name, ok := zoneCache.toName[zone]
  147. zoneCache.RUnlock()
  148. if !ok && !updated {
  149. zoneCache.update(nil, true)
  150. zoneCache.RLock()
  151. name, ok = zoneCache.toName[zone]
  152. zoneCache.RUnlock()
  153. }
  154. if !ok { // last resort
  155. name = strconv.Itoa(zone)
  156. }
  157. return name
  158. }
  159. func (zc *ipv6ZoneCache) index(zone string) int {
  160. updated := zoneCache.update(nil, false)
  161. zoneCache.RLock()
  162. index, ok := zoneCache.toIndex[zone]
  163. zoneCache.RUnlock()
  164. if !ok && !updated {
  165. zoneCache.update(nil, true)
  166. zoneCache.RLock()
  167. index, ok = zoneCache.toIndex[zone]
  168. zoneCache.RUnlock()
  169. }
  170. if !ok { // last resort
  171. index, _ = strconv.Atoi(zone)
  172. }
  173. return index
  174. }