socket_function_part_2

/**
 * NAME
 *
 * listen - listen for socket connections and limit the queue of incoming connections
 * SYNOPSIS
 *
 * #include <sys/socket.h>
 *
 * int listen(int socket, int backlog);
 *
 * DESCRIPTION
 *
 * The listen() function shall mark a connection-mode socket, specified by the socket argument, as accepting connections.
 *
 * The backlog argument provides a hint to the implementation which the implementation shall use to limit the number of outstanding connections in the socket's listen queue. Implementations may impose a limit on backlog and silently reduce the specified value. Normally, a larger backlog argument value shall result in a larger or equal length of the listen queue. Implementations shall support values of backlog up to SOMAXCONN, defined in <sys/socket.h>.
 *
 * The implementation may include incomplete connections in its listen queue. The limits on the number of incomplete connections and completed connections queued may be different.
 *
 * The implementation may have an upper limit on the length of the listen queue-either global or per accepting socket. If backlog exceeds this limit, the length of the listen queue is set to the limit.
 *
 * If listen() is called with a backlog argument value that is less than 0, the function behaves as if it had been called with a backlog argument value of 0.
 *
 * A backlog argument of 0 may allow the socket to accept connections, in which case the length of the listen queue may be set to an implementation-defined minimum value.
 *
 * The socket in use may require the process to have appropriate privileges to use the listen() function.
 *
 * RETURN VALUE
 *
 * Upon successful completions, listen() shall return 0; otherwise, -1 shall be returned and errno set to indicate the error.
 *
 * ERRORS
 *
 * The listen() function shall fail if:
 *
 * [EBADF]
 * The socket argument is not a valid file descriptor.
 * [EDESTADDRREQ]
 * The socket is not bound to a local address, and the protocol does not support listening on an unbound socket.
 * [EINVAL]
 * The socket is already connected.
 * [ENOTSOCK]
 * The socket argument does not refer to a socket.
 * [EOPNOTSUPP]
 * The socket protocol does not support listen().
 * The listen() function may fail if:
 *
 * [EACCES]
 * The calling process does not have appropriate privileges.
 * [EINVAL]
 * The socket has been shut down.
 * [ENOBUFS]
 * Insufficient resources are available in the system to complete the call.
 */
int listen(int socket, int backlog);

/**
 * NAME
 *
 * recv - receive a message from a connected socket
 * SYNOPSIS
 *
 * #include <sys/socket.h>
 *
 * ssize_t recv(int socket, void *buffer, size_t length, int flags);
 *
 * DESCRIPTION
 *
 * The recv() function shall receive a message from a connection-mode or connectionless-mode socket. It is normally used with connected sockets because it does not permit the application to retrieve the source address of received data.
 *
 * The recv() function takes the following arguments:
 *
 * socket
 * Specifies the socket file descriptor.
 * buffer
 * Points to a buffer where the message should be stored.
 * length
 * Specifies the length in bytes of the buffer pointed to by the buffer argument.
 * flags
 * Specifies the type of message reception. Values of this argument are formed by logically OR'ing zero or more of the following values:
 * MSG_PEEK
 * Peeks at an incoming message. The data is treated as unread and the next recv() or similar function shall still return this data.
 * MSG_OOB
 * Requests out-of-band data. The significance and semantics of out-of-band data are protocol-specific.
 * MSG_WAITALL
 * On SOCK_STREAM sockets this requests that the function block until the full amount of data can be returned. The function may return the smaller amount of data if the socket is a message-based socket, if a signal is caught, if the connection is terminated, if MSG_PEEK was specified, or if an error is pending for the socket.
 * The recv() function shall return the length of the message written to the buffer pointed to by the buffer argument. For message-based sockets, such as SOCK_DGRAM and SOCK_SEQPACKET, the entire message shall be read in a single operation. If a message is too long to fit in the supplied buffer, and MSG_PEEK is not set in the flags argument, the excess bytes shall be discarded. For stream-based sockets, such as SOCK_STREAM, message boundaries shall be ignored. In this case, data shall be returned to the user as soon as it becomes available, and no data shall be discarded.
 *
 * If the MSG_WAITALL flag is not set, data shall be returned only up to the end of the first message.
 *
 * If no messages are available at the socket and O_NONBLOCK is not set on the socket's file descriptor, recv() shall block until a message arrives. If no messages are available at the socket and O_NONBLOCK is set on the socket's file descriptor, recv() shall fail and set errno to [EAGAIN] or [EWOULDBLOCK].
 *
 * RETURN VALUE
 *
 * Upon successful completion, recv() shall return the length of the message in bytes. If no messages are available to be received and the peer has performed an orderly shutdown, recv() shall return 0. Otherwise, -1 shall be returned and errno set to indicate the error.
 *
 * ERRORS
 *
 * The recv() function shall fail if:
 *
 * [EAGAIN] or [EWOULDBLOCK]
 * The socket's file descriptor is marked O_NONBLOCK and no data is waiting to be received; or MSG_OOB is set and no out-of-band data is available and either the socket's file descriptor is marked O_NONBLOCK or the socket does not support blocking to await out-of-band data.
 * [EBADF]
 * The socket argument is not a valid file descriptor.
 * [ECONNRESET]
 * A connection was forcibly closed by a peer.
 * [EINTR]
 * The recv() function was interrupted by a signal that was caught, before any data was available.
 * [EINVAL]
 * The MSG_OOB flag is set and no out-of-band data is available.
 * [ENOTCONN]
 * A receive is attempted on a connection-mode socket that is not connected.
 * [ENOTSOCK]
 * The socket argument does not refer to a socket.
 * [EOPNOTSUPP]
 * The specified flags are not supported for this socket type or protocol.
 * [ETIMEDOUT]
 * The connection timed out during connection establishment, or due to a transmission timeout on active connection.
 * The recv() function may fail if:
 *
 * [EIO]
 * An I/O error occurred while reading from or writing to the file system.
 * [ENOBUFS]
 * Insufficient resources were available in the system to perform the operation.
 * [ENOMEM]
 * Insufficient memory was available to fulfill the request.
 */
