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       mysocket = socket(int socket_family, int socket_type, int protocol);
11
12

DESCRIPTION

14       This  manual  page  describes  the  Linux  networking socket layer user
15       interface. The BSD compatible sockets are the uniform interface between
16       the  user  process  and the network protocol stacks in the kernel.  The
17       protocol modules are  grouped  into  protocol  families  like  PF_INET,
18       PF_IPX, PF_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

23       These functions are used by the user process to send or receive packets
24       and  to  do  other  socket  operations.  For more information see their
25       respective manual pages.
26
27       socket(2) creates a socket, connect(2) connects a socket  to  a  remote
28       socket  address,  the bind(2) function binds a socket to a local socket
29       address, listen(2) tells the  socket  that  new  connections  shall  be
30       accepted, and accept(2) is used to get a new socket with a new incoming
31       connection.  socketpair(2)  returns  two  connected  anonymous  sockets
32       (only implemented for a few local families like PF_UNIX)
33
34       send(2),  sendto(2),  and  sendmsg(2)  send  data  over  a  socket, and
35       recv(2), recvfrom(2), recvmsg(2) receive data from a  socket.   poll(2)
36       and  select(2)  wait for arriving data or a readiness to send data.  In
37       addition, the standard I/O operations like write(2),  writev(2),  send‐
38       file(2), read(2), and readv(2) can be used to read and write data.
39
40       getsockname(2)  returns  the  local  socket  address and getpeername(2)
41       returns the remote socket address.  getsockopt(2) and setsockopt(2) are
42       used  to  set or get socket layer or protocol options.  ioctl(2) can be
43       used to set or read some other options.
44
45       close(2) is used to close a socket.  shutdown(2) closes parts of a full
46       duplex socket connection.
47
48       Seeking,  or  calling pread(2) or pwrite(2) with a non-zero position is
49       not supported on sockets.
50
51       It is possible to do non-blocking I/O on sockets by setting the  O_NON‐
52       BLOCK flag on a socket file descriptor using fcntl(2).  Then all opera‐
53       tions that would block will (usually)  return  with  EAGAIN  (operation
54       should  be  retried  later);  connect(2) will return EINPROGRESS error.
55       The user can then wait for various events via poll(2) or select(2).
56
57       ┌────────────────────────────────────────────────────────────────────┐
58       │                            I/O events                              │
59       ├───────────┬───────────┬────────────────────────────────────────────┤
60       │Event      │ Poll flag │ Occurrence                                 │
61       ├───────────┼───────────┼────────────────────────────────────────────┤
62       │Read       │ POLLIN    │ New data arrived.                          │
63       ├───────────┼───────────┼────────────────────────────────────────────┤
64       │Read       │ POLLIN    │ A connection setup has been completed (for │
65       │           │           │ connection-oriented sockets)               │
66       ├───────────┼───────────┼────────────────────────────────────────────┤
67       │Read       │ POLLHUP   │ A disconnection request has been initiated │
68       │           │           │ by the other end.                          │
69       ├───────────┼───────────┼────────────────────────────────────────────┤
70       │Read       │ POLLHUP   │ A connection is broken (only  for  connec‐ │
71       │           │           │ tion-oriented protocols).  When the socket │
72       │           │           │ is written SIGPIPE is also sent.           │
73       ├───────────┼───────────┼────────────────────────────────────────────┤
74       │Write      │ POLLOUT   │ Socket has enough send  buffer  space  for │
75       │           │           │ writing new data.                          │
76       ├───────────┼───────────┼────────────────────────────────────────────┤
77       │Read/Write │ POLLIN|   │ An outgoing connect(2) finished.           │
78       │           │ POLLOUT   │                                            │
79       ├───────────┼───────────┼────────────────────────────────────────────┤
80       │Read/Write │ POLLERR   │ An asynchronous error occurred.            │
81       ├───────────┼───────────┼────────────────────────────────────────────┤
82       │Read/Write │ POLLHUP   │ The other end has shut down one direction. │
83       ├───────────┼───────────┼────────────────────────────────────────────┤
84       │Exception  │ POLLPRI   │ Urgent data arrived.  SIGURG is sent then. │
85       └───────────┴───────────┴────────────────────────────────────────────┘
86
87       An  alternative  to poll() and select() is to let the kernel inform the
88       application about events via a SIGIO signal. For that the  FASYNC  flag
89       must be set on a socket file descriptor via fcntl(2) and a valid signal
90       handler for SIGIO must be installed via sigaction(2).  See the  SIGNALS
91       discussion below.
92

SOCKET OPTIONS

