github.com/Andyfoo/golang/x/net@v0.0.0-20190901054642-57c1bf301704/ipv6/payload_cmsg.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  // +build aix darwin dragonfly freebsd linux netbsd openbsd solaris
     6  
     7  package ipv6
     8  
     9  import (
    10  	"net"
    11  
    12  	"github.com/Andyfoo/golang/x/net/internal/socket"
    13  )
    14  
    15  // ReadFrom reads a payload of the received IPv6 datagram, from the
    16  // endpoint c, copying the payload into b. It returns the number of
    17  // bytes copied into b, the control message cm and the source address
    18  // src of the received datagram.
    19  func (c *payloadHandler) ReadFrom(b []byte) (n int, cm *ControlMessage, src net.Addr, err error) {
    20  	if !c.ok() {
    21  		return 0, nil, nil, errInvalidConn
    22  	}
    23  	c.rawOpt.RLock()
    24  	m := socket.Message{
    25  		Buffers: [][]byte{b},
    26  		OOB:     NewControlMessage(c.rawOpt.cflags),
    27  	}
    28  	c.rawOpt.RUnlock()
    29  	switch c.PacketConn.(type) {
    30  	case *net.UDPConn:
    31  		if err := c.RecvMsg(&m, 0); err != nil {
    32  			return 0, nil, nil, &net.OpError{Op: "read", Net: c.PacketConn.LocalAddr().Network(), Source: c.PacketConn.LocalAddr(), Err: err}
    33  		}
    34  	case *net.IPConn:
    35  		if err := c.RecvMsg(&m, 0); err != nil {
    36  			return 0, nil, nil, &net.OpError{Op: "read", Net: c.PacketConn.LocalAddr().Network(), Source: c.PacketConn.LocalAddr(), Err: err}
    37  		}
    38  	default:
    39  		return 0, nil, nil, &net.OpError{Op: "read", Net: c.PacketConn.LocalAddr().Network(), Source: c.PacketConn.LocalAddr(), Err: errInvalidConnType}
    40  	}
    41  	if m.NN > 0 {
    42  		cm = new(ControlMessage)
    43  		if err := cm.Parse(m.OOB[:m.NN]); err != nil {
    44  			return 0, nil, nil, &net.OpError{Op: "read", Net: c.PacketConn.LocalAddr().Network(), Source: c.PacketConn.LocalAddr(), Err: err}
    45  		}
    46  		cm.Src = netAddrToIP16(m.Addr)
    47  	}
    48  	return m.N, cm, m.Addr, nil
    49  }
    50  
    51  // WriteTo writes a payload of the IPv6 datagram, to the destination
    52  // address dst through the endpoint c, copying the payload from b. It
    53  // returns the number of bytes written. The control message cm allows
    54  // the IPv6 header fields and the datagram path to be specified. The
    55  // cm may be nil if control of the outgoing datagram is not required.
    56  func (c *payloadHandler) WriteTo(b []byte, cm *ControlMessage, dst net.Addr) (n int, err error) {
    57  	if !c.ok() {
    58  		return 0, errInvalidConn
    59  	}
    60  	m := socket.Message{
    61  		Buffers: [][]byte{b},
    62  		OOB:     cm.Marshal(),
    63  		Addr:    dst,
    64  	}
    65  	err = c.SendMsg(&m, 0)
    66  	if err != nil {
    67  		err = &net.OpError{Op: "write", Net: c.PacketConn.LocalAddr().Network(), Source: c.PacketConn.LocalAddr(), Addr: opAddr(dst), Err: err}
    68  	}
    69  	return m.N, err
    70  }