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