client-mqtt-handshake.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * libwebsockets - small server side websockets and web server implementation
  3. *
  4. * Copyright (C) 2010 - 2020 Andy Green <[email protected]>
  5. * Sakthi Kannan <[email protected]>
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to
  9. * deal in the Software without restriction, including without limitation the
  10. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  11. * sell copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  22. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  23. * IN THE SOFTWARE.
  24. */
  25. #include <private-lib-core.h>
  26. #define MQTT_CONNECT_MSG_BASE_LEN (12)
  27. struct lws *
  28. lws_mqtt_client_send_connect(struct lws *wsi)
  29. {
  30. /* static int */
  31. /* lws_mqttc_abs_writeable(lws_abs_protocol_inst_t *api, size_t budget) */
  32. const lws_mqttc_t *c = &wsi->mqtt->client;
  33. uint8_t b[256 + LWS_PRE], *start = b + LWS_PRE, *p = start,
  34. len = MQTT_CONNECT_MSG_BASE_LEN;
  35. switch (lwsi_state(wsi)) {
  36. case LRS_MQTTC_IDLE:
  37. /*
  38. * Transport connected - this is our chance to do the
  39. * protocol connect action.
  40. */
  41. /* 1. Fixed Headers */
  42. if (lws_mqtt_fill_fixed_header(p++, LMQCP_CTOS_CONNECT, 0, 0, 0)) {
  43. lwsl_err("%s: Failled to fill fixed header\n", __func__);
  44. return NULL;
  45. }
  46. /*
  47. * 2. Remaining length - Add the lengths of client ID,
  48. * username and password and their length fields if
  49. * the respective flags are set.
  50. */
  51. len += c->id->len;
  52. if (c->conn_flags & LMQCFT_USERNAME && c->username) {
  53. len += c->username->len + 2;
  54. if (c->conn_flags & LMQCFT_PASSWORD)
  55. len += (c->password ? c->password->len : 0) + 2;
  56. }
  57. if (c->conn_flags & LMQCFT_WILL_FLAG && c->will.topic) {
  58. len += c->will.topic->len + 2;
  59. len += (c->will.message ? c->will.message->len : 0) + 2;
  60. }
  61. p += lws_mqtt_vbi_encode(len, p);
  62. /*
  63. * 3. Variable Header - Protocol name & level, Connect
  64. * flags and keep alive time (in secs).
  65. */
  66. lws_ser_wu16be(p, 4); /* Length of protocol name */
  67. p += 2;
  68. *p++ = 'M';
  69. *p++ = 'Q';
  70. *p++ = 'T';
  71. *p++ = 'T';
  72. *p++ = MQTT_VER_3_1_1;
  73. *p++ = c->conn_flags;
  74. lws_ser_wu16be(p, c->keep_alive_secs);
  75. p += 2;
  76. /*
  77. * 4. Payload - Client ID, Will topic & message,
  78. * Username & password.
  79. */
  80. if (lws_mqtt_str_is_not_empty(c->id)) {
  81. lws_ser_wu16be(p, c->id->len);
  82. p += 2;
  83. memcpy(p, c->id->buf, c->id->len);
  84. p += c->id->len;
  85. } else {
  86. /*
  87. * If the Client supplies a zero-byte
  88. * ClientId, the Client MUST also set
  89. * CleanSession to 1 [MQTT-3.1.3-7].
  90. */
  91. if (!(c->conn_flags & LMQCFT_CLEAN_START)) {
  92. lwsl_err("%s: Empty client ID needs a clean start\n",
  93. __func__);
  94. return NULL;
  95. }
  96. *p++ = 0;
  97. }
  98. if ((c->conn_flags & ~LMQCFT_CLEAN_START) == 0) {
  99. *p++ = 0; /* no properties */
  100. break;
  101. }
  102. if (c->conn_flags & LMQCFT_WILL_FLAG) {
  103. if (lws_mqtt_str_is_not_empty(c->will.topic)) {
  104. lws_ser_wu16be(p, c->will.topic->len);
  105. p += 2;
  106. memcpy(p, c->will.topic->buf, c->will.topic->len);
  107. p += c->will.topic->len;
  108. if (lws_mqtt_str_is_not_empty(c->will.message)) {
  109. lws_ser_wu16be(p, c->will.topic->len);
  110. p += 2;
  111. memcpy(p, c->will.message->buf,
  112. c->will.message->len);
  113. p += c->will.message->len;
  114. } else {
  115. lws_ser_wu16be(p, 0);
  116. p += 2;
  117. }
  118. } else {
  119. lwsl_err("%s: Missing Will Topic\n", __func__);
  120. return NULL;
  121. }
  122. }
  123. if (c->conn_flags & LMQCFT_USERNAME) {
  124. /*
  125. * Detailed sanity check on the username and
  126. * password strings.
  127. */
  128. if (lws_mqtt_str_is_not_empty(c->username)) {
  129. lws_ser_wu16be(p, c->username->len);
  130. p += 2;
  131. memcpy(p, c->username->buf, c->username->len);
  132. p += c->username->len;
  133. } else {
  134. lwsl_err("%s: Empty / missing Username!\n",
  135. __func__);
  136. return NULL;
  137. }
  138. if (c->conn_flags & LMQCFT_PASSWORD) {
  139. if (lws_mqtt_str_is_not_empty(c->password)) {
  140. lws_ser_wu16be(p, c->password->len);
  141. p += 2;
  142. memcpy(p, c->password->buf,
  143. c->password->len);
  144. p += c->password->len;
  145. } else {
  146. lws_ser_wu16be(p, 0);
  147. p += 2;
  148. }
  149. }
  150. } else if (c->conn_flags & LMQCFT_PASSWORD) {
  151. lwsl_err("%s: Unsupported - Password without username\n",
  152. __func__);
  153. return NULL;
  154. }
  155. break;
  156. default:
  157. lwsl_err("%s: unexpected state %d\n", __func__, lwsi_state(wsi));
  158. return NULL;
  159. }
  160. /*
  161. * Perform the actual write
  162. */
  163. if (lws_write(wsi, (unsigned char *)&b[LWS_PRE], lws_ptr_diff(p, start),
  164. LWS_WRITE_BINARY) != lws_ptr_diff(p, start)) {
  165. lwsl_notice("%s: write failed\n", __func__);
  166. return NULL;
  167. }
  168. return wsi;
  169. }