endpoint.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. "time"
  8. "golang.org/x/net/internal/socket"
  9. )
  10. // BUG(mikio): On Windows, the JoinSourceSpecificGroup,
  11. // LeaveSourceSpecificGroup, ExcludeSourceSpecificGroup and
  12. // IncludeSourceSpecificGroup methods of PacketConn are not
  13. // implemented.
  14. // A Conn represents a network endpoint that uses IPv6 transport.
  15. // It allows to set basic IP-level socket options such as traffic
  16. // class and hop limit.
  17. type Conn struct {
  18. genericOpt
  19. }
  20. type genericOpt struct {
  21. *socket.Conn
  22. }
  23. func (c *genericOpt) ok() bool { return c != nil && c.Conn != nil }
  24. // PathMTU returns a path MTU value for the destination associated
  25. // with the endpoint.
  26. func (c *Conn) PathMTU() (int, error) {
  27. if !c.ok() {
  28. return 0, errInvalidConn
  29. }
  30. so, ok := sockOpts[ssoPathMTU]
  31. if !ok {
  32. return 0, errNotImplemented
  33. }
  34. _, mtu, err := so.getMTUInfo(c.Conn)
  35. if err != nil {
  36. return 0, err
  37. }
  38. return mtu, nil
  39. }
  40. // NewConn returns a new Conn.
  41. func NewConn(c net.Conn) *Conn {
  42. cc, _ := socket.NewConn(c)
  43. return &Conn{
  44. genericOpt: genericOpt{Conn: cc},
  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. *socket.Conn
  59. }
  60. func (c *dgramOpt) ok() bool { return c != nil && c.Conn != 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 errInvalidConn
  66. }
  67. return setControlMessage(c.dgramOpt.Conn, &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 errInvalidConn
  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 errInvalidConn
  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 errInvalidConn
  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 errInvalidConn
  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. cc, _ := socket.NewConn(c.(net.Conn))
  104. return &PacketConn{
  105. genericOpt: genericOpt{Conn: cc},
  106. dgramOpt: dgramOpt{Conn: cc},
  107. payloadHandler: payloadHandler{PacketConn: c, Conn: cc},
  108. }
  109. }