Blame man2/socket.2

Packit 7cfc04
'\" t
Packit 7cfc04
.\" Copyright (c) 1983, 1991 The Regents of the University of California.
Packit 7cfc04
.\" All rights reserved.
Packit 7cfc04
.\"
Packit 7cfc04
.\" %%%LICENSE_START(BSD_4_CLAUSE_UCB)
Packit 7cfc04
.\" Redistribution and use in source and binary forms, with or without
Packit 7cfc04
.\" modification, are permitted provided that the following conditions
Packit 7cfc04
.\" are met:
Packit 7cfc04
.\" 1. Redistributions of source code must retain the above copyright
Packit 7cfc04
.\"    notice, this list of conditions and the following disclaimer.
Packit 7cfc04
.\" 2. Redistributions in binary form must reproduce the above copyright
Packit 7cfc04
.\"    notice, this list of conditions and the following disclaimer in the
Packit 7cfc04
.\"    documentation and/or other materials provided with the distribution.
Packit 7cfc04
.\" 3. All advertising materials mentioning features or use of this software
Packit 7cfc04
.\"    must display the following acknowledgement:
Packit 7cfc04
.\"	This product includes software developed by the University of
Packit 7cfc04
.\"	California, Berkeley and its contributors.
Packit 7cfc04
.\" 4. Neither the name of the University nor the names of its contributors
Packit 7cfc04
.\"    may be used to endorse or promote products derived from this software
Packit 7cfc04
.\"    without specific prior written permission.
Packit 7cfc04
.\"
Packit 7cfc04
.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
Packit 7cfc04
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Packit 7cfc04
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Packit 7cfc04
.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
Packit 7cfc04
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
Packit 7cfc04
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
Packit 7cfc04
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
Packit 7cfc04
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
Packit 7cfc04
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
Packit 7cfc04
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
Packit 7cfc04
.\" SUCH DAMAGE.
Packit 7cfc04
.\" %%%LICENSE_END
Packit 7cfc04
.\"
Packit 7cfc04
.\"     $Id: socket.2,v 1.4 1999/05/13 11:33:42 freitag Exp $
Packit 7cfc04
.\"
Packit 7cfc04
.\" Modified 1993-07-24 by Rik Faith <faith@cs.unc.edu>
Packit 7cfc04
.\" Modified 1996-10-22 by Eric S. Raymond <esr@thyrsus.com>
Packit 7cfc04
.\" Modified 1998, 1999 by Andi Kleen <ak@muc.de>
Packit 7cfc04
.\" Modified 2002-07-17 by Michael Kerrisk <mtk.manpages@gmail.com>
Packit 7cfc04
.\" Modified 2004-06-17 by Michael Kerrisk <mtk.manpages@gmail.com>
Packit 7cfc04
.\"
Packit 7cfc04
.TH SOCKET 2 2017-09-15 "Linux" "Linux Programmer's Manual"
Packit 7cfc04
.SH NAME
Packit 7cfc04
socket \- create an endpoint for communication
Packit 7cfc04
.SH SYNOPSIS
Packit 7cfc04
.BR "#include <sys/types.h>" "          /* See NOTES */"
Packit 7cfc04
.br
Packit 7cfc04
.B #include <sys/socket.h>
Packit 7cfc04
.PP
Packit 7cfc04
.BI "int socket(int " domain ", int " type ", int " protocol );
Packit 7cfc04
.SH DESCRIPTION
Packit 7cfc04
.BR socket ()
Packit 7cfc04
creates an endpoint for communication and returns a file descriptor
Packit 7cfc04
that refers to that endpoint.
Packit 7cfc04
The file descriptor returned by a successful call will be
Packit 7cfc04
the lowest-numbered file descriptor not currently open for the process.
Packit 7cfc04
.PP
Packit 7cfc04
The
Packit 7cfc04
.I domain
Packit 7cfc04
argument specifies a communication domain; this selects the protocol
Packit 7cfc04
family which will be used for communication.
Packit 7cfc04
These families are defined in
Packit 7cfc04
.IR <sys/socket.h> .
Packit 7cfc04
The currently understood formats include:
Packit 7cfc04
.TS
Packit 7cfc04
tab(:);
Packit 7cfc04
l l l.
Packit 7cfc04
Name:Purpose:Man page
Packit 7cfc04
T{
Packit 7cfc04
.BR AF_UNIX ", " AF_LOCAL
Packit 7cfc04
T}:T{
Packit 7cfc04
Local communication
Packit 7cfc04
T}:T{
Packit 7cfc04
.BR unix (7)
Packit 7cfc04
T}
Packit 7cfc04
T{
Packit 7cfc04
.B AF_INET
Packit 7cfc04
T}:IPv4 Internet protocols:T{
Packit 7cfc04
.BR ip (7)
Packit 7cfc04
T}
Packit 7cfc04
T{
Packit 7cfc04
.B AF_INET6
Packit 7cfc04
T}:IPv6 Internet protocols:T{
Packit 7cfc04
.BR ipv6 (7)
Packit 7cfc04
T}
Packit 7cfc04
T{
Packit 7cfc04
.B AF_IPX
Packit 7cfc04
T}:IPX \- Novell protocols:
Packit 7cfc04
T{
Packit 7cfc04
.B AF_NETLINK
Packit 7cfc04
T}:T{
Packit 7cfc04
Kernel user interface device
Packit 7cfc04
T}:T{
Packit 7cfc04
.BR netlink (7)
Packit 7cfc04
T}
Packit 7cfc04
T{
Packit 7cfc04
.B AF_X25
Packit 7cfc04
T}:ITU-T X.25 / ISO-8208 protocol:T{
Packit 7cfc04
.BR x25 (7)
Packit 7cfc04
T}
Packit 7cfc04
T{
Packit 7cfc04
.B AF_AX25
Packit 7cfc04
T}:T{
Packit 7cfc04
Amateur radio AX.25 protocol
Packit 7cfc04
T}:
Packit 7cfc04
T{
Packit 7cfc04
.B AF_ATMPVC
Packit 7cfc04
T}:Access to raw ATM PVCs:
Packit 7cfc04
T{
Packit 7cfc04
.B AF_APPLETALK
Packit 7cfc04
T}:AppleTalk:T{
Packit 7cfc04
.BR ddp (7)
Packit 7cfc04
T}
Packit 7cfc04
T{
Packit 7cfc04
.B AF_PACKET
Packit 7cfc04
T}:T{
Packit 7cfc04
Low level packet interface
Packit 7cfc04
T}:T{
Packit 7cfc04
.BR packet (7)
Packit 7cfc04
T}
Packit 7cfc04
T{
Packit 7cfc04
.B AF_ALG
Packit 7cfc04
T}:T{
Packit 7cfc04
Interface to kernel crypto API
Packit 7cfc04
T}
Packit 7cfc04
.TE
Packit 7cfc04
.PP
Packit 7cfc04
The socket has the indicated
Packit 7cfc04
.IR type ,
Packit 7cfc04
which specifies the communication semantics.
Packit 7cfc04
Currently defined types
Packit 7cfc04
are:
Packit 7cfc04
.TP 16
Packit 7cfc04
.B SOCK_STREAM
Packit 7cfc04
Provides sequenced, reliable, two-way, connection-based byte streams.
Packit 7cfc04
An out-of-band data transmission mechanism may be supported.
Packit 7cfc04
.TP
Packit 7cfc04
.B SOCK_DGRAM
Packit 7cfc04
Supports datagrams (connectionless, unreliable messages of a fixed
Packit 7cfc04
maximum length).
Packit 7cfc04
.TP
Packit 7cfc04
.B SOCK_SEQPACKET
Packit 7cfc04
Provides a sequenced, reliable, two-way connection-based data
Packit 7cfc04
transmission path for datagrams of fixed maximum length; a consumer is
Packit 7cfc04
required to read an entire packet with each input system call.
Packit 7cfc04
.TP
Packit 7cfc04
.B SOCK_RAW
Packit 7cfc04
Provides raw network protocol access.
Packit 7cfc04
.TP
Packit 7cfc04
.B SOCK_RDM
Packit 7cfc04
Provides a reliable datagram layer that does not guarantee ordering.
Packit 7cfc04
.TP
Packit 7cfc04
.B SOCK_PACKET
Packit 7cfc04
Obsolete and should not be used in new programs;
Packit 7cfc04
see
Packit 7cfc04
.BR packet (7).
Packit 7cfc04
.PP
Packit 7cfc04
Some socket types may not be implemented by all protocol families.
Packit 7cfc04
.PP
Packit 7cfc04
Since Linux 2.6.27, the
Packit 7cfc04
.I type
Packit 7cfc04
argument serves a second purpose:
Packit 7cfc04
in addition to specifying a socket type,
Packit 7cfc04
it may include the bitwise OR of any of the following values,
Packit 7cfc04
to modify the behavior of
Packit 7cfc04
.BR socket ():
Packit 7cfc04
.TP 16
Packit 7cfc04
.B SOCK_NONBLOCK
Packit 7cfc04
Set the
Packit 7cfc04
.BR O_NONBLOCK
Packit 7cfc04
file status flag on the new open file description.
Packit 7cfc04
Using this flag saves extra calls to
Packit 7cfc04
.BR fcntl (2)
Packit 7cfc04
to achieve the same result.
Packit 7cfc04
.TP
Packit 7cfc04
.B SOCK_CLOEXEC
Packit 7cfc04
Set the close-on-exec
Packit 7cfc04
.RB ( FD_CLOEXEC )
Packit 7cfc04
flag on the new file descriptor.
Packit 7cfc04
See the description of the
Packit 7cfc04
.B O_CLOEXEC
Packit 7cfc04
flag in
Packit 7cfc04
.BR open (2)
Packit 7cfc04
for reasons why this may be useful.
Packit 7cfc04
.PP
Packit 7cfc04
The
Packit 7cfc04
.I protocol
Packit 7cfc04
specifies a particular protocol to be used with the socket.
Packit 7cfc04
Normally only a single protocol exists to support a particular
Packit 7cfc04
socket type within a given protocol family, in which case
Packit 7cfc04
.I protocol
Packit 7cfc04
can be specified as 0.
Packit 7cfc04
However, it is possible that many protocols may exist, in
Packit 7cfc04
which case a particular protocol must be specified in this manner.
Packit 7cfc04
The protocol number to use is specific to the \*(lqcommunication domain\*(rq
Packit 7cfc04
in which communication is to take place; see
Packit 7cfc04
.BR protocols (5).
Packit 7cfc04
See
Packit 7cfc04
.BR getprotoent (3)
Packit 7cfc04
on how to map protocol name strings to protocol numbers.
Packit 7cfc04
.PP
Packit 7cfc04
Sockets of type
Packit 7cfc04
.B SOCK_STREAM
Packit 7cfc04
are full-duplex byte streams.
Packit 7cfc04
They do not preserve
Packit 7cfc04
record boundaries.
Packit 7cfc04
A stream socket must be in
Packit 7cfc04
a
Packit 7cfc04
.I connected
Packit 7cfc04
state before any data may be sent or received on it.
Packit 7cfc04
A connection to
Packit 7cfc04
another socket is created with a
Packit 7cfc04
.BR connect (2)
Packit 7cfc04
call.
Packit 7cfc04
Once connected, data may be transferred using
Packit 7cfc04
.BR read (2)
Packit 7cfc04
and
Packit 7cfc04
.BR write (2)
Packit 7cfc04
calls or some variant of the
Packit 7cfc04
.BR send (2)
Packit 7cfc04
and
Packit 7cfc04
.BR recv (2)
Packit 7cfc04
calls.
Packit 7cfc04
When a session has been completed a
Packit 7cfc04
.BR close (2)
Packit 7cfc04
may be performed.
Packit 7cfc04
Out-of-band data may also be transmitted as described in
Packit 7cfc04
.BR send (2)
Packit 7cfc04
and received as described in
Packit 7cfc04
.BR recv (2).
Packit 7cfc04
.PP
Packit 7cfc04
The communications protocols which implement a
Packit 7cfc04
.B SOCK_STREAM
Packit 7cfc04
ensure that data is not lost or duplicated.
Packit 7cfc04
If a piece of data for which
Packit 7cfc04
the peer protocol has buffer space cannot be successfully transmitted
Packit 7cfc04
within a reasonable length of time, then the connection is considered
Packit 7cfc04
to be dead.
Packit 7cfc04
When
Packit 7cfc04
.B SO_KEEPALIVE
Packit 7cfc04
is enabled on the socket the protocol checks in a protocol-specific
Packit 7cfc04
manner if the other end is still alive.
Packit 7cfc04
A
Packit 7cfc04
.B SIGPIPE
Packit 7cfc04
signal is raised if a process sends or receives
Packit 7cfc04
on a broken stream; this causes naive processes,
Packit 7cfc04
which do not handle the signal, to exit.
Packit 7cfc04
.B SOCK_SEQPACKET
Packit 7cfc04
sockets employ the same system calls as
Packit 7cfc04
.B SOCK_STREAM
Packit 7cfc04
sockets.
Packit 7cfc04
The only difference is that
Packit 7cfc04
.BR read (2)
Packit 7cfc04
calls will return only the amount of data requested,
Packit 7cfc04
and any data remaining in the arriving packet will be discarded.
Packit 7cfc04
Also all message boundaries in incoming datagrams are preserved.
Packit 7cfc04
.PP
Packit 7cfc04
.B SOCK_DGRAM
Packit 7cfc04
and
Packit 7cfc04
.B SOCK_RAW
Packit 7cfc04
sockets allow sending of datagrams to correspondents named in
Packit 7cfc04
.BR sendto (2)
Packit 7cfc04
calls.
Packit 7cfc04
Datagrams are generally received with
Packit 7cfc04
.BR recvfrom (2),
Packit 7cfc04
which returns the next datagram along with the address of its sender.
Packit 7cfc04
.PP
Packit 7cfc04
.B SOCK_PACKET
Packit 7cfc04
is an obsolete socket type to receive raw packets directly from the
Packit 7cfc04
device driver.
Packit 7cfc04
Use
Packit 7cfc04
.BR packet (7)
Packit 7cfc04
instead.
Packit 7cfc04
.PP
Packit 7cfc04
An
Packit 7cfc04
.BR fcntl (2)
Packit 7cfc04
.B F_SETOWN
Packit 7cfc04
operation can be used to specify a process or process group to receive a
Packit 7cfc04
.B SIGURG
Packit 7cfc04
signal when the out-of-band data arrives or
Packit 7cfc04
.B SIGPIPE
Packit 7cfc04
signal when a
Packit 7cfc04
.B SOCK_STREAM
Packit 7cfc04
connection breaks unexpectedly.
Packit 7cfc04
This operation may also be used to set the process or process group
Packit 7cfc04
that receives the I/O and asynchronous notification of I/O events via
Packit 7cfc04
.BR SIGIO .
Packit 7cfc04
Using
Packit 7cfc04
.B F_SETOWN
Packit 7cfc04
is equivalent to an
Packit 7cfc04
.BR ioctl (2)
Packit 7cfc04
call with the
Packit 7cfc04
.B FIOSETOWN
Packit 7cfc04
or
Packit 7cfc04
.B SIOCSPGRP
Packit 7cfc04
argument.
Packit 7cfc04
.PP
Packit 7cfc04
When the network signals an error condition to the protocol module (e.g.,
Packit 7cfc04
using an ICMP message for IP) the pending error flag is set for the socket.
Packit 7cfc04
The next operation on this socket will return the error code of the pending
Packit 7cfc04
error.
Packit 7cfc04
For some protocols it is possible to enable a per-socket error queue
Packit 7cfc04
to retrieve detailed information about the error; see
Packit 7cfc04
.B IP_RECVERR
Packit 7cfc04
in
Packit 7cfc04
.BR ip (7).
Packit 7cfc04
.PP
Packit 7cfc04
The operation of sockets is controlled by socket level
Packit 7cfc04
.IR options .
Packit 7cfc04
These options are defined in
Packit 7cfc04
.IR <sys/socket.h> .
Packit 7cfc04
The functions
Packit 7cfc04
.BR setsockopt (2)
Packit 7cfc04
and
Packit 7cfc04
.BR getsockopt (2)
Packit 7cfc04
are used to set and get options, respectively.
Packit 7cfc04
.SH RETURN VALUE
Packit 7cfc04
On success, a file descriptor for the new socket is returned.
Packit 7cfc04
On error, \-1 is returned, and
Packit 7cfc04
.I errno
Packit 7cfc04
is set appropriately.
Packit 7cfc04
.SH ERRORS
Packit 7cfc04
.TP
Packit 7cfc04
.B EACCES
Packit 7cfc04
Permission to create a socket of the specified type and/or protocol
Packit 7cfc04
is denied.
Packit 7cfc04
.TP
Packit 7cfc04
.B EAFNOSUPPORT
Packit 7cfc04
The implementation does not support the specified address family.
Packit 7cfc04
.TP
Packit 7cfc04
.B EINVAL
Packit 7cfc04
Unknown protocol, or protocol family not available.
Packit 7cfc04
.TP
Packit 7cfc04
.B EINVAL
Packit 7cfc04
.\" Since Linux 2.6.27
Packit 7cfc04
Invalid flags in
Packit 7cfc04
.IR type .
Packit 7cfc04
.TP
Packit 7cfc04
.B EMFILE
Packit 7cfc04
The per-process limit on the number of open file descriptors has been reached.
Packit 7cfc04
.TP
Packit 7cfc04
.B ENFILE
Packit 7cfc04
The system-wide limit on the total number of open files has been reached.
Packit 7cfc04
.TP
Packit 7cfc04
.BR ENOBUFS " or " ENOMEM
Packit 7cfc04
Insufficient memory is available.
Packit 7cfc04
The socket cannot be
Packit 7cfc04
created until sufficient resources are freed.
Packit 7cfc04
.TP
Packit 7cfc04
.B EPROTONOSUPPORT
Packit 7cfc04
The protocol type or the specified protocol is not
Packit 7cfc04
supported within this domain.
Packit 7cfc04
.PP
Packit 7cfc04
Other errors may be generated by the underlying protocol modules.
Packit 7cfc04
.SH CONFORMING TO
Packit 7cfc04
POSIX.1-2001, POSIX.1-2008, 4.4BSD.
Packit 7cfc04
.PP
Packit 7cfc04
The
Packit 7cfc04
.B SOCK_NONBLOCK
Packit 7cfc04
and
Packit 7cfc04
.B SOCK_CLOEXEC
Packit 7cfc04
flags are Linux-specific.
Packit 7cfc04
.PP
Packit 7cfc04
.BR socket ()
Packit 7cfc04
appeared in 4.2BSD.
Packit 7cfc04
It is generally portable to/from
Packit 7cfc04
non-BSD systems supporting clones of the BSD socket layer (including
Packit 7cfc04
System\ V variants).
Packit 7cfc04
.SH NOTES
Packit 7cfc04
POSIX.1 does not require the inclusion of
Packit 7cfc04
.IR <sys/types.h> ,
Packit 7cfc04
and this header file is not required on Linux.
Packit 7cfc04
However, some historical (BSD) implementations required this header
Packit 7cfc04
file, and portable applications are probably wise to include it.
Packit 7cfc04
.PP
Packit 7cfc04
The manifest constants used under 4.x BSD for protocol families
Packit 7cfc04
are
Packit 7cfc04
.BR PF_UNIX ,
Packit 7cfc04
.BR PF_INET ,
Packit 7cfc04
and so on, while
Packit 7cfc04
.BR AF_UNIX ,
Packit 7cfc04
.BR AF_INET ,
Packit 7cfc04
and so on are used for address
Packit 7cfc04
families.
Packit 7cfc04
However, already the BSD man page promises: "The protocol
Packit 7cfc04
family generally is the same as the address family", and subsequent
Packit 7cfc04
standards use AF_* everywhere.
Packit 7cfc04
.PP
Packit 7cfc04
The
Packit 7cfc04
.B AF_ALG
Packit 7cfc04
protocol type was added in Linux 2.6.38.
Packit 7cfc04
More information on this interface is provided with the kernel HTML
Packit 7cfc04
documentation at
Packit 7cfc04
.IR https://www.kernel.org/doc/htmldocs/crypto\-API/User.html .
Packit 7cfc04
.SH EXAMPLE
Packit 7cfc04
An example of the use of
Packit 7cfc04
.BR socket ()
Packit 7cfc04
is shown in
Packit 7cfc04
.BR getaddrinfo (3).
Packit 7cfc04
.SH SEE ALSO
Packit 7cfc04
.BR accept (2),
Packit 7cfc04
.BR bind (2),
Packit 7cfc04
.BR close (2),
Packit 7cfc04
.BR connect (2),
Packit 7cfc04
.BR fcntl (2),
Packit 7cfc04
.BR getpeername (2),
Packit 7cfc04
.BR getsockname (2),
Packit 7cfc04
.BR getsockopt (2),
Packit 7cfc04
.BR ioctl (2),
Packit 7cfc04
.BR listen (2),
Packit 7cfc04
.BR read (2),
Packit 7cfc04
.BR recv (2),
Packit 7cfc04
.BR select (2),
Packit 7cfc04
.BR send (2),
Packit 7cfc04
.BR shutdown (2),
Packit 7cfc04
.BR socketpair (2),
Packit 7cfc04
.BR write (2),
Packit 7cfc04
.BR getprotoent (3),
Packit 7cfc04
.BR ip (7),
Packit 7cfc04
.BR socket (7),
Packit 7cfc04
.BR tcp (7),
Packit 7cfc04
.BR udp (7),
Packit 7cfc04
.BR unix (7)
Packit 7cfc04
.PP
Packit 7cfc04
\(lqAn Introductory 4.3BSD Interprocess Communication Tutorial\(rq
Packit 7cfc04
and
Packit 7cfc04
\(lqBSD Interprocess Communication Tutorial\(rq,
Packit 7cfc04
reprinted in
Packit 7cfc04
.I UNIX Programmer's Supplementary Documents Volume 1.
Packit 7cfc04
.SH COLOPHON
Packit 7cfc04
This page is part of release 4.15 of the Linux
Packit 7cfc04
.I man-pages
Packit 7cfc04
project.
Packit 7cfc04
A description of the project,
Packit 7cfc04
information about reporting bugs,
Packit 7cfc04
and the latest version of this page,
Packit 7cfc04
can be found at
Packit 7cfc04
\%https://www.kernel.org/doc/man\-pages/.