endpoint.go 3.0 KB

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