ssize_t recv(int socket, void *buffer, size_t length, int flags);

/**
 * NAME
 *
 * recvfrom - receive a message from a socket
 * SYNOPSIS
 *
 * #include <sys/socket.h>
 *
 * ssize_t recvfrom(int socket, void *restrict buffer, size_t length,
 * int flags, struct sockaddr *restrict address,
 * socklen_t *restrict address_len);
 *
 * DESCRIPTION
 *
 * The recvfrom() function shall receive a message from a connection-mode or connectionless-mode socket. It is normally used with connectionless-mode sockets because it permits the application to retrieve the source address of received data.
 *
 * The recvfrom() function takes the following arguments:
 *
 * socket
 * Specifies the socket file descriptor.
 * buffer
 * Points to the buffer where the message should be stored.
 * length
 * Specifies the length in bytes of the buffer pointed to by the buffer argument.
 * flags
 * Specifies the type of message reception. Values of this argument are formed by logically OR'ing zero or more of the following values:
 * MSG_PEEK
 * Peeks at an incoming message. The data is treated as unread and the next recvfrom() or similar function shall still return this data.
 * MSG_OOB
 * Requests out-of-band data. The significance and semantics of out-of-band data are protocol-specific.
 * MSG_WAITALL
 * On SOCK_STREAM sockets this requests that the function block until the full amount of data can be returned. The function may return the smaller amount of data if the socket is a message-based socket, if a signal is caught, if the connection is terminated, if MSG_PEEK was specified, or if an error is pending for the socket.
 * address
 * A null pointer, or points to a sockaddr structure in which the sending address is to be stored. The length and format of the address depend on the address family of the socket.
 * address_len
 * Either a null pointer, if address is a null pointer, or a pointer to a socklen_t object which on input specifies the length of the supplied sockaddr structure, and on output specifies the length of the stored address.
 * The recvfrom() function shall return the length of the message written to the buffer pointed to by the buffer argument. For message-based sockets, such as [RS] [Option Start]  SOCK_RAW, [Option End] SOCK_DGRAM, and SOCK_SEQPACKET, the entire message shall be read in a single operation. If a message is too long to fit in the supplied buffer, and MSG_PEEK is not set in the flags argument, the excess bytes shall be discarded. For stream-based sockets, such as SOCK_STREAM, message boundaries shall be ignored. In this case, data shall be returned to the user as soon as it becomes available, and no data shall be discarded.
 *
 * If the MSG_WAITALL flag is not set, data shall be returned only up to the end of the first message.
 *
 * Not all protocols provide the source address for messages. If the address argument is not a null pointer and the protocol provides the source address of messages, the source address of the received message shall be stored in the sockaddr structure pointed to by the address argument, and the length of this address shall be stored in the object pointed to by the address_len argument.
 *
 * If the actual length of the address is greater than the length of the supplied sockaddr structure, the stored address shall be truncated.
 *
 * If the address argument is not a null pointer and the protocol does not provide the source address of messages, the value stored in the object pointed to by address is unspecified.
 *
 * If no messages are available at the socket and O_NONBLOCK is not set on the socket's file descriptor, recvfrom() shall block until a message arrives. If no messages are available at the socket and O_NONBLOCK is set on the socket's file descriptor, recvfrom() shall fail and set errno to [EAGAIN] or [EWOULDBLOCK].
 *
 * RETURN VALUE
 *
 * Upon successful completion, recvfrom() shall return the length of the message in bytes. If no messages are available to be received and the peer has performed an orderly shutdown, recvfrom() shall return 0. Otherwise, the function shall return -1 and set errno to indicate the error.
 *
 * ERRORS
 *
 * The recvfrom() function shall fail if:
 *
 * [EAGAIN] or [EWOULDBLOCK]
 * The socket's file descriptor is marked O_NONBLOCK and no data is waiting to be received; or MSG_OOB is set and no out-of-band data is available and either the socket's file descriptor is marked O_NONBLOCK or the socket does not support blocking to await out-of-band data.
 * [EBADF]
 * The socket argument is not a valid file descriptor.
 * [ECONNRESET]
 * A connection was forcibly closed by a peer.
 * [EINTR]
 * A signal interrupted recvfrom() before any data was available.
 * [EINVAL]
 * The MSG_OOB flag is set and no out-of-band data is available.
 * [ENOTCONN]
 * A receive is attempted on a connection-mode socket that is not connected.
 * [ENOTSOCK]
 * The socket argument does not refer to a socket.
 * [EOPNOTSUPP]
 * The specified flags are not supported for this socket type.
 * [ETIMEDOUT]
 * The connection timed out during connection establishment, or due to a transmission timeout on active connection.
 * The recvfrom() function may fail if:
 *
 * [EIO]
 * An I/O error occurred while reading from or writing to the file system.
 * [ENOBUFS]
 * Insufficient resources were available in the system to perform the operation.
 * [ENOMEM]
 * Insufficient memory was available to fulfill the request.
 */
ssize_t recvfrom(int socket, void *restrict buffer, size_t length, int flags, struct sockaddr *restrict address, socklen_t *restrict address_len);

