ON Semiconductor / mbed-os

Dependents:   mbed-TFT-example-NCS36510 mbed-Accelerometer-example-NCS36510 mbed-Accelerometer-example-NCS36510

Embed: (wiki syntax)

« Back to documentation index

socket_api.h File Reference

socket_api.h File Reference

6LoWPAN Library Socket API More...

Go to the source code of this file.

Data Structures

struct  socket_callback_t
 ICMP socket instruction. More...

Functions

ns_cmsghdr_tNS_CMSG_NXTHDR (const ns_msghdr_t *msgh, const ns_cmsghdr_t *cmsg)
 Parse next control message from message by current control message header.
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 socket)
 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 socket_id, ns_address_t *addr, void(*passed_fptr)(void *))
 A function to accept a new connection on an 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.
int8_t socket_shutdown (int8_t socket, uint8_t how)
 A function to shut down a connection.
int8_t socket_send (int8_t socket, uint8_t *buffer, uint16_t length)
 Send data via a connected TCP socket by client.
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_recvmsg (int8_t socket, ns_msghdr_t *msg, int flags)
 A function to read received message with ancillary data from a socket.
int8_t socket_sendto (int8_t socket, ns_address_t *address, uint8_t *buffer, uint16_t length)
 A function to send UDP, TCP or raw ICMP data via the socket.
int8_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.
int8_t socket_read_session_address (int8_t socket, ns_address_t *address)
 A function to read session info for TCP event.
int8_t socket_setsockopt (int8_t socket, uint8_t level, uint8_t opt_name, const void *opt_value, uint16_t opt_len)
 Socket options summary.
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.

Variables

const uint8_t ns_in6addr_any [16]
 IPv6 wildcard address IN_ANY.

Detailed Description

6LoWPAN Library Socket API

Common socket API

Socket read API at callback

Socket TX API

  • socket_send(), A function to write data buffer to a socket.
  • socket_sendto(), A function to write data to a specific destination in the socket.
  • socket_senmsg(), A function which support socket_send and socket_sendto functionality which supports ancillary data

TCP socket connection handle

Sockets are a common abstraction model for network communication and are used in most operating systems. 6LoWPAN Library API follows BSD socket API conventions closely with some extensions necessitated by the event-based scheduling model.

Library supports the following socket types: | Socket name | Socket description | | :------------: | :----------------------------: | | SOCKET_UDP | UDP socket type | | SOCKET_TCP | TCP socket type | | SOCKET_ICMP | ICMP raw socket type |

ICMP RAW socket instruction

An ICMP raw socket can be created with the function socket_open() and the identifier 0xffff. When using ICMP sockets, the minimum packet length is 4 bytes which is the 4 bytes of ICMP header. The first byte is for type, second for code and last two are for the checksum that must always be set to zero. The stack will calculate the checksum automatically before transmitting the packet. THe header is followed by the payload if there is any. NOTE: While it is perfectly legal to send an ICMP packet without payload, some packet sniffing softwares might regard such packets as malformed. In such a case, a dummy payload can be attached by providing a socket_sendto() function with a pointer to your desired data buffer. Moreover, the current implementation of the stack allows only certain ICMP types, for example ICMP echo, reply, destination unreachable, router advertisement, router solicitation. It will drop any other unimplemented types. For example, Private experimentation type (200) will be dropped by default.

| ICMP Type | ICMP Code | Checksum | Payload | Notes | | :-------: | :----------: | :-------: | :--------: | :--------------:| | 1 | 1 | 2 | n bytes | Length in bytes | | 0xXX | 0xXX | 0x00 0x00 | n bytes | Transmit | | 0xXX | 0xXX | 0xXX 0xXX | n bytes | Receive |

ICMP echo request with 4 bytes of payload (ping6).

| ICMP Type | ICMP Code | Checksum | Payload | | :-------: | :----------: | :-------: | :-----------------: | | 0x80 | 0x00 | 0x00 0x00 | 0x00 0x01 0x02 0x03 |

ICMP echo response for the message above.

| ICMP Type | ICMP Code | Checksum | Payload | | :-------: | :----------: | :-------: | :-----------------: | | 0x81 | 0x00 | 0xXX 0xXX | 0x00 0x01 0x02 0x03 |

Socket receiving

When there is data to read from the socket, a receive callback function is called with the event type parameter set to SOCKET_DATA. The application must read the received data with socket_read() or socket_recvmsg() during the callback because the stack will release the data when returning from the receive callback.

Socket event has event_type and socket_id fields. The receive callback function must be defined when socket is opened using socket_open() API.

All supported socket event types are listed here:

| Event Type | Value | Description | | :------------------------: | :---: | :-----------------------------------------------------------------: | | SOCKET_EVENT_MASK | 0xF0 | NC Socket event mask. | | SOCKET_DATA | 0x00 | Data received, read data length available in d_len field. | | SOCKET_BIND_DONE | 0x10 | TCP connection ready. | | SOCKET_TX_FAIL | 0x50 | Socket data send failed. | | SOCKET_CONNECT_CLOSED | 0x60 | TCP connection closed. | | SOCKET_CONNECTION_RESET | 0x70 | TCP connection reset. | | SOCKET_NO_ROUTER | 0x80 | No route available to destination. | | SOCKET_TX_DONE | 0x90 | Last socket TX process done, in TCP, whole TCP process is ready. | | SOCKET_NO_RAM | 0xA0 | No RAM available. |

How to use TCP sockets:

| API | Socket Type | Description | | :---------------------------: | :-----------: | :------------------------------------------------------------: | | socket_open() | Server/Client | Open socket to specific or dynamic port number. | | socket_shutdown() | Client | Shut down opened TCP connection. | | socket_listen() | Server | Set server port to listen state. | | socket_connect() | Client | Connect client socket to specific destination. | | socket_close() | Server/Client | Closes the TCP Socket. | | socket_send() | Client | Send data to session based destination. | | socket_sendto() | Server/Client | Send data to specific destination. | | socket_read_session_address() | Server/Client | Read socket TCP session address and port information. |

When the TCP socket is opened it is in closed state. It must be set either to listen or to connect state before it can be used to receive or transmit data.

A socket can be set to listen mode with the socket_listen() function. After the call, the socket can accept an incoming connection from a remote host. The listen mode closes the connection automatically after server timeout or when the client or application closes the connection manually by socket_shutdown() function.

A TCP socket can be connected to a remote host with socket_connect() with correct arguments. After the function call, a (non-blocking) application must wait for the socket event to confirm the successful state change of the socket. After the successful state change, data can be sent using socket_send() by client and socket_send() by server. The connection can be shut down with socket_shutdown() function or by server timeout.

How to use UDP and RAW socket:

A UDP socket is ready to receive and send data immediately after a successful call of socket_open() and a NET_READY event is received. Data can be transmitted with the socket_sendto() function. An ICMP socket works with same function call.

Definition in file socket_api.h.


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:
msghPointer for socket message.
cmsgPointer for last parsed control message
Returns:
Pointer to Next control message header , Could be NULL when no more control messages data.

Definition at line 128 of file socket_api_stub.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.

NOT YET IMPLEMENTED - PLACEHOLDER FOR FUTURE TCP CHANGES

Parameters:
socket_idThe socket ID of the listening socket.
addrEither NULL pointer or pointer to structure where the remote address of the connecting host is copied.
passed_fptrA 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.
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 can be done only once. The port or address cannot be changed after binding.

Parameters:
socketSocket ID of the socket to bind.
addressAddress structure containing the port and 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 52 of file socket_api_stub.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:
socketThe socket ID.
dst_addressThe 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.
int8_t socket_close ( int8_t  socket )

A function to close a socket.

Parameters:
socketID of the socket to be closed.
Returns:
0 socket closed.
-1 socket not closed.

Definition at line 28 of file socket_api_stub.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:
socketThe socket ID.
addressThe address of a remote peer.
randomly_take_src_numberIgnored - 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 44 of file socket_api_stub.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:
socketThe socket ID.
levelThe protocol level.
opt_nameThe option name (interpretation depends on level). See OPTNAMES_IPV6.
opt_valueA pointer to output buffer.
opt_lenA pointer to length of output buffer; updated on exit.
Returns:
0 on success.
-1 invalid socket ID.
-2 invalid/unsupported option.

Definition at line 105 of file socket_api_stub.c.

int8_t socket_listen ( int8_t  socket,
uint8_t  backlog 
)

A function to set a socket to listening mode.

Parameters:
socketThe socket ID.
backlogThe pending connections queue size. (Not yet implemented).
Returns:
0 on success.
-1 on failure.

Definition at line 36 of file socket_api_stub.c.

int8_t socket_open ( uint8_t  protocol,
uint16_t  identifier,
void(*)(void *)  passed_fptr 
)

Create and initialize a socket for communication.

Parameters:
protocolDefines the protocol to use.
identifierThe socket port. 0 indicates that port is not specified and it will be selected automatically when using the socket.
passed_fptrA 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 18 of file socket_api_stub.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. This method must be called once from a socket callback when handling event SOCKET_DATA. If the received data does not fit in the buffer provided the excess data bytes are discarded.

Parameters:
socketThe socket ID.
src_addrA pointer to a structure where the sender's address is stored.
bufferA pointer to an array where the read data is written to.
lengthThe maximum length of the allocated buffer.
Returns:
greater than 0 indicates the length of the data copied to buffer.
0 if no data is available to read.
-1 invalid input parameters.

Definition at line 69 of file socket_api_stub.c.

int8_t socket_read_session_address ( int8_t  socket,
ns_address_t address 
)

A function to read session info for TCP event.

Parameters:
socketThe socket ID.
addressA pointer to the address structure where the session address information is read to.
Returns:
0 on success.
-1 if no socket is found or TCP is not compiled into this project.
-2 if no session information is found.

