payload_cmsg.go raw

   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  //go:build aix || darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris || zos
   6  
   7  package ipv4
   8  
   9  import (
  10  	"net"
  11  
  12  	"golang.org/x/net/internal/socket"
  13  )
  14  
  15  // ReadFrom reads a payload of the received IPv4 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  		OOB: NewControlMessage(c.rawOpt.cflags),
  26  	}
  27  	c.rawOpt.RUnlock()
  28  	switch c.PacketConn.(type) {
  29  	case *net.UDPConn:
  30  		m.Buffers = [][]byte{b}
  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  		h := make([]byte, HeaderLen)
  36  		m.Buffers = [][]byte{h, b}
  37  		if err := c.RecvMsg(&m, 0); err != nil {
  38  			return 0, nil, nil, &net.OpError{Op: "read", Net: c.PacketConn.LocalAddr().Network(), Source: c.PacketConn.LocalAddr(), Err: err}
  39  		}
  40  		hdrlen := int(h[0]&0x0f) << 2
  41  		if hdrlen > len(h) {
  42  			d := hdrlen - len(h)
  43  			copy(b, b[d:])
  44  			m.N -= d
  45  		} else {
  46  			m.N -= hdrlen
  47  		}
  48  	default:
  49  		return 0, nil, nil, &net.OpError{Op: "read", Net: c.PacketConn.LocalAddr().Network(), Source: c.PacketConn.LocalAddr(), Err: errInvalidConnType}
  50  	}
  51  	if m.NN > 0 {
  52  		if compatFreeBSD32 {
  53  			adjustFreeBSD32(&m)
  54  		}
  55  		cm = new(ControlMessage)
  56  		if err := cm.Parse(m.OOB[:m.NN]); err != nil {
  57  			return 0, nil, nil, &net.OpError{Op: "read", Net: c.PacketConn.LocalAddr().Network(), Source: c.PacketConn.LocalAddr(), Err: err}
  58  		}
  59  		cm.Src = netAddrToIP4(m.Addr)
  60  	}
  61  	return m.N, cm, m.Addr, nil
  62  }
  63  
  64  // WriteTo writes a payload of the IPv4 datagram, to the destination
  65  // address dst through the endpoint c, copying the payload from b. It
  66  // returns the number of bytes written. The control message cm allows
  67  // the datagram path and the outgoing interface to be specified.
  68  // Currently only Darwin and Linux support this. The cm may be nil if
  69  // control of the outgoing datagram is not required.
  70  func (c *payloadHandler) WriteTo(b []byte, cm *ControlMessage, dst net.Addr) (n int, err error) {
  71  	if !c.ok() {
  72  		return 0, errInvalidConn
  73  	}
  74  	m := socket.Message{
  75  		Buffers: [][]byte{b},
  76  		OOB:     cm.Marshal(),
  77  		Addr:    dst,
  78  	}
  79  	err = c.SendMsg(&m, 0)
  80  	if err != nil {
  81  		err = &net.OpError{Op: "write", Net: c.PacketConn.LocalAddr().Network(), Source: c.PacketConn.LocalAddr(), Addr: opAddr(dst), Err: err}
  82  	}
  83  	return m.N, err
  84  }
  85