1*84d9c625SLionel Sambuc.\" $NetBSD: socket.2,v 1.41 2013/03/01 18:25:16 joerg Exp $ 22fe8fb19SBen Gras.\" 32fe8fb19SBen Gras.\" Copyright (c) 1983, 1991, 1993 42fe8fb19SBen Gras.\" The Regents of the University of California. All rights reserved. 52fe8fb19SBen Gras.\" 62fe8fb19SBen Gras.\" Redistribution and use in source and binary forms, with or without 72fe8fb19SBen Gras.\" modification, are permitted provided that the following conditions 82fe8fb19SBen Gras.\" are met: 92fe8fb19SBen Gras.\" 1. Redistributions of source code must retain the above copyright 102fe8fb19SBen Gras.\" notice, this list of conditions and the following disclaimer. 112fe8fb19SBen Gras.\" 2. Redistributions in binary form must reproduce the above copyright 122fe8fb19SBen Gras.\" notice, this list of conditions and the following disclaimer in the 132fe8fb19SBen Gras.\" documentation and/or other materials provided with the distribution. 142fe8fb19SBen Gras.\" 3. Neither the name of the University nor the names of its contributors 152fe8fb19SBen Gras.\" may be used to endorse or promote products derived from this software 162fe8fb19SBen Gras.\" without specific prior written permission. 172fe8fb19SBen Gras.\" 182fe8fb19SBen Gras.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 192fe8fb19SBen Gras.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 202fe8fb19SBen Gras.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 212fe8fb19SBen Gras.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 222fe8fb19SBen Gras.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 232fe8fb19SBen Gras.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 242fe8fb19SBen Gras.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 252fe8fb19SBen Gras.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 262fe8fb19SBen Gras.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 272fe8fb19SBen Gras.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 282fe8fb19SBen Gras.\" SUCH DAMAGE. 292fe8fb19SBen Gras.\" 302fe8fb19SBen Gras.\" @(#)socket.2 8.1 (Berkeley) 6/4/93 312fe8fb19SBen Gras.\" 32*84d9c625SLionel Sambuc.Dd February 5, 2013 332fe8fb19SBen Gras.Dt SOCKET 2 342fe8fb19SBen Gras.Os 352fe8fb19SBen Gras.Sh NAME 362fe8fb19SBen Gras.Nm socket 372fe8fb19SBen Gras.Nd create an endpoint for communication 382fe8fb19SBen Gras.Sh LIBRARY 392fe8fb19SBen Gras.Lb libc 402fe8fb19SBen Gras.Sh SYNOPSIS 412fe8fb19SBen Gras.In sys/socket.h 422fe8fb19SBen Gras.Ft int 432fe8fb19SBen Gras.Fn socket "int domain" "int type" "int protocol" 442fe8fb19SBen Gras.Sh DESCRIPTION 452fe8fb19SBen Gras.Fn socket 462fe8fb19SBen Grascreates an endpoint for communication and returns a descriptor. 472fe8fb19SBen Gras.Pp 482fe8fb19SBen GrasThe 492fe8fb19SBen Gras.Fa domain 502fe8fb19SBen Grasparameter specifies a communications domain within which 512fe8fb19SBen Grascommunication will take place; this selects the protocol family 522fe8fb19SBen Graswhich should be used. 532fe8fb19SBen GrasThese families are defined in the include file 542fe8fb19SBen Gras.Ao Pa sys/socket.h Ac . 552fe8fb19SBen GrasThe currently understood formats are: 562fe8fb19SBen Gras.Pp 572fe8fb19SBen Gras.Bd -literal -offset indent -compact 582fe8fb19SBen GrasPF_LOCAL local (previously UNIX) domain protocols 592fe8fb19SBen GrasPF_INET ARPA Internet protocols 602fe8fb19SBen GrasPF_INET6 IPv6 (Internet Protocol version 6) protocols 612fe8fb19SBen GrasPF_NS Xerox Network Systems protocols 622fe8fb19SBen GrasPF_APPLETALK AppleTalk protocols 632fe8fb19SBen GrasPF_BLUETOOTH Bluetooth protocols 642fe8fb19SBen Gras.Ed 652fe8fb19SBen Gras.Pp 662fe8fb19SBen GrasThe socket has the indicated 672fe8fb19SBen Gras.Fa type , 682fe8fb19SBen Graswhich specifies the semantics of communication. 692fe8fb19SBen GrasCurrently defined types are: 702fe8fb19SBen Gras.Pp 712fe8fb19SBen Gras.Bd -literal -offset indent -compact 722fe8fb19SBen GrasSOCK_STREAM 732fe8fb19SBen GrasSOCK_DGRAM 742fe8fb19SBen GrasSOCK_RAW 752fe8fb19SBen GrasSOCK_SEQPACKET 762fe8fb19SBen GrasSOCK_RDM 772fe8fb19SBen Gras.Ed 782fe8fb19SBen Gras.Pp 79f14fb602SLionel SambucThe following flags can be or'ed to the type to condition the returned 80f14fb602SLionel Sambucfile descriptor: 81f14fb602SLionel SambucThe following flags are valid: 82f14fb602SLionel Sambuc.Bl -column SOCK_NONBLOCK -offset indent 83f14fb602SLionel Sambuc.It Dv SOCK_CLOEXEC 84f14fb602SLionel SambucSet the close on exec property. 85f14fb602SLionel Sambuc.It Dv SOCK_NONBLOCK 86f14fb602SLionel SambucSets non-blocking I/O. 87f14fb602SLionel Sambuc.It Dv SOCK_NOSIGPIPE 88f14fb602SLionel SambucReturn 89f14fb602SLionel Sambuc.Er EPIPE 90f14fb602SLionel Sambucinstead of raising 91f14fb602SLionel Sambuc.Dv SIGPIPE . 92f14fb602SLionel Sambuc.El 93f14fb602SLionel Sambuc.Pp 942fe8fb19SBen GrasA 952fe8fb19SBen Gras.Dv SOCK_STREAM 962fe8fb19SBen Grastype provides sequenced, reliable, 972fe8fb19SBen Grastwo-way connection based byte streams. 982fe8fb19SBen GrasAn out-of-band data transmission mechanism may be supported. 992fe8fb19SBen GrasA 1002fe8fb19SBen Gras.Dv SOCK_DGRAM 1012fe8fb19SBen Grassocket supports 1022fe8fb19SBen Grasdatagrams (connectionless, unreliable messages of 1032fe8fb19SBen Grasa fixed (typically small) maximum length). 1042fe8fb19SBen GrasA 1052fe8fb19SBen Gras.Dv SOCK_SEQPACKET 1062fe8fb19SBen Grassocket may provide a sequenced, reliable, 1072fe8fb19SBen Grastwo-way connection-based data transmission path for datagrams 1082fe8fb19SBen Grasof fixed maximum length; a consumer may be required to read 1092fe8fb19SBen Grasan entire packet with each read system call. 1102fe8fb19SBen GrasThis facility is protocol specific, and presently implemented 1112fe8fb19SBen Grasonly for 1122fe8fb19SBen Gras.Dv PF_NS . 1132fe8fb19SBen Gras.Dv SOCK_RAW 1142fe8fb19SBen Grassockets provide access to internal network protocols and interfaces. 1152fe8fb19SBen GrasThe types 1162fe8fb19SBen Gras.Dv SOCK_RAW , 1172fe8fb19SBen Graswhich is available only to the super-user, and 1182fe8fb19SBen Gras.Dv SOCK_RDM , 1192fe8fb19SBen Graswhich is planned, 1202fe8fb19SBen Grasbut not yet implemented, are not described here. 1212fe8fb19SBen Gras.Pp 1222fe8fb19SBen GrasThe 1232fe8fb19SBen Gras.Fa protocol 1242fe8fb19SBen Grasspecifies a particular protocol to be used with the socket. 1252fe8fb19SBen GrasNormally only a single protocol exists to support a particular 1262fe8fb19SBen Grassocket type within a given protocol family. 1272fe8fb19SBen GrasHowever, it is possible that many protocols may exist, in which case 1282fe8fb19SBen Grasa particular protocol must be specified in this manner. 1292fe8fb19SBen GrasThe protocol number to use is 1302fe8fb19SBen Grasparticular to the \*(lqcommunication domain\*(rq in which communication 1312fe8fb19SBen Grasis to take place; see 1322fe8fb19SBen Gras.Xr protocols 5 . 1332fe8fb19SBen Gras.Pp 1342fe8fb19SBen GrasSockets of type 1352fe8fb19SBen Gras.Dv SOCK_STREAM 1362fe8fb19SBen Grasare full-duplex byte streams. 1372fe8fb19SBen GrasA stream socket must be in a 1382fe8fb19SBen Gras.Em connected 1392fe8fb19SBen Grasstate before any data may be sent or received 1402fe8fb19SBen Grason it. 1412fe8fb19SBen GrasA connection to another socket is created with a 1422fe8fb19SBen Gras.Xr connect 2 1432fe8fb19SBen Grascall. 1442fe8fb19SBen GrasOnce connected, data may be transferred using 1452fe8fb19SBen Gras.Xr read 2 1462fe8fb19SBen Grasand 1472fe8fb19SBen Gras.Xr write 2 1482fe8fb19SBen Grascalls or some variant of the 1492fe8fb19SBen Gras.Xr send 2 1502fe8fb19SBen Grasand 1512fe8fb19SBen Gras.Xr recv 2 1522fe8fb19SBen Grascalls. 1532fe8fb19SBen GrasWhen a session has been completed a 1542fe8fb19SBen Gras.Xr close 2 1552fe8fb19SBen Grasmay be performed. 1562fe8fb19SBen GrasOut-of-band data may also be transmitted as described in 1572fe8fb19SBen Gras.Xr send 2 1582fe8fb19SBen Grasand received as described in 1592fe8fb19SBen Gras.Xr recv 2 . 1602fe8fb19SBen Gras.Pp 1612fe8fb19SBen GrasThe communications protocols used to implement a 1622fe8fb19SBen Gras.Dv SOCK_STREAM 1632fe8fb19SBen Grasensure that data 1642fe8fb19SBen Grasis not lost or duplicated. 1652fe8fb19SBen GrasIf a piece of data for which the 1662fe8fb19SBen Graspeer protocol has buffer space cannot be successfully transmitted 1672fe8fb19SBen Graswithin a reasonable length of time, then 1682fe8fb19SBen Grasthe connection is considered broken and calls 1692fe8fb19SBen Graswill indicate an error with 1702fe8fb19SBen Gras\-1 returns and with 1712fe8fb19SBen Gras.Er ETIMEDOUT 1722fe8fb19SBen Grasas the specific code 1732fe8fb19SBen Grasin the global variable 1742fe8fb19SBen Gras.Va errno . 1752fe8fb19SBen GrasThe protocols optionally keep sockets 1762fe8fb19SBen Gras.Dq warm 1772fe8fb19SBen Grasby forcing transmissions 1782fe8fb19SBen Grasroughly every minute in the absence of other activity. 1792fe8fb19SBen GrasAn error is then indicated if no response can be 1802fe8fb19SBen Graselicited on an otherwise 1812fe8fb19SBen Grasidle connection for an extended period (e.g., 5 minutes). 1822fe8fb19SBen GrasA 1832fe8fb19SBen Gras.Dv SIGPIPE 1842fe8fb19SBen Grassignal is raised if a process sends 1852fe8fb19SBen Grason a broken stream; this causes naive processes, 1862fe8fb19SBen Graswhich do not handle the signal, to exit. 1872fe8fb19SBen Gras.Pp 1882fe8fb19SBen Gras.Dv SOCK_SEQPACKET 1892fe8fb19SBen Grassockets employ the same system calls 1902fe8fb19SBen Grasas 1912fe8fb19SBen Gras.Dv SOCK_STREAM 1922fe8fb19SBen Grassockets. 1932fe8fb19SBen GrasThe only difference is that 1942fe8fb19SBen Gras.Xr read 2 1952fe8fb19SBen Grascalls will return only the amount of data requested, 1962fe8fb19SBen Grasand any remaining in the arriving packet will be discarded. 1972fe8fb19SBen Gras.Pp 1982fe8fb19SBen Gras.Dv SOCK_DGRAM 1992fe8fb19SBen Grasand 2002fe8fb19SBen Gras.Dv SOCK_RAW 2012fe8fb19SBen Grassockets allow sending of datagrams to correspondents 2022fe8fb19SBen Grasnamed in 2032fe8fb19SBen Gras.Xr send 2 2042fe8fb19SBen Grascalls. 2052fe8fb19SBen GrasDatagrams are generally received with 2062fe8fb19SBen Gras.Xr recvfrom 2 , 2072fe8fb19SBen Graswhich returns the next datagram with its return address. 2082fe8fb19SBen Gras.Pp 2092fe8fb19SBen GrasAn 2102fe8fb19SBen Gras.Xr fcntl 2 2112fe8fb19SBen Grascall can be used to specify a process group to receive 2122fe8fb19SBen Grasa 2132fe8fb19SBen Gras.Dv SIGURG 2142fe8fb19SBen Grassignal when the out-of-band data arrives. 2152fe8fb19SBen GrasIt may also enable non-blocking I/O 2162fe8fb19SBen Grasand asynchronous notification of I/O events 2172fe8fb19SBen Grasvia 2182fe8fb19SBen Gras.Dv SIGIO . 2192fe8fb19SBen Gras.Pp 2202fe8fb19SBen GrasThe operation of sockets is controlled by socket level 2212fe8fb19SBen Gras.Em options . 2222fe8fb19SBen GrasThese options are defined in the file 2232fe8fb19SBen Gras.Ao Pa sys/socket.h Ac . 2242fe8fb19SBen GrasThe 2252fe8fb19SBen Gras.Xr setsockopt 2 2262fe8fb19SBen Grasand 2272fe8fb19SBen Gras.Xr getsockopt 2 2282fe8fb19SBen Grassystem calls are used to set and get options, respectively. 2292fe8fb19SBen Gras.Sh RETURN VALUES 2302fe8fb19SBen GrasA \-1 is returned if an error occurs, otherwise the return 2312fe8fb19SBen Grasvalue is a descriptor referencing the socket. 2322fe8fb19SBen Gras.Sh ERRORS 2332fe8fb19SBen GrasThe 2342fe8fb19SBen Gras.Fn socket 2352fe8fb19SBen Grascall fails if: 2362fe8fb19SBen Gras.Bl -tag -width Er 2372fe8fb19SBen Gras.It Bq Er EACCES 2382fe8fb19SBen GrasPermission to create a socket of the specified type and/or protocol 2392fe8fb19SBen Grasis denied. 2402fe8fb19SBen Gras.It Bq Er EAFNOSUPPORT 2412fe8fb19SBen GrasThe address family (domain) is not supported or 2422fe8fb19SBen Grasthe specified domain is not supported by this protocol family. 2432fe8fb19SBen Gras.It Bq Er EMFILE 2442fe8fb19SBen GrasThe per-process descriptor table is full. 2452fe8fb19SBen Gras.It Bq Er ENFILE 2462fe8fb19SBen GrasThe system file table is full. 2472fe8fb19SBen Gras.It Bq Er ENOBUFS 2482fe8fb19SBen GrasInsufficient buffer space is available. 2492fe8fb19SBen GrasThe socket cannot be created until sufficient resources are freed. 2502fe8fb19SBen Gras.It Bq Er EPROTONOSUPPORT 2512fe8fb19SBen GrasThe protocol family is not supported or 2522fe8fb19SBen Grasthe specified protocol is not supported within this domain. 2532fe8fb19SBen Gras.It Bq Er EPROTOTYPE 2542fe8fb19SBen GrasThe socket type is not supported by the protocol. 2552fe8fb19SBen Gras.El 2562fe8fb19SBen Gras.Sh SEE ALSO 2572fe8fb19SBen Gras.Xr accept 2 , 2582fe8fb19SBen Gras.Xr bind 2 , 2592fe8fb19SBen Gras.Xr connect 2 , 2602fe8fb19SBen Gras.Xr getsockname 2 , 2612fe8fb19SBen Gras.Xr getsockopt 2 , 2622fe8fb19SBen Gras.Xr ioctl 2 , 2632fe8fb19SBen Gras.Xr listen 2 , 2642fe8fb19SBen Gras.Xr poll 2 , 2652fe8fb19SBen Gras.Xr read 2 , 2662fe8fb19SBen Gras.Xr recv 2 , 2672fe8fb19SBen Gras.Xr select 2 , 2682fe8fb19SBen Gras.Xr send 2 , 2692fe8fb19SBen Gras.Xr setsockopt 2 , 2702fe8fb19SBen Gras.Xr shutdown 2 , 2712fe8fb19SBen Gras.Xr socketpair 2 , 2722fe8fb19SBen Gras.Xr write 2 , 2732fe8fb19SBen Gras.Xr getprotoent 3 2742fe8fb19SBen Gras.Rs 2752fe8fb19SBen Gras.%T "An Introductory 4.4BSD Interprocess Communication Tutorial" 2762fe8fb19SBen Gras.%A Stuart Sechrest 2772fe8fb19SBen Gras.Re 2782fe8fb19SBen Gras.Pq see Pa /usr/share/doc/psd/20.ipctut 2792fe8fb19SBen Gras.Rs 2802fe8fb19SBen Gras.%T "Advanced 4.4BSD IPC Tutorial" 2812fe8fb19SBen Gras.%A Samuel J. Leffler 2822fe8fb19SBen Gras.%A Robert S. Fabry 2832fe8fb19SBen Gras.%A William N. Joy 2842fe8fb19SBen Gras.%A Phil Lapsley 2852fe8fb19SBen Gras.%A Steve Miller 2862fe8fb19SBen Gras.%A Chris Torek 2872fe8fb19SBen Gras.Re 2882fe8fb19SBen Gras.Pq see Pa /usr/share/doc/psd/21.ipc 2892fe8fb19SBen Gras.Sh HISTORY 2902fe8fb19SBen GrasThe 2912fe8fb19SBen Gras.Fn socket 2922fe8fb19SBen Grasfunction call appeared in 2932fe8fb19SBen Gras.Bx 4.2 . 294