MQTTPacketOut.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. /*******************************************************************************
  2. * Copyright (c) 2009, 2024 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)
  146. {
  147. MQTTProperties props = MQTTProperties_initializer;
  148. pack->properties = props;
  149. if (datalen > 2)
  150. {
  151. if (MQTTProperties_read(&pack->properties, &curdata, enddata) != 1)
  152. {
  153. if (pack->properties.array)
  154. free(pack->properties.array);
  155. if (pack)
  156. free(pack);
  157. pack = NULL; /* signal protocol error */
  158. goto exit;
  159. }
  160. }
  161. }
  162. exit:
  163. FUNC_EXIT;
  164. return pack;
  165. }
  166. /**
  167. * Free allocated storage for a connack packet.
  168. * @param pack pointer to the connack packet structure
  169. */
  170. void MQTTPacket_freeConnack(Connack* pack)
  171. {
  172. FUNC_ENTRY;
  173. if (pack->MQTTVersion >= MQTTVERSION_5)
  174. MQTTProperties_free(&pack->properties);
  175. free(pack);
  176. FUNC_EXIT;
  177. }
  178. /**
  179. * Send an MQTT PINGREQ packet down a socket.
  180. * @param socket the open socket to send the data to
  181. * @param clientID the string client identifier, only used for tracing
  182. * @return the completion code (e.g. TCPSOCKET_COMPLETE)
  183. */
  184. int MQTTPacket_send_pingreq(networkHandles* net, const char* clientID)
  185. {
  186. Header header;
  187. int rc = 0;
  188. FUNC_ENTRY;
  189. header.byte = 0;
  190. header.bits.type = PINGREQ;
  191. rc = MQTTPacket_send(net, header, NULL, 0, 0, MQTTVERSION_3_1_1);
  192. Log(LOG_PROTOCOL, 20, NULL, net->socket, clientID, rc);
  193. FUNC_EXIT_RC(rc);
  194. return rc;
  195. }
  196. /**
  197. * Send an MQTT subscribe packet down a socket.
  198. * @param topics list of topics
  199. * @param qoss list of corresponding QoSs
  200. * @param msgid the MQTT message id to use
  201. * @param dup boolean - whether to set the MQTT DUP flag
  202. * @param socket the open socket to send the data to
  203. * @param clientID the string client identifier, only used for tracing
  204. * @return the completion code (e.g. TCPSOCKET_COMPLETE)
  205. */
  206. int MQTTPacket_send_subscribe(List* topics, List* qoss, MQTTSubscribe_options* opts, MQTTProperties* props,
  207. int msgid, int dup, Clients* client)
  208. {
  209. Header header;
  210. char *data, *ptr;
  211. int rc = -1;
  212. ListElement *elem = NULL, *qosElem = NULL;
  213. int datalen, i = 0;
  214. FUNC_ENTRY;
  215. header.bits.type = SUBSCRIBE;
  216. header.bits.dup = dup;
  217. header.bits.qos = 1;
  218. header.bits.retain = 0;
  219. datalen = 2 + topics->count * 3; /* utf length + char qos == 3 */
  220. while (ListNextElement(topics, &elem))
  221. datalen += (int)strlen((char*)(elem->content));
  222. if (client->MQTTVersion >= MQTTVERSION_5)
  223. datalen += MQTTProperties_len(props);
  224. ptr = data = malloc(datalen);
  225. if (ptr == NULL)
  226. goto exit;
  227. writeInt(&ptr, msgid);
  228. if (client->MQTTVersion >= MQTTVERSION_5)
  229. MQTTProperties_write(&ptr, props);
  230. elem = NULL;
  231. while (ListNextElement(topics, &elem))
  232. {
  233. char subopts = 0;
  234. ListNextElement(qoss, &qosElem);
  235. writeUTF(&ptr, (char*)(elem->content));
  236. subopts = *(int*)(qosElem->content);
  237. if (client->MQTTVersion >= MQTTVERSION_5 && opts != NULL)
  238. {
  239. subopts |= (opts[i].noLocal << 2); /* 1 bit */
  240. subopts |= (opts[i].retainAsPublished << 3); /* 1 bit */
  241. subopts |= (opts[i].retainHandling << 4); /* 2 bits */
  242. }
  243. writeChar(&ptr, subopts);
  244. ++i;
  245. }
  246. rc = MQTTPacket_send(&client->net, header, data, datalen, 1, client->MQTTVersion);
  247. Log(LOG_PROTOCOL, 22, NULL, client->net.socket, client->clientID, msgid, rc);
  248. if (rc != TCPSOCKET_INTERRUPTED)
  249. free(data);
  250. exit:
  251. FUNC_EXIT_RC(rc);
  252. return rc;
  253. }
  254. /**
  255. * Function used in the new packets table to create suback packets.
  256. * @param MQTTVersion the version of MQTT
  257. * @param aHeader the MQTT header byte
  258. * @param data the rest of the packet
  259. * @param datalen the length of the rest of the packet
  260. * @return pointer to the packet structure
  261. */
  262. void* MQTTPacket_suback(int MQTTVersion, unsigned char aHeader, char* data, size_t datalen)
  263. {
  264. Suback* pack = NULL;
  265. char* curdata = data;
  266. char* enddata = &data[datalen];
  267. FUNC_ENTRY;
  268. if ((pack = malloc(sizeof(Suback))) == NULL)
  269. goto exit;
  270. pack->MQTTVersion = MQTTVersion;
  271. pack->header.byte = aHeader;
  272. if (enddata - curdata < 2) /* Is there enough data to read the msgid? */
  273. {
  274. free(pack);
  275. pack = NULL;
  276. goto exit;
  277. }
  278. pack->msgId = readInt(&curdata);
  279. if (MQTTVersion >= MQTTVERSION_5)
  280. {
  281. MQTTProperties props = MQTTProperties_initializer;
  282. pack->properties = props;
  283. if (MQTTProperties_read(&pack->properties, &curdata, enddata) != 1)
  284. {
  285. if (pack->properties.array)
  286. free(pack->properties.array);
  287. if (pack)
  288. free(pack);
  289. pack = NULL; /* signal protocol error */
  290. goto exit;
  291. }
  292. }
  293. pack->qoss = ListInitialize();
  294. while ((size_t)(curdata - data) < datalen)
  295. {
  296. unsigned int* newint;
  297. newint = malloc(sizeof(unsigned int));
  298. if (newint == NULL)
  299. {
  300. if (pack->properties.array)
  301. free(pack->properties.array);
  302. if (pack)
  303. free(pack);
  304. pack = NULL; /* signal protocol error */
  305. goto exit;
  306. }
  307. *newint = (unsigned int)readChar(&curdata);
  308. ListAppend(pack->qoss, newint, sizeof(unsigned int));
  309. }
  310. if (pack->qoss->count == 0)
  311. {
  312. if (pack->properties.array)
  313. free(pack->properties.array);
  314. ListFree(pack->qoss);
  315. free(pack);
  316. pack = NULL;
  317. }
  318. exit:
  319. FUNC_EXIT;
  320. return pack;
  321. }
  322. /**
  323. * Send an MQTT unsubscribe packet down a socket.
  324. * @param topics list of topics
  325. * @param msgid the MQTT message id to use
  326. * @param dup boolean - whether to set the MQTT DUP flag
  327. * @param socket the open socket to send the data to
  328. * @param clientID the string client identifier, only used for tracing
  329. * @return the completion code (e.g. TCPSOCKET_COMPLETE)
  330. */
  331. int MQTTPacket_send_unsubscribe(List* topics, MQTTProperties* props, int msgid, int dup, Clients* client)
  332. {
  333. Header header;
  334. char *data, *ptr;
  335. int rc = SOCKET_ERROR;
  336. ListElement *elem = NULL;
  337. int datalen;
  338. FUNC_ENTRY;
  339. header.bits.type = UNSUBSCRIBE;
  340. header.bits.dup = dup;
  341. header.bits.qos = 1;
  342. header.bits.retain = 0;
  343. datalen = 2 + topics->count * 2; /* utf length == 2 */
  344. while (ListNextElement(topics, &elem))
  345. datalen += (int)strlen((char*)(elem->content));
  346. if (client->MQTTVersion >= MQTTVERSION_5)
  347. datalen += MQTTProperties_len(props);
  348. ptr = data = malloc(datalen);
  349. if (ptr == NULL)
  350. goto exit;
  351. writeInt(&ptr, msgid);
  352. if (client->MQTTVersion >= MQTTVERSION_5)
  353. MQTTProperties_write(&ptr, props);
  354. elem = NULL;
  355. while (ListNextElement(topics, &elem))
  356. writeUTF(&ptr, (char*)(elem->content));
  357. rc = MQTTPacket_send(&client->net, header, data, datalen, 1, client->MQTTVersion);
  358. Log(LOG_PROTOCOL, 25, NULL, client->net.socket, client->clientID, msgid, rc);
  359. if (rc != TCPSOCKET_INTERRUPTED)
  360. free(data);
  361. exit:
  362. FUNC_EXIT_RC(rc);
  363. return rc;
  364. }
  365. /**
  366. * Function used in the new packets table to create unsuback packets.
  367. * @param MQTTVersion the version of MQTT
  368. * @param aHeader the MQTT header byte
  369. * @param data the rest of the packet
  370. * @param datalen the length of the rest of the packet
  371. * @return pointer to the packet structure
  372. */
  373. void* MQTTPacket_unsuback(int MQTTVersion, unsigned char aHeader, char* data, size_t datalen)
  374. {
  375. Unsuback* pack = NULL;
  376. char* curdata = data;
  377. char* enddata = &data[datalen];
  378. FUNC_ENTRY;
  379. if ((pack = malloc(sizeof(Unsuback))) == NULL)
  380. goto exit;
  381. pack->MQTTVersion = MQTTVersion;
  382. pack->header.byte = aHeader;
  383. if (enddata - curdata < 2) /* Is there enough data? */
  384. {
  385. free(pack);
  386. pack = NULL;
  387. goto exit;
  388. }
  389. pack->msgId = readInt(&curdata);
  390. pack->reasonCodes = NULL;
  391. if (MQTTVersion >= MQTTVERSION_5)
  392. {
  393. MQTTProperties props = MQTTProperties_initializer;
  394. pack->properties = props;
  395. if (MQTTProperties_read(&pack->properties, &curdata, enddata) != 1)
  396. {
  397. if (pack->properties.array)
  398. free(pack->properties.array);
  399. if (pack)
  400. free(pack);
  401. pack = NULL; /* signal protocol error */
  402. goto exit;
  403. }
  404. pack->reasonCodes = ListInitialize();
  405. while ((size_t)(curdata - data) < datalen)
  406. {
  407. enum MQTTReasonCodes* newrc;
  408. newrc = malloc(sizeof(enum MQTTReasonCodes));
  409. if (newrc == NULL)
  410. {
  411. if (pack->properties.array)
  412. free(pack->properties.array);
  413. if (pack)
  414. free(pack);
  415. pack = NULL; /* signal protocol error */
  416. goto exit;
  417. }
  418. *newrc = (enum MQTTReasonCodes)readChar(&curdata);
  419. ListAppend(pack->reasonCodes, newrc, sizeof(enum MQTTReasonCodes));
  420. }
  421. if (pack->reasonCodes->count == 0)
  422. {
  423. ListFree(pack->reasonCodes);
  424. if (pack->properties.array)
  425. free(pack->properties.array);
  426. if (pack)
  427. free(pack);
  428. pack = NULL;
  429. }
  430. }
  431. exit:
  432. FUNC_EXIT;
  433. return pack;
  434. }