ua_connection.h 5.7 KB

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