Zephyr API Documentation 4.0.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
socket.h File Reference

BSD Sockets compatible API definitions. More...

#include <zephyr/kernel.h>
#include <sys/types.h>
#include <zephyr/types.h>
#include <zephyr/device.h>
#include <zephyr/net/net_ip.h>
#include <zephyr/net/socket_select.h>
#include <zephyr/net/socket_poll.h>
#include <zephyr/sys/iterable_sections.h>
#include <zephyr/sys/fdtable.h>
#include <zephyr/net/dns_resolve.h>
#include <stdlib.h>
#include <zephyr/syscalls/socket.h>

Go to the source code of this file.

Data Structures

struct  zsock_addrinfo
 Definition used when querying address information. More...
 
struct  ifreq
 Interface description structure. More...
 
struct  in_pktinfo
 Incoming IPv4 packet information. More...
 
struct  ip_mreqn
 Struct used when joining or leaving a IPv4 multicast group. More...
 
struct  ipv6_mreq
 Struct used when joining or leaving a IPv6 multicast group. More...
 
struct  in6_pktinfo
 Incoming IPv6 packet information. More...
 

Macros

Options for poll()
#define ZSOCK_POLLIN   1
 zsock_poll: Poll for readability
 
#define ZSOCK_POLLPRI   2
 zsock_poll: Poll for exceptional condition
 
#define ZSOCK_POLLOUT   4
 zsock_poll: Poll for writability
 
#define ZSOCK_POLLERR   8
 zsock_poll: Poll results in error condition (output value only)
 
#define ZSOCK_POLLHUP   0x10
 zsock_poll: Poll detected closed connection (output value only)
 
#define ZSOCK_POLLNVAL   0x20
 zsock_poll: Invalid socket (output value only)
 
Options for sending and receiving data
#define ZSOCK_MSG_PEEK   0x02
 zsock_recv: Read data without removing it from socket input queue
 
#define ZSOCK_MSG_CTRUNC   0x08
 zsock_recvmsg: Control data buffer too small.
 
#define ZSOCK_MSG_TRUNC   0x20
 zsock_recv: return the real length of the datagram, even when it was longer than the passed buffer
 
#define ZSOCK_MSG_DONTWAIT   0x40
 zsock_recv/zsock_send: Override operation to non-blocking
 
#define ZSOCK_MSG_WAITALL   0x100
 zsock_recv: block until the full amount of data can be returned
 
Options for shutdown() function
#define ZSOCK_SHUT_RD   0
 zsock_shutdown: Shut down for reading
 
#define ZSOCK_SHUT_WR   1
 zsock_shutdown: Shut down for writing
 
#define ZSOCK_SHUT_RDWR   2
 zsock_shutdown: Shut down for both reading and writing
 
Socket options for TLS
#define SOL_TLS   282
 Protocol level for TLS.
 
#define TLS_SEC_TAG_LIST   1
 Socket option to select TLS credentials to use.
 
#define TLS_HOSTNAME   2
 Write-only socket option to set hostname.
 
#define TLS_CIPHERSUITE_LIST   3
 Socket option to select ciphersuites to use.
 
#define TLS_CIPHERSUITE_USED   4
 Read-only socket option to read a ciphersuite chosen during TLS handshake.
 
#define TLS_PEER_VERIFY   5
 Write-only socket option to set peer verification level for TLS connection.
 
#define TLS_DTLS_ROLE   6
 Write-only socket option to set role for DTLS connection.
 
#define TLS_ALPN_LIST   7
 Socket option for setting the supported Application Layer Protocols.
 
#define TLS_DTLS_HANDSHAKE_TIMEOUT_MIN   8
 Socket option to set DTLS min handshake timeout.
 
#define TLS_DTLS_HANDSHAKE_TIMEOUT_MAX   9
 Socket option to set DTLS max handshake timeout.
 
#define TLS_CERT_NOCOPY   10
 Socket option for preventing certificates from being copied to the mbedTLS heap if possible.
 
#define TLS_NATIVE   11
 TLS socket option to use with offloading.
 
#define TLS_SESSION_CACHE   12
 Socket option to control TLS session caching on a socket.
 
#define TLS_SESSION_CACHE_PURGE   13
 Write-only socket option to purge session cache immediately.
 
