High Performance OPC UA Server SDK  1.1.0.158
network

Modules

 Operation Control Flags
 Bitmask values used by ua_socket_op.
 
 Operation Initialization
 

Data Structures

struct  ua_socket_op
 Structure representing an asynchronous IO operation. More...
 
struct  ua_socket
 Internal structure of an async socket. More...
 
struct  ua_net_base
 Network base object. More...
 
struct  ua_ipaddress
 Storage for an IP address. More...
 
struct  ua_net_addr
 Structure for storing a network/IP address. More...
 

Macros

#define UA_NET_CCV
 calling convention
 
#define UA_NET_API
 API definition.
 
#define UA_NET_E_GOOD   0
 Result codes. More...
 
#define UA_NET_E_ASYNC   1
 Operation will complete asynchronously. More...
 
#define UA_NET_E_PARTIAL   2
 Operation completed partially (completion pending)(support optional). More...
 
#define UA_NET_E_BAD   -1
 Operation or call failed (unspecific or unexpected reason). More...
 
#define UA_NET_E_BADDISCONNECT   -2
 Connection is closed. More...
 
#define UA_NET_E_BADSHUTDOWN   -3
 Peer shutdown connection in required direction. More...
 
#define UA_NET_E_BADINVALIDARGUMENT   -4
 Function called with invalid arguments. More...
 
#define UA_NET_E_BADNOMEM   -5
 Operation failed because of not enough memory. More...
 
#define UA_NET_E_BADNOTIMPL   -6
 Function not implemented. More...
 
#define UA_NET_E_BADHOSTUNKNOWN   -7
 Connect failed because host is not known or unreacheable. More...
 
#define UA_NET_E_BADSYSCALL   -8
 Underlying system call failed. More...
 
#define UA_NET_E_BADOPCANCELLED   -9
 Operation has been cancelled before completion. More...
 
#define UA_NET_E_BADOPTIMEOUT   -10
 Operation timed out. More...
 
#define UA_NET_E_BADIOPENDING   -11
 Operation cannot be started because a prior operation is still pending. More...
 
#define UA_NET_E_BADINVALIDSTATE   -12
 An object is in an invalid state for the operation. More...
 
#define UA_NET_E_BADWOULDBLOCK   -13
 Operation would cause the thread to block. More...
 
#define UA_NET_E_BADNOTSUPPORTED   -14
 Operation is not supported. More...
 
#define UA_NET_E_BADTOOSMALL   -15
 A given destination was too small. More...
 
#define UA_NET_E_BADHOSTUNREACHABLE   -16
 Connect to given address failed. More...
 
#define UA_NET_E_BADSOCKETCLOSED   -17
 Operation interrupted because socket has been closed. More...
 
#define UA_NET_E_BADUNEXPECTED   -18
 Unexpected error. More...
 
#define UA_NET_E_BADNOTFOUND   -19
 Requested element not found. More...
 
#define UA_NET_EVENT_NULL   0x00000000
 Placeholder event type. More...
 
#define UA_NET_EVENT_CLOSE_COMPLETE   0x00000001
 These events should be handled in ua_socket_cb. More...
 
#define UA_NET_EVENT_TIMEOUT   0x00000002
 No communication during the configured timespan. More...
 

Typedefs

typedef void( ua_net_resolve_callback )(int iResult, int iNoOfAddresses, struct ua_net_addr *pAddresses, void *pvCallbackData)
 Receive addresses after completion of asynchronous address resolution. More...
 
typedef int( ua_socket_cb )(struct ua_socket *pSocket, unsigned int iEventType, int iResult, void *pvCallbackData)
 Signature of a socket callback. More...
 
typedef int( ua_socket_op_cb )(struct ua_socket *pSocket, struct ua_socket_op *pOperation, int iResult)
 Signature of an operation callback. More...
 

Enumerations

enum  ua_net_af { ua_net_af_none = 0, ua_net_af_ipv4 = 1, ua_net_af_ipv6 = 2, ua_net_af_any = 3 }
 Identifiers for supported network address families. More...
 
enum  ua_socket_type { ua_socket_type_none = 0, ua_socket_type_stream, ua_socket_type_dgram }
 Socket types for ua_socket_init call. More...
 
