endpoint.go 2.9 KB

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