[ English | 简体中文 ]
Network API
openvela provides BSD-compatible socket interfaces, supporting protocol families such as IPv4 (AF_INET), IPv6 (AF_INET6), as well as stream sockets (SOCK_STREAM), datagram sockets (SOCK_DGRAM), and raw sockets (SOCK_RAW).
Header files: #include <sys/socket.h>, #include <netinet/in.h>, #include <arpa/inet.h>
openvela Implementation Notes
- Protocol family support:
AF_INET(IPv4),AF_INET6(IPv6),AF_LOCAL/AF_UNIX(local sockets),AF_PACKET(raw link layer), etc., depending on network stack configuration - Configuration dependency: The network subsystem is optional and requires enabling
CONFIG_NETand related protocol configurations (e.g.,CONFIG_NET_TCP,CONFIG_NET_UDP) - Non-blocking I/O: Set via
fcntl(fd, F_SETFL, O_NONBLOCK)or theSOCK_NONBLOCKflag - DNS resolution: Requires enabling
CONFIG_NETDB_DNSCLIENT, DNS servers are managed through thenuttx/net/dns.hinterface
Socket Creation and Management
socket
int socket(int domain, int type, int protocol);
Creates a communication endpoint (socket) and returns a file descriptor.
Parameters:
domainProtocol family:AF_INETIPv4AF_INET6IPv6AF_LOCAL/AF_UNIXLocal socketAF_PACKETRaw link layer
typeSocket type:SOCK_STREAMConnection-oriented stream socket (TCP)SOCK_DGRAMConnectionless datagram socket (UDP)SOCK_RAWRaw socket- Can be bitwise OR'd with
SOCK_NONBLOCK,SOCK_CLOEXEC
protocolProtocol number, usually 0 (auto-select). Can also specifyIPPROTO_TCP,IPPROTO_UDP, etc.
Returns:
Returns a non-negative file descriptor on success, or -1 on failure and sets errno:
EAFNOSUPPORTUnsupported protocol family.EPROTONOSUPPORTUnsupported protocol type.EMFILEProcess file descriptor limit reached.ENOMEMInsufficient memory.
POSIX Compatibility: Compatible with the POSIX/BSD interface of the same name.
socketpair
int socketpair(int domain, int type, int protocol, int sv[2]);
Creates a pair of connected sockets, commonly used for inter-process communication between parent and child processes.
Parameters:
domainProtocol family, usuallyAF_LOCAL.typeSocket type (SOCK_STREAMorSOCK_DGRAM).protocolUsually 0.svOutput parameter, stores two connected file descriptors.
Returns:
Returns 0 on success, or -1 on failure and sets errno:
EAFNOSUPPORTUnsupported protocol family.EMFILEFile descriptor limit reached.
POSIX Compatibility: Compatible with the POSIX/BSD interface of the same name.
shutdown
int shutdown(int sockfd, int how);
Shuts down part or all communication directions of a socket. Unlike close(), shutdown() can close only the read or write direction.
Parameters:
sockfdSocket file descriptor.howShutdown mode:SHUT_RDClose the read direction.SHUT_WRClose the write direction (sends FIN).SHUT_RDWRClose both read and write directions.
Returns:
Returns 0 on success, or -1 on failure and sets errno:
EBADFInvalid file descriptor.ENOTCONNSocket is not connected.EINVALInvalidhowparameter.
POSIX Compatibility: Compatible with the POSIX/BSD interface of the same name.
Connection Management
bind
int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
Binds a socket to a specified local address and port. The server side must call bind() before listen().
Parameters:
sockfdSocket file descriptor.addrLocal address structure (struct sockaddr_inorstruct sockaddr_in6).addrlenSize of the address structure.
Returns:
Returns 0 on success, or -1 on failure and sets errno:
EBADFInvalid file descriptor.EINVALSocket is already bound, or address is invalid.EADDRINUSEAddress already in use.EADDRNOTAVAILRequested address is not available.
POSIX Compatibility: Compatible with the POSIX/BSD interface of the same name.
connect
int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
Initiates a connection to a remote address. For TCP sockets, performs the three-way handshake; for UDP sockets, sets the default destination address.
Parameters:
sockfdSocket file descriptor.addrRemote address structure.addrlenSize of the address structure.
Returns:
Returns 0 on success, or -1 on failure and sets errno:
EBADFInvalid file descriptor.ECONNREFUSEDRemote host refused the connection.ETIMEDOUTConnection timed out.ENETUNREACHNetwork is unreachable.EINPROGRESSConnection is in progress in non-blocking mode.EISCONNSocket is already connected.
POSIX Compatibility: Compatible with the POSIX/BSD interface of the same name.
listen
int listen(int sockfd, int backlog);
Marks a socket as a passive listening socket, ready to accept connection requests.
Parameters:
sockfdBound socket file descriptor.backlogMaximum length of the pending connection queue.
Returns:
Returns 0 on success, or -1 on failure and sets errno:
EBADFInvalid file descriptor.EOPNOTSUPPSocket type does not supportlisten().
POSIX Compatibility: Compatible with the POSIX/BSD interface of the same name.
accept
int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
Extracts the first connection request from the listening socket's pending queue, creates and returns a new connected socket. Blocks if the queue is empty.
Parameters:
sockfdListening socket file descriptor.addrIf non-NULL, stores the client address.addrlenOn input, the size of theaddrbuffer; on output, the actual address size.
Returns:
Returns a new connected socket descriptor on success, or -1 on failure and sets errno:
EBADFInvalid file descriptor.EMFILEFile descriptor limit reached.ECONNABORTEDConnection was aborted.EINTRInterrupted by a signal.
POSIX Compatibility: Compatible with the POSIX/BSD interface of the same name.
accept4
int accept4(int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags);
Same as accept(), but allows setting attributes on the new socket via flags.
Parameters:
sockfdListening socket file descriptor.addrClient address (can beNULL).addrlenAddress length.flagsFlags:SOCK_NONBLOCKSet the new socket to non-blocking.SOCK_CLOEXECSet the new socket to close-on-exec.
Returns:
Same as accept().
POSIX Compatibility: Compatible with Linux extension (non-POSIX).
Data Sending
send
ssize_t send(int sockfd, const void *buf, size_t len, int flags);
Sends data on a connected socket. Equivalent to sendto() without specifying a destination address.
Parameters:
sockfdConnected socket file descriptor.bufData buffer to send.lenData length (bytes).flagsSend flags:MSG_DONTWAITNon-blocking send.MSG_NOSIGNALDo not generateSIGPIPEwhen the peer closes.0Default behavior.
Returns:
Returns the number of bytes sent on success, or -1 on failure and sets errno:
EBADFInvalid file descriptor.ENOTCONNSocket is not connected.EAGAIN/EWOULDBLOCKSend buffer is full in non-blocking mode.EPIPEPeer has closed the connection.EINTRInterrupted by a signal.
POSIX Compatibility: Compatible with the POSIX/BSD interface of the same name.
sendto
ssize_t sendto(int sockfd, const void *buf, size_t len, int flags,
const struct sockaddr *to, socklen_t tolen);
Sends data to a specified address. Primarily used for UDP sockets; can also be used on connected TCP sockets (destination address is ignored in that case).
Parameters:
sockfdSocket file descriptor.bufData buffer.lenData length.flagsSend flags (same assend()).toDestination address. Can beNULLfor connected sockets.tolenDestination address length.
Returns:
Returns the number of bytes sent on success, or -1 on failure and sets errno (same as send(), plus):
EDESTADDRREQUnconnected socket and no destination address specified.
POSIX Compatibility: Compatible with the POSIX/BSD interface of the same name.
sendmsg
ssize_t sendmsg(int sockfd, const struct msghdr *msg, int flags);
Sends data via an msghdr structure, supporting scatter/gather I/O and ancillary data (e.g., file descriptor passing).
Parameters:
sockfdSocket file descriptor.msgMessage header structure containing destination address, I/O vectors, ancillary data, etc.flagsSend flags.
Returns:
Returns the number of bytes sent on success, or -1 on failure and sets errno.
POSIX Compatibility: Compatible with the POSIX/BSD interface of the same name.
Data Receiving
recv
ssize_t recv(int sockfd, void *buf, size_t len, int flags);
Receives data from a connected socket.
Parameters:
sockfdConnected socket file descriptor.bufReceive buffer.lenBuffer size (bytes).flagsReceive flags:MSG_DONTWAITNon-blocking receive.MSG_PEEKPeek at data without removing it.MSG_WAITALLWait to receive the fulllenbytes.0Default behavior.
Returns:
Returns the number of bytes received on success (0 indicates the peer closed the connection), or -1 on failure and sets errno:
EBADFInvalid file descriptor.ENOTCONNSocket is not connected.EAGAIN/EWOULDBLOCKNo data available in non-blocking mode.EINTRInterrupted by a signal.
POSIX Compatibility: Compatible with the POSIX/BSD interface of the same name.
recvfrom
ssize_t recvfrom(int sockfd, void *buf, size_t len, int flags,
struct sockaddr *from, socklen_t *fromlen);
Receives data and obtains the sender's address. Primarily used for UDP sockets.
Parameters:
sockfdSocket file descriptor.bufReceive buffer.lenBuffer size.flagsReceive flags (same asrecv()).fromIf non-NULL, stores the sender's address.fromlenOn input, the size of thefrombuffer; on output, the actual address size.
Returns:
Returns the number of bytes received on success, or -1 on failure and sets errno (same as recv()).
POSIX Compatibility: Compatible with the POSIX/BSD interface of the same name.
recvmsg
ssize_t recvmsg(int sockfd, struct msghdr *msg, int flags);
Receives data via an msghdr structure, supporting scatter/gather I/O and ancillary data.
Parameters:
sockfdSocket file descriptor.msgMessage header structure.flagsReceive flags.
Returns:
Returns the number of bytes received on success, or -1 on failure and sets errno.
POSIX Compatibility: Compatible with the POSIX/BSD interface of the same name.
Socket Options
setsockopt
int setsockopt(int sockfd, int level, int option, const void *value, socklen_t value_len);
Sets socket options.
Parameters:
sockfdSocket file descriptor.levelProtocol layer of the option:SOL_SOCKETSocket-level options.IPPROTO_TCPTCP-level options.IPPROTO_IPIP-level options.IPPROTO_IPV6IPv6-level options.
optionOption name (e.g.,SO_REUSEADDR,SO_KEEPALIVE,TCP_NODELAY, etc.).valueOption value.value_lenSize of the option value.
Returns:
Returns 0 on success, or -1 on failure and sets errno:
EBADFInvalid file descriptor.ENOPROTOOPTUnsupported option.EINVALInvalid option value.
POSIX Compatibility: Compatible with the POSIX/BSD interface of the same name.
getsockopt
int getsockopt(int sockfd, int level, int option, void *value, socklen_t *value_len);
Gets socket options.
Parameters:
sockfdSocket file descriptor.levelProtocol layer (same assetsockopt()).optionOption name.valueBuffer to store the option value.value_lenOn input, the buffer size; on output, the actual value size.
Returns:
Returns 0 on success, or -1 on failure and sets errno (same as setsockopt()).
POSIX Compatibility: Compatible with the POSIX/BSD interface of the same name.
Address Query
getsockname
int getsockname(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
Gets the local address bound to a socket.
Parameters:
sockfdSocket file descriptor.addrBuffer to store the local address.addrlenOn input, the buffer size; on output, the actual address size.
Returns:
Returns 0 on success, or -1 on failure and sets errno:
EBADFInvalid file descriptor.EINVALInvalidaddrlen.
POSIX Compatibility: Compatible with the POSIX/BSD interface of the same name.
getpeername
int getpeername(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
Gets the remote address of a connected socket.
Parameters:
sockfdConnected socket file descriptor.addrBuffer to store the remote address.addrlenOn input, the buffer size; on output, the actual address size.
Returns:
Returns 0 on success, or -1 on failure and sets errno:
EBADFInvalid file descriptor.ENOTCONNSocket is not connected.
POSIX Compatibility: Compatible with the POSIX/BSD interface of the same name.
DNS Interface
Header file: #include <nuttx/net/dns.h>
dns_add_nameserver
int dns_add_nameserver(const struct sockaddr *addr, socklen_t addrlen);
Adds a DNS name server.
Parameters:
addrDNS server address (struct sockaddr_inorstruct sockaddr_in6).addrlenSize of the address structure.
Returns:
Returns 0 on success, or a negative error code on failure.
POSIX Compatibility: openvela/NuttX extension.
dns_default_nameserver
int dns_default_nameserver(void);
Resets the DNS resolver to use only the default DNS server.
Returns:
Returns 0 on success, or a negative error code on failure.
POSIX Compatibility: openvela/NuttX extension.
dns_foreach_nameserver
int dns_foreach_nameserver(dns_callback_t callback, void *arg);
Iterates over all configured DNS servers, calling the callback function for each server.
Parameters:
callbackCallback function, called for each DNS server.argUser argument passed to the callback function.
Returns:
Returns 0 on success, or a negative error code on failure.
POSIX Compatibility: openvela/NuttX extension.
dns_register_notify
int dns_register_notify(dns_callback_t callback, void *arg);
Registers a DNS server change notification. The callback function is called when the DNS server list changes.
Parameters:
callbackChange notification callback function.argUser argument passed to the callback function.
Returns:
Returns 0 on success, or a negative error code on failure.
POSIX Compatibility: openvela/NuttX extension.
dns_unregister_notify
int dns_unregister_notify(dns_callback_t callback, void *arg);
Unregisters a DNS server change notification.
Parameters:
callbackPreviously registered callback function.argUser argument provided during registration.
Returns:
Returns 0 on success, or a negative error code on failure.
POSIX Compatibility: openvela/NuttX extension.
dns_set_queryfamily
int dns_set_queryfamily(sa_family_t family);
Sets the address family used for DNS queries.
Parameters:
familyAddress family (AF_INET,AF_INET6, orAF_UNSPEC).
Returns:
Returns 0 on success, or a negative error code on failure.
POSIX Compatibility: openvela/NuttX extension.