S2OPC OPCUA Toolkit
Macros | Typedefs | Functions
sopc_eth_sockets.h File Reference

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

#include <stdbool.h>
#include "p_sopc_sockets.h"
#include "sopc_buffer.h"
#include "sopc_enums.h"

Go to the source code of this file.

Macros

#define ETHERNET_HEADER_SIZE   14
 

Typedefs

typedef struct SOPC_ETH_Socket_SendAddressInfo SOPC_ETH_Socket_SendAddressInfo
 Socket addressing information for sending operation type. More...
 
typedef struct SOPC_ETH_Socket_ReceiveAddressInfo SOPC_ETH_Socket_ReceiveAddressInfo
 Socket addressing information for listening operation type. More...
 

Functions

SOPC_ReturnStatus SOPC_ETH_Socket_CreateSendAddressInfo (const char *interfaceName, const char *destMACaddr, SOPC_ETH_Socket_SendAddressInfo **sendAddInfo)
 Create a new address information for packet sending. More...
 
SOPC_ReturnStatus SOPC_ETH_Socket_CreateReceiveAddressInfo (const char *interfaceName, bool recvMulticast, const char *destMACaddr, const char *sourceMACaddr, SOPC_ETH_Socket_ReceiveAddressInfo **recvAddInfo)
 Create a new address information for packet reception. More...
 
SOPC_ReturnStatus SOPC_ETH_Socket_CreateToReceive (SOPC_ETH_Socket_ReceiveAddressInfo *receiveAddrInfo, bool setNonBlocking, Socket *sock)
 Create a new ETH socket, bind it using and add membership for multicast if active in receiveAddrInfo. Membership for multicast is added on specified interface in receiveAddrInfo or all compatible interfaces otherwise. More...
 
SOPC_ReturnStatus SOPC_ETH_Socket_CreateToSend (SOPC_ETH_Socket_SendAddressInfo *sendAddrInfo, bool setNonBlocking, Socket *sock)
 Create a new ETH socket using sendAddrInfo properties. More...
 
SOPC_ReturnStatus SOPC_ETH_Socket_SendTo (Socket sock, const SOPC_ETH_Socket_SendAddressInfo *sendAddrInfo, uint16_t etherType, SOPC_Buffer *buffer)
 Send data through the ETH socket to given IP address and port. More...
 
SOPC_ReturnStatus SOPC_ETH_Socket_ReceiveFrom (Socket sock, const SOPC_ETH_Socket_ReceiveAddressInfo *receiveAddrInfo, bool checkEtherType, uint16_t etherType, SOPC_Buffer *buffer)
 Receive data on the ETH socket from given address info. More...
 
void SOPC_ETH_Socket_Close (Socket *sock)
 Close the socket connection and/or clear the socket. More...
 

Detailed Description

A platform independent API to use packet sockets.

Warning
: packet socket require administrative rights

Macro Definition Documentation

◆ ETHERNET_HEADER_SIZE

#define ETHERNET_HEADER_SIZE   14

Typedef Documentation

◆ SOPC_ETH_Socket_SendAddressInfo

Socket addressing information for sending operation type.

◆ SOPC_ETH_Socket_ReceiveAddressInfo

Socket addressing information for listening operation type.

Function Documentation

◆ SOPC_ETH_Socket_CreateSendAddressInfo()

SOPC_ReturnStatus SOPC_ETH_Socket_CreateSendAddressInfo ( const char *  interfaceName,
const char *  destMACaddr,
SOPC_ETH_Socket_SendAddressInfo **  sendAddInfo 
)

Create a new address information for packet sending.

Parameters
interfaceNameName of the network interface to use to send packet (mandatory)
destMACaddrDestination MAC address to send packet as a string representation (mandatory). It shall be hexadecimal values separated by character ':' and zero terminated.

E.g.: "01-00-00-01-04-00"

Parameters
[out]sendAddInfoValue pointed is set with a newly allocated and fulfilled send address information. Caller shall call SOPC_Free when not used anymore.
Returns
SOPC_STATUS_OK if operation succeeded, SOPC_OUT_OF_MEMORY in case of allocation failure, SOPC_STATUS_INVALID_PARAMETERS in case of incorrect parameters leading to failure.

◆ SOPC_ETH_Socket_CreateReceiveAddressInfo()

SOPC_ReturnStatus SOPC_ETH_Socket_CreateReceiveAddressInfo ( const char *  interfaceName,
bool  recvMulticast,
const char *  destMACaddr,
const char *  sourceMACaddr,
SOPC_ETH_Socket_ReceiveAddressInfo **  recvAddInfo 
)

Create a new address information for packet reception.