enum  ua_net_state {
  UA_NET_STATE_NONE = 0, UA_NET_STATE_LISTEN = 1, UA_NET_STATE_CONNECTING = 2, UA_NET_STATE_ACCEPTING = 3,
  UA_NET_STATE_SHUTDOWN_SEND = 4, UA_NET_STATE_SHUTDOWN_READ = 5, UA_NET_STATE_ESTABLISHED = 6, UA_NET_STATE_CLOSING = 7,
  UA_NET_STATE_ABORTING = 8, UA_NET_STATE_UDP
}
 Internal socket state. More...
 

Functions

UA_NET_API int UA_NET_CCV ua_net_resolve (struct ua_net_base *pNetBase, const char *sHost, int *piNoOfAddresses, struct ua_net_addr *pAddresses, ua_net_resolve_callback *pfCallback, void *pvCallbackData)
 Resolve the hostname and store the results in the given array. More...
 
UA_NET_API int ua_ipaddress_set (struct ua_ipaddress *ip, const unsigned char *addr, unsigned char len)
 Assigns the given IP address in ip. More...
 
UA_NET_API int UA_NET_CCV ua_net_formataddr (struct ua_net_addr *pAddr, size_t iBufLen, char *pBuf)
 Convert the address in binary form into its string representation. More...
 
UA_NET_API int UA_NET_CCV ua_net_getaddr (const char *sIP, struct ua_net_addr *pAddr)
 Converts numeric address into binary format and determins address family. More...
 
UA_NET_API int UA_NET_CCV ua_net_makeanyaddr (struct ua_net_addr *pAddr)
 Set given address to ANY_IP for use in bind. More...
 
UA_NET_API int UA_NET_CCV ua_net_init (void)
 Initialize network library. More...
 
UA_NET_API int UA_NET_CCV ua_net_clear (void)
 Cleanup network library. More...
 
UA_NET_API int UA_NET_CCV ua_socket_init (struct ua_net_base *pNetworkBase, struct ua_socket *pSocket, enum ua_net_af AddressFamily, enum ua_socket_type SocketType, ua_socket_cb *pfCallback, void *pvCallbackData)
 Create an usable struct ua_socket within the given memory and assigns to base object. More...
 
UA_NET_API int UA_NET_CCV ua_socket_initset (struct ua_net_base *pNetworkBase, struct ua_socket *pSocket, UA_NET_P_SOCKET hSocket, ua_socket_cb *pfCallback, void *pvCallbackData)
 Create an usable struct ua_socket within the given memory and assigns to base object. More...
 
UA_NET_API int UA_NET_CCV ua_socket_set_reuse_address (struct ua_socket *pSocket)
 Set SO_REUSEADDR as socket option for the given socket. More...
 
UA_NET_API int UA_NET_CCV ua_socket_get_peer_addr (struct ua_socket *pSocket, struct ua_net_addr *pAddress)
 Get information about the peer address through a connected socket. More...
 
UA_NET_API void UA_NET_CCV ua_socket_set_timeout (struct ua_socket *pSocket, int iTimeOutMS)
 Set timeout value of a socket (<= 0 to deactivate). More...
 
UA_NET_API unsigned int UA_NET_CCV ua_net_get_last_error (struct ua_net_base *pNetworkBase)
 Retrieve last socket error. More...
 
UA_NET_API int UA_NET_CCV ua_socket_bind (struct ua_socket *pSocket, struct ua_net_addr *pAddress)
 Bind socket to given address and port. More...
 
UA_NET_API int UA_NET_CCV ua_socket_listen (struct ua_socket *pSocket)
 Put socket into listen mode. More...
 
UA_NET_API int UA_NET_CCV ua_socket_accept (struct ua_socket *pListenSocket, struct ua_socket *pAcceptSocket, struct ua_socket_op *pAcceptOperation)
 Start asynchronous accept and notify completion through operation object. More...
 
UA_NET_API int UA_NET_CCV ua_socket_connect (struct ua_socket *pSocket, struct ua_net_addr *pServerAddress, struct ua_net_addr *pLocalAddress, struct ua_socket_op *pConnectOperation)
 Start asynchronous connect and notify completion through operation object. More...
 
