control_unix.go 3.5 KB

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