// Copyright 2010 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 net

import (
	
	
)

// BUG(mikio): On every POSIX platform, reads from the "ip4" network
// using the ReadFrom or ReadFromIP method might not return a complete
// IPv4 packet, including its header, even if there is space
// available. This can occur even in cases where Read or ReadMsgIP
// could return a complete packet. For this reason, it is recommended
// that you do not use these methods if it is important to receive a
// full packet.
//
// The Go 1 compatibility guidelines make it impossible for us to
// change the behavior of these methods; use Read or ReadMsgIP
// instead.

// BUG(mikio): On JS and Plan 9, methods and functions related
// to IPConn are not implemented.

// BUG(mikio): On Windows, the File method of IPConn is not
// implemented.

// IPAddr represents the address of an IP end point.
type IPAddr struct {
	IP   IP
	Zone string // IPv6 scoped addressing zone
}

// Network returns the address's network name, "ip".
func ( *IPAddr) () string { return "ip" }

func ( *IPAddr) () string {
	if  == nil {
		return "<nil>"
	}
	 := ipEmptyString(.IP)
	if .Zone != "" {
		return  + "%" + .Zone
	}
	return 
}

func ( *IPAddr) () bool {
	if  == nil || .IP == nil {
		return true
	}
	return .IP.IsUnspecified()
}

func ( *IPAddr) () Addr {
	if  == nil {
		return nil
	}
	return 
}

// ResolveIPAddr returns an address of IP end point.
//
// The network must be an IP network name.
//
// If the host in the address parameter is not a literal IP address,
// ResolveIPAddr resolves the address to an address of IP end point.
// Otherwise, it parses the address as a literal IP address.
// The address parameter can use a host name, but this is not
// recommended, because it will return at most one of the host name's
// IP addresses.
//
// See func [Dial] for a description of the network and address
// parameters.
func (,  string) (*IPAddr, error) {
	if  == "" { // a hint wildcard for Go 1.0 undocumented behavior
		 = "ip"
	}
	, ,  := parseNetwork(context.Background(), , false)
	if  != nil {
		return nil, 
	}
	switch  {
	case "ip", "ip4", "ip6":
	default:
		return nil, UnknownNetworkError()
	}
	,  := DefaultResolver.internetAddrList(context.Background(), , )
	if  != nil {
		return nil, 
	}
	return .forResolve(, ).(*IPAddr), nil
}

// IPConn is the implementation of the [Conn] and [PacketConn] interfaces
// for IP network connections.
type IPConn struct {
	conn
}

// SyscallConn returns a raw network connection.
// This implements the [syscall.Conn] interface.
func ( *IPConn) () (syscall.RawConn, error) {
	if !.ok() {
		return nil, syscall.EINVAL
	}
	return newRawConn(.fd), nil
}

// ReadFromIP acts like ReadFrom but returns an IPAddr.
func ( *IPConn) ( []byte) (int, *IPAddr, error) {
	if !.ok() {
		return 0, nil, syscall.EINVAL
	}
	, ,  := .readFrom()
	if  != nil {
		 = &OpError{Op: "read", Net: .fd.net, Source: .fd.laddr, Addr: .fd.raddr, Err: }
	}
	return , , 
}

// ReadFrom implements the [PacketConn] ReadFrom method.
func ( *IPConn) ( []byte) (int, Addr, error) {
	if !.ok() {
		return 0, nil, syscall.EINVAL
	}
	, ,  := .readFrom()
	if  != nil {
		 = &OpError{Op: "read", Net: .fd.net, Source: .fd.laddr, Addr: .fd.raddr, Err: }
	}
	if  == nil {
		return , nil, 
	}
	return , , 
}

// ReadMsgIP reads a message from c, copying the payload into b and
// the associated out-of-band data into oob. It returns the number of
// bytes copied into b, the number of bytes copied into oob, the flags
// that were set on the message and the source address of the message.
//
// The packages golang.org/x/net/ipv4 and golang.org/x/net/ipv6 can be
// used to manipulate IP-level socket options in oob.
func ( *IPConn) (,  []byte) (, ,  int,  *IPAddr,  error) {
	if !.ok() {
		return 0, 0, 0, nil, syscall.EINVAL
	}
	, , , ,  = .readMsg(, )
	if  != nil {
		 = &OpError{Op: "read", Net: .fd.net, Source: .fd.laddr, Addr: .fd.raddr, Err: }
	}
	return
}

// WriteToIP acts like [IPConn.WriteTo] but takes an [IPAddr].
func ( *IPConn) ( []byte,  *IPAddr) (int, error) {
	if !.ok() {
		return 0, syscall.EINVAL
	}
	,  := .writeTo(, )
	if  != nil {
		 = &OpError{Op: "write", Net: .fd.net, Source: .fd.laddr, Addr: .opAddr(), Err: }
	}
	return , 
}

// WriteTo implements the [PacketConn] WriteTo method.
func ( *IPConn) ( []byte,  Addr) (int, error) {
	if !.ok() {
		return 0, syscall.EINVAL
	}
	,  := .(*IPAddr)
	if ! {
		return 0, &OpError{Op: "write", Net: .fd.net, Source: .fd.laddr, Addr: , Err: syscall.EINVAL}
	}
	,  := .writeTo(, )
	if  != nil {
		 = &OpError{Op: "write", Net: .fd.net, Source: .fd.laddr, Addr: .opAddr(), Err: }
	}
	return , 
}

// WriteMsgIP writes a message to addr via c, copying the payload from
// b and the associated out-of-band data from oob. It returns the
// number of payload and out-of-band bytes written.
//
// The packages golang.org/x/net/ipv4 and golang.org/x/net/ipv6 can be
// used to manipulate IP-level socket options in oob.
func ( *IPConn) (,  []byte,  *IPAddr) (,  int,  error) {
	if !.ok() {
		return 0, 0, syscall.EINVAL
	}
	, ,  = .writeMsg(, , )
	if  != nil {
		 = &OpError{Op: "write", Net: .fd.net, Source: .fd.laddr, Addr: .opAddr(), Err: }
	}
	return
}

func newIPConn( *netFD) *IPConn { return &IPConn{conn{}} }

// DialIP acts like [Dial] for IP networks.
//
// The network must be an IP network name; see func Dial for details.
//
// If laddr is nil, a local address is automatically chosen.
// If the IP field of raddr is nil or an unspecified IP address, the
// local system is assumed.
func ( string, ,  *IPAddr) (*IPConn, error) {
	if  == nil {
		return nil, &OpError{Op: "dial", Net: , Source: .opAddr(), Addr: nil, Err: errMissingAddress}
	}
	 := &sysDialer{network: , address: .String()}
	,  := .dialIP(context.Background(), , )
	if  != nil {
		return nil, &OpError{Op: "dial", Net: , Source: .opAddr(), Addr: .opAddr(), Err: }
	}
	return , nil
}

// ListenIP acts like [ListenPacket] for IP networks.
//
// The network must be an IP network name; see func Dial for details.
//
// If the IP field of laddr is nil or an unspecified IP address,
// ListenIP listens on all available IP addresses of the local system
// except multicast IP addresses.
func ( string,  *IPAddr) (*IPConn, error) {
	if  == nil {
		 = &IPAddr{}
	}
	 := &sysListener{network: , address: .String()}
	,  := .listenIP(context.Background(), )
	if  != nil {
		return nil, &OpError{Op: "listen", Net: , Source: nil, Addr: .opAddr(), Err: }
	}
	return , nil
}