UA_NET_API int UA_NET_CCV ua_socket_write (struct ua_socket *pSocket, struct ua_socket_op *pSendOperation)
 Start asynchronous write operation and notify completion through operation object. More...
 
UA_NET_API int UA_NET_CCV ua_socket_read (struct ua_socket *pSocket, struct ua_socket_op *pReadOperation)
 Start asynchronous read operation and notify completion through operation object. More...
 
UA_NET_API int UA_NET_CCV ua_socket_sendto (struct ua_socket *pSocket, struct ua_socket_op *pOperation, const struct ua_net_addr *pDestAddr)
 Starts an asynchronous sendto operation and notify completion through operation object. More...
 
UA_NET_API int UA_NET_CCV ua_socket_recvfrom (struct ua_socket *pSocket, struct ua_socket_op *pOperation)
 Starts an asynchronous recvfrom operation and notify completion through operation object. More...
 
UA_NET_API void UA_NET_CCV ua_socket_shutdown (struct ua_socket *pSocket)
 Shutdown connection in send direction. More...
 
UA_NET_API int UA_NET_CCV ua_socket_close (struct ua_socket *pSocket)
 Close socket, remove from base and clear memory. More...
 
UA_NET_API unsigned int UA_NET_CCV ua_net_base_size (unsigned int num_sockets)
 Calculate the size of the data to be passed to ua_net_base_init. More...
 
UA_NET_API int UA_NET_CCV ua_net_base_init (struct ua_net_base *pNetworkBase, void *data, unsigned int num_sockets)
 Initialize base object. More...
 
UA_NET_API void *UA_NET_CCV ua_net_base_clear (struct ua_net_base *pNetworkBase)
 Clear base object. More...
 
UA_NET_API int UA_NET_CCV ua_net_do_events (struct ua_net_base *pNetworkBase, int iTimeOutMSec)
 Process events of sockets organized by a network base object. More...
 

Detailed Description

Macro Definition Documentation

#define UA_NET_E_ASYNC   1

Operation will complete asynchronously.

#define UA_NET_E_BAD   -1

Operation or call failed (unspecific or unexpected reason).

#define UA_NET_E_BADDISCONNECT   -2

Connection is closed.

#define UA_NET_E_BADHOSTUNKNOWN   -7

Connect failed because host is not known or unreacheable.

#define UA_NET_E_BADHOSTUNREACHABLE   -16

Connect to given address failed.

#define UA_NET_E_BADINVALIDARGUMENT   -4

Function called with invalid arguments.

#define UA_NET_E_BADINVALIDSTATE   -12

An object is in an invalid state for the operation.

#define UA_NET_E_BADIOPENDING   -11

Operation cannot be started because a prior operation is still pending.

#define UA_NET_E_BADNOMEM   -5

Operation failed because of not enough memory.

#define UA_NET_E_BADNOTFOUND   -19

Requested element not found.

#define UA_NET_E_BADNOTIMPL   -6

Function not implemented.

#define UA_NET_E_BADNOTSUPPORTED   -14

Operation is not supported.

#define UA_NET_E_BADOPCANCELLED   -9

Operation has been cancelled before completion.

#define UA_NET_E_BADOPTIMEOUT   -10

Operation timed out.

#define UA_NET_E_BADSHUTDOWN   -3

Peer shutdown connection in required direction.

#define UA_NET_E_BADSOCKETCLOSED   -17

Operation interrupted because socket has been closed.

#define UA_NET_E_BADSYSCALL   -8

Underlying system call failed.

#define UA_NET_E_BADTOOSMALL   -15

A given destination was too small.

#define UA_NET_E_BADUNEXPECTED   -18

Unexpected error.

#define UA_NET_E_BADWOULDBLOCK   -13

Operation would cause the thread to block.

#define UA_NET_E_GOOD   0

Result codes.

Operation completed successfully.

#define UA_NET_E_PARTIAL   2

Operation completed partially (completion pending)(support optional).

#define UA_NET_EVENT_CLOSE_COMPLETE   0x00000001

These events should be handled in ua_socket_cb.

The connection was closed locally.

