MQTTConnectClient.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*******************************************************************************
  2. * Copyright (c) 2014 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
  15. *documentation
  16. *******************************************************************************/
  17. #include "MQTTPacket.h"
  18. #include "StackTrace.h"
  19. #include <string.h>
  20. /**
  21. * Determines the length of the MQTT connect packet that would be produced
  22. * using the supplied connect options.
  23. * @param options the options to be used to build the connect packet
  24. * @return the length of buffer needed to contain the serialized version of
  25. * the packet
  26. */
  27. int MQTTSerialize_connectLength(MQTTPacket_connectData* options) {
  28. int len = 0;
  29. FUNC_ENTRY;
  30. if (options->MQTTVersion == 3)
  31. len = 12; /* variable depending on MQTT or MQIsdp */
  32. else if (options->MQTTVersion == 4)
  33. len = 10;
  34. len += MQTTstrlen(options->clientID) + 2;
  35. if (options->willFlag)
  36. len += MQTTstrlen(options->will.topicName) + 2 +
  37. MQTTstrlen(options->will.message) + 2;
  38. if (options->username.cstring || options->username.lenstring.data)
  39. len += MQTTstrlen(options->username) + 2;
  40. if (options->password.cstring || options->password.lenstring.data)
  41. len += MQTTstrlen(options->password) + 2;
  42. FUNC_EXIT_RC(len);
  43. return len;
  44. }
  45. /**
  46. * Serializes the connect options into the buffer.
  47. * @param buf the buffer into which the packet will be serialized
  48. * @param len the length in bytes of the supplied buffer
  49. * @param options the options to be used to build the connect packet
  50. * @return serialized length, or error if 0
  51. */
  52. int MQTTSerialize_connect(unsigned char* buf,
  53. int buflen,
  54. MQTTPacket_connectData* options) {
  55. unsigned char* ptr = buf;
  56. MQTTHeader header = {0};
  57. MQTTConnectFlags flags = {0};
  58. int len = 0;
  59. int rc = -1;
  60. FUNC_ENTRY;
  61. if (MQTTPacket_len(len = MQTTSerialize_connectLength(options)) > buflen) {
  62. rc = MQTTPACKET_BUFFER_TOO_SHORT;
  63. goto exit;
  64. }
  65. header.byte = 0;
  66. header.bits.type = CONNECT;
  67. writeChar(&ptr, header.byte); /* write header */
  68. ptr += MQTTPacket_encode(ptr, len); /* write remaining length */
  69. if (options->MQTTVersion == 4) {
  70. writeCString(&ptr, "MQTT");
  71. writeChar(&ptr, (char)4);
  72. } else {
  73. writeCString(&ptr, "MQIsdp");
  74. writeChar(&ptr, (char)3);
  75. }
  76. flags.all = 0;
  77. flags.bits.cleansession = options->cleansession;
  78. flags.bits.will = (options->willFlag) ? 1 : 0;
  79. if (flags.bits.will) {
  80. flags.bits.willQoS = options->will.qos;
  81. flags.bits.willRetain = options->will.retained;
  82. }
  83. if (options->username.cstring || options->username.lenstring.data)
  84. flags.bits.username = 1;
  85. if (options->password.cstring || options->password.lenstring.data)
  86. flags.bits.password = 1;
  87. writeChar(&ptr, flags.all);
  88. writeInt(&ptr, options->keepAliveInterval);
  89. writeMQTTString(&ptr, options->clientID);
  90. if (options->willFlag) {
  91. writeMQTTString(&ptr, options->will.topicName);
  92. writeMQTTString(&ptr, options->will.message);
  93. }
  94. if (flags.bits.username)
  95. writeMQTTString(&ptr, options->username);
  96. if (flags.bits.password)
  97. writeMQTTString(&ptr, options->password);
  98. rc = ptr - buf;
  99. exit:
  100. FUNC_EXIT_RC(rc);
  101. return rc;
  102. }
  103. /**
  104. * Deserializes the supplied (wire) buffer into connack data - return code
  105. * @param sessionPresent the session present flag returned (only for MQTT 3.1.1)
  106. * @param connack_rc returned integer value of the connack return code
  107. * @param buf the raw buffer data, of the correct length determined by the
  108. * remaining length field
  109. * @param len the length in bytes of the data in the supplied buffer
  110. * @return error code. 1 is success, 0 is failure
  111. */
  112. int MQTTDeserialize_connack(unsigned char* sessionPresent,
  113. unsigned char* connack_rc,
  114. unsigned char* buf,
  115. int buflen) {
  116. MQTTHeader header = {0};
  117. unsigned char* curdata = buf;
  118. unsigned char* enddata = NULL;
  119. int rc = 0;
  120. int mylen;
  121. MQTTConnackFlags flags = {0};
  122. FUNC_ENTRY;
  123. header.byte = readChar(&curdata);
  124. if (header.bits.type != CONNACK)
  125. goto exit;
  126. curdata += (rc = MQTTPacket_decodeBuf(curdata,
  127. &mylen)); /* read remaining length */
  128. enddata = curdata + mylen;
  129. if (enddata - curdata < 2)
  130. goto exit;
  131. flags.all = readChar(&curdata);
  132. *sessionPresent = flags.bits.sessionpresent;
  133. *connack_rc = readChar(&curdata);
  134. rc = 1;
  135. exit:
  136. FUNC_EXIT_RC(rc);
  137. return rc;
  138. }
  139. /**
  140. * Serializes a 0-length packet into the supplied buffer, ready for writing to a
  141. * socket
  142. * @param buf the buffer into which the packet will be serialized
  143. * @param buflen the length in bytes of the supplied buffer, to avoid overruns
  144. * @param packettype the message type
  145. * @return serialized length, or error if 0
  146. */
  147. int MQTTSerialize_zero(unsigned char* buf,
  148. int buflen,
  149. unsigned char packettype) {
  150. MQTTHeader header = {0};
  151. int rc = -1;
  152. unsigned char* ptr = buf;
  153. FUNC_ENTRY;
  154. if (buflen < 2) {
  155. rc = MQTTPACKET_BUFFER_TOO_SHORT;
  156. goto exit;
  157. }
  158. header.byte = 0;
  159. header.bits.type = packettype;
  160. writeChar(&ptr, header.byte); /* write header */
  161. ptr += MQTTPacket_encode(ptr, 0); /* write remaining length */
  162. rc = ptr - buf;
  163. exit:
  164. FUNC_EXIT_RC(rc);
  165. return rc;
  166. }
  167. /**
  168. * Serializes a disconnect packet into the supplied buffer, ready for writing to
  169. * a socket
  170. * @param buf the buffer into which the packet will be serialized
  171. * @param buflen the length in bytes of the supplied buffer, to avoid overruns
  172. * @return serialized length, or error if 0
  173. */
  174. int MQTTSerialize_disconnect(unsigned char* buf, int buflen) {
  175. return MQTTSerialize_zero(buf, buflen, DISCONNECT);
  176. }
  177. /**
  178. * Serializes a disconnect packet into the supplied buffer, ready for writing to
  179. * a socket
  180. * @param buf the buffer into which the packet will be serialized
  181. * @param buflen the length in bytes of the supplied buffer, to avoid overruns
  182. * @return serialized length, or error if 0
  183. */
  184. int MQTTSerialize_pingreq(unsigned char* buf, int buflen) {
  185. return MQTTSerialize_zero(buf, buflen, PINGREQ);
  186. }