94       These  socket  options  can be set by using setsockopt(2) and read with
95       getsockopt(2) with the socket level set to SOL_SOCKET for all sockets:
96
97       SO_ACCEPTCONN
98              Returns a value indicating whether or not this socket  has  been
99              marked  to  accept connections with listen().  The value 0 indi‐
100              cates that this is not a listening socket, the value 1 indicates
101              that this is a listening socket.  Can only be read with getsock‐
102              opt().
103
104       SO_BINDTODEVICE
105              Bind this socket to a particular device like “eth0”,  as  speci‐
106              fied  in  the  passed  interface  name.  If the name is an empty
107              string or the option length is zero, the socket  device  binding
108              is  removed.  The passed option is a variable-length null termi‐
109              nated interface name string with the maximum size  of  IFNAMSIZ.
110              If a socket is bound to an interface, only packets received from
111              that particular interface are processed by the socket. Note that
112              this  only  works  for  some  socket types, particularly AF_INET
113              sockets. It is not supported  for  packet  sockets  (use  normal
114              bind(8) there).
115
116       SO_BROADCAST
117              Set  or  get  the broadcast flag. When enabled, datagram sockets
118              receive packets sent to a broadcast address and they are allowed
119              to  send  packets  to  a  broadcast address.  This option has no
120              effect on stream-oriented sockets.
121
122       SO_BSDCOMPAT
123              Enable BSD bug-to-bug compatibility.  This is used  by  the  UDP
124              protocol  module  in  Linux 2.0 and 2.2.  If enabled ICMP errors
125              received for a UDP socket will not be passed to  the  user  pro‐
126              gram.   In  later  kernel  versions, support for this option has
127              been phased out: Linux 2.4 silently ignores it,  and  Linux  2.6
128              generates  a  kernel  warning  (printk()) if a program uses this
129              option.  Linux 2.0 also  enabled  BSD  bug-to-bug  compatibility
130              options (random header changing, skipping of the broadcast flag)
131              for raw sockets with this option, but that was removed in  Linux
132              2.2.
133
134       SO_DEBUG
135              Enable  socket  debugging.  Only  allowed for processes with the
136              CAP_NET_ADMIN capability or an effective user ID of 0.
137
138       SO_ERROR
139              Get and clear the pending socket error. Only valid as a getsock‐
140              opt().  Expects an integer.
141
142       SO_DONTROUTE
143              Don't send via a gateway, only send to directly connected hosts.
144              The same effect can be achieved  by  setting  the  MSG_DONTROUTE
145              flag  on  a socket send(2) operation. Expects an integer boolean
146              flag.
147
148       SO_KEEPALIVE
149              Enable sending of  keep-alive  messages  on  connection-oriented
150              sockets.  Expects an integer boolean flag.
151
152       SO_LINGER
153              Sets  or  gets  the  SO_LINGER  option. The argument is a linger
154              structure.
155
156                struct linger {
157                    int l_onoff;    /* linger active */
158                    int l_linger;   /* how many seconds to linger for */
159                };
160
161              When enabled, a close(2) or shutdown(2) will  not  return  until
162              all  queued  messages for the socket have been successfully sent
163              or the linger timeout has  been  reached.  Otherwise,  the  call
164              returns  immediately  and the closing is done in the background.
165              When the socket is closed as part of exit(2), it always  lingers
166              in the background.
167
168       SO_OOBINLINE
169              If  this  option is enabled, out-of-band data is directly placed
170              into the receive data stream.   Otherwise  out-of-band  data  is
171              only passed when the MSG_OOB flag is set during receiving.
172
173       SO_PASSCRED
174              Enable  or  disable the receiving of the SCM_CREDENTIALS control
175              message. For more information see unix(7).
176
177       SO_PEERCRED
178              Return the credentials of the foreign process connected to  this
179              socket.   This  is  only  possible  for connected PF_UNIX stream
180              sockets and PF_UNIX stream and  datagram  socket  pairs  created
181              using  socketpair(2); see unix(7).  The returned credentials are
182              those that were in effect at the time of the call to  connect(2)
183              or socketpair(2).  Argument is a ucred structure.  Only valid as
184              a getsockopt().
185
186       SO_PRIORITY
187              Set the protocol-defined priority for all packets to be sent  on
188              this  socket.   Linux  uses  this  value to order the networking
189              queues: packets with a higher priority may  be  processed  first
190              depending  on  the  selected  device  queueing  discipline.  For
191              ip(7), this also sets the IP  type-of-service  (TOS)  field  for
192              outgoing  packets.   Setting a priority outside the range 0 to 6
193              requires the CAP_NET_ADMIN capability.
194
195       SO_RCVBUF
196              Sets or gets the maximum socket receive buffer  in  bytes.   The
197              kernel  doubles this value (to allow space for bookkeeping over‐
198              head) when it is set using setsockopt(), and this doubled  value
199              is  returned  by  getsockopt().  The default value is set by the
200              rmem_default sysctl and the maximum allowed value is set by  the
201              rmem_max sysctl.  The minimum (doubled) value for this option is
202              256.
203
204       SO_RCVBUFFORCE (since Linux 2.6.14)
205              Using this socket option, a privileged  (CAP_NET_ADMIN)  process
206              can  perform  the same task as SO_RCVBUF, but the rmem_max limit
207              can be overridden.
208
209       SO_RCVLOWAT and SO_SNDLOWAT
210              Specify the minimum number of bytes  in  the  buffer  until  the
211              socket layer will pass the data to the protocol (SO_SNDLOWAT) or
212              the user on receiving (SO_RCVLOWAT).  These two values are  ini‐
213              tialised to 1.  SO_SNDLOWAT is not changeable on Linux (setsock‐
214              opt fails with the error ENOPROTOOPT).  SO_RCVLOWAT  is  change‐
215              able  only  since  Linux  2.4.  The select(2) and poll(2) system
216              calls currently do not respect the SO_RCVLOWAT setting on Linux,
217              and  mark  a  socket readable when even a single byte of data is
218              available.  A subsequent read from the socket will  block  until
219              SO_RCVLOWAT bytes are available.
220
221       SO_RCVTIMEO and SO_SNDTIMEO
222              Specify  the  receiving  or  sending timeouts until reporting an
223              error.  The parameter is a struct timeval.  If an input or  out‐
224              put  function  blocks for this period of time, and data has been
225              sent or received, the return value of that function will be  the
226              amount  of data transferred; if no data has been transferred and
227              the timeout has been reached then -1 is returned with errno  set
228              to  EAGAIN or EWOULDBLOCK just as if the socket was specified to
229              be nonblocking.  If the timeout is set  to  zero  (the  default)
230              then the operation will never timeout.
231
232       SO_REUSEADDR
233              Indicates  that  the rules used in validating addresses supplied
234              in a bind(2) call should allow reuse  of  local  addresses.  For
235              PF_INET  sockets  this means that a socket may bind, except when
236              there is an active listening socket bound to the address.   When
237              the listening socket is bound to INADDR_ANY with a specific port
238              then it is not possible to bind  to  this  port  for  any  local
239              address.
240
241       SO_SNDBUF
242              Sets  or gets the maximum socket send buffer in bytes.  The ker‐
243              nel doubles this value (to allow space for bookkeeping overhead)
244              when  it  is  set  using setsockopt(), and this doubled value is
245              returned by getsockopt().  The  default  value  is  set  by  the
246              wmem_default  sysctl and the maximum allowed value is set by the
247              wmem_max sysctl.  The minimum (doubled) value for this option is
248              2048.
249
250       SO_SNDBUFFORCE (since Linux 2.6.14)
251              Using  this  socket option, a privileged (CAP_NET_ADMIN) process
252              can perform the same task as SO_SNDBUF, but the  wmem_max  limit
253              can be overridden.
254
255       SO_TIMESTAMP
256              Enable or disable the receiving of the SO_TIMESTAMP control mes‐
257              sage.   The  timestamp  control  message  is  sent  with   level
258              SOL_SOCKET  and the cmsg_data field is a struct timeval indicat‐
259              ing the reception time of the last packet passed to the user  in
260              this call.  See cmsg(3) for details on control messages.
261
262       SO_TYPE
263              Gets the socket type as an integer (like SOCK_STREAM).  Can only
264              be read with getsockopt().
265

