1SOCKET(7)                  Linux Programmer's Manual                 SOCKET(7)
2
3
4

NAME

6       socket - Linux socket interface
7

SYNOPSIS

9       #include <sys/socket.h>
10
11       sockfd = socket(int socket_family, int socket_type, int protocol);
12

DESCRIPTION

14       This  manual  page  describes  the  Linux  networking socket layer user
15       interface.  The  BSD  compatible  sockets  are  the  uniform  interface
16       between the user process and the network protocol stacks in the kernel.
17       The protocol modules are grouped into protocol families  like  AF_INET,
18       AF_IPX, AF_PACKET and socket types like SOCK_STREAM or SOCK_DGRAM.  See
19       socket(2) for more information on families and types.
20
21   Socket Layer Functions
22       These functions are used by the user process to send or receive packets
23       and  to  do  other  socket  operations.  For more information see their
24       respective manual pages.
25
26       socket(2) creates a socket, connect(2) connects a socket  to  a  remote
27       socket  address,  the bind(2) function binds a socket to a local socket
28       address, listen(2) tells the  socket  that  new  connections  shall  be
29       accepted, and accept(2) is used to get a new socket with a new incoming
30       connection.  socketpair(2)  returns  two  connected  anonymous  sockets
31       (only implemented for a few local families like AF_UNIX)
32
33       send(2),  sendto(2),  and  sendmsg(2)  send  data  over  a  socket, and
34       recv(2), recvfrom(2), recvmsg(2) receive data from a  socket.   poll(2)
35       and  select(2)  wait for arriving data or a readiness to send data.  In
36       addition, the standard I/O operations like write(2),  writev(2),  send‐
37       file(2), read(2), and readv(2) can be used to read and write data.
38
39       getsockname(2)  returns  the  local  socket  address and getpeername(2)
40       returns the remote socket address.  getsockopt(2) and setsockopt(2) are
41       used  to  set or get socket layer or protocol options.  ioctl(2) can be
42       used to set or read some other options.
43
44       close(2) is used to close a socket.   shutdown(2)  closes  parts  of  a
45       full-duplex socket connection.
46
47       Seeking,  or  calling  pread(2) or pwrite(2) with a nonzero position is
48       not supported on sockets.
49
50       It is possible to do nonblocking I/O on sockets by setting  the  O_NON‐
51       BLOCK flag on a socket file descriptor using fcntl(2).  Then all opera‐
52       tions that would block will (usually)  return  with  EAGAIN  (operation
53       should  be  retried  later);  connect(2) will return EINPROGRESS error.
54       The user can then wait for various events via poll(2) or select(2).
55
56       ┌────────────────────────────────────────────────────────────────────┐
57       │                            I/O events                              │
58       ├───────────┬───────────┬────────────────────────────────────────────┤
59       │Event      │ Poll flag │ Occurrence                                 │
60       ├───────────┼───────────┼────────────────────────────────────────────┤
61       │Read       │ POLLIN    │ New data arrived.                          │
62       ├───────────┼───────────┼────────────────────────────────────────────┤
63       │Read       │ POLLIN    │ A connection setup has been completed (for │
64       │           │           │ connection-oriented sockets)               │
65       ├───────────┼───────────┼────────────────────────────────────────────┤
66       │Read       │ POLLHUP   │ A disconnection request has been initiated │
67       │           │           │ by the other end.                          │
68       ├───────────┼───────────┼────────────────────────────────────────────┤
69       │Read       │ POLLHUP   │ A connection is broken (only  for  connec‐ │
70       │           │           │ tion-oriented protocols).  When the socket │
71       │           │           │ is written SIGPIPE is also sent.           │
72       ├───────────┼───────────┼────────────────────────────────────────────┤
73       │Write      │ POLLOUT   │ Socket has enough send  buffer  space  for │
74       │           │           │ writing new data.                          │
75       ├───────────┼───────────┼────────────────────────────────────────────┤
76       │Read/Write │ POLLIN|   │ An outgoing connect(2) finished.           │
77       │           │ POLLOUT   │                                            │
78       ├───────────┼───────────┼────────────────────────────────────────────┤
79       │Read/Write │ POLLERR   │ An asynchronous error occurred.            │
80       ├───────────┼───────────┼────────────────────────────────────────────┤
81       │Read/Write │ POLLHUP   │ The other end has shut down one direction. │
82       ├───────────┼───────────┼────────────────────────────────────────────┤
83       │Exception  │ POLLPRI   │ Urgent data arrived.  SIGURG is sent then. │
84       └───────────┴───────────┴────────────────────────────────────────────┘
85
86       An alternative to poll(2) and select(2) is to let the kernel inform the
87       application about events via a SIGIO signal.  For that the O_ASYNC flag
88       must be set on a socket file descriptor via fcntl(2) and a valid signal
89       handler for SIGIO must be installed via sigaction(2).  See the  Signals
90       discussion below.
91
92   Socket Options
93       These  socket  options  can be set by using setsockopt(2) and read with
94       getsockopt(2) with the socket level set to SOL_SOCKET for all sockets:
95
96       SO_ACCEPTCONN
97              Returns a value indicating whether or not this socket  has  been
98              marked  to accept connections with listen(2).  The value 0 indi‐
99              cates that this is not a listening socket, the value 1 indicates
100              that  this  is  a listening socket.  This socket option is read-
101              only.
102
103       SO_BINDTODEVICE
104              Bind this socket to a particular device like “eth0”,  as  speci‐
105              fied  in  the  passed  interface  name.  If the name is an empty
106              string or the option length is zero, the socket  device  binding
107              is  removed.  The passed option is a variable-length null-termi‐
108              nated interface name string with the maximum size  of  IFNAMSIZ.
109              If a socket is bound to an interface, only packets received from
110              that particular interface are processed  by  the  socket.   Note
111              that this only works for some socket types, particularly AF_INET
112              sockets.  It is not supported for  packet  sockets  (use  normal
113              bind(8) there).
114
115       SO_BROADCAST
116              Set  or  get the broadcast flag.  When enabled, datagram sockets
117              receive packets sent to a broadcast address and they are allowed
118              to  send  packets  to  a  broadcast address.  This option has no
119              effect on stream-oriented sockets.
120
121       SO_BSDCOMPAT
122              Enable BSD bug-to-bug compatibility.  This is used  by  the  UDP
123              protocol  module  in  Linux 2.0 and 2.2.  If enabled ICMP errors
124              received for a UDP socket will not be passed to  the  user  pro‐
125              gram.   In  later  kernel  versions, support for this option has
126              been phased out: Linux 2.4 silently ignores it,  and  Linux  2.6
127              generates  a  kernel  warning  (printk()) if a program uses this
128              option.  Linux 2.0 also  enabled  BSD  bug-to-bug  compatibility
129              options (random header changing, skipping of the broadcast flag)
130              for raw sockets with this option, but that was removed in  Linux
131              2.2.
132
133       SO_DEBUG
134              Enable  socket  debugging.   Only allowed for processes with the
135              CAP_NET_ADMIN capability or an effective user ID of 0.
136
137       SO_DOMAIN (since Linux 2.6.32)
138              Retrieves the socket domain as an  integer,  returning  a  value
139              such  as  AF_INET6.   See  socket(2)  for  details.  This socket
140              option is read-only.
141
142       SO_ERROR
143              Get and clear the pending socket error.  This socket  option  is
144              read-only.  Expects an integer.
145
146       SO_DONTROUTE
147              Don't send via a gateway, only send to directly connected hosts.
148              The same effect can be achieved  by  setting  the  MSG_DONTROUTE
149              flag  on a socket send(2) operation.  Expects an integer boolean
150              flag.
151
152       SO_KEEPALIVE
153              Enable sending of  keep-alive  messages  on  connection-oriented
154              sockets.  Expects an integer boolean flag.
155
156       SO_LINGER
157              Sets  or  gets  the  SO_LINGER option.  The argument is a linger
158              structure.
159
160                  struct linger {
161                      int l_onoff;    /* linger active */
162                      int l_linger;   /* how many seconds to linger for */
163                  };
164
165              When enabled, a close(2) or shutdown(2) will  not  return  until
166              all  queued  messages for the socket have been successfully sent
167              or the linger timeout has been  reached.   Otherwise,  the  call
168              returns  immediately  and the closing is done in the background.
169              When the socket is closed as part of exit(2), it always  lingers
170              in the background.
171
172       SO_OOBINLINE
173              If  this  option is enabled, out-of-band data is directly placed
174              into the receive data stream.   Otherwise  out-of-band  data  is
175              only passed when the MSG_OOB flag is set during receiving.
176
177       SO_PASSCRED
178              Enable  or  disable the receiving of the SCM_CREDENTIALS control
179              message.  For more information see unix(7).
180
181       SO_PEERCRED
182              Return the credentials of the foreign process connected to  this
183              socket.   This  is  only  possible  for connected AF_UNIX stream
184              sockets and AF_UNIX stream and  datagram  socket  pairs  created
185              using  socketpair(2); see unix(7).  The returned credentials are
186              those that were in effect at the time of the call to  connect(2)
187              or  socketpair(2).   Argument is a ucred structure.  This socket
188              option is read-only.
189
190       SO_PRIORITY
191              Set the protocol-defined priority for all packets to be sent  on
192              this  socket.   Linux  uses  this  value to order the networking
193              queues: packets with a higher priority may  be  processed  first
194              depending  on  the  selected  device  queueing  discipline.  For
195              ip(7), this also sets the IP  type-of-service  (TOS)  field  for
196              outgoing  packets.   Setting a priority outside the range 0 to 6
197              requires the CAP_NET_ADMIN capability.
198
199       SO_PROTOCOL (since Linux 2.6.32)
200              Retrieves the socket protocol as an integer, returning  a  value
201              such  as  IPPROTO_SCTP.  See socket(2) for details.  This socket
202              option is read-only.
203
204       SO_RCVBUF
205              Sets or gets the maximum socket receive buffer  in  bytes.   The
206              kernel  doubles this value (to allow space for bookkeeping over‐
207              head) when it is set using setsockopt(2), and this doubled value
208              is  returned  by getsockopt(2).  The default value is set by the
209              /proc/sys/net/core/rmem_default file, and  the  maximum  allowed
210              value is set by the /proc/sys/net/core/rmem_max file.  The mini‐
211              mum (doubled) value for this option is 256.
212
213       SO_RCVBUFFORCE (since Linux 2.6.14)
214              Using this socket option, a privileged  (CAP_NET_ADMIN)  process
215              can  perform  the same task as SO_RCVBUF, but the rmem_max limit
216              can be overridden.
217
218       SO_RCVLOWAT and SO_SNDLOWAT
219              Specify the minimum number of bytes  in  the  buffer  until  the
220              socket layer will pass the data to the protocol (SO_SNDLOWAT) or
221              the user on receiving (SO_RCVLOWAT).  These two values are  ini‐
222              tialized to 1.  SO_SNDLOWAT is not changeable on Linux (setsock‐
223              opt(2)  fails  with  the  error  ENOPROTOOPT).   SO_RCVLOWAT  is
224              changeable only since Linux 2.4.  The select(2) and poll(2) sys‐
225              tem calls currently do not respect the  SO_RCVLOWAT  setting  on
226              Linux,  and  mark  a  socket readable when even a single byte of
227              data is available.  A subsequent read from the socket will block
228              until SO_RCVLOWAT bytes are available.
229
230       SO_RCVTIMEO and SO_SNDTIMEO
231              Specify  the  receiving  or  sending timeouts until reporting an
232              error.  The argument is a struct timeval.  If an input or output
233              function  blocks for this period of time, and data has been sent
234              or received, the return value  of  that  function  will  be  the
235              amount  of data transferred; if no data has been transferred and
236              the timeout has been reached then -1 is returned with errno  set
237              to  EAGAIN or EWOULDBLOCK just as if the socket was specified to
238              be nonblocking.  If the timeout is set  to  zero  (the  default)
239              then  the  operation  will  never  timeout.   Timeouts only have
240              effect for system calls that perform socket I/O (e.g.,  read(2),
241              recvmsg(2),  send(2),  sendmsg(2));  timeouts have no effect for
242              select(2), poll(2), epoll_wait(2), etc.
243
244       SO_REUSEADDR
245              Indicates that the rules used in validating  addresses  supplied
246              in  a  bind(2)  call should allow reuse of local addresses.  For
247              AF_INET sockets this means that a socket may bind,  except  when
248              there  is an active listening socket bound to the address.  When
249              the listening socket is bound to INADDR_ANY with a specific port
250              then  it  is  not  possible  to  bind to this port for any local
251              address.  Argument is an integer boolean flag.
252
253       SO_SNDBUF
254              Sets or gets the maximum socket send buffer in bytes.  The  ker‐
255              nel doubles this value (to allow space for bookkeeping overhead)
256              when it is set using setsockopt(2), and this  doubled  value  is
257              returned  by  getsockopt(2).   The  default  value is set by the
258              /proc/sys/net/core/wmem_default file  and  the  maximum  allowed
259              value is set by the /proc/sys/net/core/wmem_max file.  The mini‐
260              mum (doubled) value for this option is 2048.
261
262       SO_SNDBUFFORCE (since Linux 2.6.14)
263              Using this socket option, a privileged  (CAP_NET_ADMIN)  process
264              can  perform  the same task as SO_SNDBUF, but the wmem_max limit
265              can be overridden.
266
267       SO_TIMESTAMP
268              Enable or disable the receiving of the SO_TIMESTAMP control mes‐
269              sage.    The  timestamp  control  message  is  sent  with  level
270              SOL_SOCKET and the cmsg_data field is a struct timeval  indicat‐
271              ing  the reception time of the last packet passed to the user in
272              this call.  See cmsg(3) for details on control messages.
273
274       SO_TYPE
275              Gets the socket type as an integer  (e.g.,  SOCK_STREAM).   This
276              socket option is read-only.
277
278   Signals
279       When  writing onto a connection-oriented socket that has been shut down
280       (by the local or the remote end) SIGPIPE is sent to the writing process
281       and  EPIPE  is  returned.   The  signal is not sent when the write call
282       specified the MSG_NOSIGNAL flag.
283
284       When requested with the FIOSETOWN fcntl(2) or SIOCSPGRP ioctl(2), SIGIO
285       is  sent  when  an  I/O event occurs.  It is possible to use poll(2) or
286       select(2) in the signal handler to find  out  which  socket  the  event
287       occurred  on.  An alternative (in Linux 2.2) is to set a real-time sig‐
288       nal using the F_SETSIG fcntl(2); the handler of the  real  time  signal
289       will  be called with the file descriptor in the si_fd field of its sig‐
290       info_t.  See fcntl(2) for more information.
291
292       Under some circumstances (e.g., multiple processes accessing  a  single
293       socket),  the  condition  that caused the SIGIO may have already disap‐
294       peared when the process reacts to the signal.   If  this  happens,  the
295       process should wait again because Linux will resend the signal later.
296
297   /proc interfaces
298       The  core socket networking parameters can be accessed via files in the
299       directory /proc/sys/net/core/.
300
301       rmem_default
302              contains the default setting in bytes of the socket receive buf‐
303              fer.
304
305       rmem_max
306              contains the maximum socket receive buffer size in bytes which a
307              user may set by using the SO_RCVBUF socket option.
308
309       wmem_default
310              contains the default setting in bytes of the socket send buffer.
311
312       wmem_max
313              contains the maximum socket send buffer size in  bytes  which  a
314              user may set by using the SO_SNDBUF socket option.
315
316       message_cost and message_burst
317              configure  the  token  bucket  filter used to load limit warning
318              messages caused by external network events.
319
320       netdev_max_backlog
321              Maximum number of packets in the global input queue.
322
323       optmem_max
324              Maximum length of ancillary data and user control data like  the
325              iovecs per socket.
326
327   Ioctls
328       These operations can be accessed using ioctl(2):
329
330           error = ioctl(ip_socket, ioctl_type, &value_result);
331
332       SIOCGSTAMP
333              Return  a  struct timeval with the receive timestamp of the last
334              packet passed to the user.  This is useful  for  accurate  round
335              trip  time  measurements.  See setitimer(2) for a description of
336              struct timeval.  This ioctl should only be used  if  the  socket
337              option  SO_TIMESTAMP  is  not  set on the socket.  Otherwise, it
338              returns the timestamp of the last packet that was received while
339              SO_TIMESTAMP was not set, or it fails if no such packet has been
340              received, (i.e., ioctl(2) returns -1 with errno set to ENOENT).
341
342       SIOCSPGRP
343              Set the process or process group to send SIGIO or SIGURG signals
344              to  when  an  asynchronous  I/O operation has finished or urgent
345              data is available.  The argument is a pointer to  a  pid_t.   If
346              the  argument is positive, send the signals to that process.  If
347              the argument is negative, send the signals to the process  group
348              with  the ID of the absolute value of the argument.  The process
349              may only choose itself or its own process group to receive  sig‐
350              nals  unless  it has the CAP_KILL capability or an effective UID
351              of 0.
352
353       FIOASYNC
354              Change the O_ASYNC flag to enable or  disable  asynchronous  I/O
355              mode  of the socket.  Asynchronous I/O mode means that the SIGIO
356              signal or the signal set with F_SETSIG is raised when a new  I/O
357              event occurs.
358
359              Argument is an integer boolean flag.  (This operation is synony‐
360              mous with the use of fcntl(2) to set the O_ASYNC flag.)
361
362       SIOCGPGRP
363              Get the current process or process group that receives SIGIO  or
364              SIGURG signals, or 0 when none is set.
365
366       Valid fcntl(2) operations:
367
368       FIOGETOWN
369              The same as the SIOCGPGRP ioctl(2).
370
371       FIOSETOWN
372              The same as the SIOCSPGRP ioctl(2).
373

