Clients.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*******************************************************************************
  2. * Copyright (c) 2009, 2022 IBM Corp. and Ian Craggs
  3. *
  4. * All rights reserved. This program and the accompanying materials
  5. * are made available under the terms of the Eclipse Public License v2.0
  6. * and Eclipse Distribution License v1.0 which accompany this distribution.
  7. *
  8. * The Eclipse Public License is available at
  9. * https://www.eclipse.org/legal/epl-2.0/
  10. * and the Eclipse Distribution License is available at
  11. * http://www.eclipse.org/org/documents/edl-v10.php.
  12. *
  13. * Contributors:
  14. * Ian Craggs - initial API and implementation and/or initial documentation
  15. * Ian Craggs - add SSL support
  16. * Ian Craggs - fix for bug 413429 - connectionLost not called
  17. * Ian Craggs - change will payload to binary
  18. * Ian Craggs - password to binary
  19. * Ian Craggs - MQTT 5 support
  20. *******************************************************************************/
  21. #if !defined(CLIENTS_H)
  22. #define CLIENTS_H
  23. #include <stdint.h>
  24. #include "MQTTTime.h"
  25. #if defined(_WIN32) || defined(_WIN64)
  26. #include <winsock2.h>
  27. #endif
  28. #if defined(OPENSSL)
  29. #include <openssl/ssl.h>
  30. #endif
  31. #include "MQTTClient.h"
  32. #include "LinkedList.h"
  33. #include "MQTTClientPersistence.h"
  34. #include "Socket.h"
  35. /**
  36. * Stored publication data to minimize copying
  37. */
  38. typedef struct
  39. {
  40. char *topic;
  41. int topiclen;
  42. char* payload;
  43. int payloadlen;
  44. int refcount;
  45. uint8_t mask[4];
  46. } Publications;
  47. /**
  48. * Client publication message data
  49. */
  50. typedef struct
  51. {
  52. int qos;
  53. int retain;
  54. int msgid;
  55. int MQTTVersion;
  56. MQTTProperties properties;
  57. Publications *publish;
  58. START_TIME_TYPE lastTouch; /**> used for retry and expiry */
  59. char nextMessageType; /**> PUBREC, PUBREL, PUBCOMP */
  60. int len; /**> length of the whole structure+data */
  61. } Messages;
  62. /**
  63. * Client will message data
  64. */
  65. typedef struct
  66. {
  67. char *topic;
  68. int payloadlen;
  69. void *payload;
  70. int retained;
  71. int qos;
  72. } willMessages;
  73. typedef struct
  74. {
  75. SOCKET socket;
  76. START_TIME_TYPE lastSent;
  77. START_TIME_TYPE lastReceived;
  78. START_TIME_TYPE lastPing;
  79. #if defined(OPENSSL)
  80. SSL* ssl;
  81. SSL_CTX* ctx;
  82. char *https_proxy;
  83. char *https_proxy_auth;
  84. #endif
  85. char *http_proxy;
  86. char *http_proxy_auth;
  87. int websocket; /**< socket has been upgraded to use web sockets */
  88. char *websocket_key;
  89. const MQTTClient_nameValue* httpHeaders;
  90. } networkHandles;
  91. /* connection states */
  92. /** no connection in progress, see connected value */
  93. #define NOT_IN_PROGRESS 0x0
  94. /** TCP connection in progress */
  95. #define TCP_IN_PROGRESS 0x1
  96. /** SSL connection in progress */
  97. #define SSL_IN_PROGRESS 0x2
  98. /** Websocket connection in progress */
  99. #define WEBSOCKET_IN_PROGRESS 0x3
  100. /** TCP completed, waiting for MQTT ACK */
  101. #define WAIT_FOR_CONNACK 0x4
  102. /** Proxy connection in progress */
  103. #define PROXY_CONNECT_IN_PROGRESS 0x5
  104. /** Disconnecting */
  105. #define DISCONNECTING -2
  106. /**
  107. * Data related to one client
  108. */
  109. typedef struct
  110. {
  111. char* clientID; /**< the string id of the client */
  112. const char* username; /**< MQTT v3.1 user name */
  113. int passwordlen; /**< MQTT password length */
  114. const void* password; /**< MQTT v3.1 binary password */
  115. unsigned int cleansession : 1; /**< MQTT V3 clean session flag */
  116. unsigned int cleanstart : 1; /**< MQTT V5 clean start flag */
  117. unsigned int connected : 1; /**< whether it is currently connected */
  118. unsigned int good : 1; /**< if we have an error on the socket we turn this off */
  119. unsigned int ping_outstanding : 1;
  120. unsigned int ping_due : 1; /**< we couldn't send a ping so we should send one when we can */
  121. signed int connect_state : 4;
  122. START_TIME_TYPE ping_due_time; /**< the time at which the ping should have been sent (ping_due) */
  123. networkHandles net; /**< network info for this client */
  124. int msgID; /**< the MQTT message id */
  125. int keepAliveInterval; /**< the MQTT keep alive interval */
  126. int retryInterval; /**< the MQTT retry interval for QoS > 0 */
  127. int maxInflightMessages; /**< the max number of inflight outbound messages we allow */
  128. willMessages* will; /**< the MQTT will message, if any */
  129. List* inboundMsgs; /**< inbound in flight messages */
  130. List* outboundMsgs; /**< outbound in flight messages */
  131. int connect_count; /**< the number of outbound messages on reconnect - to ensure we send them all */
  132. int connect_sent; /**< the current number of outbound messages on reconnect that we've sent */
  133. List* messageQueue; /**< inbound complete but undelivered messages */
  134. List* outboundQueue; /**< outbound queued messages */
  135. unsigned int qentry_seqno;
  136. void* phandle; /**< the persistence handle */
  137. MQTTClient_persistence* persistence; /**< a persistence implementation */
  138. MQTTPersistence_beforeWrite* beforeWrite; /**< persistence write callback */
  139. MQTTPersistence_afterRead* afterRead; /**< persistence read callback */
  140. void* beforeWrite_context; /**< context to be used with the persistence beforeWrite callbacks */
  141. void* afterRead_context; /**< context to be used with the persistence afterRead callback */
  142. void* context; /**< calling context - used when calling disconnect_internal */
  143. int MQTTVersion; /**< the version of MQTT being used, 3, 4 or 5 */
  144. int sessionExpiry; /**< MQTT 5 session expiry */
  145. char* httpProxy; /**< HTTP proxy */
  146. char* httpsProxy; /**< HTTPS proxy */
  147. #if defined(OPENSSL)
  148. MQTTClient_SSLOptions *sslopts; /**< the SSL/TLS connect options */
  149. SSL_SESSION* session; /**< SSL session pointer for fast handhake */
  150. #endif
  151. } Clients;
  152. int clientIDCompare(void* a, void* b);
  153. int clientSocketCompare(void* a, void* b);
  154. /**
  155. * Configuration data related to all clients
  156. */
  157. typedef struct
  158. {
  159. const char* version;
  160. List* clients;
  161. } ClientStates;
  162. #endif