/**
 * NAME
 *
 * recvmsg - receive a message from a socket
 * SYNOPSIS
 *
 * #include <sys/socket.h>
 *
 * ssize_t recvmsg(int socket, struct msghdr *message, int flags);
 *
 * DESCRIPTION
 *
 * The recvmsg() function shall receive a message from a connection-mode or connectionless-mode socket. It is normally used with connectionless-mode sockets because it permits the application to retrieve the source address of received data.
 *
 * The recvmsg() function takes the following arguments:
 *
 * socket
 * Specifies the socket file descriptor.
 * message
 * Points to a msghdr structure, containing both the buffer to store the source address and the buffers for the incoming message. The length and format of the address depend on the address family of the socket. The msg_flags member is ignored on input, but may contain meaningful values on output.
 * flags
 * Specifies the type of message reception. Values of this argument are formed by logically OR'ing zero or more of the following values:
 * MSG_OOB
 * Requests out-of-band data. The significance and semantics of out-of-band data are protocol-specific.
 * MSG_PEEK
 * Peeks at the incoming message.
 * MSG_WAITALL
 * On SOCK_STREAM sockets this requests that the function block until the full amount of data can be returned. The function may return the smaller amount of data if the socket is a message-based socket, if a signal is caught, if the connection is terminated, if MSG_PEEK was specified, or if an error is pending for the socket.
 * The recvmsg() function shall receive messages from unconnected or connected sockets and shall return the length of the message.
 *
 * The recvmsg() function shall return the total length of the message. For message-based sockets, such as SOCK_DGRAM and SOCK_SEQPACKET, the entire message shall be read in a single operation. If a message is too long to fit in the supplied buffers, and MSG_PEEK is not set in the flags argument, the excess bytes shall be discarded, and MSG_TRUNC shall be set in the msg_flags member of the msghdr structure. For stream-based sockets, such as SOCK_STREAM, message boundaries shall be ignored. In this case, data shall be returned to the user as soon as it becomes available, and no data shall be discarded.
 *
 * If the MSG_WAITALL flag is not set, data shall be returned only up to the end of the first message.
 *
 * If no messages are available at the socket and O_NONBLOCK is not set on the socket's file descriptor, recvmsg() shall block until a message arrives. If no messages are available at the socket and O_NONBLOCK is set on the socket's file descriptor, the recvmsg() function shall fail and set errno to [EAGAIN] or [EWOULDBLOCK].
 *
 * In the msghdr structure, the msg_name member may be a null pointer if the source address is not required. Otherwise, if the socket is unconnected, the msg_name member points to a sockaddr structure in which the source address is to be stored, and the msg_namelen member on input specifies the length of the supplied sockaddr structure and on output specifies the length of the stored address. If the actual length of the address is greater than the length of the supplied sockaddr structure, the stored address shall be truncated. If the socket is connected, the msg_name and msg_namelen members shall be ignored. The msg_iov and msg_iovlen fields are used to specify where the received data shall be stored. The msg_iov member points to an array of iovec structures; the msg_iovlen member shall be set to the dimension of this array. In each iovec structure, the iov_base field specifies a storage area and the iov_len field gives its size in bytes. Each storage area indicated by msg_iov is filled with received data in turn until all of the received data is stored or all of the areas have been filled.
 *
 * Upon successful completion, the msg_flags member of the message header shall be the bitwise-inclusive OR of all of the following flags that indicate conditions detected for the received message:
 *
 * MSG_EOR
 * End-of-record was received (if supported by the protocol).
 * MSG_OOB
 * Out-of-band data was received.
 * MSG_TRUNC
 * Normal data was truncated.
 * MSG_CTRUNC
 * Control data was truncated.
 * RETURN VALUE
 *
 * Upon successful completion, recvmsg() shall return the length of the message in bytes. If no messages are available to be received and the peer has performed an orderly shutdown, recvmsg() shall return 0. Otherwise, -1 shall be returned and errno set to indicate the error.
 *
 * ERRORS
 *
 * The recvmsg() function shall fail if:
 *
 * [EAGAIN] or [EWOULDBLOCK]
 * The socket's file descriptor is marked O_NONBLOCK and no data is waiting to be received; or MSG_OOB is set and no out-of-band data is available and either the socket's file descriptor is marked O_NONBLOCK or the socket does not support blocking to await out-of-band data.
 * [EBADF]
 * The socket argument is not a valid open file descriptor.
 * [ECONNRESET]
 * A connection was forcibly closed by a peer.
 * [EINTR]
 * This function was interrupted by a signal before any data was available.
 * [EINVAL]
 * The sum of the iov_len values overflows a ssize_t, or the MSG_OOB flag is set and no out-of-band data is available.
 * [EMSGSIZE]
 * The msg_iovlen member of the msghdr structure pointed to by message is less than or equal to 0, or is greater than {IOV_MAX}.
 * [ENOTCONN]
 * A receive is attempted on a connection-mode socket that is not connected.
 * [ENOTSOCK]
 * The socket argument does not refer to a socket.
 * [EOPNOTSUPP]
 * The specified flags are not supported for this socket type.
 * [ETIMEDOUT]
 * The connection timed out during connection establishment, or due to a transmission timeout on active connection.
 * The recvmsg() function may fail if:
 *
 * [EIO]
 * An I/O error occurred while reading from or writing to the file system.
 * [ENOBUFS]
 * Insufficient resources were available in the system to perform the operation.
 * [ENOMEM]
 * Insufficient memory was available to fulfill the request.
 */
ssize_t recvmsg(int socket, struct msghdr *message, int flags);

