2020-04-14 05:19:12 -04:00
|
|
|
/***************************************************************************
|
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2021-01-08 11:58:15 -05:00
|
|
|
* Copyright (C) 2020 - 2021, Daniel Stenberg, <daniel@haxx.se>, et al.
|
2020-04-14 05:19:12 -04:00
|
|
|
* Copyright (C) 2019, Björn Stenberg, <bjorn@haxx.se>
|
|
|
|
*
|
|
|
|
* This software is licensed as described in the file COPYING, which
|
|
|
|
* you should have received as part of this distribution. The terms
|
2020-11-04 08:02:01 -05:00
|
|
|
* are also available at https://curl.se/docs/copyright.html.
|
2020-04-14 05:19:12 -04:00
|
|
|
*
|
|
|
|
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
|
|
|
|
* copies of the Software, and permit persons to whom the Software is
|
|
|
|
* furnished to do so, under the terms of the COPYING file.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#include "curl_setup.h"
|
|
|
|
|
2020-08-31 03:45:09 -04:00
|
|
|
#ifndef CURL_DISABLE_MQTT
|
2020-04-14 05:19:12 -04:00
|
|
|
|
|
|
|
#include "urldata.h"
|
|
|
|
#include <curl/curl.h>
|
|
|
|
#include "transfer.h"
|
|
|
|
#include "sendf.h"
|
|
|
|
#include "progress.h"
|
|
|
|
#include "mqtt.h"
|
|
|
|
#include "select.h"
|
|
|
|
#include "strdup.h"
|
|
|
|
#include "url.h"
|
|
|
|
#include "escape.h"
|
|
|
|
#include "warnless.h"
|
|
|
|
#include "curl_printf.h"
|
|
|
|
#include "curl_memory.h"
|
|
|
|
#include "multiif.h"
|
|
|
|
#include "rand.h"
|
|
|
|
|
|
|
|
/* The last #include file should be: */
|
|
|
|
#include "memdebug.h"
|
|
|
|
|
|
|
|
#define MQTT_MSG_CONNECT 0x10
|
|
|
|
#define MQTT_MSG_CONNACK 0x20
|
|
|
|
#define MQTT_MSG_PUBLISH 0x30
|
|
|
|
#define MQTT_MSG_SUBSCRIBE 0x82
|
|
|
|
#define MQTT_MSG_SUBACK 0x90
|
|
|
|
#define MQTT_MSG_DISCONNECT 0xe0
|
|
|
|
|
2020-04-16 07:20:52 -04:00
|
|
|
#define MQTT_CONNACK_LEN 2
|
|
|
|
#define MQTT_SUBACK_LEN 3
|
2020-04-14 05:19:12 -04:00
|
|
|
#define MQTT_CLIENTID_LEN 12 /* "curl0123abcd" */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Forward declarations.
|
|
|
|
*/
|
|
|
|
|
2021-01-08 11:58:15 -05:00
|
|
|
static CURLcode mqtt_do(struct Curl_easy *data, bool *done);
|
|
|
|
static CURLcode mqtt_doing(struct Curl_easy *data, bool *done);
|
|
|
|
static int mqtt_getsock(struct Curl_easy *data, struct connectdata *conn,
|
|
|
|
curl_socket_t *sock);
|
|
|
|
static CURLcode mqtt_setup_conn(struct Curl_easy *data,
|
|
|
|
struct connectdata *conn);
|
2020-04-14 05:19:12 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* MQTT protocol handler.
|
|
|
|
*/
|
|
|
|
|
|
|
|
const struct Curl_handler Curl_handler_mqtt = {
|
|
|
|
"MQTT", /* scheme */
|
|
|
|
mqtt_setup_conn, /* setup_connection */
|
|
|
|
mqtt_do, /* do_it */
|
|
|
|
ZERO_NULL, /* done */
|
|
|
|
ZERO_NULL, /* do_more */
|
|
|
|
ZERO_NULL, /* connect_it */
|
|
|
|
ZERO_NULL, /* connecting */
|
|
|
|
mqtt_doing, /* doing */
|
|
|
|
ZERO_NULL, /* proto_getsock */
|
|
|
|
mqtt_getsock, /* doing_getsock */
|
|
|
|
ZERO_NULL, /* domore_getsock */
|
|
|
|
ZERO_NULL, /* perform_getsock */
|
|
|
|
ZERO_NULL, /* disconnect */
|
|
|
|
ZERO_NULL, /* readwrite */
|
|
|
|
ZERO_NULL, /* connection_check */
|
|
|
|
PORT_MQTT, /* defport */
|
|
|
|
CURLPROTO_MQTT, /* protocol */
|
2020-09-21 07:45:24 -04:00
|
|
|
CURLPROTO_MQTT, /* family */
|
2020-04-14 05:19:12 -04:00
|
|
|
PROTOPT_NONE /* flags */
|
|
|
|
};
|
|
|
|
|
2021-01-08 11:58:15 -05:00
|
|
|
static CURLcode mqtt_setup_conn(struct Curl_easy *data,
|
|
|
|
struct connectdata *conn)
|
2020-04-14 05:19:12 -04:00
|
|
|
{
|
|
|
|
/* allocate the HTTP-specific struct for the Curl_easy, only to survive
|
|
|
|
during this request */
|
|
|
|
struct MQTT *mq;
|
2021-01-08 11:58:15 -05:00
|
|
|
(void)conn;
|
2020-11-23 02:32:41 -05:00
|
|
|
DEBUGASSERT(data->req.p.mqtt == NULL);
|
2020-04-14 05:19:12 -04:00
|
|
|
|
|
|
|
mq = calloc(1, sizeof(struct MQTT));
|
|
|
|
if(!mq)
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
2020-11-23 02:32:41 -05:00
|
|
|
data->req.p.mqtt = mq;
|
2020-04-14 05:19:12 -04:00
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
2021-01-08 11:58:15 -05:00
|
|
|
static CURLcode mqtt_send(struct Curl_easy *data,
|
2020-04-14 05:19:12 -04:00
|
|
|
char *buf, size_t len)
|
|
|
|
{
|
|
|
|
CURLcode result = CURLE_OK;
|
2021-01-08 11:58:15 -05:00
|
|
|
struct connectdata *conn = data->conn;
|
2020-04-14 05:19:12 -04:00
|
|
|
curl_socket_t sockfd = conn->sock[FIRSTSOCKET];
|
2020-11-23 02:32:41 -05:00
|
|
|
struct MQTT *mq = data->req.p.mqtt;
|
2020-04-14 05:19:12 -04:00
|
|
|
ssize_t n;
|
2021-01-08 11:58:15 -05:00
|
|
|
result = Curl_write(data, sockfd, buf, len, &n);
|
2020-11-02 11:34:04 -05:00
|
|
|
if(!result)
|
2020-04-14 05:19:12 -04:00
|
|
|
Curl_debug(data, CURLINFO_HEADER_OUT, buf, (size_t)n);
|
|
|
|
if(len != (size_t)n) {
|
|
|
|
size_t nsend = len - n;
|
|
|
|
char *sendleftovers = Curl_memdup(&buf[n], nsend);
|
|
|
|
if(!sendleftovers)
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
mq->sendleftovers = sendleftovers;
|
|
|
|
mq->nsend = nsend;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Generic function called by the multi interface to figure out what socket(s)
|
|
|
|
to wait for and for what actions during the DOING and PROTOCONNECT
|
|
|
|
states */
|
2021-01-08 11:58:15 -05:00
|
|
|
static int mqtt_getsock(struct Curl_easy *data,
|
|
|
|
struct connectdata *conn,
|
2020-04-14 05:19:12 -04:00
|
|
|
curl_socket_t *sock)
|
|
|
|
{
|
2021-01-08 11:58:15 -05:00
|
|
|
(void)data;
|
2020-04-14 05:19:12 -04:00
|
|
|
sock[0] = conn->sock[FIRSTSOCKET];
|
|
|
|
return GETSOCK_READSOCK(FIRSTSOCKET);
|
|
|
|
}
|
|
|
|
|
2021-01-08 11:58:15 -05:00
|
|
|
static CURLcode mqtt_connect(struct Curl_easy *data)
|
2020-04-14 05:19:12 -04:00
|
|
|
{
|
|
|
|
CURLcode result = CURLE_OK;
|
|
|
|
const size_t client_id_offset = 14;
|
|
|
|
const size_t packetlen = client_id_offset + MQTT_CLIENTID_LEN;
|
|
|
|
char client_id[MQTT_CLIENTID_LEN + 1] = "curl";
|
2020-09-02 06:46:48 -04:00
|
|
|
const size_t clen = strlen("curl");
|
2020-04-14 05:19:12 -04:00
|
|
|
char packet[32] = {
|
|
|
|
MQTT_MSG_CONNECT, /* packet type */
|
|
|
|
0x00, /* remaining length */
|
|
|
|
0x00, 0x04, /* protocol length */
|
|
|
|
'M','Q','T','T', /* protocol name */
|
|
|
|
0x04, /* protocol level */
|
|
|
|
0x02, /* CONNECT flag: CleanSession */
|
|
|
|
0x00, 0x3c, /* keep-alive 0 = disabled */
|
|
|
|
0x00, 0x00 /* payload1 length */
|
|
|
|
};
|
|
|
|
packet[1] = (packetlen - 2) & 0x7f;
|
|
|
|
packet[client_id_offset - 1] = MQTT_CLIENTID_LEN;
|
|
|
|
|
2021-01-08 11:58:15 -05:00
|
|
|
result = Curl_rand_hex(data, (unsigned char *)&client_id[clen],
|
2020-09-02 06:46:48 -04:00
|
|
|
MQTT_CLIENTID_LEN - clen + 1);
|
2020-04-14 05:19:12 -04:00
|
|
|
memcpy(&packet[client_id_offset], client_id, MQTT_CLIENTID_LEN);
|
2021-01-08 11:58:15 -05:00
|
|
|
infof(data, "Using client id '%s'\n", client_id);
|
2020-04-14 05:19:12 -04:00
|
|
|
if(!result)
|
2021-01-08 11:58:15 -05:00
|
|
|
result = mqtt_send(data, packet, packetlen);
|
2020-04-14 05:19:12 -04:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-01-08 11:58:15 -05:00
|
|
|
static CURLcode mqtt_disconnect(struct Curl_easy *data)
|
2020-04-14 05:19:12 -04:00
|
|
|
{
|
|
|
|
CURLcode result = CURLE_OK;
|
2021-01-08 11:58:15 -05:00
|
|
|
result = mqtt_send(data, (char *)"\xe0\x00", 2);
|
2020-04-14 05:19:12 -04:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-01-08 11:58:15 -05:00
|
|
|
static CURLcode mqtt_verify_connack(struct Curl_easy *data)
|
2020-04-14 05:19:12 -04:00
|
|
|
{
|
|
|
|
CURLcode result;
|
2021-01-08 11:58:15 -05:00
|
|
|
struct connectdata *conn = data->conn;
|
2020-04-14 05:19:12 -04:00
|
|
|
curl_socket_t sockfd = conn->sock[FIRSTSOCKET];
|
|
|
|
unsigned char readbuf[MQTT_CONNACK_LEN];
|
|
|
|
ssize_t nread;
|
|
|
|
|
2021-01-08 11:58:15 -05:00
|
|
|
result = Curl_read(data, sockfd, (char *)readbuf, MQTT_CONNACK_LEN, &nread);
|
2020-04-14 05:19:12 -04:00
|
|
|
if(result)
|
|
|
|
goto fail;
|
|
|
|
|
2020-11-02 11:34:04 -05:00
|
|
|
Curl_debug(data, CURLINFO_HEADER_IN, (char *)readbuf, (size_t)nread);
|
2020-04-14 05:19:12 -04:00
|
|
|
|
|
|
|
/* fixme */
|
|
|
|
if(nread < MQTT_CONNACK_LEN) {
|
|
|
|
result = CURLE_WEIRD_SERVER_REPLY;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* verify CONNACK */
|
2020-04-16 07:20:52 -04:00
|
|
|
if(readbuf[0] != 0x00 || readbuf[1] != 0x00) {
|
|
|
|
failf(data, "Expected %02x%02x but got %02x%02x",
|
|
|
|
0x00, 0x00, readbuf[0], readbuf[1]);
|
2020-04-14 05:19:12 -04:00
|
|
|
result = CURLE_WEIRD_SERVER_REPLY;
|
|
|
|
}
|
|
|
|
|
|
|
|
fail:
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-01-08 11:58:15 -05:00
|
|
|
static CURLcode mqtt_get_topic(struct Curl_easy *data,
|
2020-04-14 05:19:12 -04:00
|
|
|
char **topic, size_t *topiclen)
|
|
|
|
{
|
|
|
|
CURLcode result = CURLE_OK;
|
2021-01-08 11:58:15 -05:00
|
|
|
char *path = data->state.up.path;
|
2020-04-14 05:19:12 -04:00
|
|
|
|
|
|
|
if(strlen(path) > 1) {
|
2021-01-08 11:58:15 -05:00
|
|
|
result = Curl_urldecode(data, path + 1, 0, topic, topiclen,
|
2020-06-23 10:13:50 -04:00
|
|
|
REJECT_NADA);
|
2020-04-14 05:19:12 -04:00
|
|
|
}
|
|
|
|
else {
|
2021-01-08 11:58:15 -05:00
|
|
|
failf(data, "Error: No topic specified.");
|
2020-04-14 05:19:12 -04:00
|
|
|
result = CURLE_URL_MALFORMAT;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int mqtt_encode_len(char *buf, size_t len)
|
|
|
|
{
|
|
|
|
unsigned char encoded;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for(i = 0; (len > 0) && (i<4); i++) {
|
|
|
|
encoded = len % 0x80;
|
|
|
|
len /= 0x80;
|
|
|
|
if(len)
|
|
|
|
encoded |= 0x80;
|
|
|
|
buf[i] = encoded;
|
|
|
|
}
|
|
|
|
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2021-01-08 11:58:15 -05:00
|
|
|
static CURLcode mqtt_subscribe(struct Curl_easy *data)
|
2020-04-14 05:19:12 -04:00
|
|
|
{
|
|
|
|
CURLcode result = CURLE_OK;
|
|
|
|
char *topic = NULL;
|
|
|
|
size_t topiclen;
|
|
|
|
unsigned char *packet = NULL;
|
|
|
|
size_t packetlen;
|
|
|
|
char encodedsize[4];
|
|
|
|
size_t n;
|
2021-01-08 11:58:15 -05:00
|
|
|
struct connectdata *conn = data->conn;
|
2020-04-14 05:19:12 -04:00
|
|
|
|
2021-01-08 11:58:15 -05:00
|
|
|
result = mqtt_get_topic(data, &topic, &topiclen);
|
2020-04-14 05:19:12 -04:00
|
|
|
if(result)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
conn->proto.mqtt.packetid++;
|
|
|
|
|
|
|
|
packetlen = topiclen + 5; /* packetid + topic (has a two byte length field)
|
|
|
|
+ 2 bytes topic length + QoS byte */
|
|
|
|
n = mqtt_encode_len((char *)encodedsize, packetlen);
|
|
|
|
packetlen += n + 1; /* add one for the control packet type byte */
|
|
|
|
|
|
|
|
packet = malloc(packetlen);
|
|
|
|
if(!packet) {
|
|
|
|
result = CURLE_OUT_OF_MEMORY;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
packet[0] = MQTT_MSG_SUBSCRIBE;
|
|
|
|
memcpy(&packet[1], encodedsize, n);
|
|
|
|
packet[1 + n] = (conn->proto.mqtt.packetid >> 8) & 0xff;
|
|
|
|
packet[2 + n] = conn->proto.mqtt.packetid & 0xff;
|
|
|
|
packet[3 + n] = (topiclen >> 8) & 0xff;
|
|
|
|
packet[4 + n ] = topiclen & 0xff;
|
|
|
|
memcpy(&packet[5 + n], topic, topiclen);
|
|
|
|
packet[5 + n + topiclen] = 0; /* QoS zero */
|
|
|
|
|
2021-01-08 11:58:15 -05:00
|
|
|
result = mqtt_send(data, (char *)packet, packetlen);
|
2020-04-14 05:19:12 -04:00
|
|
|
|
|
|
|
fail:
|
|
|
|
free(topic);
|
|
|
|
free(packet);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-04-16 07:20:52 -04:00
|
|
|
/*
|
|
|
|
* Called when the first byte was already read.
|
|
|
|
*/
|
2021-01-08 11:58:15 -05:00
|
|
|
static CURLcode mqtt_verify_suback(struct Curl_easy *data)
|
2020-04-14 05:19:12 -04:00
|
|
|
{
|
|
|
|
CURLcode result;
|
2021-01-08 11:58:15 -05:00
|
|
|
struct connectdata *conn = data->conn;
|
2020-04-14 05:19:12 -04:00
|
|
|
curl_socket_t sockfd = conn->sock[FIRSTSOCKET];
|
|
|
|
unsigned char readbuf[MQTT_SUBACK_LEN];
|
|
|
|
ssize_t nread;
|
|
|
|
struct mqtt_conn *mqtt = &conn->proto.mqtt;
|
|
|
|
|
2021-01-08 11:58:15 -05:00
|
|
|
result = Curl_read(data, sockfd, (char *)readbuf, MQTT_SUBACK_LEN, &nread);
|
2020-04-14 05:19:12 -04:00
|
|
|
if(result)
|
|
|
|
goto fail;
|
|
|
|
|
2021-01-08 11:58:15 -05:00
|
|
|
Curl_debug(data, CURLINFO_HEADER_IN, (char *)readbuf, (size_t)nread);
|
2020-04-14 05:19:12 -04:00
|
|
|
|
|
|
|
/* fixme */
|
|
|
|
if(nread < MQTT_SUBACK_LEN) {
|
|
|
|
result = CURLE_WEIRD_SERVER_REPLY;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* verify SUBACK */
|
2020-04-16 07:20:52 -04:00
|
|
|
if(readbuf[0] != ((mqtt->packetid >> 8) & 0xff) ||
|
|
|
|
readbuf[1] != (mqtt->packetid & 0xff) ||
|
|
|
|
readbuf[2] != 0x00)
|
2020-04-14 05:19:12 -04:00
|
|
|
result = CURLE_WEIRD_SERVER_REPLY;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-01-08 11:58:15 -05:00
|
|
|
static CURLcode mqtt_publish(struct Curl_easy *data)
|
2020-04-14 05:19:12 -04:00
|
|
|
{
|
|
|
|
CURLcode result;
|
2021-01-08 11:58:15 -05:00
|
|
|
char *payload = data->set.postfields;
|
2020-12-17 07:34:38 -05:00
|
|
|
size_t payloadlen;
|
2020-04-14 05:19:12 -04:00
|
|
|
char *topic = NULL;
|
|
|
|
size_t topiclen;
|
|
|
|
unsigned char *pkt = NULL;
|
|
|
|
size_t i = 0;
|
|
|
|
size_t remaininglength;
|
|
|
|
size_t encodelen;
|
|
|
|
char encodedbytes[4];
|
2021-01-08 11:58:15 -05:00
|
|
|
curl_off_t postfieldsize = data->set.postfieldsize;
|
2020-12-17 07:34:38 -05:00
|
|
|
|
|
|
|
if(!payload)
|
|
|
|
return CURLE_BAD_FUNCTION_ARGUMENT;
|
|
|
|
if(postfieldsize < 0)
|
|
|
|
payloadlen = strlen(payload);
|
|
|
|
else
|
|
|
|
payloadlen = (size_t)postfieldsize;
|
2020-04-14 05:19:12 -04:00
|
|
|
|
2021-01-08 11:58:15 -05:00
|
|
|
result = mqtt_get_topic(data, &topic, &topiclen);
|
2020-04-14 05:19:12 -04:00
|
|
|
if(result)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
remaininglength = payloadlen + 2 + topiclen;
|
|
|
|
encodelen = mqtt_encode_len(encodedbytes, remaininglength);
|
|
|
|
|
|
|
|
/* add the control byte and the encoded remaining length */
|
|
|
|
pkt = malloc(remaininglength + 1 + encodelen);
|
|
|
|
if(!pkt) {
|
|
|
|
result = CURLE_OUT_OF_MEMORY;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* assemble packet */
|
|
|
|
pkt[i++] = MQTT_MSG_PUBLISH;
|
|
|
|
memcpy(&pkt[i], encodedbytes, encodelen);
|
|
|
|
i += encodelen;
|
|
|
|
pkt[i++] = (topiclen >> 8) & 0xff;
|
|
|
|
pkt[i++] = (topiclen & 0xff);
|
|
|
|
memcpy(&pkt[i], topic, topiclen);
|
|
|
|
i += topiclen;
|
|
|
|
memcpy(&pkt[i], payload, payloadlen);
|
|
|
|
i += payloadlen;
|
2021-01-08 11:58:15 -05:00
|
|
|
result = mqtt_send(data, (char *)pkt, i);
|
2020-04-14 05:19:12 -04:00
|
|
|
|
|
|
|
fail:
|
|
|
|
free(pkt);
|
|
|
|
free(topic);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t mqtt_decode_len(unsigned char *buf,
|
|
|
|
size_t buflen, size_t *lenbytes)
|
|
|
|
{
|
|
|
|
size_t len = 0;
|
|
|
|
size_t mult = 1;
|
|
|
|
size_t i;
|
|
|
|
unsigned char encoded = 128;
|
|
|
|
|
|
|
|
for(i = 0; (i < buflen) && (encoded & 128); i++) {
|
|
|
|
encoded = buf[i];
|
|
|
|
len += (encoded & 127) * mult;
|
|
|
|
mult *= 128;
|
|
|
|
}
|
|
|
|
|
2020-04-16 07:20:52 -04:00
|
|
|
if(lenbytes)
|
|
|
|
*lenbytes = i;
|
2020-04-14 05:19:12 -04:00
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2020-04-16 07:20:52 -04:00
|
|
|
#ifdef CURLDEBUG
|
|
|
|
static const char *statenames[]={
|
|
|
|
"MQTT_FIRST",
|
|
|
|
"MQTT_REMAINING_LENGTH",
|
|
|
|
"MQTT_CONNACK",
|
|
|
|
"MQTT_SUBACK",
|
|
|
|
"MQTT_SUBACK_COMING",
|
|
|
|
"MQTT_PUBWAIT",
|
2020-04-20 17:27:04 -04:00
|
|
|
"MQTT_PUB_REMAIN",
|
|
|
|
|
|
|
|
"NOT A STATE"
|
2020-04-16 07:20:52 -04:00
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* The only way to change state */
|
2021-01-08 11:58:15 -05:00
|
|
|
static void mqstate(struct Curl_easy *data,
|
2020-04-16 07:20:52 -04:00
|
|
|
enum mqttstate state,
|
|
|
|
enum mqttstate nextstate) /* used if state == FIRST */
|
|
|
|
{
|
2021-01-08 11:58:15 -05:00
|
|
|
struct connectdata *conn = data->conn;
|
2020-04-16 07:20:52 -04:00
|
|
|
struct mqtt_conn *mqtt = &conn->proto.mqtt;
|
|
|
|
#ifdef CURLDEBUG
|
2021-01-08 11:58:15 -05:00
|
|
|
infof(data, "%s (from %s) (next is %s)\n",
|
2020-04-16 07:20:52 -04:00
|
|
|
statenames[state],
|
|
|
|
statenames[mqtt->state],
|
|
|
|
(state == MQTT_FIRST)? statenames[nextstate] : "");
|
|
|
|
#endif
|
|
|
|
mqtt->state = state;
|
|
|
|
if(state == MQTT_FIRST)
|
|
|
|
mqtt->nextstate = nextstate;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-04-14 05:19:12 -04:00
|
|
|
/* for the publish packet */
|
|
|
|
#define MQTT_HEADER_LEN 5 /* max 5 bytes */
|
|
|
|
|
2021-01-08 11:58:15 -05:00
|
|
|
static CURLcode mqtt_read_publish(struct Curl_easy *data, bool *done)
|
2020-04-14 05:19:12 -04:00
|
|
|
{
|
2020-04-16 07:20:52 -04:00
|
|
|
CURLcode result = CURLE_OK;
|
2021-01-08 11:58:15 -05:00
|
|
|
struct connectdata *conn = data->conn;
|
2020-04-14 05:19:12 -04:00
|
|
|
curl_socket_t sockfd = conn->sock[FIRSTSOCKET];
|
|
|
|
ssize_t nread;
|
|
|
|
unsigned char *pkt = (unsigned char *)data->state.buffer;
|
2020-04-16 07:20:52 -04:00
|
|
|
size_t remlen;
|
2020-04-14 05:19:12 -04:00
|
|
|
struct mqtt_conn *mqtt = &conn->proto.mqtt;
|
2020-11-23 02:32:41 -05:00
|
|
|
struct MQTT *mq = data->req.p.mqtt;
|
2020-04-16 07:20:52 -04:00
|
|
|
unsigned char packet;
|
2020-04-14 05:19:12 -04:00
|
|
|
|
|
|
|
switch(mqtt->state) {
|
2020-04-16 07:20:52 -04:00
|
|
|
MQTT_SUBACK_COMING:
|
|
|
|
case MQTT_SUBACK_COMING:
|
2021-01-08 11:58:15 -05:00
|
|
|
result = mqtt_verify_suback(data);
|
2020-04-14 05:19:12 -04:00
|
|
|
if(result)
|
2020-04-16 07:20:52 -04:00
|
|
|
break;
|
|
|
|
|
2021-01-08 11:58:15 -05:00
|
|
|
mqstate(data, MQTT_FIRST, MQTT_PUBWAIT);
|
2020-04-16 07:20:52 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case MQTT_SUBACK:
|
|
|
|
case MQTT_PUBWAIT:
|
|
|
|
/* we are expecting PUBLISH or SUBACK */
|
|
|
|
packet = mq->firstbyte & 0xf0;
|
|
|
|
if(packet == MQTT_MSG_PUBLISH)
|
2021-01-08 11:58:15 -05:00
|
|
|
mqstate(data, MQTT_PUB_REMAIN, MQTT_NOSTATE);
|
2020-04-16 07:20:52 -04:00
|
|
|
else if(packet == MQTT_MSG_SUBACK) {
|
2021-01-08 11:58:15 -05:00
|
|
|
mqstate(data, MQTT_SUBACK_COMING, MQTT_NOSTATE);
|
2020-04-16 07:20:52 -04:00
|
|
|
goto MQTT_SUBACK_COMING;
|
|
|
|
}
|
|
|
|
else if(packet == MQTT_MSG_DISCONNECT) {
|
|
|
|
infof(data, "Got DISCONNECT\n");
|
|
|
|
*done = TRUE;
|
2020-04-14 05:19:12 -04:00
|
|
|
goto end;
|
2020-04-16 07:20:52 -04:00
|
|
|
}
|
|
|
|
else {
|
2020-04-14 05:19:12 -04:00
|
|
|
result = CURLE_WEIRD_SERVER_REPLY;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* -- switched state -- */
|
2020-04-16 07:20:52 -04:00
|
|
|
remlen = mq->remaining_length;
|
2020-04-14 05:19:12 -04:00
|
|
|
infof(data, "Remaining length: %zd bytes\n", remlen);
|
|
|
|
Curl_pgrsSetDownloadSize(data, remlen);
|
|
|
|
data->req.bytecount = 0;
|
|
|
|
data->req.size = remlen;
|
|
|
|
mq->npacket = remlen; /* get this many bytes */
|
|
|
|
/* FALLTHROUGH */
|
2020-04-16 07:20:52 -04:00
|
|
|
case MQTT_PUB_REMAIN: {
|
2020-04-14 05:19:12 -04:00
|
|
|
/* read rest of packet, but no more. Cap to buffer size */
|
|
|
|
struct SingleRequest *k = &data->req;
|
|
|
|
size_t rest = mq->npacket;
|
|
|
|
if(rest > (size_t)data->set.buffer_size)
|
|
|
|
rest = (size_t)data->set.buffer_size;
|
2021-01-08 11:58:15 -05:00
|
|
|
result = Curl_read(data, sockfd, (char *)pkt, rest, &nread);
|
2020-04-14 05:19:12 -04:00
|
|
|
if(result) {
|
|
|
|
if(CURLE_AGAIN == result) {
|
|
|
|
infof(data, "EEEE AAAAGAIN\n");
|
|
|
|
}
|
|
|
|
goto end;
|
|
|
|
}
|
2020-04-16 07:20:52 -04:00
|
|
|
if(!nread) {
|
|
|
|
infof(data, "server disconnected\n");
|
|
|
|
result = CURLE_PARTIAL_FILE;
|
|
|
|
goto end;
|
|
|
|
}
|
2020-11-02 11:34:04 -05:00
|
|
|
Curl_debug(data, CURLINFO_DATA_IN, (char *)pkt, (size_t)nread);
|
2020-04-14 05:19:12 -04:00
|
|
|
|
|
|
|
mq->npacket -= nread;
|
|
|
|
k->bytecount += nread;
|
|
|
|
Curl_pgrsSetDownloadCounter(data, k->bytecount);
|
|
|
|
|
|
|
|
/* if QoS is set, message contains packet id */
|
|
|
|
|
2021-01-08 11:58:15 -05:00
|
|
|
result = Curl_client_write(data, CLIENTWRITE_BODY, (char *)pkt, nread);
|
2020-04-14 05:19:12 -04:00
|
|
|
if(result)
|
|
|
|
goto end;
|
|
|
|
|
|
|
|
if(!mq->npacket)
|
|
|
|
/* no more PUBLISH payload, back to subscribe wait state */
|
2021-01-08 11:58:15 -05:00
|
|
|
mqstate(data, MQTT_FIRST, MQTT_PUBWAIT);
|
2020-04-14 05:19:12 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
DEBUGASSERT(NULL); /* illegal state */
|
|
|
|
result = CURLE_WEIRD_SERVER_REPLY;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
end:
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-01-08 11:58:15 -05:00
|
|
|
static CURLcode mqtt_do(struct Curl_easy *data, bool *done)
|
2020-04-14 05:19:12 -04:00
|
|
|
{
|
|
|
|
CURLcode result = CURLE_OK;
|
|
|
|
*done = FALSE; /* unconditionally */
|
|
|
|
|
2021-01-08 11:58:15 -05:00
|
|
|
result = mqtt_connect(data);
|
2020-04-14 05:19:12 -04:00
|
|
|
if(result) {
|
|
|
|
failf(data, "Error %d sending MQTT CONN request", result);
|
|
|
|
return result;
|
|
|
|
}
|
2021-01-08 11:58:15 -05:00
|
|
|
mqstate(data, MQTT_FIRST, MQTT_CONNACK);
|
2020-04-14 05:19:12 -04:00
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
2021-01-08 11:58:15 -05:00
|
|
|
static CURLcode mqtt_doing(struct Curl_easy *data, bool *done)
|
2020-04-14 05:19:12 -04:00
|
|
|
{
|
|
|
|
CURLcode result = CURLE_OK;
|
2021-01-08 11:58:15 -05:00
|
|
|
struct connectdata *conn = data->conn;
|
2020-04-14 05:19:12 -04:00
|
|
|
struct mqtt_conn *mqtt = &conn->proto.mqtt;
|
2020-11-23 02:32:41 -05:00
|
|
|
struct MQTT *mq = data->req.p.mqtt;
|
2020-04-16 07:20:52 -04:00
|
|
|
ssize_t nread;
|
|
|
|
curl_socket_t sockfd = conn->sock[FIRSTSOCKET];
|
|
|
|
unsigned char *pkt = (unsigned char *)data->state.buffer;
|
|
|
|
unsigned char byte;
|
2020-04-14 05:19:12 -04:00
|
|
|
|
|
|
|
*done = FALSE;
|
|
|
|
|
|
|
|
if(mq->nsend) {
|
|
|
|
/* send the remainder of an outgoing packet */
|
|
|
|
char *ptr = mq->sendleftovers;
|
2021-01-08 11:58:15 -05:00
|
|
|
result = mqtt_send(data, mq->sendleftovers, mq->nsend);
|
2020-04-14 05:19:12 -04:00
|
|
|
free(ptr);
|
|
|
|
if(result)
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-04-16 07:20:52 -04:00
|
|
|
infof(data, "mqtt_doing: state [%d]\n", (int) mqtt->state);
|
2020-04-14 05:19:12 -04:00
|
|
|
switch(mqtt->state) {
|
2020-04-16 07:20:52 -04:00
|
|
|
case MQTT_FIRST:
|
|
|
|
/* Read the initial byte only */
|
2021-01-08 11:58:15 -05:00
|
|
|
result = Curl_read(data, sockfd, (char *)&mq->firstbyte, 1, &nread);
|
2020-12-15 02:51:33 -05:00
|
|
|
if(!nread)
|
2020-04-16 07:20:52 -04:00
|
|
|
break;
|
2020-11-02 11:34:04 -05:00
|
|
|
Curl_debug(data, CURLINFO_HEADER_IN, (char *)&mq->firstbyte, 1);
|
2020-04-16 07:20:52 -04:00
|
|
|
/* remember the first byte */
|
|
|
|
mq->npacket = 0;
|
2021-01-08 11:58:15 -05:00
|
|
|
mqstate(data, MQTT_REMAINING_LENGTH, MQTT_NOSTATE);
|
2020-04-16 07:20:52 -04:00
|
|
|
/* FALLTHROUGH */
|
|
|
|
case MQTT_REMAINING_LENGTH:
|
|
|
|
do {
|
2021-01-08 11:58:15 -05:00
|
|
|
result = Curl_read(data, sockfd, (char *)&byte, 1, &nread);
|
2020-12-15 02:51:33 -05:00
|
|
|
if(!nread)
|
2020-04-16 07:20:52 -04:00
|
|
|
break;
|
2020-11-02 11:34:04 -05:00
|
|
|
Curl_debug(data, CURLINFO_HEADER_IN, (char *)&byte, 1);
|
2020-04-16 07:20:52 -04:00
|
|
|
pkt[mq->npacket++] = byte;
|
|
|
|
} while((byte & 0x80) && (mq->npacket < 4));
|
2020-04-20 11:03:18 -04:00
|
|
|
if(result)
|
|
|
|
break;
|
2020-04-16 07:20:52 -04:00
|
|
|
mq->remaining_length = mqtt_decode_len(&pkt[0], mq->npacket, NULL);
|
|
|
|
mq->npacket = 0;
|
|
|
|
if(mq->remaining_length) {
|
2021-01-08 11:58:15 -05:00
|
|
|
mqstate(data, mqtt->nextstate, MQTT_NOSTATE);
|
2020-04-16 07:20:52 -04:00
|
|
|
break;
|
|
|
|
}
|
2021-01-08 11:58:15 -05:00
|
|
|
mqstate(data, MQTT_FIRST, MQTT_FIRST);
|
2020-04-16 07:20:52 -04:00
|
|
|
|
|
|
|
if(mq->firstbyte == MQTT_MSG_DISCONNECT) {
|
|
|
|
infof(data, "Got DISCONNECT\n");
|
|
|
|
*done = TRUE;
|
|
|
|
}
|
|
|
|
break;
|
2020-04-14 05:19:12 -04:00
|
|
|
case MQTT_CONNACK:
|
2021-01-08 11:58:15 -05:00
|
|
|
result = mqtt_verify_connack(data);
|
2020-04-14 05:19:12 -04:00
|
|
|
if(result)
|
|
|
|
break;
|
|
|
|
|
2021-01-08 11:58:15 -05:00
|
|
|
if(data->state.httpreq == HTTPREQ_POST) {
|
|
|
|
result = mqtt_publish(data);
|
2020-04-14 05:19:12 -04:00
|
|
|
if(!result) {
|
2021-01-08 11:58:15 -05:00
|
|
|
result = mqtt_disconnect(data);
|
2020-04-14 05:19:12 -04:00
|
|
|
*done = TRUE;
|
|
|
|
}
|
2020-04-16 07:20:52 -04:00
|
|
|
mqtt->nextstate = MQTT_FIRST;
|
2020-04-14 05:19:12 -04:00
|
|
|
}
|
|
|
|
else {
|
2021-01-08 11:58:15 -05:00
|
|
|
result = mqtt_subscribe(data);
|
2020-04-16 07:20:52 -04:00
|
|
|
if(!result) {
|
2021-01-08 11:58:15 -05:00
|
|
|
mqstate(data, MQTT_FIRST, MQTT_SUBACK);
|
2020-04-16 07:20:52 -04:00
|
|
|
}
|
2020-04-14 05:19:12 -04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MQTT_SUBACK:
|
2020-04-16 07:20:52 -04:00
|
|
|
case MQTT_PUBWAIT:
|
|
|
|
case MQTT_PUB_REMAIN:
|
2021-01-08 11:58:15 -05:00
|
|
|
result = mqtt_read_publish(data, done);
|
2020-04-14 05:19:12 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2021-01-08 11:58:15 -05:00
|
|
|
failf(data, "State not handled yet");
|
2020-04-14 05:19:12 -04:00
|
|
|
*done = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(result == CURLE_AGAIN)
|
|
|
|
result = CURLE_OK;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-08-31 03:45:09 -04:00
|
|
|
#endif /* CURL_DISABLE_MQTT */
|