control.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. package ipv4
  5. import (
  6. "fmt"
  7. "net"
  8. "sync"
  9. )
  10. type rawOpt struct {
  11. sync.RWMutex
  12. cflags ControlFlags
  13. }
  14. func (c *rawOpt) set(f ControlFlags) { c.cflags |= f }
  15. func (c *rawOpt) clear(f ControlFlags) { c.cflags &^= f }
  16. func (c *rawOpt) isset(f ControlFlags) bool { return c.cflags&f != 0 }
  17. type ControlFlags uint
  18. const (
  19. FlagTTL ControlFlags = 1 << iota // pass the TTL on the received packet
  20. FlagSrc // pass the source address on the received packet
  21. FlagDst // pass the destination address on the received packet
  22. FlagInterface // pass the interface index on the received packet
  23. )
  24. // A ControlMessage represents per packet basis IP-level socket options.
  25. type ControlMessage struct {
  26. // Receiving socket options: SetControlMessage allows to
  27. // receive the options from the protocol stack using ReadFrom
  28. // method of PacketConn or RawConn.
  29. //
  30. // Specifying socket options: ControlMessage for WriteTo
  31. // method of PacketConn or RawConn allows to send the options
  32. // to the protocol stack.
  33. //
  34. TTL int // time-to-live, receiving only
  35. Src net.IP // source address, specifying only
  36. Dst net.IP // destination address, receiving only
  37. IfIndex int // interface index, must be 1 <= value when specifying
  38. }
  39. func (cm *ControlMessage) String() string {
  40. if cm == nil {
  41. return "<nil>"
  42. }
  43. return fmt.Sprintf("ttl=%d src=%v dst=%v ifindex=%d", cm.TTL, cm.Src, cm.Dst, cm.IfIndex)
  44. }
  45. // Ancillary data socket options
  46. const (
  47. ctlTTL = iota // header field
  48. ctlSrc // header field
  49. ctlDst // header field
  50. ctlInterface // inbound or outbound interface
  51. ctlPacketInfo // inbound or outbound packet path
  52. ctlMax
  53. )
  54. // A ctlOpt represents a binding for ancillary data socket option.
  55. type ctlOpt struct {
  56. name int // option name, must be equal or greater than 1
  57. length int // option length
  58. marshal func([]byte, *ControlMessage) []byte
  59. parse func(*ControlMessage, []byte)
  60. }