endpoint.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. )
  9. // A Conn represents a network endpoint that uses IPv6 transport.
  10. // It allows to set basic IP-level socket options such as traffic
  11. // class and hop limit.
  12. type Conn struct {
  13. genericOpt
  14. }
  15. type genericOpt struct {
  16. net.Conn
  17. }
  18. func (c *genericOpt) ok() bool { return c != nil && c.Conn != nil }
  19. // PathMTU returns a path MTU value for the destination associated
  20. // with the endpoint.
  21. func (c *Conn) PathMTU() (int, error) {
  22. if !c.genericOpt.ok() {
  23. return 0, syscall.EINVAL
  24. }
  25. fd, err := c.genericOpt.sysfd()
  26. if err != nil {
  27. return 0, err
  28. }
  29. return ipv6PathMTU(fd)
  30. }
  31. // NewConn returns a new Conn.
  32. func NewConn(c net.Conn) *Conn {
  33. return &Conn{
  34. genericOpt: genericOpt{Conn: c},
  35. }
  36. }
  37. // A PacketConn represents a packet network endpoint that uses IPv6
  38. // transport. It is used to control several IP-level socket options
  39. // including IPv6 header manipulation. It also provides datagram
  40. // based network I/O methods specific to the IPv6 and higher layer
  41. // protocols such as OSPF, GRE, and UDP.
  42. type PacketConn struct {
  43. genericOpt
  44. dgramOpt
  45. payloadHandler
  46. }
  47. type dgramOpt struct {
  48. net.PacketConn
  49. }
  50. func (c *dgramOpt) ok() bool { return c != nil && c.PacketConn != nil }
  51. // SetControlMessage allows to receive the per packet basis IP-level
  52. // socket options.
  53. func (c *PacketConn) SetControlMessage(cf ControlFlags, on bool) error {
  54. if !c.payloadHandler.ok() {
  55. return syscall.EINVAL
  56. }
  57. fd, err := c.payloadHandler.sysfd()
  58. if err != nil {
  59. return err
  60. }
  61. return setControlMessage(fd, &c.payloadHandler.rawOpt, cf, on)
  62. }
  63. // NewPacketConn returns a new PacketConn using c as its underlying
  64. // transport.
  65. func NewPacketConn(c net.PacketConn) *PacketConn {
  66. return &PacketConn{
  67. genericOpt: genericOpt{Conn: c.(net.Conn)},
  68. dgramOpt: dgramOpt{PacketConn: c},
  69. payloadHandler: payloadHandler{PacketConn: c},
  70. }
  71. }