#define UA_NET_EVENT_NULL   0x00000000

Placeholder event type.

#define UA_NET_EVENT_TIMEOUT   0x00000002

No communication during the configured timespan.

Typedef Documentation

typedef void( ua_net_resolve_callback)(int iResult, int iNoOfAddresses, struct ua_net_addr *pAddresses, void *pvCallbackData)

Receive addresses after completion of asynchronous address resolution.

Only called if ua_net_resolve returned UA_NET_E_ASYNC.

Parameters
iResultResult of the operation.
iNoOfAddressesNumber of used slots in address array.
pAddressesFilled array passed into resolve function.
pvCallbackDataUser data passed into resolve function.
typedef int( ua_socket_cb)(struct ua_socket *pSocket, unsigned int iEventType, int iResult, void *pvCallbackData)

Signature of a socket callback.

Parameters
pSocketThe socket object on which the event occured.
iEventTypeUA_NET_EVENT_*
iResultUA_NET_E_* result assigned to the event.
pvCallbackDataThe user data specified at socket initialization.
typedef int( ua_socket_op_cb)(struct ua_socket *pSocket, struct ua_socket_op *pOperation, int iResult)

Signature of an operation callback.

Parameters
pSocketThe socket object to which the operation was assigned to.
pOperationThe structure defining the completed operation.
iResultUA_NET_E_* describing wether the operation succeeded or failed.

Enumeration Type Documentation

enum ua_net_af

Identifiers for supported network address families.

Internal socket state.

Socket types for ua_socket_init call.

Enumerator
ua_socket_type_stream 

TCP.

ua_socket_type_dgram 

UDP.

Function Documentation

UA_NET_API int ua_ipaddress_set ( struct ua_ipaddress ip,
const unsigned char *  addr,
unsigned char  len 
)

Assigns the given IP address in ip.

