You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

131 lines
3.2 KiB

  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. "net"
  7. "syscall"
  8. "time"
  9. "golang.org/x/net/internal/netreflect"
  10. )
  11. // BUG(mikio): On Windows, the JoinSourceSpecificGroup,
  12. // LeaveSourceSpecificGroup, ExcludeSourceSpecificGroup and
  13. // IncludeSourceSpecificGroup methods of PacketConn are not
  14. // implemented.
  15. // A Conn represents a network endpoint that uses IPv6 transport.
  16. // It allows to set basic IP-level socket options such as traffic
  17. // class and hop limit.
  18. type Conn struct {
  19. genericOpt
  20. }
  21. type genericOpt struct {
  22. net.Conn
  23. }
  24. func (c *genericOpt) ok() bool { return c != nil && c.Conn != nil }
  25. // PathMTU returns a path MTU value for the destination associated
  26. // with the endpoint.
  27. func (c *Conn) PathMTU() (int, error) {
  28. if !c.genericOpt.ok() {
  29. return 0, syscall.EINVAL
  30. }
  31. s, err := netreflect.SocketOf(c.genericOpt.Conn)
  32. if err != nil {
  33. return 0, err
  34. }
  35. _, mtu, err := getMTUInfo(s, &sockOpts[ssoPathMTU])
  36. if err != nil {
  37. return 0, err
  38. }
  39. return mtu, nil
  40. }
  41. // NewConn returns a new Conn.
  42. func NewConn(c net.Conn) *Conn {
  43. return &Conn{
  44. genericOpt: genericOpt{Conn: c},
  45. }
  46. }
  47. // A PacketConn represents a packet network endpoint that uses IPv6
  48. // transport. It is used to control several IP-level socket options
  49. // including IPv6 header manipulation. It also provides datagram
  50. // based network I/O methods specific to the IPv6 and higher layer
  51. // protocols such as OSPF, GRE, and UDP.
  52. type PacketConn struct {
  53. genericOpt
  54. dgramOpt
  55. payloadHandler
  56. }
  57. type dgramOpt struct {
  58. net.PacketConn
  59. }
  60. func (c *dgramOpt) ok() bool { return c != nil && c.PacketConn != nil }
  61. // SetControlMessage allows to receive the per packet basis IP-level
  62. // socket options.
  63. func (c *PacketConn) SetControlMessage(cf ControlFlags, on bool) error {
  64. if !c.payloadHandler.ok() {
  65. return syscall.EINVAL
  66. }
  67. s, err := netreflect.PacketSocketOf(c.dgramOpt.PacketConn)
  68. if err != nil {
  69. return err
  70. }
  71. return setControlMessage(s, &c.payloadHandler.rawOpt, cf, on)
  72. }
  73. // SetDeadline sets the read and write deadlines associated with the
  74. // endpoint.
  75. func (c *PacketConn) SetDeadline(t time.Time) error {
  76. if !c.payloadHandler.ok() {
  77. return syscall.EINVAL
  78. }
  79. return c.payloadHandler.SetDeadline(t)
  80. }
  81. // SetReadDeadline sets the read deadline associated with the
  82. // endpoint.
  83. func (c *PacketConn) SetReadDeadline(t time.Time) error {
  84. if !c.payloadHandler.ok() {
  85. return syscall.EINVAL
  86. }
  87. return c.payloadHandler.SetReadDeadline(t)
  88. }
  89. // SetWriteDeadline sets the write deadline associated with the
  90. // endpoint.
  91. func (c *PacketConn) SetWriteDeadline(t time.Time) error {
  92. if !c.payloadHandler.ok() {
  93. return syscall.EINVAL
  94. }
  95. return c.payloadHandler.SetWriteDeadline(t)
  96. }
  97. // Close closes the endpoint.
  98. func (c *PacketConn) Close() error {
  99. if !c.payloadHandler.ok() {
  100. return syscall.EINVAL
  101. }
  102. return c.payloadHandler.Close()
  103. }
  104. // NewPacketConn returns a new PacketConn using c as its underlying
  105. // transport.
  106. func NewPacketConn(c net.PacketConn) *PacketConn {
  107. return &PacketConn{
  108. genericOpt: genericOpt{Conn: c.(net.Conn)},
  109. dgramOpt: dgramOpt{PacketConn: c},
  110. payloadHandler: payloadHandler{PacketConn: c},
  111. }
  112. }