control_unix.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // Copyright 2012 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 ipv4
  6. import (
  7. "os"
  8. "syscall"
  9. "unsafe"
  10. "golang.org/x/net/internal/iana"
  11. "golang.org/x/net/internal/socket"
  12. )
  13. func setControlMessage(c *socket.Conn, opt *rawOpt, cf ControlFlags, on bool) error {
  14. opt.Lock()
  15. defer opt.Unlock()
  16. if so, ok := sockOpts[ssoReceiveTTL]; ok && cf&FlagTTL != 0 {
  17. if err := so.SetInt(c, boolint(on)); err != nil {
  18. return err
  19. }
  20. if on {
  21. opt.set(FlagTTL)
  22. } else {
  23. opt.clear(FlagTTL)
  24. }
  25. }
  26. if so, ok := sockOpts[ssoPacketInfo]; ok {
  27. if cf&(FlagSrc|FlagDst|FlagInterface) != 0 {
  28. if err := so.SetInt(c, boolint(on)); err != nil {
  29. return err
  30. }
  31. if on {
  32. opt.set(cf & (FlagSrc | FlagDst | FlagInterface))
  33. } else {
  34. opt.clear(cf & (FlagSrc | FlagDst | FlagInterface))
  35. }
  36. }
  37. } else {
  38. if so, ok := sockOpts[ssoReceiveDst]; ok && cf&FlagDst != 0 {
  39. if err := so.SetInt(c, boolint(on)); err != nil {
  40. return err
  41. }
  42. if on {
  43. opt.set(FlagDst)
  44. } else {
  45. opt.clear(FlagDst)
  46. }
  47. }
  48. if so, ok := sockOpts[ssoReceiveInterface]; ok && cf&FlagInterface != 0 {
  49. if err := so.SetInt(c, boolint(on)); err != nil {
  50. return err
  51. }
  52. if on {
  53. opt.set(FlagInterface)
  54. } else {
  55. opt.clear(FlagInterface)
  56. }
  57. }
  58. }
  59. return nil
  60. }
  61. func newControlMessage(opt *rawOpt) (oob []byte) {
  62. opt.RLock()
  63. var l int
  64. if opt.isset(FlagTTL) && ctlOpts[ctlTTL].name > 0 {
  65. l += syscall.CmsgSpace(ctlOpts[ctlTTL].length)
  66. }
  67. if ctlOpts[ctlPacketInfo].name > 0 {
  68. if opt.isset(FlagSrc | FlagDst | FlagInterface) {
  69. l += syscall.CmsgSpace(ctlOpts[ctlPacketInfo].length)
  70. }
  71. } else {
  72. if opt.isset(FlagDst) && ctlOpts[ctlDst].name > 0 {
  73. l += syscall.CmsgSpace(ctlOpts[ctlDst].length)
  74. }
  75. if opt.isset(FlagInterface) && ctlOpts[ctlInterface].name > 0 {
  76. l += syscall.CmsgSpace(ctlOpts[ctlInterface].length)
  77. }
  78. }
  79. if l > 0 {
  80. oob = make([]byte, l)
  81. }
  82. opt.RUnlock()
  83. return
  84. }
  85. func parseControlMessage(b []byte) (*ControlMessage, error) {
  86. if len(b) == 0 {
  87. return nil, nil
  88. }
  89. cmsgs, err := syscall.ParseSocketControlMessage(b)
  90. if err != nil {
  91. return nil, os.NewSyscallError("parse socket control message", err)
  92. }
  93. cm := &ControlMessage{}
  94. for _, m := range cmsgs {
  95. if m.Header.Level != iana.ProtocolIP {
  96. continue
  97. }
  98. switch int(m.Header.Type) {
  99. case ctlOpts[ctlTTL].name:
  100. ctlOpts[ctlTTL].parse(cm, m.Data[:])
  101. case ctlOpts[ctlDst].name:
  102. ctlOpts[ctlDst].parse(cm, m.Data[:])
  103. case ctlOpts[ctlInterface].name:
  104. ctlOpts[ctlInterface].parse(cm, m.Data[:])
  105. case ctlOpts[ctlPacketInfo].name:
  106. ctlOpts[ctlPacketInfo].parse(cm, m.Data[:])
  107. }
  108. }
  109. return cm, nil
  110. }
  111. func marshalControlMessage(cm *ControlMessage) (oob []byte) {
  112. if cm == nil {
  113. return nil
  114. }
  115. var l int
  116. pktinfo := false
  117. if ctlOpts[ctlPacketInfo].name > 0 && (cm.Src.To4() != nil || cm.IfIndex > 0) {
  118. pktinfo = true
  119. l += syscall.CmsgSpace(ctlOpts[ctlPacketInfo].length)
  120. }
  121. if l > 0 {
  122. oob = make([]byte, l)
  123. b := oob
  124. if pktinfo {
  125. b = ctlOpts[ctlPacketInfo].marshal(b, cm)
  126. }
  127. }
  128. return
  129. }
  130. func marshalTTL(b []byte, cm *ControlMessage) []byte {
  131. m := (*syscall.Cmsghdr)(unsafe.Pointer(&b[0]))
  132. m.Level = iana.ProtocolIP
  133. m.Type = sysIP_RECVTTL
  134. m.SetLen(syscall.CmsgLen(1))
  135. return b[syscall.CmsgSpace(1):]
  136. }
  137. func parseTTL(cm *ControlMessage, b []byte) {
  138. cm.TTL = int(*(*byte)(unsafe.Pointer(&b[:1][0])))
  139. }