1.\" $NetBSD: socket.2,v 1.37 2011/06/26 16:42:41 christos Exp $ 2.\" 3.\" Copyright (c) 1983, 1991, 1993 4.\" The Regents of the University of California. All rights reserved. 5.\" 6.\" Redistribution and use in source and binary forms, with or without 7.\" modification, are permitted provided that the following conditions 8.\" are met: 9.\" 1. Redistributions of source code must retain the above copyright 10.\" notice, this list of conditions and the following disclaimer. 11.\" 2. Redistributions in binary form must reproduce the above copyright 12.\" notice, this list of conditions and the following disclaimer in the 13.\" documentation and/or other materials provided with the distribution. 14.\" 3. Neither the name of the University nor the names of its contributors 15.\" may be used to endorse or promote products derived from this software 16.\" without specific prior written permission. 17.\" 18.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 19.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 22.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28.\" SUCH DAMAGE. 29.\" 30.\" @(#)socket.2 8.1 (Berkeley) 6/4/93 31.\" 32.Dd September 6, 2007 33.Dt SOCKET 2 34.Os 35.Sh NAME 36.Nm socket 37.Nd create an endpoint for communication 38.Sh LIBRARY 39.Lb libc 40.Sh SYNOPSIS 41.In sys/socket.h 42.Ft int 43.Fn socket "int domain" "int type" "int protocol" 44.Sh DESCRIPTION 45.Fn socket 46creates an endpoint for communication and returns a descriptor. 47.Pp 48The 49.Fa domain 50parameter specifies a communications domain within which 51communication will take place; this selects the protocol family 52which should be used. 53These families are defined in the include file 54.Ao Pa sys/socket.h Ac . 55The currently understood formats are: 56.Pp 57.Bd -literal -offset indent -compact 58PF_LOCAL local (previously UNIX) domain protocols 59PF_INET ARPA Internet protocols 60PF_INET6 IPv6 (Internet Protocol version 6) protocols 61PF_ISO ISO protocols 62PF_NS Xerox Network Systems protocols 63PF_IMPLINK IMP \*(lqhost at IMP\*(rq link layer 64PF_APPLETALK AppleTalk protocols 65PF_BLUETOOTH Bluetooth protocols 66.Ed 67.Pp 68The socket has the indicated 69.Fa type , 70which specifies the semantics of communication. 71Currently defined types are: 72.Pp 73.Bd -literal -offset indent -compact 74SOCK_STREAM 75SOCK_DGRAM 76SOCK_RAW 77SOCK_SEQPACKET 78SOCK_RDM 79.Ed 80.Pp 81The following flags can be or'ed to the type to condition the returned 82file descriptor: 83The following flags are valid: 84.Bl -column SOCK_NONBLOCK -offset indent 85.It Dv SOCK_CLOEXEC 86Set the close on exec property. 87.It Dv SOCK_NONBLOCK 88Sets non-blocking I/O. 89.El 90.Pp 91A 92.Dv SOCK_STREAM 93type provides sequenced, reliable, 94two-way connection based byte streams. 95An out-of-band data transmission mechanism may be supported. 96A 97.Dv SOCK_DGRAM 98socket supports 99datagrams (connectionless, unreliable messages of 100a fixed (typically small) maximum length). 101A 102.Dv SOCK_SEQPACKET 103socket may provide a sequenced, reliable, 104two-way connection-based data transmission path for datagrams 105of fixed maximum length; a consumer may be required to read 106an entire packet with each read system call. 107This facility is protocol specific, and presently implemented 108only for 109.Dv PF_NS . 110.Dv SOCK_RAW 111sockets provide access to internal network protocols and interfaces. 112The types 113.Dv SOCK_RAW , 114which is available only to the super-user, and 115.Dv SOCK_RDM , 116which is planned, 117but not yet implemented, are not described here. 118.Pp 119The 120.Fa protocol 121specifies a particular protocol to be used with the socket. 122Normally only a single protocol exists to support a particular 123socket type within a given protocol family. 124However, it is possible that many protocols may exist, in which case 125a particular protocol must be specified in this manner. 126The protocol number to use is 127particular to the \*(lqcommunication domain\*(rq in which communication 128is to take place; see 129.Xr protocols 5 . 130.Pp 131Sockets of type 132.Dv SOCK_STREAM 133are full-duplex byte streams. 134A stream socket must be in a 135.Em connected 136state before any data may be sent or received 137on it. 138A connection to another socket is created with a 139.Xr connect 2 140call. 141Once connected, data may be transferred using 142.Xr read 2 143and 144.Xr write 2 145calls or some variant of the 146.Xr send 2 147and 148.Xr recv 2 149calls. 150When a session has been completed a 151.Xr close 2 152may be performed. 153Out-of-band data may also be transmitted as described in 154.Xr send 2 155and received as described in 156.Xr recv 2 . 157.Pp 158The communications protocols used to implement a 159.Dv SOCK_STREAM 160ensure that data 161is not lost or duplicated. 162If a piece of data for which the 163peer protocol has buffer space cannot be successfully transmitted 164within a reasonable length of time, then 165the connection is considered broken and calls 166will indicate an error with 167\-1 returns and with 168.Er ETIMEDOUT 169as the specific code 170in the global variable 171.Va errno . 172The protocols optionally keep sockets 173.Dq warm 174by forcing transmissions 175roughly every minute in the absence of other activity. 176An error is then indicated if no response can be 177elicited on an otherwise 178idle connection for an extended period (e.g., 5 minutes). 179A 180.Dv SIGPIPE 181signal is raised if a process sends 182on a broken stream; this causes naive processes, 183which do not handle the signal, to exit. 184.Pp 185.Dv SOCK_SEQPACKET 186sockets employ the same system calls 187as 188.Dv SOCK_STREAM 189sockets. 190The only difference is that 191.Xr read 2 192calls will return only the amount of data requested, 193and any remaining in the arriving packet will be discarded. 194.Pp 195.Dv SOCK_DGRAM 196and 197.Dv SOCK_RAW 198sockets allow sending of datagrams to correspondents 199named in 200.Xr send 2 201calls. 202Datagrams are generally received with 203.Xr recvfrom 2 , 204which returns the next datagram with its return address. 205.Pp 206An 207.Xr fcntl 2 208call can be used to specify a process group to receive 209a 210.Dv SIGURG 211signal when the out-of-band data arrives. 212It may also enable non-blocking I/O 213and asynchronous notification of I/O events 214via 215.Dv SIGIO . 216.Pp 217The operation of sockets is controlled by socket level 218.Em options . 219These options are defined in the file 220.Ao Pa sys/socket.h Ac . 221The 222.Xr setsockopt 2 223and 224.Xr getsockopt 2 225system calls are used to set and get options, respectively. 226.Sh RETURN VALUES 227A \-1 is returned if an error occurs, otherwise the return 228value is a descriptor referencing the socket. 229.Sh ERRORS 230The 231.Fn socket 232call fails if: 233.Bl -tag -width Er 234.It Bq Er EACCES 235Permission to create a socket of the specified type and/or protocol 236is denied. 237.It Bq Er EAFNOSUPPORT 238The address family (domain) is not supported or 239the specified domain is not supported by this protocol family. 240.It Bq Er EMFILE 241The per-process descriptor table is full. 242.It Bq Er ENFILE 243The system file table is full. 244.It Bq Er ENOBUFS 245Insufficient buffer space is available. 246The socket cannot be created until sufficient resources are freed. 247.It Bq Er EPROTONOSUPPORT 248The protocol family is not supported or 249the specified protocol is not supported within this domain. 250.It Bq Er EPROTOTYPE 251The socket type is not supported by the protocol. 252.El 253.Sh SEE ALSO 254.Xr accept 2 , 255.Xr bind 2 , 256.Xr connect 2 , 257.Xr getsockname 2 , 258.Xr getsockopt 2 , 259.Xr ioctl 2 , 260.Xr listen 2 , 261.Xr poll 2 , 262.Xr read 2 , 263.Xr recv 2 , 264.Xr select 2 , 265.Xr send 2 , 266.Xr setsockopt 2 , 267.Xr shutdown 2 , 268.Xr socketpair 2 , 269.Xr write 2 , 270.Xr getprotoent 3 271.Rs 272.%T "An Introductory 4.4BSD Interprocess Communication Tutorial" 273.%A Stuart Sechrest 274.Re 275.Pq see Pa /usr/share/doc/psd/20.ipctut 276.Rs 277.%T "Advanced 4.4BSD IPC Tutorial" 278.%A Samuel J. Leffler 279.%A Robert S. Fabry 280.%A William N. Joy 281.%A Phil Lapsley 282.%A Steve Miller 283.%A Chris Torek 284.Re 285.Pq see Pa /usr/share/doc/psd/21.ipc 286.Sh HISTORY 287The 288.Fn socket 289function call appeared in 290.Bx 4.2 . 291