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