BSD Sockets#

This section provides a reference to BSD Socket API functions.

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.

Modules#

sockaddr

Socket Type

Socket Option Name

Socket Option Level

Socket Address Family

Socket Protocol

Typedefs#

typedef uint32_t

Length type for network syscalls.

typedef __socklen_t

Length type for network syscalls.

typedef uint8_t

sockaddr address family type

sockaddr address family type

Functions#

int
socket(int domain, int type, int protocol)

Create an endpoint for communication.

int
bind(int socket_id, const struct sockaddr *addr, socklen_t addr_len)

Bind a name to a socket.

int
listen(int socket_id, int backlog)

Listen for connections on a socket.

int
accept(int socket_id, struct sockaddr *addr, socklen_t *addr_len)

Accept a connection on a socket.

int
connect(int socket_id, const struct sockaddr *addr, socklen_t addr_len)

Initiate a connection on a socket.

ssize_t
recv(int socket_id, void *buf, size_t buf_len, int flags)

Receive a message from a socket.

ssize_t
recvfrom(int socket_id, void *buf, size_t buf_len, int flags, struct sockaddr *from_addr, socklen_t *from_addr_len)

Receive a message from a socket.

ssize_t
send(int socket_id, const void *buf, size_t buf_len, int flags)

Send a message on a socket.

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)

Send a message on a socket.

int
setsockopt(int socket_id, int option_level, int option_name, const void *option_value, socklen_t option_length)

Set options on sockets.

int
getsockopt(int socket_id, int option_level, int option_name, void *option_value, socklen_t *option_length)

Get options on sockets.

int
getpeername(int socket_id, struct sockaddr *name, socklen_t *name_len)

Get name of connected peer socket.

int
getsockname(int socket_id, struct sockaddr *name, socklen_t *name_len)

Get socket name.

int
close(int socket_id)

Close a file descriptor.

Typedef Documentation#

__socklen_t#

typedef uint32_t __socklen_t

Length type for network syscalls.


Definition at line 49 of file components/service/bsd_socket/inc/netinet_in.h

socklen_t#

typedef __socklen_t socklen_t

Length type for network syscalls.


Definition at line 50 of file components/service/bsd_socket/inc/netinet_in.h

__sa_family_t#

typedef uint8_t __sa_family_t

sockaddr address family type


Definition at line 71 of file components/service/bsd_socket/inc/netinet_in.h

sa_family_t#

typedef __sa_family_t sa_family_t

sockaddr address family type


Definition at line 72 of file components/service/bsd_socket/inc/netinet_in.h

Function Documentation#

socket#

int socket (int domain, int type, int protocol)

Create an endpoint for communication.

Parameters
[in]domain

The domain argument specifies a communication domain; this selects the protocol family that will be used for communication. One of values from Socket Address Family

[in]type

The socket has the indicate type, which specifies the semantics of communication. One of values from Socket Type. 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. One of values from Socket Protocol.

socket() creates an endpoint for communication and returns a file descriptor that refers to that endpoint. The file descriptor returned by a successful call will be the lowest-numbered file descriptor not currently open for the process.

Returns

  • -1 is returned if an error occurs. Otherwise, the return value is a descriptor referencing the socket.


Definition at line 638 of file components/service/bsd_socket/inc/socket.h

bind#

int bind (int socket_id, const struct sockaddr * addr, socklen_t addr_len)

Bind a name to a socket.

Parameters
[in]socket_id

Socket identification number.

[in]addr

Address to be assigned to the socket of type sockaddr.

[in]addr_len

Size of the storage pointed to by addr of type socklen_t.

When a socket is created with socket(), it exists in a name space (address family) but has no address assigned to it.
bind() assigns the address specified by addr to the socket referred to by the file descriptor socket_id.
addrlen specifies the size, in bytes, of the address structure pointed to by addr. Traditionally, this operation is called “assigning a name to a socket”.

It is normally necessary to assign a local address using bind() before a SOCK_STREAM socket may receive connections

Returns

  • The value 0 if successful. Otherwise, the value -1 is returned and the global variable errno is set to indicate the error.


Definition at line 660 of file components/service/bsd_socket/inc/socket.h

listen#

int listen (int socket_id, int backlog)

Listen for connections on a socket.

Parameters
[in]socket_id

Socket identification number.

[in]backlog

The backlog argument defines the maximum number of the clients supported.

listen() marks the socket referred to by socket_id as a passive socket, that is, as a socket that will be used to accept incoming connection requests using accept.

This applies only to sockets of type SOCK_STREAM or SOCK_SEQPACKET.

  • Pre-conditions:

    • 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 with listen(), and then the connections are accepted with accept().

Returns

  • The value 0 if successful. Otherwise, the value -1 is returned and the global variable errno is set to indicate the error.


