1*43a90889SApple OSS Distributions.\" $NetBSD: socket.2,v 1.5 1995/02/27 12:37:53 cgd Exp $ 2*43a90889SApple OSS Distributions.\" 3*43a90889SApple OSS Distributions.\" Copyright (c) 1983, 1991, 1993 4*43a90889SApple OSS Distributions.\" The Regents of the University of California. All rights reserved. 5*43a90889SApple OSS Distributions.\" 6*43a90889SApple OSS Distributions.\" Redistribution and use in source and binary forms, with or without 7*43a90889SApple OSS Distributions.\" modification, are permitted provided that the following conditions 8*43a90889SApple OSS Distributions.\" are met: 9*43a90889SApple OSS Distributions.\" 1. Redistributions of source code must retain the above copyright 10*43a90889SApple OSS Distributions.\" notice, this list of conditions and the following disclaimer. 11*43a90889SApple OSS Distributions.\" 2. Redistributions in binary form must reproduce the above copyright 12*43a90889SApple OSS Distributions.\" notice, this list of conditions and the following disclaimer in the 13*43a90889SApple OSS Distributions.\" documentation and/or other materials provided with the distribution. 14*43a90889SApple OSS Distributions.\" 3. All advertising materials mentioning features or use of this software 15*43a90889SApple OSS Distributions.\" must display the following acknowledgement: 16*43a90889SApple OSS Distributions.\" This product includes software developed by the University of 17*43a90889SApple OSS Distributions.\" California, Berkeley and its contributors. 18*43a90889SApple OSS Distributions.\" 4. Neither the name of the University nor the names of its contributors 19*43a90889SApple OSS Distributions.\" may be used to endorse or promote products derived from this software 20*43a90889SApple OSS Distributions.\" without specific prior written permission. 21*43a90889SApple OSS Distributions.\" 22*43a90889SApple OSS Distributions.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23*43a90889SApple OSS Distributions.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24*43a90889SApple OSS Distributions.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25*43a90889SApple OSS Distributions.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26*43a90889SApple OSS Distributions.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27*43a90889SApple OSS Distributions.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28*43a90889SApple OSS Distributions.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29*43a90889SApple OSS Distributions.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30*43a90889SApple OSS Distributions.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31*43a90889SApple OSS Distributions.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32*43a90889SApple OSS Distributions.\" SUCH DAMAGE. 33*43a90889SApple OSS Distributions.\" 34*43a90889SApple OSS Distributions.\" @(#)socket.2 8.1 (Berkeley) 6/4/93 35*43a90889SApple OSS Distributions.\" 36*43a90889SApple OSS Distributions.Dd March 18, 2015 37*43a90889SApple OSS Distributions.Dt SOCKET 2 38*43a90889SApple OSS Distributions.Os 39*43a90889SApple OSS Distributions.Sh NAME 40*43a90889SApple OSS Distributions.Nm socket 41*43a90889SApple OSS Distributions.Nd create an endpoint for communication 42*43a90889SApple OSS Distributions.Sh SYNOPSIS 43*43a90889SApple OSS Distributions.Fd #include <sys/socket.h> 44*43a90889SApple OSS Distributions.Ft int 45*43a90889SApple OSS Distributions.Fo socket 46*43a90889SApple OSS Distributions.Fa "int domain" 47*43a90889SApple OSS Distributions.Fa "int type" 48*43a90889SApple OSS Distributions.Fa "int protocol" 49*43a90889SApple OSS Distributions.Fc 50*43a90889SApple OSS Distributions.Sh DESCRIPTION 51*43a90889SApple OSS Distributions.Fn socket 52*43a90889SApple OSS Distributionscreates an endpoint for communication and returns a descriptor. 53*43a90889SApple OSS Distributions.Pp 54*43a90889SApple OSS DistributionsThe 55*43a90889SApple OSS Distributions.Fa domain 56*43a90889SApple OSS Distributionsparameter specifies a communications domain within which 57*43a90889SApple OSS Distributionscommunication will take place; this selects the protocol family 58*43a90889SApple OSS Distributionswhich should be used. 59*43a90889SApple OSS DistributionsThese families are defined in the include file 60*43a90889SApple OSS Distributions.Ao Pa sys/socket.h Ac . 61*43a90889SApple OSS DistributionsThe currently understood formats are 62*43a90889SApple OSS Distributions.Pp 63*43a90889SApple OSS Distributions.Bd -literal -offset indent -compact 64*43a90889SApple OSS DistributionsPF_LOCAL Host-internal protocols, formerly called PF_UNIX, 65*43a90889SApple OSS DistributionsPF_UNIX Host-internal protocols, deprecated, use PF_LOCAL, 66*43a90889SApple OSS DistributionsPF_INET Internet version 4 protocols, 67*43a90889SApple OSS DistributionsPF_ROUTE Internal Routing protocol, 68*43a90889SApple OSS DistributionsPF_KEY Internal key-management function, 69*43a90889SApple OSS DistributionsPF_INET6 Internet version 6 protocols, 70*43a90889SApple OSS DistributionsPF_SYSTEM System domain, 71*43a90889SApple OSS DistributionsPF_NDRV Raw access to network device, 72*43a90889SApple OSS DistributionsPF_VSOCK VM Sockets protocols 73*43a90889SApple OSS Distributions.Ed 74*43a90889SApple OSS Distributions.Pp 75*43a90889SApple OSS DistributionsThe socket has the indicated 76*43a90889SApple OSS Distributions.Fa type , 77*43a90889SApple OSS Distributionswhich specifies the semantics of communication. Currently 78*43a90889SApple OSS Distributionsdefined types are: 79*43a90889SApple OSS Distributions.Pp 80*43a90889SApple OSS Distributions.Bd -literal -offset indent -compact 81*43a90889SApple OSS DistributionsSOCK_STREAM 82*43a90889SApple OSS DistributionsSOCK_DGRAM 83*43a90889SApple OSS DistributionsSOCK_RAW 84*43a90889SApple OSS Distributions.\"SOCK_SEQPACKET 85*43a90889SApple OSS Distributions.\"SOCK_RDM 86*43a90889SApple OSS Distributions.Ed 87*43a90889SApple OSS Distributions.Pp 88*43a90889SApple OSS DistributionsA 89*43a90889SApple OSS Distributions.Dv SOCK_STREAM 90*43a90889SApple OSS Distributionstype provides sequenced, reliable, 91*43a90889SApple OSS Distributionstwo-way connection based byte streams. 92*43a90889SApple OSS DistributionsAn out-of-band data transmission mechanism may be supported. 93*43a90889SApple OSS DistributionsA 94*43a90889SApple OSS Distributions.Dv SOCK_DGRAM 95*43a90889SApple OSS Distributionssocket supports 96*43a90889SApple OSS Distributionsdatagrams (connectionless, unreliable messages of 97*43a90889SApple OSS Distributionsa fixed (typically small) maximum length). 98*43a90889SApple OSS Distributions.\"A 99*43a90889SApple OSS Distributions.\".Dv SOCK_SEQPACKET 100*43a90889SApple OSS Distributions.\"socket may provide a sequenced, reliable, 101*43a90889SApple OSS Distributions.\"two-way connection-based data transmission path for datagrams 102*43a90889SApple OSS Distributions.\"of fixed maximum length; a consumer may be required to read 103*43a90889SApple OSS Distributions.\"an entire packet with each read system call. 104*43a90889SApple OSS Distributions.\"This facility is protocol specific, and presently implemented 105*43a90889SApple OSS Distributions.\"only for 106*43a90889SApple OSS Distributions.\".Dv PF_NS . 107*43a90889SApple OSS Distributions.Dv SOCK_RAW 108*43a90889SApple OSS Distributionssockets provide access to internal network protocols and interfaces. 109*43a90889SApple OSS DistributionsThe type 110*43a90889SApple OSS Distributions.Dv SOCK_RAW , 111*43a90889SApple OSS Distributionswhich is available only to the super-user. 112*43a90889SApple OSS Distributions.\" , and 113*43a90889SApple OSS Distributions.\" .Dv SOCK_RDM , 114*43a90889SApple OSS Distributions.\" which is planned, 115*43a90889SApple OSS Distributions.\" but not yet implemented, are not described here. 116*43a90889SApple OSS Distributions.Pp 117*43a90889SApple OSS DistributionsThe 118*43a90889SApple OSS Distributions.Fa protocol 119*43a90889SApple OSS Distributionsspecifies a particular protocol to be used with the socket. 120*43a90889SApple OSS DistributionsNormally only a single protocol exists to support a particular 121*43a90889SApple OSS Distributionssocket type within a given protocol family. However, it is possible 122*43a90889SApple OSS Distributionsthat many protocols may exist, in which case a particular protocol 123*43a90889SApple OSS Distributionsmust be specified in this manner. The protocol number to use is 124*43a90889SApple OSS Distributionsparticular to the \*(lqcommunication domain\*(rq in which communication 125*43a90889SApple OSS Distributionsis to take place; see 126*43a90889SApple OSS Distributions.Xr protocols 5 . 127*43a90889SApple OSS Distributions.Pp 128*43a90889SApple OSS DistributionsSockets of type 129*43a90889SApple OSS Distributions.Dv SOCK_STREAM 130*43a90889SApple OSS Distributionsare full-duplex byte streams, similar 131*43a90889SApple OSS Distributionsto pipes. A stream socket must be in a 132*43a90889SApple OSS Distributions.Em connected 133*43a90889SApple OSS Distributionsstate before any data may be sent or received 134*43a90889SApple OSS Distributionson it. A connection to another socket is created with a 135*43a90889SApple OSS Distributions.Xr connect 2 136*43a90889SApple OSS Distributionsor 137*43a90889SApple OSS Distributions.Xr connectx 2 138*43a90889SApple OSS Distributionscall. Once connected, data may be transferred using 139*43a90889SApple OSS Distributions.Xr read 2 140*43a90889SApple OSS Distributionsand 141*43a90889SApple OSS Distributions.Xr write 2 142*43a90889SApple OSS Distributionscalls or some variant of the 143*43a90889SApple OSS Distributions.Xr send 2 144*43a90889SApple OSS Distributionsand 145*43a90889SApple OSS Distributions.Xr recv 2 146*43a90889SApple OSS Distributionscalls. When a session has been completed a 147*43a90889SApple OSS Distributions.Xr close 2 148*43a90889SApple OSS Distributionsmay be performed. 149*43a90889SApple OSS DistributionsOut-of-band data may also be transmitted as described in 150*43a90889SApple OSS Distributions.Xr send 2 151*43a90889SApple OSS Distributionsand received as described in 152*43a90889SApple OSS Distributions.Xr recv 2 . 153*43a90889SApple OSS Distributions.Pp 154*43a90889SApple OSS DistributionsThe communications protocols used to implement a 155*43a90889SApple OSS Distributions.Dv SOCK_STREAM 156*43a90889SApple OSS Distributionsinsure that data 157*43a90889SApple OSS Distributionsis not lost or duplicated. If a piece of data for which the 158*43a90889SApple OSS Distributionspeer protocol has buffer space cannot be successfully transmitted 159*43a90889SApple OSS Distributionswithin a reasonable length of time, then 160*43a90889SApple OSS Distributionsthe connection is considered broken and calls 161*43a90889SApple OSS Distributionswill indicate an error with 162*43a90889SApple OSS Distributions-1 returns and with 163*43a90889SApple OSS Distributions.Dv ETIMEDOUT 164*43a90889SApple OSS Distributionsas the specific code 165*43a90889SApple OSS Distributionsin the global variable 166*43a90889SApple OSS Distributions.Va errno . 167*43a90889SApple OSS DistributionsThe protocols optionally keep sockets 168*43a90889SApple OSS Distributions.Dq warm 169*43a90889SApple OSS Distributionsby forcing transmissions 170*43a90889SApple OSS Distributionsroughly every minute in the absence of other activity. 171*43a90889SApple OSS DistributionsAn error is then indicated if no response can be 172*43a90889SApple OSS Distributionselicited on an otherwise 173*43a90889SApple OSS Distributionsidle connection for a extended period (e.g. 5 minutes). 174*43a90889SApple OSS DistributionsA 175*43a90889SApple OSS Distributions.Dv SIGPIPE 176*43a90889SApple OSS Distributionssignal is raised if a process sends 177*43a90889SApple OSS Distributionson a broken stream; this causes naive processes, 178*43a90889SApple OSS Distributionswhich do not handle the signal, to exit. 179*43a90889SApple OSS Distributions.\" .Pp 180*43a90889SApple OSS Distributions.\" .Dv SOCK_SEQPACKET 181*43a90889SApple OSS Distributions.\" sockets employ the same system calls 182*43a90889SApple OSS Distributions.\" as 183*43a90889SApple OSS Distributions.\" .Dv SOCK_STREAM 184*43a90889SApple OSS Distributions.\" sockets. The only difference 185*43a90889SApple OSS Distributions.\" is that 186*43a90889SApple OSS Distributions.\" .Xr read 2 187*43a90889SApple OSS Distributions.\" calls will return only the amount of data requested, 188*43a90889SApple OSS Distributions.\" and any remaining in the arriving packet will be discarded. 189*43a90889SApple OSS Distributions.Pp 190*43a90889SApple OSS Distributions.Dv SOCK_DGRAM 191*43a90889SApple OSS Distributionsand 192*43a90889SApple OSS Distributions.Dv SOCK_RAW 193*43a90889SApple OSS Distributionssockets allow sending of datagrams to correspondents 194*43a90889SApple OSS Distributionsnamed in 195*43a90889SApple OSS Distributions.Xr send 2 196*43a90889SApple OSS Distributionscalls. Datagrams are generally received with 197*43a90889SApple OSS Distributions.Xr recvfrom 2 , 198*43a90889SApple OSS Distributionswhich returns the next datagram with its return address. 199*43a90889SApple OSS Distributions.Pp 200*43a90889SApple OSS DistributionsAn 201*43a90889SApple OSS Distributions.Xr fcntl 2 202*43a90889SApple OSS Distributionscall can be used to specify a process group to receive 203*43a90889SApple OSS Distributionsa 204*43a90889SApple OSS Distributions.Dv SIGURG 205*43a90889SApple OSS Distributionssignal when the out-of-band data arrives. 206*43a90889SApple OSS DistributionsIt may also enable non-blocking I/O 207*43a90889SApple OSS Distributionsand asynchronous notification of I/O events 208*43a90889SApple OSS Distributionsvia 209*43a90889SApple OSS Distributions.Dv SIGIO . 210*43a90889SApple OSS Distributions.Pp 211*43a90889SApple OSS DistributionsThe operation of sockets is controlled by socket level 212*43a90889SApple OSS Distributions.Em options . 213*43a90889SApple OSS DistributionsThese options are defined in the file 214*43a90889SApple OSS Distributions.Ao Pa sys/socket.h Ac . 215*43a90889SApple OSS Distributions.Xr Setsockopt 2 216*43a90889SApple OSS Distributionsand 217*43a90889SApple OSS Distributions.Xr getsockopt 2 218*43a90889SApple OSS Distributionsare used to set and get options, respectively. 219*43a90889SApple OSS Distributions.Sh RETURN VALUES 220*43a90889SApple OSS DistributionsA -1 is returned if an error occurs, otherwise the return 221*43a90889SApple OSS Distributionsvalue is a descriptor referencing the socket. 222*43a90889SApple OSS Distributions.Sh ERRORS 223*43a90889SApple OSS DistributionsThe 224*43a90889SApple OSS Distributions.Fn socket 225*43a90889SApple OSS Distributionssystem call fails if: 226*43a90889SApple OSS Distributions.Bl -tag -width Er 227*43a90889SApple OSS Distributions.\" =========== 228*43a90889SApple OSS Distributions.It Bq Er EACCES 229*43a90889SApple OSS DistributionsPermission to create a socket of the specified type and/or protocol 230*43a90889SApple OSS Distributionsis denied. 231*43a90889SApple OSS Distributions.\" =========== 232*43a90889SApple OSS Distributions.It Bq Er EAFNOSUPPORT 233*43a90889SApple OSS DistributionsThe specified address family is not supported. 234*43a90889SApple OSS Distributions.\" =========== 235*43a90889SApple OSS Distributions.It Bq Er EMFILE 236*43a90889SApple OSS DistributionsThe per-process descriptor table is full. 237*43a90889SApple OSS Distributions.\" =========== 238*43a90889SApple OSS Distributions.It Bq Er ENFILE 239*43a90889SApple OSS DistributionsThe system file table is full. 240*43a90889SApple OSS Distributions.\" =========== 241*43a90889SApple OSS Distributions.It Bq Er ENOBUFS 242*43a90889SApple OSS DistributionsInsufficient buffer space is available. 243*43a90889SApple OSS DistributionsThe socket cannot be created until sufficient resources are freed. 244*43a90889SApple OSS Distributions.\" =========== 245*43a90889SApple OSS Distributions.It Bq Er ENOMEM 246*43a90889SApple OSS DistributionsInsufficient memory was available to fulfill the request. 247*43a90889SApple OSS Distributions.\" =========== 248*43a90889SApple OSS Distributions.It Bq Er EPROTONOSUPPORT 249*43a90889SApple OSS DistributionsThe protocol type or the specified protocol is not supported 250*43a90889SApple OSS Distributionswithin this domain. 251*43a90889SApple OSS Distributions.\" =========== 252*43a90889SApple OSS Distributions.It Bq Er EPROTOTYPE 253*43a90889SApple OSS DistributionsThe socket type is not supported by the protocol. 254*43a90889SApple OSS Distributions.El 255*43a90889SApple OSS Distributions.Pp 256*43a90889SApple OSS DistributionsIf a new protocol family is defined, 257*43a90889SApple OSS Distributionsthe socreate process is free to return any desired error code. 258*43a90889SApple OSS DistributionsThe 259*43a90889SApple OSS Distributions.Fn socket 260*43a90889SApple OSS Distributionssystem call will pass this error code along 261*43a90889SApple OSS Distributions(even if it is undefined). 262*43a90889SApple OSS Distributions.Sh LEGACY SYNOPSIS 263*43a90889SApple OSS Distributions.Fd #include <sys/types.h> 264*43a90889SApple OSS Distributions.Fd #include <sys/socket.h> 265*43a90889SApple OSS Distributions.Pp 266*43a90889SApple OSS DistributionsThe include file 267*43a90889SApple OSS Distributions.In sys/types.h 268*43a90889SApple OSS Distributionsis necessary. 269*43a90889SApple OSS Distributions.Sh SEE ALSO 270*43a90889SApple OSS Distributions.Xr accept 2 , 271*43a90889SApple OSS Distributions.Xr bind 2 , 272*43a90889SApple OSS Distributions.Xr connect 2 , 273*43a90889SApple OSS Distributions.Xr connectx 2 , 274*43a90889SApple OSS Distributions.Xr disconnectx 2 , 275*43a90889SApple OSS Distributions.Xr getsockname 2 , 276*43a90889SApple OSS Distributions.Xr getsockopt 2 , 277*43a90889SApple OSS Distributions.Xr ioctl 2 , 278*43a90889SApple OSS Distributions.Xr listen 2 , 279*43a90889SApple OSS Distributions.Xr read 2 , 280*43a90889SApple OSS Distributions.Xr recv 2 , 281*43a90889SApple OSS Distributions.Xr select 2 , 282*43a90889SApple OSS Distributions.Xr send 2 , 283*43a90889SApple OSS Distributions.Xr shutdown 2 , 284*43a90889SApple OSS Distributions.Xr socketpair 2 , 285*43a90889SApple OSS Distributions.Xr write 2 , 286*43a90889SApple OSS Distributions.Xr getprotoent 3 , 287*43a90889SApple OSS Distributions.Xr inet 4 , 288*43a90889SApple OSS Distributions.Xr inet6 4 , 289*43a90889SApple OSS Distributions.Xr unix 4 , 290*43a90889SApple OSS Distributions.Xr compat 5 291*43a90889SApple OSS Distributions.Rs 292*43a90889SApple OSS Distributions.%T "An Introductory 4.3 BSD Interprocess Communication Tutorial" 293*43a90889SApple OSS Distributions.%O "reprinted in UNIX Programmer's Supplementary Documents Volume 1" 294*43a90889SApple OSS Distributions.Re 295*43a90889SApple OSS Distributions.Rs 296*43a90889SApple OSS Distributions.%T "BSD Interprocess Communication Tutorial" 297*43a90889SApple OSS Distributions.%O "reprinted in UNIX Programmer's Supplementary Documents Volume 1" 298*43a90889SApple OSS Distributions.Re 299*43a90889SApple OSS Distributions.Sh HISTORY 300*43a90889SApple OSS DistributionsThe 301*43a90889SApple OSS Distributions.Fn socket 302*43a90889SApple OSS Distributionsfunction call appeared in 303*43a90889SApple OSS Distributions.Bx 4.2 . 304