Parameters
interfaceNameName of the network interface to use to receive packets (optional)
recvMulticastIf set, multicast packet reception is active. A multicast address should be specified in destMACaddr, otherwise packet reception is active for all multicast addresses.
destMACaddrDestination MAC address of packets accepted as a string representation (optional). It shall be hexadecimal values separated by character ':' and zero terminated. It shall be set only if destMACaddr is set. E.g.: "01-00-00-01-04-00"
sourceMACaddrSource MAC address of packets accepted as a string representation (optional). It shall be hexadecimal values separated by character ':' and zero terminated. E.g.: "0A:00:00:01:04:00"
[out]recvAddInfoValue pointed is set with a newly allocated and fulfilled receive address information. Caller shall call SOPC_Free when not used anymore.
Returns
SOPC_STATUS_OK if operation succeeded, SOPC_OUT_OF_MEMORY in case of allocation failure, SOPC_STATUS_INVALID_PARAMETERS in case of incorrect parameters leading to failure.

◆ SOPC_ETH_Socket_CreateToReceive()

SOPC_ReturnStatus SOPC_ETH_Socket_CreateToReceive ( SOPC_ETH_Socket_ReceiveAddressInfo receiveAddrInfo,
bool  setNonBlocking,
Socket sock 
)

Create a new ETH socket, bind it using and add membership for multicast if active in receiveAddrInfo. Membership for multicast is added on specified interface in receiveAddrInfo or all compatible interfaces otherwise.

Parameters
receiveAddrInfoReceive address information. It is used to bind socket to particular interface if defined and to add membership for multicast if defined.
setNonBlockingSet the socket as non blocking
[out]sockValue pointed is set with the newly created socket.
Returns
SOPC_STATUS_OK if operation succeeded, SOPC_STATUS_INVALID_PARAMETERS in case of incorrect parameters, SOPC_STATUS_NOT_SUPPORTED in case of multicast activation impossibility and SOPC_STATUS_NOK in case of socket error.

◆ SOPC_ETH_Socket_CreateToSend()

SOPC_ReturnStatus SOPC_ETH_Socket_CreateToSend ( SOPC_ETH_Socket_SendAddressInfo sendAddrInfo,
bool  setNonBlocking,
Socket sock 
)

Create a new ETH socket using sendAddrInfo properties.

Parameters
sendAddrInfoSend address information
setNonBlockingSet the socket as non blocking
[out]sockValue pointed is set with the newly created socket.
Returns
SOPC_STATUS_OK if operation succeeded, SOPC_STATUS_INVALID_PARAMETERS in case of incorrect parameters and SOPC_STATUS_NOK in case of socket error.

◆ SOPC_ETH_Socket_SendTo()

SOPC_ReturnStatus SOPC_ETH_Socket_SendTo ( Socket  sock,
const SOPC_ETH_Socket_SendAddressInfo sendAddrInfo,
uint16_t  etherType,
SOPC_Buffer buffer 
)

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

Parameters
sockThe socket used for sending
sendAddrInfoThe address information for sending (source interface and destination MAC address)
etherTypeThe EtherType value to indicate in Ethernet header
bufferThe buffer containing data to be sent. Buffer containing buffer->length bytes.
Returns
SOPC_STATUS_OK if operation succeeded, SOPC_STATUS_OUT_OF_MEMORY in case of allocation failure, SOPC_STATUS_WOULD_BLOCK if not all bytes were sent, SOPC_STATUS_NOK otherwise otherwise.

◆ SOPC_ETH_Socket_ReceiveFrom()

SOPC_ReturnStatus SOPC_ETH_Socket_ReceiveFrom ( Socket  sock,
const SOPC_ETH_Socket_ReceiveAddressInfo receiveAddrInfo,
bool  checkEtherType,
uint16_t  etherType,
SOPC_Buffer buffer 
)

Receive data on the ETH socket from given address info.

Parameters
sockThe socket used for receiving
receiveAddrInfoThe address information to use for reception. Check source address, dest address and protocol version if configured.
checkEtherTypeIf set, check Ethernet header protocol value of packet is etherType.
etherTypeThe expected EtherType value in Ethernet header, packets are ignored if different when checkEtherType is set.
[in,out]bufferThe buffer with buffer->current_size bytes available. buffer->length is updated on reception. The content on reception includes the ethernet header content of 14 bytes:
  • Destination MAC (6 bytes)
  • Source MAC (6 bytes)
  • EtherType
Returns
SOPC_STATUS_OK if operation succeeded, SOPC_STATUS_INVALID_PARAMETERS in case of parameters issue, SOPC_STATUS_OUT_OF_MEMORY in case buffer size might have been insufficient, SOPC_STATUS_NOK otherwise.

◆ SOPC_ETH_Socket_Close()

void SOPC_ETH_Socket_Close ( Socket sock)

Close the socket connection and/or clear the socket.

Parameters
sockThe socket to disconnect and/or clear