control.go 3.2 KB

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