control.go 3.6 KB

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