github.com/hikaru7719/go@v0.0.0-20181025140707-c8b2ac68906a/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 JS, NaCl and Windows, the File method of TCPConn and 16 // TCPListener is not 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 returns an address of TCP end point. 54 // 55 // The network must be a TCP network name. 56 // 57 // If the host in the address parameter is not a literal IP address or 58 // the port is not a literal port number, ResolveTCPAddr resolves the 59 // address to an address of TCP end point. 60 // Otherwise, it parses the address as a pair of literal IP address 61 // and port number. 62 // The address parameter can use a host name, but this is not 63 // recommended, because it will return at most one of the host name's 64 // IP addresses. 65 // 66 // See func Dial for a description of the network and address 67 // parameters. 68 func ResolveTCPAddr(network, address string) (*TCPAddr, error) { 69 switch network { 70 case "tcp", "tcp4", "tcp6": 71 case "": // a hint wildcard for Go 1.0 undocumented behavior 72 network = "tcp" 73 default: 74 return nil, UnknownNetworkError(network) 75 } 76 addrs, err := DefaultResolver.internetAddrList(context.Background(), network, address) 77 if err != nil { 78 return nil, err 79 } 80 return addrs.forResolve(network, address).(*TCPAddr), nil 81 } 82 83 // TCPConn is an implementation of the Conn interface for TCP network 84 // connections. 85 type TCPConn struct { 86 conn 87 } 88 89 // SyscallConn returns a raw network connection. 90 // This implements the syscall.Conn interface. 91 func (c *TCPConn) SyscallConn() (syscall.RawConn, error) { 92 if !c.ok() { 93 return nil, syscall.EINVAL 94 } 95 return newRawConn(c.fd) 96 } 97 98 // ReadFrom implements the io.ReaderFrom ReadFrom method. 99 func (c *TCPConn) ReadFrom(r io.Reader) (int64, error) { 100 if !c.ok() { 101 return 0, syscall.EINVAL 102 } 103 n, err := c.readFrom(r) 104 if err != nil && err != io.EOF { 105 err = &OpError{Op: "readfrom", Net: c.fd.net, Source: c.fd.laddr, Addr: c.fd.raddr, Err: err} 106 } 107 return n, err 108 } 109 110 // CloseRead shuts down the reading side of the TCP connection. 111 // Most callers should just use Close. 112 func (c *TCPConn) CloseRead() error { 113 if !c.ok() { 114 return syscall.EINVAL 115 } 116 if err := c.fd.closeRead(); err != nil { 117 return &OpError{Op: "close", Net: c.fd.net, Source: c.fd.laddr, Addr: c.fd.raddr, Err: err} 118 } 119 return nil 120 } 121 122 // CloseWrite shuts down the writing side of the TCP connection. 123 // Most callers should just use Close. 124 func (c *TCPConn) CloseWrite() error { 125 if !c.ok() { 126 return syscall.EINVAL 127 } 128 if err := c.fd.closeWrite(); err != nil { 129 return &OpError{Op: "close", Net: c.fd.net, Source: c.fd.laddr, Addr: c.fd.raddr, Err: err} 130 } 131 return nil 132 } 133 134 // SetLinger sets the behavior of Close on a connection which still 135 // has data waiting to be sent or to be acknowledged. 136 // 137 // If sec < 0 (the default), the operating system finishes sending the 138 // data in the background. 139 // 140 // If sec == 0, the operating system discards any unsent or 141 // unacknowledged data. 142 // 143 // If sec > 0, the data is sent in the background as with sec < 0. On 144 // some operating systems after sec seconds have elapsed any remaining 145 // unsent data may be discarded. 146 func (c *TCPConn) SetLinger(sec int) error { 147 if !c.ok() { 148 return syscall.EINVAL 149 } 150 if err := setLinger(c.fd, sec); err != nil { 151 return &OpError{Op: "set", Net: c.fd.net, Source: c.fd.laddr, Addr: c.fd.raddr, Err: err} 152 } 153 return nil 154 } 155 156 // SetKeepAlive sets whether the operating system should send 157 // keepalive messages on the connection. 158 func (c *TCPConn) SetKeepAlive(keepalive bool) error { 159 if !c.ok() { 160 return syscall.EINVAL 161 } 162 if err := setKeepAlive(c.fd, keepalive); err != nil { 163 return &OpError{Op: "set", Net: c.fd.net, Source: c.fd.laddr, Addr: c.fd.raddr, Err: err} 164 } 165 return nil 166 } 167 168 // SetKeepAlivePeriod sets period between keep alives. 169 func (c *TCPConn) SetKeepAlivePeriod(d time.Duration) error { 170 if !c.ok() { 171 return syscall.EINVAL 172 } 173 if err := setKeepAlivePeriod(c.fd, d); err != nil { 174 return &OpError{Op: "set", Net: c.fd.net, Source: c.fd.laddr, Addr: c.fd.raddr, Err: err} 175 } 176 return nil 177 } 178 179 // SetNoDelay controls whether the operating system should delay 180 // packet transmission in hopes of sending fewer packets (Nagle's 181 // algorithm). The default is true (no delay), meaning that data is 182 // sent as soon as possible after a Write. 183 func (c *TCPConn) SetNoDelay(noDelay bool) error { 184 if !c.ok() { 185 return syscall.EINVAL 186 } 187 if err := setNoDelay(c.fd, noDelay); err != nil { 188 return &OpError{Op: "set", Net: c.fd.net, Source: c.fd.laddr, Addr: c.fd.raddr, Err: err} 189 } 190 return nil 191 } 192 193 func newTCPConn(fd *netFD) *TCPConn { 194 c := &TCPConn{conn{fd}} 195 setNoDelay(c.fd, true) 196 return c 197 } 198 199 // DialTCP acts like Dial for TCP networks. 200 // 201 // The network must be a TCP network name; see func Dial for details. 202 // 203 // If laddr is nil, a local address is automatically chosen. 204 // If the IP field of raddr is nil or an unspecified IP address, the 205 // local system is assumed. 206 func DialTCP(network string, laddr, raddr *TCPAddr) (*TCPConn, error) { 207 switch network { 208 case "tcp", "tcp4", "tcp6": 209 default: 210 return nil, &OpError{Op: "dial", Net: network, Source: laddr.opAddr(), Addr: raddr.opAddr(), Err: UnknownNetworkError(network)} 211 } 212 if raddr == nil { 213 return nil, &OpError{Op: "dial", Net: network, Source: laddr.opAddr(), Addr: nil, Err: errMissingAddress} 214 } 215 sd := &sysDialer{network: network, address: raddr.String()} 216 c, err := sd.dialTCP(context.Background(), laddr, raddr) 217 if err != nil { 218 return nil, &OpError{Op: "dial", Net: network, Source: laddr.opAddr(), Addr: raddr.opAddr(), Err: err} 219 } 220 return c, nil 221 } 222 223 // TCPListener is a TCP network listener. Clients should typically 224 // use variables of type Listener instead of assuming TCP. 225 type TCPListener struct { 226 fd *netFD 227 } 228 229 // SyscallConn returns a raw network connection. 230 // This implements the syscall.Conn interface. 231 // 232 // The returned RawConn only supports calling Control. Read and 233 // Write return an error. 234 func (l *TCPListener) SyscallConn() (syscall.RawConn, error) { 235 if !l.ok() { 236 return nil, syscall.EINVAL 237 } 238 return newRawListener(l.fd) 239 } 240 241 // AcceptTCP accepts the next incoming call and returns the new 242 // connection. 243 func (l *TCPListener) AcceptTCP() (*TCPConn, error) { 244 if !l.ok() { 245 return nil, syscall.EINVAL 246 } 247 c, err := l.accept() 248 if err != nil { 249 return nil, &OpError{Op: "accept", Net: l.fd.net, Source: nil, Addr: l.fd.laddr, Err: err} 250 } 251 return c, nil 252 } 253 254 // Accept implements the Accept method in the Listener interface; it 255 // waits for the next call and returns a generic Conn. 256 func (l *TCPListener) Accept() (Conn, error) { 257 if !l.ok() { 258 return nil, syscall.EINVAL 259 } 260 c, err := l.accept() 261 if err != nil { 262 return nil, &OpError{Op: "accept", Net: l.fd.net, Source: nil, Addr: l.fd.laddr, Err: err} 263 } 264 return c, nil 265 } 266 267 // Close stops listening on the TCP address. 268 // Already Accepted connections are not closed. 269 func (l *TCPListener) Close() error { 270 if !l.ok() { 271 return syscall.EINVAL 272 } 273 if err := l.close(); err != nil { 274 return &OpError{Op: "close", Net: l.fd.net, Source: nil, Addr: l.fd.laddr, Err: err} 275 } 276 return nil 277 } 278 279 // Addr returns the listener's network address, a *TCPAddr. 280 // The Addr returned is shared by all invocations of Addr, so 281 // do not modify it. 282 func (l *TCPListener) Addr() Addr { return l.fd.laddr } 283 284 // SetDeadline sets the deadline associated with the listener. 285 // A zero time value disables the deadline. 286 func (l *TCPListener) SetDeadline(t time.Time) error { 287 if !l.ok() { 288 return syscall.EINVAL 289 } 290 if err := l.fd.pfd.SetDeadline(t); err != nil { 291 return &OpError{Op: "set", Net: l.fd.net, Source: nil, Addr: l.fd.laddr, Err: err} 292 } 293 return nil 294 } 295 296 // File returns a copy of the underlying os.File. 297 // It is the caller's responsibility to close f when finished. 298 // Closing l does not affect f, and closing f does not affect l. 299 // 300 // The returned os.File's file descriptor is different from the 301 // connection's. Attempting to change properties of the original 302 // using this duplicate may or may not have the desired effect. 303 func (l *TCPListener) File() (f *os.File, err error) { 304 if !l.ok() { 305 return nil, syscall.EINVAL 306 } 307 f, err = l.file() 308 if err != nil { 309 return nil, &OpError{Op: "file", Net: l.fd.net, Source: nil, Addr: l.fd.laddr, Err: err} 310 } 311 return 312 } 313 314 // ListenTCP acts like Listen for TCP networks. 315 // 316 // The network must be a TCP network name; see func Dial for details. 317 // 318 // If the IP field of laddr is nil or an unspecified IP address, 319 // ListenTCP listens on all available unicast and anycast IP addresses 320 // of the local system. 321 // If the Port field of laddr is 0, a port number is automatically 322 // chosen. 323 func ListenTCP(network string, laddr *TCPAddr) (*TCPListener, error) { 324 switch network { 325 case "tcp", "tcp4", "tcp6": 326 default: 327 return nil, &OpError{Op: "listen", Net: network, Source: nil, Addr: laddr.opAddr(), Err: UnknownNetworkError(network)} 328 } 329 if laddr == nil { 330 laddr = &TCPAddr{} 331 } 332 sl := &sysListener{network: network, address: laddr.String()} 333 ln, err := sl.listenTCP(context.Background(), laddr) 334 if err != nil { 335 return nil, &OpError{Op: "listen", Net: network, Source: nil, Addr: laddr.opAddr(), Err: err} 336 } 337 return ln, nil 338 }