endpoint.go 2.9 KB

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