ua_plugin_network.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. #ifndef UA_PLUGIN_NETWORK_H_
  5. #define UA_PLUGIN_NETWORK_H_
  6. #ifdef __cplusplus
  7. extern "C" {
  8. #endif
  9. #include "ua_server.h"
  10. /* Forward declarations */
  11. struct UA_Connection;
  12. typedef struct UA_Connection UA_Connection;
  13. struct UA_SecureChannel;
  14. typedef struct UA_SecureChannel UA_SecureChannel;
  15. struct UA_ServerNetworkLayer;
  16. typedef struct UA_ServerNetworkLayer UA_ServerNetworkLayer;
  17. /**
  18. * .. _networking:
  19. *
  20. * Networking Plugin API
  21. * =====================
  22. *
  23. * Connection
  24. * ----------
  25. * Client-server connections are represented by a `UA_Connection`. The
  26. * connection is stateful and stores partially received messages, and so on. In
  27. * addition, the connection contains function pointers to the underlying
  28. * networking implementation. An example for this is the `send` function. So the
  29. * connection encapsulates all the required networking functionality. This lets
  30. * users on embedded (or otherwise exotic) systems implement their own
  31. * networking plugins with a clear interface to the main open62541 library. */
  32. typedef struct {
  33. UA_UInt32 protocolVersion;
  34. UA_UInt32 sendBufferSize;
  35. UA_UInt32 recvBufferSize;
  36. UA_UInt32 maxMessageSize;
  37. UA_UInt32 maxChunkCount;
  38. } UA_ConnectionConfig;
  39. typedef enum {
  40. UA_CONNECTION_OPENING, /* The socket is open, but the HEL/ACK handshake
  41. * is not done */
  42. UA_CONNECTION_ESTABLISHED, /* The socket is open and the connection
  43. * configured */
  44. UA_CONNECTION_CLOSED /* The socket has been closed and the connection
  45. * will be deleted */
  46. } UA_ConnectionState;
  47. struct UA_Connection {
  48. UA_ConnectionState state;
  49. UA_ConnectionConfig localConf;
  50. UA_ConnectionConfig remoteConf;
  51. UA_SecureChannel *channel; /* The securechannel that is attached to
  52. * this connection */
  53. UA_Int32 sockfd; /* Most connectivity solutions run on
  54. * sockets. Having the socket id here
  55. * simplifies the design. */
  56. void *handle; /* A pointer to internal data */
  57. UA_ByteString incompleteMessage; /* A half-received message (TCP is a
  58. * streaming protocol) is stored here */
  59. /* Get a buffer for sending */
  60. UA_StatusCode (*getSendBuffer)(UA_Connection *connection, size_t length,
  61. UA_ByteString *buf);
  62. /* Release the send buffer manually */
  63. void (*releaseSendBuffer)(UA_Connection *connection, UA_ByteString *buf);
  64. /* Sends a message over the connection. The message buffer is always freed,
  65. * even if sending fails.
  66. *
  67. * @param connection The connection
  68. * @param buf The message buffer
  69. * @return Returns an error code or UA_STATUSCODE_GOOD. */
  70. UA_StatusCode (*send)(UA_Connection *connection, UA_ByteString *buf);
  71. /* Receive a message from the remote connection
  72. *
  73. * @param connection The connection
  74. * @param response The response string. It is allocated by the connection
  75. * and needs to be freed with connection->releaseBuffer
  76. * @param timeout Timeout of the recv operation in milliseconds
  77. * @return Returns UA_STATUSCODE_BADCOMMUNICATIONERROR if the recv operation
  78. * can be repeated, UA_STATUSCODE_GOOD if it succeeded and
  79. * UA_STATUSCODE_BADCONNECTIONCLOSED if the connection was
  80. * closed. */
  81. UA_StatusCode (*recv)(UA_Connection *connection, UA_ByteString *response,
  82. UA_UInt32 timeout);
  83. /* Release the buffer of a received message */
  84. void (*releaseRecvBuffer)(UA_Connection *connection, UA_ByteString *buf);
  85. /* Close the connection. The network layer closes the socket. This is picked
  86. * up during the next 'listen' and the connection is freed in the network
  87. * layer. */
  88. void (*close)(UA_Connection *connection);
  89. /* To be called only from within the server (and not the network layer).
  90. * Frees up the connection's memory. */
  91. void (*free)(UA_Connection *connection);
  92. };
  93. /* Cleans up half-received messages, and so on. Called from connection->free. */
  94. void UA_EXPORT
  95. UA_Connection_deleteMembers(UA_Connection *connection);
  96. /**
  97. * Server Network Layer
  98. * --------------------
  99. * The server exposes two functions to interact with remote clients:
  100. * `processBinaryMessage` and `removeConnection`. These functions are called by
  101. * the server network layer.
  102. *
  103. * It is the job of the server network layer to listen on a TCP socket, to
  104. * accept new connections, to call the server with received messages and to
  105. * signal closed connections to the server.
  106. *
  107. * The network layer is part of the server config. So users can provide a custom
  108. * implementation if the provided example does not fit their architecture. The
  109. * network layer is invoked only from the server's main loop. So the network
  110. * layer does not need to be thread-safe. If the networklayer receives a
  111. * positive duration for blocking listening, the server's main loop will block
  112. * until a message is received or the duration times out. */
  113. /* Process a binary message (TCP packet). The message can contain partial
  114. * chunks. (TCP is a streaming protocol and packets may be split/merge during
  115. * transport.) After processing, the message is freed with
  116. * connection->releaseRecvBuffer. */
  117. void UA_EXPORT
  118. UA_Server_processBinaryMessage(UA_Server *server, UA_Connection *connection,
  119. UA_ByteString *message);
  120. /* The server internally cleans up the connection and then calls
  121. * connection->free. */
  122. void UA_EXPORT
  123. UA_Server_removeConnection(UA_Server *server, UA_Connection *connection);
  124. struct UA_ServerNetworkLayer {
  125. void *handle; /* Internal data */
  126. UA_String discoveryUrl;
  127. /* Start listening on the networklayer.
  128. *
  129. * @param nl The network layer
  130. * @return Returns UA_STATUSCODE_GOOD or an error code. */
  131. UA_StatusCode (*start)(UA_ServerNetworkLayer *nl);
  132. /* Listen for new and closed connections and arriving packets. Calls
  133. * UA_Server_processBinaryMessage for the arriving packets. Closed
  134. * connections are picked up here and forwarded to
  135. * UA_Server_removeConnection where they are cleaned up and freed.
  136. *
  137. * @param nl The network layer
  138. * @param server The server for processing the incoming packets and for
  139. * closing connections.
  140. * @param timeout The timeout during which an event must arrive in
  141. * milliseconds
  142. * @return A statuscode for the status of the network layer. */
  143. UA_StatusCode (*listen)(UA_ServerNetworkLayer *nl, UA_Server *server,
  144. UA_UInt16 timeout);
  145. /* Close the network socket and all open connections. Afterwards, the
  146. * network layer can be safely deleted.
  147. *
  148. * @param nl The network layer
  149. * @param server The server that processes the incoming packets and for
  150. * closing connections before deleting them.
  151. * @return A statuscode for the status of the closing operation. */
  152. void (*stop)(UA_ServerNetworkLayer *nl, UA_Server *server);
  153. /* Deletes the network layer context. Call only after stopping. */
  154. void (*deleteMembers)(UA_ServerNetworkLayer *nl);
  155. };
  156. /**
  157. * Client Network Layer
  158. * --------------------
  159. * The client has only a single connection. The connection is used for both
  160. * sending and receiving binary messages.
  161. * @param localConf the connection config for this client
  162. * @param endpointUrl to where to connect
  163. * @param timeout in ms until the connection try times out if remote not reachable
  164. * */
  165. typedef UA_Connection
  166. (*UA_ConnectClientConnection)(UA_ConnectionConfig localConf, const char *endpointUrl, const UA_UInt32 timeout);
  167. /**
  168. * Endpoint URL Parser
  169. * -------------------
  170. * The endpoint URL parser is generally useful for the implementation of network
  171. * layer plugins. */
  172. /* Split the given endpoint url into hostname, port and path. All arguments must
  173. * be non-NULL. EndpointUrls have the form "opc.tcp://hostname:port/path", port
  174. * and path may be omitted (together with the prefix colon and slash).
  175. *
  176. * @param endpointUrl The endpoint URL.
  177. * @param outHostname Set to the parsed hostname. The string points into the
  178. * original endpointUrl, so no memory is allocated. If an IPv6 address is
  179. * given, hostname contains e.g. '[2001:0db8:85a3::8a2e:0370:7334]'
  180. * @param outPort Set to the port of the url or left unchanged.
  181. * @param outPath Set to the path if one is present in the endpointUrl.
  182. * Starting or trailing '/' are NOT included in the path. The string
  183. * points into the original endpointUrl, so no memory is allocated.
  184. * @return Returns UA_STATUSCODE_BADTCPENDPOINTURLINVALID if parsing failed. */
  185. UA_StatusCode UA_EXPORT
  186. UA_parseEndpointUrl(const UA_String *endpointUrl, UA_String *outHostname,
  187. UA_UInt16 *outPort, UA_String *outPath);
  188. #ifdef __cplusplus
  189. } // extern "C"
  190. #endif
  191. #endif /* UA_PLUGIN_NETWORK_H_ */