2
0

mqtt.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
  9. * Copyright (C) Björn Stenberg, <bjorn@haxx.se>
  10. *
  11. * This software is licensed as described in the file COPYING, which
  12. * you should have received as part of this distribution. The terms
  13. * are also available at https://curl.se/docs/copyright.html.
  14. *
  15. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  16. * copies of the Software, and permit persons to whom the Software is
  17. * furnished to do so, under the terms of the COPYING file.
  18. *
  19. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  20. * KIND, either express or implied.
  21. *
  22. * SPDX-License-Identifier: curl
  23. *
  24. ***************************************************************************/
  25. #include "curl_setup.h"
  26. #ifndef CURL_DISABLE_MQTT
  27. #include "urldata.h"
  28. #include <curl/curl.h>
  29. #include "transfer.h"
  30. #include "sendf.h"
  31. #include "progress.h"
  32. #include "mqtt.h"
  33. #include "select.h"
  34. #include "strdup.h"
  35. #include "url.h"
  36. #include "escape.h"
  37. #include "warnless.h"
  38. #include "curl_printf.h"
  39. #include "curl_memory.h"
  40. #include "multiif.h"
  41. #include "rand.h"
  42. /* The last #include file should be: */
  43. #include "memdebug.h"
  44. #define MQTT_MSG_CONNECT 0x10
  45. #define MQTT_MSG_CONNACK 0x20
  46. #define MQTT_MSG_PUBLISH 0x30
  47. #define MQTT_MSG_SUBSCRIBE 0x82
  48. #define MQTT_MSG_SUBACK 0x90
  49. #define MQTT_MSG_DISCONNECT 0xe0
  50. #define MQTT_CONNACK_LEN 2
  51. #define MQTT_SUBACK_LEN 3
  52. #define MQTT_CLIENTID_LEN 12 /* "curl0123abcd" */
  53. /*
  54. * Forward declarations.
  55. */
  56. static CURLcode mqtt_do(struct Curl_easy *data, bool *done);
  57. static CURLcode mqtt_done(struct Curl_easy *data,
  58. CURLcode status, bool premature);
  59. static CURLcode mqtt_doing(struct Curl_easy *data, bool *done);
  60. static int mqtt_getsock(struct Curl_easy *data, struct connectdata *conn,
  61. curl_socket_t *sock);
  62. static CURLcode mqtt_setup_conn(struct Curl_easy *data,
  63. struct connectdata *conn);
  64. /*
  65. * MQTT protocol handler.
  66. */
  67. const struct Curl_handler Curl_handler_mqtt = {
  68. "MQTT", /* scheme */
  69. mqtt_setup_conn, /* setup_connection */
  70. mqtt_do, /* do_it */
  71. mqtt_done, /* done */
  72. ZERO_NULL, /* do_more */
  73. ZERO_NULL, /* connect_it */
  74. ZERO_NULL, /* connecting */
  75. mqtt_doing, /* doing */
  76. ZERO_NULL, /* proto_getsock */
  77. mqtt_getsock, /* doing_getsock */
  78. ZERO_NULL, /* domore_getsock */
  79. ZERO_NULL, /* perform_getsock */
  80. ZERO_NULL, /* disconnect */
  81. ZERO_NULL, /* readwrite */
  82. ZERO_NULL, /* connection_check */
  83. ZERO_NULL, /* attach connection */
  84. PORT_MQTT, /* defport */
  85. CURLPROTO_MQTT, /* protocol */
  86. CURLPROTO_MQTT, /* family */
  87. PROTOPT_NONE /* flags */
  88. };
  89. static CURLcode mqtt_setup_conn(struct Curl_easy *data,
  90. struct connectdata *conn)
  91. {
  92. /* allocate the HTTP-specific struct for the Curl_easy, only to survive
  93. during this request */
  94. struct MQTT *mq;
  95. (void)conn;
  96. DEBUGASSERT(data->req.p.mqtt == NULL);
  97. mq = calloc(1, sizeof(struct MQTT));
  98. if(!mq)
  99. return CURLE_OUT_OF_MEMORY;
  100. data->req.p.mqtt = mq;
  101. return CURLE_OK;
  102. }
  103. static CURLcode mqtt_send(struct Curl_easy *data,
  104. char *buf, size_t len)
  105. {
  106. CURLcode result = CURLE_OK;
  107. struct connectdata *conn = data->conn;
  108. curl_socket_t sockfd = conn->sock[FIRSTSOCKET];
  109. struct MQTT *mq = data->req.p.mqtt;
  110. ssize_t n;
  111. result = Curl_write(data, sockfd, buf, len, &n);
  112. if(result)
  113. return result;
  114. Curl_debug(data, CURLINFO_HEADER_OUT, buf, (size_t)n);
  115. if(len != (size_t)n) {
  116. size_t nsend = len - n;
  117. char *sendleftovers = Curl_memdup(&buf[n], nsend);
  118. if(!sendleftovers)
  119. return CURLE_OUT_OF_MEMORY;
  120. mq->sendleftovers = sendleftovers;
  121. mq->nsend = nsend;
  122. }
  123. else {
  124. mq->sendleftovers = NULL;
  125. mq->nsend = 0;
  126. }
  127. return result;
  128. }
  129. /* Generic function called by the multi interface to figure out what socket(s)
  130. to wait for and for what actions during the DOING and PROTOCONNECT
  131. states */
  132. static int mqtt_getsock(struct Curl_easy *data,
  133. struct connectdata *conn,
  134. curl_socket_t *sock)
  135. {
  136. (void)data;
  137. sock[0] = conn->sock[FIRSTSOCKET];
  138. return GETSOCK_READSOCK(FIRSTSOCKET);
  139. }
  140. static int mqtt_encode_len(char *buf, size_t len)
  141. {
  142. unsigned char encoded;
  143. int i;
  144. for(i = 0; (len > 0) && (i<4); i++) {
  145. encoded = len % 0x80;
  146. len /= 0x80;
  147. if(len)
  148. encoded |= 0x80;
  149. buf[i] = encoded;
  150. }
  151. return i;
  152. }
  153. /* add the passwd to the CONNECT packet */
  154. static int add_passwd(const char *passwd, const size_t plen,
  155. char *pkt, const size_t start, int remain_pos)
  156. {
  157. /* magic number that need to be set properly */
  158. const size_t conn_flags_pos = remain_pos + 8;
  159. if(plen > 0xffff)
  160. return 1;
  161. /* set password flag */
  162. pkt[conn_flags_pos] |= 0x40;
  163. /* length of password provided */
  164. pkt[start] = (char)((plen >> 8) & 0xFF);
  165. pkt[start + 1] = (char)(plen & 0xFF);
  166. memcpy(&pkt[start + 2], passwd, plen);
  167. return 0;
  168. }
  169. /* add user to the CONNECT packet */
  170. static int add_user(const char *username, const size_t ulen,
  171. unsigned char *pkt, const size_t start, int remain_pos)
  172. {
  173. /* magic number that need to be set properly */
  174. const size_t conn_flags_pos = remain_pos + 8;
  175. if(ulen > 0xffff)
  176. return 1;
  177. /* set username flag */
  178. pkt[conn_flags_pos] |= 0x80;
  179. /* length of username provided */
  180. pkt[start] = (unsigned char)((ulen >> 8) & 0xFF);
  181. pkt[start + 1] = (unsigned char)(ulen & 0xFF);
  182. memcpy(&pkt[start + 2], username, ulen);
  183. return 0;
  184. }
  185. /* add client ID to the CONNECT packet */
  186. static int add_client_id(const char *client_id, const size_t client_id_len,
  187. char *pkt, const size_t start)
  188. {
  189. if(client_id_len != MQTT_CLIENTID_LEN)
  190. return 1;
  191. pkt[start] = 0x00;
  192. pkt[start + 1] = MQTT_CLIENTID_LEN;
  193. memcpy(&pkt[start + 2], client_id, MQTT_CLIENTID_LEN);
  194. return 0;
  195. }
  196. /* Set initial values of CONNECT packet */
  197. static int init_connpack(char *packet, char *remain, int remain_pos)
  198. {
  199. /* Fixed header starts */
  200. /* packet type */
  201. packet[0] = MQTT_MSG_CONNECT;
  202. /* remaining length field */
  203. memcpy(&packet[1], remain, remain_pos);
  204. /* Fixed header ends */
  205. /* Variable header starts */
  206. /* protocol length */
  207. packet[remain_pos + 1] = 0x00;
  208. packet[remain_pos + 2] = 0x04;
  209. /* protocol name */
  210. packet[remain_pos + 3] = 'M';
  211. packet[remain_pos + 4] = 'Q';
  212. packet[remain_pos + 5] = 'T';
  213. packet[remain_pos + 6] = 'T';
  214. /* protocol level */
  215. packet[remain_pos + 7] = 0x04;
  216. /* CONNECT flag: CleanSession */
  217. packet[remain_pos + 8] = 0x02;
  218. /* keep-alive 0 = disabled */
  219. packet[remain_pos + 9] = 0x00;
  220. packet[remain_pos + 10] = 0x3c;
  221. /* end of variable header */
  222. return remain_pos + 10;
  223. }
  224. static CURLcode mqtt_connect(struct Curl_easy *data)
  225. {
  226. CURLcode result = CURLE_OK;
  227. int pos = 0;
  228. int rc = 0;
  229. /* remain length */
  230. int remain_pos = 0;
  231. char remain[4] = {0};
  232. size_t packetlen = 0;
  233. size_t payloadlen = 0;
  234. size_t start_user = 0;
  235. size_t start_pwd = 0;
  236. char client_id[MQTT_CLIENTID_LEN + 1] = "curl";
  237. const size_t clen = strlen("curl");
  238. char *packet = NULL;
  239. /* extracting username from request */
  240. const char *username = data->state.aptr.user ?
  241. data->state.aptr.user : "";
  242. const size_t ulen = strlen(username);
  243. /* extracting password from request */
  244. const char *passwd = data->state.aptr.passwd ?
  245. data->state.aptr.passwd : "";
  246. const size_t plen = strlen(passwd);
  247. payloadlen = ulen + plen + MQTT_CLIENTID_LEN + 2;
  248. /* The plus 2 are for the MSB and LSB describing the length of the string to
  249. * be added on the payload. Refer to spec 1.5.2 and 1.5.4 */
  250. if(ulen)
  251. payloadlen += 2;
  252. if(plen)
  253. payloadlen += 2;
  254. /* getting how much occupy the remain length */
  255. remain_pos = mqtt_encode_len(remain, payloadlen + 10);
  256. /* 10 length of variable header and 1 the first byte of the fixed header */
  257. packetlen = payloadlen + 10 + remain_pos + 1;
  258. /* allocating packet */
  259. if(packetlen > 268435455)
  260. return CURLE_WEIRD_SERVER_REPLY;
  261. packet = malloc(packetlen);
  262. if(!packet)
  263. return CURLE_OUT_OF_MEMORY;
  264. memset(packet, 0, packetlen);
  265. /* set initial values for the CONNECT packet */
  266. pos = init_connpack(packet, remain, remain_pos);
  267. result = Curl_rand_hex(data, (unsigned char *)&client_id[clen],
  268. MQTT_CLIENTID_LEN - clen + 1);
  269. /* add client id */
  270. rc = add_client_id(client_id, strlen(client_id), packet, pos + 1);
  271. if(rc) {
  272. failf(data, "Client ID length mismatched: [%lu]", strlen(client_id));
  273. result = CURLE_WEIRD_SERVER_REPLY;
  274. goto end;
  275. }
  276. infof(data, "Using client id '%s'", client_id);
  277. /* position where starts the user payload */
  278. start_user = pos + 3 + MQTT_CLIENTID_LEN;
  279. /* position where starts the password payload */
  280. start_pwd = start_user + ulen;
  281. /* if user name was provided, add it to the packet */
  282. if(ulen) {
  283. start_pwd += 2;
  284. rc = add_user(username, ulen,
  285. (unsigned char *)packet, start_user, remain_pos);
  286. if(rc) {
  287. failf(data, "Username is too large: [%lu]", ulen);
  288. result = CURLE_WEIRD_SERVER_REPLY;
  289. goto end;
  290. }
  291. }
  292. /* if passwd was provided, add it to the packet */
  293. if(plen) {
  294. rc = add_passwd(passwd, plen, packet, start_pwd, remain_pos);
  295. if(rc) {
  296. failf(data, "Password is too large: [%lu]", plen);
  297. result = CURLE_WEIRD_SERVER_REPLY;
  298. goto end;
  299. }
  300. }
  301. if(!result)
  302. result = mqtt_send(data, packet, packetlen);
  303. end:
  304. if(packet)
  305. free(packet);
  306. Curl_safefree(data->state.aptr.user);
  307. Curl_safefree(data->state.aptr.passwd);
  308. return result;
  309. }
  310. static CURLcode mqtt_disconnect(struct Curl_easy *data)
  311. {
  312. CURLcode result = CURLE_OK;
  313. struct MQTT *mq = data->req.p.mqtt;
  314. result = mqtt_send(data, (char *)"\xe0\x00", 2);
  315. Curl_safefree(mq->sendleftovers);
  316. return result;
  317. }
  318. static CURLcode mqtt_verify_connack(struct Curl_easy *data)
  319. {
  320. CURLcode result;
  321. struct connectdata *conn = data->conn;
  322. curl_socket_t sockfd = conn->sock[FIRSTSOCKET];
  323. unsigned char readbuf[MQTT_CONNACK_LEN];
  324. ssize_t nread;
  325. result = Curl_read(data, sockfd, (char *)readbuf, MQTT_CONNACK_LEN, &nread);
  326. if(result)
  327. goto fail;
  328. Curl_debug(data, CURLINFO_HEADER_IN, (char *)readbuf, (size_t)nread);
  329. /* fixme */
  330. if(nread < MQTT_CONNACK_LEN) {
  331. result = CURLE_WEIRD_SERVER_REPLY;
  332. goto fail;
  333. }
  334. /* verify CONNACK */
  335. if(readbuf[0] != 0x00 || readbuf[1] != 0x00) {
  336. failf(data, "Expected %02x%02x but got %02x%02x",
  337. 0x00, 0x00, readbuf[0], readbuf[1]);
  338. result = CURLE_WEIRD_SERVER_REPLY;
  339. }
  340. fail:
  341. return result;
  342. }
  343. static CURLcode mqtt_get_topic(struct Curl_easy *data,
  344. char **topic, size_t *topiclen)
  345. {
  346. char *path = data->state.up.path;
  347. CURLcode result = CURLE_URL_MALFORMAT;
  348. if(strlen(path) > 1) {
  349. result = Curl_urldecode(path + 1, 0, topic, topiclen, REJECT_NADA);
  350. if(!result && (*topiclen > 0xffff)) {
  351. failf(data, "Too long MQTT topic");
  352. result = CURLE_URL_MALFORMAT;
  353. }
  354. }
  355. else
  356. failf(data, "No MQTT topic found. Forgot to URL encode it?");
  357. return result;
  358. }
  359. static CURLcode mqtt_subscribe(struct Curl_easy *data)
  360. {
  361. CURLcode result = CURLE_OK;
  362. char *topic = NULL;
  363. size_t topiclen;
  364. unsigned char *packet = NULL;
  365. size_t packetlen;
  366. char encodedsize[4];
  367. size_t n;
  368. struct connectdata *conn = data->conn;
  369. result = mqtt_get_topic(data, &topic, &topiclen);
  370. if(result)
  371. goto fail;
  372. conn->proto.mqtt.packetid++;
  373. packetlen = topiclen + 5; /* packetid + topic (has a two byte length field)
  374. + 2 bytes topic length + QoS byte */
  375. n = mqtt_encode_len((char *)encodedsize, packetlen);
  376. packetlen += n + 1; /* add one for the control packet type byte */
  377. packet = malloc(packetlen);
  378. if(!packet) {
  379. result = CURLE_OUT_OF_MEMORY;
  380. goto fail;
  381. }
  382. packet[0] = MQTT_MSG_SUBSCRIBE;
  383. memcpy(&packet[1], encodedsize, n);
  384. packet[1 + n] = (conn->proto.mqtt.packetid >> 8) & 0xff;
  385. packet[2 + n] = conn->proto.mqtt.packetid & 0xff;
  386. packet[3 + n] = (topiclen >> 8) & 0xff;
  387. packet[4 + n ] = topiclen & 0xff;
  388. memcpy(&packet[5 + n], topic, topiclen);
  389. packet[5 + n + topiclen] = 0; /* QoS zero */
  390. result = mqtt_send(data, (char *)packet, packetlen);
  391. fail:
  392. free(topic);
  393. free(packet);
  394. return result;
  395. }
  396. /*
  397. * Called when the first byte was already read.
  398. */
  399. static CURLcode mqtt_verify_suback(struct Curl_easy *data)
  400. {
  401. CURLcode result;
  402. struct connectdata *conn = data->conn;
  403. curl_socket_t sockfd = conn->sock[FIRSTSOCKET];
  404. unsigned char readbuf[MQTT_SUBACK_LEN];
  405. ssize_t nread;
  406. struct mqtt_conn *mqtt = &conn->proto.mqtt;
  407. result = Curl_read(data, sockfd, (char *)readbuf, MQTT_SUBACK_LEN, &nread);
  408. if(result)
  409. goto fail;
  410. Curl_debug(data, CURLINFO_HEADER_IN, (char *)readbuf, (size_t)nread);
  411. /* fixme */
  412. if(nread < MQTT_SUBACK_LEN) {
  413. result = CURLE_WEIRD_SERVER_REPLY;
  414. goto fail;
  415. }
  416. /* verify SUBACK */
  417. if(readbuf[0] != ((mqtt->packetid >> 8) & 0xff) ||
  418. readbuf[1] != (mqtt->packetid & 0xff) ||
  419. readbuf[2] != 0x00)
  420. result = CURLE_WEIRD_SERVER_REPLY;
  421. fail:
  422. return result;
  423. }
  424. static CURLcode mqtt_publish(struct Curl_easy *data)
  425. {
  426. CURLcode result;
  427. char *payload = data->set.postfields;
  428. size_t payloadlen;
  429. char *topic = NULL;
  430. size_t topiclen;
  431. unsigned char *pkt = NULL;
  432. size_t i = 0;
  433. size_t remaininglength;
  434. size_t encodelen;
  435. char encodedbytes[4];
  436. curl_off_t postfieldsize = data->set.postfieldsize;
  437. if(!payload)
  438. return CURLE_BAD_FUNCTION_ARGUMENT;
  439. if(postfieldsize < 0)
  440. payloadlen = strlen(payload);
  441. else
  442. payloadlen = (size_t)postfieldsize;
  443. result = mqtt_get_topic(data, &topic, &topiclen);
  444. if(result)
  445. goto fail;
  446. remaininglength = payloadlen + 2 + topiclen;
  447. encodelen = mqtt_encode_len(encodedbytes, remaininglength);
  448. /* add the control byte and the encoded remaining length */
  449. pkt = malloc(remaininglength + 1 + encodelen);
  450. if(!pkt) {
  451. result = CURLE_OUT_OF_MEMORY;
  452. goto fail;
  453. }
  454. /* assemble packet */
  455. pkt[i++] = MQTT_MSG_PUBLISH;
  456. memcpy(&pkt[i], encodedbytes, encodelen);
  457. i += encodelen;
  458. pkt[i++] = (topiclen >> 8) & 0xff;
  459. pkt[i++] = (topiclen & 0xff);
  460. memcpy(&pkt[i], topic, topiclen);
  461. i += topiclen;
  462. memcpy(&pkt[i], payload, payloadlen);
  463. i += payloadlen;
  464. result = mqtt_send(data, (char *)pkt, i);
  465. fail:
  466. free(pkt);
  467. free(topic);
  468. return result;
  469. }
  470. static size_t mqtt_decode_len(unsigned char *buf,
  471. size_t buflen, size_t *lenbytes)
  472. {
  473. size_t len = 0;
  474. size_t mult = 1;
  475. size_t i;
  476. unsigned char encoded = 128;
  477. for(i = 0; (i < buflen) && (encoded & 128); i++) {
  478. encoded = buf[i];
  479. len += (encoded & 127) * mult;
  480. mult *= 128;
  481. }
  482. if(lenbytes)
  483. *lenbytes = i;
  484. return len;
  485. }
  486. #ifdef CURLDEBUG
  487. static const char *statenames[]={
  488. "MQTT_FIRST",
  489. "MQTT_REMAINING_LENGTH",
  490. "MQTT_CONNACK",
  491. "MQTT_SUBACK",
  492. "MQTT_SUBACK_COMING",
  493. "MQTT_PUBWAIT",
  494. "MQTT_PUB_REMAIN",
  495. "NOT A STATE"
  496. };
  497. #endif
  498. /* The only way to change state */
  499. static void mqstate(struct Curl_easy *data,
  500. enum mqttstate state,
  501. enum mqttstate nextstate) /* used if state == FIRST */
  502. {
  503. struct connectdata *conn = data->conn;
  504. struct mqtt_conn *mqtt = &conn->proto.mqtt;
  505. #ifdef CURLDEBUG
  506. infof(data, "%s (from %s) (next is %s)",
  507. statenames[state],
  508. statenames[mqtt->state],
  509. (state == MQTT_FIRST)? statenames[nextstate] : "");
  510. #endif
  511. mqtt->state = state;
  512. if(state == MQTT_FIRST)
  513. mqtt->nextstate = nextstate;
  514. }
  515. /* for the publish packet */
  516. #define MQTT_HEADER_LEN 5 /* max 5 bytes */
  517. static CURLcode mqtt_read_publish(struct Curl_easy *data, bool *done)
  518. {
  519. CURLcode result = CURLE_OK;
  520. struct connectdata *conn = data->conn;
  521. curl_socket_t sockfd = conn->sock[FIRSTSOCKET];
  522. ssize_t nread;
  523. unsigned char *pkt = (unsigned char *)data->state.buffer;
  524. size_t remlen;
  525. struct mqtt_conn *mqtt = &conn->proto.mqtt;
  526. struct MQTT *mq = data->req.p.mqtt;
  527. unsigned char packet;
  528. switch(mqtt->state) {
  529. MQTT_SUBACK_COMING:
  530. case MQTT_SUBACK_COMING:
  531. result = mqtt_verify_suback(data);
  532. if(result)
  533. break;
  534. mqstate(data, MQTT_FIRST, MQTT_PUBWAIT);
  535. break;
  536. case MQTT_SUBACK:
  537. case MQTT_PUBWAIT:
  538. /* we are expecting PUBLISH or SUBACK */
  539. packet = mq->firstbyte & 0xf0;
  540. if(packet == MQTT_MSG_PUBLISH)
  541. mqstate(data, MQTT_PUB_REMAIN, MQTT_NOSTATE);
  542. else if(packet == MQTT_MSG_SUBACK) {
  543. mqstate(data, MQTT_SUBACK_COMING, MQTT_NOSTATE);
  544. goto MQTT_SUBACK_COMING;
  545. }
  546. else if(packet == MQTT_MSG_DISCONNECT) {
  547. infof(data, "Got DISCONNECT");
  548. *done = TRUE;
  549. goto end;
  550. }
  551. else {
  552. result = CURLE_WEIRD_SERVER_REPLY;
  553. goto end;
  554. }
  555. /* -- switched state -- */
  556. remlen = mq->remaining_length;
  557. infof(data, "Remaining length: %zd bytes", remlen);
  558. if(data->set.max_filesize &&
  559. (curl_off_t)remlen > data->set.max_filesize) {
  560. failf(data, "Maximum file size exceeded");
  561. result = CURLE_FILESIZE_EXCEEDED;
  562. goto end;
  563. }
  564. Curl_pgrsSetDownloadSize(data, remlen);
  565. data->req.bytecount = 0;
  566. data->req.size = remlen;
  567. mq->npacket = remlen; /* get this many bytes */
  568. /* FALLTHROUGH */
  569. case MQTT_PUB_REMAIN: {
  570. /* read rest of packet, but no more. Cap to buffer size */
  571. struct SingleRequest *k = &data->req;
  572. size_t rest = mq->npacket;
  573. if(rest > (size_t)data->set.buffer_size)
  574. rest = (size_t)data->set.buffer_size;
  575. result = Curl_read(data, sockfd, (char *)pkt, rest, &nread);
  576. if(result) {
  577. if(CURLE_AGAIN == result) {
  578. infof(data, "EEEE AAAAGAIN");
  579. }
  580. goto end;
  581. }
  582. if(!nread) {
  583. infof(data, "server disconnected");
  584. result = CURLE_PARTIAL_FILE;
  585. goto end;
  586. }
  587. Curl_debug(data, CURLINFO_DATA_IN, (char *)pkt, (size_t)nread);
  588. mq->npacket -= nread;
  589. k->bytecount += nread;
  590. Curl_pgrsSetDownloadCounter(data, k->bytecount);
  591. /* if QoS is set, message contains packet id */
  592. result = Curl_client_write(data, CLIENTWRITE_BODY, (char *)pkt, nread);
  593. if(result)
  594. goto end;
  595. if(!mq->npacket)
  596. /* no more PUBLISH payload, back to subscribe wait state */
  597. mqstate(data, MQTT_FIRST, MQTT_PUBWAIT);
  598. break;
  599. }
  600. default:
  601. DEBUGASSERT(NULL); /* illegal state */
  602. result = CURLE_WEIRD_SERVER_REPLY;
  603. goto end;
  604. }
  605. end:
  606. return result;
  607. }
  608. static CURLcode mqtt_do(struct Curl_easy *data, bool *done)
  609. {
  610. CURLcode result = CURLE_OK;
  611. *done = FALSE; /* unconditionally */
  612. result = mqtt_connect(data);
  613. if(result) {
  614. failf(data, "Error %d sending MQTT CONNECT request", result);
  615. return result;
  616. }
  617. mqstate(data, MQTT_FIRST, MQTT_CONNACK);
  618. return CURLE_OK;
  619. }
  620. static CURLcode mqtt_done(struct Curl_easy *data,
  621. CURLcode status, bool premature)
  622. {
  623. struct MQTT *mq = data->req.p.mqtt;
  624. (void)status;
  625. (void)premature;
  626. Curl_safefree(mq->sendleftovers);
  627. return CURLE_OK;
  628. }
  629. static CURLcode mqtt_doing(struct Curl_easy *data, bool *done)
  630. {
  631. CURLcode result = CURLE_OK;
  632. struct connectdata *conn = data->conn;
  633. struct mqtt_conn *mqtt = &conn->proto.mqtt;
  634. struct MQTT *mq = data->req.p.mqtt;
  635. ssize_t nread;
  636. curl_socket_t sockfd = conn->sock[FIRSTSOCKET];
  637. unsigned char *pkt = (unsigned char *)data->state.buffer;
  638. unsigned char byte;
  639. *done = FALSE;
  640. if(mq->nsend) {
  641. /* send the remainder of an outgoing packet */
  642. char *ptr = mq->sendleftovers;
  643. result = mqtt_send(data, mq->sendleftovers, mq->nsend);
  644. free(ptr);
  645. if(result)
  646. return result;
  647. }
  648. infof(data, "mqtt_doing: state [%d]", (int) mqtt->state);
  649. switch(mqtt->state) {
  650. case MQTT_FIRST:
  651. /* Read the initial byte only */
  652. result = Curl_read(data, sockfd, (char *)&mq->firstbyte, 1, &nread);
  653. if(result)
  654. break;
  655. else if(!nread) {
  656. failf(data, "Connection disconnected");
  657. *done = TRUE;
  658. result = CURLE_RECV_ERROR;
  659. break;
  660. }
  661. Curl_debug(data, CURLINFO_HEADER_IN, (char *)&mq->firstbyte, 1);
  662. /* remember the first byte */
  663. mq->npacket = 0;
  664. mqstate(data, MQTT_REMAINING_LENGTH, MQTT_NOSTATE);
  665. /* FALLTHROUGH */
  666. case MQTT_REMAINING_LENGTH:
  667. do {
  668. result = Curl_read(data, sockfd, (char *)&byte, 1, &nread);
  669. if(!nread)
  670. break;
  671. Curl_debug(data, CURLINFO_HEADER_IN, (char *)&byte, 1);
  672. pkt[mq->npacket++] = byte;
  673. } while((byte & 0x80) && (mq->npacket < 4));
  674. if(nread && (byte & 0x80))
  675. /* MQTT supports up to 127 * 128^0 + 127 * 128^1 + 127 * 128^2 +
  676. 127 * 128^3 bytes. server tried to send more */
  677. result = CURLE_WEIRD_SERVER_REPLY;
  678. if(result)
  679. break;
  680. mq->remaining_length = mqtt_decode_len(&pkt[0], mq->npacket, NULL);
  681. mq->npacket = 0;
  682. if(mq->remaining_length) {
  683. mqstate(data, mqtt->nextstate, MQTT_NOSTATE);
  684. break;
  685. }
  686. mqstate(data, MQTT_FIRST, MQTT_FIRST);
  687. if(mq->firstbyte == MQTT_MSG_DISCONNECT) {
  688. infof(data, "Got DISCONNECT");
  689. *done = TRUE;
  690. }
  691. break;
  692. case MQTT_CONNACK:
  693. result = mqtt_verify_connack(data);
  694. if(result)
  695. break;
  696. if(data->state.httpreq == HTTPREQ_POST) {
  697. result = mqtt_publish(data);
  698. if(!result) {
  699. result = mqtt_disconnect(data);
  700. *done = TRUE;
  701. }
  702. mqtt->nextstate = MQTT_FIRST;
  703. }
  704. else {
  705. result = mqtt_subscribe(data);
  706. if(!result) {
  707. mqstate(data, MQTT_FIRST, MQTT_SUBACK);
  708. }
  709. }
  710. break;
  711. case MQTT_SUBACK:
  712. case MQTT_PUBWAIT:
  713. case MQTT_PUB_REMAIN:
  714. result = mqtt_read_publish(data, done);
  715. break;
  716. default:
  717. failf(data, "State not handled yet");
  718. *done = TRUE;
  719. break;
  720. }
  721. if(result == CURLE_AGAIN)
  722. result = CURLE_OK;
  723. return result;
  724. }
  725. #endif /* CURL_DISABLE_MQTT */