#define TLS_DTLS_CID   14
 Write-only socket option to control DTLS CID.
 
#define TLS_DTLS_CID_STATUS   15
 Read-only socket option to get DTLS CID status.
 
#define TLS_DTLS_CID_VALUE   16
 Socket option to set or get the value of the DTLS connection ID to be used for the DTLS session.
 
#define TLS_DTLS_PEER_CID_VALUE   17
 Read-only socket option to get the value of the DTLS connection ID received from the peer.
 
#define TLS_DTLS_HANDSHAKE_ON_CONNECT   18
 Socket option to configure DTLS socket behavior on connect().
 
#define TLS_PEER_VERIFY_NONE   0
 Peer verification disabled.
 
#define TLS_PEER_VERIFY_OPTIONAL   1
 Peer verification optional.
 
#define TLS_PEER_VERIFY_REQUIRED   2
 Peer verification required.
 
#define TLS_DTLS_ROLE_CLIENT   0
 Client role in a DTLS session.
 
#define TLS_DTLS_ROLE_SERVER   1
 Server role in a DTLS session.
 
#define TLS_CERT_NOCOPY_NONE   0
 Cert duplicated in heap.
 
#define TLS_CERT_NOCOPY_OPTIONAL   1
 Cert not copied in heap if DER.
 
#define TLS_SESSION_CACHE_DISABLED   0
 Disable TLS session caching.
 
#define TLS_SESSION_CACHE_ENABLED   1
 Enable TLS session caching.
 
#define TLS_DTLS_CID_DISABLED   0
 CID is disabled

 
#define TLS_DTLS_CID_SUPPORTED   1
 CID is supported.
 
#define TLS_DTLS_CID_ENABLED   2
 CID is enabled

 
#define TLS_DTLS_CID_STATUS_DISABLED   0
 CID is disabled.
 
#define TLS_DTLS_CID_STATUS_DOWNLINK   1
 CID is in use by us.
 
#define TLS_DTLS_CID_STATUS_UPLINK   2
 CID is in use by peer.
 
#define TLS_DTLS_CID_STATUS_BIDIRECTIONAL   3
 CID is in use by us and peer.
 
Flags for getaddrinfo() hints
#define AI_PASSIVE   0x1
 Address for bind() (vs for connect())
 
#define AI_CANONNAME   0x2
 Fill in ai_canonname.
 
#define AI_NUMERICHOST   0x4
 Assume host address is in numeric notation, don't DNS lookup.
 
#define AI_V4MAPPED   0x8
 May return IPv4 mapped address for IPv6

 
#define AI_ALL   0x10
 May return both native IPv6 and mapped IPv4 address for IPv6.
 
#define AI_ADDRCONFIG   0x20
 IPv4/IPv6 support depends on local system config.
 
#define AI_NUMERICSERV   0x400
 Assume service (port) is numeric.
 
#define AI_EXTFLAGS   0x800
 Extra flags present (see RFC 5014)
 
Flags for getnameinfo()
#define NI_NUMERICHOST   1
 zsock_getnameinfo(): Resolve to numeric address.
 
#define NI_NUMERICSERV   2
 zsock_getnameinfo(): Resolve to numeric port number.
 
#define NI_NOFQDN   4
 zsock_getnameinfo(): Return only hostname instead of FQDN
 
#define NI_NAMEREQD   8
 zsock_getnameinfo(): Dummy option for compatibility
 
#define NI_DGRAM   16
 zsock_getnameinfo(): Dummy option for compatibility
 
#define NI_MAXHOST   64
 zsock_getnameinfo(): Max supported hostname length
 
Network interface name description
#define IFNAMSIZ   Z_DEVICE_MAX_NAME_LEN
 Network interface name length.
 
Socket level options (SOL_SOCKET)
#define SOL_SOCKET   1
 Socket-level option.
 
#define SO_DEBUG   1
 Recording debugging information (ignored, for compatibility)
 
#define SO_REUSEADDR   2
 address reuse
 
#define SO_TYPE   3
 Type of the socket.
 
#define SO_ERROR   4
 Async error.
 
#define SO_DONTROUTE   5
 Bypass normal routing and send directly to host (ignored, for compatibility)
 
#define SO_BROADCAST   6
 Transmission of broadcast messages is supported (ignored, for compatibility)
 
