// Copyright 2012 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//go:build aix || darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris || zos

package ipv4

import (
	

	
)

// ReadFrom reads a payload of the received IPv4 datagram, from the
// endpoint c, copying the payload into b. It returns the number of
// bytes copied into b, the control message cm and the source address
// src of the received datagram.
func ( *payloadHandler) ( []byte) ( int,  *ControlMessage,  net.Addr,  error) {
	if !.ok() {
		return 0, nil, nil, errInvalidConn
	}
	.rawOpt.RLock()
	 := socket.Message{
		OOB: NewControlMessage(.rawOpt.cflags),
	}
	.rawOpt.RUnlock()
	switch .PacketConn.(type) {
	case *net.UDPConn:
		.Buffers = [][]byte{}
		if  := .RecvMsg(&, 0);  != nil {
			return 0, nil, nil, &net.OpError{Op: "read", Net: .PacketConn.LocalAddr().Network(), Source: .PacketConn.LocalAddr(), Err: }
		}
	case *net.IPConn:
		 := make([]byte, HeaderLen)
		.Buffers = [][]byte{, }
		if  := .RecvMsg(&, 0);  != nil {
			return 0, nil, nil, &net.OpError{Op: "read", Net: .PacketConn.LocalAddr().Network(), Source: .PacketConn.LocalAddr(), Err: }
		}
		 := int([0]&0x0f) << 2
		if  > len() {
			 :=  - len()
			copy(, [:])
			.N -= 
		} else {
			.N -= 
		}
	default:
		return 0, nil, nil, &net.OpError{Op: "read", Net: .PacketConn.LocalAddr().Network(), Source: .PacketConn.LocalAddr(), Err: errInvalidConnType}
	}
	if .NN > 0 {
		if compatFreeBSD32 {
			adjustFreeBSD32(&)
		}
		 = new(ControlMessage)
		if  := .Parse(.OOB[:.NN]);  != nil {
			return 0, nil, nil, &net.OpError{Op: "read", Net: .PacketConn.LocalAddr().Network(), Source: .PacketConn.LocalAddr(), Err: }
		}
		.Src = netAddrToIP4(.Addr)
	}
	return .N, , .Addr, nil
}

// WriteTo writes a payload of the IPv4 datagram, to the destination
// address dst through the endpoint c, copying the payload from b. It
// returns the number of bytes written. The control message cm allows
// the datagram path and the outgoing interface to be specified.
// Currently only Darwin and Linux support this. The cm may be nil if
// control of the outgoing datagram is not required.
func ( *payloadHandler) ( []byte,  *ControlMessage,  net.Addr) ( int,  error) {
	if !.ok() {
		return 0, errInvalidConn
	}
	 := socket.Message{
		Buffers: [][]byte{},
		OOB:     .Marshal(),
		Addr:    ,
	}
	 = .SendMsg(&, 0)
	if  != nil {
		 = &net.OpError{Op: "write", Net: .PacketConn.LocalAddr().Network(), Source: .PacketConn.LocalAddr(), Addr: opAddr(), Err: }
	}
	return .N, 
}