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