ua_connection.h 5.9 KB

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