Socket API for library model. More...
Go to the source code of this file.
Functions | |
int8_t | socket_open (uint8_t protocol, uint16_t identifier, void(*passed_fptr)(void *)) |
Create and initialize a socket for communication. | |
int8_t | socket_close (int8_t sid) |
A function to close a socket. | |
int8_t | socket_listen (int8_t socket, uint8_t backlog) |
A function to set a socket to listening mode. | |
int8_t | socket_accept (int8_t listen_socket_id, ns_address_t *addr, void(*passed_fptr)(void *)) |
A function to accept a new connection on an socket. | |
int8_t | socket_shutdown (int8_t socket, uint8_t how) |
A function to shut down a connection. | |
int8_t | socket_getsockname (int8_t socket, ns_address_t *address) |
A function to read local address and port for a bound socket. | |
int8_t | socket_getpeername (int8_t socket, ns_address_t *address) |
A function to read remote address and port for a connected socket. | |
int16_t | socket_read (int8_t socket, ns_address_t *src_addr, uint8_t *buffer, uint16_t length) |
A function to read received data buffer from a socket. | |
int16_t | socket_recv (int8_t socket, void *buffer, uint16_t length, int flags) |
A function to read received data buffer from a socket,. | |
int16_t | socket_recvfrom (int8_t socket, void *buffer, uint16_t length, int flags, ns_address_t *src_addr) |
A function to read received data buffer from a socket. | |
int16_t | socket_recvmsg (int8_t socket, ns_msghdr_t *msg, int flags) |
A function to read received message with ancillary data from a socket. | |
int16_t | socket_sendmsg (int8_t socket, const ns_msghdr_t *msg, int flags) |
A function to send UDP, TCP or raw ICMP data via the socket with or without ancillary data or destination address. | |
int16_t | socket_sendto (int8_t socket, const ns_address_t *address, const void *buffer, uint16_t length) |
A function to send UDP, TCP or raw ICMP data via the socket. | |
int8_t | socket_connect (int8_t socket, ns_address_t *address, uint8_t randomly_take_src_number) |
A function to connect to remote peer (TCP). | |
int8_t | socket_bind (int8_t socket, const ns_address_t *address) |
Bind socket to address. | |
int8_t | socket_bind2addrsel (int8_t socket, const ns_address_t *dst_address) |
Bind a local address to a socket based on the destination address and the address selection preferences. | |
int16_t | socket_send (int8_t socket, const void *buffer, uint16_t length) |
Send data via a connected socket by client. | |
int8_t | socket_setsockopt (int8_t socket, uint8_t level, uint8_t opt_name, const void *opt_value, uint16_t opt_len) |
Set an option for a socket. | |
int8_t | socket_getsockopt (int8_t socket, uint8_t level, uint8_t opt_name, void *opt_value, uint16_t *opt_len) |
Get an option for a socket. | |
ns_cmsghdr_t * | NS_CMSG_NXTHDR (const ns_msghdr_t *msgh, const ns_cmsghdr_t *cmsg) |
Parse next control message from message by current control message header. | |
Variables | |
const uint8_t | ns_in6addr_any [16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} |
IPv6 wildcard address IN_ANY. |
Detailed Description
Socket API for library model.
The socket API functions for library model
Definition in file socket_api.c.
Function Documentation
ns_cmsghdr_t* NS_CMSG_NXTHDR | ( | const ns_msghdr_t * | msgh, |
const ns_cmsghdr_t * | cmsg | ||
) |
Parse next control message from message by current control message header.
- Parameters:
-
msgh Pointer for socket message. cmsg Pointer for last parsed control message
- Returns:
- Pointer to Next control message header , Could be NULL when no more control messages data.
Definition at line 1182 of file socket_api.c.
int8_t socket_accept | ( | int8_t | socket_id, |
ns_address_t * | addr, | ||
void(*)(void *) | passed_fptr | ||
) |
A function to accept a new connection on an socket.
- Parameters:
-
socket_id The socket ID of the listening socket. addr Either NULL pointer or pointer to structure where the remote address of the connecting host is copied. passed_fptr A function pointer to a function that is called whenever a data frame is received to the new socket.
- Returns:
- 0 or greater on success; return value is the new socket ID.
- -1 on failure.
- NS_EWOULDBLOCK if no pending connections.
Definition at line 141 of file socket_api.c.
int8_t socket_bind | ( | int8_t | socket, |
const ns_address_t * | address | ||
) |
Bind socket to address.
Used by the application to bind a socket to a port and/or an address. Binding of each of address and port can only be done once.
If address is ns_in6addr_any, the address binding is not changed. If port is 0, the port binding is not changed.
- Parameters:
-
socket Socket ID of the socket to bind. address Address structure containing the port and/or address to bind.
- Returns:
- 0 on success.
- -1 if the given address is NULL.
- -2 if the port is already bound to another socket.
- -3 if address is not us.
- -4 if the socket is already bound.
- -5 bind is not supported on this type of socket.
Definition at line 619 of file socket_api.c.
int8_t socket_bind2addrsel | ( | int8_t | socket, |
const ns_address_t * | dst_address | ||
) |
Bind a local address to a socket based on the destination address and the address selection preferences.
Binding happens to the same address that socket_connect() would bind to. Reference: RFC5014 IPv6 Socket API for Source Address Selection.
- Parameters:
-
socket The socket ID. dst_address The destination address to which the socket wants to communicate.
- Returns:
- 0 on success.
- -1 if the given address is NULL or socket ID is invalid.
- -2 if the memory allocation failed.
- -3 if the socket is already bound to address.
- -4 if the interface cannot be found.
- -5 if the source address selection fails.
- -6 bind2addrsel is not supported on this type of socket.
Definition at line 669 of file socket_api.c.
int8_t socket_close | ( | int8_t | socket ) |
A function to close a socket.
- Parameters:
-
socket ID of the socket to be closed.
- Returns:
- 0 socket closed.
- -1 socket not closed.
Definition at line 97 of file socket_api.c.
int8_t socket_connect | ( | int8_t | socket, |
ns_address_t * | address, | ||
uint8_t | randomly_take_src_number | ||
) |
A function to connect to remote peer (TCP).
- Parameters:
-
socket The socket ID. address The address of a remote peer. randomly_take_src_number Ignored - random local port is always chosen if not yet bound
- Returns:
- 0 on success.
- -1 in case of an invalid socket ID or parameter.
- -2 if memory allocation fails.
- -3 if the socket is in listening state.
- -4 if the socket is already connected.
- -5 connect is not supported on this type of socket.
- -6 if the TCP session state is wrong.
- -7 if the source address selection fails.
Definition at line 529 of file socket_api.c.
int8_t socket_getpeername | ( | int8_t | socket, |
ns_address_t * | address | ||
) |
A function to read remote address and port for a connected socket.
- Parameters:
-
socket The socket ID. address A pointer to the address structure where the remote address information is written to.
- Returns:
- 0 on success.
- -1 if no socket is found.
- -2 if no socket is not connected.
Definition at line 274 of file socket_api.c.
int8_t socket_getsockname | ( | int8_t | socket, |
ns_address_t * | address | ||
) |
A function to read local address and port for a bound socket.
This call writes ns_in6addr_any if address is not bound and 0 if the port is not bound.
- Parameters:
-
socket The socket ID. address A pointer to the address structure where the local address information is written to.
- Returns:
- 0 on success.
- -1 if no socket is found.
Definition at line 259 of file socket_api.c.
int8_t socket_getsockopt | ( | int8_t | socket, |
uint8_t | level, | ||
uint8_t | opt_name, | ||
void * | opt_value, | ||
uint16_t * | opt_len | ||
) |
Get an option for a socket.
Used to read miscellaneous options for a socket. Supported levels and names defined above. If the buffer is smaller than the option, the output is silently truncated; otherwise opt_len is modified to indicate the actual length.
- Parameters:
-
socket The socket ID. level The protocol level. opt_name The option name (interpretation depends on level). See OPTNAMES_IPV6. opt_value A pointer to output buffer. opt_len A pointer to length of output buffer; updated on exit.
- Returns:
- 0 on success.
- -1 invalid socket ID.
- -2 invalid/unsupported option.
Definition at line 1134 of file socket_api.c.
int8_t socket_listen | ( | int8_t | socket, |
uint8_t | backlog | ||
) |
A function to set a socket to listening mode.
- Parameters:
-
socket The socket ID. backlog The pending connections queue size.
- Returns:
- 0 on success.
- -1 on failure.
Definition at line 108 of file socket_api.c.
int8_t socket_open | ( | uint8_t | protocol, |
uint16_t | identifier, | ||
void(*)(void *) | passed_fptr | ||
) |
Create and initialize a socket for communication.
- Parameters:
-
protocol Defines the protocol to use. identifier The socket port. 0 indicates that port is not specified and it will be selected automatically when using the socket. passed_fptr A function pointer to a function that is called whenever a data frame is received to this socket.
- Returns:
- 0 or greater on success; Return value is the socket ID.
- -1 on failure.
- -2 on port reserved.
Definition at line 48 of file socket_api.c.
int16_t socket_read | ( | int8_t | socket, |
ns_address_t * | src_addr, | ||
uint8_t * | buffer, | ||
uint16_t | length | ||
) |
A function to read received data buffer from a socket.
Used by the application to get data from a socket. See socket_recvfrom() for more details.
This is equivalent to socket_recvfrom, except that it passes the flag NS_MSG_LEGACY0, which modifies the return behaviour for zero data.
- Parameters:
-
socket The socket ID. src_addr A pointer to a structure where the sender's address is stored. May be NULL if not required. buffer A pointer to an array where the read data is written to. length The maximum length of the allocated buffer.
- Returns:
- >0 indicates the length of the data copied to buffer.
- 0 if no data was read (includes zero-length datagram, end of stream and no data currently available)
- -1 invalid input parameters.
Definition at line 380 of file socket_api.c.
int16_t socket_recv | ( | int8_t | socket, |
void * | buffer, | ||
uint16_t | length, | ||
int | flags | ||
) |
A function to read received data buffer from a socket,.
Equivalent to socket_recvfrom with src_address set to NULL.
- Parameters:
-
socket The socket ID. buffer A pointer to an array where the read data is written to. length The maximum length of the allocated buffer. flags Flags for read call
- Returns:
- as for socket_recvfrom
Definition at line 385 of file socket_api.c.
int16_t socket_recvfrom | ( | int8_t | socket, |
void * | buffer, | ||
uint16_t | length, | ||
int | flags, | ||
ns_address_t * | src_addr | ||
) |
A function to read received data buffer from a socket.
Used by the application to get data from a socket.
This has two modes of operation.
1) For non-stream sockets, if the receive queue is disabled (set to 0 via SOCKET_SO_RCVBUF), which is the non-stream default and original Nanostack behaviour, then applications receive exactly one SOCKET_DATA callback per datagram, indicating that datagram's length. They must make 1 read call in that callback, and they will be given the data. If not read, the datagram is discarded on return from the callback.
2) Otherwise - stream sockets or SOCKET_SO_RCVBUF non-zero - behaviour is akin to traditional BSD. SOCKET_DATA callbacks occur when new data arrives, and read calls can be made any time. Data will be queued to an extent determined by the receive buffer size. The length in the data callback is the total amount of data in the receive queue - possibly multiple datagrams.
- Parameters:
-
socket The socket ID. buffer A pointer to an array where the read data is written to. length The maximum length of the allocated buffer. flags Flags for read call src_addr A pointer to a structure where the sender's address is stored. May be NULL if not required.
The returned length is normally the length of data actually written to the buffer; if NS_MSG_TRUNC is set in flags, then for non-stream sockets, the actual datagram length is returned instead, which may be larger than the buffer size.
Return values assume flag NS_MSG_LEGACY0 is not set - if it is set, they are as per socket_read().
- Returns:
- >0 indicates the length of the data copied to buffer (or original datagram size)
- 0 if end of stream or zero-length datagram
- -1 invalid input parameters.
- NS_EWOULDBLOCK if no data is currently available
Definition at line 390 of file socket_api.c.
int16_t socket_recvmsg | ( | int8_t | socket, |
ns_msghdr_t * | msg, | ||
int | flags | ||
) |
A function to read received message with ancillary data from a socket.
Used by the application to get data from a socket. See socket_recvfrom for details of the two delivery mechanisms.
Ancillary data must request by socket_setsockopt().
msg->msg_controllen is updated to indicate actual length of ancillary data output
- Parameters:
-
socket The socket ID. msg A pointer to a structure where messages is stored with or without ancillary data flags A flags for message read.
- Returns:
- as for socket_recvfrom
Validate socket id
Validate message payload buffer size
Write Ancillary
Definition at line 423 of file socket_api.c.
int16_t socket_send | ( | int8_t | socket, |
const void * | buffer, | ||
uint16_t | length | ||
) |
Send data via a connected socket by client.
Note: The socket connection must be ready before using this function. The stack uses automatically the address of the remote connected host as the destination address for the packet.
This call is equivalent to socket_sendto() with address set to NULL - see that call for more details.
- Parameters:
-
socket The socket ID. buffer A pointer to data. length Data length.
Definition at line 721 of file socket_api.c.
int16_t socket_sendmsg | ( | int8_t | socket, |
const ns_msghdr_t * | msg, | ||
int | flags | ||
) |
A function to send UDP, TCP or raw ICMP data via the socket with or without ancillary data or destination address.
Used by the application to send data message header support also vector list socket_send() and socket_sendto() use this functionality internally.
- Parameters:
-
socket The socket ID. msg A pointer to the Message header which include address, payload and ancillary data. flags A flags for message send (eg NS_MSG_LEGACY0)
Messages destination address is defined by msg->msg_name which must be ns_address_t. If msg->msg_nme is NULL socket select connected address
Messages payload and length is defined msg->msg_iov and msg->msg_iovlen. API support to send multiple data vector.
Supported ancillary data for send defined by msg->msg_control and msg->msg_controllen.
msg->msg_flags is unused, and need not be initialised.
The following main return values assume flag NS_MSG_LEGACY0 is not set - if it is set, they are as per socket_sendto().
- Returns:
- length if entire amount written (which could be 0)
- value >0 and <length if partial amount written (stream only)
- NS_EWOULDBLOCK if nothing written due to lack of queue space.
Error returns:
- Returns:
- -1 Invalid socket ID.
- -2 Socket memory allocation fail.
- -3 TCP state not established or address scope not defined .
- -4 Unknown interface.
- -5 Socket not connected
- -6 Packet too short (ICMP raw socket error).
Definition at line 499 of file socket_api.c.
int16_t socket_sendto | ( | int8_t | socket, |
const ns_address_t * | address, | ||
const void * | buffer, | ||
uint16_t | length | ||
) |
A function to send UDP, TCP or raw ICMP data via the socket.
Used by the application to send data.
The return of 0 on success is unconventional, and obtained by passing NS_MSG_LEGACY0 to socket_sendmsg internally - to get conventional return values, you can use socket_sendmsg() instead.
- Parameters:
-
socket The socket ID. address A pointer to the destination address information. buffer A pointer to data to be sent. length Length of the data to be sent.
- Returns:
- 0 On success (whole packet queued)
- NS_EWOULDBLOCK if nothing written due to lack of queue space.
Error returns:
- Returns:
- -1 Invalid socket ID.
- -2 Socket memory allocation fail.
- -3 TCP state not established or address scope not defined .
- -4 Unknown interface.
- -5 Socket not connected
- -6 Packet too short (ICMP raw socket error).
Definition at line 508 of file socket_api.c.
int8_t socket_setsockopt | ( | int8_t | socket, |
uint8_t | level, | ||
uint8_t | opt_name, | ||
const void * | opt_value, | ||
uint16_t | opt_len | ||
) |
Set an option for a socket.
Used to specify miscellaneous options for a socket. Supported levels and names defined above.
- Parameters:
-
socket The socket ID. level The protocol level. opt_name The option name (interpretation depends on level). See OPTNAMES_SOCKET and OPTNAMES_IPV6. opt_value A pointer to value for the specified option. opt_len Size of the data pointed to by the value.
- Returns:
- 0 on success.
- -1 invalid socket ID.
- -2 invalid/unsupported option.
- -3 invalid option value.
Definition at line 957 of file socket_api.c.
int8_t socket_shutdown | ( | int8_t | socket, |
uint8_t | how | ||
) |
A function to shut down a connection.
- Parameters:
-
socket The ID of the socket to be shut down. how How socket is to be shut down, one of SOCKET_SHUT_XX.
- Returns:
- 0 on success.
- -1 if the given socket ID is not found, if the socket type is wrong or TCP layer returns a failure.
- -2 if socket is not connected.
Definition at line 189 of file socket_api.c.
Variable Documentation
const uint8_t ns_in6addr_any[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} |
IPv6 wildcard address IN_ANY.
Definition at line 46 of file socket_api.c.
Generated on Tue Jul 12 2022 12:22:33 by
