control_unix.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 aix darwin dragonfly freebsd linux netbsd openbsd solaris
  5. package ipv4
  6. import (
  7. "unsafe"
  8. "golang.org/x/net/internal/iana"
  9. "golang.org/x/net/internal/socket"
  10. )
  11. func setControlMessage(c *socket.Conn, opt *rawOpt, cf ControlFlags, on bool) error {
  12. opt.Lock()
  13. defer opt.Unlock()
  14. if so, ok := sockOpts[ssoReceiveTTL]; ok && cf&FlagTTL != 0 {
  15. if err := so.SetInt(c, boolint(on)); err != nil {
  16. return err
  17. }
  18. if on {
  19. opt.set(FlagTTL)
  20. } else {
  21. opt.clear(FlagTTL)
  22. }
  23. }
  24. if so, ok := sockOpts[ssoPacketInfo]; ok {
  25. if cf&(FlagSrc|FlagDst|FlagInterface) != 0 {
  26. if err := so.SetInt(c, boolint(on)); err != nil {
  27. return err
  28. }
  29. if on {
  30. opt.set(cf & (FlagSrc | FlagDst | FlagInterface))
  31. } else {
  32. opt.clear(cf & (FlagSrc | FlagDst | FlagInterface))
  33. }
  34. }
  35. } else {
  36. if so, ok := sockOpts[ssoReceiveDst]; ok && cf&FlagDst != 0 {
  37. if err := so.SetInt(c, boolint(on)); err != nil {
  38. return err
  39. }
  40. if on {
  41. opt.set(FlagDst)
  42. } else {
  43. opt.clear(FlagDst)
  44. }
  45. }
  46. if so, ok := sockOpts[ssoReceiveInterface]; ok && cf&FlagInterface != 0 {
  47. if err := so.SetInt(c, boolint(on)); err != nil {
  48. return err
  49. }
  50. if on {
  51. opt.set(FlagInterface)
  52. } else {
  53. opt.clear(FlagInterface)
  54. }
  55. }
  56. }
  57. return nil
  58. }
  59. func marshalTTL(b []byte, cm *ControlMessage) []byte {
  60. m := socket.ControlMessage(b)
  61. m.MarshalHeader(iana.ProtocolIP, sysIP_RECVTTL, 1)
  62. return m.Next(1)
  63. }
  64. func parseTTL(cm *ControlMessage, b []byte) {
  65. cm.TTL = int(*(*byte)(unsafe.Pointer(&b[:1][0])))
  66. }