/**
 * NAME
 *
 * send - send a message on a socket
 * SYNOPSIS
 *
 * #include <sys/socket.h>
 *
 * ssize_t send(int socket, const void *buffer, size_t length, int flags);
 *
 * DESCRIPTION
 *
 * The send() function shall initiate transmission of a message from the specified socket to its peer. The send() function shall send a message only when the socket is connected. If the socket is a connectionless-mode socket, the message shall be sent to the pre-specified peer address.
 *
 * The send() function takes the following arguments:
 *
 * socket
 * Specifies the socket file descriptor.
 * buffer
 * Points to the buffer containing the message to send.
 * length
 * Specifies the length of the message in bytes.
 * flags
 * Specifies the type of message transmission. Values of this argument are formed by logically OR'ing zero or more of the following flags:
 * MSG_EOR
 * Terminates a record (if supported by the protocol).
 * MSG_OOB
 * Sends out-of-band data on sockets that support out-of-band communications. The significance and semantics of out-of-band data are protocol-specific.
 * MSG_NOSIGNAL
 * Requests not to send the SIGPIPE signal if an attempt to send is made on a stream-oriented socket that is no longer connected. The [EPIPE] error shall still be returned.
 * The length of the message to be sent is specified by the length argument. If the message is too long to pass through the underlying protocol, send() shall fail and no data shall be transmitted.
 *
 * Successful completion of a call to send() does not guarantee delivery of the message. A return value of -1 indicates only locally-detected errors.
 *
 * If space is not available at the sending socket to hold the message to be transmitted, and the socket file descriptor does not have O_NONBLOCK set, send() shall block until space is available. If space is not available at the sending socket to hold the message to be transmitted, and the socket file descriptor does have O_NONBLOCK set, send() shall fail. The select() and poll() functions can be used to determine when it is possible to send more data.
 *
 * The socket in use may require the process to have appropriate privileges to use the send() function.
 *
 * RETURN VALUE
 *
 * Upon successful completion, send() shall return the number of bytes sent. Otherwise, -1 shall be returned and errno set to indicate the error.
 *
 * ERRORS
 *
 * The send() function shall fail if:
 *
 * [EAGAIN] or [EWOULDBLOCK]
 * The socket's file descriptor is marked O_NONBLOCK and the requested operation would block.
 * [EBADF]
 * The socket argument is not a valid file descriptor.
 * [ECONNRESET]
 * A connection was forcibly closed by a peer.
 * [EDESTADDRREQ]
 * The socket is not connection-mode and no peer address is set.
 * [EINTR]
 * A signal interrupted send() before any data was transmitted.
 * [EMSGSIZE]
 * The message is too large to be sent all at once, as the socket requires.
 * [ENOTCONN]
 * The socket is not connected.
 * [ENOTSOCK]
 * The socket argument does not refer to a socket.
 * [EOPNOTSUPP]
 * The socket argument is associated with a socket that does not support one or more of the values set in flags.
 * [EPIPE]
 * The socket is shut down for writing, or the socket is connection-mode and is no longer connected. In the latter case, and if the socket is of type SOCK_STREAM or SOCK_SEQPACKET and the MSG_NOSIGNAL flag is not set, the SIGPIPE signal is generated to the calling thread.
 * The send() function may fail if:
 *
 * [EACCES]
 * The calling process does not have appropriate privileges.
 * [EIO]
 * An I/O error occurred while reading from or writing to the file system.
 * [ENETDOWN]
 * The local network interface used to reach the destination is down.
 * [ENETUNREACH]
 * No route to the network is present.
 * [ENOBUFS]
 * Insufficient resources were available in the system to perform the operation.
 */
ssize_t send(int socket, const void *buffer, size_t length, int flags);