Note: This function should be called only at socket callback when the socket event is SOCKET_BIND_DONE or SOCKET_TX_DONE. The following sections introduce those functions.

Definition at line 89 of file socket_api_stub.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. This method must be called once from a socket callback when handling event SOCKET_DATA. If the received data does not fit in the buffer provided the excess data bytes are discarded.

Ancillary data must request by socket_setsockopt().

msg->msg_controllen is updated to indicate actual length of ancillary data output

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.

Parameters:
socketThe socket ID.
msgA pointer to a structure where messages is stored with or without ancillary data
flagsA flags for message read.
Returns:
greater than 0 indicates the length of the data.
0 if no data is available to read.
-1 invalid input parameters.

Definition at line 123 of file socket_api_stub.c.

int8_t socket_send ( int8_t  socket,
uint8_t *  buffer,
uint16_t  length 
)

Send data via a connected TCP 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.

Parameters:
socketThe socket ID.
bufferA pointer to data.
lengthData length.
Returns:
0 done
-1 Invalid socket ID.
-2 Socket memory allocation fail.
-3 TCP state not established or address scope not defined .
-4 Socket TX process busy or unknown interface.
-5 Socket not connected
-6 Packet too short (ICMP raw socket error).

Definition at line 61 of file socket_api_stub.c.

int8_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:
socketThe socket ID.
msgA pointer to the Message header which include address, payload and ancillary data.
flagsA flags for message send for future usage (not supported yet)

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->flags and flags is ignored

Returns:
0 on success.
-1 Invalid socket ID or message structure.
-2 Socket memory allocation fail.
-3 TCP state not established or address scope not defined .
-4 Socket TX process busy or unknown interface.
-5 Socket not connected
-6 Packet too short (ICMP raw socket error).

Definition at line 114 of file socket_api_stub.c.

int8_t socket_sendto ( int8_t  socket,
ns_address_t address,
uint8_t *  buffer,
uint16_t  length 
)

A function to send UDP, TCP or raw ICMP data via the socket.

Used by the application to send data.

Parameters:
socketThe socket ID.
addressA pointer to the destination address information.
bufferA pointer to data to be sent.
lengthLength of the data to be sent.
Returns:
0 on success.
-1 Invalid socket ID.
-2 Socket memory allocation fail.
-3 TCP state not established or address scope not defined .
-4 Socket TX process busy or unknown interface.
-5 Socket not connected
-6 Packet too short (ICMP raw socket error).

Definition at line 81 of file socket_api_stub.c.

int8_t socket_setsockopt ( int8_t  socket,
uint8_t  level,
uint8_t  opt_name,
const void *  opt_value,
uint16_t  opt_len 
)

Socket options summary.

| opt_name / cmsg_type | Data type | set/getsockopt | sendmsg | recvmsg | | :--------------------------: | :--------------: | :-------------: | :-----: | :-------------------------------: | | SOCKET_IPV6_TCLASS | int16_t | Yes | Yes | If enabled with RECVTCLASS | | SOCKET_IPV6_UNICAST_HOPS | int16_t | Yes | No | No | | SOCKET_IPV6_MULTICAST_HOPS | int16_t | Yes | No | No | | SOCKET_IPV6_ADDR_PREFERENCES | int | Yes | No | No | | SOCKET_IPV6_USE_MIN_MTU | int8_t | Yes | Yes | No | | SOCKET_IPV6_DONTFRAG | int8_t | Yes | Yes | No | | SOCKET_IPV6_FLOW_LABEL | int32_t | Yes | No | No | | SOCKET_IPV6_HOPLIMIT | int16_t | No | Yes | If enabled with RECVHOPLIMIT | | SOCKET_IPV6_PKTINFO | ns_in6_pktinfo_t | No | Yes | If enabled with RECVPKTINFO | | SOCKET_IPV6_RECVPKTINFO | bool | Yes | No | No | | SOCKET_IPV6_RECVHOPLIMIT | bool | Yes | No | No | | SOCKET_IPV6_RECVTCLASS | bool | Yes | No | No | | SOCKET_BROADCAST_PAN | int8_t | Yes | No | No | | SOCKET_LINK_LAYER_SECURITY | int8_t | Yes | No | No | | SOCKET_INTERFACE_SELECT | int8_t | Yes | No | No | Set an option for a socket

Used to specify miscellaneous options for a socket. Supported levels and names defined above.

Parameters:
socketThe socket ID.
levelThe protocol level.
opt_nameThe option name (interpretation depends on level). See OPTNAMES_IPV6.
opt_valueA pointer to value for the specified option.
opt_lenSize 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 97 of file socket_api_stub.c.

int8_t socket_shutdown ( int8_t  socket,
uint8_t  how 
)

A function to shut down a connection.

Parameters:
socketThe ID of the socket to be shut down.
howHow 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 no active TCP session was found.

Variable Documentation

const uint8_t ns_in6addr_any[16]

IPv6 wildcard address IN_ANY.

Definition at line 8 of file socket_api_stub.c.