• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*******************************************************************************
2  * Copyright (c) 2009, 2018 IBM Corp.
3  *
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * and Eclipse Distribution License v1.0 which accompany this distribution.
7  *
8  * The Eclipse Public License is available at
9  *    http://www.eclipse.org/legal/epl-v10.html
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 
22 #if !defined(CLIENTS_H)
23 #define CLIENTS_H
24 
25 #include <time.h>
26 #if defined(OPENSSL)
27 #if defined(WIN32) || defined(WIN64)
28 #include <winsock2.h>
29 #endif
30 #include <openssl/ssl.h>
31 #endif
32 #if defined(MBEDTLS)
33 #include <mbedtls/ctr_drbg.h>
34 #include <mbedtls/entropy.h>
35 #include <mbedtls/ssl.h>
36 #endif
37 
38 #include "MQTTClient.h"
39 #include "LinkedList.h"
40 #include "MQTTClientPersistence.h"
41 
42 
43 /**
44  * Stored publication data to minimize copying
45  */
46 typedef struct
47 {
48 	char *topic;
49 	int topiclen;
50 	char* payload;
51 	int payloadlen;
52 	int refcount;
53 } Publications;
54 
55 /**
56  * Client publication message data
57  */
58 typedef struct
59 {
60 	int qos;
61 	int retain;
62 	int msgid;
63 	int MQTTVersion;
64 	MQTTProperties properties;
65 	Publications *publish;
66 	time_t lastTouch;		/**> used for retry and expiry */
67 	char nextMessageType;	/**> PUBREC, PUBREL, PUBCOMP */
68 	int len;				/**> length of the whole structure+data */
69 } Messages;
70 
71 /**
72  * Client will message data
73  */
74 typedef struct
75 {
76 	char *topic;
77 	int payloadlen;
78 	void *payload;
79 	int retained;
80 	int qos;
81 } willMessages;
82 
83 #if defined(MBEDTLS)
84 typedef struct
85 {
86 	mbedtls_ssl_config conf;
87 	mbedtls_entropy_context entropy;
88 	mbedtls_ctr_drbg_context ctr_drbg;
89 	mbedtls_x509_crt cacert;
90 	mbedtls_x509_crt clicert;
91 	mbedtls_pk_context pkey;
92 }SSL_CTX;
93 
94 typedef mbedtls_ssl_context SSL;
95 typedef mbedtls_ssl_session SSL_SESSION;
96 
97 #endif
98 typedef struct
99 {
100 	int socket;
101 	time_t lastSent;
102 	time_t lastReceived;
103 	time_t lastPing;
104 #if defined(OPENSSL) || defined(MBEDTLS)
105 	SSL* ssl;
106 	SSL_CTX* ctx;
107 #endif
108 	int websocket; /**< socket has been upgraded to use web sockets */
109 	char *websocket_key;
110 } networkHandles;
111 
112 
113 /* connection states */
114 /** no connection in progress, see connected value */
115 #define NOT_IN_PROGRESS  0x0
116 /** TCP connection in progress */
117 #define TCP_IN_PROGRESS  0x1
118 /** SSL connection in progress */
119 #define SSL_IN_PROGRESS  0x2
120 /** Websocket connection in progress */
121 #define WEBSOCKET_IN_PROGRESS   0x3
122 /** TCP completed, waiting for MQTT ACK */
123 #define WAIT_FOR_CONNACK 0x4
124 /** Disconnecting */
125 #define DISCONNECTING    -2
126 
127 /**
128  * Data related to one client
129  */
130 typedef struct
131 {
132 	char* clientID;					      /**< the string id of the client */
133 	const char* username;					/**< MQTT v3.1 user name */
134 	int passwordlen;              /**< MQTT password length */
135 	const void* password;					/**< MQTT v3.1 binary password */
136 	unsigned int cleansession : 1;	/**< MQTT V3 clean session flag */
137 	unsigned int cleanstart : 1;		/**< MQTT V5 clean start flag */
138 	unsigned int connected : 1;		/**< whether it is currently connected */
139 	unsigned int good : 1; 			  /**< if we have an error on the socket we turn this off */
140 	unsigned int ping_outstanding : 1;
141 	signed int connect_state : 4;
142 	networkHandles net;
143 	int msgID;
144 	int keepAliveInterval;
145 	int retryInterval;
146 	int maxInflightMessages;
147 	willMessages* will;
148 	List* inboundMsgs;
149 	List* outboundMsgs;				/**< in flight */
150 	List* messageQueue;
151 	unsigned int qentry_seqno;
152 	void* phandle;  /* the persistence handle */
153 	MQTTClient_persistence* persistence; /* a persistence implementation */
154 	void* context; /* calling context - used when calling disconnect_internal */
155 	int MQTTVersion;
156 	int sessionExpiry; /**< MQTT 5 session expiry */
157 #if defined(OPENSSL) || defined(MBEDTLS)
158 	MQTTClient_SSLOptions *sslopts;
159 	SSL_SESSION* session;    /***< SSL session pointer for fast handhake */
160 #endif
161 } Clients;
162 
163 int clientIDCompare(void* a, void* b);
164 int clientSocketCompare(void* a, void* b);
165 
166 /**
167  * Configuration data related to all clients
168  */
169 typedef struct
170 {
171 	const char* version;
172 	List* clients;
173 } ClientStates;
174 
175 #endif
176