net

package
v0.0.0-...-1d03baa Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 21, 2013 License: BSD-3-Clause Imports: 11 Imported by: 0

Documentation

Overview

Package net provides a portable interface for network I/O, including TCP/IP, UDP, domain name resolution, and Unix domain sockets.

Although the package provides access to low-level networking primitives, most clients will need only the basic interface provided by the Dial, Listen, and Accept functions and the associated Conn and Listener interfaces. The crypto/tls package uses the same interfaces and similar Dial and Listen functions.

The Dial function connects to a server:

conn, err := net.Dial("tcp", "google.com:80")
if err != nil {
	// handle error
}
fmt.Fprintf(conn, "GET / HTTP/1.0\r\n\r\n")
status, err := bufio.NewReader(conn).ReadString('\n')
// ...

The Listen function creates servers:

ln, err := net.Listen("tcp", ":8080")
if err != nil {
	// handle error
}
for {
	conn, err := ln.Accept()
	if err != nil {
		// handle error
		continue
	}
	go handleConnection(conn)
}

Index

Examples

Constants

View Source
const (
	IPv4len = 4
	IPv6len = 16
)

IP address lengths (bytes).

Variables

View Source
var (
	IPv4bcast     = IPv4(255, 255, 255, 255) // broadcast
	IPv4allsys    = IPv4(224, 0, 0, 1)       // all systems
	IPv4allrouter = IPv4(224, 0, 0, 2)       // all routers
	IPv4zero      = IPv4(0, 0, 0, 0)         // all zeros
)

Well-known IPv4 addresses

