1.\" $NetBSD: tcp.4,v 1.5 1999/03/16 01:19:18 garbled 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 <sys/socket.h> 44.Fd #include <netinet/in.h> 45.Ft int 46.Fn socket AF_INET SOCK_STREAM 0 47.Sh DESCRIPTION 48The 49.Tn TCP 50provides reliable, flow-controlled, two-way transmission of data. 51It is a byte-stream protocol used to support the 52.Dv SOCK_STREAM 53abstraction. 54.Tn TCP 55uses the standard Internet address format and, in addition, provides 56a per-host collection of 57.Dq port addresses . 58Thus, each address is composed of an Internet address specifying 59the host and network, with a specific 60.Tn TCP 61port on the host identifying the peer entity. 62.Pp 63Sockets utilizing 64.Tn TCP 65are either 66.Dq active 67or 68.Dq passive . 69Active sockets initiate connections to passive 70sockets. 71By default 72.Tn TCP 73sockets are created active; to create a passive socket the 74.Xr listen 2 75system call must be used 76after binding the socket with the 77.Xr bind 2 78system call. 79Only passive sockets may use the 80.Xr accept 2 81call to accept incoming connections. 82Only active sockets may use the 83.Xr connect 2 84call to initiate connections. 85.Pp 86Passive sockets may 87.Dq underspecify 88their location to match incoming connection requests from multiple networks. 89This technique, termed 90.Dq wildcard addressing , 91allows a single 92server to provide service to clients on multiple networks. 93To create a socket which listens on all networks, the Internet 94address 95.Dv INADDR_ANY 96must be bound. 97The 98.Tn TCP 99port may still be specified at this time; if the port is not 100specified the system will assign one. 101Once a connection has been established the socket's address is 102fixed by the peer entity's location. 103The address assigned the socket is the address associated with the 104network interface through which packets are being transmitted and received. 105Normally this address corresponds to the peer entity's network. 106.Pp 107.Tn TCP 108supports one socket option which is set with 109.Xr setsockopt 2 110and tested with 111.Xr getsockopt 2 . 112Under most circumstances, 113.Tn TCP 114sends data when it is presented; 115when outstanding data has not yet been acknowledged, it gathers 116small amounts of output to be sent in a single packet once 117an acknowledgement is received. 118For a small number of clients, such as window systems 119that send a stream of mouse events which receive no replies, 120this packetization may cause significant delays. 121Therefore, 122.Tn TCP 123provides a boolean option, 124.Dv TCP_NODELAY 125(from 126.Aq Pa netinet/tcp.h , 127to defeat this algorithm. 128The option level for the 129.Xr setsockopt 2 130call is the protocol number for 131.Tn TCP , 132available from 133.Xr getprotobyname 3 . 134.Pp 135Options at the 136.Tn IP 137network level may be used with 138.Tn TCP ; 139see 140.Xr ip 4 . 141Incoming connection requests that are source-routed are noted, 142and the reverse source route is used in responding. 143.Sh DIAGNOSTICS 144A socket operation may fail with one of the following errors returned: 145.Bl -tag -width [EADDRNOTAVAIL] 146.It Bq Er EISCONN 147when trying to establish a connection on a socket which 148already has one; 149.It Bq Er ENOBUFS 150when the system runs out of memory for 151an internal data structure; 152.It Bq Er ETIMEDOUT 153when a connection was dropped 154due to excessive retransmissions; 155.It Bq Er ECONNRESET 156when the remote peer 157forces the connection to be closed; 158.It Bq Er ECONNREFUSED 159when the remote 160peer actively refuses connection establishment (usually because 161no process is listening to the port); 162.It Bq Er EADDRINUSE 163when an attempt 164is made to create a socket with a port which has already been 165allocated; 166.It Bq Er EADDRNOTAVAIL 167when an attempt is made to create a 168socket with a network address for which no network interface 169exists. 170.El 171.Sh SEE ALSO 172.Xr getsockopt 2 , 173.Xr socket 2 , 174.Xr intro 4 , 175.Xr inet 4 , 176.Xr ip 4 177.Rs 178.%R RFC 179.%N 793 180.%D September 1981 181.%T "Transmission Control Protocol" 182.Re 183.Rs 184.%R RFC 185.%N 1122 186.%D October 1989 187.%T "Requirements for Internet Hosts -- Communication Layers" 188.Re 189.Sh HISTORY 190The 191.Nm 192protocol stack appeared in 193.Bx 4.2 . 194