control.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. // References:
  17. //
  18. // RFC 2292 Advanced Sockets API for IPv6
  19. // http://tools.ietf.org/html/rfc2292
  20. // RFC 2460 Internet Protocol, Version 6 (IPv6) Specification
  21. // http://tools.ietf.org/html/rfc2460
  22. // RFC 3493 Basic Socket Interface Extensions for IPv6
  23. // http://tools.ietf.org/html/rfc3493.html
  24. // RFC 3542 Advanced Sockets Application Program Interface (API) for IPv6
  25. // http://tools.ietf.org/html/rfc3542
  26. // RFC 3678 Socket Interface Extensions for Multicast Source Filters
  27. // http://tools.ietf.org/html/rfc3678
  28. // RFC 4607 Source-Specific Multicast for IP
  29. // http://tools.ietf.org/html/rfc4607
  30. //
  31. // Note that RFC 3542 obsoletes RFC 2292 but OS X Snow Leopard and the
  32. // former still support RFC 2292 only. Please be aware that almost
  33. // all protocol implementations prohibit using a combination of RFC
  34. // 2292 and RFC 3542 for some practical reasons.
  35. type rawOpt struct {
  36. sync.RWMutex
  37. cflags ControlFlags
  38. }
  39. func (c *rawOpt) set(f ControlFlags) { c.cflags |= f }
  40. func (c *rawOpt) clear(f ControlFlags) { c.cflags &^= f }
  41. func (c *rawOpt) isset(f ControlFlags) bool { return c.cflags&f != 0 }
  42. // A ControlFlags represents per packet basis IP-level socket option
  43. // control flags.
  44. type ControlFlags uint
  45. const (
  46. FlagTrafficClass ControlFlags = 1 << iota // pass the traffic class on the received packet
  47. FlagHopLimit // pass the hop limit on the received packet
  48. FlagSrc // pass the source address on the received packet
  49. FlagDst // pass the destination address on the received packet
  50. FlagInterface // pass the interface index on the received packet
  51. FlagPathMTU // pass the path MTU on the received packet path
  52. )
  53. const flagPacketInfo = FlagDst | FlagInterface
  54. // A ControlMessage represents per packet basis IP-level socket
  55. // options.
  56. type ControlMessage struct {
  57. // Receiving socket options: SetControlMessage allows to
  58. // receive the options from the protocol stack using ReadFrom
  59. // method of PacketConn.
  60. //
  61. // Specifying socket options: ControlMessage for WriteTo
  62. // method of PacketConn allows to send the options to the
  63. // protocol stack.
  64. //
  65. TrafficClass int // traffic class, must be 1 <= value <= 255 when specifying
  66. HopLimit int // hop limit, must be 1 <= value <= 255 when specifying
  67. Src net.IP // source address, specifying only
  68. Dst net.IP // destination address, receiving only
  69. IfIndex int // interface index, must be 1 <= value when specifying
  70. NextHop net.IP // next hop address, specifying only
  71. MTU int // path MTU, receiving only
  72. }
  73. func (cm *ControlMessage) String() string {
  74. if cm == nil {
  75. return "<nil>"
  76. }
  77. 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)
  78. }
  79. // Ancillary data socket options
  80. const (
  81. ctlTrafficClass = iota // header field
  82. ctlHopLimit // header field
  83. ctlPacketInfo // inbound or outbound packet path
  84. ctlNextHop // nexthop
  85. ctlPathMTU // path mtu
  86. ctlMax
  87. )
  88. // A ctlOpt represents a binding for ancillary data socket option.
  89. type ctlOpt struct {
  90. name int // option name, must be equal or greater than 1
  91. length int // option length
  92. marshal func([]byte, *ControlMessage) []byte
  93. parse func(*ControlMessage, []byte)
  94. }