View Source
var (
	IPv6zero                   = IP{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
	IPv6unspecified            = IP{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
	IPv6loopback               = IP{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}
	IPv6interfacelocalallnodes = IP{0xff, 0x01, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x01}
	IPv6linklocalallnodes      = IP{0xff, 0x02, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x01}
	IPv6linklocalallrouters    = IP{0xff, 0x02, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x02}
)

Well-known IPv6 addresses

View Source
var ErrWriteToConnected = errors.New("use of WriteTo with pre-connected UDP")

Functions

func JoinHostPort

func JoinHostPort(host, port string) string

JoinHostPort combines host and port into a network address of the form "host:port" or, if host contains a colon, "[host]:port".

func LookupAddr

func LookupAddr(addr string) (name []string, err error)

LookupAddr performs a reverse lookup for the given address, returning a list of names mapping to that address.

func LookupCNAME

func LookupCNAME(name string) (cname string, err error)

LookupCNAME returns the canonical DNS host for the given name. Callers that do not care about the canonical name can call LookupHost or LookupIP directly; both take care of resolving the canonical name as part of the lookup.

func LookupHost

func LookupHost(host string) (addrs []string, err error)

LookupHost looks up the given host using the local resolver. It returns an array of that host's addresses.

func LookupPort

func LookupPort(network, service string) (port int, err error)

LookupPort looks up the port for the given network and service.

func LookupTXT

func LookupTXT(name string) (txt []string, err error)

LookupTXT returns the DNS TXT records for the given domain name.

func ParseCIDR

func ParseCIDR(s string) (IP, *IPNet, error)

ParseCIDR parses s as a CIDR notation IP address and mask, like "192.168.100.1/24" or "2001:DB8::/48", as defined in RFC 4632 and RFC 4291.

It returns the IP address and the network implied by the IP and mask. For example, ParseCIDR("192.168.100.1/16") returns the IP address 192.168.100.1 and the network 192.168.0.0/16.

func Pipe

func Pipe() (Conn, Conn)

Pipe creates a synchronous, in-memory, full duplex network connection; both ends implement the Conn interface. Reads on one end are matched with writes on the other, copying data directly between the two; there is no internal buffering.

func SplitHostPort

func SplitHostPort(hostport string) (host, port string, err error)

SplitHostPort splits a network address of the form "host:port" or "[host]:port" into host and port. The latter form must be used when host contains a colon.

Types

type Addr

type Addr interface {
	Network() string // name of the network
	String() string  // string form of address
}

Addr represents a network end point address.

func InterfaceAddrs

func InterfaceAddrs() ([]Addr, error)

InterfaceAddrs returns a list of the system's network interface addresses.

type AddrError

type AddrError struct {
	Err  string
	Addr string
}

func (*AddrError) Error

func (e *AddrError) Error() string

func (*AddrError) Temporary

func (e *AddrError) Temporary() bool

func (*AddrError) Timeout

func (e *AddrError) Timeout() bool

type Conn

type Conn interface {
	// Read reads data from the connection.
	// Read can be made to time out and return a Error with Timeout() == true
	// after a fixed time limit; see SetDeadline and SetReadDeadline.
	Read(b []byte) (n int, err error)

	// Write writes data to the connection.
	// Write can be made to time out and return a Error with Timeout() == true
	// after a fixed time limit; see SetDeadline and SetWriteDeadline.
	Write(b []byte) (n int, err error)

	// Close closes the connection.
	// Any blocked Read or Write operations will be unblocked and return errors.
	Close() error

	// LocalAddr returns the local network address.
	LocalAddr() Addr

	// RemoteAddr returns the remote network address.
	RemoteAddr() Addr

	// SetDeadline sets the read and write deadlines associated
	// with the connection. It is equivalent to calling both
	// SetReadDeadline and SetWriteDeadline.
	//
	// A deadline is an absolute time after which I/O operations
	// fail with a timeout (see type Error) instead of
	// blocking. The deadline applies to all future I/O, not just
	// the immediately following call to Read or Write.
	//
	// An idle timeout can be implemented by repeatedly extending
	// the deadline after successful Read or Write calls.
	//
	// A zero value for t means I/O operations will not time out.
	SetDeadline(t time.Time) error

	// SetReadDeadline sets the deadline for future Read calls.
	// A zero value for t means Read will not time out.
	SetReadDeadline(t time.Time) error

	// SetWriteDeadline sets the deadline for future Write calls.
	// Even if write times out, it may return n > 0, indicating that
	// some of the data was successfully written.
	// A zero value for t means Write will not time out.
	SetWriteDeadline(t time.Time) error
}

Conn is a generic stream-oriented network connection.

Multiple goroutines may invoke methods on a Conn simultaneously.

func Dial

func Dial(net, addr string) (Conn, error)

Dial connects to the address addr on the network net.

Known networks are "tcp", "tcp4" (IPv4-only), "tcp6" (IPv6-only), "udp", "udp4" (IPv4-only), "udp6" (IPv6-only), "ip", "ip4" (IPv4-only), "ip6" (IPv6-only), "unix" and "unixpacket".

For TCP and UDP networks, addresses have the form host:port. If host is a literal IPv6 address, it must be enclosed in square brackets. The functions JoinHostPort and SplitHostPort manipulate addresses in this form.

Examples:

Dial("tcp", "12.34.56.78:80")
Dial("tcp", "google.com:80")
Dial("tcp", "[de:ad:be:ef::ca:fe]:80")

For IP networks, net must be "ip", "ip4" or "ip6" followed by a colon and a protocol number or name.

Examples:

Dial("ip4:1", "127.0.0.1")
Dial("ip6:ospf", "::1")

func DialTimeout

func DialTimeout(net, addr string, timeout time.Duration) (Conn, error)

DialTimeout acts like Dial but takes a timeout. The timeout includes name resolution, if required.

func FileConn

func FileConn(f *os.File) (c Conn, err error)

FileConn returns a copy of the network connection corresponding to the open file f. It is the caller's responsibility to close f when finished. Closing c does not affect f, and closing f does not affect c.

type DNSConfigError

type DNSConfigError struct {
	Err error
}

DNSConfigError represents an error reading the machine's DNS configuration.

func (*DNSConfigError) Error

func (e *DNSConfigError) Error() string

func (*DNSConfigError) Temporary

func (e *DNSConfigError) Temporary() bool

func (*DNSConfigError) Timeout

func (e *DNSConfigError) Timeout() bool

type DNSError

type DNSError struct {
	Err       string // description of the error
	Name      string // name looked for
	Server    string // server used
	IsTimeout bool
}

DNSError represents a DNS lookup error.

func (*DNSError) Error

func (e *DNSError) Error() string

func (*DNSError) Temporary

func (e *DNSError) Temporary() bool

func (*DNSError) Timeout

func (e *DNSError) Timeout() bool

type Error

type Error interface {
	error
	Timeout() bool   // Is the error a timeout?
	Temporary() bool // Is the error temporary?
}

An Error represents a network error.

type Flags

type Flags uint
const (
	FlagUp           Flags = 1 << iota // interface is up
	FlagBroadcast                      // interface supports broadcast access capability
	FlagLoopback                       // interface is a loopback interface
	FlagPointToPoint                   // interface belongs to a point-to-point link
	FlagMulticast                      // interface supports multicast access capability
)

func (Flags) String

func (f Flags) String() string

type HardwareAddr

type HardwareAddr []byte

A HardwareAddr represents a physical hardware address.

func ParseMAC

func ParseMAC(s string) (hw HardwareAddr, err error)

ParseMAC parses s as an IEEE 802 MAC-48, EUI-48, or EUI-64 using one of the following formats:

01:23:45:67:89:ab
01:23:45:67:89:ab:cd:ef
01-23-45-67-89-ab
01-23-45-67-89-ab-cd-ef
0123.4567.89ab
0123.4567.89ab.cdef

func (HardwareAddr) String

func (a HardwareAddr) String() string

type IP

type IP []byte

An IP is a single IP address, a slice of bytes. Functions in this package accept either 4-byte (IPv4) or 16-byte (IPv6) slices as input.

Note that in this documentation, referring to an IP address as an IPv4 address or an IPv6 address is a semantic property of the address, not just the length of the byte slice: a 16-byte slice can still be an IPv4 address.

func IPv4

func IPv4(a, b, c, d byte) IP

IPv4 returns the IP address (in 16-byte form) of the IPv4 address a.b.c.d.

func LookupIP

func LookupIP(host string) (addrs []IP, err error)

LookupIP looks up host using the local resolver. It returns an array of that host's IPv4 and IPv6 addresses.

func ParseIP

func ParseIP(s string) IP

ParseIP parses s as an IP address, returning the result. The string s can be in dotted decimal ("74.125.19.99") or IPv6 ("2001:4860:0:2001::68") form. If s is not a valid textual representation of an IP address, ParseIP returns nil.

func (IP) DefaultMask

func (ip IP) DefaultMask() IPMask

DefaultMask returns the default IP mask for the IP address ip. Only IPv4 addresses have default masks; DefaultMask returns nil if ip is not a valid IPv4 address.

func (IP) Equal

func (ip IP) Equal(x IP) bool

Equal returns true if ip and x are the same IP address. An IPv4 address and that same address in IPv6 form are considered to be equal.

func (IP) IsGlobalUnicast

func (ip IP) IsGlobalUnicast() bool

IsGlobalUnicast returns true if ip is a global unicast address.

func (IP) IsInterfaceLocalMulticast

func (ip IP) IsInterfaceLocalMulticast() bool

IsInterfaceLinkLocalMulticast returns true if ip is an interface-local multicast address.

func (IP) IsLinkLocalMulticast

func (ip IP) IsLinkLocalMulticast() bool

IsLinkLocalMulticast returns true if ip is a link-local multicast address.

func (IP) IsLinkLocalUnicast

func (ip IP) IsLinkLocalUnicast() bool

IsLinkLocalUnicast returns true if ip is a link-local unicast address.

func (IP) IsLoopback

func (ip IP) IsLoopback() bool

IsLoopback returns true if ip is a loopback address.

func (IP) IsMulticast

func (ip IP) IsMulticast() bool

IsMulticast returns true if ip is a multicast address.

func (IP) IsUnspecified

func (ip IP) IsUnspecified() bool

IsUnspecified returns true if ip is an unspecified address.

func (IP) Mask

func (ip IP) Mask(mask IPMask) IP

Mask returns the result of masking the IP address ip with mask.

func (IP) String

func (ip IP) String() string

String returns the string form of the IP address ip. If the address is an IPv4 address, the string representation is dotted decimal ("74.125.19.99"). Otherwise the representation is IPv6 ("2001:4860:0:2001::68").

func (IP) To16

func (ip IP) To16() IP

To16 converts the IP address ip to a 16-byte representation. If ip is not an IP address (it is the wrong length), To16 returns nil.

func (IP) To4

func (ip IP) To4() IP

To4 converts the IPv4 address ip to a 4-byte representation. If ip is not an IPv4 address, To4 returns nil.

type IPAddr

type IPAddr struct {
	IP   IP
	Zone string // IPv6 scoped addressing zone
}

IPAddr represents the address of an IP end point.

func ResolveIPAddr

func ResolveIPAddr(net, addr string) (*IPAddr, error)

ResolveIPAddr parses addr as an IP address and resolves domain names to numeric addresses on the network net, which must be "ip", "ip4" or "ip6".

func (*IPAddr) Network

func (a *IPAddr) Network() string

Network returns the address's network name, "ip".

func (*IPAddr) String

func (a *IPAddr) String() string

type IPConn

type IPConn struct {
	// contains filtered or unexported fields
}

IPConn is the implementation of the Conn and PacketConn interfaces for IP network connections.

func DialIP

func DialIP(netProto string, laddr, raddr *IPAddr) (*IPConn, error)

DialIP connects to the remote address raddr on the network protocol netProto, which must be "ip", "ip4", or "ip6" followed by a colon and a protocol number or name.

func ListenIP

func ListenIP(netProto string, laddr *IPAddr) (*IPConn, error)

ListenIP listens for incoming IP packets addressed to the local address laddr. The returned connection c's ReadFrom and WriteTo methods can be used to receive and send IP packets with per-packet addressing.

func (*IPConn) Close

func (c *IPConn) Close() error

Close closes the connection.

func (*IPConn) File

func (c *IPConn) File() (f *os.File, err error)

File sets the underlying os.File to blocking mode and returns a copy. It is the caller's responsibility to close f when finished. Closing c does not affect f, and closing f does not affect c.

The returned os.File's file descriptor is different from the connection's. Attempting to change properties of the original using this duplicate may or may not have the desired effect.

func (*IPConn) LocalAddr

func (c *IPConn) LocalAddr() Addr

LocalAddr returns the local network address.

func (*IPConn) Read

func (c *IPConn) Read(b []byte) (int, error)

Read implements the Conn Read method.

func (*IPConn) ReadFrom

func (c *IPConn) ReadFrom(b []byte) (int, Addr, error)

ReadFrom implements the PacketConn ReadFrom method.

func (*IPConn) ReadFromIP

func (c *IPConn) ReadFromIP(b []byte) (int, *IPAddr, error)

ReadFromIP reads an IP packet from c, copying the payload into b. It returns the number of bytes copied into b and the return address that was on the packet.

ReadFromIP can be made to time out and return an error with Timeout() == true after a fixed time limit; see SetDeadline and SetReadDeadline.

func (*IPConn) ReadMsgIP

func (c *IPConn) ReadMsgIP(b, oob []byte) (n, oobn, flags int, addr *IPAddr, err error)

ReadMsgIP reads a packet from c, copying the payload into b and the associdated 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 packet and the source address of the packet.

func (*IPConn) RemoteAddr

func (c *IPConn) RemoteAddr() Addr

RemoteAddr returns the remote network address.

func (*IPConn) SetDeadline

func (c *IPConn) SetDeadline(t time.Time) error

SetDeadline implements the Conn SetDeadline method.

func (*IPConn) SetReadBuffer

func (c *IPConn) SetReadBuffer(bytes int) error

SetReadBuffer sets the size of the operating system's receive buffer associated with the connection.

func (*IPConn) SetReadDeadline

func (c *IPConn) SetReadDeadline(t time.Time) error

SetReadDeadline implements the Conn SetReadDeadline method.

func (*IPConn) SetWriteBuffer

func (c *IPConn) SetWriteBuffer(bytes int) error

SetWriteBuffer sets the size of the operating system's transmit buffer associated with the connection.

func (*IPConn) SetWriteDeadline

func (c *IPConn) SetWriteDeadline(t time.Time) error

SetWriteDeadline implements the Conn SetWriteDeadline method.

func (*IPConn) Write

func (c *IPConn) Write(b []byte) (int, error)

Write implements the Conn Write method.

func (*IPConn) WriteMsgIP

func (c *IPConn) WriteMsgIP(b, oob []byte, addr *IPAddr) (n, oobn int, err error)

WriteMsgIP writes a packet 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.

func (*IPConn) WriteTo

func (c *IPConn) WriteTo(b []byte, addr Addr) (int, error)

WriteTo implements the PacketConn WriteTo method.

func (*IPConn) WriteToIP

func (c *IPConn) WriteToIP(b []byte, addr *IPAddr) (int, error)

WriteToIP writes an IP packet to addr via c, copying the payload from b.

WriteToIP can be made to time out and return an error with Timeout() == true after a fixed time limit; see SetDeadline and SetWriteDeadline. On packet-oriented connections, write timeouts are rare.

type IPMask

type IPMask []byte

An IP mask is an IP address.

func CIDRMask

func CIDRMask(ones, bits int) IPMask

CIDRMask returns an IPMask consisting of `ones' 1 bits followed by 0s up to a total length of `bits' bits. For a mask of this form, CIDRMask is the inverse of IPMask.Size.

func IPv4Mask

func IPv4Mask(a, b, c, d byte) IPMask

IPv4Mask returns the IP mask (in 4-byte form) of the IPv4 mask a.b.c.d.

func (IPMask) Size

func (m IPMask) Size() (ones, bits int)

Size returns the number of leading ones and total bits in the mask. If the mask is not in the canonical form--ones followed by zeros--then Size returns 0, 0.

func (IPMask) String

func (m IPMask) String() string

String returns the hexadecimal form of m, with no punctuation.

type IPNet

type IPNet struct {
	IP   IP     // network number
	Mask IPMask // network mask
	Zone string // IPv6 scoped addressing zone
}

An IPNet represents an IP network.

func (*IPNet) Contains

func (n *IPNet) Contains(ip IP) bool

Contains reports whether the network includes ip.

func (*IPNet) Network

func (n *IPNet) Network() string

Network returns the address's network name, "ip+net".

func (*IPNet) String

func (n *IPNet) String() string

String returns the CIDR notation of n like "192.168.100.1/24" or "2001:DB8::/48" as defined in RFC 4632 and RFC 4291. If the mask is not in the canonical form, it returns the string which consists of an IP address, followed by a slash character and a mask expressed as hexadecimal form with no punctuation like "192.168.100.1/c000ff00".

type Interface

type Interface struct {
	Index        int          // positive integer that starts at one, zero is never used
	MTU          int          // maximum transmission unit
	Name         string       // e.g., "en0", "lo0", "eth0.100"
	HardwareAddr HardwareAddr // IEEE MAC-48, EUI-48 and EUI-64 form
	Flags        Flags        // e.g., FlagUp, FlagLoopback, FlagMulticast
}

Interface represents a mapping between network interface name and index. It also represents network interface facility information.

func InterfaceByIndex

func InterfaceByIndex(index int) (*Interface, error)

InterfaceByIndex returns the interface specified by index.

func InterfaceByName

func InterfaceByName(name string) (*Interface, error)

InterfaceByName returns the interface specified by name.

func Interfaces

func Interfaces() ([]Interface, error)

Interfaces returns a list of the system's network interfaces.

func (*Interface) Addrs

func (ifi *Interface) Addrs() ([]Addr, error)

Addrs returns interface addresses for a specific interface.

func (*Interface) MulticastAddrs

func (ifi *Interface) MulticastAddrs() ([]Addr, error)

MulticastAddrs returns multicast, joined group addresses for a specific interface.

type InvalidAddrError

type InvalidAddrError string

func (InvalidAddrError) Error

func (e InvalidAddrError) Error() string

func (InvalidAddrError) Temporary

func (e InvalidAddrError) Temporary() bool

func (InvalidAddrError) Timeout

func (e InvalidAddrError) Timeout() bool

type Listener

type Listener interface {
	// Accept waits for and returns the next connection to the listener.
	Accept() (c Conn, err error)

	// Close closes the listener.
	// Any blocked Accept operations will be unblocked and return errors.
	Close() error

	// Addr returns the listener's network address.
	Addr() Addr
}

A Listener is a generic network listener for stream-oriented protocols.

Multiple goroutines may invoke methods on a Listener simultaneously.

Example
package main

import (
	"io"
	"log"
	"net"
)

func main() {
	// Listen on TCP port 2000 on all interfaces.
	l, err := net.Listen("tcp", ":2000")
	if err != nil {
		log.Fatal(err)
	}
	for {
		// Wait for a connection.
		conn, err := l.Accept()
		if err != nil {
			log.Fatal(err)
		}
		// Handle the connection in a new goroutine.
		// The loop then returns to accepting, so that
		// multiple connections may be served concurrently.
		go func(c net.Conn) {
			// Echo all incoming data.
			io.Copy(c, c)
			// Shut down the connection.
			c.Close()
		}(conn)
	}
}
Output:

func FileListener

func FileListener(f *os.File) (l Listener, err error)

FileListener returns a copy of the network listener corresponding to the open file f. It is the caller's responsibility to close l when finished. Closing l does not affect f, and closing f does not affect l.

func Listen

func Listen(net, laddr string) (Listener, error)

Listen announces on the local network address laddr. The network string net must be a stream-oriented network: "tcp", "tcp4", "tcp6", "unix" or "unixpacket".

type MX

type MX struct {
	Host string
	Pref uint16
}

An MX represents a single DNS MX record.

func LookupMX

func LookupMX(name string) (mx []*MX, err error)

LookupMX returns the DNS MX records for the given domain name sorted by preference.

type NS

type NS struct {
	Host string
}

An NS represents a single DNS NS record.

func LookupNS

func LookupNS(name string) (ns []*NS, err error)

LookupNS returns the DNS NS records for the given domain name.

type OpError

type OpError struct {
	Op   string
	Net  string
	Addr Addr
	Err  error
}

func (*OpError) Error

func (e *OpError) Error() string

func (*OpError) Temporary

func (e *OpError) Temporary() bool

func (*OpError) Timeout

func (e *OpError) Timeout() bool

type PacketConn

type PacketConn interface {
	// ReadFrom reads a packet from the connection,
	// copying the payload into b.  It returns the number of
	// bytes copied into b and the return address that
	// was on the packet.
	// ReadFrom can be made to time out and return
	// an error with Timeout() == true after a fixed time limit;
	// see SetDeadline and SetReadDeadline.
	ReadFrom(b []byte) (n int, addr Addr, err error)

	// WriteTo writes a packet with payload b to addr.
	// WriteTo can be made to time out and return
	// an error with Timeout() == true after a fixed time limit;
	// see SetDeadline and SetWriteDeadline.
	// On packet-oriented connections, write timeouts are rare.
	WriteTo(b []byte, addr Addr) (n int, err error)

	// Close closes the connection.
	// Any blocked ReadFrom or WriteTo operations will be unblocked and return errors.
	Close() error

	// LocalAddr returns the local network address.
	LocalAddr() Addr

	// SetDeadline sets the read and write deadlines associated
	// with the connection.
	SetDeadline(t time.Time) error

	// SetReadDeadline sets the deadline for future Read calls.
	// If the deadline is reached, Read will fail with a timeout
	// (see type Error) instead of blocking.
	// A zero value for t means Read will not time out.
	SetReadDeadline(t time.Time) error

	// SetWriteDeadline sets the deadline for future Write calls.
	// If the deadline is reached, Write will fail with a timeout
	// (see type Error) instead of blocking.
	// A zero value for t means Write will not time out.
	// Even if write times out, it may return n > 0, indicating that
	// some of the data was successfully written.
	SetWriteDeadline(t time.Time) error
}

PacketConn is a generic packet-oriented network connection.

Multiple goroutines may invoke methods on a PacketConn simultaneously.

func FilePacketConn

func FilePacketConn(f *os.File) (c PacketConn, err error)

FilePacketConn returns a copy of the packet network connection corresponding to the open file f. It is the caller's responsibility to close f when finished. Closing c does not affect f, and closing f does not affect c.

func ListenPacket

func ListenPacket(net, laddr string) (PacketConn, error)

ListenPacket announces on the local network address laddr. The network string net must be a packet-oriented network: "udp", "udp4", "udp6", "ip", "ip4", "ip6" or "unixgram".

type ParseError

type ParseError struct {
	Type string
	Text string
}

A ParseError represents a malformed text string and the type of string that was expected.

func (*ParseError) Error

func (e *ParseError) Error() string

type SRV

type SRV struct {
	Target   string
	Port     uint16
	Priority uint16
	Weight   uint16
}

An SRV represents a single DNS SRV record.

func LookupSRV

func LookupSRV(service, proto, name string) (cname string, addrs []*SRV, err error)

LookupSRV tries to resolve an SRV query of the given service, protocol, and domain name. The proto is "tcp" or "udp". The returned records are sorted by priority and randomized by weight within a priority.

LookupSRV constructs the DNS name to look up following RFC 2782. That is, it looks up _service._proto.name. To accommodate services publishing SRV records under non-standard names, if both service and proto are empty strings, LookupSRV looks up name directly.

type TCPAddr

type TCPAddr struct {
	IP   IP
	Port int
	Zone string // IPv6 scoped addressing zone
}

TCPAddr represents the address of a TCP end point.

func ResolveTCPAddr

func ResolveTCPAddr(net, addr string) (*TCPAddr, error)

ResolveTCPAddr parses addr as a TCP address of the form host:port and resolves domain names or port names to numeric addresses on the network net, which must be "tcp", "tcp4" or "tcp6". A literal IPv6 host address must be enclosed in square brackets, as in "[::]:80".

func (*TCPAddr) Network

func (a *TCPAddr) Network() string

Network returns the address's network name, "tcp".

func (*TCPAddr) String

func (a *TCPAddr) String() string

type TCPConn

type TCPConn struct {
	// contains filtered or unexported fields
}

TCPConn is an implementation of the Conn interface for TCP network connections.

func DialTCP

func DialTCP(net string, laddr, raddr *TCPAddr) (*TCPConn, error)

DialTCP connects to the remote address raddr on the network net, which must be "tcp", "tcp4", or "tcp6". If laddr is not nil, it is used as the local address for the connection.

func (*TCPConn) Close

func (c *TCPConn) Close() error

Close closes the connection.

func (*TCPConn) CloseRead

func (c *TCPConn) CloseRead() error

CloseRead shuts down the reading side of the TCP connection. Most callers should just use Close.

func (*TCPConn) CloseWrite

func (c *TCPConn) CloseWrite() error

CloseWrite shuts down the writing side of the TCP connection. Most callers should just use Close.

func (*TCPConn) File

func (c *TCPConn) File() (f *os.File, err error)

File sets the underlying os.File to blocking mode and returns a copy. It is the caller's responsibility to close f when finished. Closing c does not affect f, and closing f does not affect c.

The returned os.File's file descriptor is different from the connection's. Attempting to change properties of the original using this duplicate may or may not have the desired effect.

func (*TCPConn) LocalAddr

func (c *TCPConn) LocalAddr() Addr

LocalAddr returns the local network address.

func (*TCPConn) Read

func (c *TCPConn) Read(b []byte) (int, error)

Read implements the Conn Read method.

func (*TCPConn) ReadFrom

func (c *TCPConn) ReadFrom(r io.Reader) (int64, error)

ReadFrom implements the io.ReaderFrom ReadFrom method.

func (*TCPConn) RemoteAddr

func (c *TCPConn) RemoteAddr() Addr

RemoteAddr returns the remote network address.

func (*TCPConn) SetDeadline

func (c *TCPConn) SetDeadline(t time.Time) error

SetDeadline implements the Conn SetDeadline method.

func (*TCPConn) SetKeepAlive

func (c *TCPConn) SetKeepAlive(keepalive bool) error

SetKeepAlive sets whether the operating system should send keepalive messages on the connection.

func (*TCPConn) SetLinger

func (c *TCPConn) SetLinger(sec int) error

SetLinger sets the behavior of Close() on a connection which still has data waiting to be sent or to be acknowledged.

If sec < 0 (the default), Close returns immediately and the operating system finishes sending the data in the background.

If sec == 0, Close returns immediately and the operating system discards any unsent or unacknowledged data.

If sec > 0, Close blocks for at most sec seconds waiting for data to be sent and acknowledged.

func (*TCPConn) SetNoDelay

func (c *TCPConn) SetNoDelay(noDelay bool) error

SetNoDelay controls whether the operating system should delay packet transmission in hopes of sending fewer packets (Nagle's algorithm). The default is true (no delay), meaning that data is sent as soon as possible after a Write.

func (*TCPConn) SetReadBuffer

func (c *TCPConn) SetReadBuffer(bytes int) error

SetReadBuffer sets the size of the operating system's receive buffer associated with the connection.

func (*TCPConn) SetReadDeadline

func (c *TCPConn) SetReadDeadline(t time.Time) error

SetReadDeadline implements the Conn SetReadDeadline method.

func (*TCPConn) SetWriteBuffer

func (c *TCPConn) SetWriteBuffer(bytes int) error

SetWriteBuffer sets the size of the operating system's transmit buffer associated with the connection.

func (*TCPConn) SetWriteDeadline

func (c *TCPConn) SetWriteDeadline(t time.Time) error

SetWriteDeadline implements the Conn SetWriteDeadline method.

func (*TCPConn) Write

func (c *TCPConn) Write(b []byte) (int, error)

Write implements the Conn Write method.

type TCPListener

type TCPListener struct {
	// contains filtered or unexported fields
}

TCPListener is a TCP network listener. Clients should typically use variables of type Listener instead of assuming TCP.

func ListenTCP

func ListenTCP(net string, laddr *TCPAddr) (*TCPListener, error)

ListenTCP announces on the TCP address laddr and returns a TCP listener. Net must be "tcp", "tcp4", or "tcp6". If laddr has a port of 0, it means to listen on some available port. The caller can use l.Addr() to retrieve the chosen address.

func (*TCPListener) Accept

func (l *TCPListener) Accept() (c Conn, err error)

Accept implements the Accept method in the Listener interface; it waits for the next call and returns a generic Conn.

func (*TCPListener) AcceptTCP

func (l *TCPListener) AcceptTCP() (c *TCPConn, err error)

AcceptTCP accepts the next incoming call and returns the new connection and the remote address.

func (*TCPListener) Addr

func (l *TCPListener) Addr() Addr

Addr returns the listener's network address, a *TCPAddr.

func (*TCPListener) Close

func (l *TCPListener) Close() error

Close stops listening on the TCP address. Already Accepted connections are not closed.

func (*TCPListener) File

func (l *TCPListener) File() (f *os.File, err error)

File returns a copy of the underlying os.File, set to blocking mode. It is the caller's responsibility to close f when finished. Closing l does not affect f, and closing f does not affect l.

func (*TCPListener) SetDeadline

func (l *TCPListener) SetDeadline(t time.Time) error

SetDeadline sets the deadline associated with the listener. A zero time value disables the deadline.

type UDPAddr

type UDPAddr struct {
	IP   IP
	Port int
	Zone string // IPv6 scoped addressing zone
}

UDPAddr represents the address of a UDP end point.

func ResolveUDPAddr

func ResolveUDPAddr(net, addr string) (*UDPAddr, error)

ResolveUDPAddr parses addr as a UDP address of the form host:port and resolves domain names or port names to numeric addresses on the network net, which must be "udp", "udp4" or "udp6". A literal IPv6 host address must be enclosed in square brackets, as in "[::]:80".

func (*UDPAddr) Network

func (a *UDPAddr) Network() string

Network returns the address's network name, "udp".

func (*UDPAddr) String

func (a *UDPAddr) String() string

type UDPConn

type UDPConn struct {
	// contains filtered or unexported fields
}

UDPConn is the implementation of the Conn and PacketConn interfaces for UDP network connections.

func DialUDP

func DialUDP(net string, laddr, raddr *UDPAddr) (*UDPConn, error)

DialUDP connects to the remote address raddr on the network net, which must be "udp", "udp4", or "udp6". If laddr is not nil, it is used as the local address for the connection.

func ListenMulticastUDP

func ListenMulticastUDP(net string, ifi *Interface, gaddr *UDPAddr) (*UDPConn, error)

ListenMulticastUDP listens for incoming multicast UDP packets addressed to the group address gaddr on ifi, which specifies the interface to join. ListenMulticastUDP uses default multicast interface if ifi is nil.

func ListenUDP

func ListenUDP(net string, laddr *UDPAddr) (*UDPConn, error)

ListenUDP listens for incoming UDP packets addressed to the local address laddr. The returned connection c's ReadFrom and WriteTo methods can be used to receive and send UDP packets with per-packet addressing.

func (*UDPConn) Close

func (c *UDPConn) Close() error

Close closes the connection.

func (*UDPConn) File

func (c *UDPConn) File() (f *os.File, err error)

File sets the underlying os.File to blocking mode and returns a copy. It is the caller's responsibility to close f when finished. Closing c does not affect f, and closing f does not affect c.

The returned os.File's file descriptor is different from the connection's. Attempting to change properties of the original using this duplicate may or may not have the desired effect.

func (*UDPConn) LocalAddr

func (c *UDPConn) LocalAddr() Addr

LocalAddr returns the local network address.

func (*UDPConn) Read

func (c *UDPConn) Read(b []byte) (int, error)

Read implements the Conn Read method.

func (*UDPConn) ReadFrom

func (c *UDPConn) ReadFrom(b []byte) (int, Addr, error)

ReadFrom implements the PacketConn ReadFrom method.

func (*UDPConn) ReadFromUDP

func (c *UDPConn) ReadFromUDP(b []byte) (n int, addr *UDPAddr, err error)

ReadFromUDP reads a UDP packet from c, copying the payload into b. It returns the number of bytes copied into b and the return address that was on the packet.

ReadFromUDP can be made to time out and return an error with Timeout() == true after a fixed time limit; see SetDeadline and SetReadDeadline.

func (*UDPConn) ReadMsgUDP

func (c *UDPConn) ReadMsgUDP(b, oob []byte) (n, oobn, flags int, addr *UDPAddr, err error)

ReadMsgUDP reads a packet from c, copying the payload into b and the associdated 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 packet and the source address of the packet.

func (*UDPConn) RemoteAddr

func (c *UDPConn) RemoteAddr() Addr

RemoteAddr returns the remote network address.

func (*UDPConn) SetDeadline

func (c *UDPConn) SetDeadline(t time.Time) error

SetDeadline implements the Conn SetDeadline method.

func (*UDPConn) SetReadBuffer

func (c *UDPConn) SetReadBuffer(bytes int) error

SetReadBuffer sets the size of the operating system's receive buffer associated with the connection.

func (*UDPConn) SetReadDeadline

func (c *UDPConn) SetReadDeadline(t time.Time) error

SetReadDeadline implements the Conn SetReadDeadline method.

func (*UDPConn) SetWriteBuffer

func (c *UDPConn) SetWriteBuffer(bytes int) error

SetWriteBuffer sets the size of the operating system's transmit buffer associated with the connection.

func (*UDPConn) SetWriteDeadline

func (c *UDPConn) SetWriteDeadline(t time.Time) error

SetWriteDeadline implements the Conn SetWriteDeadline method.

func (*UDPConn) Write

func (c *UDPConn) Write(b []byte) (int, error)

Write implements the Conn Write method.

func (*UDPConn) WriteMsgUDP

func (c *UDPConn) WriteMsgUDP(b, oob []byte, addr *UDPAddr) (n, oobn int, err error)

WriteMsgUDP writes a packet 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.

func (*UDPConn) WriteTo

func (c *UDPConn) WriteTo(b []byte, addr Addr) (int, error)

WriteTo implements the PacketConn WriteTo method.

func (*UDPConn) WriteToUDP

func (c *UDPConn) WriteToUDP(b []byte, addr *UDPAddr) (int, error)

WriteToUDP writes a UDP packet to addr via c, copying the payload from b.

WriteToUDP can be made to time out and return an error with Timeout() == true after a fixed time limit; see SetDeadline and SetWriteDeadline. On packet-oriented connections, write timeouts are rare.

type UnixAddr

type UnixAddr struct {
	Name string
	Net  string
}

UnixAddr represents the address of a Unix domain socket end point.

func ResolveUnixAddr

func ResolveUnixAddr(net, addr string) (*UnixAddr, error)

ResolveUnixAddr parses addr as a Unix domain socket address. The string net gives the network name, "unix", "unixgram" or "unixpacket".

func (*UnixAddr) Network

func (a *UnixAddr) Network() string

Network returns the address's network name, "unix" or "unixgram".

func (*UnixAddr) String

func (a *UnixAddr) String() string

type UnixConn

type UnixConn struct {
	// contains filtered or unexported fields
}

UnixConn is an implementation of the Conn interface for connections to Unix domain sockets.

func DialUnix

func DialUnix(net string, laddr, raddr *UnixAddr) (*UnixConn, error)

DialUnix connects to the remote address raddr on the network net, which must be "unix", "unixgram" or "unixpacket". If laddr is not nil, it is used as the local address for the connection.

func ListenUnixgram

func ListenUnixgram(net string, laddr *UnixAddr) (*UnixConn, error)

ListenUnixgram listens for incoming Unix datagram packets addressed to the local address laddr. The returned connection c's ReadFrom and WriteTo methods can be used to receive and send packets with per-packet addressing. The network net must be "unixgram".

func (*UnixConn) Close

func (c *UnixConn) Close() error

Close closes the connection.

func (*UnixConn) CloseRead

func (c *UnixConn) CloseRead() error

CloseRead shuts down the reading side of the Unix domain connection. Most callers should just use Close.

func (*UnixConn) CloseWrite

func (c *UnixConn) CloseWrite() error

CloseWrite shuts down the writing side of the Unix domain connection. Most callers should just use Close.

func (*UnixConn) File

func (c *UnixConn) File() (f *os.File, err error)

File sets the underlying os.File to blocking mode and returns a copy. It is the caller's responsibility to close f when finished. Closing c does not affect f, and closing f does not affect c.

The returned os.File's file descriptor is different from the connection's. Attempting to change properties of the original using this duplicate may or may not have the desired effect.

func (*UnixConn) LocalAddr

func (c *UnixConn) LocalAddr() Addr

LocalAddr returns the local network address.

func (*UnixConn) Read

func (c *UnixConn) Read(b []byte) (int, error)

Read implements the Conn Read method.

func (*UnixConn) ReadFrom

func (c *UnixConn) ReadFrom(b []byte) (n int, addr Addr, err error)

ReadFrom implements the PacketConn ReadFrom method.

func (*UnixConn) ReadFromUnix

func (c *UnixConn) ReadFromUnix(b []byte) (n int, addr *UnixAddr, err error)

ReadFromUnix reads a packet from c, copying the payload into b. It returns the number of bytes copied into b and the source address of the packet.

ReadFromUnix can be made to time out and return an error with Timeout() == true after a fixed time limit; see SetDeadline and SetReadDeadline.

func (*UnixConn) ReadMsgUnix

func (c *UnixConn) ReadMsgUnix(b, oob []byte) (n, oobn, flags int, addr *UnixAddr, err error)

ReadMsgUnix reads a packet 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 packet, and the source address of the packet.

func (*UnixConn) RemoteAddr

func (c *UnixConn) RemoteAddr() Addr

RemoteAddr returns the remote network address.

func (*UnixConn) SetDeadline

func (c *UnixConn) SetDeadline(t time.Time) error

SetDeadline implements the Conn SetDeadline method.

func (*UnixConn) SetReadBuffer

func (c *UnixConn) SetReadBuffer(bytes int) error

SetReadBuffer sets the size of the operating system's receive buffer associated with the connection.

func (*UnixConn) SetReadDeadline

func (c *UnixConn) SetReadDeadline(t time.Time) error

SetReadDeadline implements the Conn SetReadDeadline method.

func (*UnixConn) SetWriteBuffer

func (c *UnixConn) SetWriteBuffer(bytes int) error

SetWriteBuffer sets the size of the operating system's transmit buffer associated with the connection.

func (*UnixConn) SetWriteDeadline

func (c *UnixConn) SetWriteDeadline(t time.Time) error

SetWriteDeadline implements the Conn SetWriteDeadline method.

func (*UnixConn) Write

func (c *UnixConn) Write(b []byte) (int, error)

Write implements the Conn Write method.

func (*UnixConn) WriteMsgUnix

func (c *UnixConn) WriteMsgUnix(b, oob []byte, addr *UnixAddr) (n, oobn int, err error)

WriteMsgUnix writes a packet 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.

func (*UnixConn) WriteTo

func (c *UnixConn) WriteTo(b []byte, addr Addr) (n int, err error)

WriteTo implements the PacketConn WriteTo method.

func (*UnixConn) WriteToUnix

func (c *UnixConn) WriteToUnix(b []byte, addr *UnixAddr) (n int, err error)

WriteToUnix writes a packet to addr via c, copying the payload from b.

WriteToUnix can be made to time out and return an error with Timeout() == true after a fixed time limit; see SetDeadline and SetWriteDeadline. On packet-oriented connections, write timeouts are rare.

type UnixListener

type UnixListener struct {
	// contains filtered or unexported fields
}

UnixListener is a Unix domain socket listener. Clients should typically use variables of type Listener instead of assuming Unix domain sockets.

func ListenUnix

func ListenUnix(net string, laddr *UnixAddr) (*UnixListener, error)

ListenUnix announces on the Unix domain socket laddr and returns a Unix listener. The network net must be "unix", "unixgram" or "unixpacket".

func (*UnixListener) Accept

func (l *UnixListener) Accept() (c Conn, err error)

Accept implements the Accept method in the Listener interface; it waits for the next call and returns a generic Conn.

func (*UnixListener) AcceptUnix

func (l *UnixListener) AcceptUnix() (*UnixConn, error)

AcceptUnix accepts the next incoming call and returns the new connection and the remote address.

func (*UnixListener) Addr

func (l *UnixListener) Addr() Addr

Addr returns the listener's network address.

func (*UnixListener) Close

func (l *UnixListener) Close() error

Close stops listening on the Unix address. Already accepted connections are not closed.

func (*UnixListener) File

func (l *UnixListener) File() (f *os.File, err error)

File returns a copy of the underlying os.File, set to blocking mode. It is the caller's responsibility to close f when finished. Closing l does not affect f, and closing f does not affect l.

func (*UnixListener) SetDeadline

func (l *UnixListener) SetDeadline(t time.Time) (err error)

SetDeadline sets the deadline associated with the listener. A zero time value disables the deadline.

type UnknownNetworkError

type UnknownNetworkError string

func (UnknownNetworkError) Error

func (e UnknownNetworkError) Error() string

func (UnknownNetworkError) Temporary

func (e UnknownNetworkError) Temporary() bool

func (UnknownNetworkError) Timeout

func (e UnknownNetworkError) Timeout() bool

Notes

Bugs

  • On OpenBSD, listening on the "tcp" network does not listen for both IPv4 and IPv6 connections. This is due to the fact that IPv4 traffic will not be routed to an IPv6 socket - two separate sockets are required if both AFs are to be supported. See inet6(4) on OpenBSD for details.

Directories

Path Synopsis
Package http provides HTTP client and server implementations.
Package http provides HTTP client and server implementations.
cgi
Package cgi implements CGI (Common Gateway Interface) as specified in RFC 3875.
Package cgi implements CGI (Common Gateway Interface) as specified in RFC 3875.
fcgi
Package fcgi implements the FastCGI protocol.
Package fcgi implements the FastCGI protocol.
httptest
Package httptest provides utilities for HTTP testing.
Package httptest provides utilities for HTTP testing.
httputil
Package httputil provides HTTP utility functions, complementing the more common ones in the net/http package.
Package httputil provides HTTP utility functions, complementing the more common ones in the net/http package.
pprof
Package pprof serves via its HTTP server runtime profiling data in the format expected by the pprof visualization tool.
Package pprof serves via its HTTP server runtime profiling data in the format expected by the pprof visualization tool.
Package mail implements parsing of mail messages.
Package mail implements parsing of mail messages.
rpc
Package rpc provides access to the exported methods of an object across a network or other I/O connection.
Package rpc provides access to the exported methods of an object across a network or other I/O connection.
jsonrpc
Package jsonrpc implements a JSON-RPC ClientCodec and ServerCodec for the rpc package.
Package jsonrpc implements a JSON-RPC ClientCodec and ServerCodec for the rpc package.
Package smtp implements the Simple Mail Transfer Protocol as defined in RFC 5321.
Package smtp implements the Simple Mail Transfer Protocol as defined in RFC 5321.
Package textproto implements generic support for text-based request/response protocols in the style of HTTP, NNTP, and SMTP.
Package textproto implements generic support for text-based request/response protocols in the style of HTTP, NNTP, and SMTP.
Package url parses URLs and implements query escaping.
Package url parses URLs and implements query escaping.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL