// 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 netimport ()// 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.typeIPAddrstruct { 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 {returntrue }return .IP.IsUnspecified()}func ( *IPAddr) () Addr {if == nil {returnnil }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 {returnnil, }switch {case"ip", "ip4", "ip6":default:returnnil, UnknownNetworkError() } , := DefaultResolver.internetAddrList(context.Background(), , )if != nil {returnnil, }return .forResolve(, ).(*IPAddr), nil}// IPConn is the implementation of the [Conn] and [PacketConn] interfaces// for IP network connections.typeIPConnstruct {conn}// SyscallConn returns a raw network connection.// This implements the [syscall.Conn] interface.func ( *IPConn) () (syscall.RawConn, error) {if !.ok() {returnnil, syscall.EINVAL }returnnewRawConn(.fd), nil}// ReadFromIP acts like ReadFrom but returns an IPAddr.func ( *IPConn) ( []byte) (int, *IPAddr, error) {if !.ok() {return0, 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() {return0, 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() {return0, 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() {return0, 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() {return0, syscall.EINVAL } , := .(*IPAddr)if ! {return0, &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() {return0, 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 {returnnil, &OpError{Op: "dial", Net: , Source: .opAddr(), Addr: nil, Err: errMissingAddress} } := &sysDialer{network: , address: .String()} , := .dialIP(context.Background(), , )if != nil {returnnil, &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 {returnnil, &OpError{Op: "listen", Net: , Source: nil, Addr: .opAddr(), Err: } }return , nil}
The pages are generated with Goldsv0.7.0-preview. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.