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

     1  // Copyright 2012 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 ipv4
     6  
     7  import (
     8  	"net"
     9  	"syscall"
    10  	"time"
    11  )
    12  
    13  // A Conn represents a network endpoint that uses the IPv4 transport.
    14  // It is used to control basic IP-level socket options such as TOS and
    15  // TTL.
    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  // NewConn returns a new Conn.
    27  func NewConn(c net.Conn) *Conn {
    28  	return &Conn{
    29  		genericOpt: genericOpt{Conn: c},
    30  	}
    31  }
    32  
    33  // A PacketConn represents a packet network endpoint that uses the
    34  // IPv4 transport.  It is used to control several IP-level socket
    35  // options including multicasting.  It also provides datagram based
    36  // network I/O methods specific to the IPv4 and higher layer protocols
    37  // such as UDP.
    38  type PacketConn struct {
    39  	genericOpt
    40  	dgramOpt
    41  	payloadHandler
    42  }
    43  
    44  type dgramOpt struct {
    45  	net.PacketConn
    46  }
    47  
    48  func (c *dgramOpt) ok() bool { return c != nil && c.PacketConn != nil }
    49  
    50  // SetControlMessage sets the per packet IP-level socket options.
    51  func (c *PacketConn) SetControlMessage(cf ControlFlags, on bool) error {
    52  	if !c.payloadHandler.ok() {
    53  		return syscall.EINVAL
    54  	}
    55  	fd, err := c.payloadHandler.sysfd()
    56  	if err != nil {
    57  		return err
    58  	}
    59  	return setControlMessage(fd, &c.payloadHandler.rawOpt, cf, on)
    60  }
    61  
    62  // SetDeadline sets the read and write deadlines associated with the
    63  // endpoint.
    64  func (c *PacketConn) SetDeadline(t time.Time) error {
    65  	if !c.payloadHandler.ok() {
    66  		return syscall.EINVAL
    67  	}
    68  	return c.payloadHandler.PacketConn.SetDeadline(t)
    69  }
    70  
    71  // SetReadDeadline sets the read deadline associated with the
    72  // endpoint.
    73  func (c *PacketConn) SetReadDeadline(t time.Time) error {
    74  	if !c.payloadHandler.ok() {
    75  		return syscall.EINVAL
    76  	}
    77  	return c.payloadHandler.PacketConn.SetReadDeadline(t)
    78  }
    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.PacketConn.SetWriteDeadline(t)
    87  }
    88  
    89  // Close closes the endpoint.
    90  func (c *PacketConn) Close() error {
    91  	if !c.payloadHandler.ok() {
    92  		return syscall.EINVAL
    93  	}
    94  	return c.payloadHandler.PacketConn.Close()
    95  }
    96  
    97  // NewPacketConn returns a new PacketConn using c as its underlying
    98  // transport.
    99  func NewPacketConn(c net.PacketConn) *PacketConn {
   100  	p := &PacketConn{
   101  		genericOpt:     genericOpt{Conn: c.(net.Conn)},
   102  		dgramOpt:       dgramOpt{PacketConn: c},
   103  		payloadHandler: payloadHandler{PacketConn: c},
   104  	}
   105  	if _, ok := c.(*net.IPConn); ok && sockOpts[ssoStripHeader].name > 0 {
   106  		if fd, err := p.payloadHandler.sysfd(); err == nil {
   107  			setInt(fd, &sockOpts[ssoStripHeader], boolint(true))
   108  		}
   109  	}
   110  	return p
   111  }
   112  
   113  // A RawConn represents a packet network endpoint that uses the IPv4
   114  // transport.  It is used to control several IP-level socket options
   115  // including IPv4 header manipulation.  It also provides datagram
   116  // based network I/O methods specific to the IPv4 and higher layer
   117  // protocols that handle IPv4 datagram directly such as OSPF, GRE.
   118  type RawConn struct {
   119  	genericOpt
   120  	dgramOpt
   121  	packetHandler
   122  }
   123  
   124  // SetControlMessage sets the per packet IP-level socket options.
   125  func (c *RawConn) SetControlMessage(cf ControlFlags, on bool) error {
   126  	if !c.packetHandler.ok() {
   127  		return syscall.EINVAL
   128  	}
   129  	fd, err := c.packetHandler.sysfd()
   130  	if err != nil {
   131  		return err
   132  	}
   133  	return setControlMessage(fd, &c.packetHandler.rawOpt, cf, on)
   134  }
   135  
   136  // SetDeadline sets the read and write deadlines associated with the
   137  // endpoint.
   138  func (c *RawConn) SetDeadline(t time.Time) error {
   139  	if !c.packetHandler.ok() {
   140  		return syscall.EINVAL
   141  	}
   142  	return c.packetHandler.c.SetDeadline(t)
   143  }
   144  
   145  // SetReadDeadline sets the read deadline associated with the
   146  // endpoint.
   147  func (c *RawConn) SetReadDeadline(t time.Time) error {
   148  	if !c.packetHandler.ok() {
   149  		return syscall.EINVAL
   150  	}
   151  	return c.packetHandler.c.SetReadDeadline(t)
   152  }
   153  
   154  // SetWriteDeadline sets the write deadline associated with the
   155  // endpoint.
   156  func (c *RawConn) SetWriteDeadline(t time.Time) error {
   157  	if !c.packetHandler.ok() {
   158  		return syscall.EINVAL
   159  	}
   160  	return c.packetHandler.c.SetWriteDeadline(t)
   161  }
   162  
   163  // Close closes the endpoint.
   164  func (c *RawConn) Close() error {
   165  	if !c.packetHandler.ok() {
   166  		return syscall.EINVAL
   167  	}
   168  	return c.packetHandler.c.Close()
   169  }
   170  
   171  // NewRawConn returns a new RawConn using c as its underlying
   172  // transport.
   173  func NewRawConn(c net.PacketConn) (*RawConn, error) {
   174  	r := &RawConn{
   175  		genericOpt:    genericOpt{Conn: c.(net.Conn)},
   176  		dgramOpt:      dgramOpt{PacketConn: c},
   177  		packetHandler: packetHandler{c: c.(*net.IPConn)},
   178  	}
   179  	fd, err := r.packetHandler.sysfd()
   180  	if err != nil {
   181  		return nil, err
   182  	}
   183  	if err := setInt(fd, &sockOpts[ssoHeaderPrepend], boolint(true)); err != nil {
   184  		return nil, err
   185  	}
   186  	return r, nil
   187  }