mqtt.c 21 KB

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