/**
 * NAME
 *
 * sendmsg - send a message on a socket using a message structure
 * SYNOPSIS
 *
 * #include <sys/socket.h>
 *
 * ssize_t sendmsg(int socket, const struct msghdr *message, int flags);
 *
 * DESCRIPTION
 *
 * The sendmsg() function shall send a message through a connection-mode or connectionless-mode socket. If the socket is a connectionless-mode socket, the message shall be sent to the address specified by msghdr if no pre-specified peer address has been set. If a peer address has been pre-specified, either the message shall be sent to the address specified in msghdr (overriding the pre-specified peer address), or the function shall return -1 and set errno to [EISCONN]. If the socket is connection-mode, the destination address in msghdr shall be ignored.
 *
 * The sendmsg() function takes the following arguments:
 *
 * socket
 * Specifies the socket file descriptor.
 * message
 * Points to a msghdr structure, containing both the destination address and the buffers for the outgoing message. The length and format of the address depend on the address family of the socket. The msg_flags member is ignored.
 * flags
 * Specifies the type of message transmission. The application may specify 0 or the following flag:
 * MSG_EOR
 * Terminates a record (if supported by the protocol).
 * MSG_OOB
 * Sends out-of-band data on sockets that support out-of-bound data. The significance and semantics of out-of-band data are protocol-specific.
 * MSG_NOSIGNAL
 * Requests not to send the SIGPIPE signal if an attempt to send is made on a stream-oriented socket that is no longer connected. The [EPIPE] error shall still be returned.
 * The msg_iov and msg_iovlen fields of message specify zero or more buffers containing the data to be sent. msg_iov points to an array of iovec structures; msg_iovlen shall be set to the dimension of this array. In each iovec structure, the iov_base field specifies a storage area and the iov_len field gives its size in bytes. Some of these sizes can be zero. The data from each storage area indicated by msg_iov is sent in turn.
 *
 * Successful completion of a call to sendmsg() does not guarantee delivery of the message. A return value of -1 indicates only locally-detected errors.
 *
 * If space is not available at the sending socket to hold the message to be transmitted and the socket file descriptor does not have O_NONBLOCK set, the sendmsg() function shall block until space is available. If space is not available at the sending socket to hold the message to be transmitted and the socket file descriptor does have O_NONBLOCK set, the sendmsg() function shall fail.
 *
 * If the socket protocol supports broadcast and the specified address is a broadcast address for the socket protocol, sendmsg() shall fail if the SO_BROADCAST option is not set for the socket.
 *
 * The socket in use may require the process to have appropriate privileges to use the sendmsg() function.
 *
 * RETURN VALUE
 *
 * Upon successful completion, sendmsg() shall return the number of bytes sent. Otherwise, -1 shall be returned and errno set to indicate the error.
 *
 * ERRORS
 *
 * The sendmsg() function shall fail if:
 *
 * [EAGAIN] or [EWOULDBLOCK]
 * The socket's file descriptor is marked O_NONBLOCK and the requested operation would block.
 * [EAFNOSUPPORT]
 * Addresses in the specified address family cannot be used with this socket.
 * [EBADF]
 * The socket argument is not a valid file descriptor.
 * [ECONNRESET]
 * A connection was forcibly closed by a peer.
 * [EINTR]
 * A signal interrupted sendmsg() before any data was transmitted.
 * [EINVAL]
 * The sum of the iov_len values overflows an ssize_t.
 * [EMSGSIZE]
 * The message is too large to be sent all at once (as the socket requires), or the msg_iovlen member of the msghdr structure pointed to by message is less than or equal to 0 or is greater than {IOV_MAX}.
 * [ENOTCONN]
 * The socket is connection-mode but is not connected.
 * [ENOTSOCK]
 * The socket argument does not refer to a socket.
 * [EOPNOTSUPP]
 * The socket argument is associated with a socket that does not support one or more of the values set in flags.
 * [EPIPE]
 * The socket is shut down for writing, or the socket is connection-mode and is no longer connected. In the latter case, and if the socket is of type SOCK_STREAM or SOCK_SEQPACKET and the MSG_NOSIGNAL flag is not set, the SIGPIPE signal is generated to the calling thread.
 * If the address family of the socket is AF_UNIX, then sendmsg() shall fail if:
 *
 * [EIO]
 * An I/O error occurred while reading from or writing to the file system.
 * [ELOOP]
 * A loop exists in symbolic links encountered during resolution of the pathname in the socket address.
 * [ENAMETOOLONG]
 * The length of a component of a pathname is longer than {NAME_MAX}.
 * [ENOENT]
 * A component of the pathname does not name an existing file or the path name is an empty string.
 * [ENOTDIR]
 * A component of the path prefix of the pathname in the socket address names an existing file that is neither a directory nor a symbolic link to a directory, or the pathname in the socket address contains at least one non- <slash> character and ends with one or more trailing <slash> characters and the last pathname component names an existing file that is neither a directory nor a symbolic link to a directory.
 
 * The sendmsg() function may fail if:
 *
 * [EACCES]
 * Search permission is denied for a component of the path prefix; or write access to the named socket is denied.
 * [EDESTADDRREQ]
 * The socket is not connection-mode and does not have its peer address set, and no destination address was specified.
 * [EHOSTUNREACH]
 * The destination host cannot be reached (probably because the host is down or a remote router cannot reach it).
 * [EIO]
 * An I/O error occurred while reading from or writing to the file system.
 * [EISCONN]
 * A destination address was specified and the socket is already connected.
 * [ENETDOWN]
 * The local network interface used to reach the destination is down.
 * [ENETUNREACH]
 * No route to the network is present.
 * [ENOBUFS]
 * Insufficient resources were available in the system to perform the operation.
 * [ENOMEM]
 * Insufficient memory was available to fulfill the request.
 * If the address family of the socket is AF_UNIX, then sendmsg() may fail if:
 *
 * [ELOOP]
 * More than {SYMLOOP_MAX} symbolic links were encountered during resolution of the pathname in the socket address.
 * [ENAMETOOLONG]
 * The length of a pathname exceeds {PATH_MAX}, or pathname resolution of a symbolic link produced an intermediate result with a length that exceeds {PATH_MAX}.
 */
ssize_t sendmsg(int socket, const struct msghdr *message, int flags);

