Line | Count | Source |
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 | | |
26 | | #include "curl_setup.h" |
27 | | |
28 | | #ifndef CURL_DISABLE_MQTT |
29 | | |
30 | | #include "urldata.h" |
31 | | #include <curl/curl.h> |
32 | | #include "transfer.h" |
33 | | #include "sendf.h" |
34 | | #include "progress.h" |
35 | | #include "mqtt.h" |
36 | | #include "select.h" |
37 | | #include "url.h" |
38 | | #include "escape.h" |
39 | | #include "curlx/warnless.h" |
40 | | #include "multiif.h" |
41 | | #include "rand.h" |
42 | | |
43 | | /* first byte is command. |
44 | | second byte is for flags. */ |
45 | 1.18k | #define MQTT_MSG_CONNECT 0x10 |
46 | | /* #define MQTT_MSG_CONNACK 0x20 */ |
47 | 1.60k | #define MQTT_MSG_PUBLISH 0x30 |
48 | 121 | #define MQTT_MSG_SUBSCRIBE 0x82 |
49 | 93 | #define MQTT_MSG_SUBACK 0x90 |
50 | 16.0k | #define MQTT_MSG_DISCONNECT 0xe0 |
51 | | /* #define MQTT_MSG_PINGREQ 0xC0 */ |
52 | 15.9k | #define MQTT_MSG_PINGRESP 0xD0 |
53 | | |
54 | 4.44M | #define MQTT_CONNACK_LEN 2 |
55 | 140 | #define MQTT_SUBACK_LEN 3 |
56 | 7.08k | #define MQTT_CLIENTID_LEN 12 /* "curl0123abcd" */ |
57 | | |
58 | | /* meta key for storing protocol meta at easy handle */ |
59 | 17.8M | #define CURL_META_MQTT_EASY "meta:proto:mqtt:easy" |
60 | | /* meta key for storing protocol meta at connection */ |
61 | 9.00M | #define CURL_META_MQTT_CONN "meta:proto:mqtt:conn" |
62 | | |
63 | | enum mqttstate { |
64 | | MQTT_FIRST, /* 0 */ |
65 | | MQTT_REMAINING_LENGTH, /* 1 */ |
66 | | MQTT_CONNACK, /* 2 */ |
67 | | MQTT_SUBACK, /* 3 */ |
68 | | MQTT_SUBACK_COMING, /* 4 - the SUBACK remainder */ |
69 | | MQTT_PUBWAIT, /* 5 - wait for publish */ |
70 | | MQTT_PUB_REMAIN, /* 6 - wait for the remainder of the publish */ |
71 | | |
72 | | MQTT_NOSTATE /* 7 - never used an actual state */ |
73 | | }; |
74 | | |
75 | | struct mqtt_conn { |
76 | | enum mqttstate state; |
77 | | enum mqttstate nextstate; /* switch to this after remaining length is |
78 | | done */ |
79 | | unsigned int packetid; |
80 | | }; |
81 | | |
82 | | /* protocol-specific transfer-related data */ |
83 | | struct MQTT { |
84 | | struct dynbuf sendbuf; |
85 | | /* when receiving */ |
86 | | struct dynbuf recvbuf; |
87 | | size_t npacket; /* byte counter */ |
88 | | size_t remaining_length; |
89 | | unsigned char pkt_hd[4]; /* for decoding the arriving packet length */ |
90 | | struct curltime lastTime; /* last time we sent or received data */ |
91 | | unsigned char firstbyte; |
92 | | BIT(pingsent); /* 1 while we wait for ping response */ |
93 | | }; |
94 | | |
95 | | /* |
96 | | * Forward declarations. |
97 | | */ |
98 | | |
99 | | static CURLcode mqtt_do(struct Curl_easy *data, bool *done); |
100 | | static CURLcode mqtt_done(struct Curl_easy *data, |
101 | | CURLcode status, bool premature); |
102 | | static CURLcode mqtt_doing(struct Curl_easy *data, bool *done); |
103 | | static CURLcode mqtt_pollset(struct Curl_easy *data, |
104 | | struct easy_pollset *ps); |
105 | | static CURLcode mqtt_setup_conn(struct Curl_easy *data, |
106 | | struct connectdata *conn); |
107 | | |
108 | | /* |
109 | | * MQTT protocol handler. |
110 | | */ |
111 | | |
112 | | const struct Curl_handler Curl_handler_mqtt = { |
113 | | "mqtt", /* scheme */ |
114 | | mqtt_setup_conn, /* setup_connection */ |
115 | | mqtt_do, /* do_it */ |
116 | | mqtt_done, /* done */ |
117 | | ZERO_NULL, /* do_more */ |
118 | | ZERO_NULL, /* connect_it */ |
119 | | ZERO_NULL, /* connecting */ |
120 | | mqtt_doing, /* doing */ |
121 | | ZERO_NULL, /* proto_pollset */ |
122 | | mqtt_pollset, /* doing_pollset */ |
123 | | ZERO_NULL, /* domore_pollset */ |
124 | | ZERO_NULL, /* perform_pollset */ |
125 | | ZERO_NULL, /* disconnect */ |
126 | | ZERO_NULL, /* write_resp */ |
127 | | ZERO_NULL, /* write_resp_hd */ |
128 | | ZERO_NULL, /* connection_check */ |
129 | | ZERO_NULL, /* attach connection */ |
130 | | ZERO_NULL, /* follow */ |
131 | | PORT_MQTT, /* defport */ |
132 | | CURLPROTO_MQTT, /* protocol */ |
133 | | CURLPROTO_MQTT, /* family */ |
134 | | PROTOPT_NONE /* flags */ |
135 | | }; |
136 | | |
137 | | static void mqtt_easy_dtor(void *key, size_t klen, void *entry) |
138 | 5.42k | { |
139 | 5.42k | struct MQTT *mq = entry; |
140 | 5.42k | (void)key; |
141 | 5.42k | (void)klen; |
142 | 5.42k | curlx_dyn_free(&mq->sendbuf); |
143 | 5.42k | curlx_dyn_free(&mq->recvbuf); |
144 | 5.42k | curlx_free(mq); |
145 | 5.42k | } |
146 | | |
147 | | static void mqtt_conn_dtor(void *key, size_t klen, void *entry) |
148 | 5.42k | { |
149 | 5.42k | (void)key; |
150 | 5.42k | (void)klen; |
151 | 5.42k | curlx_free(entry); |
152 | 5.42k | } |
153 | | |
154 | | static CURLcode mqtt_setup_conn(struct Curl_easy *data, |
155 | | struct connectdata *conn) |
156 | 5.42k | { |
157 | | /* setup MQTT specific meta data at easy handle and connection */ |
158 | 5.42k | struct mqtt_conn *mqtt; |
159 | 5.42k | struct MQTT *mq; |
160 | | |
161 | 5.42k | mqtt = curlx_calloc(1, sizeof(*mqtt)); |
162 | 5.42k | if(!mqtt || |
163 | 5.42k | Curl_conn_meta_set(conn, CURL_META_MQTT_CONN, mqtt, mqtt_conn_dtor)) |
164 | 0 | return CURLE_OUT_OF_MEMORY; |
165 | | |
166 | 5.42k | mq = curlx_calloc(1, sizeof(struct MQTT)); |
167 | 5.42k | if(!mq) |
168 | 0 | return CURLE_OUT_OF_MEMORY; |
169 | 5.42k | curlx_dyn_init(&mq->recvbuf, DYN_MQTT_RECV); |
170 | 5.42k | curlx_dyn_init(&mq->sendbuf, DYN_MQTT_SEND); |
171 | 5.42k | if(Curl_meta_set(data, CURL_META_MQTT_EASY, mq, mqtt_easy_dtor)) |
172 | 0 | return CURLE_OUT_OF_MEMORY; |
173 | 5.42k | return CURLE_OK; |
174 | 5.42k | } |
175 | | |
176 | | static CURLcode mqtt_send(struct Curl_easy *data, |
177 | | const char *buf, size_t len) |
178 | 1.34k | { |
179 | 1.34k | size_t n; |
180 | 1.34k | CURLcode result; |
181 | 1.34k | struct MQTT *mq = Curl_meta_get(data, CURL_META_MQTT_EASY); |
182 | | |
183 | 1.34k | if(!mq) |
184 | 0 | return CURLE_FAILED_INIT; |
185 | | |
186 | 1.34k | result = Curl_xfer_send(data, buf, len, FALSE, &n); |
187 | 1.34k | if(result) |
188 | 0 | return result; |
189 | 1.34k | mq->lastTime = curlx_now(); |
190 | 1.34k | Curl_debug(data, CURLINFO_HEADER_OUT, buf, n); |
191 | 1.34k | if(len != n) { |
192 | 0 | size_t nsend = len - n; |
193 | 0 | if(curlx_dyn_len(&mq->sendbuf)) { |
194 | 0 | DEBUGASSERT(curlx_dyn_len(&mq->sendbuf) >= nsend); |
195 | 0 | result = curlx_dyn_tail(&mq->sendbuf, nsend); /* keep this much */ |
196 | 0 | } |
197 | 0 | else { |
198 | 0 | result = curlx_dyn_addn(&mq->sendbuf, &buf[n], nsend); |
199 | 0 | } |
200 | 0 | } |
201 | 1.34k | else |
202 | 1.34k | curlx_dyn_reset(&mq->sendbuf); |
203 | 1.34k | return result; |
204 | 1.34k | } |
205 | | |
206 | | /* Generic function called by the multi interface to figure out what socket(s) |
207 | | to wait for and for what actions during the DOING and PROTOCONNECT |
208 | | states */ |
209 | | static CURLcode mqtt_pollset(struct Curl_easy *data, |
210 | | struct easy_pollset *ps) |
211 | 4.47M | { |
212 | 4.47M | return Curl_pollset_add_in(data, ps, data->conn->sock[FIRSTSOCKET]); |
213 | 4.47M | } |
214 | | |
215 | | static int mqtt_encode_len(char *buf, size_t len) |
216 | 1.32k | { |
217 | 1.32k | int i; |
218 | | |
219 | 2.78k | for(i = 0; (len > 0) && (i < 4); i++) { |
220 | 1.45k | unsigned char encoded; |
221 | 1.45k | encoded = len % 0x80; |
222 | 1.45k | len /= 0x80; |
223 | 1.45k | if(len) |
224 | 128 | encoded |= 0x80; |
225 | 1.45k | buf[i] = (char)encoded; |
226 | 1.45k | } |
227 | | |
228 | 1.32k | return i; |
229 | 1.32k | } |
230 | | |
231 | | /* add the passwd to the CONNECT packet */ |
232 | | static int add_passwd(const char *passwd, const size_t plen, |
233 | | char *pkt, const size_t start, int remain_pos) |
234 | 38 | { |
235 | | /* magic number that need to be set properly */ |
236 | 38 | const size_t conn_flags_pos = remain_pos + 8; |
237 | 38 | if(plen > 0xffff) |
238 | 1 | return 1; |
239 | | |
240 | | /* set password flag */ |
241 | 37 | pkt[conn_flags_pos] |= 0x40; |
242 | | |
243 | | /* length of password provided */ |
244 | 37 | pkt[start] = (char)((plen >> 8) & 0xFF); |
245 | 37 | pkt[start + 1] = (char)(plen & 0xFF); |
246 | 37 | memcpy(&pkt[start + 2], passwd, plen); |
247 | 37 | return 0; |
248 | 38 | } |
249 | | |
250 | | /* add user to the CONNECT packet */ |
251 | | static int add_user(const char *username, const size_t ulen, |
252 | | unsigned char *pkt, const size_t start, int remain_pos) |
253 | 125 | { |
254 | | /* magic number that need to be set properly */ |
255 | 125 | const size_t conn_flags_pos = remain_pos + 8; |
256 | 125 | if(ulen > 0xffff) |
257 | 6 | return 1; |
258 | | |
259 | | /* set username flag */ |
260 | 119 | pkt[conn_flags_pos] |= 0x80; |
261 | | /* length of username provided */ |
262 | 119 | pkt[start] = (unsigned char)((ulen >> 8) & 0xFF); |
263 | 119 | pkt[start + 1] = (unsigned char)(ulen & 0xFF); |
264 | 119 | memcpy(&pkt[start + 2], username, ulen); |
265 | 119 | return 0; |
266 | 125 | } |
267 | | |
268 | | /* add client ID to the CONNECT packet */ |
269 | | static int add_client_id(const char *client_id, const size_t client_id_len, |
270 | | char *pkt, const size_t start) |
271 | 1.18k | { |
272 | 1.18k | if(client_id_len != MQTT_CLIENTID_LEN) |
273 | 0 | return 1; |
274 | 1.18k | pkt[start] = 0x00; |
275 | 1.18k | pkt[start + 1] = MQTT_CLIENTID_LEN; |
276 | 1.18k | memcpy(&pkt[start + 2], client_id, MQTT_CLIENTID_LEN); |
277 | 1.18k | return 0; |
278 | 1.18k | } |
279 | | |
280 | | /* Set initial values of CONNECT packet */ |
281 | | static int init_connpack(char *packet, char *remain, int remain_pos) |
282 | 1.18k | { |
283 | | /* Fixed header starts */ |
284 | | /* packet type */ |
285 | 1.18k | packet[0] = MQTT_MSG_CONNECT; |
286 | | /* remaining length field */ |
287 | 1.18k | memcpy(&packet[1], remain, remain_pos); |
288 | | /* Fixed header ends */ |
289 | | |
290 | | /* Variable header starts */ |
291 | | /* protocol length */ |
292 | 1.18k | packet[remain_pos + 1] = 0x00; |
293 | 1.18k | packet[remain_pos + 2] = 0x04; |
294 | | /* protocol name */ |
295 | 1.18k | packet[remain_pos + 3] = 'M'; |
296 | 1.18k | packet[remain_pos + 4] = 'Q'; |
297 | 1.18k | packet[remain_pos + 5] = 'T'; |
298 | 1.18k | packet[remain_pos + 6] = 'T'; |
299 | | /* protocol level */ |
300 | 1.18k | packet[remain_pos + 7] = 0x04; |
301 | | /* CONNECT flag: CleanSession */ |
302 | 1.18k | packet[remain_pos + 8] = 0x02; |
303 | | /* keep-alive 0 = disabled */ |
304 | 1.18k | packet[remain_pos + 9] = 0x00; |
305 | 1.18k | packet[remain_pos + 10] = 0x3c; |
306 | | /* end of variable header */ |
307 | 1.18k | return remain_pos + 10; |
308 | 1.18k | } |
309 | | |
310 | | static CURLcode mqtt_connect(struct Curl_easy *data) |
311 | 1.18k | { |
312 | 1.18k | CURLcode result = CURLE_OK; |
313 | 1.18k | int pos = 0; |
314 | 1.18k | int rc = 0; |
315 | | /* remain length */ |
316 | 1.18k | int remain_pos = 0; |
317 | 1.18k | char remain[4] = { 0 }; |
318 | 1.18k | size_t packetlen = 0; |
319 | 1.18k | size_t start_user = 0; |
320 | 1.18k | size_t start_pwd = 0; |
321 | 1.18k | char client_id[MQTT_CLIENTID_LEN + 1] = "curl"; |
322 | 1.18k | const size_t clen = strlen("curl"); |
323 | 1.18k | char *packet = NULL; |
324 | | |
325 | | /* extracting username from request */ |
326 | 1.18k | const char *username = data->state.aptr.user ? data->state.aptr.user : ""; |
327 | 1.18k | const size_t ulen = strlen(username); |
328 | | /* extracting password from request */ |
329 | 1.18k | const char *passwd = data->state.aptr.passwd ? data->state.aptr.passwd : ""; |
330 | 1.18k | const size_t plen = strlen(passwd); |
331 | 1.18k | const size_t payloadlen = ulen + plen + MQTT_CLIENTID_LEN + 2 + |
332 | | /* The plus 2s below are for the MSB and LSB describing the length of the |
333 | | string to be added on the payload. Refer to spec 1.5.2 and 1.5.4 */ |
334 | 1.18k | (ulen ? 2 : 0) + |
335 | 1.18k | (plen ? 2 : 0); |
336 | | |
337 | | /* getting how much occupy the remain length */ |
338 | 1.18k | remain_pos = mqtt_encode_len(remain, payloadlen + 10); |
339 | | |
340 | | /* 10 length of variable header and 1 the first byte of the fixed header */ |
341 | 1.18k | packetlen = payloadlen + 10 + remain_pos + 1; |
342 | | |
343 | | /* allocating packet */ |
344 | 1.18k | if(packetlen > 0xFFFFFFF) |
345 | 0 | return CURLE_WEIRD_SERVER_REPLY; |
346 | 1.18k | packet = curlx_calloc(1, packetlen); |
347 | 1.18k | if(!packet) |
348 | 0 | return CURLE_OUT_OF_MEMORY; |
349 | | |
350 | | /* set initial values for the CONNECT packet */ |
351 | 1.18k | pos = init_connpack(packet, remain, remain_pos); |
352 | | |
353 | 1.18k | result = Curl_rand_alnum(data, (unsigned char *)&client_id[clen], |
354 | 1.18k | MQTT_CLIENTID_LEN - clen + 1); |
355 | | /* add client id */ |
356 | 1.18k | rc = add_client_id(client_id, strlen(client_id), packet, pos + 1); |
357 | 1.18k | if(rc) { |
358 | 0 | failf(data, "Client ID length mismatched: [%zu]", strlen(client_id)); |
359 | 0 | result = CURLE_WEIRD_SERVER_REPLY; |
360 | 0 | goto end; |
361 | 0 | } |
362 | 1.18k | infof(data, "Using client id '%s'", client_id); |
363 | | |
364 | | /* position where the user payload starts */ |
365 | 1.18k | start_user = pos + 3 + MQTT_CLIENTID_LEN; |
366 | | /* position where the password payload starts */ |
367 | 1.18k | start_pwd = start_user + ulen; |
368 | | /* if username was provided, add it to the packet */ |
369 | 1.18k | if(ulen) { |
370 | 125 | start_pwd += 2; |
371 | | |
372 | 125 | rc = add_user(username, ulen, |
373 | 125 | (unsigned char *)packet, start_user, remain_pos); |
374 | 125 | if(rc) { |
375 | 6 | failf(data, "Username too long: [%zu]", ulen); |
376 | 6 | result = CURLE_WEIRD_SERVER_REPLY; |
377 | 6 | goto end; |
378 | 6 | } |
379 | 125 | } |
380 | | |
381 | | /* if passwd was provided, add it to the packet */ |
382 | 1.17k | if(plen) { |
383 | 38 | rc = add_passwd(passwd, plen, packet, start_pwd, remain_pos); |
384 | 38 | if(rc) { |
385 | 1 | failf(data, "Password too long: [%zu]", plen); |
386 | 1 | result = CURLE_WEIRD_SERVER_REPLY; |
387 | 1 | goto end; |
388 | 1 | } |
389 | 38 | } |
390 | | |
391 | 1.17k | if(!result) |
392 | 1.17k | result = mqtt_send(data, packet, packetlen); |
393 | | |
394 | 1.18k | end: |
395 | 1.18k | if(packet) |
396 | 1.18k | curlx_free(packet); |
397 | 1.18k | Curl_safefree(data->state.aptr.user); |
398 | 1.18k | Curl_safefree(data->state.aptr.passwd); |
399 | 1.18k | return result; |
400 | 1.17k | } |
401 | | |
402 | | static CURLcode mqtt_disconnect(struct Curl_easy *data) |
403 | 26 | { |
404 | 26 | return mqtt_send(data, "\xe0\x00", 2); |
405 | 26 | } |
406 | | |
407 | | static CURLcode mqtt_recv_atleast(struct Curl_easy *data, size_t nbytes) |
408 | 4.44M | { |
409 | 4.44M | struct MQTT *mq = Curl_meta_get(data, CURL_META_MQTT_EASY); |
410 | 4.44M | size_t rlen; |
411 | 4.44M | CURLcode result; |
412 | | |
413 | 4.44M | if(!mq) |
414 | 0 | return CURLE_FAILED_INIT; |
415 | 4.44M | rlen = curlx_dyn_len(&mq->recvbuf); |
416 | | |
417 | 4.44M | if(rlen < nbytes) { |
418 | 4.44M | unsigned char readbuf[1024]; |
419 | 4.44M | size_t nread; |
420 | | |
421 | 4.44M | DEBUGASSERT(nbytes - rlen < sizeof(readbuf)); |
422 | 4.44M | result = Curl_xfer_recv(data, (char *)readbuf, nbytes - rlen, &nread); |
423 | 4.44M | if(result) |
424 | 4 | return result; |
425 | 4.44M | if(curlx_dyn_addn(&mq->recvbuf, readbuf, nread)) |
426 | 0 | return CURLE_OUT_OF_MEMORY; |
427 | 4.44M | rlen = curlx_dyn_len(&mq->recvbuf); |
428 | 4.44M | } |
429 | 4.44M | return (rlen >= nbytes) ? CURLE_OK : CURLE_AGAIN; |
430 | 4.44M | } |
431 | | |
432 | | static void mqtt_recv_consume(struct Curl_easy *data, size_t nbytes) |
433 | 191 | { |
434 | 191 | struct MQTT *mq = Curl_meta_get(data, CURL_META_MQTT_EASY); |
435 | 191 | DEBUGASSERT(mq); |
436 | 191 | if(mq) { |
437 | 191 | size_t rlen = curlx_dyn_len(&mq->recvbuf); |
438 | 191 | if(rlen <= nbytes) |
439 | 191 | curlx_dyn_reset(&mq->recvbuf); |
440 | 0 | else |
441 | 0 | curlx_dyn_tail(&mq->recvbuf, rlen - nbytes); |
442 | 191 | } |
443 | 191 | } |
444 | | |
445 | | static CURLcode mqtt_verify_connack(struct Curl_easy *data) |
446 | 4.44M | { |
447 | 4.44M | struct MQTT *mq = Curl_meta_get(data, CURL_META_MQTT_EASY); |
448 | 4.44M | CURLcode result; |
449 | 4.44M | char *ptr; |
450 | | |
451 | 4.44M | DEBUGASSERT(mq); |
452 | 4.44M | if(!mq) |
453 | 0 | return CURLE_FAILED_INIT; |
454 | | |
455 | 4.44M | result = mqtt_recv_atleast(data, MQTT_CONNACK_LEN); |
456 | 4.44M | if(result) |
457 | 4.44M | goto fail; |
458 | | |
459 | | /* verify CONNACK */ |
460 | 161 | DEBUGASSERT(curlx_dyn_len(&mq->recvbuf) >= MQTT_CONNACK_LEN); |
461 | 161 | ptr = curlx_dyn_ptr(&mq->recvbuf); |
462 | 161 | Curl_debug(data, CURLINFO_HEADER_IN, ptr, MQTT_CONNACK_LEN); |
463 | | |
464 | 161 | if(ptr[0] != 0x00 || ptr[1] != 0x00) { |
465 | 5 | failf(data, "Expected %02x%02x but got %02x%02x", |
466 | 5 | 0x00, 0x00, ptr[0], ptr[1]); |
467 | 5 | curlx_dyn_reset(&mq->recvbuf); |
468 | 5 | result = CURLE_WEIRD_SERVER_REPLY; |
469 | 5 | goto fail; |
470 | 5 | } |
471 | 156 | mqtt_recv_consume(data, MQTT_CONNACK_LEN); |
472 | 4.44M | fail: |
473 | 4.44M | return result; |
474 | 156 | } |
475 | | |
476 | | static CURLcode mqtt_get_topic(struct Curl_easy *data, |
477 | | char **topic, size_t *topiclen) |
478 | 155 | { |
479 | 155 | char *path = data->state.up.path; |
480 | 155 | CURLcode result = CURLE_URL_MALFORMAT; |
481 | 155 | if(strlen(path) > 1) { |
482 | 153 | result = Curl_urldecode(path + 1, 0, topic, topiclen, REJECT_NADA); |
483 | 153 | if(!result && (*topiclen > 0xffff)) { |
484 | 6 | failf(data, "Too long MQTT topic"); |
485 | 6 | result = CURLE_URL_MALFORMAT; |
486 | 6 | } |
487 | 153 | } |
488 | 2 | else |
489 | 2 | failf(data, "No MQTT topic found. Forgot to URL encode it?"); |
490 | | |
491 | 155 | return result; |
492 | 155 | } |
493 | | |
494 | | static CURLcode mqtt_subscribe(struct Curl_easy *data) |
495 | 123 | { |
496 | 123 | CURLcode result = CURLE_OK; |
497 | 123 | char *topic = NULL; |
498 | 123 | size_t topiclen; |
499 | 123 | unsigned char *packet = NULL; |
500 | 123 | size_t packetlen; |
501 | 123 | char encodedsize[4]; |
502 | 123 | size_t n; |
503 | 123 | struct connectdata *conn = data->conn; |
504 | 123 | struct mqtt_conn *mqtt = Curl_conn_meta_get(conn, CURL_META_MQTT_CONN); |
505 | | |
506 | 123 | if(!mqtt) |
507 | 0 | return CURLE_FAILED_INIT; |
508 | | |
509 | 123 | result = mqtt_get_topic(data, &topic, &topiclen); |
510 | 123 | if(result) |
511 | 2 | goto fail; |
512 | | |
513 | 121 | mqtt->packetid++; |
514 | | |
515 | 121 | packetlen = topiclen + 5; /* packetid + topic (has a two byte length field) |
516 | | + 2 bytes topic length + QoS byte */ |
517 | 121 | n = mqtt_encode_len((char *)encodedsize, packetlen); |
518 | 121 | packetlen += n + 1; /* add one for the control packet type byte */ |
519 | | |
520 | 121 | packet = curlx_malloc(packetlen); |
521 | 121 | if(!packet) { |
522 | 0 | result = CURLE_OUT_OF_MEMORY; |
523 | 0 | goto fail; |
524 | 0 | } |
525 | | |
526 | 121 | packet[0] = MQTT_MSG_SUBSCRIBE; |
527 | 121 | memcpy(&packet[1], encodedsize, n); |
528 | 121 | packet[1 + n] = (mqtt->packetid >> 8) & 0xff; |
529 | 121 | packet[2 + n] = mqtt->packetid & 0xff; |
530 | 121 | packet[3 + n] = (topiclen >> 8) & 0xff; |
531 | 121 | packet[4 + n] = topiclen & 0xff; |
532 | 121 | memcpy(&packet[5 + n], topic, topiclen); |
533 | 121 | packet[5 + n + topiclen] = 0; /* QoS zero */ |
534 | | |
535 | 121 | result = mqtt_send(data, (const char *)packet, packetlen); |
536 | | |
537 | 123 | fail: |
538 | 123 | curlx_free(topic); |
539 | 123 | curlx_free(packet); |
540 | 123 | return result; |
541 | 121 | } |
542 | | |
543 | | /* |
544 | | * Called when the first byte was already read. |
545 | | */ |
546 | | static CURLcode mqtt_verify_suback(struct Curl_easy *data) |
547 | 62 | { |
548 | 62 | struct MQTT *mq = Curl_meta_get(data, CURL_META_MQTT_EASY); |
549 | 62 | struct connectdata *conn = data->conn; |
550 | 62 | struct mqtt_conn *mqtt = Curl_conn_meta_get(conn, CURL_META_MQTT_CONN); |
551 | 62 | CURLcode result; |
552 | 62 | char *ptr; |
553 | | |
554 | 62 | if(!mqtt || !mq) |
555 | 0 | return CURLE_FAILED_INIT; |
556 | | |
557 | 62 | result = mqtt_recv_atleast(data, MQTT_SUBACK_LEN); |
558 | 62 | if(result) |
559 | 19 | goto fail; |
560 | | |
561 | | /* verify SUBACK */ |
562 | 43 | DEBUGASSERT(curlx_dyn_len(&mq->recvbuf) >= MQTT_SUBACK_LEN); |
563 | 43 | ptr = curlx_dyn_ptr(&mq->recvbuf); |
564 | 43 | Curl_debug(data, CURLINFO_HEADER_IN, ptr, MQTT_SUBACK_LEN); |
565 | | |
566 | 43 | if(((unsigned char)ptr[0]) != ((mqtt->packetid >> 8) & 0xff) || |
567 | 38 | ((unsigned char)ptr[1]) != (mqtt->packetid & 0xff) || |
568 | 37 | ptr[2] != 0x00) { |
569 | 8 | curlx_dyn_reset(&mq->recvbuf); |
570 | 8 | result = CURLE_WEIRD_SERVER_REPLY; |
571 | 8 | goto fail; |
572 | 8 | } |
573 | 35 | mqtt_recv_consume(data, MQTT_SUBACK_LEN); |
574 | 62 | fail: |
575 | 62 | return result; |
576 | 35 | } |
577 | | |
578 | 26 | #define MAX_MQTT_MESSAGE_SIZE 0xFFFFFFF |
579 | | |
580 | | static CURLcode mqtt_publish(struct Curl_easy *data) |
581 | 33 | { |
582 | 33 | CURLcode result; |
583 | 33 | char *payload = data->set.postfields; |
584 | 33 | size_t payloadlen; |
585 | 33 | char *topic = NULL; |
586 | 33 | size_t topiclen; |
587 | 33 | unsigned char *pkt = NULL; |
588 | 33 | size_t i = 0; |
589 | 33 | size_t remaininglength; |
590 | 33 | size_t encodelen; |
591 | 33 | char encodedbytes[4]; |
592 | 33 | curl_off_t postfieldsize = data->set.postfieldsize; |
593 | | |
594 | 33 | if(!payload) { |
595 | 1 | DEBUGF(infof(data, "mqtt_publish without payload, return bad arg")); |
596 | 1 | return CURLE_BAD_FUNCTION_ARGUMENT; |
597 | 1 | } |
598 | 32 | if(!curlx_sotouz_fits(postfieldsize, &payloadlen)) { |
599 | 32 | if(postfieldsize > 0) /* off_t does not fit into size_t */ |
600 | 0 | return CURLE_BAD_FUNCTION_ARGUMENT; |
601 | 32 | payloadlen = strlen(payload); |
602 | 32 | } |
603 | | |
604 | 32 | result = mqtt_get_topic(data, &topic, &topiclen); |
605 | 32 | if(result) |
606 | 6 | goto fail; |
607 | | |
608 | 26 | remaininglength = payloadlen + 2 + topiclen; |
609 | 26 | encodelen = mqtt_encode_len(encodedbytes, remaininglength); |
610 | 26 | if(MAX_MQTT_MESSAGE_SIZE - remaininglength - 1 < encodelen) { |
611 | 0 | result = CURLE_TOO_LARGE; |
612 | 0 | goto fail; |
613 | 0 | } |
614 | | |
615 | | /* add the control byte and the encoded remaining length */ |
616 | 26 | pkt = curlx_malloc(remaininglength + 1 + encodelen); |
617 | 26 | if(!pkt) { |
618 | 0 | result = CURLE_OUT_OF_MEMORY; |
619 | 0 | goto fail; |
620 | 0 | } |
621 | | |
622 | | /* assemble packet */ |
623 | 26 | pkt[i++] = MQTT_MSG_PUBLISH; |
624 | 26 | memcpy(&pkt[i], encodedbytes, encodelen); |
625 | 26 | i += encodelen; |
626 | 26 | pkt[i++] = (topiclen >> 8) & 0xff; |
627 | 26 | pkt[i++] = (topiclen & 0xff); |
628 | 26 | memcpy(&pkt[i], topic, topiclen); |
629 | 26 | i += topiclen; |
630 | 26 | memcpy(&pkt[i], payload, payloadlen); |
631 | 26 | i += payloadlen; |
632 | 26 | result = mqtt_send(data, (const char *)pkt, i); |
633 | | |
634 | 32 | fail: |
635 | 32 | curlx_free(pkt); |
636 | 32 | curlx_free(topic); |
637 | 32 | return result; |
638 | 26 | } |
639 | | |
640 | | static size_t mqtt_decode_len(unsigned char *buf, |
641 | | size_t buflen, size_t *lenbytes) |
642 | 22.6k | { |
643 | 22.6k | size_t len = 0; |
644 | 22.6k | size_t mult = 1; |
645 | 22.6k | size_t i; |
646 | 22.6k | unsigned char encoded = 128; |
647 | | |
648 | 46.2k | for(i = 0; (i < buflen) && (encoded & 128); i++) { |
649 | 23.6k | encoded = buf[i]; |
650 | 23.6k | len += (encoded & 127) * mult; |
651 | 23.6k | mult *= 128; |
652 | 23.6k | } |
653 | | |
654 | 22.6k | if(lenbytes) |
655 | 0 | *lenbytes = i; |
656 | | |
657 | 22.6k | return len; |
658 | 22.6k | } |
659 | | |
660 | | #ifdef DEBUGBUILD |
661 | | static const char *statenames[] = { |
662 | | "MQTT_FIRST", |
663 | | "MQTT_REMAINING_LENGTH", |
664 | | "MQTT_CONNACK", |
665 | | "MQTT_SUBACK", |
666 | | "MQTT_SUBACK_COMING", |
667 | | "MQTT_PUBWAIT", |
668 | | "MQTT_PUB_REMAIN", |
669 | | |
670 | | "NOT A STATE" |
671 | | }; |
672 | | #endif |
673 | | |
674 | | /* The only way to change state */ |
675 | | static void mqstate(struct Curl_easy *data, |
676 | | enum mqttstate state, |
677 | | enum mqttstate nextstate) /* used if state == FIRST */ |
678 | 49.6k | { |
679 | 49.6k | struct connectdata *conn = data->conn; |
680 | 49.6k | struct mqtt_conn *mqtt = Curl_conn_meta_get(conn, CURL_META_MQTT_CONN); |
681 | 49.6k | DEBUGASSERT(mqtt); |
682 | 49.6k | if(!mqtt) |
683 | 0 | return; |
684 | 49.6k | #ifdef DEBUGBUILD |
685 | 49.6k | infof(data, "%s (from %s) (next is %s)", |
686 | 49.6k | statenames[state], |
687 | 49.6k | statenames[mqtt->state], |
688 | 49.6k | (state == MQTT_FIRST) ? statenames[nextstate] : ""); |
689 | 49.6k | #endif |
690 | 49.6k | mqtt->state = state; |
691 | 49.6k | if(state == MQTT_FIRST) |
692 | 23.5k | mqtt->nextstate = nextstate; |
693 | 49.6k | } |
694 | | |
695 | | static CURLcode mqtt_read_publish(struct Curl_easy *data, bool *done) |
696 | 1.77k | { |
697 | 1.77k | CURLcode result = CURLE_OK; |
698 | 1.77k | struct connectdata *conn = data->conn; |
699 | 1.77k | size_t nread; |
700 | 1.77k | size_t remlen; |
701 | 1.77k | struct mqtt_conn *mqtt = Curl_conn_meta_get(conn, CURL_META_MQTT_CONN); |
702 | 1.77k | struct MQTT *mq = Curl_meta_get(data, CURL_META_MQTT_EASY); |
703 | 1.77k | unsigned char packet; |
704 | | |
705 | 1.77k | DEBUGASSERT(mqtt); |
706 | 1.77k | if(!mqtt || !mq) |
707 | 0 | return CURLE_FAILED_INIT; |
708 | | |
709 | 1.77k | switch(mqtt->state) { |
710 | 62 | MQTT_SUBACK_COMING: |
711 | 62 | case MQTT_SUBACK_COMING: |
712 | 62 | result = mqtt_verify_suback(data); |
713 | 62 | if(result) |
714 | 27 | break; |
715 | | |
716 | 35 | mqstate(data, MQTT_FIRST, MQTT_PUBWAIT); |
717 | 35 | break; |
718 | | |
719 | 104 | case MQTT_SUBACK: |
720 | 1.58k | case MQTT_PUBWAIT: |
721 | | /* we are expecting PUBLISH or SUBACK */ |
722 | 1.58k | packet = mq->firstbyte & 0xf0; |
723 | 1.58k | if(packet == MQTT_MSG_PUBLISH) |
724 | 1.49k | mqstate(data, MQTT_PUB_REMAIN, MQTT_NOSTATE); |
725 | 93 | else if(packet == MQTT_MSG_SUBACK) { |
726 | 62 | mqstate(data, MQTT_SUBACK_COMING, MQTT_NOSTATE); |
727 | 62 | goto MQTT_SUBACK_COMING; |
728 | 62 | } |
729 | 31 | else if(packet == MQTT_MSG_DISCONNECT) { |
730 | 8 | infof(data, "Got DISCONNECT"); |
731 | 8 | *done = TRUE; |
732 | 8 | goto end; |
733 | 8 | } |
734 | 23 | else { |
735 | 23 | result = CURLE_WEIRD_SERVER_REPLY; |
736 | 23 | goto end; |
737 | 23 | } |
738 | | |
739 | | /* -- switched state -- */ |
740 | 1.49k | remlen = mq->remaining_length; |
741 | 1.49k | infof(data, "Remaining length: %zu bytes", remlen); |
742 | 1.49k | if(data->set.max_filesize && |
743 | 374 | (curl_off_t)remlen > data->set.max_filesize) { |
744 | 16 | failf(data, "Maximum file size exceeded"); |
745 | 16 | result = CURLE_FILESIZE_EXCEEDED; |
746 | 16 | goto end; |
747 | 16 | } |
748 | 1.47k | Curl_pgrsSetDownloadSize(data, remlen); |
749 | 1.47k | data->req.bytecount = 0; |
750 | 1.47k | data->req.size = remlen; |
751 | 1.47k | mq->npacket = remlen; /* get this many bytes */ |
752 | 1.47k | FALLTHROUGH(); |
753 | 1.66k | case MQTT_PUB_REMAIN: { |
754 | | /* read rest of packet, but no more. Cap to buffer size */ |
755 | 1.66k | char buffer[4 * 1024]; |
756 | 1.66k | size_t rest = mq->npacket; |
757 | 1.66k | if(rest > sizeof(buffer)) |
758 | 154 | rest = sizeof(buffer); |
759 | 1.66k | result = Curl_xfer_recv(data, buffer, rest, &nread); |
760 | 1.66k | if(result) { |
761 | 1 | if(CURLE_AGAIN == result) { |
762 | 1 | infof(data, "EEEE AAAAGAIN"); |
763 | 1 | } |
764 | 1 | goto end; |
765 | 1 | } |
766 | 1.66k | if(!nread) { |
767 | 122 | infof(data, "server disconnected"); |
768 | 122 | result = CURLE_PARTIAL_FILE; |
769 | 122 | goto end; |
770 | 122 | } |
771 | | |
772 | | /* we received something */ |
773 | 1.53k | mq->lastTime = curlx_now(); |
774 | | |
775 | | /* if QoS is set, message contains packet id */ |
776 | 1.53k | result = Curl_client_write(data, CLIENTWRITE_BODY, buffer, nread); |
777 | 1.53k | if(result) |
778 | 1 | goto end; |
779 | | |
780 | 1.53k | mq->npacket -= nread; |
781 | 1.53k | if(!mq->npacket) |
782 | | /* no more PUBLISH payload, back to subscribe wait state */ |
783 | 1.35k | mqstate(data, MQTT_FIRST, MQTT_PUBWAIT); |
784 | 1.53k | break; |
785 | 1.53k | } |
786 | 0 | default: |
787 | 0 | DEBUGASSERT(NULL); /* illegal state */ |
788 | 0 | result = CURLE_WEIRD_SERVER_REPLY; |
789 | 0 | goto end; |
790 | 1.77k | } |
791 | 1.77k | end: |
792 | 1.77k | return result; |
793 | 1.77k | } |
794 | | |
795 | | static CURLcode mqtt_do(struct Curl_easy *data, bool *done) |
796 | 1.18k | { |
797 | 1.18k | struct MQTT *mq = Curl_meta_get(data, CURL_META_MQTT_EASY); |
798 | 1.18k | CURLcode result = CURLE_OK; |
799 | 1.18k | *done = FALSE; /* unconditionally */ |
800 | | |
801 | 1.18k | if(!mq) |
802 | 0 | return CURLE_FAILED_INIT; |
803 | 1.18k | mq->lastTime = curlx_now(); |
804 | 1.18k | mq->pingsent = FALSE; |
805 | | |
806 | 1.18k | result = mqtt_connect(data); |
807 | 1.18k | if(result) { |
808 | 7 | failf(data, "Error %d sending MQTT CONNECT request", result); |
809 | 7 | return result; |
810 | 7 | } |
811 | 1.17k | mqstate(data, MQTT_FIRST, MQTT_CONNACK); |
812 | 1.17k | return CURLE_OK; |
813 | 1.18k | } |
814 | | |
815 | | static CURLcode mqtt_done(struct Curl_easy *data, |
816 | | CURLcode status, bool premature) |
817 | 1.18k | { |
818 | 1.18k | struct MQTT *mq = Curl_meta_get(data, CURL_META_MQTT_EASY); |
819 | 1.18k | (void)status; |
820 | 1.18k | (void)premature; |
821 | 1.18k | if(mq) { |
822 | 1.18k | curlx_dyn_free(&mq->sendbuf); |
823 | 1.18k | curlx_dyn_free(&mq->recvbuf); |
824 | 1.18k | } |
825 | 1.18k | return CURLE_OK; |
826 | 1.18k | } |
827 | | |
828 | | /* we ping regularly to avoid being disconnected by the server */ |
829 | | static CURLcode mqtt_ping(struct Curl_easy *data) |
830 | 4.47M | { |
831 | 4.47M | struct MQTT *mq = Curl_meta_get(data, CURL_META_MQTT_EASY); |
832 | 4.47M | CURLcode result = CURLE_OK; |
833 | 4.47M | struct connectdata *conn = data->conn; |
834 | 4.47M | struct mqtt_conn *mqtt = Curl_conn_meta_get(conn, CURL_META_MQTT_CONN); |
835 | | |
836 | 4.47M | if(!mqtt || !mq) |
837 | 0 | return CURLE_FAILED_INIT; |
838 | | |
839 | 4.47M | if(mqtt->state == MQTT_FIRST && |
840 | 23.6k | !mq->pingsent && |
841 | 23.6k | data->set.upkeep_interval_ms > 0) { |
842 | 8.82k | struct curltime t = curlx_now(); |
843 | 8.82k | timediff_t diff = curlx_timediff_ms(t, mq->lastTime); |
844 | | |
845 | 8.82k | if(diff > data->set.upkeep_interval_ms) { |
846 | | /* 0xC0 is PINGREQ, and 0x00 is remaining length */ |
847 | 0 | unsigned char packet[2] = { 0xC0, 0x00 }; |
848 | 0 | size_t packetlen = sizeof(packet); |
849 | |
|
850 | 0 | result = mqtt_send(data, (char *)packet, packetlen); |
851 | 0 | if(!result) { |
852 | 0 | mq->pingsent = TRUE; |
853 | 0 | } |
854 | 0 | infof(data, "mqtt_ping: sent ping request."); |
855 | 0 | } |
856 | 8.82k | } |
857 | 4.47M | return result; |
858 | 4.47M | } |
859 | | |
860 | | static CURLcode mqtt_doing(struct Curl_easy *data, bool *done) |
861 | 4.47M | { |
862 | 4.47M | struct MQTT *mq = Curl_meta_get(data, CURL_META_MQTT_EASY); |
863 | 4.47M | CURLcode result = CURLE_OK; |
864 | 4.47M | size_t nread; |
865 | 4.47M | unsigned char recvbyte; |
866 | 4.47M | struct mqtt_conn *mqtt = Curl_conn_meta_get(data->conn, CURL_META_MQTT_CONN); |
867 | | |
868 | 4.47M | if(!mqtt || !mq) |
869 | 0 | return CURLE_FAILED_INIT; |
870 | | |
871 | 4.47M | *done = FALSE; |
872 | | |
873 | 4.47M | if(curlx_dyn_len(&mq->sendbuf)) { |
874 | | /* send the remainder of an outgoing packet */ |
875 | 0 | result = mqtt_send(data, curlx_dyn_ptr(&mq->sendbuf), |
876 | 0 | curlx_dyn_len(&mq->sendbuf)); |
877 | 0 | if(result) |
878 | 0 | return result; |
879 | 0 | } |
880 | | |
881 | 4.47M | result = mqtt_ping(data); |
882 | 4.47M | if(result) |
883 | 0 | return result; |
884 | | |
885 | 4.47M | infof(data, "mqtt_doing: state [%d]", (int)mqtt->state); |
886 | 4.47M | switch(mqtt->state) { |
887 | 23.6k | case MQTT_FIRST: |
888 | | /* Read the initial byte only */ |
889 | 23.6k | result = Curl_xfer_recv(data, (char *)&mq->firstbyte, 1, &nread); |
890 | 23.6k | if(result) |
891 | 348 | break; |
892 | 23.3k | else if(!nread) { |
893 | 698 | failf(data, "Connection disconnected"); |
894 | 698 | *done = TRUE; |
895 | 698 | result = CURLE_RECV_ERROR; |
896 | 698 | break; |
897 | 698 | } |
898 | 22.6k | Curl_debug(data, CURLINFO_HEADER_IN, (const char *)&mq->firstbyte, 1); |
899 | | |
900 | | /* we received something */ |
901 | 22.6k | mq->lastTime = curlx_now(); |
902 | | |
903 | | /* remember the first byte */ |
904 | 22.6k | mq->npacket = 0; |
905 | 22.6k | mqstate(data, MQTT_REMAINING_LENGTH, MQTT_NOSTATE); |
906 | 22.6k | FALLTHROUGH(); |
907 | 22.6k | case MQTT_REMAINING_LENGTH: |
908 | 23.7k | do { |
909 | 23.7k | result = Curl_xfer_recv(data, (char *)&recvbyte, 1, &nread); |
910 | 23.7k | if(result || !nread) |
911 | 97 | break; |
912 | 23.6k | Curl_debug(data, CURLINFO_HEADER_IN, (const char *)&recvbyte, 1); |
913 | 23.6k | mq->pkt_hd[mq->npacket++] = recvbyte; |
914 | 23.6k | } while((recvbyte & 0x80) && (mq->npacket < 4)); |
915 | 22.6k | if(!result && nread && (recvbyte & 0x80)) |
916 | | /* MQTT supports up to 127 * 128^0 + 127 * 128^1 + 127 * 128^2 + |
917 | | 127 * 128^3 bytes. server tried to send more */ |
918 | 5 | result = CURLE_WEIRD_SERVER_REPLY; |
919 | 22.6k | if(result) |
920 | 21 | break; |
921 | 22.6k | mq->remaining_length = mqtt_decode_len(mq->pkt_hd, mq->npacket, NULL); |
922 | 22.6k | mq->npacket = 0; |
923 | 22.6k | if(mq->remaining_length) { |
924 | 6.65k | mqstate(data, mqtt->nextstate, MQTT_NOSTATE); |
925 | 6.65k | break; |
926 | 6.65k | } |
927 | 15.9k | mqstate(data, MQTT_FIRST, MQTT_FIRST); |
928 | | |
929 | 15.9k | if(mq->firstbyte == MQTT_MSG_DISCONNECT) { |
930 | 9 | infof(data, "Got DISCONNECT"); |
931 | 9 | *done = TRUE; |
932 | 9 | } |
933 | | |
934 | | /* ping response */ |
935 | 15.9k | if(mq->firstbyte == MQTT_MSG_PINGRESP) { |
936 | 206 | infof(data, "Received ping response."); |
937 | 206 | mq->pingsent = FALSE; |
938 | 206 | mqstate(data, MQTT_FIRST, MQTT_PUBWAIT); |
939 | 206 | } |
940 | 15.9k | break; |
941 | 4.44M | case MQTT_CONNACK: |
942 | 4.44M | result = mqtt_verify_connack(data); |
943 | 4.44M | if(result) |
944 | 4.44M | break; |
945 | | |
946 | 156 | if(data->state.httpreq == HTTPREQ_POST) { |
947 | 33 | result = mqtt_publish(data); |
948 | 33 | if(!result) { |
949 | 26 | result = mqtt_disconnect(data); |
950 | 26 | *done = TRUE; |
951 | 26 | } |
952 | 33 | mqtt->nextstate = MQTT_FIRST; |
953 | 33 | } |
954 | 123 | else { |
955 | 123 | result = mqtt_subscribe(data); |
956 | 123 | if(!result) { |
957 | 121 | mqstate(data, MQTT_FIRST, MQTT_SUBACK); |
958 | 121 | } |
959 | 123 | } |
960 | 156 | break; |
961 | | |
962 | 104 | case MQTT_SUBACK: |
963 | 1.58k | case MQTT_PUBWAIT: |
964 | 1.77k | case MQTT_PUB_REMAIN: |
965 | 1.77k | result = mqtt_read_publish(data, done); |
966 | 1.77k | break; |
967 | | |
968 | 141 | default: |
969 | 141 | failf(data, "State not handled yet"); |
970 | 141 | *done = TRUE; |
971 | 141 | break; |
972 | 4.47M | } |
973 | | |
974 | 4.47M | if(result == CURLE_AGAIN) |
975 | 4.44M | result = CURLE_OK; |
976 | 4.47M | return result; |
977 | 4.47M | } |
978 | | |
979 | | #endif /* CURL_DISABLE_MQTT */ |