#define SO_SNDBUF   7
 Size of socket send buffer.
 
#define SO_RCVBUF   8
 Size of socket recv buffer.
 
#define SO_KEEPALIVE   9
 Enable sending keep-alive messages on connections.
 
#define SO_OOBINLINE   10
 Place out-of-band data into receive stream (ignored, for compatibility)
 
#define SO_PRIORITY   12
 Socket priority.
 
#define SO_LINGER   13
 Socket lingers on close (ignored, for compatibility)
 
#define SO_REUSEPORT   15
 Allow multiple sockets to reuse a single port.
 
#define SO_RCVLOWAT   18
 Receive low watermark (ignored, for compatibility)
 
#define SO_SNDLOWAT   19
 Send low watermark (ignored, for compatibility)
 
#define SO_RCVTIMEO   20
 Receive timeout Applies to receive functions like recv(), but not to connect()
 
#define SO_SNDTIMEO   21
 Send timeout.
 
#define SO_BINDTODEVICE   25
 Bind a socket to an interface.
 
#define SO_ACCEPTCONN   30
 Socket accepts incoming connections (ignored, for compatibility)
 
#define SO_TIMESTAMPING   37
 Timestamp TX RX or both packets.
 
#define SO_PROTOCOL   38
 Protocol used with the socket.
 
#define SO_DOMAIN   39
 Domain used with SOCKET.
 
#define SO_SOCKS5   60
 Enable SOCKS5 for Socket.
 
#define SO_TXTIME   61
 Socket TX time (when the data should be sent)
 
#define SCM_TXTIME   SO_TXTIME
 Socket TX time (same as SO_TXTIME)
 
#define SOF_TIMESTAMPING_RX_HARDWARE   BIT(0)
 Timestamp generation flags.
 
#define SOF_TIMESTAMPING_TX_HARDWARE   BIT(1)
 Request TX timestamps generated by network adapter.
 
TCP level options (IPPROTO_TCP)
#define TCP_NODELAY   1
 Disable TCP buffering (ignored, for compatibility)
 
#define TCP_KEEPIDLE   2
 Start keepalives after this period (seconds)
 
#define TCP_KEEPINTVL   3
 Interval between keepalives (seconds)
 
#define TCP_KEEPCNT   4
 Number of keepalives before dropping connection.
 
IPv4 level options (IPPROTO_IP)
#define IP_TOS   1
 Set or receive the Type-Of-Service value for an outgoing packet.
 
#define IP_TTL   2
 Set or receive the Time-To-Live value for an outgoing packet.
 
#define IP_PKTINFO   8
 Pass an IP_PKTINFO ancillary message that contains a pktinfo structure that supplies some information about the incoming packet.
 
#define IP_MULTICAST_TTL   33
 Set IPv4 multicast TTL value.
 
#define IP_ADD_MEMBERSHIP   35
 Join IPv4 multicast group.
 
#define IP_DROP_MEMBERSHIP   36
 Leave IPv4 multicast group.
 
IPv6 level options (IPPROTO_IPV6)
#define IPV6_UNICAST_HOPS   16
 Set the unicast hop limit for the socket.
 
#define IPV6_MULTICAST_HOPS   18
 Set the multicast hop limit for the socket.
 
#define IPV6_ADD_MEMBERSHIP   20
 Join IPv6 multicast group.
 
#define IPV6_DROP_MEMBERSHIP   21
 Leave IPv6 multicast group.
 
#define IPV6_JOIN_GROUP   IPV6_ADD_MEMBERSHIP
 Join IPv6 multicast group.
 
#define IPV6_LEAVE_GROUP   IPV6_DROP_MEMBERSHIP
 Leave IPv6 multicast group.
 
#define IPV6_V6ONLY   26
 Don't support IPv4 access.
 
#define IPV6_RECVPKTINFO   49
 Pass an IPV6_RECVPKTINFO ancillary message that contains a in6_pktinfo structure that supplies some information about the incoming packet.
 
#define IPV6_ADDR_PREFERENCES   72
 RFC5014: Source address selection.
 
#define IPV6_PREFER_SRC_TMP   0x0001
 Prefer temporary address as source.
 
#define IPV6_PREFER_SRC_PUBLIC   0x0002
 Prefer public address as source.
 
