ua_connection.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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_CONNECTION_H_
  5. #define UA_CONNECTION_H_
  6. #ifdef __cplusplus
  7. extern "C" {
  8. #endif
  9. #include "ua_types.h"
  10. /**
  11. * Networking
  12. * ----------
  13. * Client-server connection is represented by a `UA_Connection` structure. In
  14. * order to allow for different operating systems and connection types. For
  15. * this, `UA_Connection` stores a pointer to user-defined data and
  16. * function-pointers to interact with the underlying networking implementation.
  17. *
  18. * An example networklayer for TCP communication is contained in the plugins
  19. * folder. The networklayer forwards messages with `UA_Connection` structures to
  20. * the main open62541 library. The library can then return messages vie TCP
  21. * without being aware of the underlying transport technology.
  22. *
  23. * Connection Config
  24. * ^^^^^^^^^^^^^^^^^ */
  25. typedef struct {
  26. UA_UInt32 protocolVersion;
  27. UA_UInt32 sendBufferSize;
  28. UA_UInt32 recvBufferSize;
  29. UA_UInt32 maxMessageSize;
  30. UA_UInt32 maxChunkCount;
  31. } UA_ConnectionConfig;
  32. extern const UA_EXPORT UA_ConnectionConfig UA_ConnectionConfig_standard;
  33. /**
  34. * Connection Structure
  35. * ^^^^^^^^^^^^^^^^^^^^ */
  36. typedef enum {
  37. UA_CONNECTION_OPENING, /* The socket is open, but the HEL/ACK handshake
  38. is not done */
  39. UA_CONNECTION_ESTABLISHED, /* The socket is open and the connection
  40. configured */
  41. UA_CONNECTION_CLOSED, /* The socket has been closed and the connection
  42. will be deleted */
  43. } UA_ConnectionState;
  44. /* Forward declarations */
  45. struct UA_Connection;
  46. typedef struct UA_Connection UA_Connection;
  47. struct UA_SecureChannel;
  48. typedef struct UA_SecureChannel UA_SecureChannel;
  49. struct UA_Connection {
  50. UA_ConnectionState state;
  51. UA_ConnectionConfig localConf;
  52. UA_ConnectionConfig remoteConf;
  53. UA_SecureChannel *channel; /* The securechannel that is attached to
  54. this connection */
  55. UA_Int32 sockfd; /* Most connectivity solutions run on
  56. sockets. Having the socket id here
  57. simplifies the design. */
  58. void *handle; /* A pointer to internal data */
  59. UA_ByteString incompleteMessage; /* A half-received message (TCP is a
  60. streaming protocol) is stored here */
  61. /* Get a buffer for sending */
  62. UA_StatusCode (*getSendBuffer)(UA_Connection *connection, size_t length,
  63. UA_ByteString *buf);
  64. /* Release the send buffer manually */
  65. void (*releaseSendBuffer)(UA_Connection *connection, UA_ByteString *buf);
  66. /* Sends a message over the connection. The message buffer is always freed,
  67. * even if sending fails.
  68. *
  69. * @param connection The connection
  70. * @param buf The message buffer
  71. * @return Returns an error code or UA_STATUSCODE_GOOD. */
  72. UA_StatusCode (*send)(UA_Connection *connection, UA_ByteString *buf);
  73. /* Receive a message from the remote connection
  74. *
  75. * @param connection The connection
  76. * @param response The response string. It is allocated by the connection
  77. * and needs to be freed with connection->releaseBuffer
  78. * @param timeout Timeout of the recv operation in milliseconds
  79. * @return Returns UA_STATUSCODE_BADCOMMUNICATIONERROR if the recv operation
  80. * can be repeated, UA_STATUSCODE_GOOD if it succeeded and
  81. * UA_STATUSCODE_BADCONNECTIONCLOSED if the connection was
  82. * closed. */
  83. UA_StatusCode (*recv)(UA_Connection *connection, UA_ByteString *response,
  84. UA_UInt32 timeout);
  85. /* Release the buffer of a received message */
  86. void (*releaseRecvBuffer)(UA_Connection *connection, UA_ByteString *buf);
  87. /* Close the connection */
  88. void (*close)(UA_Connection *connection);
  89. };
  90. void UA_EXPORT UA_Connection_deleteMembers(UA_Connection *connection);
  91. /**
  92. * EndpointURL Helper
  93. * ^^^^^^^^^^^^^^^^^^ */
  94. /* Split the given endpoint url into hostname and port
  95. * @param endpointUrl The endpoint URL to split up
  96. * @param hostname the target array for hostname. Has to be at least 256 size.
  97. * If an IPv6 address is given, hostname contains e.g.
  98. * '[2001:0db8:85a3::8a2e:0370:7334]'
  99. * @param port set to the port of the url or 0
  100. * @param path pointing to the end of given endpointUrl or to NULL if no
  101. * path given. The starting '/' is NOT included in path
  102. * @return UA_STATUSCODE_BADOUTOFRANGE if url too long,
  103. * UA_STATUSCODE_BADATTRIBUTEIDINVALID if url not starting with
  104. * 'opc.tcp://', UA_STATUSCODE_GOOD on success
  105. */
  106. UA_StatusCode UA_EXPORT
  107. UA_EndpointUrl_split(const char *endpointUrl, char *hostname,
  108. UA_UInt16 * port, const char ** path);
  109. /* Convert given byte string to a positive number. Returns the number of valid
  110. * digits. Stops if a non-digit char is found and returns the number of digits
  111. * up to that point. */
  112. size_t UA_EXPORT
  113. UA_readNumber(UA_Byte *buf, size_t buflen, UA_UInt32 *number);
  114. #ifdef __cplusplus
  115. } // extern "C"
  116. #endif
  117. #endif /* UA_CONNECTION_H_ */