2008-09-16 14:42:47 -06:00
|
|
|
// Copyright 2009 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.
|
|
|
|
|
2011-04-19 17:57:05 -06:00
|
|
|
// Package net provides a portable interface to Unix networks sockets,
|
|
|
|
// including TCP/IP, UDP, domain name resolution, and Unix domain sockets.
|
2008-09-16 14:42:47 -06:00
|
|
|
package net
|
|
|
|
|
2009-11-01 12:15:34 -07:00
|
|
|
// TODO(rsc):
|
|
|
|
// support for raw ethernet sockets
|
2008-09-16 14:42:47 -06:00
|
|
|
|
2011-11-01 20:05:34 -06:00
|
|
|
import "errors"
|
2009-05-07 18:36:29 -06:00
|
|
|
|
2009-11-02 19:37:30 -07:00
|
|
|
// Addr represents a network end point address.
|
|
|
|
type Addr interface {
|
2009-12-15 16:35:38 -07:00
|
|
|
Network() string // name of the network
|
|
|
|
String() string // string form of address
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Conn is a generic stream-oriented network connection.
|
2009-05-13 19:03:41 -06:00
|
|
|
type Conn interface {
|
2009-11-02 19:37:30 -07:00
|
|
|
// Read reads data from the connection.
|
2010-04-26 23:15:25 -06:00
|
|
|
// Read can be made to time out and return a net.Error with Timeout() == true
|
2009-11-02 19:37:30 -07:00
|
|
|
// after a fixed time limit; see SetTimeout and SetReadTimeout.
|
2011-11-01 20:05:34 -06:00
|
|
|
Read(b []byte) (n int, err error)
|
2009-05-13 19:03:41 -06:00
|
|
|
|
2009-11-02 19:37:30 -07:00
|
|
|
// Write writes data to the connection.
|
2010-04-26 23:15:25 -06:00
|
|
|
// Write can be made to time out and return a net.Error with Timeout() == true
|
|
|
|
// after a fixed time limit; see SetTimeout and SetWriteTimeout.
|
2011-11-01 20:05:34 -06:00
|
|
|
Write(b []byte) (n int, err error)
|
2009-05-13 19:03:41 -06:00
|
|
|
|
|
|
|
// Close closes the connection.
|
2011-11-01 20:05:34 -06:00
|
|
|
Close() error
|
2009-05-13 19:03:41 -06:00
|
|
|
|
2009-06-19 17:03:59 -06:00
|
|
|
// LocalAddr returns the local network address.
|
2009-12-15 16:35:38 -07:00
|
|
|
LocalAddr() Addr
|
2009-06-19 17:03:59 -06:00
|
|
|
|
|
|
|
// RemoteAddr returns the remote network address.
|
2009-12-15 16:35:38 -07:00
|
|
|
RemoteAddr() Addr
|
2009-11-02 19:37:30 -07:00
|
|
|
|
|
|
|
// SetTimeout sets the read and write deadlines associated
|
|
|
|
// with the connection.
|
2011-11-01 20:05:34 -06:00
|
|
|
SetTimeout(nsec int64) error
|
2009-11-02 19:37:30 -07:00
|
|
|
|
|
|
|
// SetReadTimeout sets the time (in nanoseconds) that
|
2010-04-26 23:15:25 -06:00
|
|
|
// Read will wait for data before returning an error with Timeout() == true.
|
2009-11-02 19:37:30 -07:00
|
|
|
// Setting nsec == 0 (the default) disables the deadline.
|
2011-11-01 20:05:34 -06:00
|
|
|
SetReadTimeout(nsec int64) error
|
2009-11-02 19:37:30 -07:00
|
|
|
|
|
|
|
// SetWriteTimeout sets the time (in nanoseconds) that
|
2010-04-26 23:15:25 -06:00
|
|
|
// Write will wait to send its data before returning an error with Timeout() == true.
|
2009-11-02 19:37:30 -07:00
|
|
|
// Setting nsec == 0 (the default) disables the deadline.
|
|
|
|
// Even if write times out, it may return n > 0, indicating that
|
|
|
|
// some of the data was successfully written.
|
2011-11-01 20:05:34 -06:00
|
|
|
SetWriteTimeout(nsec int64) error
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
2009-06-19 17:03:59 -06:00
|
|
|
|
2010-04-26 23:15:25 -06:00
|
|
|
// An Error represents a network error.
|
|
|
|
type Error interface {
|
2011-11-01 20:05:34 -06:00
|
|
|
error
|
2010-04-26 23:15:25 -06:00
|
|
|
Timeout() bool // Is the error a timeout?
|
|
|
|
Temporary() bool // Is the error temporary?
|
|
|
|
}
|
|
|
|
|
2009-11-02 19:37:30 -07:00
|
|
|
// PacketConn is a generic packet-oriented network connection.
|
|
|
|
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.
|
2010-04-26 23:15:25 -06:00
|
|
|
// ReadFrom can be made to time out and return
|
|
|
|
// an error with Timeout() == true after a fixed time limit;
|
|
|
|
// see SetTimeout and SetReadTimeout.
|
2011-11-01 20:05:34 -06:00
|
|
|
ReadFrom(b []byte) (n int, addr Addr, err error)
|
2009-05-13 19:03:41 -06:00
|
|
|
|
2009-11-02 19:37:30 -07:00
|
|
|
// WriteTo writes a packet with payload b to addr.
|
2010-04-26 23:15:25 -06:00
|
|
|
// WriteTo can be made to time out and return
|
|
|
|
// an error with Timeout() == true after a fixed time limit;
|
|
|
|
// see SetTimeout and SetWriteTimeout.
|
2009-11-02 19:37:30 -07:00
|
|
|
// On packet-oriented connections, write timeouts are rare.
|
2011-11-01 20:05:34 -06:00
|
|
|
WriteTo(b []byte, addr Addr) (n int, err error)
|
2009-05-13 19:03:41 -06:00
|
|
|
|
2009-11-02 19:37:30 -07:00
|
|
|
// Close closes the connection.
|
2011-11-01 20:05:34 -06:00
|
|
|
Close() error
|
2009-05-13 19:03:41 -06:00
|
|
|
|
2009-11-02 19:37:30 -07:00
|
|
|
// LocalAddr returns the local network address.
|
2009-12-15 16:35:38 -07:00
|
|
|
LocalAddr() Addr
|
2009-05-13 19:03:41 -06:00
|
|
|
|
|
|
|
// SetTimeout sets the read and write deadlines associated
|
|
|
|
// with the connection.
|
2011-11-01 20:05:34 -06:00
|
|
|
SetTimeout(nsec int64) error
|
2009-05-13 19:03:41 -06:00
|
|
|
|
|
|
|
// SetReadTimeout sets the time (in nanoseconds) that
|
2010-04-26 23:15:25 -06:00
|
|
|
// Read will wait for data before returning an error with Timeout() == true.
|
2009-05-13 19:03:41 -06:00
|
|
|
// Setting nsec == 0 (the default) disables the deadline.
|
2011-11-01 20:05:34 -06:00
|
|
|
SetReadTimeout(nsec int64) error
|
2009-05-13 19:03:41 -06:00
|
|
|
|
|
|
|
// SetWriteTimeout sets the time (in nanoseconds) that
|
2010-04-26 23:15:25 -06:00
|
|
|
// Write will wait to send its data before returning an error with Timeout() == true.
|
2009-05-13 19:03:41 -06:00
|
|
|
// Setting nsec == 0 (the default) disables the deadline.
|
|
|
|
// Even if write times out, it may return n > 0, indicating that
|
|
|
|
// some of the data was successfully written.
|
2011-11-01 20:05:34 -06:00
|
|
|
SetWriteTimeout(nsec int64) error
|
2009-05-13 19:03:41 -06:00
|
|
|
}
|
|
|
|
|
2009-11-02 19:37:30 -07:00
|
|
|
// A Listener is a generic network listener for stream-oriented protocols.
|
2009-11-01 12:15:34 -07:00
|
|
|
type Listener interface {
|
2010-04-26 23:15:25 -06:00
|
|
|
// Accept waits for and returns the next connection to the listener.
|
2011-11-01 20:05:34 -06:00
|
|
|
Accept() (c Conn, err error)
|
2010-04-26 23:15:25 -06:00
|
|
|
|
|
|
|
// Close closes the listener.
|
2011-11-01 20:05:34 -06:00
|
|
|
Close() error
|
2010-04-26 23:15:25 -06:00
|
|
|
|
|
|
|
// Addr returns the listener's network address.
|
|
|
|
Addr() Addr
|
2009-06-17 22:44:26 -06:00
|
|
|
}
|
|
|
|
|
2011-11-01 20:05:34 -06:00
|
|
|
var errMissingAddress = errors.New("missing address")
|
2009-11-01 12:15:34 -07:00
|
|
|
|
|
|
|
type OpError struct {
|
2011-11-01 20:05:34 -06:00
|
|
|
Op string
|
|
|
|
Net string
|
|
|
|
Addr Addr
|
|
|
|
Err error
|
2009-11-01 12:15:34 -07:00
|
|
|
}
|
|
|
|
|
2011-11-01 20:05:34 -06:00
|
|
|
func (e *OpError) Error() string {
|
2010-07-27 01:22:22 -06:00
|
|
|
if e == nil {
|
|
|
|
return "<nil>"
|
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
s := e.Op
|
2009-11-01 12:15:34 -07:00
|
|
|
if e.Net != "" {
|
2009-11-09 13:07:39 -07:00
|
|
|
s += " " + e.Net
|
2009-11-01 12:15:34 -07:00
|
|
|
}
|
2009-11-02 19:37:30 -07:00
|
|
|
if e.Addr != nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
s += " " + e.Addr.String()
|
2009-11-01 12:15:34 -07:00
|
|
|
}
|
2011-11-01 20:05:34 -06:00
|
|
|
s += ": " + e.Err.Error()
|
2009-12-15 16:35:38 -07:00
|
|
|
return s
|
2009-11-01 12:15:34 -07:00
|
|
|
}
|
|
|
|
|
2010-04-26 23:15:25 -06:00
|
|
|
type temporary interface {
|
|
|
|
Temporary() bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *OpError) Temporary() bool {
|
2011-11-01 20:05:34 -06:00
|
|
|
t, ok := e.Err.(temporary)
|
2010-04-26 23:15:25 -06:00
|
|
|
return ok && t.Temporary()
|
|
|
|
}
|
|
|
|
|
|
|
|
type timeout interface {
|
|
|
|
Timeout() bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *OpError) Timeout() bool {
|
2011-11-01 20:05:34 -06:00
|
|
|
t, ok := e.Err.(timeout)
|
2010-04-26 23:15:25 -06:00
|
|
|
return ok && t.Timeout()
|
|
|
|
}
|
|
|
|
|
2011-12-20 15:32:33 -07:00
|
|
|
type timeoutError struct{}
|
|
|
|
|
|
|
|
func (e *timeoutError) Error() string { return "i/o timeout" }
|
|
|
|
func (e *timeoutError) Timeout() bool { return true }
|
|
|
|
func (e *timeoutError) Temporary() bool { return true }
|
|
|
|
|
|
|
|
var errTimeout error = &timeoutError{}
|
|
|
|
|
2009-11-01 12:15:34 -07:00
|
|
|
type AddrError struct {
|
2011-11-01 20:05:34 -06:00
|
|
|
Err string
|
|
|
|
Addr string
|
2009-11-01 12:15:34 -07:00
|
|
|
}
|
|
|
|
|
2011-11-01 20:05:34 -06:00
|
|
|
func (e *AddrError) Error() string {
|
2010-07-27 01:22:22 -06:00
|
|
|
if e == nil {
|
|
|
|
return "<nil>"
|
|
|
|
}
|
2011-11-01 20:05:34 -06:00
|
|
|
s := e.Err
|
2009-11-01 12:15:34 -07:00
|
|
|
if e.Addr != "" {
|
2009-11-09 13:07:39 -07:00
|
|
|
s += " " + e.Addr
|
2009-11-01 12:15:34 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
return s
|
2009-11-01 12:15:34 -07:00
|
|
|
}
|
|
|
|
|
2010-04-26 23:15:25 -06:00
|
|
|
func (e *AddrError) Temporary() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *AddrError) Timeout() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2009-11-01 12:15:34 -07:00
|
|
|
type UnknownNetworkError string
|
2009-11-05 00:16:46 -07:00
|
|
|
|
2011-11-01 20:05:34 -06:00
|
|
|
func (e UnknownNetworkError) Error() string { return "unknown network " + string(e) }
|
2010-04-26 23:15:25 -06:00
|
|
|
func (e UnknownNetworkError) Temporary() bool { return false }
|
|
|
|
func (e UnknownNetworkError) Timeout() bool { return false }
|