control_unix.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // Copyright 2013 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. // +build darwin dragonfly freebsd linux netbsd openbsd
  5. package ipv6
  6. import (
  7. "os"
  8. "syscall"
  9. "golang.org/x/net/internal/iana"
  10. )
  11. func setControlMessage(fd int, opt *rawOpt, cf ControlFlags, on bool) error {
  12. opt.Lock()
  13. defer opt.Unlock()
  14. if cf&FlagTrafficClass != 0 && sockOpts[ssoReceiveTrafficClass].name > 0 {
  15. if err := setInt(fd, &sockOpts[ssoReceiveTrafficClass], boolint(on)); err != nil {
  16. return err
  17. }
  18. if on {
  19. opt.set(FlagTrafficClass)
  20. } else {
  21. opt.clear(FlagTrafficClass)
  22. }
  23. }
  24. if cf&FlagHopLimit != 0 && sockOpts[ssoReceiveHopLimit].name > 0 {
  25. if err := setInt(fd, &sockOpts[ssoReceiveHopLimit], boolint(on)); err != nil {
  26. return err
  27. }
  28. if on {
  29. opt.set(FlagHopLimit)
  30. } else {
  31. opt.clear(FlagHopLimit)
  32. }
  33. }
  34. if cf&flagPacketInfo != 0 && sockOpts[ssoReceivePacketInfo].name > 0 {
  35. if err := setInt(fd, &sockOpts[ssoReceivePacketInfo], boolint(on)); err != nil {
  36. return err
  37. }
  38. if on {
  39. opt.set(cf & flagPacketInfo)
  40. } else {
  41. opt.clear(cf & flagPacketInfo)
  42. }
  43. }
  44. if cf&FlagPathMTU != 0 && sockOpts[ssoReceivePathMTU].name > 0 {
  45. if err := setInt(fd, &sockOpts[ssoReceivePathMTU], boolint(on)); err != nil {
  46. return err
  47. }
  48. if on {
  49. opt.set(FlagPathMTU)
  50. } else {
  51. opt.clear(FlagPathMTU)
  52. }
  53. }
  54. return nil
  55. }
  56. func newControlMessage(opt *rawOpt) (oob []byte) {
  57. opt.RLock()
  58. var l int
  59. if opt.isset(FlagTrafficClass) && ctlOpts[ctlTrafficClass].name > 0 {
  60. l += syscall.CmsgSpace(ctlOpts[ctlTrafficClass].length)
  61. }
  62. if opt.isset(FlagHopLimit) && ctlOpts[ctlHopLimit].name > 0 {
  63. l += syscall.CmsgSpace(ctlOpts[ctlHopLimit].length)
  64. }
  65. if opt.isset(flagPacketInfo) && ctlOpts[ctlPacketInfo].name > 0 {
  66. l += syscall.CmsgSpace(ctlOpts[ctlPacketInfo].length)
  67. }
  68. if opt.isset(FlagPathMTU) && ctlOpts[ctlPathMTU].name > 0 {
  69. l += syscall.CmsgSpace(ctlOpts[ctlPathMTU].length)
  70. }
  71. if l > 0 {
  72. oob = make([]byte, l)
  73. b := oob
  74. if opt.isset(FlagTrafficClass) && ctlOpts[ctlTrafficClass].name > 0 {
  75. b = ctlOpts[ctlTrafficClass].marshal(b, nil)
  76. }
  77. if opt.isset(FlagHopLimit) && ctlOpts[ctlHopLimit].name > 0 {
  78. b = ctlOpts[ctlHopLimit].marshal(b, nil)
  79. }
  80. if opt.isset(flagPacketInfo) && ctlOpts[ctlPacketInfo].name > 0 {
  81. b = ctlOpts[ctlPacketInfo].marshal(b, nil)
  82. }
  83. if opt.isset(FlagPathMTU) && ctlOpts[ctlPathMTU].name > 0 {
  84. b = ctlOpts[ctlPathMTU].marshal(b, nil)
  85. }
  86. }
  87. opt.RUnlock()
  88. return
  89. }
  90. func parseControlMessage(b []byte) (*ControlMessage, error) {
  91. if len(b) == 0 {
  92. return nil, nil
  93. }
  94. cmsgs, err := syscall.ParseSocketControlMessage(b)
  95. if err != nil {
  96. return nil, os.NewSyscallError("parse socket control message", err)
  97. }
  98. cm := &ControlMessage{}
  99. for _, m := range cmsgs {
  100. if m.Header.Level != iana.ProtocolIPv6 {
  101. continue
  102. }
  103. switch int(m.Header.Type) {
  104. case ctlOpts[ctlTrafficClass].name:
  105. ctlOpts[ctlTrafficClass].parse(cm, m.Data[:])
  106. case ctlOpts[ctlHopLimit].name:
  107. ctlOpts[ctlHopLimit].parse(cm, m.Data[:])
  108. case ctlOpts[ctlPacketInfo].name:
  109. ctlOpts[ctlPacketInfo].parse(cm, m.Data[:])
  110. case ctlOpts[ctlPathMTU].name:
  111. ctlOpts[ctlPathMTU].parse(cm, m.Data[:])
  112. }
  113. }
  114. return cm, nil
  115. }
  116. func marshalControlMessage(cm *ControlMessage) (oob []byte) {
  117. if cm == nil {
  118. return
  119. }
  120. var l int
  121. tclass := false
  122. if ctlOpts[ctlTrafficClass].name > 0 && cm.TrafficClass > 0 {
  123. tclass = true
  124. l += syscall.CmsgSpace(ctlOpts[ctlTrafficClass].length)
  125. }
  126. hoplimit := false
  127. if ctlOpts[ctlHopLimit].name > 0 && cm.HopLimit > 0 {
  128. hoplimit = true
  129. l += syscall.CmsgSpace(ctlOpts[ctlHopLimit].length)
  130. }
  131. pktinfo := false
  132. if ctlOpts[ctlPacketInfo].name > 0 && (cm.Src.To16() != nil && cm.Src.To4() == nil || cm.IfIndex > 0) {
  133. pktinfo = true
  134. l += syscall.CmsgSpace(ctlOpts[ctlPacketInfo].length)
  135. }
  136. nexthop := false
  137. if ctlOpts[ctlNextHop].name > 0 && cm.NextHop.To16() != nil && cm.NextHop.To4() == nil {
  138. nexthop = true
  139. l += syscall.CmsgSpace(ctlOpts[ctlNextHop].length)
  140. }
  141. if l > 0 {
  142. oob = make([]byte, l)
  143. b := oob
  144. if tclass {
  145. b = ctlOpts[ctlTrafficClass].marshal(b, cm)
  146. }
  147. if hoplimit {
  148. b = ctlOpts[ctlHopLimit].marshal(b, cm)
  149. }
  150. if pktinfo {
  151. b = ctlOpts[ctlPacketInfo].marshal(b, cm)
  152. }
  153. if nexthop {
  154. b = ctlOpts[ctlNextHop].marshal(b, cm)
  155. }
  156. }
  157. return
  158. }