1.\" $NetBSD: tcp.4,v 1.8 2002/02/13 08:17:48 ross 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 . 136.Pp 137Options at the 138.Tn IP 139network level may be used with 140.Tn TCP ; 141see 142.Xr ip 4 143or 144.Xr ip6 4 . 145Incoming connection requests that are source-routed are noted, 146and the reverse source route is used in responding. 147.Sh DIAGNOSTICS 148A socket operation may fail with one of the following errors returned: 149.Bl -tag -width [EADDRNOTAVAIL] 150.It Bq Er EISCONN 151when trying to establish a connection on a socket which 152already has one; 153.It Bq Er ENOBUFS 154when the system runs out of memory for 155an internal data structure; 156.It Bq Er ETIMEDOUT 157when a connection was dropped 158due to excessive retransmissions; 159.It Bq Er ECONNRESET 160when the remote peer 161forces the connection to be closed; 162.It Bq Er ECONNREFUSED 163when the remote 164peer actively refuses connection establishment (usually because 165no process is listening to the port); 166.It Bq Er EADDRINUSE 167when an attempt 168is made to create a socket with a port which has already been 169allocated; 170.It Bq Er EADDRNOTAVAIL 171when an attempt is made to create a 172socket with a network address for which no network interface 173exists. 174.El 175.Sh SEE ALSO 176.Xr getsockopt 2 , 177.Xr socket 2 , 178.Xr inet 4 , 179.Xr inet6 4 , 180.Xr intro 4 , 181.Xr ip 4 , 182.Xr ip6 4 183.Rs 184.%R RFC 185.%N 793 186.%D September 1981 187.%T "Transmission Control Protocol" 188.Re 189.Rs 190.%R RFC 191.%N 1122 192.%D October 1989 193.%T "Requirements for Internet Hosts -- Communication Layers" 194.Re 195.Sh HISTORY 196The 197.Nm 198protocol stack appeared in 199.Bx 4.2 . 200