/**
 * NAME
 *
 * sendto - send a message on a socket
 * SYNOPSIS
 *
 * #include <sys/socket.h>
 *
 * ssize_t sendto(int socket, const void *message, size_t length,
 * int flags, const struct sockaddr *dest_addr,
 * socklen_t dest_len);
 *
 * DESCRIPTION
 *
 * The sendto() function shall send a message through a connection-mode or connectionless-mode socket.
 *
 * If the socket is a connectionless-mode socket, the message shall be sent to the address specified by dest_addr if no pre-specified peer address has been set. If a peer address has been pre-specified, either the message shall be sent to the address specified by dest_addr (overriding the pre-specified peer address), or the function shall return -1 and set errno to [EISCONN].
 *
 * If the socket is connection-mode, dest_addr shall be ignored.
 *
 * The sendto() function takes the following arguments:
 *
 * socket
 * Specifies the socket file descriptor.
 * message
 * Points to a buffer containing the message to be sent.
 * length
 * Specifies the size of the message in bytes.
 * flags
 * Specifies the type of message transmission. Values of this argument are formed by logically OR'ing zero or more of the following flags:
 * MSG_EOR
 * Terminates a record (if supported by the protocol).
 * MSG_OOB
 * Sends out-of-band data on sockets that support out-of-band data. The significance and semantics of out-of-band data are protocol-specific.
 * MSG_NOSIGNAL
 * Requests not to send the SIGPIPE signal if an attempt to send is made on a stream-oriented socket that is no longer connected. The [EPIPE] error shall still be returned.
 * dest_addr
 * Points to a sockaddr structure containing the destination address. The length and format of the address depend on the address family of the socket.
 * dest_len
 * Specifies the length of the sockaddr structure pointed to by the dest_addr argument.
 * If the socket protocol supports broadcast and the specified address is a broadcast address for the socket protocol, sendto() shall fail if the SO_BROADCAST option is not set for the socket.
 *
 * The dest_addr argument specifies the address of the target.
 *
 * The length argument specifies the length of the message.
 *
 * Successful completion of a call to sendto() does not guarantee delivery of the message. A return value of -1 indicates only locally-detected errors.
 *
 * If space is not available at the sending socket to hold the message to be transmitted and the socket file descriptor does not have O_NONBLOCK set, sendto() shall block until space is available. If space is not available at the sending socket to hold the message to be transmitted and the socket file descriptor does have O_NONBLOCK set, sendto() shall fail.
 * The socket in use may require the process to have appropriate privileges to use the sendto() function.
 *
 * RETURN VALUE
 *
 * Upon successful completion, sendto() shall return the number of bytes sent. Otherwise, -1 shall be returned and errno set to indicate the error.
 *
 * ERRORS
 *
 * The sendto() function shall fail if:
 *
 * [EAFNOSUPPORT]
 * Addresses in the specified address family cannot be used with this socket.
 * [EAGAIN] or [EWOULDBLOCK]
 * The socket's file descriptor is marked O_NONBLOCK and the requested operation would block.
 * [EBADF]
 * The socket argument is not a valid file descriptor.
 * [ECONNRESET]
 * A connection was forcibly closed by a peer.
 * [EINTR]
 * A signal interrupted sendto() before any data was transmitted.
 * [EMSGSIZE]
 * The message is too large to be sent all at once, as the socket requires.
 * [ENOTCONN]
 * The socket is connection-mode but is not connected.
 * [ENOTSOCK]
 * The socket argument does not refer to a socket.
 * [EOPNOTSUPP]
 * The socket argument is associated with a socket that does not support one or more of the values set in flags.
 * [EPIPE]
 * The socket is shut down for writing, or the socket is connection-mode and is no longer connected. In the latter case, and if the socket is of type SOCK_STREAM or SOCK_SEQPACKET and the MSG_NOSIGNAL flag is not set, the SIGPIPE signal is generated to the calling thread.
 * If the address family of the socket is AF_UNIX, then sendto() shall fail if:
 *
 * [EIO]
 * An I/O error occurred while reading from or writing to the file system.
 * [ELOOP]
 * A loop exists in symbolic links encountered during resolution of the pathname in the socket address.
 * [ENAMETOOLONG]
 * The length of a component of a pathname is longer than {NAME_MAX}.
 * [ENOENT]
 * A component of the pathname does not name an existing file or the pathname is an empty string.
 * [ENOTDIR]
 * A component of the path prefix of the pathname in the socket address names an existing file that is neither a directory nor a symbolic link to a directory, or the pathname in the socket address contains at least one non- <slash> character and ends with one or more trailing <slash> characters and the last pathname component names an existing file that is neither a directory nor a symbolic link to a directory.
 *
 * The sendto() function may fail if:
 *
 * [EACCES]
 * Search permission is denied for a component of the path prefix; or write access to the named socket is denied.
 * [EDESTADDRREQ]
 * The socket is not connection-mode and does not have its peer address set, and no destination address was specified.
 * [EHOSTUNREACH]
 * The destination host cannot be reached (probably because the host is down or a remote router cannot reach it).
 * [EINVAL]
 * The dest_len argument is not a valid length for the address family.
 * [EIO]
 * An I/O error occurred while reading from or writing to the file system.
 * [EISCONN]
 * A destination address was specified and the socket is already connected.
 * [ENETDOWN]
 * The local network interface used to reach the destination is down.
 * [ENETUNREACH]
 * No route to the network is present.
 * [ENOBUFS]
 * Insufficient resources were available in the system to perform the operation.
 * [ENOMEM]
 * Insufficient memory was available to fulfill the request.
 * If the address family of the socket is AF_UNIX, then sendto() may fail if:
 *
 * [ELOOP]
 * More than {SYMLOOP_MAX} symbolic links were encountered during resolution of the pathname in the socket address.
 * [ENAMETOOLONG]
 * The length of a pathname exceeds {PATH_MAX}, or pathname resolution of a symbolic link produced an intermediate result with a length that exceeds {PATH_MAX}.
 * The following sections are informative.
 * EXAMPLES
 *
 * None.
 *
 * APPLICATION USAGE
 *
 * The select() and poll() functions can be used to determine when it is possible to send more data.
 *
 * RATIONALE
 *
 * None.
 *
 * FUTURE DIRECTIONS
 *
 * None.
 *
 * SEE ALSO
 *
 * getsockopt, poll, pselect, recv, recvfrom, recvmsg, send, sendmsg, setsockopt, shutdown, socket
 *
 * XBD <sys/socket.h>
 *
 * CHANGE HISTORY
 *
 * First released in Issue 6. Derived from the XNS, Issue 5.2 specification.
 *
 * The wording of the mandatory [ELOOP] error condition is updated, and a second optional [ELOOP] error condition is added.
 *
 * Issue 7
 *
 * Austin Group Interpretations 1003.1-2001 #035 and #073 are applied, updating the [EISCONN] error and the DESCRIPTION.
 *
 * Austin Group Interpretation 1003.1-2001 #143 is applied, clarifying the [ENAMETOOLONG] error condition.
 *
 * The MSG_NOSIGNAL flag is added from The Open Group Technical Standard, 2006, Extended API Set Part 2.
 *
 * The [EPIPE] error is modified.
 *
 * POSIX.1-2008, Technical Corrigendum 1, XSH/TC1-2008/0545 [324] is applied.
 */
ssize_t sendto(int socket, const void *message, size_t length,
               int flags, const struct sockaddr *dest_addr,
               socklen_t dest_len);

