MQTTConnectClient.c 6.2 KB

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