control.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. errNotSupported = errors.New("not supported")
  13. errMissingAddress = errors.New("missing address")
  14. errInvalidConnType = errors.New("invalid conn type")
  15. errNoSuchInterface = errors.New("no such interface")
  16. )
  17. // References:
  18. //
  19. // RFC 2292 Advanced Sockets API for IPv6
  20. // http://tools.ietf.org/html/rfc2292
  21. // RFC 2460 Internet Protocol, Version 6 (IPv6) Specification
  22. // http://tools.ietf.org/html/rfc2460
  23. // RFC 3493 Basic Socket Interface Extensions for IPv6
  24. // http://tools.ietf.org/html/rfc3493.html
  25. // RFC 3542 Advanced Sockets Application Program Interface (API) for IPv6
  26. // http://tools.ietf.org/html/rfc3542
  27. //
  28. // Note that RFC 3542 obsoltes RFC 2292 but OS X Snow Leopard and the
  29. // former still support RFC 2292 only. Please be aware that almost
  30. // all protocol implementations prohibit using a combination of RFC
  31. // 2292 and RFC 3542 for some practical reasons.
  32. type rawOpt struct {
  33. sync.Mutex
  34. cflags ControlFlags
  35. }
  36. func (c *rawOpt) set(f ControlFlags) { c.cflags |= f }
  37. func (c *rawOpt) clear(f ControlFlags) { c.cflags &^= f }
  38. func (c *rawOpt) isset(f ControlFlags) bool { return c.cflags&f != 0 }
  39. // A ControlFlags reprensents per packet basis IP-level socket option
  40. // control flags.
  41. type ControlFlags uint
  42. const (
  43. FlagTrafficClass ControlFlags = 1 << iota // pass the traffic class on the received packet
  44. FlagHopLimit // pass the hop limit on the received packet
  45. FlagSrc // pass the source address on the received packet
  46. FlagDst // pass the destination address on the received packet
  47. FlagInterface // pass the interface index on the received packet
  48. FlagPathMTU // pass the path MTU on the received packet path
  49. )
  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. }