/**
 * NAME
 *
 * setsockopt - set the socket options
 * SYNOPSIS
 *
 * #include <sys/socket.h>
 *
 * int setsockopt(int socket, int level, int option_name, const void *option_value, socklen_t option_len);
 *
 * DESCRIPTION
 *
 * The setsockopt() function shall set the option specified by the option_name argument, at the protocol level specified by the level argument, to the value pointed to by the option_value argument for the socket associated with the file descriptor specified by the socket argument.
 *
 * The level argument specifies the protocol level at which the option resides. To set options at the socket level, specify the level argument as SOL_SOCKET. To set options at other levels, supply the appropriate level identifier for the protocol controlling the option. For example, to indicate that an option is interpreted by the TCP (Transport Control Protocol), set level to IPPROTO_TCP as defined in the <netinet/in.h> header.
 *
 * The option_name argument specifies a single option to set. It can be one of the socket-level options defined in <sys/socket.h> and described in Use of Options. If option_name is equal to SO_RCVTIMEO or SO_SNDTIMEO and the implementation supports setting the option, it is unspecified whether the struct timeval pointed to by option_value is stored as provided by this function or is rounded up to align with the resolution of the clock being used. If setsockopt() is called with option_name equal to SO_ACCEPTCONN, SO_ERROR, or SO_TYPE, the behavior is unspecified.
 *
 * RETURN VALUE
 *
 * Upon successful completion, setsockopt() shall return 0. Otherwise, -1 shall be returned and errno set to indicate the error.
 *
 * ERRORS
 *
 * The setsockopt() function shall fail if:
 *
 * [EBADF]
 * The socket argument is not a valid file descriptor.
 * [EDOM]
 * The send and receive timeout values are too big to fit into the timeout fields in the socket structure.
 * [EINVAL]
 * The specified option is invalid at the specified socket level or the socket has been shut down.
 * [EISCONN]
 * The socket is already connected, and a specified option cannot be set while the socket is connected.
 * [ENOPROTOOPT]
 * The option is not supported by the protocol.
 * [ENOTSOCK]
 * The socket argument does not refer to a socket.
 * The setsockopt() function may fail if:
 *
 * [ENOMEM]
 * There was insufficient memory available for the operation to complete.
 * [ENOBUFS]
 * Insufficient resources are available in the system to complete the call.
 */
int setsockopt(int socket, int level, int option_name, const void *option_value, socklen_t option_len);

/**
 * NAME
 *
 * shutdown - shut down socket send and receive operations
 * SYNOPSIS
 *
 * #include <sys/socket.h>
 *
 * int shutdown(int socket, int how);
 *
 * DESCRIPTION
 *
 * The shutdown() function shall cause all or part of a full-duplex connection on the socket associated with the file descriptor socket to be shut down.
 *
 * The shutdown() function takes the following arguments:
 *
 * socket
 * Specifies the file descriptor of the socket.
 * how
 * Specifies the type of shutdown. The values are as follows:
 * SHUT_RD
 * Disables further receive operations.
 * SHUT_WR
 * Disables further send operations.
 * SHUT_RDWR
 * Disables further send and receive operations.
 * The shutdown() function disables subsequent send and/or receive operations on a socket, depending on the value of the how argument.
 *
 * RETURN VALUE
 *
 * Upon successful completion, shutdown() shall return 0; otherwise, -1 shall be returned and errno set to indicate the error.
 *
 * ERRORS
 *
 * The shutdown() function shall fail if:
 *
 * [EBADF]
 * The socket argument is not a valid file descriptor.
 * [EINVAL]
 * The how argument is invalid.
 * [ENOTCONN]
 * The socket is not connected.
 * [ENOTSOCK]
 * The socket argument does not refer to a socket.
 * The shutdown() function may fail if:
 *
 * [ENOBUFS]
 * Insufficient resources were available in the system to perform the operation.
 */
int shutdown(int socket, int how);
*
/**
 * NAME
 *
 * sockatmark - determine whether a socket is at the out-of-band mark
 * SYNOPSIS
 *
 * #include <sys/socket.h>
 *
 * int sockatmark(int s);
 *
 * DESCRIPTION
 *
 * The sockatmark() function shall determine whether the socket specified by the descriptor s is at the out-of-band data mark (see Socket Out-of-Band Data State). If the protocol for the socket supports out-of-band data by marking the stream with an out-of-band data mark, the sockatmark() function shall return 1 when all data preceding the mark has been read and the out-of-band data mark is the first element in the receive queue. The sockatmark() function shall not remove the mark from the stream.
 *
 * RETURN VALUE
 *
 * Upon successful completion, the sockatmark() function shall return a value indicating whether the socket is at an out-of-band data mark. If the protocol has marked the data stream and all data preceding the mark has been read, the return value shall be 1; if there is no mark, or if data precedes the mark in the receive queue, the sockatmark() function shall return 0. Otherwise, it shall return a value of -1 and set errno to indicate the error.
 *
 * ERRORS
 *
 * The sockatmark() function shall fail if:
 *
 * [EBADF]
 * The s argument is not a valid file descriptor.
 * [ENOTTY]
 * The file associated with the s argument is not a socket.
 */
int sockatmark(int s);

