1.\" $NetBSD: tcp.4,v 1.10 2002/07/18 03:22:11 wrstuden Exp $ 2.\" 3.\" Copyright (c) 1983, 1991, 1993 4.\" The Regents of the University of California. All rights reserved. 5.\" 6.\" Redistribution and use in source and binary forms, with or without 7.\" modification, are permitted provided that the following conditions 8.\" are met: 9.\" 1. Redistributions of source code must retain the above copyright 10.\" notice, this list of conditions and the following disclaimer. 11.\" 2. Redistributions in binary form must reproduce the above copyright 12.\" notice, this list of conditions and the following disclaimer in the 13.\" documentation and/or other materials provided with the distribution. 14.\" 3. All advertising materials mentioning features or use of this software 15.\" must display the following acknowledgement: 16.\" This product includes software developed by the University of 17.\" California, Berkeley and its contributors. 18.\" 4. Neither the name of the University nor the names of its contributors 19.\" may be used to endorse or promote products derived from this software 20.\" without specific prior written permission. 21.\" 22.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32.\" SUCH DAMAGE. 33.\" 34.\" @(#)tcp.4 8.1 (Berkeley) 6/5/93 35.\" 36.Dd June 5, 1993 37.Dt TCP 4 38.Os 39.Sh NAME 40.Nm tcp 41.Nd Internet Transmission Control Protocol 42.Sh SYNOPSIS 43.Fd #include \*[Lt]sys/socket.h\*[Gt] 44.Fd #include \*[Lt]netinet/in.h\*[Gt] 45.Ft int 46.Fn socket AF_INET SOCK_STREAM 0 47.Ft int 48.Fn socket AF_INET6 SOCK_STREAM 0 49.Sh DESCRIPTION 50The 51.Tn TCP 52provides reliable, flow-controlled, two-way transmission of data. 53It is a byte-stream protocol used to support the 54.Dv SOCK_STREAM 55abstraction. 56.Tn TCP 57uses the standard Internet address format and, in addition, provides 58a per-host collection of 59.Dq port addresses . 60Thus, each address is composed of an Internet address specifying 61the host and network, with a specific 62.Tn TCP 63port on the host identifying the peer entity. 64.Pp 65Sockets utilizing 66.Tn TCP 67are either 68.Dq active 69or 70.Dq passive . 71Active sockets initiate connections to passive 72sockets. 73By default 74.Tn TCP 75sockets are created active; to create a passive socket the 76.Xr listen 2 77system call must be used 78after binding the socket with the 79.Xr bind 2 80system call. 81Only passive sockets may use the 82.Xr accept 2 83call to accept incoming connections. 84Only active sockets may use the 85.Xr connect 2 86call to initiate connections. 87.Pp 88Passive sockets may 89.Dq underspecify 90their location to match incoming connection requests from multiple networks. 91This technique, termed 92.Dq wildcard addressing , 93allows a single 94server to provide service to clients on multiple networks. 95To create a socket which listens on all networks, the Internet 96address 97.Dv INADDR_ANY 98must be bound. 99The 100.Tn TCP 101port may still be specified at this time; if the port is not 102specified the system will assign one. 103Once a connection has been established the socket's address is 104fixed by the peer entity's location. 105The address assigned the socket is the address associated with the 106network interface through which packets are being transmitted and received. 107Normally this address corresponds to the peer entity's network. 108.Pp 109.Tn TCP 110supports one socket option which is set with 111.Xr setsockopt 2 112and tested with 113.Xr getsockopt 2 . 114Under most circumstances, 115.Tn TCP 116sends data when it is presented; 117when outstanding data has not yet been acknowledged, it gathers 118small amounts of output to be sent in a single packet once 119an acknowledgement is received. 120For a small number of clients, such as window systems 121that send a stream of mouse events which receive no replies, 122this packetization may cause significant delays. 123Therefore, 124.Tn TCP 125provides a boolean option, 126.Dv TCP_NODELAY 127(from 128.Aq Pa netinet/tcp.h , 129to defeat this algorithm. 130The option level for the 131.Xr setsockopt 2 132call is the protocol number for 133.Tn TCP , 134available from 135.Xr getprotobyname 3 . 136In the historical 137.Bx 138.Tn TCP 139implementation, if the 140.Dv TCP_NODELAY 141option was set on a passive socket, the sockets returned by 142.Xr accept 2 143erroneously did not have the 144.Dv TCP_NODELAY 145option set; the behavior was corrected to inherit 146.Dv TCP_NODELAY 147in 148.Nx 1.6 . 149.Pp 150Options at the 151.Tn IP 152network level may be used with 153.Tn TCP ; 154see 155.Xr ip 4 156or 157.Xr ip6 4 . 158Incoming connection requests that are source-routed are noted, 159and the reverse source route is used in responding. 160.Sh DIAGNOSTICS 161A socket operation may fail with one of the following errors returned: 162.Bl -tag -width [EADDRNOTAVAIL] 163.It Bq Er EISCONN 164when trying to establish a connection on a socket which 165already has one; 166.It Bq Er ENOBUFS 167when the system runs out of memory for 168an internal data structure; 169.It Bq Er ETIMEDOUT 170when a connection was dropped 171due to excessive retransmissions; 172.It Bq Er ECONNRESET 173when the remote peer 174forces the connection to be closed; 175.It Bq Er ECONNREFUSED 176when the remote 177peer actively refuses connection establishment (usually because 178no process is listening to the port); 179.It Bq Er EADDRINUSE 180when an attempt 181is made to create a socket with a port which has already been 182allocated; 183.It Bq Er EADDRNOTAVAIL 184when an attempt is made to create a 185socket with a network address for which no network interface 186exists. 187.El 188.Sh SEE ALSO 189.Xr getsockopt 2 , 190.Xr socket 2 , 191.Xr inet 4 , 192.Xr inet6 4 , 193.Xr intro 4 , 194.Xr ip 4 , 195.Xr ip6 4 196.Rs 197.%R RFC 198.%N 793 199.%D September 1981 200.%T "Transmission Control Protocol" 201.Re 202.Rs 203.%R RFC 204.%N 1122 205.%D October 1989 206.%T "Requirements for Internet Hosts -- Communication Layers" 207.Re 208.Sh HISTORY 209The 210.Nm 211protocol stack appeared in 212.Bx 4.2 . 213