mosquitto_internal.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /*
  2. Copyright (c) 2010-2020 Roger Light <roger@atchoo.org>
  3. All rights reserved. This program and the accompanying materials
  4. are made available under the terms of the Eclipse Public License 2.0
  5. and Eclipse Distribution License v1.0 which accompany this distribution.
  6. The Eclipse Public License is available at
  7. https://www.eclipse.org/legal/epl-2.0/
  8. and the Eclipse Distribution License is available at
  9. http://www.eclipse.org/org/documents/edl-v10.php.
  10. SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
  11. Contributors:
  12. Roger Light - initial implementation and documentation.
  13. Tatsuzo Osawa - Add epoll.
  14. */
  15. #ifndef MOSQUITTO_INTERNAL_H
  16. #define MOSQUITTO_INTERNAL_H
  17. #include "config.h"
  18. #ifdef WIN32
  19. # include <winsock2.h>
  20. #endif
  21. #ifdef WITH_TLS
  22. # include <openssl/ssl.h>
  23. #else
  24. # include <time.h>
  25. #endif
  26. #include <stdlib.h>
  27. #if defined(WITH_THREADING) && !defined(WITH_BROKER)
  28. # include <pthread.h>
  29. #else
  30. # include <dummypthread.h>
  31. #endif
  32. #ifdef WITH_SRV
  33. # include <ares.h>
  34. #endif
  35. #ifdef WIN32
  36. # if _MSC_VER < 1600
  37. typedef unsigned char uint8_t;
  38. typedef unsigned short uint16_t;
  39. typedef unsigned int uint32_t;
  40. typedef unsigned long long uint64_t;
  41. # else
  42. # include <stdint.h>
  43. # endif
  44. #else
  45. # include <stdint.h>
  46. #endif
  47. #include "mosquitto.h"
  48. #include "time_mosq.h"
  49. #ifdef WITH_BROKER
  50. # ifdef __linux__
  51. # include <netdb.h>
  52. # endif
  53. # include "uthash.h"
  54. struct mosquitto_client_msg;
  55. #endif
  56. #ifdef WIN32
  57. typedef SOCKET mosq_sock_t;
  58. #else
  59. typedef int mosq_sock_t;
  60. #endif
  61. enum mosquitto_msg_direction {
  62. mosq_md_in = 0,
  63. mosq_md_out = 1
  64. };
  65. enum mosquitto_msg_state {
  66. mosq_ms_invalid = 0,
  67. mosq_ms_publish_qos0 = 1,
  68. mosq_ms_publish_qos1 = 2,
  69. mosq_ms_wait_for_puback = 3,
  70. mosq_ms_publish_qos2 = 4,
  71. mosq_ms_wait_for_pubrec = 5,
  72. mosq_ms_resend_pubrel = 6,
  73. mosq_ms_wait_for_pubrel = 7,
  74. mosq_ms_resend_pubcomp = 8,
  75. mosq_ms_wait_for_pubcomp = 9,
  76. mosq_ms_send_pubrec = 10,
  77. mosq_ms_queued = 11
  78. };
  79. enum mosquitto_client_state {
  80. mosq_cs_new = 0,
  81. mosq_cs_connected = 1,
  82. mosq_cs_disconnecting = 2,
  83. mosq_cs_active = 3,
  84. mosq_cs_connect_pending = 4,
  85. mosq_cs_connect_srv = 5,
  86. mosq_cs_disconnect_ws = 6,
  87. mosq_cs_disconnected = 7,
  88. mosq_cs_socks5_new = 8,
  89. mosq_cs_socks5_start = 9,
  90. mosq_cs_socks5_request = 10,
  91. mosq_cs_socks5_reply = 11,
  92. mosq_cs_socks5_auth_ok = 12,
  93. mosq_cs_socks5_userpass_reply = 13,
  94. mosq_cs_socks5_send_userpass = 14,
  95. mosq_cs_expiring = 15,
  96. mosq_cs_duplicate = 17, /* client that has been taken over by another with the same id */
  97. mosq_cs_disconnect_with_will = 18,
  98. mosq_cs_disused = 19, /* client that has been added to the disused list to be freed */
  99. mosq_cs_authenticating = 20, /* Client has sent CONNECT but is still undergoing extended authentication */
  100. mosq_cs_reauthenticating = 21, /* Client is undergoing reauthentication and shouldn't do anything else until complete */
  101. };
  102. enum mosquitto__protocol {
  103. mosq_p_invalid = 0,
  104. mosq_p_mqtt31 = 1,
  105. mosq_p_mqtt311 = 2,
  106. mosq_p_mqtts = 3,
  107. mosq_p_mqtt5 = 5,
  108. };
  109. enum mosquitto__threaded_state {
  110. mosq_ts_none, /* No threads in use */
  111. mosq_ts_self, /* Threads started by libmosquitto */
  112. mosq_ts_external /* Threads started by external code */
  113. };
  114. enum mosquitto__transport {
  115. mosq_t_invalid = 0,
  116. mosq_t_tcp = 1,
  117. mosq_t_ws = 2,
  118. mosq_t_sctp = 3
  119. };
  120. struct mosquitto__alias{
  121. char *topic;
  122. uint16_t alias;
  123. };
  124. struct session_expiry_list {
  125. struct mosquitto *context;
  126. struct session_expiry_list *prev;
  127. struct session_expiry_list *next;
  128. };
  129. struct mosquitto__packet{
  130. uint8_t *payload;
  131. struct mosquitto__packet *next;
  132. uint32_t remaining_mult;
  133. uint32_t remaining_length;
  134. uint32_t packet_length;
  135. uint32_t to_process;
  136. uint32_t pos;
  137. uint16_t mid;
  138. uint8_t command;
  139. int8_t remaining_count;
  140. };
  141. struct mosquitto_message_all{
  142. struct mosquitto_message_all *next;
  143. struct mosquitto_message_all *prev;
  144. mosquitto_property *properties;
  145. time_t timestamp;
  146. enum mosquitto_msg_state state;
  147. bool dup;
  148. struct mosquitto_message msg;
  149. uint32_t expiry_interval;
  150. };
  151. #ifdef WITH_TLS
  152. enum mosquitto__keyform {
  153. mosq_k_pem = 0,
  154. mosq_k_engine = 1,
  155. };
  156. #endif
  157. struct will_delay_list {
  158. struct mosquitto *context;
  159. struct will_delay_list *prev;
  160. struct will_delay_list *next;
  161. };
  162. struct mosquitto_msg_data{
  163. #ifdef WITH_BROKER
  164. struct mosquitto_client_msg *inflight;
  165. struct mosquitto_client_msg *queued;
  166. long inflight_bytes;
  167. long inflight_bytes12;
  168. int inflight_count;
  169. int inflight_count12;
  170. long queued_bytes;
  171. long queued_bytes12;
  172. int queued_count;
  173. int queued_count12;
  174. #else
  175. struct mosquitto_message_all *inflight;
  176. int queue_len;
  177. # ifdef WITH_THREADING
  178. pthread_mutex_t mutex;
  179. # endif
  180. #endif
  181. int inflight_quota;
  182. uint16_t inflight_maximum;
  183. };
  184. struct mosquitto {
  185. #if defined(WITH_BROKER) && defined(WITH_EPOLL)
  186. /* This *must* be the first element in the struct. */
  187. int ident;
  188. #endif
  189. mosq_sock_t sock;
  190. #ifndef WITH_BROKER
  191. mosq_sock_t sockpairR, sockpairW;
  192. #endif
  193. uint32_t maximum_packet_size;
  194. #if defined(__GLIBC__) && defined(WITH_ADNS)
  195. struct gaicb *adns; /* For getaddrinfo_a */
  196. #endif
  197. enum mosquitto__protocol protocol;
  198. char *address;
  199. char *id;
  200. char *username;
  201. char *password;
  202. uint16_t keepalive;
  203. uint16_t last_mid;
  204. enum mosquitto_client_state state;
  205. time_t last_msg_in;
  206. time_t next_msg_out;
  207. time_t ping_t;
  208. struct mosquitto__packet in_packet;
  209. struct mosquitto__packet *current_out_packet;
  210. struct mosquitto__packet *out_packet;
  211. struct mosquitto_message_all *will;
  212. struct mosquitto__alias *aliases;
  213. struct will_delay_list *will_delay_entry;
  214. int alias_count;
  215. int out_packet_count;
  216. uint32_t will_delay_interval;
  217. time_t will_delay_time;
  218. #ifdef WITH_TLS
  219. SSL *ssl;
  220. SSL_CTX *ssl_ctx;
  221. #ifndef WITH_BROKER
  222. SSL_CTX *user_ssl_ctx;
  223. #endif
  224. char *tls_cafile;
  225. char *tls_capath;
  226. char *tls_certfile;
  227. char *tls_keyfile;
  228. int (*tls_pw_callback)(char *buf, int size, int rwflag, void *userdata);
  229. char *tls_version;
  230. char *tls_ciphers;
  231. char *tls_psk;
  232. char *tls_psk_identity;
  233. char *tls_engine;
  234. char *tls_engine_kpass_sha1;
  235. char *tls_alpn;
  236. int tls_cert_reqs;
  237. bool tls_insecure;
  238. bool ssl_ctx_defaults;
  239. bool tls_ocsp_required;
  240. bool tls_use_os_certs;
  241. enum mosquitto__keyform tls_keyform;
  242. #endif
  243. bool want_write;
  244. bool want_connect;
  245. #if defined(WITH_THREADING) && !defined(WITH_BROKER)
  246. pthread_mutex_t callback_mutex;
  247. pthread_mutex_t log_callback_mutex;
  248. pthread_mutex_t msgtime_mutex;
  249. pthread_mutex_t out_packet_mutex;
  250. pthread_mutex_t current_out_packet_mutex;
  251. pthread_mutex_t state_mutex;
  252. pthread_mutex_t mid_mutex;
  253. pthread_t thread_id;
  254. #endif
  255. bool clean_start;
  256. time_t session_expiry_time;
  257. uint32_t session_expiry_interval;
  258. #ifdef WITH_BROKER
  259. bool in_by_id;
  260. bool is_dropping;
  261. bool is_bridge;
  262. struct mosquitto__bridge *bridge;
  263. struct mosquitto_msg_data msgs_in;
  264. struct mosquitto_msg_data msgs_out;
  265. struct mosquitto__acl_user *acl_list;
  266. struct mosquitto__listener *listener;
  267. struct mosquitto__packet *out_packet_last;
  268. struct mosquitto__client_sub **subs;
  269. char *auth_method;
  270. int sub_count;
  271. # ifndef WITH_EPOLL
  272. int pollfd_index;
  273. # endif
  274. # ifdef WITH_WEBSOCKETS
  275. struct lws *wsi;
  276. # endif
  277. bool ws_want_write;
  278. bool assigned_id;
  279. #else
  280. # ifdef WITH_SOCKS
  281. char *socks5_host;
  282. uint16_t socks5_port;
  283. char *socks5_username;
  284. char *socks5_password;
  285. # endif
  286. void *userdata;
  287. bool in_callback;
  288. struct mosquitto_msg_data msgs_in;
  289. struct mosquitto_msg_data msgs_out;
  290. void (*on_connect)(struct mosquitto *, void *userdata, int rc);
  291. void (*on_connect_with_flags)(struct mosquitto *, void *userdata, int rc, int flags);
  292. void (*on_connect_v5)(struct mosquitto *, void *userdata, int rc, int flags, const mosquitto_property *props);
  293. void (*on_disconnect)(struct mosquitto *, void *userdata, int rc);
  294. void (*on_disconnect_v5)(struct mosquitto *, void *userdata, int rc, const mosquitto_property *props);
  295. void (*on_publish)(struct mosquitto *, void *userdata, int mid);
  296. void (*on_publish_v5)(struct mosquitto *, void *userdata, int mid, int reason_code, const mosquitto_property *props);
  297. void (*on_message)(struct mosquitto *, void *userdata, const struct mosquitto_message *message);
  298. void (*on_message_v5)(struct mosquitto *, void *userdata, const struct mosquitto_message *message, const mosquitto_property *props);
  299. void (*on_subscribe)(struct mosquitto *, void *userdata, int mid, int qos_count, const int *granted_qos);
  300. void (*on_subscribe_v5)(struct mosquitto *, void *userdata, int mid, int qos_count, const int *granted_qos, const mosquitto_property *props);
  301. void (*on_unsubscribe)(struct mosquitto *, void *userdata, int mid);
  302. void (*on_unsubscribe_v5)(struct mosquitto *, void *userdata, int mid, const mosquitto_property *props);
  303. void (*on_log)(struct mosquitto *, void *userdata, int level, const char *str);
  304. /*void (*on_error)();*/
  305. char *host;
  306. uint16_t port;
  307. char *bind_address;
  308. unsigned int reconnects;
  309. unsigned int reconnect_delay;
  310. unsigned int reconnect_delay_max;
  311. bool reconnect_exponential_backoff;
  312. char threaded;
  313. struct mosquitto__packet *out_packet_last;
  314. mosquitto_property *connect_properties;
  315. # ifdef WITH_SRV
  316. ares_channel achan;
  317. # endif
  318. #endif
  319. uint8_t max_qos;
  320. uint8_t retain_available;
  321. bool tcp_nodelay;
  322. #ifdef WITH_BROKER
  323. UT_hash_handle hh_id;
  324. UT_hash_handle hh_sock;
  325. struct mosquitto *for_free_next;
  326. struct session_expiry_list *expiry_list_item;
  327. uint16_t remote_port;
  328. #endif
  329. uint32_t events;
  330. };
  331. #define STREMPTY(str) (str[0] == '\0')
  332. void do_client_disconnect(struct mosquitto *mosq, int reason_code, const mosquitto_property *properties);
  333. #endif