VERSIONS

375       SO_BINDTODEVICE  was introduced in Linux 2.0.30.  SO_PASSCRED is new in
376       Linux 2.2.  The /proc interfaces was introduced in Linux 2.2.   SO_RCV‐
377       TIMEO and SO_SNDTIMEO are supported since Linux 2.3.41.  Earlier, time‐
378       outs were fixed to a protocol-specific setting, and could not  be  read
379       or written.
380

NOTES

382       Linux assumes that half of the send/receive buffer is used for internal
383       kernel structures; thus the values in the corresponding /proc files are
384       twice what can be observed on the wire.
385
386       Linux will only allow port reuse with the SO_REUSEADDR option when this
387       option was set both in the previous program that performed a bind(2) to
388       the port and in the program that wants to reuse the port.  This differs
389       from some implementations (e.g., FreeBSD) where only the later  program
390       needs  to  set  the  SO_REUSEADDR option.  Typically this difference is
391       invisible, since, for example, a server program is designed  to  always
392       set this option.
393

BUGS

395       The  CONFIG_FILTER socket options SO_ATTACH_FILTER and SO_DETACH_FILTER
396       are not documented.  The suggested interface to use  them  is  via  the
397       libpcap library.
398

SEE ALSO

400       getsockopt(2),   setsockopt(2),   socket(2),  capabilities(7),  ddp(7),
401       ip(7), packet(7), tcp(7), udp(7), unix(7)
402

COLOPHON

404       This page is part of release 3.25 of the Linux  man-pages  project.   A
405       description  of  the project, and information about reporting bugs, can
406       be found at http://www.kernel.org/doc/man-pages/.
407
408
409
410Linux                             2010-06-13                         SOCKET(7)
Impressum