ua_plugin_network.h 9.5 KB

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