SIGNALS

267       When writing onto a connection-oriented socket that has been shut  down
268       (by the local or the remote end) SIGPIPE is sent to the writing process
269       and EPIPE is returned.  The signal is not  sent  when  the  write  call
270       specified the MSG_NOSIGNAL flag.
271
272       When  requested  with the FIOSETOWN fcntl() or SIOCSPGRP ioctl(), SIGIO
273       is sent when an I/O event occurs. It is  possible  to  use  poll(2)  or
274       select(2)  in  the  signal  handler  to find out which socket the event
275       occurred on.  An alternative (in Linux 2.2) is to set a realtime signal
276       using the F_SETSIG fcntl(); the handler of the real time signal will be
277       called with the file descriptor in the si_fd field  of  its  siginfo_t.
278       See fcntl(2) for more information.
279
280       Under  some  circumstances  (e.g. multiple processes accessing a single
281       socket), the condition that caused the SIGIO may  have  already  disap‐
282       peared  when  the  process  reacts to the signal.  If this happens, the
283       process should wait again because Linux will resend the signal later.
284

SYSCTLS

286       The  core  socket  networking  sysctls  can  be  accessed   using   the
287       /proc/sys/net/core/* files or with the sysctl(2) interface.
288
289       rmem_default
290              contains the default setting in bytes of the socket receive buf‐
291              fer.
292
293       rmem_max
294              contains the maximum socket receive buffer size in bytes which a
295              user may set by using the SO_RCVBUF socket option.
296
297       wmem_default
298              contains the default setting in bytes of the socket send buffer.
299
300       wmem_max
301              contains  the  maximum  socket send buffer size in bytes which a
302              user may set by using the SO_SNDBUF socket option.
303
304       message_cost and message_burst
305              configure the token bucket filter used  to  load  limit  warning
306              messages caused by external network events.
307
308       netdev_max_backlog
309              Maximum number of packets in the global input queue.
310
311       optmem_max
312              Maximum  length of ancillary data and user control data like the
313              iovecs per socket.
314

IOCTLS

316       These operations can be accessed using ioctl(2):
317
318         error = ioctl(ip_socket, ioctl_type, &value_result);
319
320
321       SIOCGSTAMP
322              Return a struct timeval with the receive timestamp of  the  last
323              packet  passed  to  the user.  This is useful for accurate round
324              trip time measurements.  See setitimer(2) for a  description  of
325              struct  timeval.   This  ioctl should only be used if the socket
326              option SO_TIMESTAMP is not set on  the  socket.   Otherwise,  it
327              returns the timestamp of the last packet that was received while
328              SO_TIMESTAMP was not set, or it fails if no such packet has been
329              received, (i.e., ioctl() returns -1 with errno set to ENOENT).
330
331       SIOCSPGRP
332              Set the process or process group to send SIGIO or SIGURG signals
333              to when an asynchronous I/O operation  has  finished  or  urgent
334              data  is  available.   The argument is a pointer to a pid_t.  If
335              the argument is positive, send the signals to that process.   If
336              the  argument is negative, send the signals to the process group
337              with the ID of the absolute value of the argument.  The  process
338              may  only choose itself or its own process group to receive sig‐
339              nals unless it has the CAP_KILL capability or an  effective  UID
340              of 0.
341
342       FIOASYNC
343              Change  the  O_ASYNC  flag to enable or disable asynchronous I/O
344              mode of the socket.  Asynchronous I/O mode means that the  SIGIO
345              signal  or the signal set with F_SETSIG is raised when a new I/O
346              event occurs.
347
348              Argument is an integer boolean flag.
349
350       SIOCGPGRP
351              Get the current process or process group that receives SIGIO  or
352              SIGURG signals, or 0 when none is set.
353
354       Valid fcntl() operations:
355
356       FIOGETOWN
357              The same as the SIOCGPGRP ioctl().
358
359       FIOSETOWN
360              The same as the SIOCSPGRP ioctl().
361

NOTES

363       Linux assumes that half of the send/receive buffer is used for internal
364       kernel structures; thus the sysctls are twice what can be  observed  on
365       the wire.
366
367       Linux  will  only  allow  port re-use with the SO_REUSEADDR option when
368       this option was set both in  the  previous  program  that  performed  a
369       bind()  to  the  port and in the program that wants to re-use the port.
370       This differs from some implementations (e.g., FreeBSD) where  only  the
371       later  program  needs  to  set the SO_REUSEADDR option.  Typically this
372       difference is invisible,  since,  for  example,  a  server  program  is
373       designed to always set this option.
374

BUGS

376       The  CONFIG_FILTER socket options SO_ATTACH_FILTER and SO_DETACH_FILTER
377       are not documented. The suggested interface to  use  them  is  via  the
378       libpcap library.
379

VERSIONS

381       SO_BINDTODEVICE  was introduced in Linux 2.0.30.  SO_PASSCRED is new in
382       Linux 2.2.  The sysctls are new in Linux 2.2.  SO_RCVTIMEO and  SO_SND‐
383       TIMEO are supported since Linux 2.3.41. Earlier, timeouts were fixed to
384       a protocol specific setting, and could not be read or written.
385

SEE ALSO

387       getsockopt(2),  setsockopt(2),  socket(2),   capabilities(7),   ddp(7),
388       ip(7), packet(7)
389
390
391
392Linux 2.6.6                       2004-05-27                         SOCKET(7)
Impressum