MQTTPacketOut.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. /*******************************************************************************
  2. * Copyright (c) 2009, 2021 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, Allan Stockdill-Mander - SSL updates
  16. * Ian Craggs - MQTT 3.1.1 support
  17. * Rong Xiang, Ian Craggs - C++ compatibility
  18. * Ian Craggs - binary password and will payload
  19. * Ian Craggs - MQTT 5.0 support
  20. *******************************************************************************/
  21. /**
  22. * @file
  23. * \brief functions to deal with reading and writing of MQTT packets from and to sockets
  24. *
  25. * Some other related functions are in the MQTTPacket module
  26. */
  27. #include "MQTTPacketOut.h"
  28. #include "Log.h"
  29. #include "StackTrace.h"
  30. #include <string.h>
  31. #include <stdlib.h>
  32. #include "Heap.h"
  33. /**
  34. * Send an MQTT CONNECT packet down a socket for V5 or later
  35. * @param client a structure from which to get all the required values
  36. * @param MQTTVersion the MQTT version to connect with
  37. * @param connectProperties MQTT V5 properties for the connect packet
  38. * @param willProperties MQTT V5 properties for the will message, if any
  39. * @return the completion code (e.g. TCPSOCKET_COMPLETE)
  40. */
  41. int MQTTPacket_send_connect(Clients* client, int MQTTVersion,
  42. MQTTProperties* connectProperties, MQTTProperties* willProperties)
  43. {
  44. char *buf, *ptr;
  45. Connect packet;
  46. int rc = SOCKET_ERROR, len;
  47. FUNC_ENTRY;
  48. packet.header.byte = 0;
  49. packet.header.bits.type = CONNECT;
  50. len = ((MQTTVersion == MQTTVERSION_3_1) ? 12 : 10) + (int)strlen(client->clientID)+2;
  51. if (client->will)
  52. len += (int)strlen(client->will->topic)+2 + client->will->payloadlen+2;
  53. if (client->username)
  54. len += (int)strlen(client->username)+2;
  55. if (client->password)
  56. len += client->passwordlen+2;
  57. if (MQTTVersion >= MQTTVERSION_5)
  58. {
  59. len += MQTTProperties_len(connectProperties);
  60. if (client->will)
  61. len += MQTTProperties_len(willProperties);
  62. }
  63. ptr = buf = malloc(len);
  64. if (ptr == NULL)
  65. goto exit_nofree;
  66. if (MQTTVersion == MQTTVERSION_3_1)
  67. {
  68. writeUTF(&ptr, "MQIsdp");
  69. writeChar(&ptr, (char)MQTTVERSION_3_1);
  70. }
  71. else if (MQTTVersion == MQTTVERSION_3_1_1 || MQTTVersion == MQTTVERSION_5)
  72. {
  73. writeUTF(&ptr, "MQTT");
  74. writeChar(&ptr, (char)MQTTVersion);
  75. }
  76. else
  77. goto exit;
  78. packet.flags.all = 0;
  79. if (MQTTVersion >= MQTTVERSION_5)
  80. packet.flags.bits.cleanstart = client->cleanstart;
  81. else
  82. packet.flags.bits.cleanstart = client->cleansession;
  83. packet.flags.bits.will = (client->will) ? 1 : 0;
  84. if (packet.flags.bits.will)
  85. {
  86. packet.flags.bits.willQoS = client->will->qos;
  87. packet.flags.bits.willRetain = client->will->retained;
  88. }
  89. if (client->username)
  90. packet.flags.bits.username = 1;
  91. if (client->password)
  92. packet.flags.bits.password = 1;
  93. writeChar(&ptr, packet.flags.all);
  94. writeInt(&ptr, client->keepAliveInterval);
  95. if (MQTTVersion >= MQTTVERSION_5)
  96. MQTTProperties_write(&ptr, connectProperties);
  97. writeUTF(&ptr, client->clientID);
  98. if (client->will)
  99. {
  100. if (MQTTVersion >= MQTTVERSION_5)
  101. MQTTProperties_write(&ptr, willProperties);
  102. writeUTF(&ptr, client->will->topic);
  103. writeData(&ptr, client->will->payload, client->will->payloadlen);
  104. }
  105. if (client->username)
  106. writeUTF(&ptr, client->username);
  107. if (client->password)
  108. writeData(&ptr, client->password, client->passwordlen);
  109. rc = MQTTPacket_send(&client->net, packet.header, buf, len, 1, MQTTVersion);
  110. Log(LOG_PROTOCOL, 0, NULL, client->net.socket, client->clientID,
  111. MQTTVersion, client->cleansession, rc);
  112. exit:
  113. if (rc != TCPSOCKET_INTERRUPTED)
  114. free(buf);
  115. exit_nofree:
  116. FUNC_EXIT_RC(rc);
  117. return rc;
  118. }
  119. /**
  120. * Function used in the new packets table to create connack packets.
  121. * @param MQTTVersion MQTT 5 or less?
  122. * @param aHeader the MQTT header byte
  123. * @param data the rest of the packet
  124. * @param datalen the length of the rest of the packet
  125. * @return pointer to the packet structure
  126. */
  127. void* MQTTPacket_connack(int MQTTVersion, unsigned char aHeader, char* data, size_t datalen)
  128. {
  129. Connack* pack = NULL;
  130. char* curdata = data;
  131. char* enddata = &data[datalen];
  132. FUNC_ENTRY;
  133. if ((pack = malloc(sizeof(Connack))) == NULL)
  134. goto exit;
  135. pack->MQTTVersion = MQTTVersion;
  136. pack->header.byte = aHeader;
  137. if (datalen < 2) /* enough data for connect flags and reason code? */
  138. {
  139. free(pack);
  140. pack = NULL;
  141. goto exit;
  142. }
  143. pack->flags.all = readChar(&curdata); /* connect flags */
  144. pack->rc = readChar(&curdata); /* reason code */
  145. if (MQTTVersion >= MQTTVERSION_5 && datalen > 2)
  146. {
  147. MQTTProperties props = MQTTProperties_initializer;
  148. pack->properties = props;
  149. if (MQTTProperties_read(&pack->properties, &curdata, enddata) != 1)
  150. {
  151. if (pack->properties.array)
  152. free(pack->properties.array);
  153. if (pack)
  154. free(pack);
  155. pack = NULL; /* signal protocol error */
  156. goto exit;
  157. }
  158. }
  159. exit:
  160. FUNC_EXIT;
  161. return pack;
  162. }
  163. /**
  164. * Free allocated storage for a connack packet.
  165. * @param pack pointer to the connack packet structure
  166. */
  167. void MQTTPacket_freeConnack(Connack* pack)
  168. {
  169. FUNC_ENTRY;
  170. if (pack->MQTTVersion >= MQTTVERSION_5)
  171. MQTTProperties_free(&pack->properties);
  172. free(pack);
  173. FUNC_EXIT;
  174. }
  175. /**
  176. * Send an MQTT PINGREQ packet down a socket.
  177. * @param socket the open socket to send the data to
  178. * @param clientID the string client identifier, only used for tracing
  179. * @return the completion code (e.g. TCPSOCKET_COMPLETE)
  180. */
  181. int MQTTPacket_send_pingreq(networkHandles* net, const char* clientID)
  182. {
  183. Header header;
  184. int rc = 0;
  185. FUNC_ENTRY;
  186. header.byte = 0;
  187. header.bits.type = PINGREQ;
  188. rc = MQTTPacket_send(net, header, NULL, 0, 0, MQTTVERSION_3_1_1);
  189. Log(LOG_PROTOCOL, 20, NULL, net->socket, clientID, rc);
  190. FUNC_EXIT_RC(rc);
  191. return rc;
  192. }
  193. /**
  194. * Send an MQTT subscribe packet down a socket.
  195. * @param topics list of topics
  196. * @param qoss list of corresponding QoSs
  197. * @param msgid the MQTT message id to use
  198. * @param dup boolean - whether to set the MQTT DUP flag
  199. * @param socket the open socket to send the data to
  200. * @param clientID the string client identifier, only used for tracing
  201. * @return the completion code (e.g. TCPSOCKET_COMPLETE)
  202. */
  203. int MQTTPacket_send_subscribe(List* topics, List* qoss, MQTTSubscribe_options* opts, MQTTProperties* props,
  204. int msgid, int dup, Clients* client)
  205. {
  206. Header header;
  207. char *data, *ptr;
  208. int rc = -1;
  209. ListElement *elem = NULL, *qosElem = NULL;
  210. int datalen, i = 0;
  211. FUNC_ENTRY;
  212. header.bits.type = SUBSCRIBE;
  213. header.bits.dup = dup;
  214. header.bits.qos = 1;
  215. header.bits.retain = 0;
  216. datalen = 2 + topics->count * 3; /* utf length + char qos == 3 */
  217. while (ListNextElement(topics, &elem))
  218. datalen += (int)strlen((char*)(elem->content));
  219. if (client->MQTTVersion >= MQTTVERSION_5)
  220. datalen += MQTTProperties_len(props);
  221. ptr = data = malloc(datalen);
  222. if (ptr == NULL)
  223. goto exit;
  224. writeInt(&ptr, msgid);
  225. if (client->MQTTVersion >= MQTTVERSION_5)
  226. MQTTProperties_write(&ptr, props);
  227. elem = NULL;
  228. while (ListNextElement(topics, &elem))
  229. {
  230. char subopts = 0;
  231. ListNextElement(qoss, &qosElem);
  232. writeUTF(&ptr, (char*)(elem->content));
  233. subopts = *(int*)(qosElem->content);
  234. if (client->MQTTVersion >= MQTTVERSION_5 && opts != NULL)
  235. {
  236. subopts |= (opts[i].noLocal << 2); /* 1 bit */
  237. subopts |= (opts[i].retainAsPublished << 3); /* 1 bit */
  238. subopts |= (opts[i].retainHandling << 4); /* 2 bits */
  239. }
  240. writeChar(&ptr, subopts);
  241. ++i;
  242. }
  243. rc = MQTTPacket_send(&client->net, header, data, datalen, 1, client->MQTTVersion);
  244. Log(LOG_PROTOCOL, 22, NULL, client->net.socket, client->clientID, msgid, rc);
  245. if (rc != TCPSOCKET_INTERRUPTED)
  246. free(data);
  247. exit:
  248. FUNC_EXIT_RC(rc);
  249. return rc;
  250. }
  251. /**
  252. * Function used in the new packets table to create suback packets.
  253. * @param MQTTVersion the version of MQTT
  254. * @param aHeader the MQTT header byte
  255. * @param data the rest of the packet
  256. * @param datalen the length of the rest of the packet
  257. * @return pointer to the packet structure
  258. */
  259. void* MQTTPacket_suback(int MQTTVersion, unsigned char aHeader, char* data, size_t datalen)
  260. {
  261. Suback* pack = NULL;
  262. char* curdata = data;
  263. char* enddata = &data[datalen];
  264. FUNC_ENTRY;
  265. if ((pack = malloc(sizeof(Suback))) == NULL)
  266. goto exit;
  267. pack->MQTTVersion = MQTTVersion;
  268. pack->header.byte = aHeader;
  269. if (enddata - curdata < 2) /* Is there enough data to read the msgid? */
  270. {
  271. free(pack);
  272. pack = NULL;
  273. goto exit;
  274. }
  275. pack->msgId = readInt(&curdata);
  276. if (MQTTVersion >= MQTTVERSION_5)
  277. {
  278. MQTTProperties props = MQTTProperties_initializer;
  279. pack->properties = props;
  280. if (MQTTProperties_read(&pack->properties, &curdata, enddata) != 1)
  281. {
  282. if (pack->properties.array)
  283. free(pack->properties.array);
  284. if (pack)
  285. free(pack);
  286. pack = NULL; /* signal protocol error */
  287. goto exit;
  288. }
  289. }
  290. pack->qoss = ListInitialize();
  291. while ((size_t)(curdata - data) < datalen)
  292. {
  293. unsigned int* newint;
  294. newint = malloc(sizeof(unsigned int));
  295. if (newint == NULL)
  296. {
  297. if (pack->properties.array)
  298. free(pack->properties.array);
  299. if (pack)
  300. free(pack);
  301. pack = NULL; /* signal protocol error */
  302. goto exit;
  303. }
  304. *newint = (unsigned int)readChar(&curdata);
  305. ListAppend(pack->qoss, newint, sizeof(unsigned int));
  306. }
  307. if (pack->qoss->count == 0)
  308. {
  309. if (pack->properties.array)
  310. free(pack->properties.array);
  311. ListFree(pack->qoss);
  312. free(pack);
  313. pack = NULL;
  314. }
  315. exit:
  316. FUNC_EXIT;
  317. return pack;
  318. }
  319. /**
  320. * Send an MQTT unsubscribe packet down a socket.
  321. * @param topics list of topics
  322. * @param msgid the MQTT message id to use
  323. * @param dup boolean - whether to set the MQTT DUP flag
  324. * @param socket the open socket to send the data to
  325. * @param clientID the string client identifier, only used for tracing
  326. * @return the completion code (e.g. TCPSOCKET_COMPLETE)
  327. */
  328. int MQTTPacket_send_unsubscribe(List* topics, MQTTProperties* props, int msgid, int dup, Clients* client)
  329. {
  330. Header header;
  331. char *data, *ptr;
  332. int rc = SOCKET_ERROR;
  333. ListElement *elem = NULL;
  334. int datalen;
  335. FUNC_ENTRY;
  336. header.bits.type = UNSUBSCRIBE;
  337. header.bits.dup = dup;
  338. header.bits.qos = 1;
  339. header.bits.retain = 0;
  340. datalen = 2 + topics->count * 2; /* utf length == 2 */
  341. while (ListNextElement(topics, &elem))
  342. datalen += (int)strlen((char*)(elem->content));
  343. if (client->MQTTVersion >= MQTTVERSION_5)
  344. datalen += MQTTProperties_len(props);
  345. ptr = data = malloc(datalen);
  346. if (ptr == NULL)
  347. goto exit;
  348. writeInt(&ptr, msgid);
  349. if (client->MQTTVersion >= MQTTVERSION_5)
  350. MQTTProperties_write(&ptr, props);
  351. elem = NULL;
  352. while (ListNextElement(topics, &elem))
  353. writeUTF(&ptr, (char*)(elem->content));
  354. rc = MQTTPacket_send(&client->net, header, data, datalen, 1, client->MQTTVersion);
  355. Log(LOG_PROTOCOL, 25, NULL, client->net.socket, client->clientID, msgid, rc);
  356. if (rc != TCPSOCKET_INTERRUPTED)
  357. free(data);
  358. exit:
  359. FUNC_EXIT_RC(rc);
  360. return rc;
  361. }
  362. /**
  363. * Function used in the new packets table to create unsuback packets.
  364. * @param MQTTVersion the version of MQTT
  365. * @param aHeader the MQTT header byte
  366. * @param data the rest of the packet
  367. * @param datalen the length of the rest of the packet
  368. * @return pointer to the packet structure
  369. */
  370. void* MQTTPacket_unsuback(int MQTTVersion, unsigned char aHeader, char* data, size_t datalen)
  371. {
  372. Unsuback* pack = NULL;
  373. char* curdata = data;
  374. char* enddata = &data[datalen];
  375. FUNC_ENTRY;
  376. if ((pack = malloc(sizeof(Unsuback))) == NULL)
  377. goto exit;
  378. pack->MQTTVersion = MQTTVersion;
  379. pack->header.byte = aHeader;
  380. if (enddata - curdata < 2) /* Is there enough data? */
  381. {
  382. free(pack);
  383. pack = NULL;
  384. goto exit;
  385. }
  386. pack->msgId = readInt(&curdata);
  387. pack->reasonCodes = NULL;
  388. if (MQTTVersion >= MQTTVERSION_5)
  389. {
  390. MQTTProperties props = MQTTProperties_initializer;
  391. pack->properties = props;
  392. if (MQTTProperties_read(&pack->properties, &curdata, enddata) != 1)
  393. {
  394. if (pack->properties.array)
  395. free(pack->properties.array);
  396. if (pack)
  397. free(pack);
  398. pack = NULL; /* signal protocol error */
  399. goto exit;
  400. }
  401. pack->reasonCodes = ListInitialize();
  402. while ((size_t)(curdata - data) < datalen)
  403. {
  404. enum MQTTReasonCodes* newrc;
  405. newrc = malloc(sizeof(enum MQTTReasonCodes));
  406. if (newrc == NULL)
  407. {
  408. if (pack->properties.array)
  409. free(pack->properties.array);
  410. if (pack)
  411. free(pack);
  412. pack = NULL; /* signal protocol error */
  413. goto exit;
  414. }
  415. *newrc = (enum MQTTReasonCodes)readChar(&curdata);
  416. ListAppend(pack->reasonCodes, newrc, sizeof(enum MQTTReasonCodes));
  417. }
  418. if (pack->reasonCodes->count == 0)
  419. {
  420. ListFree(pack->reasonCodes);
  421. if (pack->properties.array)
  422. free(pack->properties.array);
  423. if (pack)
  424. free(pack);
  425. pack = NULL;
  426. }
  427. }
  428. exit:
  429. FUNC_EXIT;
  430. return pack;
  431. }