BSD Sockets#
The BSD socket implementation tries to emulate the standards library to the extent possible for embedded offerings from Silabs. There are substantial limitation/exceptions however, and those are mentioned in the subsequent documentation of BSD sockets.
Note
sl_si91x_get_saved_firmware_status() needs to be called to get to know error code returned by firmware in case when API returns -1 and errno being 0.
Functions#
The getpeername() system call returns the name of the peer connected to socket socket_id.
The getsockname() system call returns the current name for the specified socket.
The getsockopt() system call manipulate the options associated with a socket.
The recvfrom() system calls are used to receive messages from a socket, and may be used to receive data on a socket whether or not it is connection-oriented.
The setsockopt() system call set options on sockets.
Function Documentation#
accept#
int accept (int socket_id, struct sockaddr * addr, socklen_t * addr_len)
The argument socket_id is a socket that has been created with socket(), bound to an address with bind(), and is listening for connections after a listen().
[in] | socket_id | Socket identification number of the socket, which is to be accepted. |
[in] | addr | The argument addr is a result argument that is filled-in with the address of the connecting entity, as known to the communications layer. The exact format of the addr argument is determined by the domain in which the communication is occurring. A null pointer may be specified for addr if the address information is not desired; in this case, addrlen is not used and should also be null. |
[in] | addr_len | The addrlen argument is a value-result argument; it should initially contain the amount of space pointed to by addr; on return it will contain the actual length (in bytes) of the address returned. This call is used with connection-based socket types, currently with SOCK_STREAM. |
The accept() system call extracts the first connection request on the queue of pending connections, creates a new socket, and allocates a new file descriptor for the socket.
If no pending connections are present on the queue, and the original socket is not marked as non-blocking, accept() blocks the caller until a connection is present. If the original socket is marked non-blocking and no pending connections are present on the queue, accept() returns an error. The accepted socket may not be used to accept more connections. The original socket socket_id remains open.
Returns
These calls return -1 on error. If they succeed, they return a non-negative integer that is a descriptor for the accepted socket.
Note
The accept() system call only supports blocking.
610
of file components/service/bsd_socket/inc/socket.h
bind#
int bind (int socket_id, const struct sockaddr * addr, socklen_t addr_len)
The bind() system call assigns the local protocol address to a socket.
[in] | socket_id | Socket identification number. |
[in] | addr | Address to be assigned to the socket. |
[in] | addr_len | Size of the storage pointed to by addr. |
When a socket is created with socket() it exists in an address family space but has no protocol address assigned. The bind() system call requests that addr be assigned to the socket.
Returns
The bind() function returns the value 0 if successful; otherwise the value -1 is returned and the global variable errno is set to indicate the error.
627
of file components/service/bsd_socket/inc/socket.h
connect#
int connect (int socket_id, const struct sockaddr * addr, socklen_t addr_len)
If socket_id is of type SOCK_DGRAM, connect() system call specifies the peer with which the socket is to be associated; If the socket is of type SOCK_STREAM, connect() system call attempts to make a connection to another socket.
[in] | socket_id | The socket_id argument is a socket. |
[in] | addr | The addr argument is the address is that to which datagrams are to be sent. |
[in] | addr_len | The addr_len argument indicates the amount of space pointed to by addr, in bytes. |
The other socket is specified by addr, which is an address in the communications space of the socket. Each communications space interprets the addr argument in its own way. Generally, stream sockets may successfully connect() only once; datagram sockets may use connect() multiple times to change their association.
Returns
The connect() function returns the value 0 if successful; otherwise the value -1 is returned and the global variable errno is set to indicate the error.
Note
Connecting to an invalid address, such as null address will result in EFAULT error.
648
of file components/service/bsd_socket/inc/socket.h
getpeername#
int getpeername (int socket_id, struct sockaddr * name, socklen_t * name_len)
The getpeername() system call returns the name of the peer connected to socket socket_id.
[in] | socket_id | Socket identification number. |
[in] | name | Pointer to the peer name. |
[in] | name_len | The name_len argument should be initialized to indicate the amount of space pointed to by name. On return it contains the actual size of the name returned (in bytes). |
The name is truncated if the buffer provided is too small.
Returns
The getpeername() function returns the value 0 if successful; otherwise the value -1 is returned and the global variable errno is set to indicate the error.
664
of file components/service/bsd_socket/inc/socket.h
getsockname#
int getsockname (int socket_id, struct sockaddr * name, socklen_t * name_len)
The getsockname() system call returns the current name for the specified socket.
[in] | socket_id | Socket identification number. |
[in] | name | Pointer to the socket name. |
[out] | name_len | The name_len argument should be initialized to indicate the amount of space pointed to by name. On return it contains the actual size of the name returned (in bytes). |
Returns
The getsockname() function returns the value 0 if successful; otherwise the value -1 is returned and the global variable errno is set to indicate the error.
679
of file components/service/bsd_socket/inc/socket.h
getsockopt#
int getsockopt (int socket_id, int option_level, int option_name, void * option_value, socklen_t * option_length)
The getsockopt() system call manipulate the options associated with a socket.
[in] | socket_id | Socket identification number. |
[in] | option_level | Level for which the option is set. |
[in] | option_name | The option_name argument and any specified options are passed uninterpreted to the appropriate protocol module for interpretation. |
[in] | option_value | Pointer to option data. |
[in] | option_length | The option_length is a value-result argument, initially containing the size of the buffer pointed to by option_value, and modified on return to indicate the actual size of the value returned. If no option value is to be supplied or returned, optval may be NULL. |
Options may exist at multiple protocol levels; they are always present at the uppermost "socket" level. When manipulating socket options the level at which the option resides and the name of the option must be specified. To manipulate options at the socket level, level is specified as SOL_SOCKET. To manipulate options at any other level the protocol number of the appropriate protocol controlling the option is supplied. For example, to indicate that an option is to be interpreted by the TCP protocol, level should be set to the protocol number of TCP; For getsockopt() they identify a buffer in which the value for the requested option(s) are to be returned. The include file <sys/socket.h> contains definitions for socket level options. Options at other protocol levels vary in format and name. Most socket-level options utilize an int argument for option_value.
Returns
Upon successful completion, the value 0 is returned; otherwise the value -1 is returned and the global variable errno is set to indicate the error.
Note
The following are the options, which are supported currently.
SO_RCVTIMEO
SO_KEEPALIVE
TCP_ULP
SO_MAX_RETRANSMISSION_TIMEOUT_VALUE
IP_TOS.
714
of file components/service/bsd_socket/inc/socket.h
listen#
int listen (int socket_id, int backlog)
The listen() sysytem call listen for socket connections.
[in] | socket_id | Socket identification number. |
[in] | backlog | The backlog argument defines the maximum number of the clients supported. |
The listen() system call applies only to sockets of type SOCK_STREAM or SOCK_SEQPACKET.
Pre-conditions:
Returns
The listen() function returns the value 0 if successful; otherwise the value -1 is returned and the global variable errno is set to indicate the error.
730
of file components/service/bsd_socket/inc/socket.h
recv#
ssize_t recv (int socket_id, void * buf, size_t buf_len, int flags)
The recv() function is normally used only on a connected socket.
[in] | socket_id | Socket identification number. |
[in] | buf | Pointer to the buffer that receives the data. |
[in] | buf_len | Length of the buffer pointed to by the buf parameter. |
[in] | flags | Controls the reception of the data. |
recv() function Receive message(s) from a socket. The recv() return the length of the message on successful completion. If a message is too long to fit in the supplied buffer, excess bytes may be discarded depending on the type of socket the message is received from.
Returns
ssize_t.
Note
The recv() system call doesn't support any flags.
751
of file components/service/bsd_socket/inc/socket.h
recvfrom#
ssize_t recvfrom (int socket_id, void * buf, size_t buf_len, int flags, struct sockaddr * from_addr, socklen_t * from_addr_len)
The recvfrom() system calls are used to receive messages from a socket, and may be used to receive data on a socket whether or not it is connection-oriented.
[in] | socket_id | Socket identification number. |
[in] | buf | Pointer to the buffer that receives the data. |
[in] | buf_len | Length of the buffer pointed to by the buf parameter. |
[in] | flags | Controls the reception of the data. |
[in] | from_addr | Pointer to a socket address structure from which data is received. |
[in] | from_addr_len | The from_addr_len argument is a value-result argument, initialized to the size of the buffer associated with from_addr, and modified on return to indicate the actual size of the address stored there. The recvfrom() return the length of the message on successful completion. |
If from_addr is not a null pointer and the socket is not connection-oriented, the source address of the message is filled in.
Returns
ssize_t.
Note
The recvfrom() system call doesn't support any flags.
778
of file components/service/bsd_socket/inc/socket.h
send#
ssize_t send (int socket_id, const void * buf, size_t buf_len, int flags)
The send() system call is used to transmit one or more messages to another socket.
[in] | socket_id | Socket identification number. |
[in] | buf | Pointer to the buffer containing the message to transmit. |
[in] | buf_len | The length of the message is given by buf_len. |
[in] | flags | Controls the transmission of the data. |
The send() function may be used only when the socket is in a connected state. If the socket is connection-mode, the protocol must support implied connect or the socket must be in a connected state before use. No indication of failure to deliver is implicit in a send(). Locally detected errors are indicated by a return value of -1. If no messages space is available at the socket to hold the message to be transmitted, then send() normally blocks, unless the socket has been placed in non-blocking I/O mode.
Returns
The send() call return the number of octets sent. If an error occurred a value of -1 is returned.
Note
Currently, send() system call supports only blocking. The send() system call doesn't guarantees the packets are transmitted to remote note, which are enqueued in the queue. The send() system call doesn't supports any flags. The send() system call can only send max of 1460 bytes incase of plain TCP, UDP. Whereas incase of TLS, the max buffer length is 1370.
806
of file components/service/bsd_socket/inc/socket.h
sendto#
ssize_t sendto (int socket_id, const void * buf, size_t buf_len, int flags, const struct sockaddr * to_addr, socklen_t to_addr_len)
The sendto() system calls are used to transmit one or more messages to another socket.
[in] | socket_id | Socket identification number. |
[in] | buf | Pointer to the buffer containing the message to transmit. |
[in] | buf_len | The length of the message is given by buf_len. |
[in] | flags | Controls the transmission of the data. |
[in] | to_addr | Address of the target. |
[in] | to_addr_len | Size of the address pointed by to_addr. |
The functions sendto() may be used at any time if the socket is connectionless-mode. If the socket is connection-mode, the protocol must support implied connect or the socket must be in a connected state before use. The address of the target is given by to with to_addr_len specifying its size. If the socket is in a connected state, the target address passed to sendto() is ignored. If the message is too long to pass atomically through the protocol, the error EMSGSIZE is returned, and the message is not transmitted.
Returns
The sendto() call return the number of octets sent. If an error occurred a value of -1 is returned.
Note
Due to firmware limitations, sendto() system call doesn't support any flags. The sendto() system call can only send max of 1460 bytes incase of plain TCP, UDP. Whereas incase of TLS, the max buffer length is 1370.
834
of file components/service/bsd_socket/inc/socket.h
setsockopt#
int setsockopt (int socket_id, int option_level, int option_name, const void * option_value, socklen_t option_length)
The setsockopt() system call set options on sockets.
[in] | socket_id | Socket identification number. |
[in] | option_level | Level for which the option is being set. |
[in] | option_name | The option_name argument and any specified options are passed uninterpreted to the appropriate protocol module for interpretation. |
[in] | option_value | Most socket-level options utilize an int argument for option_value. For setsockopt(), the argument should be non-zero to enable a boolean option, or zero if the option is to be disabled. |
[in] | option_length | Pointer to the length of the option data. |
The setsockopt() system call manipulate the options associated with a socket. Options may exist at multiple protocol levels; they are always present at the uppermost "socket" level. When manipulating socket options the level at which the option resides and the name of the option must be specified. To manipulate options at the socket level, level is specified as SOL_SOCKET. To manipulate options at any other level the protocol number of the appropriate protocol controlling the option is supplied. For example, to indicate that an option is to be interpreted by the TCP protocol, level should be set to the protocol number of TCP; The option_value and option_length arguments are used to access option values for setsockopt(). The include file <sys/socket.h> contains definitions for socket level options. Options at other protocol levels vary in format and name.
Returns
Upon successful completion, the value 0 is returned; otherwise the value -1 is returned and the global variable errno is set to indicate the error.
Note
The following are the options, which are supported currently.
SO_RCVTIMEO
SO_KEEPALIVE
TCP_ULP
SO_MAX_RETRANSMISSION_TIMEOUT_VALUE
IP_TOS.
871
of file components/service/bsd_socket/inc/socket.h
close#
int close (int socket_id)
The close() system call deletes the file descriptor of the socket.
[in] | socket_id | Socket identification number. |
Returns
The close() function returns the value 0 if successful; otherwise the value -1 is returned and the global variable errno is set to indicate the error.
Note
Calling close on server or first client socket would end up closing othet socket as well. Closing an non existing socket, error EBADF (Bad file descriptor) will be thrown.
884
of file components/service/bsd_socket/inc/socket.h
socket#
int socket (int family, int type, int protocol)
The socket() system call creates an endpoint for communication and returns a descriptor.
[in] | family | The domain argument specifies a communications domain within which communication will take place; this selects the protocol family which should be used. These families are defined in the include file <sys/socket.h>. |
[in] | type | The socket has the indicate type, which specifies the semantics of communication. Currently defined types are SOCK_STREAM, SOCK_DGRAM, SOCK_RAW, SOCK_RDM, and SOCK_SEQPACKET. |
[in] | protocol | The protocol argument specifies a particular protocol to be used with the socket. Normally only a single protocol exists to support a particular socket type within a given protocol family. However, it is possible that many protocols may exist, in which case a particular protocol must be specified in this manner. The protocol number to use is particular to the "communication domain" in which communication is to take place. The protocol argument may be set to zero (0) to request the default implementation of a socket type for the protocol, if any. |
Returns
A -1 is returned if an error occurs, otherwise the return value is a descriptor referencing the socket.
905
of file components/service/bsd_socket/inc/socket.h