Parameters
ipdestination to assign to
addrpointer to byte array which contains the IP address (e.g. from sockaddr_in.sin_addr or sockaddr_in6.sin6_addr.
lenlength in bytes. 4 for IPv4, 16 for IPv6.
Returns
Zero on success, -1 otherwise.
UA_NET_API void* UA_NET_CCV ua_net_base_clear ( struct ua_net_base pNetworkBase)

Clear base object.

Parameters
pNetworkBaseThe network base object to be cleared.
Returns
UA_NET_E_* error code
UA_NET_API int UA_NET_CCV ua_net_base_init ( struct ua_net_base pNetworkBase,
void *  data,
unsigned int  num_sockets 
)

Initialize base object.

Parameters
pNetworkBaseThe network base object to be initialized.
dataPreallocated data of size calculated by ua_net_base_size.
num_socketsNumber of sockets for the network base, must be the same as when calculating the data size.
Returns
UA_NET_E_* error code
UA_NET_API unsigned int UA_NET_CCV ua_net_base_size ( unsigned int  num_sockets)

Calculate the size of the data to be passed to ua_net_base_init.

Parameters
num_socketsNumber of sockets for the base to initialize.
Returns
Number of bytes to allocate as data.
UA_NET_API int UA_NET_CCV ua_net_clear ( void  )

Cleanup network library.

Must be the last call to this API.

Returns
UA_NET_E_* error code
UA_NET_API int UA_NET_CCV ua_net_do_events ( struct ua_net_base pNetworkBase,
int  iTimeOutMSec 
)

Process events of sockets organized by a network base object.

Parameters
pNetworkBaseThe network base object.
iTimeOutMSecMaximum wait time.
Returns
UA_NET_E_* error code
UA_NET_API int UA_NET_CCV ua_net_formataddr ( struct ua_net_addr a_pAddr,
size_t  a_BufLen,
char *  a_pBuf 
)

Convert the address in binary form into its string representation.

The string is stored in the buffer which must be large enough to store the address plus the zero terminator.(ie. IPv4 address has 15 characters). The resulting string will be zero terminated. If the buffer is too small the resulting string may be truncated.

Parameters
a_pAddrAddress storage with the address to be converted.
a_BufLenSize of the destination buffer.
a_pBufDestination buffer.
Returns
UA_NET_E_* error code
UA_NET_API unsigned int UA_NET_CCV ua_net_get_last_error ( struct ua_net_base pNetworkBase)

Retrieve last socket error.

Parameters
pNetworkBaseThe network base object.
Returns
UA_NET_E_* error code
UA_NET_API int UA_NET_CCV ua_net_getaddr ( const char *  a_sIP,
struct ua_net_addr a_pAddr 
)

Converts numeric address into binary format and determins address family.

Uses getaddrinfo when IPv6 support is enabled to get automatic format check. Without IPv6 support inet_addr is used for backward compatibility. This function is deprecated but the newer alternative inet_pton() is problably only available on platforms with IPv6 support anyway. This function is not meant to resolve hostnames! Use ua_net_resolve() instead.

Parameters
a_sAddrThe IP address in string form.
a_pAddrTarget for storing the address information.
Returns
Number of characters printed.
UA_NET_API int UA_NET_CCV ua_net_init ( void  )

Initialize network library.

Must be called first.

Returns
UA_NET_E_* error code
UA_NET_API int UA_NET_CCV ua_net_makeanyaddr ( struct ua_net_addr a_pAddr)

Set given address to ANY_IP for use in bind.

Parameters
a_pAddrAddress storage with the address to be set to ANY_IP.
Returns
UA_NET_E_* error code
UA_NET_API int UA_NET_CCV ua_net_resolve ( struct ua_net_base a_pNetBase,
const char *  a_sHost,
int *  a_piNoOfAddrs,
struct ua_net_addr a_pAddrs,
ua_net_resolve_callback a_pfCallback,
void *  a_pvCallbackData 
)

Resolve the hostname and store the results in the given array.

Depending on the implementation the process may complete immediatly or asynchronously by invoking the given callback. The function only returns as many addresses as are requested in a_piNoOfAddrs. There might be more available. The function returns UA_NET_E_GOOD if the operation completed synchronously. The callback won't be invoked in this case. UA_NET_E_ASYNC is returned if the result will be reported in the callback function. If no callback function is given in this case the function returns UA_NET_E_BADWOULDBLOCK. If the result is reported asynchronously the destination array has be be valid until the callback function has been called.

Parameters
a_pNetBaseNetwork base which sockets are regularly polled for events.
a_sHostString containing the hostname to be resolved.
a_piNoOfAddrsSize of the a_pAddrs array. Contains number of used slots on sync completion.
a_pAddrsDestination for storing the resolved addresses.
a_pfCallbackFunction for reporting async completion.
a_pvCallbackDataUser data passed to callback function.
Returns
UA_NET_E_* error code.
UA_NET_API int UA_NET_CCV ua_socket_accept ( struct ua_socket pListenSocket,
struct ua_socket pAcceptSocket,
struct ua_socket_op pAcceptOperation 
)

Start asynchronous accept and notify completion through operation object.

Parameters
pListenSocketSocket which has been put into listening mode.
pAcceptSocketSocket which will represent the accepted connection. Handle must be empty (use ua_socket_initset with UA_NET_P_SOCKET_INVALID).
pAcceptOperationOperation object which will be notified when a client is accepted.
Returns
UA_NET_E_* error code
UA_NET_API int UA_NET_CCV ua_socket_bind ( struct ua_socket pSocket,
struct ua_net_addr pAddress 
)

Bind socket to given address and port.

Parameters
pSocketThe socket object.
pAddressThe address information for binding the socket.
Returns
UA_NET_E_* error code
UA_NET_API int UA_NET_CCV ua_socket_close ( struct ua_socket pSocket)

Close socket, remove from base and clear memory.

Parameters
pSocketThe socket object.
Returns
UA_NET_E_* error code
UA_NET_API int UA_NET_CCV ua_socket_connect ( struct ua_socket pSocket,
struct ua_net_addr pServerAddress,
struct ua_net_addr pLocalAddress,
struct ua_socket_op pConnectOperation 
)

Start asynchronous connect and notify completion through operation object.

Parameters
pSocketThe socket object.
pServerAddressAddress information of the host.
uPortPort number at the host.
pLocalAddressBind the client socket to this local address.
pConnectOperationInitialized async operation structure.
Returns
UA_NET_E_* error code
UA_NET_API int UA_NET_CCV ua_socket_get_peer_addr ( struct ua_socket pSocket,
struct ua_net_addr pAddress 
)

Get information about the peer address through a connected socket.

Parameters
pSocketThe socket object.
pAddressThe address information of the peer.
Returns
UA_NET_E_* error code
UA_NET_API int UA_NET_CCV ua_socket_init ( struct ua_net_base pNetworkBase,
struct ua_socket pSocket,
enum ua_net_af  AddressFamily,
enum ua_socket_type  SocketType,
ua_socket_cb pfCallback,
void *  pvCallbackData 
)

Create an usable struct ua_socket within the given memory and assigns to base object.

Creates new socket handle based on address family and type.

Parameters
pNetworkBaseThe network base object
pSocketThe socket object.
AddressFamilyThe address family of the socket to be created.
SocketTypeType of the socket to be created.
pfCallbackFunction which will be called if non-operation events occur on the socket.
pvCallbackDataUser data passed back to the callback function.
Returns
UA_NET_E_* error code
UA_NET_API int UA_NET_CCV ua_socket_initset ( struct ua_net_base pNetworkBase,
struct ua_socket pSocket,
UA_NET_P_SOCKET  hSocket,
ua_socket_cb pfCallback,
void *  pvCallbackData 
)

Create an usable struct ua_socket within the given memory and assigns to base object.

Uses given system socket handle instead of creating a new one. Socket is set to non-blocking.

Parameters
pNetworkBaseThe network base object
pSocketThe socket object.
hSocketA platform dependent socket or file descriptor which will be assigned to the socket.
pfCallbackFunction which will be called if non-operation events occur on the socket.
pvCallbackDataUser data passed back to the callback function.
Returns
UA_NET_E_* error code
UA_NET_API int UA_NET_CCV ua_socket_listen ( struct ua_socket pSocket)

Put socket into listen mode.

Parameters
pSocketThe socket object.
pAddressThe address information for binding the socket.
Returns
UA_NET_E_* error code
UA_NET_API int UA_NET_CCV ua_socket_read ( struct ua_socket pSocket,
struct ua_socket_op pReadOperation 
)

Start asynchronous read operation and notify completion through operation object.

Parameters
pSocketThe socket object.
pReadOperationInitialized operation structure.
Returns
UA_NET_E_* error code
UA_NET_API int UA_NET_CCV ua_socket_recvfrom ( struct ua_socket pSocket,
struct ua_socket_op pOperation 
)

Starts an asynchronous recvfrom operation and notify completion through operation object.

Note that the operation object contains the sender address once the operation is completed.

Parameters
pSocketThe socket object.
pOperationInitialized operation structure.
Returns
UA_NET_E_* error code
UA_NET_API int UA_NET_CCV ua_socket_sendto ( struct ua_socket pSocket,
struct ua_socket_op pOperation,
const struct ua_net_addr pDestAddr 
)

Starts an asynchronous sendto operation and notify completion through operation object.

Parameters
pSocketThe socket object.
pOperationInitialized operation structure.
pDestAddrDestination address for this UDP packet.
Returns
UA_NET_E_* error code
UA_NET_API int UA_NET_CCV ua_socket_set_reuse_address ( struct ua_socket pSocket)

Set SO_REUSEADDR as socket option for the given socket.

Parameters
pSocketThe socket object.
Returns
UA_NET_E_* error code
UA_NET_API void UA_NET_CCV ua_socket_set_timeout ( struct ua_socket pSocket,
int  iTimeOutMS 
)

Set timeout value of a socket (<= 0 to deactivate).

Parameters
pSocket
iTimeOutMS
UA_NET_API void UA_NET_CCV ua_socket_shutdown ( struct ua_socket pSocket)

Shutdown connection in send direction.

Parameters
pSocketThe socket object.
UA_NET_API int UA_NET_CCV ua_socket_write ( struct ua_socket pSocket,
struct ua_socket_op pSendOperation 
)

Start asynchronous write operation and notify completion through operation object.

Parameters
pSocketThe socket object.
pSendOperationInitialized operation structure.
Returns
UA_NET_E_* error code