Definition at line 682 of file components/service/bsd_socket/inc/socket.h

accept#

int accept (int socket_id, struct sockaddr * addr, socklen_t * addr_len)

Accept a connection on a socket.

Parameters
[in]socket_id

Socket identification number of the socket, which is to be accepted.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]addr

The argument addr is a result argument of type sockaddr 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 of type socklen_t. 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 is used with connection-based socket types (SOCK_STREAM, SOCK_SEQPACKET).
It extracts the first connection request on the queue of pending connections for the listening socket (socket_id), creates a new connected socket, and returns a new file descriptor referring to that socket.
The newly created socket is not in the listening state. The original socket sockfd is unaffected by this call.

If no pending connections are present on the queue and the original socket is not marked as non-blocking, this function 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

  • -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.


Definition at line 715 of file components/service/bsd_socket/inc/socket.h

connect#

int connect (int socket_id, const struct sockaddr * addr, socklen_t addr_len)

Initiate a connection on a socket.

Parameters
[in]socket_id

The socket_id argument is a socket.

[in]addr

The addr argument of type sockaddr is the address is that to which datagrams are to be sent.

[in]addr_len

The addr_len argument of type socklen_t indicates the amount of space pointed to by addr, in bytes.

The connect() system call connects the socket referred to by the file descriptor sockfd to the address specified by addr.
The addrlen argument specifies the size of addr. The format of the address in addr is determined by the address space of the socket sockfd; see socket() for further details.

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. 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 can use connect() multiple times to change their association.

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.


Definition at line 742 of file components/service/bsd_socket/inc/socket.h

recv#

ssize_t recv (int socket_id, void * buf, size_t buf_len, int flags)

Receive a message from a socket.

Parameters
[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.

The recv() function is normally used only on a connected socket. recv() receives messages from a socket. recv() returns 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.


Definition at line 765 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)

Receive a message from a socket.

Parameters
[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 of type sockaddr from which data is received.

[in]from_addr_len

The from_addr_len argument is a value-result argument of type socklen_t, 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.

The recvfrom() system calls are used to receive messages from a socket and can be used to receive data on a socket whether or not it is connection-oriented. 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.


Definition at line 794 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)

Send a message on a socket.

Parameters
[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.

This 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 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 guarantee 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.


Definition at line 821 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)

Send a message on a socket.

Parameters
[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 of type sockaddr

[in]to_addr_len

Size of the address pointed by to_addr of type socklen_t

The function sendto() can 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 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.


Definition at line 850 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)

Set options on sockets.

Parameters
[in]socket_id

Socket identification number.

[in]option_level

Level for which the option is being set. One of the values from Socket Option Level.

[in]option_name

The option_name argument and any specified options are passed uninterpreted to the appropriate protocol module for interpretation. One of the values from Socket Option Name

[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 of type socklen_t

The setsockopt() system call manipulates 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().

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.


Definition at line 885 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)

Get options on sockets.

Parameters
[in]socket_id

Socket identification number.

[in]option_level

Level for which the option is set. One of the values from Socket Option Level.

[in]option_name

The option_name argument and any specified options are passed uninterpreted to the appropriate protocol module for interpretation. One of the values from Socket Option Name

[in]option_value

Pointer to option data.

[in]option_length

The option_length is a value-result argument of type socklen_t, 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.

The getsockopt() system call manipulates 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. For getsockopt(), they identify a buffer in which the value for the requested options are to be returned.

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.


Definition at line 920 of file components/service/bsd_socket/inc/socket.h

getpeername#

int getpeername (int socket_id, struct sockaddr * name, socklen_t * name_len)

Get name of connected peer socket.

Parameters
[in]socket_id

Socket identification number.

[in]name

Pointer to the peer name of type sockaddr

[in]name_len

The name_len argument of type socklen_t 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 getpeername() system call returns the name of the peer connected to socket socket_id. 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.


Definition at line 938 of file components/service/bsd_socket/inc/socket.h

getsockname#

int getsockname (int socket_id, struct sockaddr * name, socklen_t * name_len)

Get socket name.

Parameters
[in]socket_id

Socket identification number.

[in]name

Pointer to the socket name of type sockaddr.

[out]name_len

The name_len argument of type socklen_t 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).

getsockname() system call returns the current name for the specified socket.

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.


Definition at line 956 of file components/service/bsd_socket/inc/socket.h

close#

int close (int socket_id)

Close a file descriptor.

Parameters
[in]socket_id

Socket identification number.

close() closes a file descriptor, so that it no longer refers to any file and can be reused.

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 will close other sockets as well.

  • Closing an non existing socket, error EBADF (Bad file descriptor) will be thrown.


Definition at line 973 of file components/service/bsd_socket/inc/socket.h