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