/**
 * NAME
 *
 * socket - create an endpoint for communication
 * SYNOPSIS
 *
 * #include <sys/socket.h>
 *
 * int socket(int domain, int type, int protocol);
 *
 * DESCRIPTION
 *
 * The socket() function shall create an unbound socket in a communications domain, and return a file descriptor that can be used in later function calls that operate on sockets. The file descriptor shall be allocated as described in File Descriptor Allocation.
 *
 * The socket() function takes the following arguments:
 *
 * domain
 * Specifies the communications domain in which a socket is to be created.
 * type
 * Specifies the type of socket to be created.
 * protocol
 * Specifies a particular protocol to be used with the socket. Specifying a protocol of 0 causes socket() to use an unspecified default protocol appropriate for the requested socket type.
 * The domain argument specifies the address family used in the communications domain. The address families supported by the system are implementation-defined.
 *
 * Symbolic constants that can be used for the domain argument are defined in the <sys/socket.h> header.
 *
 * The type argument specifies the socket type, which determines the semantics of communication over the socket. The following socket types are defined; implementations may specify additional socket types:
 *
 * SOCK_STREAM
 * Provides sequenced, reliable, bidirectional, connection-mode byte streams, and may provide a transmission mechanism for out-of-band data.
 * SOCK_DGRAM
 * Provides datagrams, which are connectionless-mode, unreliable messages of fixed maximum length.
 * SOCK_SEQPACKET
 * Provides sequenced, reliable, bidirectional, connection-mode transmission paths for records. A record can be sent using one or more output operations and received using one or more input operations, but a single operation never transfers part of more than one record. Record boundaries are visible to the receiver via the MSG_EOR flag.
 * If the protocol argument is non-zero, it shall specify a protocol that is supported by the address family. If the protocol argument is zero, the default protocol for this address family and type shall be used. The protocols supported by the system are implementation-defined.
 *
 * The process may need to have appropriate privileges to use the socket() function or to create some sockets.
 *
 * RETURN VALUE
 *
 * Upon successful completion, socket() shall return a non-negative integer, the socket file descriptor. Otherwise, a value of -1 shall be returned and errno set to indicate the error.
 * ERRORS
 *
 * The socket() function shall fail if:
 *
 * [EAFNOSUPPORT]
 * The implementation does not support the specified address family.
 * [EMFILE]
 * All file descriptors available to the process are currently open.
 * [ENFILE]
 * No more file descriptors are available for the system.
 * [EPROTONOSUPPORT]
 * The protocol is not supported by the address family, or the protocol is not supported by the implementation.
 * [EPROTOTYPE]
 * The socket type is not supported by the protocol.
 * The socket() function may fail if:
 
 * [EACCES]
 * The process does not have appropriate privileges.
 * [ENOBUFS]
 * Insufficient resources were available in the system to perform the operation.
 * [ENOMEM]
 * Insufficient memory was available to fulfill the request.
 */
int socket(int domain, int type, int protocol);

/**
 * NAME
 *
 * socketpair - create a pair of connected sockets
 * SYNOPSIS
 *
 * #include <sys/socket.h>
 *
 * int socketpair(int domain, int type, int protocol, int socket_vector[2]);
 *
 * DESCRIPTION
 *
 * The socketpair() function shall create an unbound pair of connected sockets in a specified domain, of a specified type, under the protocol optionally specified by the protocol argument. The two sockets shall be identical. The file descriptors used in referencing the created sockets shall be returned in socket_vector[0] and socket_vector[1]. The file descriptors shall be allocated as described in File Descriptor Allocation.
 *
 * The socketpair() function takes the following arguments:
 *
 * domain
 * Specifies the communications domain in which the sockets are to be created.
 * type
 * Specifies the type of sockets to be created.
 * protocol
 * Specifies a particular protocol to be used with the sockets. Specifying a protocol of 0 causes socketpair() to use an unspecified default protocol appropriate for the requested socket type.
 * socket_vector
 * Specifies a 2-integer array to hold the file descriptors of the created socket pair.
 * The type argument specifies the socket type, which determines the semantics of communications over the socket. The following socket types are defined; implementations may specify additional socket types:
 *
 * SOCK_STREAM
 * Provides sequenced, reliable, bidirectional, connection-mode byte streams, and may provide a transmission mechanism for out-of-band data.
 * SOCK_DGRAM
 * Provides datagrams, which are connectionless-mode, unreliable messages of fixed maximum length.
 * SOCK_SEQPACKET
 * Provides sequenced, reliable, bidirectional, connection-mode transmission paths for records. A record can be sent using one or more output operations and received using one or more input operations, but a single operation never transfers part of more than one record. Record boundaries are visible to the receiver via the MSG_EOR flag.
 * If the protocol argument is non-zero, it shall specify a protocol that is supported by the address family. If the protocol argument is zero, the default protocol for this address family and type shall be used. The protocols supported by the system are implementation-defined.
 *
 * The process may need to have appropriate privileges to use the socketpair() function or to create some sockets.
 *
 * RETURN VALUE
 *
 * Upon successful completion, this function shall return 0; otherwise, -1 shall be returned and errno set to indicate the error, no file descriptors shall be allocated, and the contents of socket_vector shall be left unmodified.
 *
 * ERRORS
 *
 * The socketpair() function shall fail if:
 *
 * [EAFNOSUPPORT]
 * The implementation does not support the specified address family.
 * [EMFILE]
 * All, or all but one, of the file descriptors available to the process are currently open.
 * [ENFILE]
 * No more file descriptors are available for the system.
 * [EOPNOTSUPP]
 * The specified protocol does not permit creation of socket pairs.
 * [EPROTONOSUPPORT]
 * The protocol is not supported by the address family, or the protocol is not supported by the implementation.
 * [EPROTOTYPE]
 * The socket type is not supported by the protocol.
 * The socketpair() function may fail if:
 *
 * [EACCES]
 * The process does not have appropriate privileges.
 * [ENOBUFS]
 * Insufficient resources were available in the system to perform the operation.
 * [ENOMEM]
 * Insufficient memory was available to fulfill the request.
 */
int socketpair(int domain, int type, int protocol, int socket_vector[2]);
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 205,236评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 87,867评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 151,715评论 0 340
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,899评论 1 278
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,895评论 5 368
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,733评论 1 283
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,085评论 3 399
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,722评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 43,025评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,696评论 2 323
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,816评论 1 333
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,447评论 4 322
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,057评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,009评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,254评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,204评论 2 352
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,561评论 2 343

推荐阅读更多精彩内容