2011-04-11 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / libgo / go / net / tcpsock.go
blobb484be20b463dd7af28617f40e2d5d2479588c72
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 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) {
220 if raddr == nil {
221 return nil, &OpError{"dial", "tcp", nil, errMissingAddress}
223 fd, e := internetSocket(net, laddr.toAddr(), raddr.toAddr(), syscall.SOCK_STREAM, 0, "dial", sockaddrToTCP)
224 if e != nil {
225 return nil, e
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 {
234 fd *netFD
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)
243 if err != nil {
244 return nil, err
246 errno := syscall.Listen(fd.sysfd, listenBacklog())
247 if errno != 0 {
248 closesocket(fd.sysfd)
249 return nil, &OpError{"listen", "tcp", laddr, os.Errno(errno)}
251 l = new(TCPListener)
252 l.fd = fd
253 return l, nil
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)
263 if err != nil {
264 return nil, err
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()
273 if err != nil {
274 return nil, err
276 return c1, nil
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 {
283 return os.EINVAL
285 return l.fd.Close()
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() }