Update Files
This commit is contained in:
48
Kinc/Sources/kinc/libs/core/mqtt/CMakeLists.txt
Normal file
48
Kinc/Sources/kinc/libs/core/mqtt/CMakeLists.txt
Normal file
@ -0,0 +1,48 @@
|
||||
#
|
||||
# libwebsockets - small server side websockets and web server implementation
|
||||
#
|
||||
# Copyright (C) 2010 - 2020 Andy Green <andy@warmcat.com>
|
||||
#
|
||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
# of this software and associated documentation files (the "Software"), to
|
||||
# deal in the Software without restriction, including without limitation the
|
||||
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
||||
# sell copies of the Software, and to permit persons to whom the Software is
|
||||
# furnished to do so, subject to the following conditions:
|
||||
#
|
||||
# The above copyright notice and this permission notice shall be included in
|
||||
# all copies or substantial portions of the Software.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||
# IN THE SOFTWARE.
|
||||
#
|
||||
# The strategy is to only export to PARENT_SCOPE
|
||||
#
|
||||
# - changes to LIB_LIST
|
||||
# - changes to SOURCES
|
||||
# - includes via include_directories
|
||||
#
|
||||
# and keep everything else private
|
||||
|
||||
include_directories(.)
|
||||
|
||||
if (LWS_WITH_CLIENT)
|
||||
list(APPEND SOURCES
|
||||
roles/mqtt/mqtt.c
|
||||
roles/mqtt/ops-mqtt.c
|
||||
roles/mqtt/primitives.c
|
||||
roles/mqtt/client/client-mqtt.c
|
||||
roles/mqtt/client/client-mqtt-handshake.c
|
||||
)
|
||||
endif()
|
||||
|
||||
#
|
||||
# Keep explicit parent scope exports at end
|
||||
#
|
||||
|
||||
exports_to_parent_scope()
|
200
Kinc/Sources/kinc/libs/core/mqtt/client/client-mqtt-handshake.c
Normal file
200
Kinc/Sources/kinc/libs/core/mqtt/client/client-mqtt-handshake.c
Normal file
@ -0,0 +1,200 @@
|
||||
/*
|
||||
* libwebsockets - small server side websockets and web server implementation
|
||||
*
|
||||
* Copyright (C) 2010 - 2020 Andy Green <andy@warmcat.com>
|
||||
* Sakthi Kannan <saktr@amazon.com>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to
|
||||
* deal in the Software without restriction, including without limitation the
|
||||
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
||||
* sell copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||
* IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <private-lib-core.h>
|
||||
|
||||
#define MQTT_CONNECT_MSG_BASE_LEN (12)
|
||||
|
||||
struct lws *
|
||||
lws_mqtt_client_send_connect(struct lws *wsi)
|
||||
{
|
||||
/* static int */
|
||||
/* lws_mqttc_abs_writeable(lws_abs_protocol_inst_t *api, size_t budget) */
|
||||
const lws_mqttc_t *c = &wsi->mqtt->client;
|
||||
uint8_t b[256 + LWS_PRE], *start = b + LWS_PRE, *p = start;
|
||||
unsigned int len = MQTT_CONNECT_MSG_BASE_LEN;
|
||||
|
||||
switch (lwsi_state(wsi)) {
|
||||
case LRS_MQTTC_IDLE:
|
||||
/*
|
||||
* Transport connected - this is our chance to do the
|
||||
* protocol connect action.
|
||||
*/
|
||||
|
||||
/* 1. Fixed Headers */
|
||||
if (lws_mqtt_fill_fixed_header(p++, LMQCP_CTOS_CONNECT, 0, 0, 0)) {
|
||||
lwsl_err("%s: Failled to fill fixed header\n", __func__);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/*
|
||||
* 2. Remaining length - Add the lengths of client ID,
|
||||
* username and password and their length fields if
|
||||
* the respective flags are set.
|
||||
*/
|
||||
len += c->id->len;
|
||||
if (c->conn_flags & LMQCFT_USERNAME && c->username) {
|
||||
len = len + (unsigned int)c->username->len + 2;
|
||||
if (c->conn_flags & LMQCFT_PASSWORD)
|
||||
len += (unsigned int)(c->password ? c->password->len : 0) + 2u;
|
||||
}
|
||||
if (c->conn_flags & LMQCFT_WILL_FLAG && c->will.topic) {
|
||||
len = len + (unsigned int)c->will.topic->len + 2;
|
||||
len += (c->will.message ? c->will.message->len : 0) + 2u;
|
||||
}
|
||||
p += lws_mqtt_vbi_encode(len, p);
|
||||
|
||||
/*
|
||||
* 3. Variable Header - Protocol name & level, Connect
|
||||
* flags and keep alive time (in secs).
|
||||
*/
|
||||
lws_ser_wu16be(p, 4); /* Length of protocol name */
|
||||
p += 2;
|
||||
*p++ = 'M';
|
||||
*p++ = 'Q';
|
||||
*p++ = 'T';
|
||||
*p++ = 'T';
|
||||
*p++ = MQTT_VER_3_1_1;
|
||||
*p++ = (uint8_t)c->conn_flags;
|
||||
lws_ser_wu16be(p, c->keep_alive_secs);
|
||||
p += 2;
|
||||
|
||||
/*
|
||||
* 4. Payload - Client ID, Will topic & message,
|
||||
* Username & password.
|
||||
*/
|
||||
if (lws_mqtt_str_is_not_empty(c->id)) {
|
||||
lws_ser_wu16be(p, c->id->len);
|
||||
p += 2;
|
||||
memcpy(p, c->id->buf, c->id->len);
|
||||
p += c->id->len;
|
||||
} else {
|
||||
/*
|
||||
* If the Client supplies a zero-byte
|
||||
* ClientId, the Client MUST also set
|
||||
* CleanSession to 1 [MQTT-3.1.3-7].
|
||||
*/
|
||||
if (!(c->conn_flags & LMQCFT_CLEAN_START)) {
|
||||
lwsl_err("%s: Empty client ID needs a clean start\n",
|
||||
__func__);
|
||||
return NULL;
|
||||
}
|
||||
*p++ = 0;
|
||||
}
|
||||
|
||||
if (c->conn_flags & LMQCFT_WILL_FLAG) {
|
||||
if (lws_mqtt_str_is_not_empty(c->will.topic)) {
|
||||
lws_ser_wu16be(p, c->will.topic->len);
|
||||
p += 2;
|
||||
memcpy(p, c->will.topic->buf, c->will.topic->len);
|
||||
p += c->will.topic->len;
|
||||
if (lws_mqtt_str_is_not_empty(c->will.message)) {
|
||||
lws_ser_wu16be(p, c->will.message->len);
|
||||
p += 2;
|
||||
memcpy(p, c->will.message->buf,
|
||||
c->will.message->len);
|
||||
p += c->will.message->len;
|
||||
} else {
|
||||
lws_ser_wu16be(p, 0);
|
||||
p += 2;
|
||||
}
|
||||
} else {
|
||||
lwsl_err("%s: Missing Will Topic\n", __func__);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
if (c->conn_flags & LMQCFT_USERNAME) {
|
||||
/*
|
||||
* Detailed sanity check on the username and
|
||||
* password strings.
|
||||
*/
|
||||
if (lws_mqtt_str_is_not_empty(c->username)) {
|
||||
lws_ser_wu16be(p, c->username->len);
|
||||
p += 2;
|
||||
memcpy(p, c->username->buf, c->username->len);
|
||||
p += c->username->len;
|
||||
} else {
|
||||
lwsl_err("%s: Empty / missing Username!\n",
|
||||
__func__);
|
||||
return NULL;
|
||||
}
|
||||
if (c->conn_flags & LMQCFT_PASSWORD) {
|
||||
if (lws_mqtt_str_is_not_empty(c->password)) {
|
||||
lws_ser_wu16be(p, c->password->len);
|
||||
p += 2;
|
||||
memcpy(p, c->password->buf,
|
||||
c->password->len);
|
||||
p += c->password->len;
|
||||
} else {
|
||||
lws_ser_wu16be(p, 0);
|
||||
p += 2;
|
||||
}
|
||||
}
|
||||
} else if (c->conn_flags & LMQCFT_PASSWORD) {
|
||||
lwsl_err("%s: Unsupported - Password without username\n",
|
||||
__func__);
|
||||
return NULL;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
lwsl_err("%s: unexpected state %d\n", __func__, lwsi_state(wsi));
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/*
|
||||
* Perform the actual write
|
||||
*/
|
||||
if (lws_write(wsi, (unsigned char *)&b[LWS_PRE], lws_ptr_diff_size_t(p, start),
|
||||
LWS_WRITE_BINARY) != lws_ptr_diff(p, start)) {
|
||||
lwsl_notice("%s: write failed\n", __func__);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return wsi;
|
||||
}
|
||||
|
||||
struct lws *
|
||||
lws_mqtt_client_send_disconnect(struct lws *wsi)
|
||||
{
|
||||
uint8_t b[256 + LWS_PRE], *start = b + LWS_PRE, *p = start;
|
||||
|
||||
/* 1. Fixed Headers */
|
||||
if (lws_mqtt_fill_fixed_header(p++, LMQCP_DISCONNECT, 0, 0, 0))
|
||||
{
|
||||
lwsl_err("%s: Failled to fill fixed header\n", __func__);
|
||||
return NULL;
|
||||
}
|
||||
*p++ = 0;
|
||||
if (lws_write(wsi, (unsigned char *)&b[LWS_PRE], lws_ptr_diff_size_t(p, start),
|
||||
LWS_WRITE_BINARY) != lws_ptr_diff(p, start)) {
|
||||
lwsl_err("%s: write failed\n", __func__);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return wsi;
|
||||
}
|
375
Kinc/Sources/kinc/libs/core/mqtt/client/client-mqtt.c
Normal file
375
Kinc/Sources/kinc/libs/core/mqtt/client/client-mqtt.c
Normal file
@ -0,0 +1,375 @@
|
||||
/*
|
||||
* libwebsockets - small server side websockets and web server implementation
|
||||
*
|
||||
* Copyright (C) 2010 - 2020 Andy Green <andy@warmcat.com>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to
|
||||
* deal in the Software without restriction, including without limitation the
|
||||
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
||||
* sell copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||
* IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
/*
|
||||
* You can leave buf NULL, if so it will be allocated on the heap once the
|
||||
* actual length is known. nf should be 0, it will be set at allocation time.
|
||||
*
|
||||
* Or you can ensure no allocation and use an external buffer by setting buf
|
||||
* and lim. But buf must be in the ep context somehow, since it may have to
|
||||
* survive returns to the event loop unchanged. Set nf to 0 in this case.
|
||||
*
|
||||
* Or you can set buf to an externally allocated buffer, in which case you may
|
||||
* set nf so it will be freed when the string is "freed".
|
||||
*/
|
||||
|
||||
#include "private-lib-core.h"
|
||||
/* #include "lws-mqtt.h" */
|
||||
/* 3.1.3.1-5: MUST allow... that contain only the characters... */
|
||||
|
||||
static const uint8_t *code = (const uint8_t *)
|
||||
"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||
|
||||
static int
|
||||
lws_mqtt_generate_id(struct lws* wsi, lws_mqtt_str_t **ms, const char *client_id)
|
||||
{
|
||||
struct lws_context *context = wsi->a.context;
|
||||
uint16_t ran[24]; /* 16-bit so wrap bias from %62 diluted by ~1000 */
|
||||
size_t n, len;
|
||||
uint8_t *buf;
|
||||
|
||||
if (client_id)
|
||||
len = strlen(client_id);
|
||||
else
|
||||
len = LWS_MQTT_RANDOM_CIDLEN;
|
||||
|
||||
*ms = lws_mqtt_str_create((uint16_t)(len + 1));
|
||||
if (!*ms)
|
||||
return 1;
|
||||
|
||||
buf = lws_mqtt_str_next(*ms, NULL);
|
||||
|
||||
if (client_id) {
|
||||
lws_strnncpy((char *)buf, client_id, len, len + 1);
|
||||
lwsl_notice("%s: User space provided a client ID '%s'\n",
|
||||
__func__, (const char *)buf);
|
||||
} else {
|
||||
lwsl_notice("%s: generating random client id\n", __func__);
|
||||
n = len * sizeof(ran[0]);
|
||||
if (lws_get_random(context, ran, n) != n) {
|
||||
lws_mqtt_str_free(ms);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
for (n = 0; n < len; n++)
|
||||
buf[n] = code[ran[n] % 62];
|
||||
buf[len] = '\0';
|
||||
}
|
||||
|
||||
if (lws_mqtt_str_advance(*ms, (uint16_t)len)) {
|
||||
lws_mqtt_str_free(ms);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
lws_read_mqtt(struct lws *wsi, unsigned char *buf, lws_filepos_t len)
|
||||
{
|
||||
lws_mqttc_t *c = &wsi->mqtt->client;
|
||||
|
||||
return _lws_mqtt_rx_parser(wsi, &c->par, buf, (size_t)len);
|
||||
}
|
||||
|
||||
int
|
||||
lws_create_client_mqtt_object(const struct lws_client_connect_info *i,
|
||||
struct lws *wsi)
|
||||
{
|
||||
lws_mqttc_t *c;
|
||||
const lws_mqtt_client_connect_param_t *cp = i->mqtt_cp;
|
||||
|
||||
/* allocate the ws struct for the wsi */
|
||||
wsi->mqtt = lws_zalloc(sizeof(*wsi->mqtt), "client mqtt struct");
|
||||
if (!wsi->mqtt)
|
||||
goto oom;
|
||||
|
||||
wsi->mqtt->wsi = wsi;
|
||||
c = &wsi->mqtt->client;
|
||||
|
||||
if (lws_mqtt_generate_id(wsi, &c->id, cp->client_id)) {
|
||||
lwsl_err("%s: Error generating client ID\n", __func__);
|
||||
return 1;
|
||||
}
|
||||
lwsl_info("%s: using client id '%.*s'\n", __func__, c->id->len,
|
||||
(const char *)c->id->buf);
|
||||
|
||||
if (cp->clean_start || !(cp->client_id &&
|
||||
cp->client_id[0]))
|
||||
c->conn_flags = LMQCFT_CLEAN_START;
|
||||
if (cp->client_id_nofree)
|
||||
c->conn_flags |= LMQCFT_CLIENT_ID_NOFREE;
|
||||
if (cp->username_nofree)
|
||||
c->conn_flags |= LMQCFT_USERNAME_NOFREE;
|
||||
if (cp->password_nofree)
|
||||
c->conn_flags |= LMQCFT_PASSWORD_NOFREE;
|
||||
|
||||
if (!(c->conn_flags & LMQCFT_CLIENT_ID_NOFREE))
|
||||
lws_free((void *)cp->client_id);
|
||||
|
||||
c->keep_alive_secs = cp->keep_alive;
|
||||
c->aws_iot = cp->aws_iot;
|
||||
|
||||
if (cp->will_param.topic &&
|
||||
*cp->will_param.topic) {
|
||||
c->will.topic = lws_mqtt_str_create_cstr_dup(
|
||||
cp->will_param.topic, 0);
|
||||
if (!c->will.topic)
|
||||
goto oom1;
|
||||
c->conn_flags |= LMQCFT_WILL_FLAG;
|
||||
if (cp->will_param.message) {
|
||||
c->will.message = lws_mqtt_str_create_cstr_dup(
|
||||
cp->will_param.message, 0);
|
||||
if (!c->will.message)
|
||||
goto oom2;
|
||||
}
|
||||
c->conn_flags = (uint16_t)(unsigned int)(c->conn_flags | ((cp->will_param.qos << 3) & LMQCFT_WILL_QOS_MASK));
|
||||
c->conn_flags |= (uint16_t)((!!cp->will_param.retain) * LMQCFT_WILL_RETAIN);
|
||||
}
|
||||
|
||||
if (cp->username &&
|
||||
*cp->username) {
|
||||
c->username = lws_mqtt_str_create_cstr_dup(cp->username, 0);
|
||||
if (!c->username)
|
||||
goto oom3;
|
||||
c->conn_flags |= LMQCFT_USERNAME;
|
||||
if (!(c->conn_flags & LMQCFT_USERNAME_NOFREE))
|
||||
lws_free((void *)cp->username);
|
||||
if (cp->password) {
|
||||
c->password =
|
||||
lws_mqtt_str_create_cstr_dup(cp->password, 0);
|
||||
if (!c->password)
|
||||
goto oom4;
|
||||
c->conn_flags |= LMQCFT_PASSWORD;
|
||||
if (!(c->conn_flags & LMQCFT_PASSWORD_NOFREE))
|
||||
lws_free((void *)cp->password);
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
oom4:
|
||||
lws_mqtt_str_free(&c->username);
|
||||
oom3:
|
||||
lws_mqtt_str_free(&c->will.message);
|
||||
oom2:
|
||||
lws_mqtt_str_free(&c->will.topic);
|
||||
oom1:
|
||||
lws_mqtt_str_free(&c->id);
|
||||
oom:
|
||||
lwsl_err("%s: OOM!\n", __func__);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int
|
||||
lws_mqtt_client_socket_service(struct lws *wsi, struct lws_pollfd *pollfd,
|
||||
struct lws *wsi_conn)
|
||||
{
|
||||
struct lws_context *context = wsi->a.context;
|
||||
struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
|
||||
int n = 0, m = 0;
|
||||
struct lws_tokens ebuf;
|
||||
int buffered = 0;
|
||||
int pending = 0;
|
||||
#if defined(LWS_WITH_TLS)
|
||||
char erbuf[128];
|
||||
#endif
|
||||
const char *cce = NULL;
|
||||
|
||||
switch (lwsi_state(wsi)) {
|
||||
#if defined(LWS_WITH_SOCKS5)
|
||||
/* SOCKS Greeting Reply */
|
||||
case LRS_WAITING_SOCKS_GREETING_REPLY:
|
||||
case LRS_WAITING_SOCKS_AUTH_REPLY:
|
||||
case LRS_WAITING_SOCKS_CONNECT_REPLY:
|
||||
|
||||
switch (lws_socks5c_handle_state(wsi, pollfd, &cce)) {
|
||||
case LW5CHS_RET_RET0:
|
||||
return 0;
|
||||
case LW5CHS_RET_BAIL3:
|
||||
goto bail3;
|
||||
case LW5CHS_RET_STARTHS:
|
||||
|
||||
/*
|
||||
* Now we got the socks5 connection, we need to go down
|
||||
* the tls path on it if that's what we want
|
||||
*/
|
||||
|
||||
if (!(wsi->tls.use_ssl & LCCSCF_USE_SSL))
|
||||
goto start_ws_handshake;
|
||||
|
||||
switch (lws_client_create_tls(wsi, &cce, 0)) {
|
||||
case 0:
|
||||
break;
|
||||
case 1:
|
||||
return 0;
|
||||
default:
|
||||
goto bail3;
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
break;
|
||||
#endif
|
||||
case LRS_WAITING_DNS:
|
||||
/*
|
||||
* we are under PENDING_TIMEOUT_SENT_CLIENT_HANDSHAKE
|
||||
* timeout protection set in client-handshake.c
|
||||
*/
|
||||
if (!lws_client_connect_2_dnsreq(wsi)) {
|
||||
/* closed */
|
||||
lwsl_client("closed\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* either still pending connection, or changed mode */
|
||||
return 0;
|
||||
|
||||
case LRS_WAITING_CONNECT:
|
||||
|
||||
/*
|
||||
* we are under PENDING_TIMEOUT_SENT_CLIENT_HANDSHAKE
|
||||
* timeout protection set in client-handshake.c
|
||||
*/
|
||||
if (pollfd->revents & LWS_POLLOUT)
|
||||
lws_client_connect_3_connect(wsi, NULL, NULL, 0, NULL);
|
||||
break;
|
||||
|
||||
#if defined(LWS_WITH_TLS)
|
||||
case LRS_WAITING_SSL:
|
||||
|
||||
if (wsi->tls.use_ssl & LCCSCF_USE_SSL) {
|
||||
n = lws_ssl_client_connect2(wsi, erbuf, sizeof(erbuf));
|
||||
if (!n)
|
||||
return 0;
|
||||
if (n < 0) {
|
||||
cce = erbuf;
|
||||
goto bail3;
|
||||
}
|
||||
} else
|
||||
wsi->tls.ssl = NULL;
|
||||
#endif /* LWS_WITH_TLS */
|
||||
|
||||
/* fallthru */
|
||||
|
||||
#if defined(LWS_WITH_SOCKS5)
|
||||
start_ws_handshake:
|
||||
#endif
|
||||
lwsi_set_state(wsi, LRS_MQTTC_IDLE);
|
||||
lws_set_timeout(wsi, PENDING_TIMEOUT_AWAITING_CLIENT_HS_SEND,
|
||||
(int)context->timeout_secs);
|
||||
|
||||
/* fallthru */
|
||||
|
||||
case LRS_MQTTC_IDLE:
|
||||
/*
|
||||
* we should be ready to send out MQTT CONNECT
|
||||
*/
|
||||
lwsl_info("%s: %s: Transport established, send out CONNECT\n",
|
||||
__func__, lws_wsi_tag(wsi));
|
||||
if (lws_change_pollfd(wsi, LWS_POLLOUT, 0))
|
||||
return -1;
|
||||
if (!lws_mqtt_client_send_connect(wsi)) {
|
||||
lwsl_err("%s: Unable to send MQTT CONNECT\n", __func__);
|
||||
return -1;
|
||||
}
|
||||
if (lws_change_pollfd(wsi, 0, LWS_POLLIN))
|
||||
return -1;
|
||||
|
||||
lwsi_set_state(wsi, LRS_MQTTC_AWAIT_CONNACK);
|
||||
return 0;
|
||||
|
||||
case LRS_ESTABLISHED:
|
||||
case LRS_MQTTC_AWAIT_CONNACK:
|
||||
buffered = 0;
|
||||
ebuf.token = pt->serv_buf;
|
||||
ebuf.len = (int)wsi->a.context->pt_serv_buf_size;
|
||||
|
||||
if ((unsigned int)ebuf.len > wsi->a.context->pt_serv_buf_size)
|
||||
ebuf.len = (int)wsi->a.context->pt_serv_buf_size;
|
||||
|
||||
if ((int)pending > ebuf.len)
|
||||
pending = (char)ebuf.len;
|
||||
|
||||
ebuf.len = lws_ssl_capable_read(wsi, ebuf.token,
|
||||
(unsigned int)(pending ? pending :
|
||||
ebuf.len));
|
||||
switch (ebuf.len) {
|
||||
case 0:
|
||||
lwsl_info("%s: zero length read\n",
|
||||
__func__);
|
||||
goto fail;
|
||||
case LWS_SSL_CAPABLE_MORE_SERVICE:
|
||||
lwsl_info("SSL Capable more service\n");
|
||||
return 0;
|
||||
case LWS_SSL_CAPABLE_ERROR:
|
||||
lwsl_info("%s: LWS_SSL_CAPABLE_ERROR\n",
|
||||
__func__);
|
||||
goto fail;
|
||||
}
|
||||
|
||||
if (ebuf.len < 0)
|
||||
n = -1;
|
||||
else
|
||||
n = lws_read_mqtt(wsi, ebuf.token, (unsigned int)ebuf.len);
|
||||
if (n < 0) {
|
||||
lwsl_err("%s: Parsing packet failed\n", __func__);
|
||||
goto fail;
|
||||
}
|
||||
|
||||
m = ebuf.len - n;
|
||||
// lws_buflist_describe(&wsi->buflist, wsi, __func__);
|
||||
lwsl_debug("%s: consuming %d / %d\n", __func__, n, ebuf.len);
|
||||
if (lws_buflist_aware_finished_consuming(wsi, &ebuf, m,
|
||||
buffered,
|
||||
__func__))
|
||||
return -1;
|
||||
|
||||
return 0;
|
||||
|
||||
#if defined(LWS_WITH_TLS) || defined(LWS_WITH_SOCKS5)
|
||||
bail3:
|
||||
#endif
|
||||
lwsl_info("closing conn at LWS_CONNMODE...SERVER_REPLY\n");
|
||||
if (cce)
|
||||
lwsl_info("reason: %s\n", cce);
|
||||
lws_inform_client_conn_fail(wsi, (void *)cce, strlen(cce));
|
||||
|
||||
lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS, "cbail3");
|
||||
return -1;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return 0;
|
||||
fail:
|
||||
lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS, "mqtt svc fail");
|
||||
|
||||
return LWS_HPI_RET_WSI_ALREADY_DIED;
|
||||
}
|
2506
Kinc/Sources/kinc/libs/core/mqtt/mqtt.c
Normal file
2506
Kinc/Sources/kinc/libs/core/mqtt/mqtt.c
Normal file
File diff suppressed because it is too large
Load Diff
654
Kinc/Sources/kinc/libs/core/mqtt/ops-mqtt.c
Normal file
654
Kinc/Sources/kinc/libs/core/mqtt/ops-mqtt.c
Normal file
@ -0,0 +1,654 @@
|
||||
/*
|
||||
* libwebsockets - small server side websockets and web server implementation
|
||||
*
|
||||
* Copyright (C) 2010 - 2020 Andy Green <andy@warmcat.com>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to
|
||||
* deal in the Software without restriction, including without limitation the
|
||||
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
||||
* sell copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||
* IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "private-lib-core.h"
|
||||
|
||||
static int
|
||||
rops_handle_POLLIN_mqtt(struct lws_context_per_thread *pt, struct lws *wsi,
|
||||
struct lws_pollfd *pollfd)
|
||||
{
|
||||
unsigned int pending = 0;
|
||||
struct lws_tokens ebuf;
|
||||
int n = 0;
|
||||
char buffered = 0;
|
||||
|
||||
lwsl_debug("%s: wsistate 0x%x, %s pollout %d\n", __func__,
|
||||
(unsigned int)wsi->wsistate, wsi->a.protocol->name,
|
||||
pollfd->revents);
|
||||
|
||||
/*
|
||||
* After the CONNACK and nwsi establishment, the first logical
|
||||
* stream is migrated out of the nwsi to be child sid 1, and the
|
||||
* nwsi no longer has a wsi->mqtt of its own.
|
||||
*
|
||||
* RX events on the nwsi must be converted to events seen or not
|
||||
* seen by one or more child streams.
|
||||
*
|
||||
* SUBACK - reflected to child stream that asked for it
|
||||
* PUBACK - routed to child that did the related publish
|
||||
*/
|
||||
|
||||
ebuf.token = NULL;
|
||||
ebuf.len = 0;
|
||||
|
||||
if (lwsi_state(wsi) != LRS_ESTABLISHED) {
|
||||
#if defined(LWS_WITH_CLIENT)
|
||||
|
||||
if (lwsi_state(wsi) == LRS_WAITING_SSL &&
|
||||
((pollfd->revents & LWS_POLLOUT)) &&
|
||||
lws_change_pollfd(wsi, LWS_POLLOUT, 0)) {
|
||||
lwsl_info("failed at set pollfd\n");
|
||||
return LWS_HPI_RET_PLEASE_CLOSE_ME;
|
||||
}
|
||||
|
||||
if ((pollfd->revents & LWS_POLLOUT) &&
|
||||
lws_handle_POLLOUT_event(wsi, pollfd)) {
|
||||
lwsl_debug("POLLOUT event closed it\n");
|
||||
return LWS_HPI_RET_PLEASE_CLOSE_ME;
|
||||
}
|
||||
|
||||
n = lws_mqtt_client_socket_service(wsi, pollfd, NULL);
|
||||
if (n)
|
||||
return LWS_HPI_RET_WSI_ALREADY_DIED;
|
||||
#endif
|
||||
return LWS_HPI_RET_HANDLED;
|
||||
}
|
||||
|
||||
/* 1: something requested a callback when it was OK to write */
|
||||
|
||||
if ((pollfd->revents & LWS_POLLOUT) &&
|
||||
lwsi_state_can_handle_POLLOUT(wsi) &&
|
||||
lws_handle_POLLOUT_event(wsi, pollfd)) {
|
||||
if (lwsi_state(wsi) == LRS_RETURNED_CLOSE)
|
||||
lwsi_set_state(wsi, LRS_FLUSHING_BEFORE_CLOSE);
|
||||
|
||||
return LWS_HPI_RET_PLEASE_CLOSE_ME;
|
||||
}
|
||||
|
||||
/* 3: buflist needs to be drained
|
||||
*/
|
||||
read:
|
||||
// lws_buflist_describe(&wsi->buflist, wsi, __func__);
|
||||
ebuf.len = (int)lws_buflist_next_segment_len(&wsi->buflist, &ebuf.token);
|
||||
if (ebuf.len) {
|
||||
lwsl_info("draining buflist (len %d)\n", ebuf.len);
|
||||
buffered = 1;
|
||||
goto drain;
|
||||
}
|
||||
|
||||
if (!(pollfd->revents & pollfd->events & LWS_POLLIN))
|
||||
return LWS_HPI_RET_HANDLED;
|
||||
|
||||
/* if (lws_is_flowcontrolled(wsi)) { */
|
||||
/* lwsl_info("%s: %p should be rxflow (bm 0x%x)..\n", */
|
||||
/* __func__, wsi, wsi->rxflow_bitmap); */
|
||||
/* return LWS_HPI_RET_HANDLED; */
|
||||
/* } */
|
||||
|
||||
if (!(lwsi_role_client(wsi) && lwsi_state(wsi) != LRS_ESTABLISHED)) {
|
||||
/*
|
||||
* In case we are going to react to this rx by scheduling
|
||||
* writes, we need to restrict the amount of rx to the size
|
||||
* the protocol reported for rx buffer.
|
||||
*
|
||||
* Otherwise we get a situation we have to absorb possibly a
|
||||
* lot of reads before we get a chance to drain them by writing
|
||||
* them, eg, with echo type tests in autobahn.
|
||||
*/
|
||||
|
||||
buffered = 0;
|
||||
ebuf.token = pt->serv_buf;
|
||||
ebuf.len = (int)wsi->a.context->pt_serv_buf_size;
|
||||
|
||||
if ((unsigned int)ebuf.len > wsi->a.context->pt_serv_buf_size)
|
||||
ebuf.len = (int)wsi->a.context->pt_serv_buf_size;
|
||||
|
||||
if ((int)pending > ebuf.len)
|
||||
pending = (unsigned int)ebuf.len;
|
||||
|
||||
ebuf.len = lws_ssl_capable_read(wsi, ebuf.token,
|
||||
pending ? pending :
|
||||
(unsigned int)ebuf.len);
|
||||
switch (ebuf.len) {
|
||||
case 0:
|
||||
lwsl_info("%s: zero length read\n",
|
||||
__func__);
|
||||
return LWS_HPI_RET_PLEASE_CLOSE_ME;
|
||||
case LWS_SSL_CAPABLE_MORE_SERVICE:
|
||||
lwsl_info("SSL Capable more service\n");
|
||||
return LWS_HPI_RET_HANDLED;
|
||||
case LWS_SSL_CAPABLE_ERROR:
|
||||
lwsl_info("%s: LWS_SSL_CAPABLE_ERROR\n",
|
||||
__func__);
|
||||
return LWS_HPI_RET_PLEASE_CLOSE_ME;
|
||||
}
|
||||
|
||||
/*
|
||||
* coverity thinks ssl_capable_read() may read over
|
||||
* 2GB. Dissuade it...
|
||||
*/
|
||||
ebuf.len &= 0x7fffffff;
|
||||
}
|
||||
|
||||
drain:
|
||||
/* service incoming data */
|
||||
//lws_buflist_describe(&wsi->buflist, wsi, __func__);
|
||||
if (ebuf.len) {
|
||||
n = lws_read_mqtt(wsi, ebuf.token, (unsigned int)ebuf.len);
|
||||
if (n < 0) {
|
||||
lwsl_notice("%s: lws_read_mqtt returned %d\n",
|
||||
__func__, n);
|
||||
/* we closed wsi */
|
||||
goto fail;
|
||||
}
|
||||
// lws_buflist_describe(&wsi->buflist, wsi, __func__);
|
||||
lwsl_debug("%s: consuming %d / %d\n", __func__, n, ebuf.len);
|
||||
if (lws_buflist_aware_finished_consuming(wsi, &ebuf, ebuf.len,
|
||||
buffered, __func__))
|
||||
return LWS_HPI_RET_PLEASE_CLOSE_ME;
|
||||
}
|
||||
|
||||
ebuf.token = NULL;
|
||||
ebuf.len = 0;
|
||||
|
||||
pending = (unsigned int)lws_ssl_pending(wsi);
|
||||
if (pending) {
|
||||
pending = pending > wsi->a.context->pt_serv_buf_size ?
|
||||
wsi->a.context->pt_serv_buf_size : pending;
|
||||
goto read;
|
||||
}
|
||||
|
||||
if (buffered && /* were draining, now nothing left */
|
||||
!lws_buflist_next_segment_len(&wsi->buflist, NULL)) {
|
||||
lwsl_info("%s: %s flow buf: drained\n", __func__, lws_wsi_tag(wsi));
|
||||
/* having drained the rxflow buffer, can rearm POLLIN */
|
||||
#if !defined(LWS_WITH_SERVER)
|
||||
n =
|
||||
#endif
|
||||
__lws_rx_flow_control(wsi);
|
||||
/* n ignored, needed for NO_SERVER case */
|
||||
}
|
||||
|
||||
/* n = 0 */
|
||||
return LWS_HPI_RET_HANDLED;
|
||||
|
||||
fail:
|
||||
lwsl_err("%s: Failed, bailing\n", __func__);
|
||||
lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS, "mqtt svc fail");
|
||||
|
||||
return LWS_HPI_RET_WSI_ALREADY_DIED;
|
||||
}
|
||||
|
||||
#if 0 /* defined(LWS_WITH_SERVER) */
|
||||
|
||||
static int
|
||||
rops_adoption_bind_mqtt(struct lws *wsi, int type, const char *vh_prot_name)
|
||||
{
|
||||
/* no http but socket... must be mqtt */
|
||||
if ((type & LWS_ADOPT_HTTP) || !(type & LWS_ADOPT_SOCKET) ||
|
||||
(type & _LWS_ADOPT_FINISH))
|
||||
return 0; /* no match */
|
||||
|
||||
lws_role_transition(wsi, 0, (type & LWS_ADOPT_ALLOW_SSL) ? LRS_SSL_INIT :
|
||||
LRS_ESTABLISHED, &role_ops_mqtt);
|
||||
|
||||
if (vh_prot_name)
|
||||
lws_bind_protocol(wsi, wsi->a.protocol, __func__);
|
||||
else
|
||||
/* this is the only time he will transition */
|
||||
lws_bind_protocol(wsi,
|
||||
&wsi->a.vhost->protocols[wsi->a.vhost->mqtt_protocol_index],
|
||||
__func__);
|
||||
|
||||
return 1; /* bound */
|
||||
}
|
||||
#endif
|
||||
|
||||
static int
|
||||
rops_client_bind_mqtt(struct lws *wsi, const struct lws_client_connect_info *i)
|
||||
{
|
||||
lwsl_debug("%s: i = %p\n", __func__, i);
|
||||
if (!i) {
|
||||
|
||||
/* finalize */
|
||||
|
||||
if (!wsi->user_space && wsi->stash->cis[CIS_METHOD])
|
||||
if (lws_ensure_user_space(wsi))
|
||||
return 1;
|
||||
|
||||
if (!wsi->stash->cis[CIS_METHOD] && !wsi->stash->cis[CIS_ALPN])
|
||||
wsi->stash->cis[CIS_ALPN] = "x-amzn-mqtt-ca";
|
||||
|
||||
/* if we went on the ah waiting list, it's ok, we can
|
||||
* wait.
|
||||
*
|
||||
* When we do get the ah, now or later, he will end up
|
||||
* at lws_http_client_connect_via_info2().
|
||||
*/
|
||||
#if defined(LWS_WITH_CLIENT)
|
||||
if (lws_header_table_attach(wsi, 0) < 0)
|
||||
/*
|
||||
* if we failed here, the connection is already closed
|
||||
* and freed.
|
||||
*/
|
||||
return -1;
|
||||
#else
|
||||
if (lws_header_table_attach(wsi, 0))
|
||||
return 0;
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* if a recognized mqtt method, bind to it */
|
||||
if (strcmp(i->method, "MQTT"))
|
||||
return 0; /* no match */
|
||||
|
||||
if (lws_create_client_mqtt_object(i, wsi))
|
||||
return 1;
|
||||
|
||||
lws_role_transition(wsi, LWSIFR_CLIENT, LRS_UNCONNECTED,
|
||||
&role_ops_mqtt);
|
||||
return 1; /* matched */
|
||||
}
|
||||
|
||||
static int
|
||||
rops_handle_POLLOUT_mqtt(struct lws *wsi)
|
||||
{
|
||||
struct lws **wsi2;
|
||||
|
||||
lwsl_debug("%s\n", __func__);
|
||||
|
||||
#if defined(LWS_WITH_CLIENT)
|
||||
if (wsi->mqtt && wsi->mqtt->send_pingreq && !wsi->mqtt->inside_payload) {
|
||||
uint8_t buf[LWS_PRE + 2];
|
||||
|
||||
/*
|
||||
* We are swallowing this POLLOUT in order to send a PINGREQ
|
||||
* autonomously
|
||||
*/
|
||||
|
||||
wsi->mqtt->send_pingreq = 0;
|
||||
|
||||
lwsl_notice("%s: issuing PINGREQ\n", __func__);
|
||||
|
||||
buf[LWS_PRE] = LMQCP_CTOS_PINGREQ << 4;
|
||||
buf[LWS_PRE + 1] = 0;
|
||||
|
||||
if (lws_write(wsi, (uint8_t *)&buf[LWS_PRE], 2,
|
||||
LWS_WRITE_BINARY) != 2)
|
||||
return LWS_HP_RET_BAIL_DIE;
|
||||
|
||||
return LWS_HP_RET_BAIL_OK;
|
||||
}
|
||||
#endif
|
||||
if (wsi->mqtt && !wsi->mqtt->inside_payload &&
|
||||
(wsi->mqtt->send_pubrec || wsi->mqtt->send_pubrel ||
|
||||
wsi->mqtt->send_pubcomp)) {
|
||||
uint8_t buf[LWS_PRE + 4];
|
||||
/* Remaining len = 2 */
|
||||
buf[LWS_PRE + 1] = 2;
|
||||
if (wsi->mqtt->send_pubrec) {
|
||||
lwsl_notice("%s: issuing PUBREC for pkt id: %d\n",
|
||||
__func__, wsi->mqtt->peer_ack_pkt_id);
|
||||
buf[LWS_PRE] = LMQCP_PUBREC << 4 | 0x2;
|
||||
/* Packet ID */
|
||||
lws_ser_wu16be(&buf[LWS_PRE + 2],
|
||||
wsi->mqtt->peer_ack_pkt_id);
|
||||
wsi->mqtt->send_pubrec = 0;
|
||||
} else if (wsi->mqtt->send_pubrel) {
|
||||
lwsl_notice("%s: issuing PUBREL for pkt id: %d\n",
|
||||
__func__, wsi->mqtt->ack_pkt_id);
|
||||
buf[LWS_PRE] = LMQCP_PUBREL << 4 | 0x2;
|
||||
lws_ser_wu16be(&buf[LWS_PRE + 2],
|
||||
wsi->mqtt->ack_pkt_id);
|
||||
wsi->mqtt->send_pubrel = 0;
|
||||
} else {
|
||||
lwsl_notice("%s: issuing PUBCOMP for pkt id: %d\n",
|
||||
__func__, wsi->mqtt->peer_ack_pkt_id);
|
||||
buf[LWS_PRE] = LMQCP_PUBCOMP << 4 | 0x2;
|
||||
lws_ser_wu16be(&buf[LWS_PRE + 2],
|
||||
wsi->mqtt->peer_ack_pkt_id);
|
||||
wsi->mqtt->send_pubcomp = 0;
|
||||
}
|
||||
if (lws_write(wsi, (uint8_t *)&buf[LWS_PRE], 4,
|
||||
LWS_WRITE_BINARY) != 4)
|
||||
return LWS_HP_RET_BAIL_DIE;
|
||||
return LWS_HP_RET_BAIL_OK;
|
||||
}
|
||||
|
||||
wsi = lws_get_network_wsi(wsi);
|
||||
|
||||
wsi->mux.requested_POLLOUT = 0;
|
||||
|
||||
wsi2 = &wsi->mux.child_list;
|
||||
if (!*wsi2) {
|
||||
lwsl_debug("%s: no children\n", __func__);
|
||||
return LWS_HP_RET_DROP_POLLOUT;
|
||||
}
|
||||
|
||||
if (!wsi->mqtt)
|
||||
return LWS_HP_RET_BAIL_DIE;
|
||||
|
||||
lws_wsi_mux_dump_waiting_children(wsi);
|
||||
|
||||
do {
|
||||
struct lws *w, **wa;
|
||||
|
||||
wa = &(*wsi2)->mux.sibling_list;
|
||||
if (!(*wsi2)->mux.requested_POLLOUT)
|
||||
goto next_child;
|
||||
|
||||
if (!lwsi_state_can_handle_POLLOUT(wsi))
|
||||
goto next_child;
|
||||
|
||||
/*
|
||||
* If the nwsi is in the middle of a frame, we can only
|
||||
* continue to send that
|
||||
*/
|
||||
|
||||
if (wsi->mqtt->inside_payload && !(*wsi2)->mqtt->inside_payload)
|
||||
goto next_child;
|
||||
|
||||
/*
|
||||
* we're going to do writable callback for this child.
|
||||
* move him to be the last child
|
||||
*/
|
||||
w = lws_wsi_mux_move_child_to_tail(wsi2);
|
||||
if (!w) {
|
||||
wa = &wsi->mux.child_list;
|
||||
goto next_child;
|
||||
}
|
||||
|
||||
lwsl_debug("%s: child %s (wsistate 0x%x)\n", __func__,
|
||||
lws_wsi_tag(w), (unsigned int)w->wsistate);
|
||||
|
||||
if (lwsi_state(wsi) == LRS_ESTABLISHED &&
|
||||
!wsi->mqtt->inside_payload &&
|
||||
wsi->mqtt->send_puback) {
|
||||
uint8_t buf[LWS_PRE + 4];
|
||||
lwsl_notice("%s: issuing PUBACK for pkt id: %d\n",
|
||||
__func__, wsi->mqtt->ack_pkt_id);
|
||||
|
||||
/* Fixed header */
|
||||
buf[LWS_PRE] = LMQCP_PUBACK << 4;
|
||||
/* Remaining len = 2 */
|
||||
buf[LWS_PRE + 1] = 2;
|
||||
/* Packet ID */
|
||||
lws_ser_wu16be(&buf[LWS_PRE + 2], wsi->mqtt->peer_ack_pkt_id);
|
||||
|
||||
if (lws_write(wsi, (uint8_t *)&buf[LWS_PRE], 4,
|
||||
LWS_WRITE_BINARY) != 4)
|
||||
return LWS_HP_RET_BAIL_DIE;
|
||||
|
||||
wsi->mqtt->send_puback = 0;
|
||||
w->mux.requested_POLLOUT = 1;
|
||||
|
||||
wa = &wsi->mux.child_list;
|
||||
goto next_child;
|
||||
}
|
||||
|
||||
if (lws_callback_as_writeable(w)) {
|
||||
lwsl_notice("%s: Closing child %s\n", __func__, lws_wsi_tag(w));
|
||||
lws_close_free_wsi(w, LWS_CLOSE_STATUS_NOSTATUS,
|
||||
"mqtt pollout handle");
|
||||
wa = &wsi->mux.child_list;
|
||||
}
|
||||
|
||||
next_child:
|
||||
wsi2 = wa;
|
||||
} while (wsi2 && *wsi2 && !lws_send_pipe_choked(wsi));
|
||||
|
||||
// lws_wsi_mux_dump_waiting_children(wsi);
|
||||
|
||||
if (lws_wsi_mux_action_pending_writeable_reqs(wsi))
|
||||
return LWS_HP_RET_BAIL_DIE;
|
||||
|
||||
return LWS_HP_RET_BAIL_OK;
|
||||
}
|
||||
|
||||
#if defined(LWS_WITH_CLIENT)
|
||||
static int
|
||||
rops_issue_keepalive_mqtt(struct lws *wsi, int isvalid)
|
||||
{
|
||||
struct lws *nwsi = lws_get_network_wsi(wsi);
|
||||
|
||||
if (isvalid) {
|
||||
_lws_validity_confirmed_role(nwsi);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
nwsi->mqtt->send_pingreq = 1;
|
||||
lws_callback_on_writable(nwsi);
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
static int
|
||||
rops_close_role_mqtt(struct lws_context_per_thread *pt, struct lws *wsi)
|
||||
{
|
||||
struct lws *nwsi = lws_get_network_wsi(wsi);
|
||||
lws_mqtt_subs_t *s, *s1, *mysub;
|
||||
lws_mqttc_t *c;
|
||||
|
||||
if (!wsi->mqtt)
|
||||
return 0;
|
||||
|
||||
c = &wsi->mqtt->client;
|
||||
|
||||
lws_sul_cancel(&wsi->mqtt->sul_qos_puback_pubrec_wait);
|
||||
|
||||
lws_mqtt_str_free(&c->username);
|
||||
lws_mqtt_str_free(&c->password);
|
||||
lws_mqtt_str_free(&c->will.message);
|
||||
lws_mqtt_str_free(&c->will.topic);
|
||||
lws_mqtt_str_free(&c->id);
|
||||
|
||||
/* clean up any subscription allocations */
|
||||
|
||||
s = wsi->mqtt->subs_head;
|
||||
wsi->mqtt->subs_head = NULL;
|
||||
while (s) {
|
||||
s1 = s->next;
|
||||
/*
|
||||
* Account for children no longer using nwsi subscription
|
||||
*/
|
||||
mysub = lws_mqtt_find_sub(nwsi->mqtt, (const char *)&s[1]);
|
||||
// assert(mysub); /* if child subscribed, nwsi must feel the same */
|
||||
if (mysub) {
|
||||
assert(mysub->ref_count);
|
||||
mysub->ref_count--;
|
||||
}
|
||||
lws_free(s);
|
||||
s = s1;
|
||||
}
|
||||
|
||||
lws_mqtt_publish_param_t *pub =
|
||||
(lws_mqtt_publish_param_t *)
|
||||
wsi->mqtt->rx_cpkt_param;
|
||||
|
||||
if (pub)
|
||||
lws_free_set_NULL(pub->topic);
|
||||
|
||||
lws_free_set_NULL(wsi->mqtt->rx_cpkt_param);
|
||||
|
||||
lws_free_set_NULL(wsi->mqtt);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
rops_callback_on_writable_mqtt(struct lws *wsi)
|
||||
{
|
||||
#if defined(LWS_WITH_CLIENT)
|
||||
struct lws *network_wsi;
|
||||
#endif
|
||||
int already;
|
||||
|
||||
lwsl_debug("%s: %s (wsistate 0x%x)\n", __func__, lws_wsi_tag(wsi),
|
||||
(unsigned int)wsi->wsistate);
|
||||
|
||||
if (wsi->mux.requested_POLLOUT
|
||||
#if defined(LWS_WITH_CLIENT)
|
||||
&& !wsi->client_h2_alpn
|
||||
#endif
|
||||
) {
|
||||
lwsl_debug("already pending writable\n");
|
||||
return 1;
|
||||
}
|
||||
#if 0
|
||||
/* is this for DATA or for control messages? */
|
||||
if (wsi->upgraded_to_http2 && !wsi->h2.h2n->pps &&
|
||||
!lws_h2_tx_cr_get(wsi)) {
|
||||
/*
|
||||
* other side is not able to cope with us sending DATA
|
||||
* anything so no matter if we have POLLOUT on our side if it's
|
||||
* DATA we want to send.
|
||||
*
|
||||
* Delay waiting for our POLLOUT until peer indicates he has
|
||||
* space for more using tx window command in http2 layer
|
||||
*/
|
||||
lwsl_notice("%s: %p: skint (%d)\n", __func__, wsi,
|
||||
wsi->h2.tx_cr);
|
||||
wsi->h2.skint = 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
wsi->h2.skint = 0;
|
||||
#endif
|
||||
#if defined(LWS_WITH_CLIENT)
|
||||
network_wsi = lws_get_network_wsi(wsi);
|
||||
#endif
|
||||
already = lws_wsi_mux_mark_parents_needing_writeable(wsi);
|
||||
|
||||
/* for network action, act only on the network wsi */
|
||||
|
||||
if (already
|
||||
#if defined(LWS_WITH_CLIENT)
|
||||
&& !network_wsi->client_mux_substream
|
||||
#endif
|
||||
)
|
||||
return 1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
rops_close_kill_connection_mqtt(struct lws *wsi, enum lws_close_status reason)
|
||||
{
|
||||
lwsl_info(" %s, his parent %s: child list %p, siblings:\n",
|
||||
lws_wsi_tag(wsi),
|
||||
lws_wsi_tag(wsi->mux.parent_wsi), wsi->mux.child_list);
|
||||
//lws_wsi_mux_dump_children(wsi);
|
||||
|
||||
if (wsi->mux_substream
|
||||
#if defined(LWS_WITH_CLIENT)
|
||||
|| wsi->client_mux_substream
|
||||
#endif
|
||||
) {
|
||||
lwsl_info("closing %s: parent %s: first child %p\n",
|
||||
lws_wsi_tag(wsi),
|
||||
lws_wsi_tag(wsi->mux.parent_wsi),
|
||||
wsi->mux.child_list);
|
||||
|
||||
if (wsi->mux.child_list && lwsl_visible(LLL_INFO)) {
|
||||
lwsl_info(" parent %s: closing children: list:\n", lws_wsi_tag(wsi));
|
||||
lws_wsi_mux_dump_children(wsi);
|
||||
}
|
||||
|
||||
lws_wsi_mux_close_children(wsi, (int)reason);
|
||||
}
|
||||
|
||||
if ((
|
||||
#if defined(LWS_WITH_CLIENT)
|
||||
wsi->client_mux_substream ||
|
||||
#endif
|
||||
wsi->mux_substream) &&
|
||||
wsi->mux.parent_wsi) {
|
||||
lws_wsi_mux_sibling_disconnect(wsi);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static const lws_rops_t rops_table_mqtt[] = {
|
||||
/* 1 */ { .handle_POLLIN = rops_handle_POLLIN_mqtt },
|
||||
/* 2 */ { .handle_POLLOUT = rops_handle_POLLOUT_mqtt },
|
||||
/* 3 */ { .callback_on_writable = rops_callback_on_writable_mqtt },
|
||||
/* 4 */ { .close_role = rops_close_role_mqtt },
|
||||
/* 5 */ { .close_kill_connection = rops_close_kill_connection_mqtt },
|
||||
#if defined(LWS_WITH_CLIENT)
|
||||
/* 6 */ { .client_bind = rops_client_bind_mqtt },
|
||||
/* 7 */ { .issue_keepalive = rops_issue_keepalive_mqtt },
|
||||
#endif
|
||||
};
|
||||
|
||||
struct lws_role_ops role_ops_mqtt = {
|
||||
/* role name */ "mqtt",
|
||||
/* alpn id */ "x-amzn-mqtt-ca", /* "mqtt/3.1.1" */
|
||||
|
||||
/* rops_table */ rops_table_mqtt,
|
||||
/* rops_idx */ {
|
||||
/* LWS_ROPS_check_upgrades */
|
||||
/* LWS_ROPS_pt_init_destroy */ 0x00,
|
||||
/* LWS_ROPS_init_vhost */
|
||||
/* LWS_ROPS_destroy_vhost */ 0x00,
|
||||
/* LWS_ROPS_service_flag_pending */
|
||||
/* LWS_ROPS_handle_POLLIN */ 0x01,
|
||||
/* LWS_ROPS_handle_POLLOUT */
|
||||
/* LWS_ROPS_perform_user_POLLOUT */ 0x20,
|
||||
/* LWS_ROPS_callback_on_writable */
|
||||
/* LWS_ROPS_tx_credit */ 0x30,
|
||||
/* LWS_ROPS_write_role_protocol */
|
||||
/* LWS_ROPS_encapsulation_parent */ 0x00,
|
||||
/* LWS_ROPS_alpn_negotiated */
|
||||
/* LWS_ROPS_close_via_role_protocol */ 0x00,
|
||||
/* LWS_ROPS_close_role */
|
||||
/* LWS_ROPS_close_kill_connection */ 0x45,
|
||||
/* LWS_ROPS_destroy_role */
|
||||
/* LWS_ROPS_adoption_bind */ 0x00,
|
||||
|
||||
/* LWS_ROPS_client_bind */
|
||||
#if defined(LWS_WITH_CLIENT)
|
||||
/* LWS_ROPS_issue_keepalive */ 0x67,
|
||||
#else
|
||||
/* LWS_ROPS_issue_keepalive */ 0x00,
|
||||
#endif
|
||||
},
|
||||
|
||||
.adoption_cb = { LWS_CALLBACK_MQTT_NEW_CLIENT_INSTANTIATED,
|
||||
LWS_CALLBACK_MQTT_NEW_CLIENT_INSTANTIATED },
|
||||
.rx_cb = { LWS_CALLBACK_MQTT_CLIENT_RX,
|
||||
LWS_CALLBACK_MQTT_CLIENT_RX },
|
||||
.writeable_cb = { LWS_CALLBACK_MQTT_CLIENT_WRITEABLE,
|
||||
LWS_CALLBACK_MQTT_CLIENT_WRITEABLE },
|
||||
.close_cb = { LWS_CALLBACK_MQTT_CLIENT_CLOSED,
|
||||
LWS_CALLBACK_MQTT_CLIENT_CLOSED },
|
||||
.protocol_bind_cb = { LWS_CALLBACK_MQTT_IDLE,
|
||||
LWS_CALLBACK_MQTT_IDLE },
|
||||
.protocol_unbind_cb = { LWS_CALLBACK_MQTT_DROP_PROTOCOL,
|
||||
LWS_CALLBACK_MQTT_DROP_PROTOCOL },
|
||||
.file_handle = 0,
|
||||
};
|
324
Kinc/Sources/kinc/libs/core/mqtt/primitives.c
Normal file
324
Kinc/Sources/kinc/libs/core/mqtt/primitives.c
Normal file
@ -0,0 +1,324 @@
|
||||
/*
|
||||
* libwebsockets - small server side websockets and web server implementation
|
||||
*
|
||||
* Copyright (C) 2010 - 2020 Andy Green <andy@warmcat.com>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to
|
||||
* deal in the Software without restriction, including without limitation the
|
||||
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
||||
* sell copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||
* IN THE SOFTWARE.
|
||||
*
|
||||
* MQTT v5
|
||||
*
|
||||
* http://docs.oasis-open.org/mqtt/mqtt/v5.0/mqtt-v5.0.html
|
||||
*/
|
||||
|
||||
#include "private-lib-core.h"
|
||||
|
||||
#include <string.h>
|
||||
#include <sys/types.h>
|
||||
#include <assert.h>
|
||||
|
||||
|
||||
/*
|
||||
* Encode is done into a buffer of at least 4 bytes space.
|
||||
*
|
||||
* Returns -1 for error, or number of bytes used
|
||||
*/
|
||||
|
||||
int
|
||||
lws_mqtt_vbi_encode(uint32_t value, void *buf)
|
||||
{
|
||||
uint8_t *p = (uint8_t *)buf, b;
|
||||
|
||||
if (value > 0xfffffff) {
|
||||
assert(0);
|
||||
return -1;
|
||||
}
|
||||
|
||||
do {
|
||||
b = value & 0x7f;
|
||||
value >>= 7;
|
||||
if (value)
|
||||
*p++ = (0x80 | b);
|
||||
else
|
||||
*p++ = b;
|
||||
} while (value);
|
||||
|
||||
return lws_ptr_diff(p, (uint8_t *)buf);
|
||||
}
|
||||
|
||||
void
|
||||
lws_mqtt_vbi_init(lws_mqtt_vbi *vbi)
|
||||
{
|
||||
vbi->value = 0;
|
||||
vbi->consumed = 0;
|
||||
vbi->budget = 4;
|
||||
}
|
||||
|
||||
void
|
||||
lws_mqtt_2byte_init(lws_mqtt_vbi *vbi)
|
||||
{
|
||||
vbi->value = 0;
|
||||
vbi->consumed = 0;
|
||||
vbi->budget = 2;
|
||||
}
|
||||
|
||||
void
|
||||
lws_mqtt_4byte_init(lws_mqtt_vbi *vbi)
|
||||
{
|
||||
vbi->value = 0;
|
||||
vbi->consumed = 0;
|
||||
vbi->budget = 4;
|
||||
}
|
||||
|
||||
lws_mqtt_stateful_primitive_return_t
|
||||
lws_mqtt_vbi_r(lws_mqtt_vbi *vbi, const uint8_t **in, size_t *len)
|
||||
{
|
||||
uint8_t multiplier = 0;
|
||||
if (!vbi->budget) {
|
||||
lwsl_info("%s: bad vbi\n", __func__);
|
||||
|
||||
return LMSPR_FAILED_ALREADY_COMPLETED;
|
||||
}
|
||||
|
||||
while (*len && vbi->budget--) {
|
||||
uint8_t u = *((*in)++);
|
||||
|
||||
(*len)--;
|
||||
vbi->consumed++;
|
||||
vbi->value = vbi->value + (uint32_t)((u & 0x7f) << multiplier);
|
||||
multiplier = (uint8_t)(multiplier + 7);
|
||||
if (!(u & 0x80))
|
||||
return LMSPR_COMPLETED; /* finished */
|
||||
}
|
||||
|
||||
if (!vbi->budget) { /* should have ended on b7 = 0 and exited then... */
|
||||
lwsl_info("%s: bad vbi\n", __func__);
|
||||
|
||||
return LMSPR_FAILED_FORMAT;
|
||||
}
|
||||
|
||||
return LMSPR_NEED_MORE;
|
||||
}
|
||||
|
||||
lws_mqtt_stateful_primitive_return_t
|
||||
lws_mqtt_mb_parse(lws_mqtt_vbi *vbi, const uint8_t **in, size_t *len)
|
||||
{
|
||||
if (!vbi->budget)
|
||||
return LMSPR_FAILED_ALREADY_COMPLETED;
|
||||
|
||||
while (*len && vbi->budget--) {
|
||||
vbi->value = (vbi->value << 8) | *((*in)++);
|
||||
(*len)--;
|
||||
vbi->consumed++;
|
||||
}
|
||||
|
||||
return vbi->budget ? LMSPR_NEED_MORE : LMSPR_COMPLETED;
|
||||
}
|
||||
|
||||
/*
|
||||
* You can leave buf NULL, if so it will be allocated on the heap once the
|
||||
* actual length is known. nf should be 0, it will be set at allocation time.
|
||||
*
|
||||
* Or you can ensure no allocation and use an external buffer by setting buf
|
||||
* and lim. But buf must be in the ep context somehow, since it may have to
|
||||
* survive returns to the event loop unchanged. Set nf to 0 in this case.
|
||||
*
|
||||
* Or you can set buf to an externally allocated buffer, in which case you may
|
||||
* set nf so it will be freed when the string is "freed".
|
||||
*/
|
||||
|
||||
void
|
||||
lws_mqtt_str_init(lws_mqtt_str_t *s, uint8_t *buf, uint16_t lim, char nf)
|
||||
{
|
||||
s->len = 0; /* at COMPLETED, consumed count is s->len + 2 */
|
||||
s->pos = 0;
|
||||
s->buf = buf;
|
||||
s->limit = lim;
|
||||
s->len_valid = 0;
|
||||
s->needs_freeing = nf;
|
||||
}
|
||||
|
||||
lws_mqtt_str_t *
|
||||
lws_mqtt_str_create(uint16_t lim)
|
||||
{
|
||||
lws_mqtt_str_t *s = lws_malloc(sizeof(*s) + lim + 1, __func__);
|
||||
|
||||
if (!s)
|
||||
return NULL;
|
||||
|
||||
s->len = 0;
|
||||
s->pos = 0;
|
||||
s->buf = (uint8_t *)&s[1];
|
||||
s->limit = lim;
|
||||
s->len_valid = 0;
|
||||
s->needs_freeing = 1;
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
lws_mqtt_str_t *
|
||||
lws_mqtt_str_create_init(uint8_t *buf, uint16_t len, uint16_t lim)
|
||||
{
|
||||
lws_mqtt_str_t *s;
|
||||
|
||||
if (!lim)
|
||||
lim = len;
|
||||
|
||||
s = lws_mqtt_str_create(lim);
|
||||
|
||||
if (!s)
|
||||
return NULL;
|
||||
|
||||
memcpy(s->buf, buf, len);
|
||||
s->len = len;
|
||||
s->len_valid = 1;
|
||||
s->pos = len;
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
|
||||
lws_mqtt_str_t *
|
||||
lws_mqtt_str_create_cstr_dup(const char *buf, uint16_t lim)
|
||||
{
|
||||
size_t len = strlen(buf);
|
||||
|
||||
if (!lim)
|
||||
lim = (uint16_t)len;
|
||||
|
||||
return lws_mqtt_str_create_init((uint8_t *)buf, (uint16_t)len, lim);
|
||||
}
|
||||
|
||||
uint8_t *
|
||||
lws_mqtt_str_next(lws_mqtt_str_t *s, uint16_t *budget)
|
||||
{
|
||||
if (budget)
|
||||
*budget = (uint16_t)(s->limit - s->pos);
|
||||
|
||||
return &s->buf[s->pos];
|
||||
}
|
||||
|
||||
int
|
||||
lws_mqtt_str_advance(lws_mqtt_str_t *s, int n)
|
||||
{
|
||||
if (n > s->limit - s->pos) {
|
||||
lwsl_err("%s: attempted overflow %d vs %d\n", __func__,
|
||||
n, s->limit - s->pos);
|
||||
return 1;
|
||||
}
|
||||
|
||||
s->pos = (uint16_t)(s->pos + (uint16_t)n);
|
||||
s->len = (uint16_t)(s->len + (uint16_t)n);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void
|
||||
lws_mqtt_str_free(lws_mqtt_str_t **ps)
|
||||
{
|
||||
lws_mqtt_str_t *s = *ps;
|
||||
|
||||
if (!s || !s->needs_freeing)
|
||||
return;
|
||||
|
||||
/* buf may be independently allocated or allocated along with the
|
||||
* lws_mqtt_str_t at the end... if so the whole lws_mqtt_str_t is freed.
|
||||
*/
|
||||
|
||||
if (s->buf != (uint8_t *)&s[1])
|
||||
lws_free_set_NULL(s->buf);
|
||||
else
|
||||
lws_free_set_NULL(*ps);
|
||||
}
|
||||
|
||||
/*
|
||||
* Parses and allocates for lws_mqtt_str_t in a fragmentation-immune, but
|
||||
* efficient for bulk data way.
|
||||
*
|
||||
* Returns: LMSPR_NEED_MORE if needs more data,
|
||||
* LMSPR_COMPLETED if complete, <0 for error
|
||||
*
|
||||
* *len is reduced by, and *in is advanced by, the amount of data actually used,
|
||||
* except in error case
|
||||
*
|
||||
* lws_mqtt_str_free() must be called after calling this successfully
|
||||
* or not.
|
||||
*/
|
||||
lws_mqtt_stateful_primitive_return_t
|
||||
lws_mqtt_str_parse(lws_mqtt_str_t *s, const uint8_t **in, size_t *len)
|
||||
{
|
||||
const uint8_t *oin = *in;
|
||||
|
||||
/* handle the length + allocation if needed */
|
||||
while (*len && !s->len_valid && s->pos < 2) {
|
||||
s->len = (uint16_t)((s->len << 8) | *((*in)++));
|
||||
(*len)--;
|
||||
oin = *in;
|
||||
if (++s->pos == 2) {
|
||||
if (s->len > s->limit)
|
||||
return LMSPR_FAILED_OVERSIZE;
|
||||
|
||||
s->pos = 0;
|
||||
s->len_valid = 1;
|
||||
|
||||
if (!s->len) /* do not need to allocate */
|
||||
return LMSPR_COMPLETED;
|
||||
|
||||
if (!s->buf) {
|
||||
s->buf = lws_malloc(s->len, __func__);
|
||||
if (!s->buf)
|
||||
return LMSPR_FAILED_OOM;
|
||||
|
||||
s->needs_freeing = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* handle copying bulk data into allocation */
|
||||
if (s->len_valid && *len) {
|
||||
uint16_t span = (uint16_t)(s->len - s->pos);
|
||||
|
||||
if (span > *len)
|
||||
span = (uint16_t)*len;
|
||||
|
||||
memcpy(s->buf + s->pos, *in, span);
|
||||
*in += span;
|
||||
s->pos = (uint16_t)(s->pos + (uint16_t)span);
|
||||
}
|
||||
|
||||
*len -= (unsigned long)(*in - oin);
|
||||
|
||||
return s->buf && s->pos == s->len ? LMSPR_COMPLETED : LMSPR_NEED_MORE;
|
||||
}
|
||||
|
||||
int
|
||||
lws_mqtt_bindata_cmp(const lws_mqtt_str_t *bd1,
|
||||
const lws_mqtt_str_t *bd2)
|
||||
{
|
||||
if (bd1->len != bd2->len)
|
||||
return 1;
|
||||
|
||||
if (!!bd1->buf != !!bd2->buf)
|
||||
return 1;
|
||||
|
||||
if (!bd1->buf && !bd2->buf)
|
||||
return 0;
|
||||
|
||||
return memcmp(bd1->buf, bd2->buf, bd1->len);
|
||||
}
|
||||
|
449
Kinc/Sources/kinc/libs/core/mqtt/private-lib-roles-mqtt.h
Normal file
449
Kinc/Sources/kinc/libs/core/mqtt/private-lib-roles-mqtt.h
Normal file
@ -0,0 +1,449 @@
|
||||
/*
|
||||
* libwebsockets - small server side websockets and web server implementation
|
||||
*
|
||||
* Copyright (C) 2010 - 2021 Andy Green <andy@warmcat.com>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to
|
||||
* deal in the Software without restriction, including without limitation the
|
||||
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
||||
* sell copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||
* IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef _PRIVATE_LIB_ROLES_MQTT
|
||||
#define _PRIVATE_LIB_ROLES_MQTT 1
|
||||
|
||||
extern struct lws_role_ops role_ops_mqtt;
|
||||
|
||||
#define lwsi_role_mqtt(wsi) (wsi->role_ops == &role_ops_mqtt)
|
||||
|
||||
#define LWS_MQTT_MAX_CHILDREN 8 /* max child streams on same parent */
|
||||
|
||||
#define LMQCP_LUT_FLAG_RESERVED_FLAGS 0x10
|
||||
#define LMQCP_LUT_FLAG_PACKET_ID_NONE 0x00
|
||||
#define LMQCP_LUT_FLAG_PACKET_ID_HAS 0x20
|
||||
#define LMQCP_LUT_FLAG_PACKET_ID_QOS12 0x40
|
||||
#define LMQCP_LUT_FLAG_PACKET_ID_MASK 0x60
|
||||
#define LMQCP_LUT_FLAG_PAYLOAD 0x80 /* payload req (publish = opt)*/
|
||||
|
||||
#define lws_mqtt_str_is_not_empty(s) ( ((s)) && \
|
||||
((s))->len && \
|
||||
((s))->buf && \
|
||||
*((s))->buf )
|
||||
|
||||
#define LWS_MQTT_RESPONSE_TIMEOUT (3 * LWS_US_PER_SEC)
|
||||
#define LWS_MQTT_RETRY_CEILING (60 * LWS_US_PER_SEC)
|
||||
#define LWS_MQTT_MAX_PUBLISH_RETRY (3)
|
||||
|
||||
typedef enum {
|
||||
LMSPR_COMPLETED = 0,
|
||||
LMSPR_NEED_MORE = 1,
|
||||
|
||||
LMSPR_FAILED_OOM = -1,
|
||||
LMSPR_FAILED_OVERSIZE = -2,
|
||||
LMSPR_FAILED_FORMAT = -3,
|
||||
LMSPR_FAILED_ALREADY_COMPLETED = -4,
|
||||
} lws_mqtt_stateful_primitive_return_t;
|
||||
|
||||
typedef struct {
|
||||
uint32_t value;
|
||||
char budget;
|
||||
char consumed;
|
||||
} lws_mqtt_vbi;
|
||||
|
||||
/* works for vbi, 2-byte and 4-byte fixed length */
|
||||
static inline int
|
||||
lws_mqtt_mb_first(lws_mqtt_vbi *vbi) { return !vbi->consumed; }
|
||||
|
||||
int
|
||||
lws_mqtt_vbi_encode(uint32_t value, void *buf);
|
||||
|
||||
/*
|
||||
* Decode is done statefully on an arbitrary amount of input data (which may
|
||||
* be one byte). It's like this so it can continue seamlessly if a buffer ends
|
||||
* partway through the primitive, and the api matches the bulk binary data case.
|
||||
*
|
||||
* VBI decode:
|
||||
*
|
||||
* Initialize the lws_mqtt_vbi state by calling lws_mqtt_vbi_init() on it, then
|
||||
* feed lws_mqtt_vbi_r() bytes to decode.
|
||||
*
|
||||
* Returns <0 for error, LMSPR_COMPLETED if done (vbi->value is valid), or
|
||||
* LMSPR_NEED_MORE if more calls to lws_mqtt_vbi_r() with subsequent bytes
|
||||
* needed.
|
||||
*
|
||||
* *in and *len are updated accordingly.
|
||||
*
|
||||
* 2-byte and 4-byte decode:
|
||||
*
|
||||
* Initialize the lws_mqtt_vbi state by calling lws_mqtt_2byte_init() or
|
||||
* lws_mqtt_4byte_init() on it, then feed lws_mqtt_mb_parse() bytes
|
||||
* to decode.
|
||||
*
|
||||
* Returns <0 for error, LMSPR_COMPLETED if done (vbi->value is valid), or
|
||||
* LMSPR_NEED_MORE if more calls to lws_mqtt_mb_parse() with subsequent
|
||||
* bytes needed.
|
||||
*
|
||||
* *in and *len are updated accordingly.
|
||||
*/
|
||||
|
||||
void
|
||||
lws_mqtt_vbi_init(lws_mqtt_vbi *vbi);
|
||||
|
||||
void
|
||||
lws_mqtt_2byte_init(lws_mqtt_vbi *vbi);
|
||||
|
||||
void
|
||||
lws_mqtt_4byte_init(lws_mqtt_vbi *vbi);
|
||||
|
||||
lws_mqtt_stateful_primitive_return_t
|
||||
lws_mqtt_vbi_r(lws_mqtt_vbi *vbi, const uint8_t **in, size_t *len);
|
||||
|
||||
lws_mqtt_stateful_primitive_return_t
|
||||
lws_mqtt_mb_parse(lws_mqtt_vbi *vbi, const uint8_t **in, size_t *len);
|
||||
|
||||
struct lws_mqtt_str_st {
|
||||
uint8_t *buf;
|
||||
uint16_t len;
|
||||
|
||||
uint16_t limit; /* it's cheaper to add the state here than
|
||||
* the pointer to point to it elsewhere */
|
||||
uint16_t pos;
|
||||
char len_valid;
|
||||
char needs_freeing;
|
||||
};
|
||||
|
||||
static inline int
|
||||
lws_mqtt_str_first(struct lws_mqtt_str_st *s) { return !s->buf && !s->pos; }
|
||||
|
||||
|
||||
lws_mqtt_stateful_primitive_return_t
|
||||
lws_mqtt_str_parse(struct lws_mqtt_str_st *bd, const uint8_t **in, size_t *len);
|
||||
|
||||
typedef enum {
|
||||
LMQCPP_IDLE,
|
||||
|
||||
/* receive packet type part of fixed header took us out of idle... */
|
||||
LMQCPP_CONNECT_PACKET = LMQCP_CTOS_CONNECT << 4,
|
||||
LMQCPP_CONNECT_REMAINING_LEN_VBI,
|
||||
LMQCPP_CONNECT_VH_PNAME,
|
||||
LMQCPP_CONNECT_VH_PVERSION,
|
||||
LMQCPP_CONNECT_VH_FLAGS,
|
||||
LMQCPP_CONNECT_VH_KEEPALIVE,
|
||||
LMQCPP_CONNECT_VH_PROPERTIES_VBI_LEN,
|
||||
|
||||
LMQCPP_CONNACK_PACKET = LMQCP_STOC_CONNACK << 4,
|
||||
LMQCPP_CONNACK_VH_FLAGS,
|
||||
LMQCPP_CONNACK_VH_RETURN_CODE,
|
||||
|
||||
LMQCPP_PUBLISH_PACKET = LMQCP_PUBLISH << 4,
|
||||
LMQCPP_PUBLISH_REMAINING_LEN_VBI,
|
||||
LMQCPP_PUBLISH_VH_TOPIC,
|
||||
LMQCPP_PUBLISH_VH_PKT_ID,
|
||||
|
||||
LMQCPP_PUBACK_PACKET = LMQCP_PUBACK << 4,
|
||||
LMQCPP_PUBACK_VH_PKT_ID,
|
||||
LMQCPP_PUBACK_PROPERTIES_LEN_VBI,
|
||||
|
||||
LMQCPP_PUBREC_PACKET = LMQCP_PUBREC << 4,
|
||||
LMQCPP_PUBREC_VH_PKT_ID,
|
||||
|
||||
LMQCPP_PUBREL_PACKET = LMQCP_PUBREL << 4,
|
||||
LMQCPP_PUBREL_VH_PKT_ID,
|
||||
|
||||
LMQCPP_PUBCOMP_PACKET = LMQCP_PUBCOMP << 4,
|
||||
LMQCPP_PUBCOMP_VH_PKT_ID,
|
||||
|
||||
LMQCPP_SUBACK_PACKET = LMQCP_STOC_SUBACK << 4,
|
||||
LMQCPP_SUBACK_VH_PKT_ID,
|
||||
LMQCPP_SUBACK_PAYLOAD,
|
||||
|
||||
LMQCPP_UNSUBACK_PACKET = LMQCP_STOC_UNSUBACK << 4,
|
||||
LMQCPP_UNSUBACK_VH_PKT_ID,
|
||||
|
||||
LMQCPP_PINGRESP_ZERO = LMQCP_STOC_PINGRESP << 4,
|
||||
|
||||
LMQCPP_PAYLOAD,
|
||||
|
||||
LMQCPP_EAT_PROPERTIES_AND_COMPLETE,
|
||||
|
||||
LMQCPP_PROP_ID_VBI,
|
||||
|
||||
/* all possible property payloads */
|
||||
|
||||
/* 3.3.2.3.2 */
|
||||
LMQCPP_PROP_PAYLOAD_FORMAT_INDICATOR_1BYTE = 0x101,
|
||||
|
||||
LMQCPP_PROP_MSG_EXPIRY_INTERVAL_4BYTE = 0x102,
|
||||
|
||||
LMQCPP_PROP_CONTENT_TYPE_UTF8S = 0x103,
|
||||
|
||||
LMQCPP_PROP_RESPONSE_TOPIC_UTF8S = 0x108,
|
||||
|
||||
LMQCPP_PROP_CORRELATION_BINDATA = 0x109,
|
||||
|
||||
LMQCPP_PROP_SUBSCRIPTION_ID_VBI = 0x10b,
|
||||
|
||||
LMQCPP_PROP_SESSION_EXPIRY_INTERVAL_4BYTE = 0x111,
|
||||
|
||||
LMQCPP_PROP_ASSIGNED_CLIENTID_UTF8S = 0x112,
|
||||
|
||||
LMQCPP_PROP_SERVER_KEEPALIVE_2BYTE = 0x113,
|
||||
|
||||
LMQCPP_PROP_AUTH_METHOD_UTF8S = 0x115,
|
||||
|
||||
LMQCPP_PROP_AUTH_DATA_BINDATA = 0x116,
|
||||
|
||||
LMQCPP_PROP_REQUEST_PROBLEM_INFO_1BYTE = 0x117,
|
||||
|
||||
LMQCPP_PROP_WILL_DELAY_INTERVAL_4BYTE = 0x118,
|
||||
|
||||
LMQCPP_PROP_REQUEST_REPSONSE_INFO_1BYTE = 0x119,
|
||||
|
||||
LMQCPP_PROP_RESPONSE_INFO_UTF8S = 0x11a,
|
||||
|
||||
LMQCPP_PROP_SERVER_REFERENCE_UTF8S = 0x11c,
|
||||
|
||||
LMQCPP_PROP_REASON_STRING_UTF8S = 0x11f,
|
||||
|
||||
LMQCPP_PROP_RECEIVE_MAXIMUM_2BYTE = 0x121,
|
||||
|
||||
LMQCPP_PROP_TOPIC_MAXIMUM_2BYTE = 0x122,
|
||||
|
||||
LMQCPP_PROP_TOPIC_ALIAS_2BYTE = 0x123,
|
||||
|
||||
LMQCPP_PROP_MAXIMUM_QOS_1BYTE = 0x124,
|
||||
|
||||
LMQCPP_PROP_RETAIN_AVAILABLE_1BYTE = 0x125,
|
||||
|
||||
LMQCPP_PROP_USER_PROPERTY_NAME_UTF8S = 0x126,
|
||||
LMQCPP_PROP_USER_PROPERTY_VALUE_UTF8S = 0x226,
|
||||
|
||||
LMQCPP_PROP_MAXIMUM_PACKET_SIZE_4BYTE = 0x127,
|
||||
|
||||
LMQCPP_PROP_WILDCARD_SUBSCRIPTION_AVAILABLE_1BYTE = 0x128,
|
||||
|
||||
LMQCPP_PROP_SUBSCRIPTION_IDENTIFIER_AVAILABLE_1BYTE = 0x129,
|
||||
|
||||
LMQCPP_PROP_SHARED_SUBSCRIPTION_AVAILABLE_1BYTE = 0x12a,
|
||||
|
||||
} lws_mqtt_packet_parse_state_t;
|
||||
|
||||
/*
|
||||
* the states an MQTT connection can be in
|
||||
*/
|
||||
|
||||
typedef enum {
|
||||
LGSMQTT_UNKNOWN,
|
||||
LGSMQTT_IDLE,
|
||||
LGSMQTT_TRANSPORT_CONNECTED,
|
||||
|
||||
LGSMQTT_SENT_CONNECT,
|
||||
LGSMQTT_ESTABLISHED,
|
||||
|
||||
LGSMQTT_SENT_SUBSCRIBE,
|
||||
LGSMQTT_SUBSCRIBED,
|
||||
|
||||
} lwsgs_mqtt_states_t;
|
||||
|
||||
typedef struct lws_mqtt_parser_st {
|
||||
/* struct lws_mqtt_str_st s_content_type; */
|
||||
lws_mqtt_packet_parse_state_t state;
|
||||
lws_mqtt_vbi vbit;
|
||||
|
||||
lws_mqtt_reason_t reason;
|
||||
|
||||
lws_mqtt_str_t s_temp;
|
||||
|
||||
uint8_t fixed_seen[4];
|
||||
uint8_t props_seen[8];
|
||||
|
||||
uint8_t cpkt_flags;
|
||||
uint32_t cpkt_remlen;
|
||||
|
||||
uint32_t props_len;
|
||||
uint32_t consumed;
|
||||
uint32_t prop_id;
|
||||
uint32_t props_consumed;
|
||||
uint32_t payload_consumed;
|
||||
|
||||
uint16_t keepalive;
|
||||
uint16_t cpkt_id;
|
||||
uint32_t n;
|
||||
|
||||
uint8_t temp[32];
|
||||
uint8_t conn_rc;
|
||||
uint8_t payload_format;
|
||||
uint8_t packet_type_flags;
|
||||
uint8_t conn_protocol_version;
|
||||
uint8_t fixed;
|
||||
|
||||
uint8_t flag_pending_send_connack_close:1;
|
||||
uint8_t flag_pending_send_reason_close:1;
|
||||
uint8_t flag_prop_multi:1;
|
||||
uint8_t flag_server:1;
|
||||
|
||||
} lws_mqtt_parser_t;
|
||||
|
||||
typedef enum {
|
||||
LMVTR_VALID = 0,
|
||||
LMVTR_VALID_WILDCARD = 1,
|
||||
LMVTR_VALID_SHADOW = 2,
|
||||
|
||||
LMVTR_FAILED_OVERSIZE = -1,
|
||||
LMVTR_FAILED_WILDCARD_FORMAT = -2,
|
||||
LMVTR_FAILED_SHADOW_FORMAT = -3,
|
||||
} lws_mqtt_validate_topic_return_t;
|
||||
|
||||
typedef enum {
|
||||
LMMTR_TOPIC_NOMATCH = 0,
|
||||
LMMTR_TOPIC_MATCH = 1,
|
||||
|
||||
LMMTR_TOPIC_MATCH_ERROR = -1
|
||||
} lws_mqtt_match_topic_return_t;
|
||||
|
||||
typedef struct lws_mqtt_subs {
|
||||
struct lws_mqtt_subs *next;
|
||||
|
||||
uint8_t ref_count; /* number of children referencing */
|
||||
|
||||
/* Flags */
|
||||
uint8_t wildcard:1;
|
||||
uint8_t shadow:1;
|
||||
|
||||
/* subscription name + NUL overallocated here */
|
||||
char topic[];
|
||||
} lws_mqtt_subs_t;
|
||||
|
||||
typedef struct lws_mqtts {
|
||||
lws_mqtt_parser_t par;
|
||||
lwsgs_mqtt_states_t estate;
|
||||
struct lws_dll2 active_session_list_head;
|
||||
struct lws_dll2 limbo_session_list_head;
|
||||
} lws_mqtts_t;
|
||||
|
||||
typedef struct lws_mqttc {
|
||||
lws_mqtt_parser_t par;
|
||||
lwsgs_mqtt_states_t estate;
|
||||
struct lws_mqtt_str_st *id;
|
||||
struct lws_mqtt_str_st *username;
|
||||
struct lws_mqtt_str_st *password;
|
||||
struct {
|
||||
struct lws_mqtt_str_st *topic;
|
||||
struct lws_mqtt_str_st *message;
|
||||
lws_mqtt_qos_levels_t qos;
|
||||
uint8_t retain;
|
||||
} will;
|
||||
uint16_t keep_alive_secs;
|
||||
uint16_t conn_flags;
|
||||
uint8_t aws_iot;
|
||||
} lws_mqttc_t;
|
||||
|
||||
struct _lws_mqtt_related {
|
||||
lws_mqttc_t client;
|
||||
lws_sorted_usec_list_t sul_qos_puback_pubrec_wait; /* QoS1 puback or QoS2 pubrec wait TO */
|
||||
lws_sorted_usec_list_t sul_qos1_puback_wait; /* QoS1 puback wait TO */
|
||||
lws_sorted_usec_list_t sul_unsuback_wait; /* unsuback wait TO */
|
||||
lws_sorted_usec_list_t sul_qos2_pubrec_wait; /* QoS2 pubrec wait TO */
|
||||
lws_sorted_usec_list_t sul_shadow_wait; /* Device Shadow wait TO */
|
||||
struct lws *wsi; /**< so sul can use lws_container_of */
|
||||
lws_mqtt_subs_t *subs_head; /**< Linked-list of heap-allocated subscription objects */
|
||||
void *rx_cpkt_param;
|
||||
uint16_t pkt_id;
|
||||
uint16_t ack_pkt_id;
|
||||
uint16_t peer_ack_pkt_id;
|
||||
uint16_t sub_size;
|
||||
|
||||
#if defined(LWS_WITH_CLIENT)
|
||||
uint8_t send_pingreq:1;
|
||||
uint8_t session_resumed:1;
|
||||
#endif
|
||||
uint8_t inside_payload:1;
|
||||
uint8_t inside_subscribe:1;
|
||||
uint8_t inside_unsubscribe:1;
|
||||
uint8_t inside_birth:1;
|
||||
uint8_t inside_resume_session:1;
|
||||
uint8_t send_puback:1;
|
||||
uint8_t send_pubrel:1;
|
||||
uint8_t send_pubrec:1;
|
||||
uint8_t send_pubcomp:1;
|
||||
uint8_t unacked_publish:1;
|
||||
uint8_t unacked_pubrel:1;
|
||||
|
||||
uint8_t done_subscribe:1;
|
||||
uint8_t done_birth:1;
|
||||
uint8_t inside_shadow:1;
|
||||
uint8_t done_shadow_subscribe:1;
|
||||
uint8_t send_shadow_unsubscribe:1;
|
||||
};
|
||||
|
||||
/*
|
||||
* New sessions are created by starting CONNECT. If the ClientID sent
|
||||
* by the client matches a different, extant session, then the
|
||||
* existing one is taken over and the new one created for duration of
|
||||
* CONNECT processing is destroyed.
|
||||
*
|
||||
* On the server side, bearing in mind multiple simultaneous,
|
||||
* fragmented CONNECTs may be interleaved ongoing, all state and
|
||||
* parsing temps for a session must live in the session object.
|
||||
*/
|
||||
|
||||
struct lws_mqtt_endpoint_st;
|
||||
|
||||
typedef struct lws_mqtts_session_st {
|
||||
struct lws_dll2 session_list;
|
||||
|
||||
} lws_mqtts_session_t;
|
||||
|
||||
#define ctl_pkt_type(x) (x->packet_type_flags >> 4)
|
||||
|
||||
|
||||
void
|
||||
lws_mqttc_state_transition(lws_mqttc_t *ep, lwsgs_mqtt_states_t s);
|
||||
|
||||
int
|
||||
_lws_mqtt_rx_parser(struct lws *wsi, lws_mqtt_parser_t *par,
|
||||
const uint8_t *buf, size_t len);
|
||||
|
||||
int
|
||||
lws_mqtt_client_socket_service(struct lws *wsi, struct lws_pollfd *pollfd,
|
||||
struct lws *wsi_conn);
|
||||
|
||||
int
|
||||
lws_create_client_mqtt_object(const struct lws_client_connect_info *i,
|
||||
struct lws *wsi);
|
||||
|
||||
struct lws *
|
||||
lws_mqtt_client_send_connect(struct lws *wsi);
|
||||
|
||||
struct lws *
|
||||
lws_mqtt_client_send_disconnect(struct lws *wsi);
|
||||
|
||||
int
|
||||
lws_mqtt_fill_fixed_header(uint8_t *p, lws_mqtt_control_packet_t ctrl_pkt_type,
|
||||
uint8_t dup, lws_mqtt_qos_levels_t qos,
|
||||
uint8_t retain);
|
||||
|
||||
struct lws *
|
||||
lws_wsi_mqtt_adopt(struct lws *parent_wsi, struct lws *wsi);
|
||||
|
||||
lws_mqtt_subs_t *
|
||||
lws_mqtt_find_sub(struct _lws_mqtt_related *mqtt, const char *topic);
|
||||
|
||||
lws_mqtt_match_topic_return_t
|
||||
lws_mqtt_is_topic_matched(const char* sub, const char* pub);
|
||||
|
||||
#endif /* _PRIVATE_LIB_ROLES_MQTT */
|
||||
|
Reference in New Issue
Block a user