Network Interfaces
NuttX supports a BSD-compatible socket interface layer. These socket interface can be enabled by settings in the architecture configuration file. Those socket APIs are discussed in the following paragraphs.
-
int socket(int domain, int type, int protocol);
Creates an endpoint for communication and returns a descriptor.
- Parameters
domain – (see sys/socket.h)
type – (see sys/socket.h)
protocol – (see sys/socket.h)
- Returns
0 on success; -1 on error with
errno
set appropriately:EACCES
. Permission to create a socket of the specified type and/or protocol is denied.EAFNOSUPPORT
. The implementation does not support the specified address family.EINVAL
. Unknown protocol, or protocol family not available.EMFILE
. Process file table overflow.ENFILE
The system limit on the total number of open files has been reached.ENOBUFS
orENOMEM
. Insufficient memory is available. The socket cannot be created until sufficient resources are freed.EPROTONOSUPPORT
. The protocol type or the specified protocol is not supported within this domain.
-
int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen)
Gives the socket sockfd the local address
addr
.addr
isaddrlen
bytes long. Traditionally, this is called “assigning a name to a socket.” When a socket is created withsocket()
, it exists in a name space (address family) but has no name assigned.- Parameters
sockfd – Socket descriptor from socket.
addr – Socket local address.
addrlen – Length of
addr
.
- Returns
0 on success; -1 on error with
errno
set appropriately: -EACCES
The address is protected, and the user is not the superuser. -EADDRINUSE
The given address is already in use. -EBADF
sockfd
is not a valid descriptor. -EINVAL
The socket is already bound to an address. -ENOTSOCK
sockfd
is a descriptor for a file, not a socket.
-
int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
connect()
connects the socket referred to by the file descriptorsockfd
to the address specified byaddr
. Theaddrlen
argument specifies the size ofaddr
. The format of the address inaddr
is determined by the address space of the socket sockfd. If the socket sockfd is of type SOCK_DGRAM thenaddr
is the address to which datagrams are sent by default, and the only address from which datagrams are received. If the socket is of type SOCK_STREAM or SOCK_SEQPACKET, this call attempts to make a connection to the socket that is bound to the address specified byaddr
. Generally, connection-based protocol sockets may successfullyconnect()
only once; connectionless protocol sockets may useconnect()
multiple times to change their association. Connectionless sockets may dissolve the association by connecting to an address with the sa_family member of sockaddr set to AF_UNSPEC.Input Parameters:
sockfd
: Socket descriptor returned bysocket()
addr
: Server address (form depends on type of socket)addrlen
: Length of actualaddr
Returned Value: 0 on success; -1 on error with
`errno
<#ErrnoAccess>`__ set appropriately:EACCES
or EPERM: The user tried to connect to a broadcast address without having the socket broadcast flag enabled or the connection request failed because of a local firewall rule.EADDRINUSE
Local address is already in use.EAFNOSUPPORT
The passed address didn’t have the correct address family in its sa_family field.EAGAIN
No more free local ports or insufficient entries in the routing cache. For PF_INET.EALREADY
The socket is non-blocking and a previous connection attempt has not yet been completed.EBADF
The file descriptor is not a valid index in the descriptor table.ECONNREFUSED
No one listening on the remote address.EFAULT
The socket structure address is outside the user’s address space.EINPROGRESS
The socket is non-blocking and the connection cannot be completed immediately.EINTR
The system call was interrupted by a signal that was caught.EISCONN
The socket is already connected.ENETUNREACH
Network is unreachable.ENOTSOCK
The file descriptor is not associated with a socket.ETIMEDOUT
Timeout while attempting connection. The server may be too busy to accept new connections.
-
int listen(int sockfd, int backlog);
To accept connections, a socket is first created with
socket()
, a willingness to accept incoming connections and a queue limit for incoming connections are specified withlisten()
, and then the connections are accepted withaccept()
. Thelisten()
call applies only to sockets of typeSOCK_STREAM
orSOCK_SEQPACKET
.Input Parameters:
sockfd
: Socket descriptor of the bound socket.backlog
: The maximum length the queue of pending connections may grow. If a connection request arrives with the queue full, the client may receive an error with an indication of ECONNREFUSED or, if the underlying protocol supports retransmission, the request may be ignored so that retries succeed.
Returned Value: On success, zero is returned. On error, -1 is returned, and
`errno
<#ErrnoAccess>`__ is set appropriately.EADDRINUSE
: Another socket is already listening on the same port.EBADF
: The argumentsockfd
is not a valid descriptor.ENOTSOCK
: The argumentsockfd
is not a socket.EOPNOTSUPP
: The socket is not of a type that supports the listen operation.
-
int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
The
accept()
function is used with connection-based socket types (SOCK_STREAM
,SOCK_SEQPACKET
andSOCK_RDM
). It extracts the first connection request on the queue of pending connections, creates a new connected socket with most of the same properties assockfd
, and allocates a new socket descriptor for the socket, which is returned. The newly created socket is no longer in the listening state. The original socketsockfd
is unaffected by this call. Per file descriptor flags are not inherited across an accept.The
sockfd
argument is a socket descriptor that has been created withsocket()
, bound to a local address withbind()
, and is listening for connections after a call tolisten()
.On return, the
addr
structure is filled in with the address of the connecting entity. Theaddrlen
argument initially contains the size of the structure pointed to byaddr
; on return it will contain the actual length of the address returned.If no pending connections are present on the queue, and the socket is not marked as non-blocking, accept blocks the caller until a connection is present. If the socket is marked non-blocking and no pending connections are present on the queue, accept returns
EAGAIN
.Input Parameters:
sockfd
: Socket descriptor of the listening socket.addr
: Receives the address of the connecting client.addrlen
: Input: allocated size ofaddr
, Return: returned size ofaddr
.
Returned Value: Returns -1 on error. If it succeeds, it returns a non-negative integer that is a descriptor for the accepted socket.
EAGAIN
orEWOULDBLOCK
: The socket is marked non-blocking and no connections are present to be accepted.EBADF
: The descriptor is invalid.ENOTSOCK
: The descriptor references a file, not a socket.EOPNOTSUPP
: The referenced socket is not of typeSOCK_STREAM
.EINTR
: The system call was interrupted by a signal that was caught before a valid connection arrived.ECONNABORTED
: A connection has been aborted.EINVAL
: Socket is not listening for connections.EMFILE
: The per-process limit of open file descriptors has been reached.ENFILE
: The system maximum for file descriptors has been reached.EFAULT
: The addr parameter is not in a writable part of the user address space.ENOBUFS
orENOMEM
: Not enough free memory.EPROTO
: Protocol error.EPERM
: Firewall rules forbid connection.
-
ssize_t send(int sockfd, const void *buf, size_t len, int flags);
The
send()
call may be used only when the socket is in a connected state (so that the intended recipient is known). The only difference betweensend()
andwrite()
is the presence offlags
. Withzero
flags parameter,send()
is equivalent towrite()
. Also,send(s,buf,len,flags)
is equivalent tosendto(s,buf,len,flags,NULL,0)
.Input Parameters:
sockfd
: Socket descriptor of socketbuf
: Data to sendlen
: Length of data to sendflags
: Send flags
Returned Value: See
`sendto()
<#sendto>`__.
-
ssize_t sendto(int sockfd, const void *buf, size_t len, int flags, const struct sockaddr *to, socklen_t tolen);
If
sendto()
is used on a connection-mode (SOCK_STREAM, SOCK_SEQPACKET) socket, the parameters to and tolen are ignored (and the error EISCONN may be returned when they are not NULL and 0), and the error ENOTCONN is returned when the socket was not actually connected.Input Parameters:
sockfd
: Socket descriptor of socketbuf
: Data to sendlen
: Length of data to sendflags
: Send flagsto
: Address of recipienttolen
: The length of the address structure
Returned Value: On success, returns the number of characters sent. On error, -1 is returned, and
`errno
<#ErrnoAccess>`__ is set appropriately:EAGAIN
orEWOULDBLOCK
. The socket is marked non-blocking and the requested operation would block.EBADF
. An invalid descriptor was specified.ECONNRESET
. Connection reset by peer.EDESTADDRREQ
. The socket is not connection-mode, and no peer address is set.EFAULT
. An invalid user space address was specified for a parameter.EINTR
. A signal occurred before any data was transmitted.EINVAL
. Invalid argument passed.EISCONN
. The connection-mode socket was connected already but a recipient was specified. (Now either this error is returned, or the recipient specification is ignored.)EMSGSIZE
. The socket type requires that message be sent atomically, and the size of the message to be sent made this impossible.ENOBUFS
. The output queue for a network interface was full. This generally indicates that the interface has stopped sending, but may be caused by transient congestion.ENOMEM
. No memory available.ENOTCONN
. The socket is not connected, and no target has been given.ENOTSOCK
. The argument s is not a socket.EOPNOTSUPP
. Some bit in the flags argument is inappropriate for the socket type.EPIPE
. The local end has been shut down on a connection oriented socket. In this case the process will also receive a SIGPIPE unless MSG_NOSIGNAL is set.
-
ssize_t recv(int sockfd, void *buf, size_t len, int flags);
The
recv()
call is identical to`recvfrom()
<#recvfrom>`__ with a NULLfrom
parameter.Input Parameters:
sockfd: Socket descriptor of socket
buf: Buffer to receive data
len: Length of buffer
flags: Receive flags
Returned Value: See
`recvfrom()
<#recvfrom>`__.
-
ssize_t recvfrom(int sockfd, void *buf, size_t len, int flags, struct sockaddr *from, socklen_t *fromlen);
recvfrom()
receives messages from a socket, and may be used to receive data on a socket whether or not it is connection-oriented.If
from
is not NULL, and the underlying protocol provides the source address, this source address is filled in. The argumentfromlen
initialized to the size of the buffer associated withfrom
, and modified on return to indicate the actual size of the address stored there.Input Parameters:
sockfd
: Socket descriptor of socket.buf
: Buffer to receive data.len
: Length of buffer.flags
: Receive flags.from
: Address of source.fromlen
: The length of the address structure.
Returned Value: On success, returns the number of characters sent. If no data is available to be received and the peer has performed an orderly shutdown, recv() will return 0. Otherwise, on errors, -1 is returned, and
`errno
<#ErrnoAccess>`__ is set appropriately:EAGAIN
. The socket is marked non-blocking and the receive operation would block, or a receive timeout had been set and the timeout expired before data was received.EBADF
. The argumentsockfd
is an invalid descriptor.ECONNREFUSED
. A remote host refused to allow the network connection (typically because it is not running the requested service).EFAULT
. The receive buffer pointer(s) point outside the process’s address space.EINTR
. The receive was interrupted by delivery of a signal before any data were available.EINVAL
. Invalid argument passed.ENOMEM
. Could not allocate memory.ENOTCONN
. The socket is associated with a connection-oriented protocol and has not been connected.ENOTSOCK
. The argumentsockfd
does not refer to a socket.
-
int setsockopt(int sockfd, int level, int option, const void *value, socklen_t value_len);
setsockopt()
sets the option specified by theoption
argument, at the protocol level specified by thelevel
argument, to the value pointed to by thevalue
argument for the socket associated with the file descriptor specified by thesockfd
argument.The
level
argument specifies the protocol level of the option. To set options at the socket level, specify the level argument as SOL_SOCKET.See
sys/socket.h
for a complete list of values for theoption
argument.Input Parameters:
sockfd
: Socket descriptor of socketlevel
: Protocol level to set the optionoption
: identifies the option to setvalue
: Points to the argument valuevalue_len
: The length of the argument value
Returned Value: On success, returns the number of characters sent. On error, -1 is returned, and
`errno
<#ErrnoAccess>`__ is set appropriately:BADF
. Thesockfd
argument is not a valid socket descriptor.DOM
. The send and receive timeout values are too big to fit into the timeout fields in the socket structure.INVAL
. The specified option is invalid at the specified socketlevel
or the socket has been shut down.ISCONN
. The socket is already connected, and a specified option cannot be set while the socket is connected.NOPROTOOPT
. Theoption
is not supported by the protocol.NOTSOCK
. Thesockfd
argument does not refer to a socket.NOMEM
. There was insufficient memory available for the operation to complete.NOBUFS
. Insufficient resources are available in the system to complete the call.
-
int getsockopt(int sockfd, int level, int option, void *value, socklen_t *value_len);
getsockopt()
retrieve those value for the option specified by theoption
argument for the socket specified by thesockfd
argument. If the size of the option value is greater thanvalue_len
, the value stored in the object pointed to by thevalue
argument will be silently truncated. Otherwise, the length pointed to by thevalue_len
argument will be modified to indicate the actual length of thevalue
.The
level
argument specifies the protocol level of the option. To retrieve options at the socket level, specify the level argument as SOL_SOCKET.See
sys/socket.h
for a complete list of values for theoption
argument.Input Parameters:
sockfd
: Socket descriptor of socketlevel
: Protocol level to set the optionoption
: Identifies the option to getvalue
: Points to the argument valuevalue_len
: The length of the argument value
Returned Value: On success, returns the number of characters sent. On error, -1 is returned, and
`errno
<#ErrnoAccess>`__ is set appropriately:BADF
. Thesockfd
argument is not a valid socket descriptor.INVAL
. The specified option is invalid at the specified socketlevel
or the socket has been shutdown.NOPROTOOPT
. Theoption
is not supported by the protocol.NOTSOCK
. Thesockfd
argument does not refer to a socket.NOBUFS
. Insufficient resources are available in the system to complete the call.