control.go 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // Copyright 2013 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 ipv6
  5. import (
  6. "fmt"
  7. "net"
  8. "sync"
  9. )
  10. // Note that RFC 3542 obsoletes RFC 2292 but OS X Snow Leopard and the
  11. // former still support RFC 2292 only. Please be aware that almost
  12. // all protocol implementations prohibit using a combination of RFC
  13. // 2292 and RFC 3542 for some practical reasons.
  14. type rawOpt struct {
  15. sync.RWMutex
  16. cflags ControlFlags
  17. }
  18. func (c *rawOpt) set(f ControlFlags) { c.cflags |= f }
  19. func (c *rawOpt) clear(f ControlFlags) { c.cflags &^= f }
  20. func (c *rawOpt) isset(f ControlFlags) bool { return c.cflags&f != 0 }
  21. // A ControlFlags represents per packet basis IP-level socket option
  22. // control flags.
  23. type ControlFlags uint
  24. const (
  25. FlagTrafficClass ControlFlags = 1 << iota // pass the traffic class on the received packet
  26. FlagHopLimit // pass the hop limit on the received packet
  27. FlagSrc // pass the source address on the received packet
  28. FlagDst // pass the destination address on the received packet
  29. FlagInterface // pass the interface index on the received packet
  30. FlagPathMTU // pass the path MTU on the received packet path
  31. )
  32. const flagPacketInfo = FlagDst | FlagInterface
  33. // A ControlMessage represents per packet basis IP-level socket
  34. // options.
  35. type ControlMessage struct {
  36. // Receiving socket options: SetControlMessage allows to
  37. // receive the options from the protocol stack using ReadFrom
  38. // method of PacketConn.
  39. //
  40. // Specifying socket options: ControlMessage for WriteTo
  41. // method of PacketConn allows to send the options to the
  42. // protocol stack.
  43. //
  44. TrafficClass int // traffic class, must be 1 <= value <= 255 when specifying
  45. HopLimit int // hop limit, must be 1 <= value <= 255 when specifying
  46. Src net.IP // source address, specifying only
  47. Dst net.IP // destination address, receiving only
  48. IfIndex int // interface index, must be 1 <= value when specifying
  49. NextHop net.IP // next hop address, specifying only
  50. MTU int // path MTU, receiving only
  51. }
  52. func (cm *ControlMessage) String() string {
  53. if cm == nil {
  54. return "<nil>"
  55. }
  56. return fmt.Sprintf("tclass=%#x hoplim=%d src=%v dst=%v ifindex=%d nexthop=%v mtu=%d", cm.TrafficClass, cm.HopLimit, cm.Src, cm.Dst, cm.IfIndex, cm.NextHop, cm.MTU)
  57. }
  58. // Ancillary data socket options
  59. const (
  60. ctlTrafficClass = iota // header field
  61. ctlHopLimit // header field
  62. ctlPacketInfo // inbound or outbound packet path
  63. ctlNextHop // nexthop
  64. ctlPathMTU // path mtu
  65. ctlMax
  66. )
  67. // A ctlOpt represents a binding for ancillary data socket option.
  68. type ctlOpt struct {
  69. name int // option name, must be equal or greater than 1
  70. length int // option length
  71. marshal func([]byte, *ControlMessage) []byte
  72. parse func(*ControlMessage, []byte)
  73. }