control_unix.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 solaris
  5. package ipv6
  6. import (
  7. "os"
  8. "syscall"
  9. "golang.org/x/net/internal/iana"
  10. )
  11. func setControlMessage(s uintptr, 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(s, &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(s, &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(s, &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(s, &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. }
  74. opt.RUnlock()
  75. return
  76. }
  77. func parseControlMessage(b []byte) (*ControlMessage, error) {
  78. if len(b) == 0 {
  79. return nil, nil
  80. }
  81. cmsgs, err := syscall.ParseSocketControlMessage(b)
  82. if err != nil {
  83. return nil, os.NewSyscallError("parse socket control message", err)
  84. }
  85. cm := &ControlMessage{}
  86. for _, m := range cmsgs {
  87. if m.Header.Level != iana.ProtocolIPv6 {
  88. continue
  89. }
  90. switch int(m.Header.Type) {
  91. case ctlOpts[ctlTrafficClass].name:
  92. ctlOpts[ctlTrafficClass].parse(cm, m.Data[:])
  93. case ctlOpts[ctlHopLimit].name:
  94. ctlOpts[ctlHopLimit].parse(cm, m.Data[:])
  95. case ctlOpts[ctlPacketInfo].name:
  96. ctlOpts[ctlPacketInfo].parse(cm, m.Data[:])
  97. case ctlOpts[ctlPathMTU].name:
  98. ctlOpts[ctlPathMTU].parse(cm, m.Data[:])
  99. }
  100. }
  101. return cm, nil
  102. }
  103. func marshalControlMessage(cm *ControlMessage) (oob []byte) {
  104. if cm == nil {
  105. return
  106. }
  107. var l int
  108. tclass := false
  109. if ctlOpts[ctlTrafficClass].name > 0 && cm.TrafficClass > 0 {
  110. tclass = true
  111. l += syscall.CmsgSpace(ctlOpts[ctlTrafficClass].length)
  112. }
  113. hoplimit := false
  114. if ctlOpts[ctlHopLimit].name > 0 && cm.HopLimit > 0 {
  115. hoplimit = true
  116. l += syscall.CmsgSpace(ctlOpts[ctlHopLimit].length)
  117. }
  118. pktinfo := false
  119. if ctlOpts[ctlPacketInfo].name > 0 && (cm.Src.To16() != nil && cm.Src.To4() == nil || cm.IfIndex > 0) {
  120. pktinfo = true
  121. l += syscall.CmsgSpace(ctlOpts[ctlPacketInfo].length)
  122. }
  123. nexthop := false
  124. if ctlOpts[ctlNextHop].name > 0 && cm.NextHop.To16() != nil && cm.NextHop.To4() == nil {
  125. nexthop = true
  126. l += syscall.CmsgSpace(ctlOpts[ctlNextHop].length)
  127. }
  128. if l > 0 {
  129. oob = make([]byte, l)
  130. b := oob
  131. if tclass {
  132. b = ctlOpts[ctlTrafficClass].marshal(b, cm)
  133. }
  134. if hoplimit {
  135. b = ctlOpts[ctlHopLimit].marshal(b, cm)
  136. }
  137. if pktinfo {
  138. b = ctlOpts[ctlPacketInfo].marshal(b, cm)
  139. }
  140. if nexthop {
  141. b = ctlOpts[ctlNextHop].marshal(b, cm)
  142. }
  143. }
  144. return
  145. }