mqtt.c 22 KB

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