PageRenderTime 77ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/raw/man7/socket.7

https://github.com/nohappiness/manpages-zh
Unknown | 557 lines | 551 code | 6 blank | 0 comment | 0 complexity | 5a728330de69192f5bc9f72948405306 MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. '\" t
  2. .\" Don't change the first line, it tells man that we need tbl.
  3. .\" This man page is Copyright (C) 1999 Andi Kleen <ak@muc.de>.
  4. .\" and copyright (c) 1999 Matthew Wilcox.
  5. .\" Permission is granted to distribute possibly modified copies
  6. .\" of this page provided the header is included verbatim,
  7. .\" and in case of nontrivial modification author and date
  8. .\" of the modification is added to the header.
  9. .\" $Id: socket.7,v 1.1 2003/12/20 03:31:53 bbbush Exp $
  10. .\"
  11. .\" 30 Oct 2002, Modified, Michael Kerrisk, mtk16@ext.canterbury.ac.nz
  12. .\" Added description of SO_ACCEPTCONN
  13. .\" Plus 1 language tidy-up
  14. .\"
  15. .TH SOCKET 7 1999-05-07 "Linux Man Page" "Linux Programmer's Manual"
  16. .SH NAME
  17. socket - Linux socket interface
  18. .SH SYNOPSIS
  19. .B #include <sys/socket.h>
  20. .br
  21. .IB mysocket " = socket(int " socket_family ", int " socket_type ", int " protocol );
  22. .SH DESCRIPTION
  23. This manual page describes the Linux networking socket layer user
  24. interface. The BSD compatible sockets
  25. are the uniform interface
  26. between the user process and the network protocol stacks in the kernel.
  27. The protocol modules are grouped into
  28. .I protocol families
  29. like
  30. .BR PF_INET ", " PF_IPX ", " PF_PACKET
  31. and
  32. .I socket types
  33. like
  34. .B SOCK_STREAM
  35. or
  36. .BR SOCK_DGRAM .
  37. See
  38. .BR socket (2)
  39. for more information on families and types.
  40. .SH "SOCKET LAYER FUNCTIONS"
  41. These functions are used by the user process to send or receive packets and
  42. to do other socket operations. For more information see their respective
  43. manual pages.
  44. .BR socket (2)
  45. creates a socket,
  46. .BR connect (2)
  47. connects a socket to a remote socket address,
  48. the
  49. .BR bind (2)
  50. function binds a socket to a local socket address,
  51. .BR listen (2)
  52. tells the socket that new connections shall be accepted, and
  53. .BR accept (2)
  54. is used to get a new socket with a new incoming connection.
  55. .BR socketpair (2)
  56. returns two connected anonymous sockets (only implemented for a few
  57. local families like
  58. .BR PF_UNIX )
  59. .PP
  60. .BR send (2),
  61. .BR sendto (2),
  62. and
  63. .BR sendmsg (2)
  64. send data over a socket, and
  65. .BR recv (2),
  66. .BR recvfrom (2),
  67. .BR recvmsg (2)
  68. receive data from a socket.
  69. .BR poll (2)
  70. and
  71. .BR select (2)
  72. wait for arriving data or a readiness to send data.
  73. In addition, the standard I/O operations like
  74. .BR write (2),
  75. .BR writev (2),
  76. .BR sendfile (2),
  77. .BR read (2),
  78. and
  79. .BR readv (2)
  80. can be used to read and write data.
  81. .PP
  82. .BR getsockname (2)
  83. returns the local socket address and
  84. .BR getpeername (2)
  85. returns the remote socket address.
  86. .BR getsockopt (2)
  87. and
  88. .BR setsockopt (2)
  89. are used to set or get socket layer or protocol options.
  90. .BR ioctl (2)
  91. can be used to set or read some other options.
  92. .PP
  93. .BR close (2)
  94. is used to close a socket.
  95. .BR shutdown (2)
  96. closes parts of a full duplex socket connection.
  97. .PP
  98. Seeking, or calling
  99. .BR pread (2)
  100. or
  101. .BR pwrite (2)
  102. with a non-zero position is not supported on sockets.
  103. .PP
  104. It is possible to do non-blocking IO on sockets by setting the
  105. .B O_NONBLOCK
  106. flag on a socket file descriptor using
  107. .BR fcntl (2).
  108. Then all operations that would block will (usually)
  109. return with
  110. .B EAGAIN
  111. (operation should be retried later);
  112. .BR connect (2)
  113. will return
  114. .B EINPROGRESS
  115. error.
  116. The user can then wait for various events via
  117. .BR poll (2)
  118. or
  119. .BR select (2).
  120. .PP
  121. .TS
  122. tab(:) allbox;
  123. c s s
  124. l l l.
  125. I/O events
  126. Event:Poll flag:Occurrence
  127. Read:POLLIN:T{
  128. New data arrived.
  129. T}
  130. Read:POLLIN:T{
  131. A connection setup has been completed
  132. (for connection-oriented sockets)
  133. T}
  134. Read:POLLHUP:T{
  135. A disconnection request has been initiated by the other end.
  136. T}
  137. Read:POLLHUP:T{
  138. A connection is broken (only for connection-oriented protocols).
  139. When the socket is written
  140. .B SIGPIPE
  141. is also sent.
  142. T}
  143. Write:POLLOUT:T{
  144. Socket has enough send buffer space for writing new data.
  145. T}
  146. Read/Write:T{
  147. POLLIN|
  148. .br
  149. POLLOUT
  150. T}:T{
  151. An outgoing
  152. .BR connect (2)
  153. finished.
  154. T}
  155. Read/Write:POLLERR:An asynchronous error occurred.
  156. Read/Write:POLLHUP:The other end has shut down one direction.
  157. Exception:POLLPRI:T{
  158. Urgent data arrived.
  159. .B SIGURG
  160. is sent then.
  161. T}
  162. .\" XXX not true currently
  163. .\" It is no I/O event when the connection
  164. .\" is broken from the local end using
  165. .\" .BR shutdown (2)
  166. .\" or
  167. .\" .BR close (2)
  168. .\" .
  169. .TE
  170. .PP
  171. An alternative to poll/select
  172. is to let the kernel inform the application about events
  173. via a
  174. .B SIGIO
  175. signal. For that the
  176. .B FASYNC
  177. flag must be set on a socket file descriptor
  178. via
  179. .BR fcntl (2)
  180. and a valid signal handler for
  181. .B SIGIO
  182. must be installed via
  183. .BR sigaction (2).
  184. See the
  185. .I SIGNALS
  186. discussion below.
  187. .SH "SOCKET OPTIONS"
  188. These socket options can be set by using
  189. .BR setsockopt (2)
  190. and read with
  191. .BR getsockopt (2)
  192. with the socket level set to
  193. .B SOL_SOCKET
  194. for all sockets:
  195. .TP
  196. .B SO_KEEPALIVE
  197. Enable sending of keep-alive messages on connection-oriented sockets. Expects
  198. a integer boolean flag.
  199. .TP
  200. .B SO_OOBINLINE
  201. If this option is enabled, out-of-band data is directly placed into the receive
  202. data stream. Otherwise out-of-band data is only passed when the
  203. .B MSG_OOB
  204. flag is set during receiving.
  205. .\" don't document it because it can do too much harm.
  206. .\".B SO_NO_CHECK
  207. .TP
  208. .BR SO_RCVLOWAT " and " SO_SNDLOWAT
  209. Specify the minimum number of bytes in the buffer until the socket layer
  210. will pass the data to the protocol
  211. .RB ( SO_SNDLOWAT )
  212. or the user on receiving
  213. .RB ( SO_RCVLOWAT ).
  214. These two values are not changeable in Linux and their argument size
  215. is always fixed
  216. to 1 byte.
  217. .B getsockopt
  218. is able to read them;
  219. .B setsockopt
  220. will always return
  221. .BR ENOPROTOOPT .
  222. .TP
  223. .BR SO_RCVTIMEO " and " SO_SNDTIMEO
  224. Specify the sending or receiving timeouts until reporting an error.
  225. They are fixed to a protocol specific setting in Linux and cannot be read
  226. or written. Their functionality can be emulated using
  227. .BR alarm (2)
  228. or
  229. .BR setitimer (2).
  230. .TP
  231. .B SO_BSDCOMPAT
  232. Enable BSD bug-to-bug compatibility. This is used only by the UDP
  233. protocol module and scheduled to be removed in future.
  234. If enabled ICMP errors received for a UDP socket will not be passed
  235. to the user program. Linux 2.0 also enabled BSD bug-to-bug compatibility
  236. options (random header changing, skipping of the broadcast flag) for raw
  237. sockets with this option, but that has been removed in Linux 2.2. It is
  238. better to fix the user programs than to enable this flag.
  239. .TP
  240. .B SO_PASSCRED
  241. Enable or disable the receiving of the
  242. .B SCM_CREDENTIALS
  243. control message. For more information see
  244. .BR unix (7).
  245. .TP
  246. .B SO_PEERCRED
  247. Return the credentials of the foreign process connected to this socket.
  248. Only useful for
  249. .B PF_UNIX
  250. sockets; see
  251. .BR unix (7).
  252. Argument is a
  253. .B ucred
  254. structure. Only valid as a
  255. .BR getsockopt .
  256. .TP
  257. .B SO_BINDTODEVICE
  258. Bind this socket to a particular device like \(lqeth0\(rq,
  259. as specified in the passed interface name. If the
  260. name is an empty string or the option length is zero, the socket device
  261. binding is removed. The passed option is a variable-length null terminated
  262. interface name string with the maximum size of
  263. .BR IFNAMSIZ .
  264. If a socket is bound to an interface,
  265. only packets received from that particular interface are processed by the
  266. socket. Note that this only works for some socket types, particularly
  267. .B AF_INET
  268. sockets. It is not supported for packet sockets (use normal
  269. .BR bind (8)
  270. there).
  271. .TP
  272. .B SO_DEBUG
  273. Enable socket debugging. Only allowed for processes with the
  274. .B CAP_NET_ADMIN
  275. capability or an effective user id of 0.
  276. .TP
  277. .B SO_REUSEADDR
  278. Indicates that the rules used in validating addresses supplied in a
  279. .BR bind (2)
  280. call should allow reuse of local addresses. For
  281. .B PF_INET
  282. sockets this
  283. means that a socket may bind, except when there
  284. is an active listening socket bound to the address. When the listening
  285. socket is bound to
  286. .B INADDR_ANY
  287. with a specific port then it is not possible
  288. to bind to this port for any local address.
  289. .TP
  290. .B SO_TYPE
  291. Gets the socket type as an integer (like
  292. .BR SOCK_STREAM ).
  293. Can only be read
  294. with
  295. .BR getsockopt .
  296. .\" SO_ACCEPTCONN is in SUSv3, and its origin is explained in
  297. .\" W R Stevens, UNPv1
  298. .TP
  299. .B SO_ACCEPTCONN
  300. Returns a value indicating whether or not this socket has been marked
  301. to accept connections with
  302. .BR listen ().
  303. The value 0 indicates that this is not a listening socket,
  304. the value 1 indicates that this is a listening socket.
  305. Can only be read
  306. with
  307. .BR getsockopt .
  308. .TP
  309. .B SO_DONTROUTE
  310. Don't send via a gateway, only send to directly connected hosts.
  311. The same effect can be achieved by setting the
  312. .B MSG_DONTROUTE
  313. flag on a socket
  314. .BR send (2)
  315. operation. Expects an integer boolean flag.
  316. .TP
  317. .B SO_BROADCAST
  318. Set or get the broadcast flag. When enabled, datagram sockets
  319. receive packets sent to a broadcast address and they are allowed to send
  320. packets to a broadcast address.
  321. This option has no effect on stream-oriented sockets.
  322. .TP
  323. .B SO_SNDBUF
  324. Sets or gets the maximum socket send buffer in bytes. The default value is set
  325. by the
  326. .B wmem_default
  327. sysctl and the maximum allowed value is set by the
  328. .B wmem_max
  329. sysctl.
  330. .TP
  331. .B SO_RCVBUF
  332. Sets or gets the maximum socket receive buffer in bytes. The default value is
  333. set by the
  334. .B rmem_default
  335. sysctl and the maximum allowed value is set by the
  336. .B rmem_max
  337. sysctl.
  338. .TP
  339. .B SO_LINGER
  340. Sets or gets the
  341. .B SO_LINGER
  342. option. The argument is a
  343. .B linger
  344. structure.
  345. .PP
  346. .RS
  347. .nf
  348. .ta 4n 10n 22n
  349. struct linger {
  350. int l_onoff; /* linger active */
  351. int l_linger; /* how many seconds to linger for */
  352. };
  353. .ta
  354. .fi
  355. .RE
  356. .IP
  357. When enabled, a
  358. .BR close (2)
  359. or
  360. .BR shutdown (2)
  361. will not return until all queued messages for the socket have been
  362. successfully sent or the linger timeout has been reached. Otherwise,
  363. the call returns immediately and the closing is done in the background.
  364. When the socket is closed as part of
  365. .BR exit (2),
  366. it always lingers in the background.
  367. .TP
  368. .B SO_PRIORITY
  369. Set the protocol-defined priority for all packets to be sent on this socket.
  370. Linux uses this value to order the networking queues: packets with a higher
  371. priority may be processed first depending on the selected device queueing
  372. discipline. For
  373. .BR ip (7),
  374. this also sets the IP type-of-service (TOS) field for outgoing packets.
  375. .TP
  376. .B SO_ERROR
  377. Get and clear the pending socket error. Only valid as a
  378. .BR getsockopt .
  379. Expects an integer.
  380. .SH SIGNALS
  381. When writing onto a connection-oriented socket that has been shut down
  382. (by the local or the remote end)
  383. .B SIGPIPE
  384. is sent to the writing process and
  385. .B EPIPE
  386. is returned.
  387. The signal is not sent when the write call
  388. specified the
  389. .B MSG_NOSIGNAL
  390. flag.
  391. .PP
  392. When requested with the
  393. .B FIOSETOWN
  394. fcntl or
  395. .B SIOCSPGRP
  396. ioctl,
  397. .B SIGIO
  398. is sent when an I/O event occurs. It is possible to use
  399. .BR poll (2)
  400. or
  401. .BR select (2)
  402. in the signal handler to find out which socket the event occurred on.
  403. An alternative (in Linux 2.2) is to set a realtime signal using the
  404. .B F_SETSIG
  405. fcntl; the handler of the real time signal will be called with
  406. the file descriptor in the
  407. .I si_fd
  408. field of its
  409. .IR siginfo_t .
  410. See
  411. .BR fcntl (2)
  412. for more information.
  413. .PP
  414. Under some circumstances (e.g. multiple processes accessing a single socket),
  415. the condition that caused the
  416. .B SIGIO
  417. may have already disappeared when the process reacts to the signal.
  418. If this happens, the process should wait again because Linux will resend the
  419. signal later.
  420. .\" .SH ANCILLARY MESSAGES
  421. .SH SYSCTLS
  422. The core socket networking sysctls can be accessed using the
  423. .B /proc/sys/net/core/*
  424. files or with the
  425. .BR sysctl (2)
  426. interface.
  427. .TP
  428. .B rmem_default
  429. contains the default setting in bytes of the socket receive buffer.
  430. .TP
  431. .B rmem_max
  432. contains the maximum socket receive buffer size in bytes which a user may
  433. set by using the
  434. .B SO_RCVBUF
  435. socket option.
  436. .TP
  437. .B wmem_default
  438. contains the default setting in bytes of the socket send buffer.
  439. .TP
  440. .B wmem_max
  441. contains the maximum socket send buffer size in bytes which a user may
  442. set by using the
  443. .B SO_SNDBUF
  444. socket option.
  445. .TP
  446. .BR message_cost " and " message_burst
  447. configure the token bucket filter used to load limit warning messages
  448. caused by external network events.
  449. .TP
  450. .B netdev_max_backlog
  451. Maximum number of packets in the global input queue.
  452. .TP
  453. .B optmem_max
  454. Maximum length of ancillary data and user control data like the iovecs
  455. per socket.
  456. .\" netdev_fastroute is not documented because it is experimental
  457. .SH IOCTLS
  458. These ioctls can be accessed using
  459. .BR ioctl (2):
  460. .RS
  461. .nf
  462. .IB error " = ioctl(" ip_socket ", " ioctl_type ", " &value_result ");"
  463. .fi
  464. .RE
  465. .TP
  466. .B SIOCGSTAMP
  467. Return a
  468. .B struct timeval
  469. with the receive timestamp of the last packet passed to the user. This is useful
  470. for accurate round trip time measurements. See
  471. .BR setitimer (2)
  472. for a description of
  473. .BR "struct timeval" .
  474. .\"
  475. .TP
  476. .BR SIOCSPGRP
  477. Set the process or process group to send
  478. .B SIGIO
  479. or
  480. .B SIGURG
  481. signals
  482. to when an
  483. asynchronous I/O operation has finished or urgent data is available.
  484. The argument is a pointer to a
  485. .BR pid_t .
  486. If the argument is positive, send the signals to that process. If the
  487. argument is negative, send the signals to the process group with the id
  488. of the absolute value of the argument.
  489. The process may only choose itself or its own process group to receive
  490. signals unless it has the
  491. .B CAP_KILL
  492. capability or an effective UID of 0.
  493. .TP
  494. .B FIOASYNC
  495. Change the
  496. .B O_ASYNC
  497. flag to enable or disable asynchronous IO mode of the socket. Asynchronous IO
  498. mode means that the
  499. .B SIGIO
  500. signal or the signal set with
  501. .B F_SETSIG
  502. is raised when a new I/O event occurs.
  503. .IP
  504. Argument is a integer boolean flag.
  505. .\"
  506. .TP
  507. .BR SIOCGPGRP
  508. Get the current process or process group that receives
  509. .B SIGIO
  510. or
  511. .B SIGURG
  512. signals,
  513. or 0
  514. when none is set.
  515. .PP
  516. Valid fcntls:
  517. .TP
  518. .BR FIOGETOWN
  519. The same as the SIOCGPGRP ioctl.
  520. .TP
  521. .BR FIOSETOWN
  522. The same as the SIOCSPGRP ioctl
  523. .SH NOTES
  524. Linux assumes that half of the send/receive buffer is used for internal
  525. kernel structures; thus the sysctls are twice what can be observed
  526. on the wire.
  527. .SH BUGS
  528. The
  529. .B CONFIG_FILTER
  530. socket options
  531. .B SO_ATTACH_FILTER
  532. and
  533. .B SO_DETACH_FILTER
  534. are
  535. not documented. The suggested interface to use them is via the libpcap
  536. library.
  537. .SH VERSIONS
  538. .B SO_BINDTODEVICE
  539. was introduced in Linux 2.0.30.
  540. .B SO_PASSCRED
  541. is new in Linux 2.2.
  542. The sysctls are new in Linux 2.2.
  543. .SH AUTHORS
  544. This man page was written by Andi Kleen.
  545. .SH "SEE ALSO"
  546. .BR socket (2),
  547. .BR ip (7),
  548. .BR setsockopt (2),
  549. .BR getsockopt (2),
  550. .BR packet (7),
  551. .BR ddp (7)