control.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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.Mutex
  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: %v, src: %v, dst: %v, ifindex: %v", cm.TTL, cm.Src, cm.Dst, cm.IfIndex)
  44. }