POSIX socket wrapper of RIOT's Sock API. More...
POSIX socket wrapper of RIOT's Sock API.
Files | |
file | inet.h |
Definitions for internet operations. | |
file | in.h |
Main socket header. | |
file | socket.h |
Main socket header. | |
Data Structures | |
struct | in_addr |
IPv4 address structure type. More... | |
struct | in6_addr |
IPv6 address structure type. More... | |
struct | sockaddr_in |
IPv4 socket address type. More... | |
struct | sockaddr_in6 |
IPv6 socket address type. More... | |
struct | ipv6_mreq |
IPv6 multicast request. More... | |
struct | sockaddr |
Used to define the socket address. More... | |
struct | sockaddr_storage |
Implementation based socket address table. More... | |
Macros | |
#define | INADDRSZ (4) |
Size in byte of an IPv4 address. | |
#define | IN6ADDRSZ (16) |
Size in byte of an IPv6 address. | |
#define | INET_ADDRSTRLEN (16) |
Length of the string form for IPv4. | |
#define | INET6_ADDRSTRLEN (46) |
Length of the string form for IPv6. | |
#define | INADDR_ANY ((in_addr_t)0x00000000) |
IPv4 local host address. | |
#define | INADDR_BROADCAST ((in_addr_t)0xffffffff) |
IPv4 broadcast address. | |
#define | IN6ADDR_ANY_INIT IPV6_ADDR_UNSPECIFIED |
IPv6 wildcard address. | |
#define | IN6ADDR_LOOPBACK_INIT IPV6_ADDR_LOOPBACK |
IPv6 loopback address. | |
#define | SOCKET_POOL_SIZE (4) |
Maximum number of sockets available on for creation with socket() | |
#define | SOCKET_TCP_QUEUE_SIZE (0) |
Maximum number of incoming TCP connections a listening socket can handle. | |
#define | SOCKADDR_MAX_DATA_LEN (26) |
Maximum data length for a socket address. More... | |
#define | SOL_SOCKET (-1) |
Options to be accessed at socket level, not protocol level. | |
Typedefs | |
typedef uint16_t | in_port_t |
Internet port type. More... | |
typedef uint32_t | in_addr_t |
IPv4 address type. | |
typedef unsigned short | sa_family_t |
address family type | |
Functions | |
const char * | inet_ntop (int af, const void *restrict src, char *restrict dst, socklen_t size) |
Converts an IP address to its string representation. More... | |
int | inet_pton (int af, const char *src, void *dst) |
Converts an IP address string representation to a byte-represented IP address. More... | |
int | accept (int socket, struct sockaddr *__restrict address, socklen_t *__restrict address_len) |
Accept a new connection on a socket. More... | |
int | bind (int socket, const struct sockaddr *address, socklen_t address_len) |
Bind a name to a socket. More... | |
int | connect (int socket, const struct sockaddr *address, socklen_t address_len) |
Connect a socket. More... | |
int | getpeername (int socket, struct sockaddr *__restrict address, socklen_t *__restrict address_len) |
Get the name of the peer socket. More... | |
int | getsockname (int socket, struct sockaddr *__restrict address, socklen_t *__restrict address_len) |
Get the socket name. More... | |
int | listen (int socket, int backlog) |
Listen for socket connections and limit the queue of incoming connections. More... | |
ssize_t | recvfrom (int socket, void *__restrict buffer, size_t length, int flags, struct sockaddr *__restrict address, socklen_t *__restrict address_len) |
Receive a message from a socket. More... | |
static ssize_t | recv (int socket, void *buffer, size_t length, int flags) |
Receive a message from a connected socket. More... | |
ssize_t | sendto (int socket, const void *buffer, size_t length, int flags, const struct sockaddr *address, socklen_t address_len) |
Send a message on a socket. More... | |
static ssize_t | send (int socket, const void *buffer, size_t length, int flags) |
Send a message on a socket. More... | |
int | socket (int domain, int type, int protocol) |
Create an endpoint for communication. More... | |
Variables | |
const struct in6_addr | in6addr_any |
IPv6 socket address for the wildcard address. | |
const struct in6_addr | in6addr_loopback |
IPv6 socket address for the loopback address. | |
IPv6 address macros | |
#define | IN6_IS_ADDR_UNSPECIFIED(addr) ((int)(ipv6_addr_is_unspecified((const ipv6_addr_t *)(addr)))) |
Check if address is the unspecified address (:: ). More... | |
#define | IN6_IS_ADDR_LOOPBACK(addr) ((int)(ipv6_addr_is_loopback((const ipv6_addr_t *)(addr)))) |
Check if address is the loopback address (::1 ). More... | |
#define | IN6_IS_ADDR_MULTICAST(addr) ((int)(ipv6_addr_is_multicast((const ipv6_addr_t *)(addr)))) |
Check if address is a multicast address. More... | |
#define | IN6_IS_ADDR_LINKLOCAL(addr) ((int)(ipv6_addr_is_link_local((const ipv6_addr_t *)addr))) |
Check if address is a link-local address. More... | |
#define | IN6_IS_ADDR_SITELOCAL(addr) ((int)(ipv6_addr_is_site_local((const ipv6_addr_t *)addr))) |
Check if address is a site-local address. More... | |
#define | IN6_IS_ADDR_V4MAPPED(addr) ((int)(ipv6_addr_is_ipv4_mapped((const ipv6_addr_t *)addr))) |
Check if address is an IPv4 mapped address. More... | |
#define | IN6_IS_ADDR_V4COMPAT(addr) ((int)(ipv6_addr_is_ipv4_compat((const ipv6_addr_t *)addr))) |
Check if address is an IPv4-compatible address. More... | |
#define | IN6_IS_ADDR_MC_NODELOCAL(addr) |
Check if address is a multicast node-local address. More... | |
#define | IN6_IS_ADDR_MC_LINKLOCAL(addr) |
Check if address is a multicast link-local address. More... | |
#define | IN6_IS_ADDR_MC_SITELOCAL(addr) |
Check if address is a multicast site-local address. More... | |
#define | IN6_IS_ADDR_MC_ORGLOCAL(addr) |
Check if address is a multicast organization-local address. More... | |
#define | IN6_IS_ADDR_MC_GLOBAL(addr) |
Check if address is a multicast global address. More... | |
Protocol numbers for option | |
#define | IPPROTO_IP (PROTNUM_IPV4) |
Internet Protocol version 4. | |
#define | IPPROTO_IPV6 (PROTNUM_IPV6) |
Internet Protocol version 6. | |
#define | IPPROTO_ICMP (PROTNUM_ICMP) |
Internet Control Message Protocol. | |
#define | IPPROTO_ICMPV6 (PROTNUM_ICMPV6) |
ICMP for IPv6. | |
#define | IPPROTO_RAW (PROTNUM_RESERVED) |
Raw IP packets protocol. | |
#define | IPPROTO_TCP (PROTNUM_TCP) |
Transmission control protocol. | |
#define | IPPROTO_UDP (PROTNUM_UDP) |
User datagram protocol. | |
Socket types | |
#define | SOCK_DGRAM (1) |
Datagram socket. | |
#define | SOCK_RAW (2) |
Raw socket. | |
#define | SOCK_SEQPACKET (3) |
Sequenced-packet socket. | |
#define | SOCK_STREAM (4) |
Stream socket. | |
Option names | |
Option names for getsockopt() and setsockopt() | |
#define | SO_ACCEPTCONN (0) |
Socket is accepting connections. | |
#define | SO_BROADCAST (1) |
Transmission of broadcast messages is supported. | |
#define | SO_DEBUG (2) |
Debugging information is being recorded. | |
#define | SO_DONTROUTE (3) |
Bypass normal routing. | |
#define | SO_ERROR (4) |
Socket error status. | |
#define | SO_KEEPALIVE (5) |
Connections are kept alive with periodic messages. | |
#define | SO_LINGER (6) |
Socket lingers on close. | |
#define | SO_OOBINLINE (7) |
Out-of-band data is transmitted in line. | |
#define | SO_RCVBUF (8) |
Receive buffer size. | |
#define | SO_RCVLOWAT (9) |
Receive "low water mark". | |
#define | SO_RCVTIMEO (10) |
Receive timeout. | |
#define | SO_REUSEADDR (11) |
Reuse of local addresses is supported. | |
#define | SO_SNDBUF (12) |
Send buffer size. | |
#define | SO_SNDLOWAT (13) |
Send "low water mark". | |
#define | SO_SNDTIMEO (14) |
Send timeout. | |
#define | SO_TYPE (15) |
Socket type. | |
static int | getsockopt (int socket, int level, int option_name, void *option_value, socklen_t *option_len) |
int | setsockopt (int socket, int level, int option_name, const void *option_value, socklen_t option_len) |
#define IN6_IS_ADDR_LINKLOCAL | ( | addr | ) | ((int)(ipv6_addr_is_link_local((const ipv6_addr_t *)addr))) |
#define IN6_IS_ADDR_LOOPBACK | ( | addr | ) | ((int)(ipv6_addr_is_loopback((const ipv6_addr_t *)(addr)))) |
#define IN6_IS_ADDR_MC_GLOBAL | ( | addr | ) |
Check if address is a multicast global address.
[in] | addr | address of type const struct in6_addr * |
#define IN6_IS_ADDR_MC_LINKLOCAL | ( | addr | ) |
Check if address is a multicast link-local address.
[in] | addr | address of type const struct in6_addr * |
#define IN6_IS_ADDR_MC_NODELOCAL | ( | addr | ) |
Check if address is a multicast node-local address.
[in] | addr | address of type const struct in6_addr * |
#define IN6_IS_ADDR_MC_ORGLOCAL | ( | addr | ) |
Check if address is a multicast organization-local address.
[in] | addr | address of type const struct in6_addr * |
#define IN6_IS_ADDR_MC_SITELOCAL | ( | addr | ) |
Check if address is a multicast site-local address.
[in] | addr | address of type const struct in6_addr * |
#define IN6_IS_ADDR_MULTICAST | ( | addr | ) | ((int)(ipv6_addr_is_multicast((const ipv6_addr_t *)(addr)))) |
#define IN6_IS_ADDR_SITELOCAL | ( | addr | ) | ((int)(ipv6_addr_is_site_local((const ipv6_addr_t *)addr))) |
#define IN6_IS_ADDR_UNSPECIFIED | ( | addr | ) | ((int)(ipv6_addr_is_unspecified((const ipv6_addr_t *)(addr)))) |
#define IN6_IS_ADDR_V4COMPAT | ( | addr | ) | ((int)(ipv6_addr_is_ipv4_compat((const ipv6_addr_t *)addr))) |
#define IN6_IS_ADDR_V4MAPPED | ( | addr | ) | ((int)(ipv6_addr_is_ipv4_mapped((const ipv6_addr_t *)addr))) |
#define SOCKADDR_MAX_DATA_LEN (26) |
Maximum data length for a socket address.
It is assumed that struct sockaddr_in6 is currently the longest socket address struct. As such it's data length is taken consisting of the IPv6 address (16 byte), the port (2 byte), the flow information (4 byte) and the scope ID (4 byte)
typedef uint16_t in_port_t |
Accept a new connection on a socket.
The accept() function shall extract the first connection on the queue of pending connections, create a new socket with the same socket type protocol and address family as the specified socket, and allocate a new file descriptor for that socket. If the listen queue is empty of connection requests and O_NONBLOCK is not set on the file descriptor for the socket, accept() shall block until a connection is present. If the listen() queue is empty of connection requests and O_NONBLOCK is set on the file descriptor for the socket, accept() shall fail and set errno to [EAGAIN] or [EWOULDBLOCK]. The accepted socket cannot itself accept more connections. The original socket remains open and can accept more connections.
[in] | socket | Specifies a socket that was created with socket(), has been bound to an address with bind(), and has issued a successful call to listen(). |
[out] | address | Either a null pointer, or a pointer to a sockaddr structure where the address of the connecting socket shall be returned. If address is not a null pointer, the address of the peer for the accepted connection shall be stored in the sockaddr structure pointed to by address, and the length of this address shall be stored in the object pointed to by address_len. 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 protocol permits connections by unbound clients, and the peer is not bound, then the value stored in the object pointed to by address is unspecified. |
[out] | 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. |
Bind a name to a socket.
The bind() function shall assign a local socket address address to a socket identified by descriptor socket that has no local socket address assigned. Sockets created with the socket() function are initially unnamed; they are identified only by their address family.
socket | Specifies the file descriptor of the socket to be bound. |
address | Points to a sockaddr structure containing the address to be bound to the socket. The length and format of the address depend on the address family of the socket. If the address family of the socket is AF_UNIX and the pathname in address names a symbolic link, bind() shall fail and set errno to [EADDRINUSE]. |
address_len | Specifies the length of the sockaddr structure pointed to by the address argument. |
Connect a socket.
The connect() function shall attempt to make a connection on a connection-mode socket or to set or reset the peer address of a connectionless-mode socket.
[in] | socket | Specifies the file descriptor associated with the socket. |
[in] | address | Points to a sockaddr structure containing the peer address. The length and format of the address depend on the address family of the socket. |
[in] | address_len | Specifies the length of the sockaddr structure pointed to by the address argument. |
int getpeername | ( | int | socket, |
struct sockaddr *__restrict | address, | ||
socklen_t *__restrict | address_len | ||
) |
Get the name of the peer socket.
The getpeername() function shall retrieve the peer address of the specified socket, store this address in the sockaddr structure pointed to by the address
argument, and store the length of this address in the object pointed to by the address_len
argument.
[in] | socket | Specifies the file descriptor associated with the socket. |
[out] | address | Points to a sockaddr structure containing the peer address. The length and format of the address depend on the address family of the socket. |
[in,out] | address_len | Specifies the length of the sockaddr structure on input and the length of the stored address on output. If the address is greater than the length of the supplied sockaddr structure, the stored address shal be truncated. |
int getsockname | ( | int | socket, |
struct sockaddr *__restrict | address, | ||
socklen_t *__restrict | address_len | ||
) |
Get the socket name.
The getsockname() function shall retrieve the locally-bound name of the specified socket, store this address in the sockaddr structure pointed to by the address
argument, and store the length of this address in the object pointed to by the address_len
argument.
[in] | socket | Specifies the file descriptor associated with the socket. |
[out] | address | Points to a sockaddr structure containing the peer address. The length and format of the address depend on the address family of the socket. |
[in,out] | address_len | Specifies the length of the sockaddr structure on input and the length of the stored address on output. If the address is greater than the length of the supplied sockaddr structure, the stored address shal be truncated. |
|
inlinestatic |
const char* inet_ntop | ( | int | af, |
const void *restrict | src, | ||
char *restrict | dst, | ||
socklen_t | size | ||
) |
Converts an IP address to its string representation.
[in] | af | Address family of src . Must be AF_INET or AF_INET6. |
[in] | src | An IP address. |
[out] | dst | The resulting string representation. |
[in] | size | Length of result . |
dst
, on success size
was smaller than needed src
or dst
was NULL int inet_pton | ( | int | af, |
const char * | src, | ||
void * | dst | ||
) |
Converts an IP address string representation to a byte-represented IP address.
[in] | af | Address family of src . Must be AF_INET or AF_INET6. |
[in] | src | An IP address string representation |
[out] | dst | The resulting byte representation |
src
was malformed or input pointers were NULL. af
is not supported. int listen | ( | int | socket, |
int | backlog | ||
) |
Listen for socket connections and limit the queue of incoming connections.
[in] | socket | Specifies the file descriptor associated with the socket. |
[in] | backlog | 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>. |
|
inlinestatic |
Receive a message from a connected socket.
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.
[in] | socket | Specifies the socket file descriptor. |
[out] | buffer | Points to a buffer where the message should be stored. |
[in] | length | Specifies the length in bytes of the buffer pointed to by the buffer argument. |
[in] | flags | Specifies the type of message reception. Support for values other than 0 is not implemented yet. |
ssize_t recvfrom | ( | int | socket, |
void *__restrict | buffer, | ||
size_t | length, | ||
int | flags, | ||
struct sockaddr *__restrict | address, | ||
socklen_t *__restrict | address_len | ||
) |
Receive a message from a socket.
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.
[in] | socket | Specifies the socket file descriptor. |
[out] | buffer | Points to a buffer where the message should be i stored. |
[in] | length | Specifies the length in bytes of the buffer pointed to by the buffer argument. |
[in] | flags | Specifies the type of message reception. Support for values other than 0 is not implemented yet. |
[out] | 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. |
[out] | 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. |
|
inlinestatic |
Send a message on a socket.
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.
[in] | socket | Specifies the socket file descriptor. |
[in] | buffer | Points to the buffer containing the message to send. |
[in] | length | Specifies the length of the message in bytes. |
[in] | flags | Specifies the type of message reception. Support for values other than 0 is not implemented yet. |
ssize_t sendto | ( | int | socket, |
const void * | buffer, | ||
size_t | length, | ||
int | flags, | ||
const struct sockaddr * | address, | ||
socklen_t | address_len | ||
) |
Send a message on a socket.
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 address
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 address
(overriding the pre-specified peer address), or the function shall return -1 and set errno to EISCONN.
[in] | socket | Specifies the socket file descriptor. |
[in] | buffer | Points to the buffer containing the message to send. |
[in] | length | Specifies the length of the message in bytes. |
[in] | flags | Specifies the type of message reception. Support for values other than 0 is not implemented yet. |
[in] | address | Points to a sockaddr structure containing the destination address. The length and format of the address depend on the address family of the socket. |
[in] | address_len | Specifies the length of the sockaddr structure pointed to by the address argument. |
int socket | ( | int | domain, |
int | type, | ||
int | protocol | ||
) |
Create an endpoint for communication.
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.
[in] | domain | Specifies the communications domain in which a socket is to be created. Valid values are prefixed with `AF_ and defined in socket.h. |
[in] | type | Specifies the type of socket to be created. Valued values are prefixed with SOCK_ and defined in socket.h. |
[in] | 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. |