#define IPV6_PREFER_SRC_PUBTMP_DEFAULT   0x0100
 Either public or temporary address is selected as a default source depending on the output interface configuration (this is the default value).
 
#define IPV6_PREFER_SRC_COA   0x0004
 Prefer Care-of address as source.
 
#define IPV6_PREFER_SRC_HOME   0x0400
 Prefer Home address as source.
 
#define IPV6_PREFER_SRC_CGA   0x0008
 Prefer CGA (Cryptographically Generated Address) address as source.
 
#define IPV6_PREFER_SRC_NONCGA   0x0800
 Prefer non-CGA address as source.
 
#define IPV6_TCLASS   67
 Set or receive the traffic class value for an outgoing packet.
 
Backlog size for listen()
#define SOMAXCONN   128
 listen: The maximum backlog queue length
 
Macros for checking special IPv6 addresses.
#define IN6_IS_ADDR_UNSPECIFIED(addr)
 Check unspecified IPv6 address.
 
#define IN6_IS_ADDR_LOOPBACK(addr)
 Check loopback IPv6 address.
 
#define IN6_IS_ADDR_MULTICAST(addr)
 Check IPv6 multicast address.
 
#define IN6_IS_ADDR_LINKLOCAL(addr)
 Check IPv6 link local address.
 
#define IN6_IS_ADDR_SITELOCAL(addr)
 Check IPv6 site local address.
 
#define IN6_IS_ADDR_V4MAPPED(addr)
 Check IPv6 v4 mapped address.
 
#define IN6_IS_ADDR_MC_GLOBAL(addr)
 Check IPv6 multicast global address.
 
#define IN6_IS_ADDR_MC_NODELOCAL(addr)
 Check IPv6 multicast node local address.
 
#define IN6_IS_ADDR_MC_LINKLOCAL(addr)
 Check IPv6 multicast link local address.
 
#define IN6_IS_ADDR_MC_SITELOCAL(addr)
 Check IPv6 multicast site local address.
 
#define IN6_IS_ADDR_MC_ORGLOCAL(addr)
 Check IPv6 multicast organization local address.
 

Functions

void * zsock_get_context_object (int sock)
 Obtain a file descriptor's associated net context.
 
int zsock_socket (int family, int type, int proto)
 Create a network socket.
 
int zsock_socketpair (int family, int type, int proto, int *sv)
 Create an unnamed pair of connected sockets.
 
int zsock_close (int sock)
 Close a network socket.
 
int zsock_shutdown (int sock, int how)
 Shutdown socket send/receive operations.
 
int zsock_bind (int sock, const struct sockaddr *addr, socklen_t addrlen)
 Bind a socket to a local network address.
 
int zsock_connect (int sock, const struct sockaddr *addr, socklen_t addrlen)
 Connect a socket to a peer network address.
 
int zsock_listen (int sock, int backlog)
 Set up a STREAM socket to accept peer connections.
 
int zsock_accept (int sock, struct sockaddr *addr, socklen_t *addrlen)
 Accept a connection on listening socket.
 
ssize_t zsock_sendto (int sock, const void *buf, size_t len, int flags, const struct sockaddr *dest_addr, socklen_t addrlen)
 Send data to an arbitrary network address.
 
static ssize_t zsock_send (int sock, const void *buf, size_t len, int flags)
 Send data to a connected peer.
 
ssize_t zsock_sendmsg (int sock, const struct msghdr *msg, int flags)
 Send data to an arbitrary network address.
 
ssize_t zsock_recvfrom (int sock, void *buf, size_t max_len, int flags, struct sockaddr *src_addr, socklen_t *addrlen)
 Receive data from an arbitrary network address.
 
ssize_t zsock_recvmsg (int sock, struct msghdr *msg, int flags)
 Receive a message from an arbitrary network address.
 
static ssize_t zsock_recv (int sock, void *buf, size_t max_len, int flags)
 Receive data from a connected peer.
 
int zsock_fcntl_impl (int sock, int cmd, int flags)
 Control blocking/non-blocking mode of a socket.
 
int zsock_ioctl_impl (int sock, unsigned long request, va_list ap)
 Control underlying socket parameters.
 
static int zsock_poll (struct zsock_pollfd *fds, int nfds, int timeout)
 Efficiently poll multiple sockets for events.
 
