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.
17 // BUG(mikio): On JS and Windows, the File method of TCPConn and
18 // TCPListener is not implemented.
20 // TCPAddr represents the address of a TCP end point.
24 Zone string // IPv6 scoped addressing zone
27 // AddrPort returns the TCPAddr a as a netip.AddrPort.
29 // If a.Port does not fit in a uint16, it's silently truncated.
31 // If a is nil, a zero value is returned.
32 func (a *TCPAddr) AddrPort() netip.AddrPort {
34 return netip.AddrPort{}
36 na, _ := netip.AddrFromSlice(a.IP)
37 na = na.WithZone(a.Zone)
38 return netip.AddrPortFrom(na, uint16(a.Port))
41 // Network returns the address's network name, "tcp".
42 func (a *TCPAddr) Network() string { return "tcp" }
44 func (a *TCPAddr) String() string {
48 ip := ipEmptyString(a.IP)
50 return JoinHostPort(ip+"%"+a.Zone, itoa.Itoa(a.Port))
52 return JoinHostPort(ip, itoa.Itoa(a.Port))
55 func (a *TCPAddr) isWildcard() bool {
56 if a == nil || a.IP == nil {
59 return a.IP.IsUnspecified()
62 func (a *TCPAddr) opAddr() Addr {
69 // ResolveTCPAddr returns an address of TCP end point.
71 // The network must be a TCP network name.
73 // If the host in the address parameter is not a literal IP address or
74 // the port is not a literal port number, ResolveTCPAddr resolves the
75 // address to an address of TCP end point.
76 // Otherwise, it parses the address as a pair of literal IP address
78 // The address parameter can use a host name, but this is not
79 // recommended, because it will return at most one of the host name's
82 // See func Dial for a description of the network and address
84 func ResolveTCPAddr(network, address string) (*TCPAddr, error) {
86 case "tcp", "tcp4", "tcp6":
87 case "": // a hint wildcard for Go 1.0 undocumented behavior
90 return nil, UnknownNetworkError(network)
92 addrs, err := DefaultResolver.internetAddrList(context.Background(), network, address)
96 return addrs.forResolve(network, address).(*TCPAddr), nil
99 // TCPAddrFromAddrPort returns addr as a TCPAddr. If addr.IsValid() is false,
100 // then the returned TCPAddr will contain a nil IP field, indicating an
101 // address family-agnostic unspecified address.
102 func TCPAddrFromAddrPort(addr netip.AddrPort) *TCPAddr {
104 IP: addr.Addr().AsSlice(),
105 Zone: addr.Addr().Zone(),
106 Port: int(addr.Port()),
110 // TCPConn is an implementation of the Conn interface for TCP network
112 type TCPConn struct {
116 // SyscallConn returns a raw network connection.
117 // This implements the syscall.Conn interface.
118 func (c *TCPConn) SyscallConn() (syscall.RawConn, error) {
120 return nil, syscall.EINVAL
122 return newRawConn(c.fd), nil
125 // ReadFrom implements the io.ReaderFrom ReadFrom method.
126 func (c *TCPConn) ReadFrom(r io.Reader) (int64, error) {
128 return 0, syscall.EINVAL
130 n, err := c.readFrom(r)
131 if err != nil && err != io.EOF {
132 err = &OpError{Op: "readfrom", Net: c.fd.net, Source: c.fd.laddr, Addr: c.fd.raddr, Err: err}
137 // CloseRead shuts down the reading side of the TCP connection.
138 // Most callers should just use Close.
139 func (c *TCPConn) CloseRead() error {
141 return syscall.EINVAL
143 if err := c.fd.closeRead(); err != nil {
144 return &OpError{Op: "close", Net: c.fd.net, Source: c.fd.laddr, Addr: c.fd.raddr, Err: err}
149 // CloseWrite shuts down the writing side of the TCP connection.
150 // Most callers should just use Close.
151 func (c *TCPConn) CloseWrite() error {
153 return syscall.EINVAL
155 if err := c.fd.closeWrite(); err != nil {
156 return &OpError{Op: "close", Net: c.fd.net, Source: c.fd.laddr, Addr: c.fd.raddr, Err: err}
161 // SetLinger sets the behavior of Close on a connection which still
162 // has data waiting to be sent or to be acknowledged.
164 // If sec < 0 (the default), the operating system finishes sending the
165 // data in the background.
167 // If sec == 0, the operating system discards any unsent or
168 // unacknowledged data.
170 // If sec > 0, the data is sent in the background as with sec < 0.
171 // On some operating systems including Linux, this may cause Close to block
172 // until all data has been sent or discarded.
173 // On some operating systems after sec seconds have elapsed any remaining
174 // unsent data may be discarded.
175 func (c *TCPConn) SetLinger(sec int) error {
177 return syscall.EINVAL
179 if err := setLinger(c.fd, sec); err != nil {
180 return &OpError{Op: "set", Net: c.fd.net, Source: c.fd.laddr, Addr: c.fd.raddr, Err: err}
185 // SetKeepAlive sets whether the operating system should send
186 // keep-alive messages on the connection.
187 func (c *TCPConn) SetKeepAlive(keepalive bool) error {
189 return syscall.EINVAL
191 if err := setKeepAlive(c.fd, keepalive); err != nil {
192 return &OpError{Op: "set", Net: c.fd.net, Source: c.fd.laddr, Addr: c.fd.raddr, Err: err}
197 // SetKeepAlivePeriod sets period between keep-alives.
198 func (c *TCPConn) SetKeepAlivePeriod(d time.Duration) error {
200 return syscall.EINVAL
202 if err := setKeepAlivePeriod(c.fd, d); err != nil {
203 return &OpError{Op: "set", Net: c.fd.net, Source: c.fd.laddr, Addr: c.fd.raddr, Err: err}
208 // SetNoDelay controls whether the operating system should delay
209 // packet transmission in hopes of sending fewer packets (Nagle's
210 // algorithm). The default is true (no delay), meaning that data is
211 // sent as soon as possible after a Write.
212 func (c *TCPConn) SetNoDelay(noDelay bool) error {
214 return syscall.EINVAL
216 if err := setNoDelay(c.fd, noDelay); err != nil {
217 return &OpError{Op: "set", Net: c.fd.net, Source: c.fd.laddr, Addr: c.fd.raddr, Err: err}
222 // MultipathTCP reports whether the ongoing connection is using MPTCP.
224 // If Multipath TCP is not supported by the host, by the other peer or
225 // intentionally / accidentally filtered out by a device in between, a
226 // fallback to TCP will be done. This method does its best to check if
227 // MPTCP is still being used or not.
229 // On Linux, more conditions are verified on kernels >= v5.16, improving
231 func (c *TCPConn) MultipathTCP() (bool, error) {
233 return false, syscall.EINVAL
235 return isUsingMultipathTCP(c.fd), nil
238 func newTCPConn(fd *netFD, keepAlive time.Duration, keepAliveHook func(time.Duration)) *TCPConn {
241 keepAlive = defaultTCPKeepAlive
244 setKeepAlive(fd, true)
245 setKeepAlivePeriod(fd, keepAlive)
246 if keepAliveHook != nil {
247 keepAliveHook(keepAlive)
250 return &TCPConn{conn{fd}}
253 // DialTCP acts like Dial for TCP networks.
255 // The network must be a TCP network name; see func Dial for details.
257 // If laddr is nil, a local address is automatically chosen.
258 // If the IP field of raddr is nil or an unspecified IP address, the
259 // local system is assumed.
260 func DialTCP(network string, laddr, raddr *TCPAddr) (*TCPConn, error) {
262 case "tcp", "tcp4", "tcp6":
264 return nil, &OpError{Op: "dial", Net: network, Source: laddr.opAddr(), Addr: raddr.opAddr(), Err: UnknownNetworkError(network)}
267 return nil, &OpError{Op: "dial", Net: network, Source: laddr.opAddr(), Addr: nil, Err: errMissingAddress}
269 sd := &sysDialer{network: network, address: raddr.String()}
270 c, err := sd.dialTCP(context.Background(), laddr, raddr)
272 return nil, &OpError{Op: "dial", Net: network, Source: laddr.opAddr(), Addr: raddr.opAddr(), Err: err}
277 // TCPListener is a TCP network listener. Clients should typically
278 // use variables of type Listener instead of assuming TCP.
279 type TCPListener struct {
284 // SyscallConn returns a raw network connection.
285 // This implements the syscall.Conn interface.
287 // The returned RawConn only supports calling Control. Read and
288 // Write return an error.
289 func (l *TCPListener) SyscallConn() (syscall.RawConn, error) {
291 return nil, syscall.EINVAL
293 return newRawListener(l.fd), nil
296 // AcceptTCP accepts the next incoming call and returns the new
298 func (l *TCPListener) AcceptTCP() (*TCPConn, error) {
300 return nil, syscall.EINVAL
304 return nil, &OpError{Op: "accept", Net: l.fd.net, Source: nil, Addr: l.fd.laddr, Err: err}
309 // Accept implements the Accept method in the Listener interface; it
310 // waits for the next call and returns a generic Conn.
311 func (l *TCPListener) Accept() (Conn, error) {
313 return nil, syscall.EINVAL
317 return nil, &OpError{Op: "accept", Net: l.fd.net, Source: nil, Addr: l.fd.laddr, Err: err}
322 // Close stops listening on the TCP address.
323 // Already Accepted connections are not closed.
324 func (l *TCPListener) Close() error {
326 return syscall.EINVAL
328 if err := l.close(); err != nil {
329 return &OpError{Op: "close", Net: l.fd.net, Source: nil, Addr: l.fd.laddr, Err: err}
334 // Addr returns the listener's network address, a *TCPAddr.
335 // The Addr returned is shared by all invocations of Addr, so
337 func (l *TCPListener) Addr() Addr { return l.fd.laddr }
339 // SetDeadline sets the deadline associated with the listener.
340 // A zero time value disables the deadline.
341 func (l *TCPListener) SetDeadline(t time.Time) error {
343 return syscall.EINVAL
345 return l.fd.SetDeadline(t)
348 // File returns a copy of the underlying os.File.
349 // It is the caller's responsibility to close f when finished.
350 // Closing l does not affect f, and closing f does not affect l.
352 // The returned os.File's file descriptor is different from the
353 // connection's. Attempting to change properties of the original
354 // using this duplicate may or may not have the desired effect.
355 func (l *TCPListener) File() (f *os.File, err error) {
357 return nil, syscall.EINVAL
361 return nil, &OpError{Op: "file", Net: l.fd.net, Source: nil, Addr: l.fd.laddr, Err: err}
366 // ListenTCP acts like Listen for TCP networks.
368 // The network must be a TCP network name; see func Dial for details.
370 // If the IP field of laddr is nil or an unspecified IP address,
371 // ListenTCP listens on all available unicast and anycast IP addresses
372 // of the local system.
373 // If the Port field of laddr is 0, a port number is automatically
375 func ListenTCP(network string, laddr *TCPAddr) (*TCPListener, error) {
377 case "tcp", "tcp4", "tcp6":
379 return nil, &OpError{Op: "listen", Net: network, Source: nil, Addr: laddr.opAddr(), Err: UnknownNetworkError(network)}
384 sl := &sysListener{network: network, address: laddr.String()}
385 ln, err := sl.listenTCP(context.Background(), laddr)
387 return nil, &OpError{Op: "listen", Net: network, Source: nil, Addr: laddr.opAddr(), Err: err}
392 // roundDurationUp rounds d to the next multiple of to.
393 func roundDurationUp(d time.Duration, to time.Duration) time.Duration {
394 return (d + to - 1) / to