// 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.

package ipv4

import (
	

	
)

// BUG(mikio): On Windows, the ReadFrom and WriteTo methods of RawConn
// are not implemented.

// A packetHandler represents the IPv4 datagram handler.
type packetHandler struct {
	*net.IPConn
	*socket.Conn
	rawOpt
}

func ( *packetHandler) () bool { return  != nil && .IPConn != nil && .Conn != nil }

// ReadFrom reads an IPv4 datagram from the endpoint c, copying the
// datagram into b. It returns the received datagram as the IPv4
// header h, the payload p and the control message cm.
func ( *packetHandler) ( []byte) ( *Header,  []byte,  *ControlMessage,  error) {
	if !.ok() {
		return nil, nil, nil, errInvalidConn
	}
	.rawOpt.RLock()
	 := socket.Message{
		Buffers: [][]byte{},
		OOB:     NewControlMessage(.rawOpt.cflags),
	}
	.rawOpt.RUnlock()
	if  := .RecvMsg(&, 0);  != nil {
		return nil, nil, nil, &net.OpError{Op: "read", Net: .IPConn.LocalAddr().Network(), Source: .IPConn.LocalAddr(), Err: }
	}
	var  []byte
	if , ,  = slicePacket([:.N]);  != nil {
		return nil, nil, nil, &net.OpError{Op: "read", Net: .IPConn.LocalAddr().Network(), Source: .IPConn.LocalAddr(), Err: }
	}
	if ,  = ParseHeader();  != nil {
		return nil, nil, nil, &net.OpError{Op: "read", Net: .IPConn.LocalAddr().Network(), Source: .IPConn.LocalAddr(), Err: }
	}
	if .NN > 0 {
		if compatFreeBSD32 {
			adjustFreeBSD32(&)
		}
		 = new(ControlMessage)
		if  := .Parse(.OOB[:.NN]);  != nil {
			return nil, nil, nil, &net.OpError{Op: "read", Net: .IPConn.LocalAddr().Network(), Source: .IPConn.LocalAddr(), Err: }
		}
	}
	if ,  := .Addr.(*net.IPAddr);  &&  != nil {
		.Src = .IP
	}
	return
}

func slicePacket( []byte) (,  []byte,  error) {
	if len() < HeaderLen {
		return nil, nil, errHeaderTooShort
	}
	 := int([0]&0x0f) << 2
	return [:], [:], nil
}

// WriteTo writes an IPv4 datagram through the endpoint c, copying the
// datagram from the IPv4 header h and the payload p. 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.
//
// The IPv4 header h must contain appropriate fields that include:
//
//	Version       = <must be specified>
//	Len           = <must be specified>
//	TOS           = <must be specified>
//	TotalLen      = <must be specified>
//	ID            = platform sets an appropriate value if ID is zero
//	FragOff       = <must be specified>
//	TTL           = <must be specified>
//	Protocol      = <must be specified>
//	Checksum      = platform sets an appropriate value if Checksum is zero
//	Src           = platform sets an appropriate value if Src is nil
//	Dst           = <must be specified>
//	Options       = optional
func ( *packetHandler) ( *Header,  []byte,  *ControlMessage) error {
	if !.ok() {
		return errInvalidConn
	}
	 := socket.Message{
		OOB: .Marshal(),
	}
	,  := .Marshal()
	if  != nil {
		return 
	}
	.Buffers = [][]byte{, }
	 := new(net.IPAddr)
	if  != nil {
		if  := .Dst.To4();  != nil {
			.IP = 
		}
	}
	if .IP == nil {
		.IP = .Dst
	}
	.Addr = 
	if  := .SendMsg(&, 0);  != nil {
		return &net.OpError{Op: "write", Net: .IPConn.LocalAddr().Network(), Source: .IPConn.LocalAddr(), Addr: opAddr(), Err: }
	}
	return nil
}