CycloneTCP User Manual
CycloneTCP User Manual
The link layer is the lowest layer and is responsible for moving data in and out across
a physical network (commonly Ethernet).
The network layer is based on the Internet Protocol (IP). This layer is in charge of
delivering packets from a source host to a destination host across one or more
networks.
The transport layer provides end-to-end communication between applications. The
most well-known transport protocol is the Transmission Control Protocol (TCP) that is
used for connection-oriented transmissions. The connectionless User Datagram
Protocol (UDP) is another common transport protocol used for simpler message-
based transmissions.
The application layer contains the higher-level protocols used by most applications for
network communication. Some of the most popular ones are HTTP, FTP and SMTP.
ETHERNET
The Internet Protocol (IP) is implemented in two versions: IPv4 and IPv6. IPv4 is the most
widely deployed protocol but has limited address space (32-bit addresses). This limitation
motivates the development of the next-generation IPv6 protocol that features 128-bit
addresses.
HOST A HOST D
CLIENT
SOCKET SOCKET
APPLICATION
HOST B
CLIENT SERVER
SOCKET SOCKET
APPLICATION APPLICATION
HOST C
CLIENT
APPLICATION SOCKET SOCKET
Socket API consists of standard functions that ease the burden of writing networking
applications (establish a connection or wait for connection attempts from a peer, write data,
read data, terminate a connection). A socket can be set up in either connection-oriented or
connectionless mode:
Sockets may operate in one of two modes: blocking or non-blocking. A blocking socket will
not return control until it has sent (or received) some or all data specified for the operation.
Blocking mode may cause an application to hang if a socket is waiting for data that never
arrive. When operating in non-blocking mode, socket functions always return and are
guaranteed not to block the calling task.
TCP/IP TCP/IP
STACK STACK
USER USER USER
TASK 1 TASK 2 TASK 3 TIMER RX
TASK TASK
CycloneTCP adopts a two-task model. The first one handles periodic operations such as
ARP cache management, TCP packet retransmissions, IP fragment reassembly timeout or
multicast related operations. The second task is responsible for processing incoming
Ethernet frames when an RX interrupt is fired. As most of the processing is done at task
level, CycloneTCP minimizes interrupt lockout time and offers great real-time performances.
If the application makes use of multiple network interfaces, each interface owns a dedicated
RX task while periodic operations are still managed by the same task. This feature allows the
user to define a given priority for each network interface when processing incoming packets.
Folder Description
common/ Contains some features that are common to CycloneTCP and
CycloneSSL (endianness conversion functions, RTOS
abstraction layer, debugging facilities)
cyclone_tcp/core/ Contains the core features of the TCP/IP stack. All the files
included in this folder are mandatory and must be linked to
your project
cyclone_tcp/drivers/ Contains the supported Network controller drivers. Only pick
up the necessary driver files and discard others
cyclone_tcp/ipv4/ Adds support for IPv4. Link all the files from this folder if IPv4
support is desired
cyclone_tcp/ipv6/ Adds support for IPv6. Link all the files from this folder if IPv6
support is desired
cyclone_tcp/ppp/ Adds support for PPP
cyclone_tcp/dns/ Adds support for DNS
cyclone_tcp/mdns/ Adds support for mDNS (optional module)
cyclone_tcp/netbios/ Adds support for NetBIOS name system (optional module)
cyclone_tcp/dns_sd/ Adds support for DNS-SD (optional module)
cyclone_tcp/dhcp/ Adds support for DHCP client (optional module)
cyclone_tcp/dhcpv6/ Adds support for DHCPv6 client and relay agent (optional
module)
cyclone_tcp/ftp/ Adds support for FTP client/server (optional module)
cyclone_tcp/http/ Adds support for HTTP server (optional module)
cyclone_tcp/mqtt/ Adds support for MQTT client (optional module)
cyclone_tcp/smtp/ Adds support for SMTP client (optional module)
cyclone_tcp/snmp/ Adds support for SNMP agent (optional module)
cyclone_tcp/sntp/ Adds support for SNTP client (optional module)
cyclone_tcp/icecast/ Adds support for Icecast/SHOUTcast client (optional module)
cyclone_tcp/std_services/ Adds support for standard Internet services such as Echo,
Discard, Chargen and Daytime (optional module)
Edit project properties and add the following directories to the include path:
common/
cyclone_tcp/
Then, add the relevant source and header files to your project:
RTOS Dependencies
ChibiOS common/os_port_chibios.c
common/os_port_chibios.h
CMSIS-RTOS common/os_port_cmsis_rtos.c
common/os_port_cmsis_rtos.h
CMSIS-RTOS2 common/os_port_cmsis_rtos2.c
common/os_port_cmsis_rtos2.h
FreeRTOS common/os_port_freertos.c
common/os_port_freertos.h
Keil RTX common/os_port_rtx.c
common/os_port_rtx.h
Micrium µC/OS-II common/os_port_ucos2.c
common/os_port_ucos2.h
Micrium µC/OS-III common/os_port_ucos3.c
common/os_port_ucos3.h
Segger embOS common/os_port_embos.c
common/os_port_embos.h
SYS/BIOS (TI-RTOS) common/os_port_sys_bios.c
common/os_port_sys_bios.h
If your operating system is not supported, it will be necessary to port the RTOS by redefining
all the functions of the RTOS abstraction layer. For more details, refer to the dedicated
section.
If your device is not supported by the TCP/IP stack, Oryx Embedded can guide you through
the process of writing your own Ethernet driver.
The main function must be modified in order to configure the TCP/IP stack properly:
void main(void)
{
error_t error;
MacAddr macAddr;
NetInterface *interface;
//Initialize kernel
osInitKernel();
Syntax
error_t netInit(void)
Parameters
none
Return value
The netInit function returns NO_ERROR if the TCP/IP stack has been successfully initialized.
Otherwise a non-zero status code describing the error is returned.
Description
The netInit function initializes the TCP/IP stack and allocates the necessary resources. This
routine must be called before any other TCP/IP related functions.
Example
void main(void)
{
error_t error;
4.1.2 netConfigInterface
Syntax
error_t netConfigInterface(NetInterface *interface)
Parameters
Interface [in] Pointer to the network interface to be configured.
Return value
The netConfigInterface function returns NO_ERROR if the specified network interface has
been successfully configured. Otherwise a non-zero status code describing the error is
returned.
Description
The netConfigInterface function configures the underlying network driver and performs
interface-specific initialization. Before calling this function, the interface driver and the MAC
address of the interface must properly set.
If address auto-configuration is used (DHCP for IPv4, SLAAC and/or DHCPv6 pour IPv6),
the network interface does not require any further action. Otherwise the following parameters
should be manually configured:
IP address, subnet mask, default gateway and DNS servers (for IPv4)
Link-local address, global address, prefix, router and DNS servers (for IPv6)
Example
error_t error;
MacAddr macAddr;
Syntax
error_t netSetInterfaceName(NetInterface *interface,
const char_t *name)
Parameters
interface Pointer to the network interface to be configured.
Return value
The netSetInterfaceName function returns NO_ERROR if the interface has been successfully
renamed. Otherwise a non-zero status code describing the error is returned.
Description
By default, network interfaces are named eth0, eth1, etc. The netSetInterfaceName function
can be used to customize the name of the network interface.
Example
NetInterface *interface;
4.1.4 netSetDriver
Syntax
error_t netSetDriver(NetInterface *interface,
const NicDriver *driver)
Parameters
interface Pointer to the network interface to be configured.
Return value
The netSetDriver function returns NO_ERROR if the device driver has been successfully
assigned. Otherwise a non-zero status code describing the error is returned.
Description
The netSetDriver function is used to assign a device driver to the specified interface. This
function must be called before calling the netConfigInterface function
Example
Refer to netConfigInterface fonction
Syntax
error_t netSetPhyDriver (NetInterface *interface,
const PhyDriver *driver)
Parameters
interface Pointer to the network interface to be configured.
Return value
The netSetPhyDriver function returns NO_ERROR if the PHY device driver has been
successfully assigned. Otherwise a non-zero status code describing the error is returned.
Description
The netSetPhyDriver function is used to assign a PHY device driver to the specified
interface. For Ethernet controllers with integrated MAC + PHY, this function should not be
called.
Example
Refer to netConfigInterface fonction
4.1.6 netSetMacAddr
Syntax
error_t netSetMacAddr(NetInterface *interface,
const MacAddr *macAddr)
Parameters
interface Pointer to the network interface whose MAC address is to be configured.
Return value
The netSetMacAddr function returns NO_ERROR if the MAC address has been successfully
assigned. Otherwise a non-zero status code describing the error is returned.
Description
The netSetMacAddr function is used to assign a MAC address to the specified interface. For
Ethernet controller with factory preprogrammed MAC address, this function should not be
called.
Example
Refer to netConfigInterface fonction
Syntax
error_t ipv4SetHostAddr(NetInterface *interface, Ipv4Addr addr)
Parameters
interface Pointer to the network interface whose host address is to be configured.
Return value
The ipv4SetHostAddr function returns NO_ERROR if the IPv4 address has been
successfully assigned. Otherwise a non-zero status code describing the error is returned.
Description
The ipv4SetHostAddr function is used to manually assign an IPv4 address. This function
should not be called when the DHCP client is running. The currently assigned IPv4 address
can be retrieved using the ipv4GetHostAddr function.
Example
error_t error;
Ipv4Addr ipv4Addr;
4.2.2 ipv4SetSubnetMask
Syntax
error_t ipv4SetSubnetMask(NetInterface *interface, Ipv4Addr mask)
Parameters
interface Pointer to the network interface whose subnet mask is to be configured.
Return value
The ipv4SetSubnetMask function returns NO_ERROR if the subnet mask has been
successfully updated. Otherwise a non-zero status code describing the error is returned.
Description
The ipv4SetSubnetMask is used to manually configure the subnet mask. This function should
not be called when the DHCP client is running. The current subnet mask can be retrieved
using the ipv4GetSubnetMask function.
Example
Refer to ipv4SetHostAddr function
Syntax
error_t ipv4SetDefaultGateway(NetInterface *interface, Ipv4Addr addr)
Parameters
interface Pointer to the network interface to be configured.
Return value
The ipv4SetDefaultGateway function returns NO_ERROR if the address of gateway has
been successfully updated. Otherwise a non-zero status code describing the error is
returned.
Description
The ipv4SetDefaultGateway is used to manually assign the IPv4 address of the gateway.
This function should not be called when the DHCP client is running. The currently assigned
address can be retrieved using the ipv4GetDefaultGateway function.
Example
Refer to ipv4SetHostAddr function
4.2.4 ipv4SetDnsServer
Syntax
error_t ipv4SetDnsServer(NetInterface *interface, uint_t num,
Ipv4Addr addr)
Parameters
interface Pointer to the network interface to be configured.
num This parameter selects between the primary and secondary DNS server.
Return value
The ipv4SetDnsServer function returns NO_ERROR if the address of DNS server has been
successfully updated. Otherwise a non-zero status code describing the error is returned.
Description
The ipv4SetDnsServer is used to manually assign the IPv6 address of the primary or the
secondary DNS server. This function should not be called when the DHCP client is running.
The currently assigned address can be retrieved using the ipv4GetDnsServer function.
Example
error_t error;
Ipv4Addr ipv4Addr;
Syntax
error_t ipv6SetLinkLocalAddr(NetInterface *interface,
const Ipv6Addr *addr)
Parameters
interface Pointer to the network interface to be configured.
Return value
The ipv6SetLinkLocalAddr function returns NO_ERROR if the link-local address has been
successfully assigned. Otherwise a non-zero status code describing the error is returned.
Description
The ipv6SetLinkLocalAddr function is used to manually assign the link-local address. The
current link-local address can be retrieved using the ipv6GetLinkLocalAddr function.
Example
error_t error;
Ipv6Addr ipv6Addr;
//Set router
ipv6StringToAddr("fe80::1111:2222", &ipv6Addr);
ipv6SetRouter(interface, &ipv6Addr);
4.3.2 ipv6SetPrefix
Syntax
error_t ipv6SetPrefix(NetInterface *interface,
const Ipv6Addr *prefix, uint_t length)
Parameters
interface Pointer to the network interface whose subnet mask is to be configured.
Return value
The ipv6SetPrefix function returns NO_ERROR if the IPv6 prefix has been successfully
updated. Otherwise a non-zero status code describing the error is returned.
Description
The ipv6SetPrefix is used to manually configure the IPv6 prefix. The current IPv6 prefix can
be retrieved using the ipv6GetPrefix function.
Example
Refer to ipv6SetLinkLocalAddr function
Syntax
error_t ipv6SetGlobalAddr(NetInterface *interface, const Ipv6Addr *addr)
Parameters
interface Pointer to the network interface whose IPv6 global address is to be
configured.
Return value
The ipv6SetGlobalAddr function returns NO_ERROR if the global address has been
successfully updated. Otherwise a non-zero status code describing the error is returned.
Description
The ipv6SetGlobalAddr is used to manually configure the IPv6 global address. The currently
assigned address can be retrieved using the ipv6GetGlobalAddr function.
Example
Refer to ipv6SetLinkLocalAddr function
4.3.4 ipv6SetRouter
Syntax
error_t ipv6SetRouter(NetInterface *interface, const Ipv6Addr *addr)
Parameters
interface Pointer to the network interface to be configured.
Return value
The ipv6SetRouter function returns NO_ERROR if the address of the IPv6 router has been
successfully updated. Otherwise a non-zero status code describing the error is returned.
Description
The ipv6SetRouter is used to manually assign the address of the IPv6 router. The currently
assigned address can be retrieved using the ipv6GetRouter function.
Example
Refer to ipv6SetLinkLocalAddr function
Syntax
error_t ipv6SetDnsServer (NetInterface *interface, uint_t num,
const Ipv6Addr *addr)
Parameters
interface Pointer to the network interface to be configured.
num This parameter selects between the primary and secondary DNS server.
Return value
The ipv6SetDnsServer function returns NO_ERROR if the address of DNS server has been
successfully updated. Otherwise a non-zero status code describing the error is returned.
Description
The ipv6SetDnsServer is used to manually assign the IPv6 address of the primary or the
secondary DNS server. The currently assigned address can be retrieved using the
ipv6GetDnsServer function.
Example
error_t error;
Ipv6Addr ipv6Addr;
Syntax
Socket *socketOpen(uint_t type, uint_t protocol)
Parameters
type The type specification for the new socket. This parameter can be one of
the following values:
SOCKET_TYPE_STREAM for connected sockets (TCP)
SOCKET_TYPE_DGRAM for connectionless sockets (UDP)
SOCKET_TYPE_RAW_IP for sending/receiving raw IP packets
SOCKET_TYPE_RAW_ETH for sending/receiving raw Ethernet
packets
Return value
If no error occurs, socketOpen returns a valid pointer referencing the new socket. Otherwise,
NULL is returned.
Description
The socketOpen creates a socket bound to the specified protocol. The created socket can
operate in either connection-oriented or connectionless mode. Connection-oriented sockets
use TCP as underlying transport protocol and provide a reliable stream-based
communication between hosts. Connectionless sockets are based on UDP and provide
simple but unreliable transfer mechanism. There is no guarantee of delivery, ordering, or
duplicate protection. Raw sockets are special sockets that allow direct sending and receiving
of raw IP packets.
Example
Socket *socket;
Syntax
error_t socketSetTimeout(Socket *socket, systime_t timeout)
Parameters
socket Valid handle referencing a socket.
Return value
If the timeout value has been successfully updated, NO_ERROR is returned. Otherwise a
non-zero status code describing the error is returned.
Description
The socketSetTimeout function is called to define a timeout limit when performing socket
operations. When a value non-zero is specified, socket functions (socketConnect,
socketAccept, socketSend, socketSendTo, socketReceive, socketReceiveFrom) are
guaranteed not to block for a time longer than timeout. In the case a socket operation timed
out, an ERROR_TIMEOUT status code will be returned. When timeout is zero, the socket
operates in non-blocking mode and will never block.
Example
error_t error;
Socket *socket;
IpAddr serverIpAddr;
//Server address
ipStringToAddr("192.168.0.100", &serverIpAddr);
4.4.3 socketSetTxBufferSize
Syntax
error_t socketSetTxBufferSize(Socket *socket, size_t size)
Parameters
socket Valid handle referencing a socket.
Return value
If the TX buffer size has been successfully updated, NO_ERROR is returned. Otherwise a
non-zero status code describing the error is returned.
Description
The socketSetTxBufferSize can be used to tune the size of the send buffer for any
connection-oriented socket. The size of the buffer can no longer be changed once the
connection has been established.
Example
error_t error;
Socket *socket;
IpAddr serverIpAddr;
//Server address
ipStringToAddr("192.168.0.100", &serverIpAddr);
Syntax
error_t socketSetRxBufferSize(Socket *socket, size_t size)
Parameters
socket Valid handle referencing a socket.
Return value
If the RX buffer size has been successfully updated, NO_ERROR is returned. Otherwise a
non-zero status code describing the error is returned.
Description
The socketSetRxBufferSize can be used to tune the size of the receive buffer for any
connection-oriented socket. The size of the buffer can no longer be changed once the
connection has been established.
Example
error_t error;
Socket *socket;
IpAddr serverIpAddr;
//Server address
ipStringToAddr("192.168.0.100", &serverIpAddr);
4.4.5 socketBindToInterface
Syntax
error_t socketBindToInterface(Socket *socket,
NetInterface *interface)
Parameters
socket Valid handle referencing a socket.
Return value
The socketBindToInterface function returns NO_ERROR if the socket has been successfully
associated with the specified interface. Otherwise a non-zero status code describing the
error is returned.
Description
By default, a socket accepts data, regardless the network interface the request arrives and
automatically chooses the most appropriate interface when sending data. The
socketBindToInterface function can be called to explicitly assign a specific network interface
to a socket. An explicitly bound socket accepts data only from the interface the socket is
associated with, and sends data to this particular interface only.
Example
Socket *socket;
Syntax
error_t socketBind(Socket *socket, const IpAddr *localIpAddr,
uint16_t localPort)
Parameters
socket Valid handle referencing a socket.
localIpAddr Local address to assign to the bound socket. The wildcard IP address
IP_ADDR_ANY can be specified to associate the socket only with a port
number.
Return value
The socketBind function returns NO_ERROR if the socket has been successfully associated
with the specified address. Otherwise a non-zero status code describing the error is returned.
Description
The socketBind function is required on an unconnected socket before subsequent calls to the
socketListen function. It is normally used to bind to either connection-oriented (stream) or
connectionless (datagram) sockets.
Example
error_t error;
Socket *serverSocket;
Socket *clientSocket;
IpAddr clientIpAddr;
uint16_t clientPort;
4.4.7 socketConnect
Syntax
error_t socketConnect(Socket *socket, const IpAddr *remoteIpAddr,
uint16_t remotePort)
Parameters
socket Valid handle referencing a socket.
remotePort Remote port number that will be used to establish the connection.
Return value
The socketConnect function returns NO_ERROR if the connection succeeds. Otherwise a
non-zero status code describing the error is returned.
Description
The socketConnect function is normally used to establish a connection with the specified
remote host.
Stream sockets can call socketConnect only once, but datagram sockets can call
socketConnect multiple times to change their association.
4.4.8 socketListen
Syntax
error_t socketListen(Socket *socket, uint_t backlog)
Parameters
socket Valid handle referencing a socket.
backlog The maximum length of the pending connection queue. If this parameter
is zero, then the default backlog value is used instead
Return value
The socketListen function returns NO_ERROR if the socket has been successfully placed
into listening mode. Otherwise a non-zero status code describing the error is returned.
Description
The socketListen function causes a socket to listen for incoming connection attempts. This
function is only necessary for connection-oriented sockets
Example
Refer to socketBind function
Syntax
Socket *socketAccept(Socket *socket, IpAddr *clientIpAddr,
uint16_t *clientPort)
Parameters
socket Valid handle referencing a socket.
clientPort Optional pointer to a 16-bit integer that receives the port number used by
the client (can be NULL).
Return value
The function returns a handle to the socket in which the actual connection is made. If the
timeout expires and the connection request queue is still empty, NULL is returned.
Description
When passive socket is listening to for incoming connection attempts, the socketAccept
retrieve the first pending connection request and return the corresponding socket. If the
socketSetTimeout function has been previously called, socketAccept will block the task for
specified amount of time before returning. If non connection requests are pending in the
queue, NULL is returned.
Example
Refer to socketBind function
4.4.10 socketSend
Syntax
error_t socketSend(Socket *socket, const void *data,
uint_t length, uint_t *written, uint_t flags)
Parameters
socket Valid handle referencing a connected socket.
flags A set of flags that influences the behavior of this function. For most
applications, a value of 0 is recommended.
Return value
The socketSend function returns NO_ERROR if the specified data has been successfully
sent. Otherwise a non-zero status code describing the error is returned.
Description
The socketSend function sends data on a connected socket. If the socket operates in
blocking mode, the function will not return until all the specified data has been sent (or at
least buffered for connection-oriented sockets). If the write operation timed out, the
ERROR_TIMEOUT status code is returned. The written parameter always reflects the actual
number of bytes that have been processed. In non-blocking mode, the socket sends as many
data bytes as possible without blocking and return immediately.
Example
#define SERVER_NAME "www.oryx-embedded.com"
#define SERVER_PORT 80
#define REQUEST_URI "/test.php"
error_t error;
size_t length;
uint8_t buffer[256];
Socket *socket;
IpAddr serverIpAddr;
4.4.11 socketSendTo
Syntax
error_t socketSendTo(Socket *socket, const IpAddr *destIpAddr,
uint16_t destPort, const void *data, uint_t length,
uint_t *written, uint_t flags)
Parameters
socket Valid handle referencing a socket.
flags A set of flags that influences the behavior of this function. For most
applications, a value of 0 is recommended.
Return value
The socketSendTo function returns NO_ERROR if the specified data has been successfully
sent. Otherwise a non-zero status code describing the error is returned.
Description
The socketSendTo function writes outgoing data to the specified host. For datagram
(connection-oriented) sockets, the remoteIpAddr parameter can specify either a unicast,
multicast or broadcast address. For stream (connection-oriented) sockets, remoteIpAddr and
remotePort are ignored making socketSendTo equivalent to socketSend.
//Server IP address
error = ipStringToAddr("fe80::1234:5678:abcd", &serverIpAddr);
//Failed to convert the string to a valid IP address?
if(error) {...}
4.4.12 socketReceive
Syntax
error_t socketReceive(Socket *socket, void *data,
uint_t size, uint_t *received, uint_t flags)
Parameters
socket Valid handle referencing a connected socket.
flags A set of flags that influences the behavior of this function. For most
applications, a value of 0 is recommended.
Return value
The socketReceive function returns NO_ERROR if some or all of the requested data has
been successfully received. Otherwise a non-zero status code describing the error is
returned.
Description
The socketReceive function receives data from a connected socket. If the socket operates in
blocking mode, the function will not return until it has received some or all data specified for
the operation. It is normal for a blocking socket not to receive all the requested data. The
received parameter always reflects the actual number of bytes that have been read. In non-
blocking mode, the function reads as much data as possible without blocking and returns
immediately.
When the SOCKET_FLAG_PEEK is set, the data is copied into the buffer, but is not
removed from the input queue.
When the SOCKET_FLAG_WAIT_ALL flag is specified, the read operation will complete only
if the buffer supplied by the caller is completely full, or the connection is closed, or the
timeout expires. This flags is meaningless for connectionless sockets.
Example
Refer to socketSend function
Syntax
error_t socketReceiveFrom(Socket *socket, IpAddr *srcIpAddr,
uint16_t *srcPort, void *data, uint_t size, uint_t *received,
uint_t flags)
Parameters
socket Valid handle referencing a socket.
srcIpAddr Optional pointer to a structure that receives the source IP address of the
incoming datagram (can be NULL).
srcPort Optional pointer to a 16-bit integer that receives the source port number
of the incoming datagram (can be NULL).
flags A set of flags that influences the behavior of this function. For most
applications, a value of 0 is recommended.
Return value
The socketReceiveFrom function returns NO_ERROR if some data has been successfully
received. Otherwise a non-zero status code describing the error is returned.
Description
The socketReceiveFrom function receives a message from a connectionless socket. If no
messages are available in the receive queue, the socket wait for a message to arrive, unless
the socket is non-blocking in which case the function returns immediately.
When the SOCKET_FLAG_PEEK is set, the data is copied into the buffer, but is not
removed from the input queue.
Example
error_t error;
size_t length;
char_t s[40];
uint8_t buffer[256];
Socket *socket;
IpAddr clientIpAddr;
uint16_t clientPort;
//Endless loop
while(1)
{
//Wait for an incoming datagram
error = socketReceiveFrom(socket, &clientIpAddr,
&clientPort, buffer, sizeof(buffer), &length, 0);
Syntax
error_t socketReceiveEx(Socket *socket, IpAddr *srcIpAddr,
uint16_t *srcPort, IpAddr *destIpAddr, void *data, size_t size,
size_t *received, uint_t flags)
Parameters
socket Valid handle referencing a socket.
srcIpAddr Optional pointer to a structure that receives the source IP address of the
incoming datagram (can be NULL).
srcPort Optional pointer to a 16-bit integer that receives the source port number
of the incoming datagram (can be NULL).
flags A set of flags that influences the behavior of this function. For most
applications, a value of 0 is recommended.
Return value
The socketReceiveEx function returns NO_ERROR if some data has been successfully
received. Otherwise a non-zero status code describing the error is returned.
Description
The socketReceiveEx function receives a message from a connectionless socket. If no
messages are available in the receive queue, the socket wait for a message to arrive, unless
the socket is non-blocking in which case the function returns immediately. This function is
similar to the socketReceiveFrom function expect that the destination IP address is also
returned.
When the SOCKET_FLAG_PEEK is set, the data is copied into the buffer, but is not
removed from the input queue.
Example
Refer to socketReceiveFrom function
4.4.15 socketPoll
Syntax
error_t socketPoll(SocketEventDesc *eventDesc, uint_t size,
OsEvent *extEvent, systime_t timeout)
Parameters
eventDesc Set of entries specifying the events the user is interested in.
event External event that can abort the wait if necessary (optional parameter).
Return value
The socketPoll function returns NO_ERROR if at least one socket is ready to perform I/O.
Otherwise a non-zero status code describing the error is returned.
Description
The socketPoll function waits for one of a set of sockets to become ready to perform I/O. The
eventDesc structure must be initialized prior to function call with desired socket events
(eventMask field). When the function completes, the eventFlags field stores the events that
have been fired for each socket.
The following table lists the events that can be specified in the eventDesc field. Some events
can only be used in conjunction with connection-oriented sockets and are meaningless for
connectionless sockets.
while(1)
{
//Set the events the application is interrested in
eventDesc[0].socket = socket1;
eventDesc[0].eventMask = SOCKET_EVENT_RX_READY;
eventDesc[1].socket = socket2;
eventDesc[1].eventMask = SOCKET_EVENT_RX_READY;
//Sanity check
if(!error)
{
//Check the state of the first socket
if(eventDesc[0].eventFlags & SOCKET_EVENT_RX_READY)
{
//The receive function should not block since the first
//socket is marked as readable
error = socketReceiveFrom(socket1, NULL, NULL,
buffer, sizeof(buffer), &length, 0);
}
//Check the state of the second socket
if(eventDesc[1].eventFlags & SOCKET_EVENT_RX_READY)
{
//The receive function should not block since the second
//socket is marked as readable
error = socketReceiveFrom(socket2, NULL, NULL,
buffer, sizeof(buffer), &length, 0);
}
}
}
4.4.16 socketShutdown
Syntax
error_t socketShutdown(Socket *socket, uint_t how)
Parameters
socket Valid handle referencing a socket.
how A flag that describes what types of operation will no longer be allowed.
Possible values are SOCKET_SD_RECEIVE, SOCKET_SD_SEND or
SOCKET_SD_BOTH.
Return value
The socketShutdown function returns NO_ERROR if the operation was successful.
Otherwise a non-zero status code describing the error is returned.
Description
The socketShutdown function is used to disable reception, transmission or both. Call this
function to gracefully disconnect from the remote host. socketShutdown does not close the
socket. Any resources attached to the socket will be not released until socketClose is
invoked.
//Endless loop
while(1)
{
//Accept an incoming connection
clientSocket = socketAccept(serverSocket, NULL, NULL);
4.4.17 socketClose
Syntax
void socketClose(Socket *socket)
Parameters
socket Handle identifying the socket to close.
Return value
None.
Description
The socketClose function aborts any running connection and causes the system to release
all the resources allocated to the socket. A closed socket cannot be used for communication
anymore.
4.5.1 socket
4.5.2 bind
4.5.3 connect
4.5.4 listen
4.5.5 accept
4.5.6 send
4.5.7 sendto
4.5.8 recv
4.5.9 recvfrom
4.5.10 select
4.5.11 setsockopt
4.5.12 getsockopt
4.5.13 shutdown
4.5.14 close
Syntax
error_t ipJoinMulticastGroup(NetInterface *interface,
const IpAddr *groupAddr)
Parameters
interface Pointer to the network interface on which the multicast group
membership is to be added. This parameter is optional and can be
NULL. In this case the default network interface is used.
Return value
The ipJoinMulticastGroup function returns NO_ERROR if the specified host group was
successfully joined. Otherwise a non-zero status code describing the error is returned.
Description
The ipJoinMulticastGroup function adds the multicast membership on the specified interface,
if not yet installed. The underlying network interface receives multicast traffic only for the
groups that were previously joined.
Example
error_t error;
IpAddr groupAddr;
4.6.2 ipLeaveMulticastGroup
Syntax
error_t ipLeaveMulticastGroup(NetInterface *interface,
const IpAddr *groupAddr)
Parameters
interface Pointer to the network interface on which the multicast group
membership must be dropped. This parameter is optional and can be
NULL. In this case the default network interface is used.
Return value
The ipLeaveMulticastGroup function returns NO_ERROR if the specified membership was
successfully dropped. Otherwise a non-zero status code describing the error is returned.
Description
The ipLeaveMulticastGroup function can be called to leave the multicast group identified by
groupAddr on the given network interface. ipLeaveMulticastGroup may succeed, but the
membership persists, if more than one upper-layer protocol has requested membership in
the same group.
Example
error_t error;
IpAddr groupAddr;
Syntax
error_t getHostByName(NetInterface *interface, const char_t *name,
IpAddr *ipAddr, uint_t flags)
Parameters
Interface [in] Pointer to the network interface on which the host name resolution
should be performed. This parameter is optional and can be NULL. In
this case the default network interface is used.
name [in] Pointer to the NULL-terminated string containing the host name to be
resolved.
Return value
The getHostByName function returns NO_ERROR if the host name was successfully
resolved. Otherwise a non-zero status code describing the error is returned.
Description
The getHostByName function translates a given host name to an IP address.
The last parameter serves a guidance to restrict the host name resolution to a limited set of
protocols. If this parameter is zero, the function will automatically select the most appropriate
protocol (DNS, mDNS, NetBIOS name service or LLMNR) to resolve the specified host
name.
Example
error_t error;
size_t i;
size_t n;
char_t buffer[40];
IpAddr ipAddr;
Syntax
error_t macStringToAddr(const char_t *str, MacAddr *macAddr)
Parameters
str [in] NULL-terminated string representing the MAC address.
macAddr [out] Pointer to a buffer in which to store the binary representation of the MAC
address.
Return value
The macStringToAddr function returns NO_ERROR if the input string was successfully
converted to a valid MAC address. Otherwise a non-zero status code describing the error is
returned.
Description
The macStringToAddr function converts a string representation of an MAC address into its
binary form. This function is case insensitive and accepts two commonly used formats:
Six groups of two hexadecimal digits separated by hyphens
Six groups of two hexadecimal digits separated by colons
Example
error_t error;
MacAddr macAddr;
4.8.2 macAddrToString
Syntax
char_t *macAddrToString(const MacAddr *macAddr, char_t *str)
Parameters
macAddr [in] Binary representation of the MAC address.
str |out] Pointer to a buffer in which to store the text representation of the MAC
address. The buffer must be at least 18-byte long.
Return value
The macAddrToString function returns a pointer to the resulting string.
Description
The macAddrToString function converts a binary MAC address into a human-friendly string
representation. The output string consists of six groups of two hexadecimal digits separated
by hyphens.
Example
To be done
Syntax
error_t ipStringToAddr(const char_t *str, IpAddr *ipAddr)
Parameters
str [in] NULL-terminated string representing the IP address.
ipAddr [out] Pointer to a buffer in which to store the binary representation of the IP
address.
Return value
The ipStringToAddr function returns NO_ERROR if the input string was successfully
converted to a valid IPv4 or IPv6 address. Otherwise a non-zero status code describing the
error is returned.
Description
The ipStringToAddr function converts a string representation of an IP address into its binary
form. The function accepts dot-decimal notation for IPv4 addresses. The conventional text
representations of IPv6 addresses defined in RFC 4291 are supported as well.
Example
error_t error;
IpAddr ipAddr;
4.8.4 ipAddrToString
Syntax
char_t *ipAddrToString(const IpAddr *ipAddr, char_t *str)
Parameters
ipAddr [in] Binary representation of the IP address.
str |out] Pointer to a buffer in which to store the text representation of the IP
address. The buffer must be at least 16-byte long for IPv4 addresses and
40-byte long for IPv6.
Return value
The ipAddrToString function returns a pointer to the resulting string.
Description
The ipAddrToString function converts a binary IP address into a human-friendly string
representation. IPv4 addresses are converted to dot-decimal form. ipAddrToString formats
IPv6 addresses according to RFC 5952 recommendations.
Example
error_t error;
char_t buffer[40];
IpAddr ipAddr;