xref: /illumos-gate/usr/src/man/man4p/tcp.4p (revision 11994f6f)
1f5037cd0SCody Peter Mello'\"
2f5037cd0SCody Peter Mello.\" This file and its contents are supplied under the terms of the
3f5037cd0SCody Peter Mello.\" Common Development and Distribution License ("CDDL"), version 1.0.
4f5037cd0SCody Peter Mello.\" You may only use this file in accordance with the terms of version
5f5037cd0SCody Peter Mello.\" 1.0 of the CDDL.
6f5037cd0SCody Peter Mello.\"
7f5037cd0SCody Peter Mello.\" A full copy of the text of the CDDL should have accompanied this
8f5037cd0SCody Peter Mello.\" source.  A copy of the CDDL is also available via the Internet at
9f5037cd0SCody Peter Mello.\" http://www.illumos.org/license/CDDL.
10f5037cd0SCody Peter Mello.\"
11f5037cd0SCody Peter Mello.\"
12c10c16deSRichard Lowe.\" Copyright (c) 2006, Sun Microsystems, Inc. All Rights Reserved.
133d0a255cSGarrett D'Amore.\" Copyright (c) 2011 Nexenta Systems, Inc. All rights reserved.
14f5037cd0SCody Peter Mello.\" Copyright 2019 Joyent, Inc.
15861fa149SNils Nieuwejaar.\" Copyright 2022 Oxide Computer Company
16c10c16deSRichard Lowe.\" Copyright 1989 AT&T
17f5037cd0SCody Peter Mello.\"
18*11994f6fSRobert Mustacchi.Dd December 2, 2023
19bbf21555SRichard Lowe.Dt TCP 4P
20f5037cd0SCody Peter Mello.Os
21f5037cd0SCody Peter Mello.Sh NAME
22f5037cd0SCody Peter Mello.Nm tcp ,
23f5037cd0SCody Peter Mello.Nm TCP
24f5037cd0SCody Peter Mello.Nd Internet Transmission Control Protocol
25f5037cd0SCody Peter Mello.Sh SYNOPSIS
26f5037cd0SCody Peter Mello.In sys/socket.h
27f5037cd0SCody Peter Mello.In netinet/in.h
28f5037cd0SCody Peter Mello.In netinet/tcp.h
29f5037cd0SCody Peter Mello.Bd -literal
30f5037cd0SCody Peter Mellos = socket(AF_INET, SOCK_STREAM, 0);
31f5037cd0SCody Peter Mellos = socket(AF_INET6, SOCK_STREAM, 0);
32f5037cd0SCody Peter Mellot = t_open("/dev/tcp", O_RDWR);
33f5037cd0SCody Peter Mellot = t_open("/dev/tcp6", O_RDWR);
34f5037cd0SCody Peter Mello.Ed
35f5037cd0SCody Peter Mello.Sh DESCRIPTION
36f5037cd0SCody Peter MelloTCP is the virtual circuit protocol of the Internet protocol family.
37f5037cd0SCody Peter MelloIt provides reliable, flow-controlled, in-order, two-way transmission of data.
38f5037cd0SCody Peter MelloIt is a byte-stream protocol layered above the Internet Protocol
39f5037cd0SCody Peter Mello.Po Sy IP Pc ,
40f5037cd0SCody Peter Melloor the Internet Protocol Version 6
41f5037cd0SCody Peter Mello.Po Sy IPv6 Pc ,
42f5037cd0SCody Peter Mellothe Internet protocol family's
43c10c16deSRichard Loweinternetwork datagram delivery protocol.
44f5037cd0SCody Peter Mello.Pp
45f5037cd0SCody Peter MelloPrograms can access TCP using the socket interface as a
46f5037cd0SCody Peter Mello.Dv SOCK_STREAM
47f5037cd0SCody Peter Mellosocket type, or using the Transport Level Interface
48f5037cd0SCody Peter Mello.Po Sy TLI Pc
49f5037cd0SCody Peter Mellowhere it supports the connection-oriented
50f5037cd0SCody Peter Mello.Po Dv BT_COTS_ORD Pc
51f5037cd0SCody Peter Melloservice type.
52f5037cd0SCody Peter Mello.Pp
53f5037cd0SCody Peter MelloA checksum over all data helps TCP provide reliable communication.
54f5037cd0SCody Peter MelloUsing a window-based flow control mechanism that makes use of positive
55f5037cd0SCody Peter Melloacknowledgements, sequence numbers, and a retransmission strategy, TCP can
56f5037cd0SCody Peter Mellousually recover when datagrams are damaged, delayed, duplicated or delivered
57f5037cd0SCody Peter Melloout of order by the underlying medium.
58f5037cd0SCody Peter Mello.Pp
59f5037cd0SCody Peter MelloTCP provides several socket options, defined in
60f5037cd0SCody Peter Mello.In netinet/tcp.h
61f5037cd0SCody Peter Melloand described throughout this document,
62f5037cd0SCody Peter Mellowhich may be set using
63f5037cd0SCody Peter Mello.Xr setsockopt 3SOCKET
64f5037cd0SCody Peter Melloand read using
65f5037cd0SCody Peter Mello.Xr getsockopt 3SOCKET .
66f5037cd0SCody Peter MelloThe
67f5037cd0SCody Peter Mello.Fa level
68f5037cd0SCody Peter Melloargument for these calls is the protocol number for TCP, available from
69f5037cd0SCody Peter Mello.Xr getprotobyname 3SOCKET .
70f5037cd0SCody Peter MelloIP level options may also be used with TCP.
71f5037cd0SCody Peter MelloSee
72bbf21555SRichard Lowe.Xr ip 4P
73f5037cd0SCody Peter Melloand
74bbf21555SRichard Lowe.Xr ip6 4P .
75f5037cd0SCody Peter Mello.Ss "Listening And Connecting"
76f5037cd0SCody Peter MelloTCP uses IP's host-level addressing and adds its own per-host
77f5037cd0SCody Peter Mellocollection of
78f5037cd0SCody Peter Mello.Dq port addresses .
79f5037cd0SCody Peter MelloThe endpoints of a TCP connection are
80f5037cd0SCody Peter Melloidentified by the combination of an IPv4 or IPv6 address and a TCP
81f5037cd0SCody Peter Melloport number.
82f5037cd0SCody Peter MelloAlthough other protocols, such as the User Datagram Protocol
83f5037cd0SCody Peter Mello.Po Sy UDP Pc ,
84f5037cd0SCody Peter Mellomay use the same host and port address format, the port space of these
85f5037cd0SCody Peter Melloprotocols is distinct.
86f5037cd0SCody Peter MelloSee
87bbf21555SRichard Lowe.Xr inet 4P
88f5037cd0SCody Peter Melloand
89bbf21555SRichard Lowe.Xr inet6 4P
90f5037cd0SCody Peter Mellofor details on
91c10c16deSRichard Lowethe common aspects of addressing in the Internet protocol family.
92f5037cd0SCody Peter Mello.Pp
93f5037cd0SCody Peter MelloSockets utilizing TCP are either
94f5037cd0SCody Peter Mello.Dq active
95f5037cd0SCody Peter Melloor
96f5037cd0SCody Peter Mello.Dq passive .
97f5037cd0SCody Peter MelloActive sockets
98f5037cd0SCody Peter Melloinitiate connections to passive sockets.
99f5037cd0SCody Peter MelloPassive sockets must have their local IPv4 or IPv6 address and TCP port number
100f5037cd0SCody Peter Mellobound with the
101f5037cd0SCody Peter Mello.Xr bind 3SOCKET
102f5037cd0SCody Peter Mellosystem call after the socket is created.
103f5037cd0SCody Peter MelloIf an active socket has not been bound by the time
104f5037cd0SCody Peter Mello.Xr connect 3SOCKET
105f5037cd0SCody Peter Mellois called, then the operating system will choose a local address and port for
106f5037cd0SCody Peter Mellothe application.
107f5037cd0SCody Peter MelloBy default, TCP sockets are active.
108f5037cd0SCody Peter MelloA passive socket is created by calling the
109f5037cd0SCody Peter Mello.Xr listen 3SOCKET
110f5037cd0SCody Peter Mellosystem call after binding, which establishes a queueing parameter for the
111f5037cd0SCody Peter Mellopassive socket.
112f5037cd0SCody Peter MelloConnections to the passive socket can then be received using the
113f5037cd0SCody Peter Mello.Xr accept 3SOCKET
114f5037cd0SCody Peter Mellosystem call.
115f5037cd0SCody Peter MelloActive sockets use the
116f5037cd0SCody Peter Mello.Xr connect 3SOCKET
117f5037cd0SCody Peter Mellocall after binding to initiate connections.
118f5037cd0SCody Peter Mello.Pp
119f5037cd0SCody Peter MelloIf incoming connection requests include an IP source route option, then the
120f5037cd0SCody Peter Melloreverse source route will be used when responding.
121f5037cd0SCody Peter Mello.Pp
122f5037cd0SCody Peter MelloBy using the special value
123f5037cd0SCody Peter Mello.Dv INADDR_ANY
124f5037cd0SCody Peter Mellowith IPv4, or the unspecified
125f5037cd0SCody Peter Melloaddress (all zeroes) with IPv6, the local IP address can be left
126f5037cd0SCody Peter Mellounspecified in the
127f5037cd0SCody Peter Mello.Fn bind
128f5037cd0SCody Peter Mellocall by either active or passive TCP
129f5037cd0SCody Peter Mellosockets.
130f5037cd0SCody Peter MelloThis feature is usually used if the local address is either unknown or
131f5037cd0SCody Peter Melloirrelevant.
132f5037cd0SCody Peter MelloIf left unspecified, the local IP address will be bound at connection time to
133f5037cd0SCody Peter Mellothe address of the network interface used to service the connection.
134f5037cd0SCody Peter MelloFor passive sockets, this is the destination address used by the connecting
135f5037cd0SCody Peter Mellopeer.
136f5037cd0SCody Peter MelloFor active sockets, this is usually an address on the same subnet as the
137f5037cd0SCody Peter Mellodestination or default gateway address, although the rules can be more complex.
138f5037cd0SCody Peter MelloSee
139f5037cd0SCody Peter Mello.Sy "Source Address Selection"
140f5037cd0SCody Peter Melloin
141bbf21555SRichard Lowe.Xr inet6 4P
142f5037cd0SCody Peter Mellofor a detailed discussion of how this works in IPv6.
143f5037cd0SCody Peter Mello.Pp
144c10c16deSRichard LoweNote that no two TCP sockets can be bound to the same port unless the bound IP
145f5037cd0SCody Peter Melloaddresses are different.
146f5037cd0SCody Peter MelloIPv4
147f5037cd0SCody Peter Mello.Dv INADDR_ANY
148f5037cd0SCody Peter Melloand IPv6 unspecified addresses compare as equal to any IPv4 or IPv6 address.
149f5037cd0SCody Peter MelloFor example, if a socket is bound to
150f5037cd0SCody Peter Mello.Dv INADDR_ANY
151f5037cd0SCody Peter Melloor the unspecified address and port
152f5037cd0SCody Peter Mello.Em N ,
153f5037cd0SCody Peter Mellono other socket can bind to port
154f5037cd0SCody Peter Mello.Em N ,
155f5037cd0SCody Peter Melloregardless of the binding address.
156f5037cd0SCody Peter MelloThis special consideration of
157f5037cd0SCody Peter Mello.Dv INADDR_ANY
158f5037cd0SCody Peter Melloand the unspecified address can be changed using the socket option
159f5037cd0SCody Peter Mello.Dv SO_REUSEADDR .
160f5037cd0SCody Peter MelloIf
161f5037cd0SCody Peter Mello.Dv SO_REUSEADDR
162f5037cd0SCody Peter Mellois set on a socket doing a bind, IPv4
163f5037cd0SCody Peter Mello.Dv INADDR_ANY
164f5037cd0SCody Peter Melloand the IPv6 unspecified address do not compare as equal to any IP address.
165f5037cd0SCody Peter MelloThis means that as long as the two sockets are not both bound to
166f5037cd0SCody Peter Mello.Dv INADDR_ANY ,
167f5037cd0SCody Peter Mellothe unspecified address, or the same IP address, then the two sockets can be
168f5037cd0SCody Peter Mellobound to the same port.
169f5037cd0SCody Peter Mello.Pp
170f5037cd0SCody Peter MelloIf an application does not want to allow another socket using the
171f5037cd0SCody Peter Mello.Dv SO_REUSEADDR
172f5037cd0SCody Peter Mellooption to bind to a port its socket is bound to, the
173f5037cd0SCody Peter Melloapplication can set the socket-level
174f5037cd0SCody Peter Mello.Po Dv SOL_SOCKET Pc
175f5037cd0SCody Peter Mellooption
176f5037cd0SCody Peter Mello.Dv SO_EXCLBIND
177f5037cd0SCody Peter Melloon a socket.
178f5037cd0SCody Peter MelloThe
179c10c16deSRichard Loweoption values of 0 and 1 mean enabling and disabling the option respectively.
180c10c16deSRichard LoweOnce this option is enabled on a socket, no other socket can be bound to the
181c10c16deSRichard Lowesame port.
182f5037cd0SCody Peter Mello.Ss "Sending And Receiving Data"
183c10c16deSRichard LoweOnce a connection has been established, data can be exchanged using the
184f5037cd0SCody Peter Mello.Xr read 2
185f5037cd0SCody Peter Melloand
186f5037cd0SCody Peter Mello.Xr write 2
187f5037cd0SCody Peter Mellosystem calls.
188f5037cd0SCody Peter MelloIf, after sending data, the local TCP receives no acknowledgements from its
189f5037cd0SCody Peter Mellopeer for a period of time (for example, if the remote machine crashes), the
190f5037cd0SCody Peter Melloconnection is closed and an error is returned.
191f5037cd0SCody Peter Mello.Pp
192f5037cd0SCody Peter MelloWhen a peer is sending data, it will only send up to the advertised
193f5037cd0SCody Peter Mello.Dq receive window ,
194f5037cd0SCody Peter Mellowhich is determined by how much more data the recipient can fit in its buffer.
195f5037cd0SCody Peter MelloApplications can use the socket-level option
196f5037cd0SCody Peter Mello.Dv SO_RCVBUF
197f5037cd0SCody Peter Melloto increase or decrease the receive buffer size.
198f5037cd0SCody Peter MelloSimilarly, the socket-level option
199f5037cd0SCody Peter Mello.Dv SO_SNDBUF
200f5037cd0SCody Peter Mellocan be used to allow TCP to buffer more unacknowledged and unsent data locally.
201f5037cd0SCody Peter Mello.Pp
202f5037cd0SCody Peter MelloUnder most circumstances, TCP will send data when it is written by the
203f5037cd0SCody Peter Melloapplication.
204f5037cd0SCody Peter MelloWhen outstanding data has not yet been acknowledged, though, TCP will gather
205f5037cd0SCody Peter Mellosmall amounts of output to be sent as a single packet once an acknowledgement
206f5037cd0SCody Peter Mellohas been received.
207f5037cd0SCody Peter MelloUsually referred to as Nagle's Algorithm (RFC 896), this behavior helps prevent
208f5037cd0SCody Peter Melloflooding the network with many small packets.
209f5037cd0SCody Peter Mello.Pp
210f5037cd0SCody Peter MelloHowever, for some highly interactive clients (such as remote shells or
211f5037cd0SCody Peter Mellowindowing systems that send a stream of keypresses or mouse events), this
212f5037cd0SCody Peter Mellobatching may cause significant delays.
213f5037cd0SCody Peter MelloTo disable this behavior, TCP provides a boolean socket option,
214f5037cd0SCody Peter Mello.Dv TCP_NODELAY .
215f5037cd0SCody Peter Mello.Pp
216f5037cd0SCody Peter MelloConversely, for other applications, it may be desirable for TCP not to send out
217f5037cd0SCody Peter Melloany data until a full TCP segment can be sent.
218f5037cd0SCody Peter MelloTo enable this behavior, an application can use the TCP-level socket option
219f5037cd0SCody Peter Mello.Dv TCP_CORK .
220f5037cd0SCody Peter MelloWhen set to a non-zero value, TCP will only send out a full TCP segment.
221f5037cd0SCody Peter MelloWhen
222f5037cd0SCody Peter Mello.Dv TCP_CORK
223f5037cd0SCody Peter Mellois set to zero after it has been enabled, all currently buffered data is sent
224f5037cd0SCody Peter Melloout (as permitted by the peer's receive window and the current congestion
225f5037cd0SCody Peter Mellowindow).
226f5037cd0SCody Peter Mello.Pp
227861fa149SNils NieuwejaarStill other latency-sensitive applications rely on receiving a quick
228861fa149SNils Nieuwejaarnotification that their packets have been successfully received.
229861fa149SNils NieuwejaarTo satisfy the requirements of those applications, setting the
230861fa149SNils Nieuwejaar.Dv TCP_QUICKACK
231861fa149SNils Nieuwejaaroption to a non-zero value will instruct the TCP stack to send an acknowlegment
232861fa149SNils Nieuwejaarimmediately upon receipt of a packet, rather than waiting to acknowledge
233861fa149SNils Nieuwejaarmultiple packets at once.
234861fa149SNils Nieuwejaar.Pp
235f5037cd0SCody Peter MelloTCP provides an urgent data mechanism, which may be invoked using the
236f5037cd0SCody Peter Melloout-of-band provisions of
237f5037cd0SCody Peter Mello.Xr send 3SOCKET .
238f5037cd0SCody Peter MelloThe caller may mark one byte as
239f5037cd0SCody Peter Mello.Dq urgent
240f5037cd0SCody Peter Mellowith the
241f5037cd0SCody Peter Mello.Dv MSG_OOB
242f5037cd0SCody Peter Melloflag to
243f5037cd0SCody Peter Mello.Xr send 3SOCKET .
244f5037cd0SCody Peter MelloThis sets an
245f5037cd0SCody Peter Mello.Dq urgent pointer
246f5037cd0SCody Peter Mellopointing to this byte in the TCP stream.
247f5037cd0SCody Peter MelloThe receiver on the other side of the stream is notified of the urgent data by a
248f5037cd0SCody Peter Mello.Dv SIGURG
249f5037cd0SCody Peter Mellosignal.
250f5037cd0SCody Peter MelloThe
251f5037cd0SCody Peter Mello.Dv SIOCATMARK
252f5037cd0SCody Peter Mello.Xr ioctl 2
253f5037cd0SCody Peter Mellorequest returns a value indicating whether the stream is at the urgent mark.
254f5037cd0SCody Peter MelloBecause the system never returns data across the urgent mark in a single
255f5037cd0SCody Peter Mello.Xr read 2
256f5037cd0SCody Peter Mellocall, it is possible to
257c10c16deSRichard Loweadvance to the urgent data in a simple loop which reads data, testing the
258f5037cd0SCody Peter Mellosocket with the
259f5037cd0SCody Peter Mello.Dv SIOCATMARK
260f5037cd0SCody Peter Mello.Fn ioctl
261f5037cd0SCody Peter Mellorequest, until it reaches the mark.
262f5037cd0SCody Peter Mello.Ss "Congestion Control"
263f5037cd0SCody Peter MelloTCP follows the congestion control algorithm described in RFC 2581, and
264f5037cd0SCody Peter Melloalso supports the initial congestion window (cwnd) changes in RFC 3390.
265c10c16deSRichard LoweThe initial cwnd calculation can be overridden by the socket option
266f5037cd0SCody Peter Mello.Dv TCP_INIT_CWND .
267f5037cd0SCody Peter MelloAn application can use this option to set the initial cwnd to a
268f5037cd0SCody Peter Mellospecified number of TCP segments.
269f5037cd0SCody Peter MelloThis applies to the cases when the connection
270f5037cd0SCody Peter Mellofirst starts and restarts after an idle period.
271f5037cd0SCody Peter MelloThe process must have the
272f5037cd0SCody Peter Mello.Dv PRIV_SYS_NET_CONFIG
273f5037cd0SCody Peter Melloprivilege if it wants to specify a number greater than that
274f5037cd0SCody Peter Mellocalculated by RFC 3390.
275d4994511SCody Peter Mello.Pp
276d4994511SCody Peter MelloThe operating system also provides alternative algorithms that may be more
277d4994511SCody Peter Melloappropriate for your application, including the CUBIC congestion control
278d4994511SCody Peter Melloalgorithm described in RFC 8312.
279d4994511SCody Peter MelloThese can be configured system-wide using
280bbf21555SRichard Lowe.Xr ipadm 8 ,
281d4994511SCody Peter Melloor on a per-connection basis with the TCP-level socket option
282d4994511SCody Peter Mello.Dv TCP_CONGESTION ,
283d4994511SCody Peter Mellowhose argument is the name of the algorithm to use
284d4994511SCody Peter Mello.Pq for example Dq cubic .
285d4994511SCody Peter MelloIf the requested algorithm does not exist, then
286d4994511SCody Peter Mello.Fn setsockopt
287d4994511SCody Peter Mellowill fail, and
288d4994511SCody Peter Mello.Va errno
289d4994511SCody Peter Mellowill be set to
290d4994511SCody Peter Mello.Er ENOENT .
291f5037cd0SCody Peter Mello.Ss "TCP Keep-Alive"
292f5037cd0SCody Peter MelloSince TCP determines whether a remote peer is no longer reachable by timing out
293f5037cd0SCody Peter Mellowaiting for acknowledgements, a host that never sends any new data may never
294f5037cd0SCody Peter Mellonotice a peer that has gone away.
295f5037cd0SCody Peter MelloWhile consumers can avoid this problem by sending their own periodic heartbeat
296f5037cd0SCody Peter Mellomessages (Transport Layer Security does this, for example),
297f5037cd0SCody Peter MelloTCP describes an optional keep-alive mechanism in RFC 1122.
298f5037cd0SCody Peter MelloApplications can enable it using the socket-level option
299f5037cd0SCody Peter Mello.Dv SO_KEEPALIVE .
300f5037cd0SCody Peter MelloWhen enabled, the first keep-alive probe is sent out after a TCP connection is
301f5037cd0SCody Peter Melloidle for two hours.
302f5037cd0SCody Peter MelloIf the peer does not respond to the probe within eight minutes, the TCP
303f5037cd0SCody Peter Melloconnection is aborted.
304f5037cd0SCody Peter MelloAn application can alter the probe behavior using the following TCP-level
305f5037cd0SCody Peter Mellosocket options:
306f5037cd0SCody Peter Mello.Bl -tag -offset indent -width 16m
307f5037cd0SCody Peter Mello.It Dv TCP_KEEPALIVE_THRESHOLD
308f5037cd0SCody Peter MelloDetermines the interval for sending the first probe.
309f5037cd0SCody Peter MelloThe option value is specified as an unsigned integer in milliseconds.
310f5037cd0SCody Peter MelloThe system default is controlled by the TCP
311f5037cd0SCody Peter Mello.Nm ndd
312f5037cd0SCody Peter Melloparameter
313f5037cd0SCody Peter Mello.Cm tcp_keepalive_interval .
314f5037cd0SCody Peter MelloThe minimum value is ten seconds.
315f5037cd0SCody Peter MelloThe maximum is ten days, while the default is two hours.
316f5037cd0SCody Peter Mello.It Dv TCP_KEEPALIVE_ABORT_THRESHOLD
317f5037cd0SCody Peter MelloIf TCP does not receive a response to the probe, then this option determines
318f5037cd0SCody Peter Mellohow long to wait before aborting a TCP connection.
319f5037cd0SCody Peter MelloThe option value is an unsigned integer in milliseconds.
320f5037cd0SCody Peter MelloThe value zero indicates that TCP should never time
321f5037cd0SCody Peter Melloout and abort the connection when probing.
322f5037cd0SCody Peter MelloThe system default is controlled by the TCP
323f5037cd0SCody Peter Mello.Nm ndd
324f5037cd0SCody Peter Melloparameter
325f5037cd0SCody Peter Mello.Sy tcp_keepalive_abort_interval .
326f5037cd0SCody Peter MelloThe default is eight minutes.
327f5037cd0SCody Peter Mello.It Dv TCP_KEEPIDLE
328f5037cd0SCody Peter MelloThis option, like
329f5037cd0SCody Peter Mello.Dv TCP_KEEPALIVE_THRESHOLD ,
330f5037cd0SCody Peter Mellodetermines the interval for sending the first probe, except that
331f5037cd0SCody Peter Mellothe option value is an unsigned integer in
332f5037cd0SCody Peter Mello.Sy seconds .
333f5037cd0SCody Peter MelloIt is provided primarily for compatibility with other Unix flavors.
334f5037cd0SCody Peter Mello.It Dv TCP_KEEPCNT
335f5037cd0SCody Peter MelloThis option specifies the number of keep-alive probes that should be sent
336f5037cd0SCody Peter Mellowithout any response from the peer before aborting the connection.
337f5037cd0SCody Peter Mello.It Dv TCP_KEEPINTVL
338f5037cd0SCody Peter MelloThis option specifies the interval in seconds between successive,
339f5037cd0SCody Peter Mellounacknowledged keep-alive probes.
340f5037cd0SCody Peter Mello.El
341f5037cd0SCody Peter Mello.Ss "Additional Configuration"
342f5037cd0SCody Peter Melloillumos supports TCP Extensions for High Performance (RFC 7323)
3431f183ba0SLauri Tirkkonenwhich includes the window scale and timestamp options, and Protection Against
344f5037cd0SCody Peter MelloWrap Around Sequence Numbers
345f5037cd0SCody Peter Mello.Po Sy PAWS Pc .
346f5037cd0SCody Peter MelloNote that if timestamps are negotiated on
3471f183ba0SLauri Tirkkonena connection, received segments without timestamps on that connection are
3481f183ba0SLauri Tirkkonensilently dropped per the suggestion in the RFC. illumos also supports Selective
349f5037cd0SCody Peter MelloAcknowledgment
350f5037cd0SCody Peter Mello.Po Sy SACK Pc
351f5037cd0SCody Peter Mellocapabilities (RFC 2018) and Explicit Congestion
352f5037cd0SCody Peter MelloNotification
353f5037cd0SCody Peter Mello.Po Sy ECN Pc
354f5037cd0SCody Peter Mellomechanism (RFC 3168).
355f5037cd0SCody Peter Mello.Pp
356c10c16deSRichard LoweTurn on the window scale option in one of the following ways:
357f5037cd0SCody Peter Mello.Bl -bullet -offset indent -width 4m
358f5037cd0SCody Peter Mello.It
359f5037cd0SCody Peter MelloAn application can set
360f5037cd0SCody Peter Mello.Dv SO_SNDBUF
361f5037cd0SCody Peter Melloor
362f5037cd0SCody Peter Mello.Dv SO_RCVBUF
363f5037cd0SCody Peter Mellosize in the
364f5037cd0SCody Peter Mello.Fn setsockopt
365f5037cd0SCody Peter Mellooption to be larger than 64K.
366f5037cd0SCody Peter MelloThis must be done
367f5037cd0SCody Peter Mello.Em before
368f5037cd0SCody Peter Mellothe program calls
369f5037cd0SCody Peter Mello.Fn listen
370f5037cd0SCody Peter Melloor
371f5037cd0SCody Peter Mello.Fn connect ,
372f5037cd0SCody Peter Mellobecause the window scale
373f5037cd0SCody Peter Mellooption is negotiated when the connection is established.
374f5037cd0SCody Peter MelloOnce the connection
375c10c16deSRichard Lowehas been made, it is too late to increase the send or receive window beyond the
376f5037cd0SCody Peter Mellodefault TCP limit of 64K.
377f5037cd0SCody Peter Mello.It
378f5037cd0SCody Peter MelloFor all applications, use
379bbf21555SRichard Lowe.Xr ndd 8
380f5037cd0SCody Peter Melloto modify the configuration parameter
381f5037cd0SCody Peter Mello.Cm tcp_wscale_always .
382f5037cd0SCody Peter MelloIf
383f5037cd0SCody Peter Mello.Cm tcp_wscale_always
384f5037cd0SCody Peter Mellois set to
385f5037cd0SCody Peter Mello.Sy 1 ,
386f5037cd0SCody Peter Mellothe
387f5037cd0SCody Peter Mellowindow scale option will always be set when connecting to a remote system.
388f5037cd0SCody Peter MelloIf
389f5037cd0SCody Peter Mello.Cm tcp_wscale_always
390f5037cd0SCody Peter Mellois
391f5037cd0SCody Peter Mello.Sy 0 ,
392f5037cd0SCody Peter Mellothe window scale option will be set only if
393f5037cd0SCody Peter Mellothe user has requested a send or receive window larger than 64K.
394f5037cd0SCody Peter MelloThe default value of
395f5037cd0SCody Peter Mello.Cm tcp_wscale_always
396f5037cd0SCody Peter Mellois
397f5037cd0SCody Peter Mello.Sy 1 .
398f5037cd0SCody Peter Mello.It
399f5037cd0SCody Peter MelloRegardless of the value of
400f5037cd0SCody Peter Mello.Cm tcp_wscale_always ,
401f5037cd0SCody Peter Mellothe window scale option
402c10c16deSRichard Lowewill always be included in a connect acknowledgement if the connecting system
403c10c16deSRichard Lowehas used the option.
404f5037cd0SCody Peter Mello.El
405f5037cd0SCody Peter Mello.Pp
406f5037cd0SCody Peter MelloTurn on SACK capabilities in the following way:
407f5037cd0SCody Peter Mello.Bl -bullet -offset indent -width 4m
408f5037cd0SCody Peter Mello.It
409f5037cd0SCody Peter MelloUse
410f5037cd0SCody Peter Mello.Nm ndd
411f5037cd0SCody Peter Melloto modify the configuration parameter
412f5037cd0SCody Peter Mello.Cm tcp_sack_permitted .
413f5037cd0SCody Peter MelloIf
414f5037cd0SCody Peter Mello.Cm tcp_sack_permitted
415f5037cd0SCody Peter Mellois set to
416f5037cd0SCody Peter Mello.Sy 0 ,
417f5037cd0SCody Peter MelloTCP will not accept SACK or send out SACK information.
418f5037cd0SCody Peter MelloIf
419f5037cd0SCody Peter Mello.Cm tcp_sack_permitted
420f5037cd0SCody Peter Mellois
421f5037cd0SCody Peter Melloset to
422f5037cd0SCody Peter Mello.Sy 1 ,
423f5037cd0SCody Peter MelloTCP will not initiate a connection with SACK permitted option in the
424f5037cd0SCody Peter Mello.Sy SYN
425f5037cd0SCody Peter Mellosegment, but will respond with SACK permitted option in the
426f5037cd0SCody Peter Mello.Sy SYN|ACK
427f5037cd0SCody Peter Mellosegment if an incoming connection request has the SACK permitted option.
428f5037cd0SCody Peter MelloThis means that TCP will only accept SACK information if the other side of the
429f5037cd0SCody Peter Melloconnection also accepts SACK information.
430f5037cd0SCody Peter MelloIf
431f5037cd0SCody Peter Mello.Cm tcp_sack_permitted
432f5037cd0SCody Peter Mellois set to
433f5037cd0SCody Peter Mello.Sy 2 ,
434f5037cd0SCody Peter Melloit will both initiate and accept connections with SACK information.
435f5037cd0SCody Peter MelloThe default for
436f5037cd0SCody Peter Mello.Cm tcp_sack_permitted
437f5037cd0SCody Peter Mellois
438f5037cd0SCody Peter Mello.Sy 2
439f5037cd0SCody Peter Mello.Pq active enabled .
440f5037cd0SCody Peter Mello.El
441f5037cd0SCody Peter Mello.Pp
442f5037cd0SCody Peter MelloTurn on the TCP ECN mechanism in the following way:
443f5037cd0SCody Peter Mello.Bl -bullet -offset indent -width 4m
444f5037cd0SCody Peter Mello.It
445f5037cd0SCody Peter MelloUse
446f5037cd0SCody Peter Mello.Nm ndd
447f5037cd0SCody Peter Melloto modify the configuration parameter
448f5037cd0SCody Peter Mello.Cm tcp_ecn_permitted .
449f5037cd0SCody Peter MelloIf
450f5037cd0SCody Peter Mello.Cm tcp_ecn_permitted
451f5037cd0SCody Peter Mellois set to
452f5037cd0SCody Peter Mello.Sy 0 ,
453f5037cd0SCody Peter Mellothen TCP will not negotiate with a peer that supports ECN mechanism.
454f5037cd0SCody Peter MelloIf
455f5037cd0SCody Peter Mello.Cm tcp_ecn_permitted
456f5037cd0SCody Peter Mellois set to
457f5037cd0SCody Peter Mello.Sy 1
458f5037cd0SCody Peter Mellowhen initiating a connection, TCP will not tell a peer that it supports
459f5037cd0SCody Peter Mello.Sy ECN
460f5037cd0SCody Peter Mellomechanism.
461f5037cd0SCody Peter MelloHowever, it will tell a peer that it supports
462f5037cd0SCody Peter Mello.Sy ECN
463c10c16deSRichard Lowemechanism when accepting a new incoming connection request if the peer
464f5037cd0SCody Peter Melloindicates that it supports
465f5037cd0SCody Peter Mello.Sy ECN
466f5037cd0SCody Peter Mellomechanism in the
467f5037cd0SCody Peter Mello.Sy SYN
468f5037cd0SCody Peter Mellosegment.
469f5037cd0SCody Peter MelloIf
470f5037cd0SCody Peter Mello.Cm tcp_ecn_permitted
471f5037cd0SCody Peter Mellois set to 2, in addition to negotiating with a peer on
472f5037cd0SCody Peter Mello.Sy ECN
473f5037cd0SCody Peter Mellomechanism when accepting connections, TCP will indicate in the outgoing
474f5037cd0SCody Peter Mello.Sy SYN
475f5037cd0SCody Peter Mellosegment that it supports
476f5037cd0SCody Peter Mello.Sy ECN
477f5037cd0SCody Peter Mellomechanism when TCP makes active outgoing connections.
478f5037cd0SCody Peter MelloThe default for
479f5037cd0SCody Peter Mello.Cm tcp_ecn_permitted
480f5037cd0SCody Peter Mellois 1.
481f5037cd0SCody Peter Mello.El
482f5037cd0SCody Peter Mello.Pp
48364f4fcacSWilliam DowlingTurn on the timestamp option in the following way:
484f5037cd0SCody Peter Mello.Bl -bullet -offset indent -width 4m
485f5037cd0SCody Peter Mello.It
486f5037cd0SCody Peter MelloUse
487f5037cd0SCody Peter Mello.Nm ndd
488f5037cd0SCody Peter Melloto modify the configuration parameter
489f5037cd0SCody Peter Mello.Cm tcp_tstamp_always .
490f5037cd0SCody Peter MelloIf
491f5037cd0SCody Peter Mello.Cm tcp_tstamp_always
492f5037cd0SCody Peter Mellois
493f5037cd0SCody Peter Mello.Sy 1 ,
494f5037cd0SCody Peter Mellothe timestamp option will always be set
495f5037cd0SCody Peter Mellowhen connecting to a remote machine.
496f5037cd0SCody Peter MelloIf
497f5037cd0SCody Peter Mello.Cm tcp_tstamp_always
498f5037cd0SCody Peter Mellois
499f5037cd0SCody Peter Mello.Sy 0 ,
500f5037cd0SCody Peter Mellothe timestamp option will not be set when connecting to a remote system.
501f5037cd0SCody Peter MelloThe
502f5037cd0SCody Peter Mellodefault for
503f5037cd0SCody Peter Mello.Cm tcp_tstamp_always
504f5037cd0SCody Peter Mellois
505f5037cd0SCody Peter Mello.Sy 0 .
506f5037cd0SCody Peter Mello.It
507f5037cd0SCody Peter MelloRegardless of the value of
508f5037cd0SCody Peter Mello.Cm tcp_tstamp_always ,
509f5037cd0SCody Peter Mellothe timestamp option will
510c10c16deSRichard Lowealways be included in a connect acknowledgement (and all succeeding packets) if
51164f4fcacSWilliam Dowlingthe connecting system has used the timestamp option.
512f5037cd0SCody Peter Mello.El
513f5037cd0SCody Peter Mello.Pp
51464f4fcacSWilliam DowlingUse the following procedure to turn on the timestamp option only when the
515c10c16deSRichard Lowewindow scale option is in effect:
516f5037cd0SCody Peter Mello.Bl -bullet -offset indent -width 4m
517f5037cd0SCody Peter Mello.It
518f5037cd0SCody Peter MelloUse
519f5037cd0SCody Peter Mello.Nm ndd
520f5037cd0SCody Peter Melloto modify the configuration parameter
521f5037cd0SCody Peter Mello.Cm tcp_tstamp_if_wscale .
522f5037cd0SCody Peter MelloSetting
523f5037cd0SCody Peter Mello.Cm tcp_tstamp_if_wscale
524f5037cd0SCody Peter Melloto
525f5037cd0SCody Peter Mello.Sy 1
526f5037cd0SCody Peter Mellowill cause the timestamp option
527c10c16deSRichard Loweto be set when connecting to a remote system, if the window scale option has
528f5037cd0SCody Peter Mellobeen set.
529f5037cd0SCody Peter MelloIf
530f5037cd0SCody Peter Mello.Cm tcp_tstamp_if_wscale
531f5037cd0SCody Peter Mellois
532f5037cd0SCody Peter Mello.Sy 0 ,
533f5037cd0SCody Peter Mellothe timestamp option will
534f5037cd0SCody Peter Mellonot be set when connecting to a remote system.
535f5037cd0SCody Peter MelloThe default for
536f5037cd0SCody Peter Mello.Cm tcp_tstamp_if_wscale
537f5037cd0SCody Peter Mellois
538f5037cd0SCody Peter Mello.Sy 1 .
539f5037cd0SCody Peter Mello.El
540f5037cd0SCody Peter Mello.Pp
541f5037cd0SCody Peter MelloProtection Against Wrap Around Sequence Numbers
542f5037cd0SCody Peter Mello.Po Sy PAWS Pc
543f5037cd0SCody Peter Mellois always used when the
54464f4fcacSWilliam Dowlingtimestamp option is set.
545f5037cd0SCody Peter Mello.Pp
546f5037cd0SCody Peter MelloThe operating system also supports multiple methods of generating initial sequence numbers.
547f5037cd0SCody Peter MelloOne of these methods is the improved technique suggested in RFC 1948.
548f5037cd0SCody Peter MelloWe
549f5037cd0SCody Peter Mello.Em HIGHLY
550f5037cd0SCody Peter Mellorecommend that you set sequence number generation parameters as
551f5037cd0SCody Peter Melloclose to boot time as possible.
552f5037cd0SCody Peter MelloThis prevents sequence number problems on
553c10c16deSRichard Loweconnections that use the same connection-ID as ones that used a different
554f5037cd0SCody Peter Mellosequence number generation.
555f5037cd0SCody Peter MelloThe
556f5037cd0SCody Peter Mello.Sy svc:/network/initial:default
557f5037cd0SCody Peter Melloservice configures the initial sequence number generation.
558f5037cd0SCody Peter MelloThe service reads the value contained in the configuration file
559f5037cd0SCody Peter Mello.Pa /etc/default/inetinit
560f5037cd0SCody Peter Melloto determine which method to use.
561f5037cd0SCody Peter Mello.Pp
562f5037cd0SCody Peter MelloThe
563f5037cd0SCody Peter Mello.Pa /etc/default/inetinit
564f5037cd0SCody Peter Mellofile is an unstable interface, and may change in future releases.
565f5037cd0SCody Peter Mello.Sh EXAMPLES
566f5037cd0SCody Peter Mello.Ss Example 1: Connecting to a server
567f5037cd0SCody Peter Mello.Bd -literal
568f5037cd0SCody Peter Mello$ gcc -std=c99 -Wall -lsocket -o client client.c
569f5037cd0SCody Peter Mello$ cat client.c
570f5037cd0SCody Peter Mello#include <sys/socket.h>
571f5037cd0SCody Peter Mello#include <netinet/in.h>
572f5037cd0SCody Peter Mello#include <netinet/tcp.h>
573f5037cd0SCody Peter Mello#include <netdb.h>
574f5037cd0SCody Peter Mello#include <stdio.h>
575f5037cd0SCody Peter Mello#include <string.h>
576f5037cd0SCody Peter Mello#include <unistd.h>
577f5037cd0SCody Peter Mello
578f5037cd0SCody Peter Melloint
579f5037cd0SCody Peter Mellomain(int argc, char *argv[])
580f5037cd0SCody Peter Mello{
581f5037cd0SCody Peter Mello	struct addrinfo hints, *gair, *p;
582f5037cd0SCody Peter Mello	int fd, rv, rlen;
583f5037cd0SCody Peter Mello	char buf[1024];
584f5037cd0SCody Peter Mello	int y = 1;
585f5037cd0SCody Peter Mello
586f5037cd0SCody Peter Mello	if (argc != 3) {
587*11994f6fSRobert Mustacchi		fprintf(stderr, "%s <host> <port>\en", argv[0]);
588f5037cd0SCody Peter Mello		return (1);
589f5037cd0SCody Peter Mello	}
590f5037cd0SCody Peter Mello
591f5037cd0SCody Peter Mello	memset(&hints, 0, sizeof (hints));
592f5037cd0SCody Peter Mello	hints.ai_family = PF_UNSPEC;
593f5037cd0SCody Peter Mello	hints.ai_socktype = SOCK_STREAM;
594f5037cd0SCody Peter Mello
595f5037cd0SCody Peter Mello	if ((rv = getaddrinfo(argv[1], argv[2], &hints, &gair)) != 0) {
596*11994f6fSRobert Mustacchi		fprintf(stderr, "getaddrinfo() failed: %s\en",
597f5037cd0SCody Peter Mello		    gai_strerror(rv));
598f5037cd0SCody Peter Mello		return (1);
599f5037cd0SCody Peter Mello	}
600f5037cd0SCody Peter Mello
601f5037cd0SCody Peter Mello	for (p = gair; p != NULL; p = p->ai_next) {
602f5037cd0SCody Peter Mello		if ((fd = socket(
603f5037cd0SCody Peter Mello		    p->ai_family,
604f5037cd0SCody Peter Mello		    p->ai_socktype,
605f5037cd0SCody Peter Mello		    p->ai_protocol)) == -1) {
606f5037cd0SCody Peter Mello			perror("socket() failed");
607f5037cd0SCody Peter Mello			continue;
608f5037cd0SCody Peter Mello		}
609f5037cd0SCody Peter Mello
610f5037cd0SCody Peter Mello		if (connect(fd, p->ai_addr, p->ai_addrlen) == -1) {
611f5037cd0SCody Peter Mello			close(fd);
612f5037cd0SCody Peter Mello			perror("connect() failed");
613f5037cd0SCody Peter Mello			continue;
614f5037cd0SCody Peter Mello		}
615f5037cd0SCody Peter Mello
616f5037cd0SCody Peter Mello		break;
617f5037cd0SCody Peter Mello	}
618f5037cd0SCody Peter Mello
619f5037cd0SCody Peter Mello	if (p == NULL) {
620*11994f6fSRobert Mustacchi		fprintf(stderr, "failed to connect to server\en");
621f5037cd0SCody Peter Mello		return (1);
622f5037cd0SCody Peter Mello	}
623f5037cd0SCody Peter Mello
624f5037cd0SCody Peter Mello	freeaddrinfo(gair);
625f5037cd0SCody Peter Mello
626f5037cd0SCody Peter Mello	if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &y,
627f5037cd0SCody Peter Mello	    sizeof (y)) == -1) {
628f5037cd0SCody Peter Mello		perror("setsockopt(SO_KEEPALIVE) failed");
629f5037cd0SCody Peter Mello		return (1);
630f5037cd0SCody Peter Mello	}
631f5037cd0SCody Peter Mello
632f5037cd0SCody Peter Mello	while ((rlen = read(fd, buf, sizeof (buf))) > 0) {
633f5037cd0SCody Peter Mello		fwrite(buf, rlen, 1, stdout);
634f5037cd0SCody Peter Mello	}
635f5037cd0SCody Peter Mello
636f5037cd0SCody Peter Mello	if (rlen == -1) {
637f5037cd0SCody Peter Mello		perror("read() failed");
638f5037cd0SCody Peter Mello	}
639f5037cd0SCody Peter Mello
640f5037cd0SCody Peter Mello	fflush(stdout);
641f5037cd0SCody Peter Mello
642f5037cd0SCody Peter Mello	if (close(fd) == -1) {
643f5037cd0SCody Peter Mello		perror("close() failed");
644f5037cd0SCody Peter Mello	}
645f5037cd0SCody Peter Mello
646f5037cd0SCody Peter Mello	return (0);
647f5037cd0SCody Peter Mello}
648f5037cd0SCody Peter Mello$ ./client 127.0.0.1 8080
649f5037cd0SCody Peter Mellohello
650f5037cd0SCody Peter Mello$ ./client ::1 8080
651f5037cd0SCody Peter Mellohello
652f5037cd0SCody Peter Mello.Ed
653f5037cd0SCody Peter Mello.Ss Example 2: Accepting client connections
654f5037cd0SCody Peter Mello.Bd -literal
655f5037cd0SCody Peter Mello$ gcc -std=c99 -Wall -lsocket -o server server.c
656f5037cd0SCody Peter Mello$ cat server.c
657f5037cd0SCody Peter Mello#include <sys/socket.h>
658f5037cd0SCody Peter Mello#include <netinet/in.h>
659f5037cd0SCody Peter Mello#include <netinet/tcp.h>
660f5037cd0SCody Peter Mello#include <netdb.h>
661f5037cd0SCody Peter Mello#include <stdio.h>
662f5037cd0SCody Peter Mello#include <string.h>
663f5037cd0SCody Peter Mello#include <unistd.h>
664f5037cd0SCody Peter Mello#include <arpa/inet.h>
665f5037cd0SCody Peter Mello
666f5037cd0SCody Peter Mellovoid
667f5037cd0SCody Peter Mellologmsg(struct sockaddr *s, int bytes)
668f5037cd0SCody Peter Mello{
669f5037cd0SCody Peter Mello	char dq[INET6_ADDRSTRLEN];
670f5037cd0SCody Peter Mello
671f5037cd0SCody Peter Mello	switch (s->sa_family) {
672f5037cd0SCody Peter Mello	case AF_INET: {
673f5037cd0SCody Peter Mello		struct sockaddr_in *s4 = (struct sockaddr_in *)s;
674f5037cd0SCody Peter Mello		inet_ntop(AF_INET, &s4->sin_addr, dq, sizeof (dq));
675*11994f6fSRobert Mustacchi		fprintf(stdout, "sent %d bytes to %s:%d\en",
676f5037cd0SCody Peter Mello		    bytes, dq, ntohs(s4->sin_port));
677f5037cd0SCody Peter Mello		break;
678f5037cd0SCody Peter Mello	}
679f5037cd0SCody Peter Mello	case AF_INET6: {
680f5037cd0SCody Peter Mello		struct sockaddr_in6 *s6 = (struct sockaddr_in6 *)s;
681f5037cd0SCody Peter Mello		inet_ntop(AF_INET6, &s6->sin6_addr, dq, sizeof (dq));
682*11994f6fSRobert Mustacchi		fprintf(stdout, "sent %d bytes to [%s]:%d\en",
683f5037cd0SCody Peter Mello		    bytes, dq, ntohs(s6->sin6_port));
684f5037cd0SCody Peter Mello		break;
685f5037cd0SCody Peter Mello	}
686f5037cd0SCody Peter Mello	default:
687*11994f6fSRobert Mustacchi		fprintf(stdout, "sent %d bytes to unknown client\en",
688f5037cd0SCody Peter Mello		    bytes);
689f5037cd0SCody Peter Mello		break;
690f5037cd0SCody Peter Mello	}
691f5037cd0SCody Peter Mello}
692f5037cd0SCody Peter Mello
693f5037cd0SCody Peter Melloint
694f5037cd0SCody Peter Mellomain(int argc, char *argv[])
695f5037cd0SCody Peter Mello{
696f5037cd0SCody Peter Mello	struct addrinfo hints, *gair, *p;
697f5037cd0SCody Peter Mello	int sfd, cfd;
698f5037cd0SCody Peter Mello	int slen, wlen, rv;
699f5037cd0SCody Peter Mello
700f5037cd0SCody Peter Mello	if (argc != 3) {
701*11994f6fSRobert Mustacchi		fprintf(stderr, "%s <port> <message>\en", argv[0]);
702f5037cd0SCody Peter Mello		return (1);
703f5037cd0SCody Peter Mello	}
704f5037cd0SCody Peter Mello
705f5037cd0SCody Peter Mello	slen = strlen(argv[2]);
706f5037cd0SCody Peter Mello
707f5037cd0SCody Peter Mello	memset(&hints, 0, sizeof (hints));
708f5037cd0SCody Peter Mello	hints.ai_family = PF_UNSPEC;
709f5037cd0SCody Peter Mello	hints.ai_socktype = SOCK_STREAM;
710f5037cd0SCody Peter Mello	hints.ai_flags = AI_PASSIVE;
711f5037cd0SCody Peter Mello
712f5037cd0SCody Peter Mello	if ((rv = getaddrinfo(NULL, argv[1], &hints, &gair)) != 0) {
713*11994f6fSRobert Mustacchi		fprintf(stderr, "getaddrinfo() failed: %s\en",
714f5037cd0SCody Peter Mello		    gai_strerror(rv));
715f5037cd0SCody Peter Mello		return (1);
716f5037cd0SCody Peter Mello	}
717f5037cd0SCody Peter Mello
718f5037cd0SCody Peter Mello	for (p = gair; p != NULL; p = p->ai_next) {
719f5037cd0SCody Peter Mello		if ((sfd = socket(
720f5037cd0SCody Peter Mello		    p->ai_family,
721f5037cd0SCody Peter Mello		    p->ai_socktype,
722f5037cd0SCody Peter Mello		    p->ai_protocol)) == -1) {
723f5037cd0SCody Peter Mello			perror("socket() failed");
724f5037cd0SCody Peter Mello			continue;
725f5037cd0SCody Peter Mello		}
726f5037cd0SCody Peter Mello
727f5037cd0SCody Peter Mello		if (bind(sfd, p->ai_addr, p->ai_addrlen) == -1) {
728f5037cd0SCody Peter Mello			close(sfd);
729f5037cd0SCody Peter Mello			perror("bind() failed");
730f5037cd0SCody Peter Mello			continue;
731f5037cd0SCody Peter Mello		}
732f5037cd0SCody Peter Mello
733f5037cd0SCody Peter Mello		break;
734f5037cd0SCody Peter Mello	}
735f5037cd0SCody Peter Mello
736f5037cd0SCody Peter Mello	if (p == NULL) {
737*11994f6fSRobert Mustacchi		fprintf(stderr, "server failed to bind()\en");
738f5037cd0SCody Peter Mello		return (1);
739f5037cd0SCody Peter Mello	}
740f5037cd0SCody Peter Mello
741f5037cd0SCody Peter Mello	freeaddrinfo(gair);
742f5037cd0SCody Peter Mello
743f5037cd0SCody Peter Mello	if (listen(sfd, 1024) != 0) {
744f5037cd0SCody Peter Mello		perror("listen() failed");
745f5037cd0SCody Peter Mello		return (1);
746f5037cd0SCody Peter Mello	}
747f5037cd0SCody Peter Mello
748*11994f6fSRobert Mustacchi	fprintf(stdout, "waiting for clients...\en");
749f5037cd0SCody Peter Mello
750f5037cd0SCody Peter Mello	for (int times = 0; times < 5; times++) {
751f5037cd0SCody Peter Mello		struct sockaddr_storage stor;
752f5037cd0SCody Peter Mello		socklen_t alen = sizeof (stor);
753f5037cd0SCody Peter Mello		struct sockaddr *addr = (struct sockaddr *)&stor;
754f5037cd0SCody Peter Mello
755f5037cd0SCody Peter Mello		if ((cfd = accept(sfd, addr, &alen)) == -1) {
756f5037cd0SCody Peter Mello			perror("accept() failed");
757f5037cd0SCody Peter Mello			continue;
758f5037cd0SCody Peter Mello		}
759f5037cd0SCody Peter Mello
760f5037cd0SCody Peter Mello		wlen = 0;
761f5037cd0SCody Peter Mello
762f5037cd0SCody Peter Mello		do {
763f5037cd0SCody Peter Mello			wlen += write(cfd, argv[2] + wlen, slen - wlen);
764f5037cd0SCody Peter Mello		} while (wlen < slen);
765f5037cd0SCody Peter Mello
766f5037cd0SCody Peter Mello		logmsg(addr, wlen);
767f5037cd0SCody Peter Mello
768f5037cd0SCody Peter Mello		if (close(cfd) == -1) {
769f5037cd0SCody Peter Mello			perror("close(cfd) failed");
770f5037cd0SCody Peter Mello		}
771f5037cd0SCody Peter Mello	}
772f5037cd0SCody Peter Mello
773f5037cd0SCody Peter Mello	if (close(sfd) == -1) {
774f5037cd0SCody Peter Mello		perror("close(sfd) failed");
775f5037cd0SCody Peter Mello	}
776f5037cd0SCody Peter Mello
777*11994f6fSRobert Mustacchi	fprintf(stdout, "finished.\en");
778f5037cd0SCody Peter Mello
779f5037cd0SCody Peter Mello	return (0);
780f5037cd0SCody Peter Mello}
781*11994f6fSRobert Mustacchi$ ./server 8080 $'hello\en'
782f5037cd0SCody Peter Mellowaiting for clients...
783f5037cd0SCody Peter Mellosent 6 bytes to [::ffff:127.0.0.1]:59059
784f5037cd0SCody Peter Mellosent 6 bytes to [::ffff:127.0.0.1]:47448
785f5037cd0SCody Peter Mellosent 6 bytes to [::ffff:127.0.0.1]:54949
786f5037cd0SCody Peter Mellosent 6 bytes to [::ffff:127.0.0.1]:55186
787f5037cd0SCody Peter Mellosent 6 bytes to [::1]:62256
788f5037cd0SCody Peter Mellofinished.
789f5037cd0SCody Peter Mello.Ed
790f5037cd0SCody Peter Mello.Sh DIAGNOSTICS
791c10c16deSRichard LoweA socket operation may fail if:
792f5037cd0SCody Peter Mello.Bl -tag -offset indent -width 16m
793f5037cd0SCody Peter Mello.It Er EISCONN
794f5037cd0SCody Peter MelloA
795f5037cd0SCody Peter Mello.Fn connect
796f5037cd0SCody Peter Mellooperation was attempted on a socket on which a
797f5037cd0SCody Peter Mello.Fn connect
798f5037cd0SCody Peter Mellooperation had already been performed.
799f5037cd0SCody Peter Mello.It Er ETIMEDOUT
800c10c16deSRichard LoweA connection was dropped due to excessive retransmissions.
801f5037cd0SCody Peter Mello.It Er ECONNRESET
802c10c16deSRichard LoweThe remote peer forced the connection to be closed (usually because the remote
803c10c16deSRichard Lowemachine has lost state information about the connection due to a crash).
804f5037cd0SCody Peter Mello.It Er ECONNREFUSED
805c10c16deSRichard LoweThe remote peer actively refused connection establishment (usually because no
806c10c16deSRichard Loweprocess is listening to the port).
807f5037cd0SCody Peter Mello.It Er EADDRINUSE
808f5037cd0SCody Peter MelloA
809f5037cd0SCody Peter Mello.Fn bind
810f5037cd0SCody Peter Mellooperation was attempted on a socket with a network address/port pair that has
811f5037cd0SCody Peter Melloalready been bound to another socket.
812f5037cd0SCody Peter Mello.It Er EADDRNOTAVAIL
813f5037cd0SCody Peter MelloA
814f5037cd0SCody Peter Mello.Fn bind
815f5037cd0SCody Peter Mellooperation was attempted on a socket with a network address for which no network
816f5037cd0SCody Peter Mellointerface exists.
817f5037cd0SCody Peter Mello.It Er EACCES
818f5037cd0SCody Peter MelloA
819f5037cd0SCody Peter Mello.Fn bind
820f5037cd0SCody Peter Mellooperation was attempted with a
821f5037cd0SCody Peter Mello.Dq reserved
822f5037cd0SCody Peter Melloport number and the effective user ID of the process was not the privileged user.
823f5037cd0SCody Peter Mello.It Er ENOBUFS
824c10c16deSRichard LoweThe system ran out of memory for internal data structures.
825f5037cd0SCody Peter Mello.El
826f5037cd0SCody Peter Mello.Sh SEE ALSO
827f5037cd0SCody Peter Mello.Xr svcs 1 ,
828f5037cd0SCody Peter Mello.Xr ioctl 2 ,
829f5037cd0SCody Peter Mello.Xr read 2 ,
830f5037cd0SCody Peter Mello.Xr write 2 ,
831f5037cd0SCody Peter Mello.Xr accept 3SOCKET ,
832f5037cd0SCody Peter Mello.Xr bind 3SOCKET ,
833f5037cd0SCody Peter Mello.Xr connect 3SOCKET ,
834f5037cd0SCody Peter Mello.Xr getprotobyname 3SOCKET ,
835f5037cd0SCody Peter Mello.Xr getsockopt 3SOCKET ,
836f5037cd0SCody Peter Mello.Xr listen 3SOCKET ,
837f5037cd0SCody Peter Mello.Xr send 3SOCKET ,
838bbf21555SRichard Lowe.Xr inet 4P ,
839bbf21555SRichard Lowe.Xr inet6 4P ,
840bbf21555SRichard Lowe.Xr ip 4P ,
841bbf21555SRichard Lowe.Xr ip6 4P ,
842bbf21555SRichard Lowe.Xr smf 7 ,
843bbf21555SRichard Lowe.Xr ndd 8 ,
844bbf21555SRichard Lowe.Xr svcadm 8
845f5037cd0SCody Peter Mello.Rs
846f5037cd0SCody Peter Mello.%A "K. Ramakrishnan"
847f5037cd0SCody Peter Mello.%A "S. Floyd"
848f5037cd0SCody Peter Mello.%A "D. Black"
849f5037cd0SCody Peter Mello.%T "The Addition of Explicit Congestion Notification (ECN) to IP"
850f5037cd0SCody Peter Mello.%R "RFC 3168"
851f5037cd0SCody Peter Mello.%D "September 2001"
852f5037cd0SCody Peter Mello.Re
853f5037cd0SCody Peter Mello.Rs
854f5037cd0SCody Peter Mello.%A "M. Mathias"
855f5037cd0SCody Peter Mello.%A "J. Mahdavi"
856f5037cd0SCody Peter Mello.%A "S. Ford"
857f5037cd0SCody Peter Mello.%A "A. Romanow"
858f5037cd0SCody Peter Mello.%T "TCP Selective Acknowledgement Options"
859f5037cd0SCody Peter Mello.%R "RFC 2018"
860f5037cd0SCody Peter Mello.%D "October 1996"
861f5037cd0SCody Peter Mello.Re
862f5037cd0SCody Peter Mello.Rs
863f5037cd0SCody Peter Mello.%A "S. Bellovin"
864f5037cd0SCody Peter Mello.%T "Defending Against Sequence Number Attacks"
865f5037cd0SCody Peter Mello.%R "RFC 1948"
866f5037cd0SCody Peter Mello.%D "May 1996"
867f5037cd0SCody Peter Mello.Re
868f5037cd0SCody Peter Mello.Rs
869f5037cd0SCody Peter Mello.%A "D. Borman"
870f5037cd0SCody Peter Mello.%A "B. Braden"
871f5037cd0SCody Peter Mello.%A "V. Jacobson"
872f5037cd0SCody Peter Mello.%A "R. Scheffenegger, Ed."
873f5037cd0SCody Peter Mello.%T "TCP Extensions for High Performance"
874f5037cd0SCody Peter Mello.%R "RFC 7323"
875f5037cd0SCody Peter Mello.%D "September 2014"
876f5037cd0SCody Peter Mello.Re
877f5037cd0SCody Peter Mello.Rs
878f5037cd0SCody Peter Mello.%A "Jon Postel"
879f5037cd0SCody Peter Mello.%T "Transmission Control Protocol - DARPA Internet Program Protocol Specification"
880f5037cd0SCody Peter Mello.%R "RFC 793"
881f5037cd0SCody Peter Mello.%C "Network Information Center, SRI International, Menlo Park, CA."
882f5037cd0SCody Peter Mello.%D "September 1981"
883f5037cd0SCody Peter Mello.Re
884f5037cd0SCody Peter Mello.Sh NOTES
885f5037cd0SCody Peter MelloThe
886f5037cd0SCody Peter Mello.Sy tcp
887f5037cd0SCody Peter Melloservice is managed by the service management facility,
888bbf21555SRichard Lowe.Xr smf 7 ,
889f5037cd0SCody Peter Mellounder the service identifier
890f5037cd0SCody Peter Mello.Sy svc:/network/initial:default .
891f5037cd0SCody Peter Mello.Pp
892c10c16deSRichard LoweAdministrative actions on this service, such as enabling, disabling, or
893f5037cd0SCody Peter Mellorequesting restart, can be performed using
894bbf21555SRichard Lowe.Xr svcadm 8 .
895f5037cd0SCody Peter MelloThe service's
896f5037cd0SCody Peter Mellostatus can be queried using the
897f5037cd0SCody Peter Mello.Xr svcs 1
898f5037cd0SCody Peter Mellocommand.
899