int zsock_getsockopt (int sock, int level, int optname, void *optval, socklen_t *optlen)
 Get various socket options.
 
int zsock_setsockopt (int sock, int level, int optname, const void *optval, socklen_t optlen)
 Set various socket options.
 
int zsock_getpeername (int sock, struct sockaddr *addr, socklen_t *addrlen)
 Get peer name.
 
int zsock_getsockname (int sock, struct sockaddr *addr, socklen_t *addrlen)
 Get socket name.
 
int zsock_gethostname (char *buf, size_t len)
 Get local host name.
 
static char * zsock_inet_ntop (sa_family_t family, const void *src, char *dst, size_t size)
 Convert network address from internal to numeric ASCII form.
 
int zsock_inet_pton (sa_family_t family, const char *src, void *dst)
 Convert network address from numeric ASCII form to internal representation.
 
int zsock_getaddrinfo (const char *host, const char *service, const struct zsock_addrinfo *hints, struct zsock_addrinfo **res)
 Resolve a domain name to one or more network addresses.
 
void zsock_freeaddrinfo (struct zsock_addrinfo *ai)
 Free results returned by zsock_getaddrinfo()
 
const char * zsock_gai_strerror (int errcode)
 Convert zsock_getaddrinfo() error code to textual message.
 
int zsock_getnameinfo (const struct sockaddr *addr, socklen_t addrlen, char *host, socklen_t hostlen, char *serv, socklen_t servlen, int flags)
 Resolve a network address to a domain name or ASCII address.
 

Socket APIs available if CONFIG_NET_SOCKETS_POSIX_NAMES is enabled

#define pollfd   zsock_pollfd
 POSIX wrapper for zsock_pollfd.
 
#define addrinfo   zsock_addrinfo
 POSIX wrapper for zsock_addrinfo.
 
#define POLLIN   ZSOCK_POLLIN
 POSIX wrapper for ZSOCK_POLLIN.
 
#define POLLOUT   ZSOCK_POLLOUT
 POSIX wrapper for ZSOCK_POLLOUT.
 
#define POLLERR   ZSOCK_POLLERR
 POSIX wrapper for ZSOCK_POLLERR.
 
#define POLLHUP   ZSOCK_POLLHUP
 POSIX wrapper for ZSOCK_POLLHUP.
 
#define POLLNVAL   ZSOCK_POLLNVAL
 POSIX wrapper for ZSOCK_POLLNVAL.
 
#define MSG_PEEK   ZSOCK_MSG_PEEK
 POSIX wrapper for ZSOCK_MSG_PEEK.
 
#define MSG_CTRUNC   ZSOCK_MSG_CTRUNC
 POSIX wrapper for ZSOCK_MSG_CTRUNC.
 
#define MSG_TRUNC   ZSOCK_MSG_TRUNC
 POSIX wrapper for ZSOCK_MSG_TRUNC.
 
#define MSG_DONTWAIT   ZSOCK_MSG_DONTWAIT
 POSIX wrapper for ZSOCK_MSG_DONTWAIT.
 
#define MSG_WAITALL   ZSOCK_MSG_WAITALL
 POSIX wrapper for ZSOCK_MSG_WAITALL.
 
#define SHUT_RD   ZSOCK_SHUT_RD
 POSIX wrapper for ZSOCK_SHUT_RD.
 
#define SHUT_WR   ZSOCK_SHUT_WR
 POSIX wrapper for ZSOCK_SHUT_WR.
 
#define SHUT_RDWR   ZSOCK_SHUT_RDWR
 POSIX wrapper for ZSOCK_SHUT_RDWR.
 
#define EAI_BADFLAGS   DNS_EAI_BADFLAGS
 POSIX wrapper for DNS_EAI_BADFLAGS.
 
#define EAI_NONAME   DNS_EAI_NONAME
 POSIX wrapper for DNS_EAI_NONAME.
 
#define EAI_AGAIN   DNS_EAI_AGAIN
 POSIX wrapper for DNS_EAI_AGAIN.
 
#define EAI_FAIL   DNS_EAI_FAIL
 POSIX wrapper for DNS_EAI_FAIL.
 
#define EAI_NODATA   DNS_EAI_NODATA
 POSIX wrapper for DNS_EAI_NODATA.
 
