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