github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/net/ipv6/endpoint.go (about)

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