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