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