1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
14 func sockaddrToTCP(sa syscall
.Sockaddr
) Addr
{
15 switch sa
:= sa
.(type) {
16 case *syscall
.SockaddrInet4
:
17 return &TCPAddr
{sa
.Addr
[0:], sa
.Port
}
18 case *syscall
.SockaddrInet6
:
19 return &TCPAddr
{sa
.Addr
[0:], sa
.Port
}
24 // TCPAddr represents the address of a TCP end point.
30 // Network returns the address's network name, "tcp".
31 func (a
*TCPAddr
) Network() string { return "tcp" }
33 func (a
*TCPAddr
) String() string {
37 return JoinHostPort(a
.IP
.String(), itoa(a
.Port
))
40 func (a
*TCPAddr
) family() int {
41 if a
== nil ||
len(a
.IP
) <= 4 {
42 return syscall
.AF_INET
44 if ip
:= a
.IP
.To4(); ip
!= nil {
45 return syscall
.AF_INET
47 return syscall
.AF_INET6
50 func (a
*TCPAddr
) sockaddr(family
int) (syscall
.Sockaddr
, os
.Error
) {
51 return ipToSockaddr(family
, a
.IP
, a
.Port
)
54 func (a
*TCPAddr
) toAddr() sockaddr
{
55 if a
== nil { // nil *TCPAddr
56 return nil // nil interface
61 // ResolveTCPAddr parses addr as a TCP address of the form
62 // host:port and resolves domain names or port names to
63 // numeric addresses. A literal IPv6 host address must be
64 // enclosed in square brackets, as in "[::]:80".
65 func ResolveTCPAddr(addr
string) (*TCPAddr
, os
.Error
) {
66 ip
, port
, err
:= hostPortToIP("tcp", addr
)
70 return &TCPAddr
{ip
, port
}, nil
73 // TCPConn is an implementation of the Conn interface
74 // for TCP network connections.
79 func newTCPConn(fd
*netFD
) *TCPConn
{
85 func (c
*TCPConn
) ok() bool { return c
!= nil && c
.fd
!= nil }
87 // Implementation of the Conn interface - see Conn for documentation.
89 // Read implements the net.Conn Read method.
90 func (c
*TCPConn
) Read(b
[]byte) (n
int, err os
.Error
) {
97 // Write implements the net.Conn Write method.
98 func (c
*TCPConn
) Write(b
[]byte) (n
int, err os
.Error
) {
105 // Close closes the TCP connection.
106 func (c
*TCPConn
) Close() os
.Error
{
115 // LocalAddr returns the local network address, a *TCPAddr.
116 func (c
*TCPConn
) LocalAddr() Addr
{
123 // RemoteAddr returns the remote network address, a *TCPAddr.
124 func (c
*TCPConn
) RemoteAddr() Addr
{
131 // SetTimeout implements the net.Conn SetTimeout method.
132 func (c
*TCPConn
) SetTimeout(nsec
int64) os
.Error
{
136 return setTimeout(c
.fd
, nsec
)
139 // SetReadTimeout implements the net.Conn SetReadTimeout method.
140 func (c
*TCPConn
) SetReadTimeout(nsec
int64) os
.Error
{
144 return setReadTimeout(c
.fd
, nsec
)
147 // SetWriteTimeout implements the net.Conn SetWriteTimeout method.
148 func (c
*TCPConn
) SetWriteTimeout(nsec
int64) os
.Error
{
152 return setWriteTimeout(c
.fd
, nsec
)
155 // SetReadBuffer sets the size of the operating system's
156 // receive buffer associated with the connection.
157 func (c
*TCPConn
) SetReadBuffer(bytes
int) os
.Error
{
161 return setReadBuffer(c
.fd
, bytes
)
164 // SetWriteBuffer sets the size of the operating system's
165 // transmit buffer associated with the connection.
166 func (c
*TCPConn
) SetWriteBuffer(bytes
int) os
.Error
{
170 return setWriteBuffer(c
.fd
, bytes
)
173 // SetLinger sets the behavior of Close() on a connection
174 // which still has data waiting to be sent or to be acknowledged.
176 // If sec < 0 (the default), Close returns immediately and
177 // the operating system finishes sending the data in the background.
179 // If sec == 0, Close returns immediately and the operating system
180 // discards any unsent or unacknowledged data.
182 // If sec > 0, Close blocks for at most sec seconds waiting for
183 // data to be sent and acknowledged.
184 func (c
*TCPConn
) SetLinger(sec
int) os
.Error
{
188 return setLinger(c
.fd
, sec
)
191 // SetKeepAlive sets whether the operating system should send
192 // keepalive messages on the connection.
193 func (c
*TCPConn
) SetKeepAlive(keepalive
bool) os
.Error
{
197 return setKeepAlive(c
.fd
, keepalive
)
200 // SetNoDelay controls whether the operating system should delay
201 // packet transmission in hopes of sending fewer packets
202 // (Nagle's algorithm). The default is true (no delay), meaning
203 // that data is sent as soon as possible after a Write.
204 func (c
*TCPConn
) SetNoDelay(noDelay
bool) os
.Error
{
208 return setNoDelay(c
.fd
, noDelay
)
211 // File returns a copy of the underlying os.File, set to blocking mode.
212 // It is the caller's responsibility to close f when finished.
213 // Closing c does not affect f, and closing f does not affect c.
214 func (c
*TCPConn
) File() (f
*os
.File
, err os
.Error
) { return c
.fd
.dup() }
216 // DialTCP connects to the remote address raddr on the network net,
217 // which must be "tcp", "tcp4", or "tcp6". If laddr is not nil, it is used
218 // as the local address for the connection.
219 func DialTCP(net
string, laddr
, raddr
*TCPAddr
) (c
*TCPConn
, err os
.Error
) {
221 return nil, &OpError
{"dial", "tcp", nil, errMissingAddress
}
223 fd
, e
:= internetSocket(net
, laddr
.toAddr(), raddr
.toAddr(), syscall
.SOCK_STREAM
, 0, "dial", sockaddrToTCP
)
227 return newTCPConn(fd
), nil
230 // TCPListener is a TCP network listener.
231 // Clients should typically use variables of type Listener
232 // instead of assuming TCP.
233 type TCPListener
struct {
237 // ListenTCP announces on the TCP address laddr and returns a TCP listener.
238 // Net must be "tcp", "tcp4", or "tcp6".
239 // If laddr has a port of 0, it means to listen on some available port.
240 // The caller can use l.Addr() to retrieve the chosen address.
241 func ListenTCP(net
string, laddr
*TCPAddr
) (l
*TCPListener
, err os
.Error
) {
242 fd
, err
:= internetSocket(net
, laddr
.toAddr(), nil, syscall
.SOCK_STREAM
, 0, "listen", sockaddrToTCP
)
246 errno
:= syscall
.Listen(fd
.sysfd
, listenBacklog())
248 closesocket(fd
.sysfd
)
249 return nil, &OpError
{"listen", "tcp", laddr
, os
.Errno(errno
)}
256 // AcceptTCP accepts the next incoming call and returns the new connection
257 // and the remote address.
258 func (l
*TCPListener
) AcceptTCP() (c
*TCPConn
, err os
.Error
) {
259 if l
== nil || l
.fd
== nil || l
.fd
.sysfd
< 0 {
260 return nil, os
.EINVAL
262 fd
, err
:= l
.fd
.accept(sockaddrToTCP
)
266 return newTCPConn(fd
), nil
269 // Accept implements the Accept method in the Listener interface;
270 // it waits for the next call and returns a generic Conn.
271 func (l
*TCPListener
) Accept() (c Conn
, err os
.Error
) {
272 c1
, err
:= l
.AcceptTCP()
279 // Close stops listening on the TCP address.
280 // Already Accepted connections are not closed.
281 func (l
*TCPListener
) Close() os
.Error
{
282 if l
== nil || l
.fd
== nil {
288 // Addr returns the listener's network address, a *TCPAddr.
289 func (l
*TCPListener
) Addr() Addr
{ return l
.fd
.laddr
}
291 // File returns a copy of the underlying os.File, set to blocking mode.
292 // It is the caller's responsibility to close f when finished.
293 // Closing c does not affect f, and closing f does not affect c.
294 func (l
*TCPListener
) File() (f
*os
.File
, err os
.Error
) { return l
.fd
.dup() }