github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/net/ipv4/payload_cmsg.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  // +build !plan9,!solaris,!windows
     6  
     7  package ipv4
     8  
     9  import (
    10  	"net"
    11  	"syscall"
    12  )
    13  
    14  // ReadFrom reads a payload of the received IPv4 datagram, from the
    15  // endpoint c, copying the payload into b.  It returns the number of
    16  // bytes copied into b, the control message cm and the source address
    17  // src of the received datagram.
    18  func (c *payloadHandler) ReadFrom(b []byte) (n int, cm *ControlMessage, src net.Addr, err error) {
    19  	if !c.ok() {
    20  		return 0, nil, nil, syscall.EINVAL
    21  	}
    22  	oob := newControlMessage(&c.rawOpt)
    23  	var oobn int
    24  	switch c := c.PacketConn.(type) {
    25  	case *net.UDPConn:
    26  		if n, oobn, _, src, err = c.ReadMsgUDP(b, oob); err != nil {
    27  			return 0, nil, nil, err
    28  		}
    29  	case *net.IPConn:
    30  		if sockOpts[ssoStripHeader].name > 0 {
    31  			if n, oobn, _, src, err = c.ReadMsgIP(b, oob); err != nil {
    32  				return 0, nil, nil, err
    33  			}
    34  		} else {
    35  			nb := make([]byte, maxHeaderLen+len(b))
    36  			if n, oobn, _, src, err = c.ReadMsgIP(nb, oob); err != nil {
    37  				return 0, nil, nil, err
    38  			}
    39  			hdrlen := int(nb[0]&0x0f) << 2
    40  			copy(b, nb[hdrlen:])
    41  			n -= hdrlen
    42  		}
    43  	default:
    44  		return 0, nil, nil, errInvalidConnType
    45  	}
    46  	if cm, err = parseControlMessage(oob[:oobn]); err != nil {
    47  		return 0, nil, nil, err
    48  	}
    49  	if cm != nil {
    50  		cm.Src = netAddrToIP4(src)
    51  	}
    52  	return
    53  }
    54  
    55  // WriteTo writes a payload of the IPv4 datagram, to the destination
    56  // address dst through the endpoint c, copying the payload from b.  It
    57  // returns the number of bytes written.  The control message cm allows
    58  // the datagram path and the outgoing interface to be specified.
    59  // Currently only Darwin and Linux support this.  The cm may be nil if
    60  // control of the outgoing datagram is not required.
    61  func (c *payloadHandler) WriteTo(b []byte, cm *ControlMessage, dst net.Addr) (n int, err error) {
    62  	if !c.ok() {
    63  		return 0, syscall.EINVAL
    64  	}
    65  	oob := marshalControlMessage(cm)
    66  	if dst == nil {
    67  		return 0, errMissingAddress
    68  	}
    69  	switch c := c.PacketConn.(type) {
    70  	case *net.UDPConn:
    71  		n, _, err = c.WriteMsgUDP(b, oob, dst.(*net.UDPAddr))
    72  	case *net.IPConn:
    73  		n, _, err = c.WriteMsgIP(b, oob, dst.(*net.IPAddr))
    74  	default:
    75  		return 0, errInvalidConnType
    76  	}
    77  	if err != nil {
    78  		return 0, err
    79  	}
    80  	return
    81  }