Merge from mainline (167278:168000).
[official-gcc/graphite-test-results.git] / libgo / go / net / tcpsock.go
blobb0cb8f99926411fef69872331e34f7776ceccbc9
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.
5 // TCP sockets
7 package net
9 import (
10 "os"
11 "syscall"
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}
21 return nil
24 // TCPAddr represents the address of a TCP end point.
25 type TCPAddr struct {
26 IP IP
27 Port int
30 // Network returns the address's network name, "tcp".
31 func (a *TCPAddr) Network() string { return "tcp" }
33 func (a *TCPAddr) String() string {
34 if a == nil {
35 return "<nil>"
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
58 return a
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)
67 if err != nil {
68 return nil, err
70 return &TCPAddr{ip, port}, nil
73 // TCPConn is an implementation of the Conn interface
74 // for TCP network connections.
75 type TCPConn struct {
76 fd *netFD
79 func newTCPConn(fd *netFD) *TCPConn {
80 c := &TCPConn{fd}
81 c.SetNoDelay(true)
82 return c
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) {
91 if !c.ok() {
92 return 0, os.EINVAL
94 return c.fd.Read(b)
97 // Write implements the net.Conn Write method.
98 func (c *TCPConn) Write(b []byte) (n int, err os.Error) {
99 if !c.ok() {
100 return 0, os.EINVAL
102 return c.fd.Write(b)
105 // Close closes the TCP connection.
106 func (c *TCPConn) Close() os.Error {
107 if !c.ok() {
108 return os.EINVAL
110 err := c.fd.Close()
111 c.fd = nil
112 return err
115 // LocalAddr returns the local network address, a *TCPAddr.
116 func (c *TCPConn) LocalAddr() Addr {
117 if !c.ok() {
118 return nil
120 return c.fd.laddr
123 // RemoteAddr returns the remote network address, a *TCPAddr.
124 func (c *TCPConn) RemoteAddr() Addr {
125 if !c.ok() {
126 return nil
128 return c.fd.raddr
131 // SetTimeout implements the net.Conn SetTimeout method.
132 func (c *TCPConn) SetTimeout(nsec int64) os.Error {
133 if !c.ok() {
134 return os.EINVAL
136 return setTimeout(c.fd, nsec)
139 // SetReadTimeout implements the net.Conn SetReadTimeout method.
140 func (c *TCPConn) SetReadTimeout(nsec int64) os.Error {
141 if !c.ok() {
142 return os.EINVAL
144 return setReadTimeout(c.fd, nsec)
147 // SetWriteTimeout implements the net.Conn SetWriteTimeout method.
148 func (c *TCPConn) SetWriteTimeout(nsec int64) os.Error {
149 if !c.ok() {
150 return os.EINVAL
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 {
158 if !c.ok() {
159 return os.EINVAL
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 {
167 if !c.ok() {
168 return os.EINVAL
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 {
185 if !c.ok() {
186 return os.EINVAL
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 {
194 if !c.ok() {
195 return os.EINVAL
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 {
205 if !c.ok() {
206 return os.EINVAL
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 is like Dial but can only connect to TCP networks
217 // and returns a TCPConn structure.
218 func DialTCP(net string, laddr, raddr *TCPAddr) (c *TCPConn, err os.Error) {
219 if raddr == nil {
220 return nil, &OpError{"dial", "tcp", nil, errMissingAddress}
222 fd, e := internetSocket(net, laddr.toAddr(), raddr.toAddr(), syscall.SOCK_STREAM, 0, "dial", sockaddrToTCP)
223 if e != nil {
224 return nil, e
226 return newTCPConn(fd), nil
229 // TCPListener is a TCP network listener.
230 // Clients should typically use variables of type Listener
231 // instead of assuming TCP.
232 type TCPListener struct {
233 fd *netFD
236 // ListenTCP announces on the TCP address laddr and returns a TCP listener.
237 // Net must be "tcp", "tcp4", or "tcp6".
238 // If laddr has a port of 0, it means to listen on some available port.
239 // The caller can use l.Addr() to retrieve the chosen address.
240 func ListenTCP(net string, laddr *TCPAddr) (l *TCPListener, err os.Error) {
241 fd, err := internetSocket(net, laddr.toAddr(), nil, syscall.SOCK_STREAM, 0, "listen", sockaddrToTCP)
242 if err != nil {
243 return nil, err
245 errno := syscall.Listen(fd.sysfd, listenBacklog())
246 if errno != 0 {
247 syscall.Close(fd.sysfd)
248 return nil, &OpError{"listen", "tcp", laddr, os.Errno(errno)}
250 l = new(TCPListener)
251 l.fd = fd
252 return l, nil
255 // AcceptTCP accepts the next incoming call and returns the new connection
256 // and the remote address.
257 func (l *TCPListener) AcceptTCP() (c *TCPConn, err os.Error) {
258 if l == nil || l.fd == nil || l.fd.sysfd < 0 {
259 return nil, os.EINVAL
261 fd, err := l.fd.accept(sockaddrToTCP)
262 if err != nil {
263 return nil, err
265 return newTCPConn(fd), nil
268 // Accept implements the Accept method in the Listener interface;
269 // it waits for the next call and returns a generic Conn.
270 func (l *TCPListener) Accept() (c Conn, err os.Error) {
271 c1, err := l.AcceptTCP()
272 if err != nil {
273 return nil, err
275 return c1, nil
278 // Close stops listening on the TCP address.
279 // Already Accepted connections are not closed.
280 func (l *TCPListener) Close() os.Error {
281 if l == nil || l.fd == nil {
282 return os.EINVAL
284 return l.fd.Close()
287 // Addr returns the listener's network address, a *TCPAddr.
288 func (l *TCPListener) Addr() Addr { return l.fd.laddr }
290 // File returns a copy of the underlying os.File, set to blocking mode.
291 // It is the caller's responsibility to close f when finished.
292 // Closing c does not affect f, and closing f does not affect c.
293 func (l *TCPListener) File() (f *os.File, err os.Error) { return l.fd.dup() }