#define EAI_MEMORY   DNS_EAI_MEMORY
 POSIX wrapper for DNS_EAI_MEMORY.
 
#define EAI_SYSTEM   DNS_EAI_SYSTEM
 POSIX wrapper for DNS_EAI_SYSTEM.
 
#define EAI_SERVICE   DNS_EAI_SERVICE
 POSIX wrapper for DNS_EAI_SERVICE.
 
#define EAI_SOCKTYPE   DNS_EAI_SOCKTYPE
 POSIX wrapper for DNS_EAI_SOCKTYPE.
 
#define EAI_FAMILY   DNS_EAI_FAMILY
 POSIX wrapper for DNS_EAI_FAMILY.
 
static int socket (int family, int type, int proto)
 POSIX wrapper for zsock_socket.
 
static int socketpair (int family, int type, int proto, int sv[2])
 POSIX wrapper for zsock_socketpair.
 
static int close (int sock)
 POSIX wrapper for zsock_close.
 
static int shutdown (int sock, int how)
 POSIX wrapper for zsock_shutdown.
 
static int bind (int sock, const struct sockaddr *addr, socklen_t addrlen)
 POSIX wrapper for zsock_bind.
 
static int connect (int sock, const struct sockaddr *addr, socklen_t addrlen)
 POSIX wrapper for zsock_connect.
 
static int listen (int sock, int backlog)
 POSIX wrapper for zsock_listen.
 
static int accept (int sock, struct sockaddr *addr, socklen_t *addrlen)
 POSIX wrapper for zsock_accept.
 
static ssize_t send (int sock, const void *buf, size_t len, int flags)
 POSIX wrapper for zsock_send.
 
static ssize_t recv (int sock, void *buf, size_t max_len, int flags)
 POSIX wrapper for zsock_recv.
 
static ssize_t sendto (int sock, const void *buf, size_t len, int flags, const struct sockaddr *dest_addr, socklen_t addrlen)
 POSIX wrapper for zsock_sendto.
 
static ssize_t sendmsg (int sock, const struct msghdr *message, int flags)
 POSIX wrapper for zsock_sendmsg.
 
static ssize_t recvfrom (int sock, void *buf, size_t max_len, int flags, struct sockaddr *src_addr, socklen_t *addrlen)
 POSIX wrapper for zsock_recvfrom.
 
static ssize_t recvmsg (int sock, struct msghdr *msg, int flags)
 POSIX wrapper for zsock_recvmsg.
 
static int poll (struct zsock_pollfd *fds, int nfds, int timeout)
 POSIX wrapper for zsock_poll.
 
static int getsockopt (int sock, int level, int optname, void *optval, socklen_t *optlen)
 POSIX wrapper for zsock_getsockopt.
 
static int setsockopt (int sock, int level, int optname, const void *optval, socklen_t optlen)
 POSIX wrapper for zsock_setsockopt.
 
static int getpeername (int sock, struct sockaddr *addr, socklen_t *addrlen)
 POSIX wrapper for zsock_getpeername.
 
static int getsockname (int sock, struct sockaddr *addr, socklen_t *addrlen)
 POSIX wrapper for zsock_getsockname.
 
static int getaddrinfo (const char *host, const char *service, const struct zsock_addrinfo *hints, struct zsock_addrinfo **res)
 POSIX wrapper for zsock_getaddrinfo.
 
static void freeaddrinfo (struct zsock_addrinfo *ai)
 POSIX wrapper for zsock_freeaddrinfo.
 
static const char * gai_strerror (int errcode)
 POSIX wrapper for zsock_gai_strerror.
 
static int getnameinfo (const struct sockaddr *addr, socklen_t addrlen, char *host, socklen_t hostlen, char *serv, socklen_t servlen, int flags)
 POSIX wrapper for zsock_getnameinfo.
 
static int gethostname (char *buf, size_t len)
 POSIX wrapper for zsock_gethostname.
 
static int inet_pton (sa_family_t family, const char *src, void *dst)
 POSIX wrapper for zsock_inet_pton.
 
static char * inet_ntop (sa_family_t family, const void *src, char *dst, size_t size)
 POSIX wrapper for zsock_inet_ntop.
 

Detailed Description

BSD Sockets compatible API definitions.

An API for applications to use BSD Sockets like API.