| 1 |
747 |
jeremybenn |
// 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.
|
| 4 |
|
|
|
| 5 |
|
|
// +build darwin freebsd linux netbsd openbsd windows
|
| 6 |
|
|
|
| 7 |
|
|
// TCP sockets
|
| 8 |
|
|
|
| 9 |
|
|
package net
|
| 10 |
|
|
|
| 11 |
|
|
import (
|
| 12 |
|
|
"io"
|
| 13 |
|
|
"os"
|
| 14 |
|
|
"syscall"
|
| 15 |
|
|
"time"
|
| 16 |
|
|
)
|
| 17 |
|
|
|
| 18 |
|
|
// BUG(rsc): On OpenBSD, listening on the "tcp" network does not listen for
|
| 19 |
|
|
// both IPv4 and IPv6 connections. This is due to the fact that IPv4 traffic
|
| 20 |
|
|
// will not be routed to an IPv6 socket - two separate sockets are required
|
| 21 |
|
|
// if both AFs are to be supported. See inet6(4) on OpenBSD for details.
|
| 22 |
|
|
|
| 23 |
|
|
func sockaddrToTCP(sa syscall.Sockaddr) Addr {
|
| 24 |
|
|
switch sa := sa.(type) {
|
| 25 |
|
|
case *syscall.SockaddrInet4:
|
| 26 |
|
|
return &TCPAddr{sa.Addr[0:], sa.Port}
|
| 27 |
|
|
case *syscall.SockaddrInet6:
|
| 28 |
|
|
return &TCPAddr{sa.Addr[0:], sa.Port}
|
| 29 |
|
|
}
|
| 30 |
|
|
return nil
|
| 31 |
|
|
}
|
| 32 |
|
|
|
| 33 |
|
|
func (a *TCPAddr) family() int {
|
| 34 |
|
|
if a == nil || len(a.IP) <= IPv4len {
|
| 35 |
|
|
return syscall.AF_INET
|
| 36 |
|
|
}
|
| 37 |
|
|
if a.IP.To4() != nil {
|
| 38 |
|
|
return syscall.AF_INET
|
| 39 |
|
|
}
|
| 40 |
|
|
return syscall.AF_INET6
|
| 41 |
|
|
}
|
| 42 |
|
|
|
| 43 |
|
|
func (a *TCPAddr) sockaddr(family int) (syscall.Sockaddr, error) {
|
| 44 |
|
|
return ipToSockaddr(family, a.IP, a.Port)
|
| 45 |
|
|
}
|
| 46 |
|
|
|
| 47 |
|
|
func (a *TCPAddr) toAddr() sockaddr {
|
| 48 |
|
|
if a == nil { // nil *TCPAddr
|
| 49 |
|
|
return nil // nil interface
|
| 50 |
|
|
}
|
| 51 |
|
|
return a
|
| 52 |
|
|
}
|
| 53 |
|
|
|
| 54 |
|
|
// TCPConn is an implementation of the Conn interface
|
| 55 |
|
|
// for TCP network connections.
|
| 56 |
|
|
type TCPConn struct {
|
| 57 |
|
|
fd *netFD
|
| 58 |
|
|
}
|
| 59 |
|
|
|
| 60 |
|
|
func newTCPConn(fd *netFD) *TCPConn {
|
| 61 |
|
|
c := &TCPConn{fd}
|
| 62 |
|
|
c.SetNoDelay(true)
|
| 63 |
|
|
return c
|
| 64 |
|
|
}
|
| 65 |
|
|
|
| 66 |
|
|
func (c *TCPConn) ok() bool { return c != nil && c.fd != nil }
|
| 67 |
|
|
|
| 68 |
|
|
// Implementation of the Conn interface - see Conn for documentation.
|
| 69 |
|
|
|
| 70 |
|
|
// Read implements the Conn Read method.
|
| 71 |
|
|
func (c *TCPConn) Read(b []byte) (n int, err error) {
|
| 72 |
|
|
if !c.ok() {
|
| 73 |
|
|
return 0, os.EINVAL
|
| 74 |
|
|
}
|
| 75 |
|
|
return c.fd.Read(b)
|
| 76 |
|
|
}
|
| 77 |
|
|
|
| 78 |
|
|
// ReadFrom implements the io.ReaderFrom ReadFrom method.
|
| 79 |
|
|
func (c *TCPConn) ReadFrom(r io.Reader) (int64, error) {
|
| 80 |
|
|
if n, err, handled := sendFile(c.fd, r); handled {
|
| 81 |
|
|
return n, err
|
| 82 |
|
|
}
|
| 83 |
|
|
return genericReadFrom(c, r)
|
| 84 |
|
|
}
|
| 85 |
|
|
|
| 86 |
|
|
// Write implements the Conn Write method.
|
| 87 |
|
|
func (c *TCPConn) Write(b []byte) (n int, err error) {
|
| 88 |
|
|
if !c.ok() {
|
| 89 |
|
|
return 0, os.EINVAL
|
| 90 |
|
|
}
|
| 91 |
|
|
return c.fd.Write(b)
|
| 92 |
|
|
}
|
| 93 |
|
|
|
| 94 |
|
|
// Close closes the TCP connection.
|
| 95 |
|
|
func (c *TCPConn) Close() error {
|
| 96 |
|
|
if !c.ok() {
|
| 97 |
|
|
return os.EINVAL
|
| 98 |
|
|
}
|
| 99 |
|
|
err := c.fd.Close()
|
| 100 |
|
|
c.fd = nil
|
| 101 |
|
|
return err
|
| 102 |
|
|
}
|
| 103 |
|
|
|
| 104 |
|
|
// CloseRead shuts down the reading side of the TCP connection.
|
| 105 |
|
|
// Most callers should just use Close.
|
| 106 |
|
|
func (c *TCPConn) CloseRead() error {
|
| 107 |
|
|
if !c.ok() {
|
| 108 |
|
|
return os.EINVAL
|
| 109 |
|
|
}
|
| 110 |
|
|
return c.fd.CloseRead()
|
| 111 |
|
|
}
|
| 112 |
|
|
|
| 113 |
|
|
// CloseWrite shuts down the writing side of the TCP connection.
|
| 114 |
|
|
// Most callers should just use Close.
|
| 115 |
|
|
func (c *TCPConn) CloseWrite() error {
|
| 116 |
|
|
if !c.ok() {
|
| 117 |
|
|
return os.EINVAL
|
| 118 |
|
|
}
|
| 119 |
|
|
return c.fd.CloseWrite()
|
| 120 |
|
|
}
|
| 121 |
|
|
|
| 122 |
|
|
// LocalAddr returns the local network address, a *TCPAddr.
|
| 123 |
|
|
func (c *TCPConn) LocalAddr() Addr {
|
| 124 |
|
|
if !c.ok() {
|
| 125 |
|
|
return nil
|
| 126 |
|
|
}
|
| 127 |
|
|
return c.fd.laddr
|
| 128 |
|
|
}
|
| 129 |
|
|
|
| 130 |
|
|
// RemoteAddr returns the remote network address, a *TCPAddr.
|
| 131 |
|
|
func (c *TCPConn) RemoteAddr() Addr {
|
| 132 |
|
|
if !c.ok() {
|
| 133 |
|
|
return nil
|
| 134 |
|
|
}
|
| 135 |
|
|
return c.fd.raddr
|
| 136 |
|
|
}
|
| 137 |
|
|
|
| 138 |
|
|
// SetDeadline implements the Conn SetDeadline method.
|
| 139 |
|
|
func (c *TCPConn) SetDeadline(t time.Time) error {
|
| 140 |
|
|
if !c.ok() {
|
| 141 |
|
|
return os.EINVAL
|
| 142 |
|
|
}
|
| 143 |
|
|
return setDeadline(c.fd, t)
|
| 144 |
|
|
}
|
| 145 |
|
|
|
| 146 |
|
|
// SetReadDeadline implements the Conn SetReadDeadline method.
|
| 147 |
|
|
func (c *TCPConn) SetReadDeadline(t time.Time) error {
|
| 148 |
|
|
if !c.ok() {
|
| 149 |
|
|
return os.EINVAL
|
| 150 |
|
|
}
|
| 151 |
|
|
return setReadDeadline(c.fd, t)
|
| 152 |
|
|
}
|
| 153 |
|
|
|
| 154 |
|
|
// SetWriteDeadline implements the Conn SetWriteDeadline method.
|
| 155 |
|
|
func (c *TCPConn) SetWriteDeadline(t time.Time) error {
|
| 156 |
|
|
if !c.ok() {
|
| 157 |
|
|
return os.EINVAL
|
| 158 |
|
|
}
|
| 159 |
|
|
return setWriteDeadline(c.fd, t)
|
| 160 |
|
|
}
|
| 161 |
|
|
|
| 162 |
|
|
// SetReadBuffer sets the size of the operating system's
|
| 163 |
|
|
// receive buffer associated with the connection.
|
| 164 |
|
|
func (c *TCPConn) SetReadBuffer(bytes int) error {
|
| 165 |
|
|
if !c.ok() {
|
| 166 |
|
|
return os.EINVAL
|
| 167 |
|
|
}
|
| 168 |
|
|
return setReadBuffer(c.fd, bytes)
|
| 169 |
|
|
}
|
| 170 |
|
|
|
| 171 |
|
|
// SetWriteBuffer sets the size of the operating system's
|
| 172 |
|
|
// transmit buffer associated with the connection.
|
| 173 |
|
|
func (c *TCPConn) SetWriteBuffer(bytes int) error {
|
| 174 |
|
|
if !c.ok() {
|
| 175 |
|
|
return os.EINVAL
|
| 176 |
|
|
}
|
| 177 |
|
|
return setWriteBuffer(c.fd, bytes)
|
| 178 |
|
|
}
|
| 179 |
|
|
|
| 180 |
|
|
// SetLinger sets the behavior of Close() on a connection
|
| 181 |
|
|
// which still has data waiting to be sent or to be acknowledged.
|
| 182 |
|
|
//
|
| 183 |
|
|
// If sec < 0 (the default), Close returns immediately and
|
| 184 |
|
|
// the operating system finishes sending the data in the background.
|
| 185 |
|
|
//
|
| 186 |
|
|
// If sec == 0, Close returns immediately and the operating system
|
| 187 |
|
|
// discards any unsent or unacknowledged data.
|
| 188 |
|
|
//
|
| 189 |
|
|
// If sec > 0, Close blocks for at most sec seconds waiting for
|
| 190 |
|
|
// data to be sent and acknowledged.
|
| 191 |
|
|
func (c *TCPConn) SetLinger(sec int) error {
|
| 192 |
|
|
if !c.ok() {
|
| 193 |
|
|
return os.EINVAL
|
| 194 |
|
|
}
|
| 195 |
|
|
return setLinger(c.fd, sec)
|
| 196 |
|
|
}
|
| 197 |
|
|
|
| 198 |
|
|
// SetKeepAlive sets whether the operating system should send
|
| 199 |
|
|
// keepalive messages on the connection.
|
| 200 |
|
|
func (c *TCPConn) SetKeepAlive(keepalive bool) error {
|
| 201 |
|
|
if !c.ok() {
|
| 202 |
|
|
return os.EINVAL
|
| 203 |
|
|
}
|
| 204 |
|
|
return setKeepAlive(c.fd, keepalive)
|
| 205 |
|
|
}
|
| 206 |
|
|
|
| 207 |
|
|
// SetNoDelay controls whether the operating system should delay
|
| 208 |
|
|
// packet transmission in hopes of sending fewer packets
|
| 209 |
|
|
// (Nagle's algorithm). The default is true (no delay), meaning
|
| 210 |
|
|
// that data is sent as soon as possible after a Write.
|
| 211 |
|
|
func (c *TCPConn) SetNoDelay(noDelay bool) error {
|
| 212 |
|
|
if !c.ok() {
|
| 213 |
|
|
return os.EINVAL
|
| 214 |
|
|
}
|
| 215 |
|
|
return setNoDelay(c.fd, noDelay)
|
| 216 |
|
|
}
|
| 217 |
|
|
|
| 218 |
|
|
// File returns a copy of the underlying os.File, set to blocking mode.
|
| 219 |
|
|
// It is the caller's responsibility to close f when finished.
|
| 220 |
|
|
// Closing c does not affect f, and closing f does not affect c.
|
| 221 |
|
|
func (c *TCPConn) File() (f *os.File, err error) { return c.fd.dup() }
|
| 222 |
|
|
|
| 223 |
|
|
// DialTCP connects to the remote address raddr on the network net,
|
| 224 |
|
|
// which must be "tcp", "tcp4", or "tcp6". If laddr is not nil, it is used
|
| 225 |
|
|
// as the local address for the connection.
|
| 226 |
|
|
func DialTCP(net string, laddr, raddr *TCPAddr) (*TCPConn, error) {
|
| 227 |
|
|
if raddr == nil {
|
| 228 |
|
|
return nil, &OpError{"dial", net, nil, errMissingAddress}
|
| 229 |
|
|
}
|
| 230 |
|
|
fd, err := internetSocket(net, laddr.toAddr(), raddr.toAddr(), syscall.SOCK_STREAM, 0, "dial", sockaddrToTCP)
|
| 231 |
|
|
if err != nil {
|
| 232 |
|
|
return nil, err
|
| 233 |
|
|
}
|
| 234 |
|
|
return newTCPConn(fd), nil
|
| 235 |
|
|
}
|
| 236 |
|
|
|
| 237 |
|
|
// TCPListener is a TCP network listener.
|
| 238 |
|
|
// Clients should typically use variables of type Listener
|
| 239 |
|
|
// instead of assuming TCP.
|
| 240 |
|
|
type TCPListener struct {
|
| 241 |
|
|
fd *netFD
|
| 242 |
|
|
}
|
| 243 |
|
|
|
| 244 |
|
|
// ListenTCP announces on the TCP address laddr and returns a TCP listener.
|
| 245 |
|
|
// Net must be "tcp", "tcp4", or "tcp6".
|
| 246 |
|
|
// If laddr has a port of 0, it means to listen on some available port.
|
| 247 |
|
|
// The caller can use l.Addr() to retrieve the chosen address.
|
| 248 |
|
|
func ListenTCP(net string, laddr *TCPAddr) (*TCPListener, error) {
|
| 249 |
|
|
fd, err := internetSocket(net, laddr.toAddr(), nil, syscall.SOCK_STREAM, 0, "listen", sockaddrToTCP)
|
| 250 |
|
|
if err != nil {
|
| 251 |
|
|
return nil, err
|
| 252 |
|
|
}
|
| 253 |
|
|
err = syscall.Listen(fd.sysfd, listenerBacklog)
|
| 254 |
|
|
if err != nil {
|
| 255 |
|
|
closesocket(fd.sysfd)
|
| 256 |
|
|
return nil, &OpError{"listen", net, laddr, err}
|
| 257 |
|
|
}
|
| 258 |
|
|
l := new(TCPListener)
|
| 259 |
|
|
l.fd = fd
|
| 260 |
|
|
return l, nil
|
| 261 |
|
|
}
|
| 262 |
|
|
|
| 263 |
|
|
// AcceptTCP accepts the next incoming call and returns the new connection
|
| 264 |
|
|
// and the remote address.
|
| 265 |
|
|
func (l *TCPListener) AcceptTCP() (c *TCPConn, err error) {
|
| 266 |
|
|
if l == nil || l.fd == nil || l.fd.sysfd < 0 {
|
| 267 |
|
|
return nil, os.EINVAL
|
| 268 |
|
|
}
|
| 269 |
|
|
fd, err := l.fd.accept(sockaddrToTCP)
|
| 270 |
|
|
if err != nil {
|
| 271 |
|
|
return nil, err
|
| 272 |
|
|
}
|
| 273 |
|
|
return newTCPConn(fd), nil
|
| 274 |
|
|
}
|
| 275 |
|
|
|
| 276 |
|
|
// Accept implements the Accept method in the Listener interface;
|
| 277 |
|
|
// it waits for the next call and returns a generic Conn.
|
| 278 |
|
|
func (l *TCPListener) Accept() (c Conn, err error) {
|
| 279 |
|
|
c1, err := l.AcceptTCP()
|
| 280 |
|
|
if err != nil {
|
| 281 |
|
|
return nil, err
|
| 282 |
|
|
}
|
| 283 |
|
|
return c1, nil
|
| 284 |
|
|
}
|
| 285 |
|
|
|
| 286 |
|
|
// Close stops listening on the TCP address.
|
| 287 |
|
|
// Already Accepted connections are not closed.
|
| 288 |
|
|
func (l *TCPListener) Close() error {
|
| 289 |
|
|
if l == nil || l.fd == nil {
|
| 290 |
|
|
return os.EINVAL
|
| 291 |
|
|
}
|
| 292 |
|
|
return l.fd.Close()
|
| 293 |
|
|
}
|
| 294 |
|
|
|
| 295 |
|
|
// Addr returns the listener's network address, a *TCPAddr.
|
| 296 |
|
|
func (l *TCPListener) Addr() Addr { return l.fd.laddr }
|
| 297 |
|
|
|
| 298 |
|
|
// SetDeadline sets the deadline associated with the listener.
|
| 299 |
|
|
// A zero time value disables the deadline.
|
| 300 |
|
|
func (l *TCPListener) SetDeadline(t time.Time) error {
|
| 301 |
|
|
if l == nil || l.fd == nil {
|
| 302 |
|
|
return os.EINVAL
|
| 303 |
|
|
}
|
| 304 |
|
|
return setDeadline(l.fd, t)
|
| 305 |
|
|
}
|
| 306 |
|
|
|
| 307 |
|
|
// File returns a copy of the underlying os.File, set to blocking mode.
|
| 308 |
|
|
// It is the caller's responsibility to close f when finished.
|
| 309 |
|
|
// Closing c does not affect f, and closing f does not affect c.
|
| 310 |
|
|
func (l *TCPListener) File() (f *os.File, err error) { return l.fd.dup() }
|