S2OPC OPCUA Toolkit
Functions
sopc_udp_sockets.h File Reference

A platform independent API to use sockets. More...

#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include "sopc_buffer.h"
#include "sopc_enums.h"
#include "sopc_raw_sockets.h"

Go to the source code of this file.

Functions

SOPC_Socket_AddressInfoSOPC_UDP_SocketAddress_Create (bool IPv6, const char *node, const char *service)
 Create a new UDP socket address using getaddrinfo. More...
 
void SOPC_UDP_SocketAddress_Delete (SOPC_Socket_AddressInfo **addr)
 
SOPC_ReturnStatus SOPC_UDP_Socket_CreateToReceive (SOPC_Socket_AddressInfo *listenAddress, const char *interfaceName, bool setReuseAddr, bool setNonBlocking, Socket *sock)
 Create a new UDP socket and bind it. More...
 
SOPC_ReturnStatus SOPC_UDP_Socket_CreateToSend (SOPC_Socket_AddressInfo *destAddress, const char *interfaceName, bool setNonBlocking, Socket *sock)
 Create a new UDP socket and bind it. More...
 
SOPC_ReturnStatus SOPC_UDP_Socket_SendTo (Socket sock, const SOPC_Socket_AddressInfo *destAddr, SOPC_Buffer *buffer)
 Send data through the UDP socket to given IP address and port. More...
 
SOPC_ReturnStatus SOPC_UDP_Socket_ReceiveFrom (Socket sock, SOPC_Buffer *buffer)
 Receive data on the UDP socket from given IP address and port. More...
 
SOPC_ReturnStatus SOPC_UDP_Socket_Set_MulticastTTL (Socket sock, uint8_t TTL_scope)
 Set the Multicast TTL configuration value (default value is 1) Controls the live time of datagram (decremented by 1 by each router). In IPv4 multicasting is also used as threshold: More...
 
void SOPC_UDP_Socket_Close (Socket *sock)
 Close the socket connection and/or clear the socket If the socket was automatically added to a multicast group at creation (see SOPC_UDP_Socket_CreateToReceive), then the multicast group is dropped before closing the socket. More...
 

Detailed Description

A platform independent API to use sockets.

Function Documentation

◆ SOPC_UDP_SocketAddress_Create()

SOPC_Socket_AddressInfo* SOPC_UDP_SocketAddress_Create ( bool  IPv6,
const char *  node,
const char *  service 
)

Create a new UDP socket address using getaddrinfo.

Parameters
IPv6Flag to activate IPv6 protocol
nodeAn IPv4 or IPv6 address or hostname. For default local address set to NULL.
serviceThe port to use.
Returns
A instantiated address, NULL otherwise. It must be deleted when not used anymore.

◆ SOPC_UDP_SocketAddress_Delete()

void SOPC_UDP_SocketAddress_Delete ( SOPC_Socket_AddressInfo **  addr)

◆ SOPC_UDP_Socket_CreateToReceive()

SOPC_ReturnStatus SOPC_UDP_Socket_CreateToReceive ( SOPC_Socket_AddressInfo listenAddress,
const char *  interfaceName,
bool  setReuseAddr,
bool  setNonBlocking,
Socket sock 
)

Create a new UDP socket and bind it.

Parameters
listenAddressAddress on which the socket shall listen for input data. If listenAddress is a multicast address, then the socket will automatically be added in a dedicated multicast group.
interfaceNameThe name of the interface to use, or null if unspecified. The name shall be a null-terminated string if defined.
setReuseAddrIf value is not false (0) the socket is configured to be reused
setNonBlockingIf set the socket is non-blocking for reception
[out]sockValue pointed is set with the newly created socket
Returns
SOPC_STATUS_OK if operation succeeded, SOPC_STATUS_NOK otherwise.

◆ SOPC_UDP_Socket_CreateToSend()

SOPC_ReturnStatus SOPC_UDP_Socket_CreateToSend ( SOPC_Socket_AddressInfo destAddress,
const char *  interfaceName,
bool  setNonBlocking,
Socket sock 
)

Create a new UDP socket and bind it.

Parameters
destAddressDestination IP address, used to determine version of the protocol
interfaceNameThe name of the interface to use, or null if unspecified. The name shall be a null-terminated string if defined.
setNonBlockingIf set the socket is non-blocking for sending
[out]sockValue pointed is set with the newly created socket
Returns
SOPC_STATUS_OK if operation succeeded, SOPC_STATUS_NOK otherwise.

◆ SOPC_UDP_Socket_SendTo()

SOPC_ReturnStatus SOPC_UDP_Socket_SendTo ( Socket  sock,
const SOPC_Socket_AddressInfo destAddr,
SOPC_Buffer buffer 
)

Send data through the UDP socket to given IP address and port.

Parameters
sockThe socket used for sending
destAddrThe destination IPv4 address
bufferThe buffer containing data to be sent. Buffer considered with buffer->position 0 and containing buffer->length bytes.
Returns
SOPC_STATUS_OK if operation succeeded, SOPC_STATUS_WOULD_BLOCK if partially sent, SOPC_STATUS_NOK otherwise otherwise.

◆ SOPC_UDP_Socket_ReceiveFrom()

SOPC_ReturnStatus SOPC_UDP_Socket_ReceiveFrom ( Socket  sock,
SOPC_Buffer buffer 
)

Receive data on the UDP socket from given IP address and port.

Parameters
sockThe socket used for receiving
bufferThe buffer with buffer->current_size bytes
Returns
SOPC_STATUS_OK if operation succeeded, SOPC_STATUS_OUT_OF_MEMORY if possible partial reception, SOPC_STATUS_NOK

◆ SOPC_UDP_Socket_Set_MulticastTTL()

SOPC_ReturnStatus SOPC_UDP_Socket_Set_MulticastTTL ( Socket  sock,
uint8_t  TTL_scope 
)

Set the Multicast TTL configuration value (default value is 1) Controls the live time of datagram (decremented by 1 by each router). In IPv4 multicasting is also used as threshold:

  • 0 : Restricted to current host
  • 1 : Restricted to same subnet
  • <32 : Restricted to same site
  • <64 : Restricted to same region
  • <128: Restricted to same continent
  • <255: Unrestricted
Parameters
sockThe socket to configure
TTL_scopeThe TTL value to set
Returns
SOPC_STATUS_OK if operation succeeded, SOPC_STATUS_NOK otherwise

◆ SOPC_UDP_Socket_Close()

void SOPC_UDP_Socket_Close ( Socket sock)

Close the socket connection and/or clear the socket If the socket was automatically added to a multicast group at creation (see SOPC_UDP_Socket_CreateToReceive), then the multicast group is dropped before closing the socket.

Parameters
sockThe socket to disconnect and/or clear