github.com/activestate/go@v0.0.0-20170614201249-0b81c023a722/src/net/tcpsock.go (about) 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. 4 5 package net 6 7 import ( 8 "context" 9 "io" 10 "os" 11 "syscall" 12 "time" 13 ) 14 15 // BUG(mikio): On Windows, the File method of TCPListener is not 16 // implemented. 17 18 // TCPAddr represents the address of a TCP end point. 19 type TCPAddr struct { 20 IP IP 21 Port int 22 Zone string // IPv6 scoped addressing zone 23 } 24 25 // Network returns the address's network name, "tcp". 26 func (a *TCPAddr) Network() string { return "tcp" } 27 28 func (a *TCPAddr) String() string { 29 if a == nil { 30 return "<nil>" 31 } 32 ip := ipEmptyString(a.IP) 33 if a.Zone != "" { 34 return JoinHostPort(ip+"%"+a.Zone, itoa(a.Port)) 35 } 36 return JoinHostPort(ip, itoa(a.Port)) 37 } 38 39 func (a *TCPAddr) isWildcard() bool { 40 if a == nil || a.IP == nil { 41 return true 42 } 43 return a.IP.IsUnspecified() 44 } 45 46 func (a *TCPAddr) opAddr() Addr { 47 if a == nil { 48 return nil 49 } 50 return a 51 } 52 53 // ResolveTCPAddr parses addr as a TCP address of the form "host:port" 54 // or "[ipv6-host%zone]:port" and resolves a pair of domain name and 55 // port name on the network net, which must be "tcp", "tcp4" or 56 // "tcp6". A literal address or host name for IPv6 must be enclosed 57 // in square brackets, as in "[::1]:80", "[ipv6-host]:http" or 58 // "[ipv6-host%zone]:80". 59 // 60 // Resolving a hostname is not recommended because this returns at most 61 // one of its IP addresses. 62 func ResolveTCPAddr(net, addr string) (*TCPAddr, error) { 63 switch net { 64 case "tcp", "tcp4", "tcp6": 65 case "": // a hint wildcard for Go 1.0 undocumented behavior 66 net = "tcp" 67 default: 68 return nil, UnknownNetworkError(net) 69 } 70 addrs, err := DefaultResolver.internetAddrList(context.Background(), net, addr) 71 if err != nil { 72 return nil, err 73 } 74 return addrs.first(isIPv4).(*TCPAddr), nil 75 } 76 77 // TCPConn is an implementation of the Conn interface for TCP network 78 // connections. 79 type TCPConn struct { 80 conn 81 } 82 83 // SyscallConn returns a raw network connection. 84 // This implements the syscall.Conn interface. 85 func (c *TCPConn) SyscallConn() (syscall.RawConn, error) { 86 if !c.ok() { 87 return nil, syscall.EINVAL 88 } 89 return newRawConn(c.fd) 90 } 91 92 // ReadFrom implements the io.ReaderFrom ReadFrom method. 93 func (c *TCPConn) ReadFrom(r io.Reader) (int64, error) { 94 if !c.ok() { 95 return 0, syscall.EINVAL 96 } 97 n, err := c.readFrom(r) 98 if err != nil && err != io.EOF { 99 err = &OpError{Op: "readfrom", Net: c.fd.net, Source: c.fd.laddr, Addr: c.fd.raddr, Err: err} 100 } 101 return n, 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 syscall.EINVAL 109 } 110 if err := c.fd.closeRead(); err != nil { 111 return &OpError{Op: "close", Net: c.fd.net, Source: c.fd.laddr, Addr: c.fd.raddr, Err: err} 112 } 113 return nil 114 } 115 116 // CloseWrite shuts down the writing side of the TCP connection. 117 // Most callers should just use Close. 118 func (c *TCPConn) CloseWrite() error { 119 if !c.ok() { 120 return syscall.EINVAL 121 } 122 if err := c.fd.closeWrite(); err != nil { 123 return &OpError{Op: "close", Net: c.fd.net, Source: c.fd.laddr, Addr: c.fd.raddr, Err: err} 124 } 125 return nil 126 } 127 128 // SetLinger sets the behavior of Close on a connection which still 129 // has data waiting to be sent or to be acknowledged. 130 // 131 // If sec < 0 (the default), the operating system finishes sending the 132 // data in the background. 133 // 134 // If sec == 0, the operating system discards any unsent or 135 // unacknowledged data. 136 // 137 // If sec > 0, the data is sent in the background as with sec < 0. On 138 // some operating systems after sec seconds have elapsed any remaining 139 // unsent data may be discarded. 140 func (c *TCPConn) SetLinger(sec int) error { 141 if !c.ok() { 142 return syscall.EINVAL 143 } 144 if err := setLinger(c.fd, sec); err != nil { 145 return &OpError{Op: "set", Net: c.fd.net, Source: c.fd.laddr, Addr: c.fd.raddr, Err: err} 146 } 147 return nil 148 } 149 150 // SetKeepAlive sets whether the operating system should send 151 // keepalive messages on the connection. 152 func (c *TCPConn) SetKeepAlive(keepalive bool) error { 153 if !c.ok() { 154 return syscall.EINVAL 155 } 156 if err := setKeepAlive(c.fd, keepalive); err != nil { 157 return &OpError{Op: "set", Net: c.fd.net, Source: c.fd.laddr, Addr: c.fd.raddr, Err: err} 158 } 159 return nil 160 } 161 162 // SetKeepAlivePeriod sets period between keep alives. 163 func (c *TCPConn) SetKeepAlivePeriod(d time.Duration) error { 164 if !c.ok() { 165 return syscall.EINVAL 166 } 167 if err := setKeepAlivePeriod(c.fd, d); err != nil { 168 return &OpError{Op: "set", Net: c.fd.net, Source: c.fd.laddr, Addr: c.fd.raddr, Err: err} 169 } 170 return nil 171 } 172 173 // SetNoDelay controls whether the operating system should delay 174 // packet transmission in hopes of sending fewer packets (Nagle's 175 // algorithm). The default is true (no delay), meaning that data is 176 // sent as soon as possible after a Write. 177 func (c *TCPConn) SetNoDelay(noDelay bool) error { 178 if !c.ok() { 179 return syscall.EINVAL 180 } 181 if err := setNoDelay(c.fd, noDelay); err != nil { 182 return &OpError{Op: "set", Net: c.fd.net, Source: c.fd.laddr, Addr: c.fd.raddr, Err: err} 183 } 184 return nil 185 } 186 187 func newTCPConn(fd *netFD) *TCPConn { 188 c := &TCPConn{conn{fd}} 189 setNoDelay(c.fd, true) 190 return c 191 } 192 193 // DialTCP connects to the remote address raddr on the network net, 194 // which must be "tcp", "tcp4", or "tcp6". If laddr is not nil, it is 195 // used as the local address for the connection. 196 func DialTCP(net string, laddr, raddr *TCPAddr) (*TCPConn, error) { 197 switch net { 198 case "tcp", "tcp4", "tcp6": 199 default: 200 return nil, &OpError{Op: "dial", Net: net, Source: laddr.opAddr(), Addr: raddr.opAddr(), Err: UnknownNetworkError(net)} 201 } 202 if raddr == nil { 203 return nil, &OpError{Op: "dial", Net: net, Source: laddr.opAddr(), Addr: nil, Err: errMissingAddress} 204 } 205 c, err := dialTCP(context.Background(), net, laddr, raddr) 206 if err != nil { 207 return nil, &OpError{Op: "dial", Net: net, Source: laddr.opAddr(), Addr: raddr.opAddr(), Err: err} 208 } 209 return c, nil 210 } 211 212 // TCPListener is a TCP network listener. Clients should typically 213 // use variables of type Listener instead of assuming TCP. 214 type TCPListener struct { 215 fd *netFD 216 } 217 218 // AcceptTCP accepts the next incoming call and returns the new 219 // connection. 220 func (l *TCPListener) AcceptTCP() (*TCPConn, error) { 221 if !l.ok() { 222 return nil, syscall.EINVAL 223 } 224 c, err := l.accept() 225 if err != nil { 226 return nil, &OpError{Op: "accept", Net: l.fd.net, Source: nil, Addr: l.fd.laddr, Err: err} 227 } 228 return c, nil 229 } 230 231 // Accept implements the Accept method in the Listener interface; it 232 // waits for the next call and returns a generic Conn. 233 func (l *TCPListener) Accept() (Conn, error) { 234 if !l.ok() { 235 return nil, syscall.EINVAL 236 } 237 c, err := l.accept() 238 if err != nil { 239 return nil, &OpError{Op: "accept", Net: l.fd.net, Source: nil, Addr: l.fd.laddr, Err: err} 240 } 241 return c, nil 242 } 243 244 // Close stops listening on the TCP address. 245 // Already Accepted connections are not closed. 246 func (l *TCPListener) Close() error { 247 if !l.ok() { 248 return syscall.EINVAL 249 } 250 if err := l.close(); err != nil { 251 return &OpError{Op: "close", Net: l.fd.net, Source: nil, Addr: l.fd.laddr, Err: err} 252 } 253 return nil 254 } 255 256 // Addr returns the listener's network address, a *TCPAddr. 257 // The Addr returned is shared by all invocations of Addr, so 258 // do not modify it. 259 func (l *TCPListener) Addr() Addr { return l.fd.laddr } 260 261 // SetDeadline sets the deadline associated with the listener. 262 // A zero time value disables the deadline. 263 func (l *TCPListener) SetDeadline(t time.Time) error { 264 if !l.ok() { 265 return syscall.EINVAL 266 } 267 if err := l.fd.pfd.SetDeadline(t); err != nil { 268 return &OpError{Op: "set", Net: l.fd.net, Source: nil, Addr: l.fd.laddr, Err: err} 269 } 270 return nil 271 } 272 273 // File returns a copy of the underlying os.File, set to blocking 274 // mode. It is the caller's responsibility to close f when finished. 275 // Closing l does not affect f, and closing f does not affect l. 276 // 277 // The returned os.File's file descriptor is different from the 278 // connection's. Attempting to change properties of the original 279 // using this duplicate may or may not have the desired effect. 280 func (l *TCPListener) File() (f *os.File, err error) { 281 if !l.ok() { 282 return nil, syscall.EINVAL 283 } 284 f, err = l.file() 285 if err != nil { 286 return nil, &OpError{Op: "file", Net: l.fd.net, Source: nil, Addr: l.fd.laddr, Err: err} 287 } 288 return 289 } 290 291 // ListenTCP announces on the TCP address laddr and returns a TCP 292 // listener. Net must be "tcp", "tcp4", or "tcp6". If laddr has a 293 // port of 0, ListenTCP will choose an available port. The caller can 294 // use the Addr method of TCPListener to retrieve the chosen address. 295 func ListenTCP(net string, laddr *TCPAddr) (*TCPListener, error) { 296 switch net { 297 case "tcp", "tcp4", "tcp6": 298 default: 299 return nil, &OpError{Op: "listen", Net: net, Source: nil, Addr: laddr.opAddr(), Err: UnknownNetworkError(net)} 300 } 301 if laddr == nil { 302 laddr = &TCPAddr{} 303 } 304 ln, err := listenTCP(context.Background(), net, laddr) 305 if err != nil { 306 return nil, &OpError{Op: "listen", Net: net, Source: nil, Addr: laddr.opAddr(), Err: err} 307 } 308 return ln, nil 309 }