socket(7) Miscellaneous Information Manual socket(7) socket - Linux #include sockfd = socket(int socket_family, int socket_type, int protocol); Linux. BSD, . , , AF_INET, AF_IPX AF_PACKET, , , SOCK_STREAM SOCK_DGRAM. socket(2). These functions are used by the user process to send or receive packets and to do other socket operations. For more information, see their respective manual pages. socket(2) , connect(2) , bind(2) , listen(2) , , accept(2) . socketpair(2) ( , AF_UNIX). send(2), sendto(2) sendmsg(2) , recv(2), recvfrom(2) recvmsg(2) . poll(2) select(2) . , -: write(2), writev(2), sendfile(2), read(2) readv(2). getsockname(2) , getpeername(2) . getsockopt(2) setsockopt(2) . ioctl(2) . close(2) . shutdown(2) . (seeking), pread(2) pwrite(2) , . / O_NONBLOCK fcntl(2). , EAGAIN ( ); connect(2) EINPROGRESS. poll(2) select(2). +----------------------------------------------------------------------+ | - | +--------------------------------------+-------------------+-----------+ | | poll | | | | | | +--------------------------------------+-------------------+-----------+ | | POLLIN | | | | | | | | | . | +--------------------------------------+-------------------+-----------+ | | POLLIN | | | | | | | | | | | | | ( | | | | | | | | | | | | | | | | ) | +--------------------------------------+-------------------+-----------+ | | POLLHUP | | | | | | | | | | | | | | | | | | | | | . | +--------------------------------------+-------------------+-----------+ | | POLLHUP | | | | | | | | | ( | | | | | | | | | | | | | | | | | | | | ). | | | | | | | | | | | | | | | | | | | | , | | | | | | | | | | | | | | | | | | | | SIGPIPE. | +--------------------------------------+-------------------+-----------+ | | POLLOUT | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | . | +--------------------------------------+-------------------+-----------+ |/ | POLLIN | POLLOUT | | | | | connect(2) | | | | . | +--------------------------------------+-------------------+-----------+ |/ | POLLERR | | | | | | | | | . | +--------------------------------------+-------------------+-----------+ |/ | POLLHUP | | | | | | | | | | | | | (shut | | | | down) | | | | | | | | . | +--------------------------------------+-------------------+-----------+ | | POLLPRI | | | | | | | | | . | | | | | | | | | | | | | | | | SIGURG. | +--------------------------------------+-------------------+-----------+ poll(2) select(2) SIGIO. fcntl(2) O_ASYNC, sigaction(2) SIGIO. . , . <<>> ( sa_family_t), . (, connect(2), bind(2), accept(2), getsockname(2), getpeername(2)), , . struct sockaddr. <<>> , API . , struct sockaddr_storage. ; ( , IPv6). , , : sa_family_t ss_family; sockaddr_storage , (, , IPv4 IPv6). setsockopt(2) getsockopt(2) , SOL_SOCKET : SO_ACCEPTCONN , listen(2). 0 , . 1 , . . SO_ATTACH_FILTER (since Linux 2.2) SO_ATTACH_BPF (since Linux 3.19) (SO_ATTACH_FILTER) (SO_ATTACH_BPF) BPF , . , . , , . , . SO_ATTACH_FILTER sock_fprog, : struct sock_fprog { unsigned short len; struct sock_filter *filter; }; SO_ATTACH_BPF , bpf(2), BPF_PROG_TYPE_SOCKET_FILTER. , . , , . BPF Documentation/networking/filter.txt. SO_ATTACH_REUSEPORT_CBPF SO_ATTACH_REUSEPORT_EBPF SO_REUSEPORT (SO_ATTACH_REUSEPORT_CBPF) (SO_ATTACH_REUSEPORT_EBPF) BPF, reuseport ( , SO_REUSEPORT, ). BPF 0 N-1, , ( N -- ). BPF , SO_REUSEPORT. ( , bind(2) UDP listen(2) TCP). , reuseport, BPF. reuseport ( close(2)), . BPF, . SO_ATTACH_REUSEPORT_CBPF SO_ATTACH_FILTER, SO_ATTACH_REUSEPORT_EBPF SO_ATTACH_BPF. UDP Linux 4.5; TCP Linux 4.6. SO_BINDTODEVICE Bind this socket to a particular device like "eth0", as specified in the passed interface name. If the name is an empty string or the option length is zero, the socket device binding is removed. The passed option is a variable-length null-terminated interface name string with the maximum size of IFNAMSIZ. If a socket is bound to an interface, only packets received from that particular interface are processed by the socket. Note that this works only for some socket types, particularly AF_INET sockets. It is not supported for packet sockets (use normal bind(2) there). Linux 3.8, , getsockopt(2). Linux 3.8 . optlen , ; -- IFNAMSIZ . optlen. SO_BROADCAST . , . . SO_BSDCOMPAT BSD. UDP Linux 2.0 2.2. , UDP- ICMP . : Linux 2.4 , Linux 2.6 (printk()). Linux 2.0 BSD ( , ), Linux 2.2 . SO_DEBUG . CAP_NET_ADMIN . SO_DETACH_FILTER (since Linux 2.2) SO_DETACH_BPF (since Linux 3.19) , , BPF, SO_ATTACH_FILTER SO_ATTACH_BPF. . SO_DOMAIN ( Linux 2.6.32) ; : AF_INET6. socket(2). . SO_ERROR . . . SO_DONTROUTE , , . MSG_DONTROUTE send(2). . SO_INCOMING_CPU (, Linux 3.19; , Linux 4.4) . . int cpu = 1; setsockopt(fd, SOL_SOCKET, SO_INCOMING_CPU, &cpu, sizeof(cpu)); (. ., 4- (4-tuple)) RX, , , RX, , , , RX. NUMA . SO_INCOMING_NAPI_ID (gettable since Linux 4.12) Returns a system-level unique ID called NAPI ID that is associated with a RX queue on which the last packet associated with that socket is received. This can be used by an application to split the incoming flows among worker threads based on the RX queue on which the packets associated with the flows are received. It allows each worker thread to be associated with a NIC HW receive queue and service all the connection requests received on that RX queue. This mapping between an app thread and a HW NIC queue streamlines the flow of data from the NIC to the application. SO_KEEPALIVE <<>> (keep-alive) , . . SO_LINGER SO_LINGER. linger. struct linger { int l_onoff; /* */ int l_linger; /* */ }; , close(2) shutdown(2) , (linger). . exit(2), . SO_LOCK_FILTER , , . , SO_ATTACH_FILTER SO_ATTACH_BPF SO_ATTACH_REUSEPORT_CBPF SO_ATTACH_REUSEPORT_EBPF. , : ( CAP_NET_RAW), , SO_LOCK_FILTER, UNIX. SO_LOCK_FILTER, , SO_LOCK_FILTER, EPERM. SO_MARK ( Linux 2.6.25) Set the mark for each packet sent through this socket (similar to the netfilter MARK target but socket-based). Changing the mark can be used for mark-based routing without netfilter or for packet filtering. Setting this option requires the CAP_NET_ADMIN or CAP_NET_RAW (since Linux 5.17) capability. SO_OOBINLINE , . , MSG_OOB. SO_PASSCRED Enable or disable the receiving of the SCM_CREDENTIALS control message. For more information, see unix(7). SO_PASSSEC Enable or disable the receiving of the SCM_SECURITY control message. For more information, see unix(7). SO_PEEK_OFF ( Linux 3.4) , unix(7), << >> (peek offset) recv(2), MSG_PEEK. ( -1 ), : recv(2) MSG_PEEK (front) . , , . , << >> , , . recv(2) ( ) MSG_PEEK, << >> . , MSG_PEEK << >> , , , . , << >> , MSG_TRUNC. SO_PEEK_OFF. , : aabbccddeeff recv(2) , : int ov = 4; // Set peek offset to 4 setsockopt(fd, SOL_SOCKET, SO_PEEK_OFF, &ov, sizeof(ov)); recv(fd, buf, 2, MSG_PEEK); // Peeks "cc"; offset set to 6 recv(fd, buf, 2, MSG_PEEK); // Peeks "dd"; offset set to 8 recv(fd, buf, 2, 0); // Reads "aa"; offset set to 6 recv(fd, buf, 2, MSG_PEEK); // Peeks "ee"; offset set to 8 SO_PEERCRED (credentials) , . unix(7). SO_PEERSEC (since Linux 2.6.2) Return the security context of the peer socket connected to this socket. For further details, see unix(7) and ip(7). SO_PRIORITY , . Linux : ( ). 0 6 CAP_NET_ADMIN. SO_PROTOCOL ( Linux 2.6.32) ; : IPPROTO_SCTP. socket(2). . SO_RCVBUF ( ). ( (bookkeeping overhead)) setsockopt(2), getsockopt(2). /proc/sys/net/core/rmem_default, /proc/sys/net/core/rmem_max. () 256. SO_RCVBUFFORCE ( Linux 2.6.14) (CAP_NET_ADMIN) , SO_RCVBUF, rmem_max. SO_RCVLOWAT SO_SNDLOWAT (SO_SNDLOWAT) (SO_RCVLOWAT). 1. SO_SNDLOWAT Linux (setsockopt(2) ENOPROTOOPT). SO_RCVLOWAT Linux 2.4. Before Linux 2.6.28 select(2), poll(2), and epoll(7) did not respect the SO_RCVLOWAT setting on Linux, and indicated a socket as readable when even a single byte of data was available. A subsequent read from the socket would then block until SO_RCVLOWAT bytes are available. Since Linux 2.6.28, select(2), poll(2), and epoll(7) indicate a socket as readable only if at least SO_RCVLOWAT bytes are available. SO_RCVTIMEO SO_SNDTIMEO Specify the receiving or sending timeouts until reporting an error. The argument is a struct timeval. If an input or output function blocks for this period of time, and data has been sent or received, the return value of that function will be the amount of data transferred; if no data has been transferred and the timeout has been reached, then -1 is returned with errno set to EAGAIN or EWOULDBLOCK, or EINPROGRESS (for connect(2)) just as if the socket was specified to be nonblocking. If the timeout is set to zero (the default), then the operation will never timeout. Timeouts only have effect for system calls that perform socket I/O (e.g., accept(2), connect(2), read(2), recvmsg(2), send(2), sendmsg(2)); timeouts have no effect for select(2), poll(2), epoll_wait(2), and so on. SO_REUSEADDR , , bind(2), . AF_INET , (bind), , . INADDR_ANY , . . SO_REUSEPORT ( Linux 3.9) AF_INET AF_INET6 . ( ) bind(2). UID. TCP UDP. TCP accept(2) , . , accept(2), , , accept(2) . UDP ( ) , . SO_RXQ_OVFL ( Linux 2.6.33) , skbs (cmsg) 32- , , . SO_SELECT_ERR_QUEUE (since Linux 3.10) When this option is set on a socket, an error condition on a socket causes notification not only via the exceptfds set of select(2). Similarly, poll(2) also returns a POLLPRI whenever an POLLERR event is returned. Background: this option was added when waking up on an error condition occurred only via the readfds and writefds sets of select(2). The option was added to allow monitoring for error conditions via the exceptfds argument without simultaneously having to receive notifications (via readfds) for regular data that can be read from the socket. After changes in Linux 4.16, the use of this flag to achieve the desired notifications is no longer necessary. This option is nevertheless retained for backwards compatibility. SO_SNDBUF ( ). ( (bookkeeping overhead)) setsockopt(2), getsockopt(2). /proc/sys/net/core/wmem_default, /proc/sys/net/core/wmem_max. () 2048. SO_SNDBUFFORCE ( Linux 2.6.14) (CAP_NET_ADMIN) , SO_SNDBUF, wmem_max. SO_TIMESTAMP Enable or disable the receiving of the SO_TIMESTAMP control message. The timestamp control message is sent with level SOL_SOCKET and a cmsg_type of SCM_TIMESTAMP. The cmsg_data field is a struct timeval indicating the reception time of the last packet passed to the user in this call. See cmsg(3) for details on control messages. SO_TIMESTAMPNS (since Linux 2.6.22) Enable or disable the receiving of the SO_TIMESTAMPNS control message. The timestamp control message is sent with level SOL_SOCKET and a cmsg_type of SCM_TIMESTAMPNS. The cmsg_data field is a struct timespec indicating the reception time of the last packet passed to the user in this call. The clock used for the timestamp is CLOCK_REALTIME. See cmsg(3) for details on control messages. A socket cannot mix SO_TIMESTAMP and SO_TIMESTAMPNS: the two modes are mutually exclusive. SO_TYPE (, SOCK_STREAM). . SO_BUSY_POLL ( Linux 3.11) . CAP_NET_ADMIN. /proc/sys/net/core/busy_read. /proc/sys/net/core/busy_poll select(2) poll(2) , SO_BUSY_POLL . (busy polling) , , . , , . , , (shutdown) , SIGPIPE EPIPE. , MSG_NOSIGNAL. fcntl(2) FIOSETOWN ioctl(2) SIOCSPGRP, / SIGIO. , , poll(2) select(2). ( Linux 2.2) fcntl(2) F_SETSIG; siginfo_t si_fd . fcntl(2). (, ) , SIGIO, . , - , Linux . /proc /proc/sys/net/core/. rmem_default ( ) rmem_max ( ), SO_RCVBUF wmem_default ( ) wmem_max ( ), SO_SNDBUF message_cost message_burst , , netdev_max_backlog optmem_max , , iovec ( ) ioctl ioctl(2): error = ioctl(ip_socket, ioctl_type, &value_result); SIOCGSTAMP Return a struct timeval with the receive timestamp of the last packet passed to the user. This is useful for accurate round trip time measurements. See setitimer(2) for a description of struct timeval. This ioctl should be used only if the socket options SO_TIMESTAMP and SO_TIMESTAMPNS are not set on the socket. Otherwise, it returns the timestamp of the last packet that was received while SO_TIMESTAMP and SO_TIMESTAMPNS were not set, or it fails if no such packet has been received, (i.e., ioctl(2) returns -1 with errno set to ENOENT). SIOCSPGRP , SIGIO SIGURG - . pid_t. F_SETOWN fcntl(2). FIOASYNC O_ASYNC / . / SIGIO , F_SETSIG. . fcntl(2) O_ASYNC. SIOCGPGRP , SIGIO SIGURG. , . fcntl(2): FIOGETOWN , ioctl(2) SIOCGPGRP. FIOSETOWN , ioctl(2) SIOCSPGRP. SO_BINDTODEVICE Linux 2.0.30. SO_PASSCRED Linux 2.2. /proc Linux 2.2. SO_RCVTIMEO SO_SNDTIMEO Linux 2.3.41. , . Linux , / ; /proc , . Linux SO_REUSEADDR , , bind(2) , . (, FreeBSD), SO_REUSEADDR. , , , , . . wireshark(1), bpf(2), connect(2), getsockopt(2), setsockopt(2), socket(2), pcap(3), address_families(7), capabilities(7), ddp(7), ip(7), ipv6(7), packet(7), tcp(7), udp(7), unix(7), tcpdump(8) Alexander Golubev , Azamat Hackimov , Hotellook, Nikita , Spiros Georgaras , Vladislav , Yuri Kozlov ; GNU 3 , . . , , . Linux man-pages 6.06 16 2024 . socket(7)