control.go 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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.Mutex
  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. // A ControlMessage represents per packet basis IP-level socket
  50. // options.
  51. type ControlMessage struct {
  52. // Receiving socket options: SetControlMessage allows to
  53. // receive the options from the protocol stack using ReadFrom
  54. // method of PacketConn.
  55. //
  56. // Specifying socket options: ControlMessage for WriteTo
  57. // method of PacketConn allows to send the options to the
  58. // protocol stack.
  59. //
  60. TrafficClass int // traffic class, must be 1 <= value <= 255 when specifying
  61. HopLimit int // hop limit, must be 1 <= value <= 255 when specifying
  62. Src net.IP // source address, specifying only
  63. Dst net.IP // destination address, receiving only
  64. IfIndex int // interface index, must be 1 <= value when specifying
  65. NextHop net.IP // next hop address, specifying only
  66. MTU int // path MTU, receiving only
  67. }
  68. func (cm *ControlMessage) String() string {
  69. if cm == nil {
  70. return "<nil>"
  71. }
  72. 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)
  73. }