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.
|
|
|
|
|
|
|
|
package net
|
|
|
|
|
2009-11-01 12:15:34 -07:00
|
|
|
// TODO(rsc):
|
|
|
|
// support for raw IP sockets
|
|
|
|
// support for raw ethernet sockets
|
2008-09-16 14:42:47 -06:00
|
|
|
|
2009-11-01 12:15:34 -07:00
|
|
|
import "os"
|
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 {
|
|
|
|
Network() string; // name of the network
|
2009-11-05 00:16:46 -07:00
|
|
|
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.
|
|
|
|
// Read can be made to time out and return err == os.EAGAIN
|
|
|
|
// after a fixed time limit; see SetTimeout and SetReadTimeout.
|
2009-05-13 19:03:41 -06:00
|
|
|
Read(b []byte) (n int, err os.Error);
|
|
|
|
|
2009-11-02 19:37:30 -07:00
|
|
|
// Write writes data to the connection.
|
|
|
|
// Write can be made to time out and return err == os.EAGAIN
|
|
|
|
// after a fixed time limit; see SetTimeout and SetReadTimeout.
|
2009-05-13 19:03:41 -06:00
|
|
|
Write(b []byte) (n int, err os.Error);
|
|
|
|
|
|
|
|
// Close closes the connection.
|
|
|
|
Close() os.Error;
|
|
|
|
|
2009-06-19 17:03:59 -06:00
|
|
|
// LocalAddr returns the local network address.
|
2009-11-02 19:37:30 -07:00
|
|
|
LocalAddr() Addr;
|
2009-06-19 17:03:59 -06:00
|
|
|
|
|
|
|
// RemoteAddr returns the remote network address.
|
2009-11-02 19:37:30 -07:00
|
|
|
RemoteAddr() Addr;
|
|
|
|
|
|
|
|
// SetTimeout sets the read and write deadlines associated
|
|
|
|
// with the connection.
|
|
|
|
SetTimeout(nsec int64) os.Error;
|
|
|
|
|
|
|
|
// SetReadTimeout sets the time (in nanoseconds) that
|
|
|
|
// Read will wait for data before returning os.EAGAIN.
|
|
|
|
// Setting nsec == 0 (the default) disables the deadline.
|
|
|
|
SetReadTimeout(nsec int64) os.Error;
|
|
|
|
|
|
|
|
// SetWriteTimeout sets the time (in nanoseconds) that
|
|
|
|
// Write will wait to send its data before returning os.EAGAIN.
|
|
|
|
// 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.
|
|
|
|
SetWriteTimeout(nsec int64) os.Error;
|
|
|
|
}
|
2009-06-19 17:03:59 -06:00
|
|
|
|
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.
|
|
|
|
// ReadFrom can be made to time out and return err == os.EAGAIN
|
|
|
|
// after a fixed time limit; see SetTimeout and SetReadTimeout.
|
|
|
|
ReadFrom(b []byte) (n int, addr Addr, err os.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.
|
|
|
|
// WriteTo can be made to time out and return err == os.EAGAIN
|
|
|
|
// after a fixed time limit; see SetTimeout and SetWriteTimeout.
|
|
|
|
// On packet-oriented connections, write timeouts are rare.
|
|
|
|
WriteTo(b []byte, addr Addr) (n int, err os.Error);
|
2009-05-13 19:03:41 -06:00
|
|
|
|
2009-11-02 19:37:30 -07:00
|
|
|
// Close closes the connection.
|
|
|
|
Close() os.Error;
|
2009-05-13 19:03:41 -06:00
|
|
|
|
2009-11-02 19:37:30 -07:00
|
|
|
// LocalAddr returns the local network address.
|
|
|
|
LocalAddr() Addr;
|
2009-05-13 19:03:41 -06:00
|
|
|
|
|
|
|
// SetTimeout sets the read and write deadlines associated
|
|
|
|
// with the connection.
|
|
|
|
SetTimeout(nsec int64) os.Error;
|
|
|
|
|
|
|
|
// SetReadTimeout sets the time (in nanoseconds) that
|
|
|
|
// Read will wait for data before returning os.EAGAIN.
|
|
|
|
// Setting nsec == 0 (the default) disables the deadline.
|
|
|
|
SetReadTimeout(nsec int64) os.Error;
|
|
|
|
|
|
|
|
// SetWriteTimeout sets the time (in nanoseconds) that
|
|
|
|
// Write will wait to send its data before returning os.EAGAIN.
|
|
|
|
// 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.
|
|
|
|
SetWriteTimeout(nsec int64) os.Error;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
// Accept waits for the next connection and Close closes the connection.
|
|
|
|
type Listener interface {
|
2009-11-02 19:37:30 -07:00
|
|
|
Accept() (c Conn, err os.Error);
|
2009-11-01 12:15:34 -07:00
|
|
|
Close() os.Error;
|
2009-11-02 19:37:30 -07:00
|
|
|
Addr() Addr; // Listener's network address
|
2009-06-17 22:44:26 -06:00
|
|
|
}
|
|
|
|
|
2009-03-05 16:48:12 -07:00
|
|
|
// Dial connects to the remote address raddr on the network net.
|
|
|
|
// If the string laddr is not empty, it is used as the local address
|
|
|
|
// for the connection.
|
|
|
|
//
|
|
|
|
// Known networks are "tcp", "tcp4" (IPv4-only), "tcp6" (IPv6-only),
|
|
|
|
// "udp", "udp4" (IPv4-only), and "udp6" (IPv6-only).
|
|
|
|
//
|
|
|
|
// For IP networks, addresses have the form host:port. If host is
|
|
|
|
// a literal IPv6 address, it must be enclosed in square brackets.
|
|
|
|
//
|
2008-09-16 14:42:47 -06:00
|
|
|
// Examples:
|
|
|
|
// Dial("tcp", "", "12.34.56.78:80")
|
2009-03-05 16:48:12 -07:00
|
|
|
// Dial("tcp", "", "google.com:80")
|
2008-09-16 14:42:47 -06:00
|
|
|
// Dial("tcp", "", "[de:ad:be:ef::ca:fe]:80")
|
|
|
|
// Dial("tcp", "127.0.0.1:123", "127.0.0.1:88")
|
2009-11-02 19:37:30 -07:00
|
|
|
//
|
2009-04-17 01:08:24 -06:00
|
|
|
func Dial(net, laddr, raddr string) (c Conn, err os.Error) {
|
2008-09-30 15:03:13 -06:00
|
|
|
switch net {
|
2008-09-16 14:42:47 -06:00
|
|
|
case "tcp", "tcp4", "tcp6":
|
2009-11-02 19:37:30 -07:00
|
|
|
var la, ra *TCPAddr;
|
|
|
|
if laddr != "" {
|
|
|
|
if la, err = ResolveTCPAddr(laddr); err != nil {
|
|
|
|
goto Error;
|
|
|
|
}
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
2009-11-02 19:37:30 -07:00
|
|
|
if raddr != "" {
|
|
|
|
if ra, err = ResolveTCPAddr(raddr); err != nil {
|
|
|
|
goto Error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return DialTCP(net, la, ra);
|
2008-09-16 14:42:47 -06:00
|
|
|
case "udp", "udp4", "upd6":
|
2009-11-02 19:37:30 -07:00
|
|
|
var la, ra *UDPAddr;
|
|
|
|
if laddr != "" {
|
|
|
|
if la, err = ResolveUDPAddr(laddr); err != nil {
|
|
|
|
goto Error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if raddr != "" {
|
|
|
|
if ra, err = ResolveUDPAddr(raddr); err != nil {
|
|
|
|
goto Error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return DialUDP(net, la, ra);
|
|
|
|
case "unix", "unixgram":
|
|
|
|
var la, ra *UnixAddr;
|
|
|
|
if raddr != "" {
|
|
|
|
if ra, err = ResolveUnixAddr(net, raddr); err != nil {
|
|
|
|
goto Error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if laddr != "" {
|
|
|
|
if la, err = ResolveUnixAddr(net, laddr); err != nil {
|
|
|
|
goto Error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return DialUnix(net, la, ra);
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
2009-11-02 19:37:30 -07:00
|
|
|
err = UnknownNetworkError(net);
|
|
|
|
Error:
|
|
|
|
return nil, &OpError{"dial", net+" "+raddr, nil, err};
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
|
|
|
|
2009-03-05 16:48:12 -07:00
|
|
|
// Listen announces on the local network address laddr.
|
2009-11-02 19:37:30 -07:00
|
|
|
// The network string net must be a stream-oriented
|
|
|
|
// network: "tcp", "tcp4", "tcp6", or "unix".
|
2009-04-17 01:08:24 -06:00
|
|
|
func Listen(net, laddr string) (l Listener, err os.Error) {
|
2008-09-30 15:03:13 -06:00
|
|
|
switch net {
|
2008-09-17 14:49:23 -06:00
|
|
|
case "tcp", "tcp4", "tcp6":
|
2009-11-02 19:37:30 -07:00
|
|
|
var la *TCPAddr;
|
|
|
|
if laddr != "" {
|
|
|
|
if la, err = ResolveTCPAddr(laddr); err != nil {
|
|
|
|
return nil, err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
l, err := ListenTCP(net, la);
|
2008-09-17 14:49:23 -06:00
|
|
|
if err != nil {
|
2009-05-13 19:03:41 -06:00
|
|
|
return nil, err;
|
|
|
|
}
|
|
|
|
return l, nil;
|
2009-11-02 19:37:30 -07:00
|
|
|
case "unix":
|
|
|
|
var la *UnixAddr;
|
|
|
|
if laddr != "" {
|
|
|
|
if la, err = ResolveUnixAddr(net, laddr); err != nil {
|
|
|
|
return nil, err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
l, err := ListenUnix(net, la);
|
2009-05-13 19:03:41 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err;
|
2008-09-17 14:49:23 -06:00
|
|
|
}
|
2009-05-13 19:03:41 -06:00
|
|
|
return l, nil;
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
|
|
|
return nil, UnknownNetworkError(net);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListenPacket announces on the local network address laddr.
|
|
|
|
// The network string net must be a packet-oriented network:
|
|
|
|
// "udp", "udp4", "udp6", or "unixgram".
|
|
|
|
func ListenPacket(net, laddr string) (c PacketConn, err os.Error) {
|
|
|
|
switch net {
|
|
|
|
case "udp", "udp4", "udp6":
|
|
|
|
var la *UDPAddr;
|
|
|
|
if laddr != "" {
|
|
|
|
if la, err = ResolveUDPAddr(laddr); err != nil {
|
|
|
|
return nil, err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
c, err := ListenUDP(net, la);
|
|
|
|
if err != nil {
|
|
|
|
return nil, err;
|
|
|
|
}
|
|
|
|
return c, nil;
|
|
|
|
case "unixgram":
|
|
|
|
var la *UnixAddr;
|
|
|
|
if laddr != "" {
|
|
|
|
if la, err = ResolveUnixAddr(net, laddr); err != nil {
|
|
|
|
return nil, err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
c, err := DialUnix(net, la, nil);
|
|
|
|
if err != nil {
|
|
|
|
return nil, err;
|
|
|
|
}
|
|
|
|
return c, nil;
|
2008-09-17 14:49:23 -06:00
|
|
|
}
|
2009-06-25 21:24:55 -06:00
|
|
|
return nil, UnknownNetworkError(net);
|
2008-09-17 14:49:23 -06:00
|
|
|
}
|
2008-09-26 15:11:26 -06:00
|
|
|
|
2009-11-01 12:15:34 -07:00
|
|
|
var errMissingAddress = os.ErrorString("missing address")
|
|
|
|
|
|
|
|
type OpError struct {
|
2009-11-05 00:16:46 -07:00
|
|
|
Op string;
|
|
|
|
Net string;
|
|
|
|
Addr Addr;
|
|
|
|
Error os.Error;
|
2009-11-01 12:15:34 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *OpError) String() string {
|
|
|
|
s := e.Op;
|
|
|
|
if e.Net != "" {
|
|
|
|
s += " " + e.Net;
|
|
|
|
}
|
2009-11-02 19:37:30 -07:00
|
|
|
if e.Addr != nil {
|
|
|
|
s += " " + e.Addr.String();
|
2009-11-01 12:15:34 -07:00
|
|
|
}
|
|
|
|
s += ": " + e.Error.String();
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
type AddrError struct {
|
2009-11-05 00:16:46 -07:00
|
|
|
Error string;
|
|
|
|
Addr string;
|
2009-11-01 12:15:34 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *AddrError) String() string {
|
|
|
|
s := e.Error;
|
|
|
|
if e.Addr != "" {
|
|
|
|
s += " " + e.Addr;
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
type UnknownNetworkError string
|
2009-11-05 00:16:46 -07:00
|
|
|
|
2009-11-01 12:15:34 -07:00
|
|
|
func (e UnknownNetworkError) String() string {
|
|
|
|
return "unknown network " + string(e);
|
|
|
|
}
|