18269e767SBrooks Davis.\" Copyright (c) 1983, 1991, 1993 28269e767SBrooks Davis.\" The Regents of the University of California. All rights reserved. 38269e767SBrooks Davis.\" 48269e767SBrooks Davis.\" Redistribution and use in source and binary forms, with or without 58269e767SBrooks Davis.\" modification, are permitted provided that the following conditions 68269e767SBrooks Davis.\" are met: 78269e767SBrooks Davis.\" 1. Redistributions of source code must retain the above copyright 88269e767SBrooks Davis.\" notice, this list of conditions and the following disclaimer. 98269e767SBrooks Davis.\" 2. Redistributions in binary form must reproduce the above copyright 108269e767SBrooks Davis.\" notice, this list of conditions and the following disclaimer in the 118269e767SBrooks Davis.\" documentation and/or other materials provided with the distribution. 128269e767SBrooks Davis.\" 3. Neither the name of the University nor the names of its contributors 138269e767SBrooks Davis.\" may be used to endorse or promote products derived from this software 148269e767SBrooks Davis.\" without specific prior written permission. 158269e767SBrooks Davis.\" 168269e767SBrooks Davis.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 178269e767SBrooks Davis.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 188269e767SBrooks Davis.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 198269e767SBrooks Davis.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 208269e767SBrooks Davis.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 218269e767SBrooks Davis.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 228269e767SBrooks Davis.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 238269e767SBrooks Davis.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 248269e767SBrooks Davis.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 258269e767SBrooks Davis.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 268269e767SBrooks Davis.\" SUCH DAMAGE. 278269e767SBrooks Davis.\" 288269e767SBrooks Davis.Dd January 15, 2023 298269e767SBrooks Davis.Dt SOCKET 2 308269e767SBrooks Davis.Os 318269e767SBrooks Davis.Sh NAME 328269e767SBrooks Davis.Nm socket 338269e767SBrooks Davis.Nd create an endpoint for communication 348269e767SBrooks Davis.Sh LIBRARY 358269e767SBrooks Davis.Lb libc 368269e767SBrooks Davis.Sh SYNOPSIS 378269e767SBrooks Davis.In sys/socket.h 388269e767SBrooks Davis.Ft int 398269e767SBrooks Davis.Fn socket "int domain" "int type" "int protocol" 408269e767SBrooks Davis.Sh DESCRIPTION 418269e767SBrooks DavisThe 428269e767SBrooks Davis.Fn socket 438269e767SBrooks Davissystem call 448269e767SBrooks Daviscreates an endpoint for communication and returns a descriptor. 458269e767SBrooks Davis.Pp 468269e767SBrooks DavisThe 478269e767SBrooks Davis.Fa domain 488269e767SBrooks Davisargument specifies a communications domain within which 498269e767SBrooks Daviscommunication will take place; this selects the protocol family 508269e767SBrooks Daviswhich should be used. 518269e767SBrooks DavisThese families are defined in the include file 528269e767SBrooks Davis.In sys/socket.h . 538269e767SBrooks DavisThe currently understood formats are: 548269e767SBrooks Davis.Pp 558269e767SBrooks Davis.Bd -literal -offset indent -compact 568269e767SBrooks DavisPF_LOCAL Host-internal protocols (alias for PF_UNIX), 578269e767SBrooks DavisPF_UNIX Host-internal protocols, 588269e767SBrooks DavisPF_INET Internet version 4 protocols, 598269e767SBrooks DavisPF_INET6 Internet version 6 protocols, 608269e767SBrooks DavisPF_DIVERT Firewall packet diversion/re-injection, 618269e767SBrooks DavisPF_ROUTE Internal routing protocol, 628269e767SBrooks DavisPF_KEY Internal key-management function, 638269e767SBrooks DavisPF_NETGRAPH Netgraph sockets, 648269e767SBrooks DavisPF_NETLINK Netlink protocols, 658269e767SBrooks DavisPF_BLUETOOTH Bluetooth protocols, 668269e767SBrooks DavisPF_INET_SDP OFED socket direct protocol (IPv4), 678269e767SBrooks DavisAF_HYPERV HyperV sockets 688269e767SBrooks Davis.Ed 698269e767SBrooks Davis.Pp 708269e767SBrooks DavisEach protocol family is connected to an address family, which has the 718269e767SBrooks Davissame name except that the prefix is 728269e767SBrooks Davis.Dq Dv AF_ 738269e767SBrooks Davisin place of 748269e767SBrooks Davis.Dq Dv PF_ . 758269e767SBrooks DavisOther protocol families may be also defined, beginning with 768269e767SBrooks Davis.Dq Dv PF_ , 778269e767SBrooks Daviswith corresponding address families. 788269e767SBrooks Davis.Pp 798269e767SBrooks DavisThe socket has the indicated 808269e767SBrooks Davis.Fa type , 818269e767SBrooks Daviswhich specifies the semantics of communication. 828269e767SBrooks DavisCurrently 838269e767SBrooks Davisdefined types are: 848269e767SBrooks Davis.Pp 858269e767SBrooks Davis.Bd -literal -offset indent -compact 868269e767SBrooks DavisSOCK_STREAM Stream socket, 878269e767SBrooks DavisSOCK_DGRAM Datagram socket, 888269e767SBrooks DavisSOCK_RAW Raw-protocol interface, 898269e767SBrooks DavisSOCK_SEQPACKET Sequenced packet stream 908269e767SBrooks Davis.Ed 918269e767SBrooks Davis.Pp 928269e767SBrooks DavisA 938269e767SBrooks Davis.Dv SOCK_STREAM 948269e767SBrooks Davistype provides sequenced, reliable, 958269e767SBrooks Davistwo-way connection based byte streams. 968269e767SBrooks DavisAn out-of-band data transmission mechanism may be supported. 978269e767SBrooks DavisA 988269e767SBrooks Davis.Dv SOCK_DGRAM 998269e767SBrooks Davissocket supports 1008269e767SBrooks Davisdatagrams (connectionless, unreliable messages of 1018269e767SBrooks Davisa fixed (typically small) maximum length). 1028269e767SBrooks DavisA 1038269e767SBrooks Davis.Dv SOCK_SEQPACKET 1048269e767SBrooks Davissocket may provide a sequenced, reliable, 1058269e767SBrooks Davistwo-way connection-based data transmission path for datagrams 1068269e767SBrooks Davisof fixed maximum length; a consumer may be required to read 1078269e767SBrooks Davisan entire packet with each read system call. 1088269e767SBrooks DavisThis facility may have protocol-specific properties. 1098269e767SBrooks Davis.Dv SOCK_RAW 1108269e767SBrooks Davissockets provide access to internal network protocols and interfaces. 1118269e767SBrooks DavisThe 1128269e767SBrooks Davis.Dv SOCK_RAW 1138269e767SBrooks Davistype is available only to the super-user and is described in 1148269e767SBrooks Davis.Xr ip 4 1158269e767SBrooks Davisand 1168269e767SBrooks Davis.Xr ip6 4 . 1178269e767SBrooks Davis.Pp 1188269e767SBrooks DavisAdditionally, the following flags are allowed in the 1198269e767SBrooks Davis.Fa type 1208269e767SBrooks Davisargument: 1218269e767SBrooks Davis.Pp 1228269e767SBrooks Davis.Bd -literal -offset indent -compact 1238269e767SBrooks DavisSOCK_CLOEXEC Set close-on-exec on the new descriptor, 1248269e767SBrooks DavisSOCK_NONBLOCK Set non-blocking mode on the new socket 1258269e767SBrooks Davis.Ed 1268269e767SBrooks Davis.Pp 1278269e767SBrooks DavisThe 1288269e767SBrooks Davis.Fa protocol 1298269e767SBrooks Davisargument 1308269e767SBrooks Davisspecifies a particular protocol to be used with the socket. 1318269e767SBrooks DavisNormally only a single protocol exists to support a particular 1328269e767SBrooks Davissocket type within a given protocol family. 1338269e767SBrooks DavisHowever, it is possible 1348269e767SBrooks Davisthat many protocols may exist, in which case a particular protocol 1358269e767SBrooks Davismust be specified in this manner. 1368269e767SBrooks DavisThe protocol number to use is 1378269e767SBrooks Davisparticular to the 1388269e767SBrooks Davis.Dq "communication domain" 1398269e767SBrooks Davisin which communication 1408269e767SBrooks Davisis to take place; see 1418269e767SBrooks Davis.Xr protocols 5 . 1428269e767SBrooks Davis.Pp 1438269e767SBrooks DavisThe 1448269e767SBrooks Davis.Fa protocol 1458269e767SBrooks Davisargument may be set to zero (0) to request the default 1468269e767SBrooks Davisimplementation of a socket type for the protocol, if any. 1478269e767SBrooks Davis.Pp 1488269e767SBrooks DavisSockets of type 1498269e767SBrooks Davis.Dv SOCK_STREAM 1508269e767SBrooks Davisare full-duplex byte streams, similar 1518269e767SBrooks Davisto pipes. 1528269e767SBrooks DavisA stream socket must be in a 1538269e767SBrooks Davis.Em connected 1548269e767SBrooks Davisstate before any data may be sent or received 1558269e767SBrooks Davison it. 1568269e767SBrooks DavisA connection to another socket is created with a 1578269e767SBrooks Davis.Xr connect 2 1588269e767SBrooks Davissystem call. 1598269e767SBrooks DavisOnce connected, data may be transferred using 1608269e767SBrooks Davis.Xr read 2 1618269e767SBrooks Davisand 1628269e767SBrooks Davis.Xr write 2 1638269e767SBrooks Daviscalls or some variant of the 1648269e767SBrooks Davis.Xr send 2 1658269e767SBrooks Davisand 1668269e767SBrooks Davis.Xr recv 2 1678269e767SBrooks Davisfunctions. 1688269e767SBrooks Davis(Some protocol families, such as the Internet family, 1698269e767SBrooks Davissupport the notion of an 1708269e767SBrooks Davis.Dq implied connect , 1718269e767SBrooks Daviswhich permits data to be sent piggybacked onto a connect operation by 1728269e767SBrooks Davisusing the 1738269e767SBrooks Davis.Xr sendto 2 1748269e767SBrooks Davissystem call.) 1758269e767SBrooks DavisWhen a session has been completed a 1768269e767SBrooks Davis.Xr close 2 1778269e767SBrooks Davismay be performed. 1788269e767SBrooks DavisOut-of-band data may also be transmitted as described in 1798269e767SBrooks Davis.Xr send 2 1808269e767SBrooks Davisand received as described in 1818269e767SBrooks Davis.Xr recv 2 . 1828269e767SBrooks Davis.Pp 1838269e767SBrooks DavisThe communications protocols used to implement a 1848269e767SBrooks Davis.Dv SOCK_STREAM 1858269e767SBrooks Davisensure that data 1868269e767SBrooks Davisis not lost or duplicated. 1878269e767SBrooks DavisIf a piece of data for which the 1888269e767SBrooks Davispeer protocol has buffer space cannot be successfully transmitted 1898269e767SBrooks Daviswithin a reasonable length of time, then 1908269e767SBrooks Davisthe connection is considered broken and calls 1918269e767SBrooks Daviswill indicate an error with 1928269e767SBrooks Davis-1 returns and with 1938269e767SBrooks Davis.Er ETIMEDOUT 1948269e767SBrooks Davisas the specific code 1958269e767SBrooks Davisin the global variable 1968269e767SBrooks Davis.Va errno . 1978269e767SBrooks DavisThe protocols optionally keep sockets 1988269e767SBrooks Davis.Dq warm 1998269e767SBrooks Davisby forcing transmissions 2008269e767SBrooks Davisroughly every minute in the absence of other activity. 2018269e767SBrooks DavisAn error is then indicated if no response can be 2028269e767SBrooks Daviselicited on an otherwise 2038269e767SBrooks Davisidle connection for an extended period (e.g.\& 5 minutes). 2048269e767SBrooks DavisBy default, a 2058269e767SBrooks Davis.Dv SIGPIPE 2068269e767SBrooks Davissignal is raised if a process sends 2078269e767SBrooks Davison a broken stream, but this behavior may be inhibited via 2088269e767SBrooks Davis.Xr setsockopt 2 . 2098269e767SBrooks Davis.Pp 2108269e767SBrooks Davis.Dv SOCK_SEQPACKET 2118269e767SBrooks Davissockets employ the same system calls 2128269e767SBrooks Davisas 2138269e767SBrooks Davis.Dv SOCK_STREAM 2148269e767SBrooks Davissockets. 2158269e767SBrooks DavisThe only difference 2168269e767SBrooks Davisis that 2178269e767SBrooks Davis.Xr read 2 2188269e767SBrooks Daviscalls will return only the amount of data requested, 2198269e767SBrooks Davisand any remaining in the arriving packet will be discarded. 2208269e767SBrooks Davis.Pp 2218269e767SBrooks Davis.Dv SOCK_DGRAM 2228269e767SBrooks Davisand 2238269e767SBrooks Davis.Dv SOCK_RAW 2248269e767SBrooks Davissockets allow sending of datagrams to correspondents 2258269e767SBrooks Davisnamed in 2268269e767SBrooks Davis.Xr send 2 2278269e767SBrooks Daviscalls. 2288269e767SBrooks DavisDatagrams are generally received with 2298269e767SBrooks Davis.Xr recvfrom 2 , 2308269e767SBrooks Daviswhich returns the next datagram with its return address. 2318269e767SBrooks Davis.Pp 2328269e767SBrooks DavisAn 2338269e767SBrooks Davis.Xr fcntl 2 2348269e767SBrooks Davissystem call can be used to specify a process group to receive 2358269e767SBrooks Davisa 2368269e767SBrooks Davis.Dv SIGURG 2378269e767SBrooks Davissignal when the out-of-band data arrives. 2388269e767SBrooks DavisIt may also enable non-blocking I/O 2398269e767SBrooks Davisand asynchronous notification of I/O events 2408269e767SBrooks Davisvia 2418269e767SBrooks Davis.Dv SIGIO . 2428269e767SBrooks Davis.Pp 2438269e767SBrooks DavisThe operation of sockets is controlled by socket level 2448269e767SBrooks Davis.Em options . 2458269e767SBrooks DavisThese options are defined in the file 2468269e767SBrooks Davis.In sys/socket.h . 2478269e767SBrooks DavisThe 2488269e767SBrooks Davis.Xr setsockopt 2 2498269e767SBrooks Davisand 2508269e767SBrooks Davis.Xr getsockopt 2 2518269e767SBrooks Davissystem calls are used to set and get options, respectively. 2528269e767SBrooks Davis.Sh RETURN VALUES 2538269e767SBrooks DavisA -1 is returned if an error occurs, otherwise the return 2548269e767SBrooks Davisvalue is a descriptor referencing the socket. 2558269e767SBrooks Davis.Sh ERRORS 2568269e767SBrooks DavisThe 2578269e767SBrooks Davis.Fn socket 2588269e767SBrooks Davissystem call fails if: 2598269e767SBrooks Davis.Bl -tag -width Er 2608269e767SBrooks Davis.It Bq Er EACCES 2618269e767SBrooks DavisPermission to create a socket of the specified type and/or protocol 2628269e767SBrooks Davisis denied. 2638269e767SBrooks Davis.It Bq Er EAFNOSUPPORT 2648269e767SBrooks DavisThe address family (domain) is not supported or the 2658269e767SBrooks Davisspecified domain is not supported by this protocol family. 2668269e767SBrooks Davis.It Bq Er EMFILE 2678269e767SBrooks DavisThe per-process descriptor table is full. 2688269e767SBrooks Davis.It Bq Er ENFILE 2698269e767SBrooks DavisThe system file table is full. 2708269e767SBrooks Davis.It Bq Er ENOBUFS 2718269e767SBrooks DavisInsufficient buffer space is available. 2728269e767SBrooks DavisThe socket cannot be created until sufficient resources are freed. 2738269e767SBrooks Davis.It Bq Er EPERM 2748269e767SBrooks DavisUser has insufficient privileges to carry out the requested operation. 2758269e767SBrooks Davis.It Bq Er EPROTONOSUPPORT 2768269e767SBrooks DavisThe protocol type or the specified protocol is not supported 2778269e767SBrooks Daviswithin this domain. 2788269e767SBrooks Davis.It Bq Er EPROTOTYPE 2798269e767SBrooks DavisThe socket type is not supported by the protocol. 2808269e767SBrooks Davis.El 2818269e767SBrooks Davis.Sh SEE ALSO 2828269e767SBrooks Davis.Xr accept 2 , 2838269e767SBrooks Davis.Xr bind 2 , 2848269e767SBrooks Davis.Xr connect 2 , 2858269e767SBrooks Davis.Xr getpeername 2 , 2868269e767SBrooks Davis.Xr getsockname 2 , 2878269e767SBrooks Davis.Xr getsockopt 2 , 2888269e767SBrooks Davis.Xr ioctl 2 , 2898269e767SBrooks Davis.Xr listen 2 , 2908269e767SBrooks Davis.Xr read 2 , 2918269e767SBrooks Davis.Xr recv 2 , 2928269e767SBrooks Davis.Xr select 2 , 2938269e767SBrooks Davis.Xr send 2 , 2948269e767SBrooks Davis.Xr shutdown 2 , 2958269e767SBrooks Davis.Xr socketpair 2 , 2968269e767SBrooks Davis.Xr write 2 , 2978269e767SBrooks Davis.Xr CMSG_DATA 3 , 2988269e767SBrooks Davis.Xr getprotoent 3 , 299*6e1fc011SGraham Percival.Xr divert 4 , 300*6e1fc011SGraham Percival.Xr ip 4 , 301*6e1fc011SGraham Percival.Xr ip6 4 , 3028269e767SBrooks Davis.Xr netgraph 4 , 3038269e767SBrooks Davis.Xr protocols 5 3048269e767SBrooks Davis.Rs 3058269e767SBrooks Davis.%T "An Introductory 4.3 BSD Interprocess Communication Tutorial" 3068269e767SBrooks Davis.%B PS1 3078269e767SBrooks Davis.%N 7 3088269e767SBrooks Davis.Re 3098269e767SBrooks Davis.Rs 3108269e767SBrooks Davis.%T "BSD Interprocess Communication Tutorial" 3118269e767SBrooks Davis.%B PS1 3128269e767SBrooks Davis.%N 8 3138269e767SBrooks Davis.Re 3148269e767SBrooks Davis.Sh STANDARDS 3158269e767SBrooks DavisThe 3168269e767SBrooks Davis.Fn socket 3178269e767SBrooks Davisfunction conforms to 3188269e767SBrooks Davis.St -p1003.1-2008 . 3198269e767SBrooks DavisThe 3208269e767SBrooks Davis.Tn POSIX 3218269e767SBrooks Davisstandard specifies only the 3228269e767SBrooks Davis.Dv AF_INET , 3238269e767SBrooks Davis.Dv AF_INET6 , 3248269e767SBrooks Davisand 3258269e767SBrooks Davis.Dv AF_UNIX 3268269e767SBrooks Davisconstants for address families, and requires the use of 3278269e767SBrooks Davis.Dv AF_* 3288269e767SBrooks Davisconstants for the 3298269e767SBrooks Davis.Fa domain 3308269e767SBrooks Davisargument of 3318269e767SBrooks Davis.Fn socket . 3328269e767SBrooks DavisThe 3338269e767SBrooks Davis.Dv SOCK_CLOEXEC 3348269e767SBrooks Davisflag is expected to conform to the next revision of the 3358269e767SBrooks Davis.Tn POSIX 3368269e767SBrooks Davisstandard. 3378269e767SBrooks DavisThe 3388269e767SBrooks Davis.Dv SOCK_RDM 3398269e767SBrooks Davis.Fa type , 3408269e767SBrooks Davisthe 3418269e767SBrooks Davis.Dv PF_* 3428269e767SBrooks Davisconstants, and other address families are 3438269e767SBrooks Davis.Fx 3448269e767SBrooks Davisextensions. 3458269e767SBrooks Davis.Sh HISTORY 3468269e767SBrooks DavisThe 3478269e767SBrooks Davis.Fn socket 3488269e767SBrooks Davissystem call appeared in 3498269e767SBrooks Davis.Bx 4.2 . 350