2020-02-29 12:37:24 +00:00
|
|
|
/*
|
|
|
|
* libwebsockets - small server side websockets and web server implementation
|
|
|
|
*
|
2021-03-22 11:58:28 -07:00
|
|
|
* Copyright (C) 2019 - 2021 Andy Green <andy@warmcat.com>
|
2020-02-29 12:37:24 +00:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
secstream_mqtt(struct lws *wsi, enum lws_callback_reasons reason, void *user,
|
|
|
|
void *in, size_t len)
|
|
|
|
{
|
|
|
|
lws_ss_handle_t *h = (lws_ss_handle_t *)lws_get_opaque_user_data(wsi);
|
|
|
|
lws_mqtt_publish_param_t mqpp, *pmqpp;
|
|
|
|
uint8_t buf[LWS_PRE + 1400];
|
2020-08-26 11:05:41 +01:00
|
|
|
lws_ss_state_return_t r;
|
2020-02-29 12:37:24 +00:00
|
|
|
size_t buflen;
|
2020-06-01 07:33:37 +01:00
|
|
|
int f = 0;
|
2020-02-29 12:37:24 +00:00
|
|
|
|
|
|
|
switch (reason) {
|
|
|
|
|
|
|
|
/* because we are protocols[0] ... */
|
|
|
|
case LWS_CALLBACK_CLIENT_CONNECTION_ERROR:
|
|
|
|
lwsl_info("%s: CLIENT_CONNECTION_ERROR: %s\n", __func__,
|
|
|
|
in ? (char *)in : "(null)");
|
|
|
|
if (!h)
|
|
|
|
break;
|
ss: sspc: add conmon performance telemetry
This provides a way to get ahold of LWS_WITH_CONMON telemetry from Secure
Streams, it works the same with direct onward connections or via the proxy.
You can mark streamtypes with a "perf": true policy attribute... this
causes the onward connections on those streamtypes to collect information
about the connection performance, and the unsorted DNS results.
Streams with that policy attribute receive extra data in their rx callback,
with the LWSSS_FLAG_PERF_JSON flag set on it, containing JSON describing the
performance of the onward connection taken from CONMON data, in a JSON
representation. Streams without the "perf" attribute set never receive
this extra rx.
The received JSON is based on the CONMON struct info and looks like
{"peer":"46.105.127.147","dns_us":596,"sockconn_us":31382,"tls_us":28180,"txn_resp_us:23015,"dns":["2001:41d0:2:ee93::1","46.105.127.147"]}
A new minimal example minimal-secure-streams-perf is added that collects
this data on an HTTP GET from warmcat.com, and is built with a -client
version as well if LWS_WITH_SECURE_STREAMS_PROXY_API is set, that operates
via the ss proxy and produces the same result at the client.
2021-03-31 13:20:34 +01:00
|
|
|
|
2021-08-03 15:25:39 +08:00
|
|
|
#if defined(LWS_WITH_CONMON)
|
ss: sspc: add conmon performance telemetry
This provides a way to get ahold of LWS_WITH_CONMON telemetry from Secure
Streams, it works the same with direct onward connections or via the proxy.
You can mark streamtypes with a "perf": true policy attribute... this
causes the onward connections on those streamtypes to collect information
about the connection performance, and the unsorted DNS results.
Streams with that policy attribute receive extra data in their rx callback,
with the LWSSS_FLAG_PERF_JSON flag set on it, containing JSON describing the
performance of the onward connection taken from CONMON data, in a JSON
representation. Streams without the "perf" attribute set never receive
this extra rx.
The received JSON is based on the CONMON struct info and looks like
{"peer":"46.105.127.147","dns_us":596,"sockconn_us":31382,"tls_us":28180,"txn_resp_us:23015,"dns":["2001:41d0:2:ee93::1","46.105.127.147"]}
A new minimal example minimal-secure-streams-perf is added that collects
this data on an HTTP GET from warmcat.com, and is built with a -client
version as well if LWS_WITH_SECURE_STREAMS_PROXY_API is set, that operates
via the ss proxy and produces the same result at the client.
2021-03-31 13:20:34 +01:00
|
|
|
lws_conmon_ss_json(h);
|
2021-08-03 15:25:39 +08:00
|
|
|
#endif
|
ss: sspc: add conmon performance telemetry
This provides a way to get ahold of LWS_WITH_CONMON telemetry from Secure
Streams, it works the same with direct onward connections or via the proxy.
You can mark streamtypes with a "perf": true policy attribute... this
causes the onward connections on those streamtypes to collect information
about the connection performance, and the unsorted DNS results.
Streams with that policy attribute receive extra data in their rx callback,
with the LWSSS_FLAG_PERF_JSON flag set on it, containing JSON describing the
performance of the onward connection taken from CONMON data, in a JSON
representation. Streams without the "perf" attribute set never receive
this extra rx.
The received JSON is based on the CONMON struct info and looks like
{"peer":"46.105.127.147","dns_us":596,"sockconn_us":31382,"tls_us":28180,"txn_resp_us:23015,"dns":["2001:41d0:2:ee93::1","46.105.127.147"]}
A new minimal example minimal-secure-streams-perf is added that collects
this data on an HTTP GET from warmcat.com, and is built with a -client
version as well if LWS_WITH_SECURE_STREAMS_PROXY_API is set, that operates
via the ss proxy and produces the same result at the client.
2021-03-31 13:20:34 +01:00
|
|
|
|
2020-08-26 11:05:41 +01:00
|
|
|
r = lws_ss_event_helper(h, LWSSSCS_UNREACHABLE);
|
2020-02-29 12:37:24 +00:00
|
|
|
h->wsi = NULL;
|
2020-08-26 11:05:41 +01:00
|
|
|
|
2020-06-15 15:02:52 +01:00
|
|
|
if (h->u.mqtt.heap_baggage) {
|
|
|
|
lws_free(h->u.mqtt.heap_baggage);
|
|
|
|
h->u.mqtt.heap_baggage = NULL;
|
|
|
|
}
|
|
|
|
|
2020-08-26 11:05:41 +01:00
|
|
|
if (r == LWSSSSRET_DESTROY_ME)
|
2020-12-22 15:56:41 +00:00
|
|
|
return _lws_ss_handle_state_ret_CAN_DESTROY_HANDLE(r, wsi, &h);
|
2020-08-26 11:05:41 +01:00
|
|
|
|
|
|
|
r = lws_ss_backoff(h);
|
|
|
|
if (r != LWSSSSRET_OK)
|
2020-12-22 15:56:41 +00:00
|
|
|
return _lws_ss_handle_state_ret_CAN_DESTROY_HANDLE(r, wsi, &h);
|
2020-08-26 11:05:41 +01:00
|
|
|
|
2020-02-29 12:37:24 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case LWS_CALLBACK_MQTT_CLIENT_CLOSED:
|
|
|
|
if (!h)
|
|
|
|
break;
|
2020-06-30 16:42:37 +01:00
|
|
|
lws_sul_cancel(&h->sul_timeout);
|
2021-08-03 15:25:39 +08:00
|
|
|
#if defined(LWS_WITH_CONMON)
|
ss: sspc: add conmon performance telemetry
This provides a way to get ahold of LWS_WITH_CONMON telemetry from Secure
Streams, it works the same with direct onward connections or via the proxy.
You can mark streamtypes with a "perf": true policy attribute... this
causes the onward connections on those streamtypes to collect information
about the connection performance, and the unsorted DNS results.
Streams with that policy attribute receive extra data in their rx callback,
with the LWSSS_FLAG_PERF_JSON flag set on it, containing JSON describing the
performance of the onward connection taken from CONMON data, in a JSON
representation. Streams without the "perf" attribute set never receive
this extra rx.
The received JSON is based on the CONMON struct info and looks like
{"peer":"46.105.127.147","dns_us":596,"sockconn_us":31382,"tls_us":28180,"txn_resp_us:23015,"dns":["2001:41d0:2:ee93::1","46.105.127.147"]}
A new minimal example minimal-secure-streams-perf is added that collects
this data on an HTTP GET from warmcat.com, and is built with a -client
version as well if LWS_WITH_SECURE_STREAMS_PROXY_API is set, that operates
via the ss proxy and produces the same result at the client.
2021-03-31 13:20:34 +01:00
|
|
|
lws_conmon_ss_json(h);
|
2021-08-03 15:25:39 +08:00
|
|
|
#endif
|
2021-04-13 08:23:13 -07:00
|
|
|
if (h->ss_dangling_connected)
|
|
|
|
r = lws_ss_event_helper(h, LWSSSCS_DISCONNECTED);
|
|
|
|
else
|
|
|
|
r = lws_ss_event_helper(h, LWSSSCS_UNREACHABLE);
|
2020-02-29 12:37:24 +00:00
|
|
|
if (h->wsi)
|
|
|
|
lws_set_opaque_user_data(h->wsi, NULL);
|
|
|
|
h->wsi = NULL;
|
2020-06-15 15:02:52 +01:00
|
|
|
|
|
|
|
if (h->u.mqtt.heap_baggage) {
|
|
|
|
lws_free(h->u.mqtt.heap_baggage);
|
|
|
|
h->u.mqtt.heap_baggage = NULL;
|
|
|
|
}
|
|
|
|
|
2020-08-26 11:05:41 +01:00
|
|
|
if (r)
|
2020-12-22 15:56:41 +00:00
|
|
|
return _lws_ss_handle_state_ret_CAN_DESTROY_HANDLE(r, wsi, &h);
|
2020-02-29 12:37:24 +00:00
|
|
|
|
|
|
|
if (h->policy && !(h->policy->flags & LWSSSPOLF_OPPORTUNISTIC) &&
|
2020-08-26 11:05:41 +01:00
|
|
|
!h->txn_ok && !wsi->a.context->being_destroyed) {
|
|
|
|
r = lws_ss_backoff(h);
|
|
|
|
if (r != LWSSSSRET_OK)
|
2020-12-22 15:56:41 +00:00
|
|
|
return _lws_ss_handle_state_ret_CAN_DESTROY_HANDLE(r, wsi, &h);
|
2020-08-26 11:05:41 +01:00
|
|
|
}
|
2020-02-29 12:37:24 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case LWS_CALLBACK_MQTT_CLIENT_ESTABLISHED:
|
|
|
|
/*
|
|
|
|
* Make sure the handle wsi points to the stream wsi not the
|
|
|
|
* original nwsi, in the case it was migrated
|
|
|
|
*/
|
|
|
|
h->wsi = wsi;
|
|
|
|
h->retry = 0;
|
|
|
|
h->seqstate = SSSEQ_CONNECTED;
|
2021-03-22 11:58:28 -07:00
|
|
|
/*
|
|
|
|
* If a subscribe is pending on the stream, then make
|
|
|
|
* sure the SUBSCRIBE is done before signaling the
|
|
|
|
* user application.
|
|
|
|
*/
|
|
|
|
if (h->policy->u.mqtt.subscribe &&
|
|
|
|
!wsi->mqtt->done_subscribe) {
|
|
|
|
lws_callback_on_writable(wsi);
|
|
|
|
break;
|
|
|
|
}
|
2020-05-28 12:48:17 +01:00
|
|
|
lws_sul_cancel(&h->sul);
|
2021-01-06 15:08:22 +00:00
|
|
|
#if defined(LWS_WITH_SYS_METRICS)
|
|
|
|
/*
|
|
|
|
* If any hanging caliper measurement, dump it, and free any tags
|
|
|
|
*/
|
|
|
|
lws_metrics_caliper_report_hist(h->cal_txn, (struct lws *)NULL);
|
|
|
|
#endif
|
2020-08-26 11:05:41 +01:00
|
|
|
r = lws_ss_event_helper(h, LWSSSCS_CONNECTED);
|
|
|
|
if (r != LWSSSSRET_OK)
|
2020-12-22 15:56:41 +00:00
|
|
|
return _lws_ss_handle_state_ret_CAN_DESTROY_HANDLE(r, wsi, &h);
|
2020-02-29 12:37:24 +00:00
|
|
|
if (h->policy->u.mqtt.topic)
|
|
|
|
lws_callback_on_writable(wsi);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LWS_CALLBACK_MQTT_CLIENT_RX:
|
|
|
|
// lwsl_user("LWS_CALLBACK_CLIENT_RECEIVE: read %d\n", (int)len);
|
2020-06-01 07:17:48 +01:00
|
|
|
if (!h || !h->info.rx)
|
2020-02-29 12:37:24 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
pmqpp = (lws_mqtt_publish_param_t *)in;
|
|
|
|
|
|
|
|
f = 0;
|
|
|
|
if (!pmqpp->payload_pos)
|
|
|
|
f |= LWSSS_FLAG_SOM;
|
|
|
|
if (pmqpp->payload_pos + len == pmqpp->payload_len)
|
|
|
|
f |= LWSSS_FLAG_EOM;
|
|
|
|
|
|
|
|
h->subseq = 1;
|
|
|
|
|
2020-08-26 11:05:41 +01:00
|
|
|
r = h->info.rx(ss_to_userobj(h), (const uint8_t *)pmqpp->payload,
|
|
|
|
len, f);
|
|
|
|
if (r != LWSSSSRET_OK)
|
2020-12-22 15:56:41 +00:00
|
|
|
return _lws_ss_handle_state_ret_CAN_DESTROY_HANDLE(r, wsi, &h);
|
2020-02-29 12:37:24 +00:00
|
|
|
|
|
|
|
return 0; /* don't passthru */
|
|
|
|
|
|
|
|
case LWS_CALLBACK_MQTT_SUBSCRIBED:
|
2021-03-22 11:58:28 -07:00
|
|
|
/*
|
|
|
|
* Stream demanded a subscribe while connecting, once
|
|
|
|
* done notify CONNECTED event to the application.
|
|
|
|
*/
|
|
|
|
if (wsi->mqtt->done_subscribe == 0) {
|
|
|
|
lws_sul_cancel(&h->sul);
|
|
|
|
r = lws_ss_event_helper(h, LWSSSCS_CONNECTED);
|
|
|
|
if (r != LWSSSSRET_OK)
|
|
|
|
return _lws_ss_handle_state_ret_CAN_DESTROY_HANDLE(r,
|
|
|
|
wsi, &h);
|
|
|
|
}
|
2020-02-29 12:37:24 +00:00
|
|
|
wsi->mqtt->done_subscribe = 1;
|
|
|
|
lws_callback_on_writable(wsi);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LWS_CALLBACK_MQTT_ACK:
|
2020-06-30 16:42:37 +01:00
|
|
|
lws_sul_cancel(&h->sul_timeout);
|
2020-08-26 11:05:41 +01:00
|
|
|
r = lws_ss_event_helper(h, LWSSSCS_QOS_ACK_REMOTE);
|
|
|
|
if (r != LWSSSSRET_OK)
|
2020-12-22 15:56:41 +00:00
|
|
|
return _lws_ss_handle_state_ret_CAN_DESTROY_HANDLE(r, wsi, &h);
|
2020-02-29 12:37:24 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case LWS_CALLBACK_MQTT_CLIENT_WRITEABLE:
|
2020-11-17 16:29:48 +00:00
|
|
|
{
|
2021-03-29 07:46:14 -07:00
|
|
|
size_t used_in, used_out, topic_limit;
|
2020-11-17 16:29:48 +00:00
|
|
|
lws_strexp_t exp;
|
2021-03-29 07:46:14 -07:00
|
|
|
char *expbuf;
|
2020-11-17 16:29:48 +00:00
|
|
|
|
2020-06-01 07:17:48 +01:00
|
|
|
if (!h || !h->info.tx)
|
2020-02-29 12:37:24 +00:00
|
|
|
return 0;
|
2020-12-25 05:54:19 +00:00
|
|
|
lwsl_notice("%s: %s: WRITEABLE\n", __func__, lws_ss_tag(h));
|
2020-02-29 12:37:24 +00:00
|
|
|
|
|
|
|
if (h->seqstate != SSSEQ_CONNECTED) {
|
|
|
|
lwsl_warn("%s: seqstate %d\n", __func__, h->seqstate);
|
|
|
|
break;
|
|
|
|
}
|
2021-03-29 07:46:14 -07:00
|
|
|
if (h->policy->u.mqtt.aws_iot)
|
|
|
|
topic_limit = LWS_MQTT_MAX_AWSIOT_TOPICLEN;
|
|
|
|
else
|
|
|
|
topic_limit = LWS_MQTT_MAX_TOPICLEN;
|
2020-02-29 12:37:24 +00:00
|
|
|
|
2020-06-15 15:02:52 +01:00
|
|
|
if (h->policy->u.mqtt.subscribe &&
|
|
|
|
!wsi->mqtt->done_subscribe) {
|
2021-03-29 07:46:14 -07:00
|
|
|
lws_strexp_init(&exp, (void *)h, lws_ss_exp_cb_metadata,
|
|
|
|
NULL, topic_limit);
|
|
|
|
/*
|
|
|
|
* Expand with no output first to calculate the size of
|
|
|
|
* expanded string then, allocate new buffer and expand
|
|
|
|
* again with the buffer
|
|
|
|
*/
|
|
|
|
if (lws_strexp_expand(&exp, h->policy->u.mqtt.subscribe,
|
|
|
|
strlen(h->policy->u.mqtt.subscribe),
|
|
|
|
&used_in, &used_out) != LSTRX_DONE) {
|
|
|
|
lwsl_err("%s, failed to expand MQTT subscribe"
|
|
|
|
" topic with no output\n", __func__);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
expbuf = lws_malloc(used_out + 1, __func__);
|
|
|
|
if (!expbuf) {
|
|
|
|
lwsl_err("%s, failed to allocate MQTT subscribe"
|
|
|
|
"topic", __func__);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
lws_strexp_init(&exp, (void *)h, lws_ss_exp_cb_metadata,
|
|
|
|
expbuf, used_out);
|
2020-11-17 16:29:48 +00:00
|
|
|
|
|
|
|
if (lws_strexp_expand(&exp, h->policy->u.mqtt.subscribe,
|
|
|
|
strlen(h->policy->u.mqtt.subscribe),
|
2021-03-22 12:38:08 -07:00
|
|
|
&used_in, &used_out) != LSTRX_DONE) {
|
2021-03-29 07:46:14 -07:00
|
|
|
lwsl_err("%s, failed to expand MQTT subscribe topic\n",
|
2021-03-22 12:38:08 -07:00
|
|
|
__func__);
|
2021-03-29 07:46:14 -07:00
|
|
|
lws_free(expbuf);
|
2020-11-17 16:29:48 +00:00
|
|
|
return 1;
|
2021-03-22 12:38:08 -07:00
|
|
|
}
|
2020-11-17 16:29:48 +00:00
|
|
|
lwsl_notice("%s, expbuf - %s\n", __func__, expbuf);
|
|
|
|
h->u.mqtt.sub_top.name = expbuf;
|
2020-02-29 12:37:24 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* The policy says to subscribe to something, and we
|
2020-06-15 15:02:52 +01:00
|
|
|
* haven't done it yet. Do it using the pre-prepared
|
|
|
|
* string-substituted version of the policy string.
|
2020-02-29 12:37:24 +00:00
|
|
|
*/
|
|
|
|
|
2020-06-15 15:02:52 +01:00
|
|
|
lwsl_notice("%s: subscribing %s\n", __func__,
|
2020-11-17 16:29:48 +00:00
|
|
|
h->u.mqtt.sub_top.name);
|
2020-02-29 12:37:24 +00:00
|
|
|
|
|
|
|
h->u.mqtt.sub_top.qos = h->policy->u.mqtt.qos;
|
|
|
|
memset(&h->u.mqtt.sub_info, 0, sizeof(h->u.mqtt.sub_info));
|
|
|
|
h->u.mqtt.sub_info.num_topics = 1;
|
|
|
|
h->u.mqtt.sub_info.topic = &h->u.mqtt.sub_top;
|
|
|
|
|
|
|
|
if (lws_mqtt_client_send_subcribe(wsi, &h->u.mqtt.sub_info)) {
|
|
|
|
lwsl_notice("%s: unable to subscribe", __func__);
|
2021-03-29 07:46:14 -07:00
|
|
|
lws_free(expbuf);
|
|
|
|
h->u.mqtt.sub_top.name = NULL;
|
2020-02-29 12:37:24 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2021-03-29 07:46:14 -07:00
|
|
|
lws_free(expbuf);
|
|
|
|
h->u.mqtt.sub_top.name = NULL;
|
2021-03-22 11:58:28 -07:00
|
|
|
/* Expect a SUBACK */
|
|
|
|
if (lws_change_pollfd(wsi, 0, LWS_POLLIN)) {
|
|
|
|
lwsl_err("%s: Unable to set LWS_POLLIN\n", __func__);
|
|
|
|
return -1;
|
|
|
|
}
|
2020-02-29 12:37:24 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
buflen = sizeof(buf) - LWS_PRE;
|
2020-08-26 11:05:41 +01:00
|
|
|
r = h->info.tx(ss_to_userobj(h), h->txord++, buf + LWS_PRE,
|
|
|
|
&buflen, &f);
|
|
|
|
if (r == LWSSSSRET_TX_DONT_SEND)
|
2020-02-29 12:37:24 +00:00
|
|
|
return 0;
|
2020-08-26 11:05:41 +01:00
|
|
|
|
|
|
|
if (r < 0)
|
2020-12-22 15:56:41 +00:00
|
|
|
return _lws_ss_handle_state_ret_CAN_DESTROY_HANDLE(r, wsi, &h);
|
2020-02-29 12:37:24 +00:00
|
|
|
|
|
|
|
memset(&mqpp, 0, sizeof(mqpp));
|
2020-06-15 15:02:52 +01:00
|
|
|
/* this is the string-substituted h->policy->u.mqtt.topic */
|
|
|
|
mqpp.topic = (char *)h->u.mqtt.topic_qos.name;
|
2021-03-29 07:46:14 -07:00
|
|
|
lws_strexp_init(&exp, h, lws_ss_exp_cb_metadata, NULL,
|
|
|
|
topic_limit);
|
2020-11-17 16:29:48 +00:00
|
|
|
|
|
|
|
if (lws_strexp_expand(&exp, h->policy->u.mqtt.topic,
|
|
|
|
strlen(h->policy->u.mqtt.topic),
|
2021-03-29 07:46:14 -07:00
|
|
|
&used_in, &used_out) != LSTRX_DONE) {
|
|
|
|
lwsl_err("%s, failed to expand MQTT publish"
|
|
|
|
" topic with no output\n", __func__);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
expbuf = lws_malloc(used_out + 1, __func__);
|
|
|
|
if (!expbuf) {
|
|
|
|
lwsl_err("%s, failed to allocate MQTT publish topic",
|
|
|
|
__func__);
|
2020-11-17 16:29:48 +00:00
|
|
|
return 1;
|
2021-03-29 07:46:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
lws_strexp_init(&exp, (void *)h, lws_ss_exp_cb_metadata, expbuf,
|
|
|
|
used_out);
|
|
|
|
|
|
|
|
if (lws_strexp_expand(&exp, h->policy->u.mqtt.topic,
|
|
|
|
strlen(h->policy->u.mqtt.topic), &used_in,
|
|
|
|
&used_out) != LSTRX_DONE) {
|
|
|
|
lws_free(expbuf);
|
|
|
|
return 1;
|
|
|
|
}
|
2020-11-17 16:29:48 +00:00
|
|
|
lwsl_notice("%s, expbuf - %s\n", __func__, expbuf);
|
|
|
|
mqpp.topic = (char *)expbuf;
|
|
|
|
|
2020-12-12 06:21:40 +00:00
|
|
|
mqpp.topic_len = (uint16_t)strlen(mqpp.topic);
|
|
|
|
mqpp.packet_id = (uint16_t)(h->txord - 1);
|
2020-02-29 12:37:24 +00:00
|
|
|
mqpp.payload = buf + LWS_PRE;
|
|
|
|
if (h->writeable_len)
|
2020-12-12 06:21:40 +00:00
|
|
|
mqpp.payload_len = (uint32_t)h->writeable_len;
|
2020-02-29 12:37:24 +00:00
|
|
|
else
|
2020-12-12 06:21:40 +00:00
|
|
|
mqpp.payload_len = (uint32_t)buflen;
|
2020-02-29 12:37:24 +00:00
|
|
|
|
2020-06-15 15:02:52 +01:00
|
|
|
lwsl_notice("%s: payload len %d\n", __func__,
|
|
|
|
(int)mqpp.payload_len);
|
2020-02-29 12:37:24 +00:00
|
|
|
|
|
|
|
mqpp.qos = h->policy->u.mqtt.qos;
|
|
|
|
|
|
|
|
if (lws_mqtt_client_send_publish(wsi, &mqpp,
|
2020-12-12 06:21:40 +00:00
|
|
|
(const char *)buf + LWS_PRE,
|
|
|
|
(uint32_t)buflen,
|
2020-02-29 12:37:24 +00:00
|
|
|
f & LWSSS_FLAG_EOM)) {
|
|
|
|
lwsl_notice("%s: failed to publish\n", __func__);
|
2021-03-29 07:46:14 -07:00
|
|
|
lws_free(expbuf);
|
2020-02-29 12:37:24 +00:00
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
2021-03-29 07:46:14 -07:00
|
|
|
lws_free(expbuf);
|
2020-02-29 12:37:24 +00:00
|
|
|
|
|
|
|
return 0;
|
2020-11-17 16:29:48 +00:00
|
|
|
}
|
2020-02-29 12:37:24 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return lws_callback_http_dummy(wsi, reason, user, in, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
const struct lws_protocols protocol_secstream_mqtt = {
|
|
|
|
"lws-secstream-mqtt",
|
|
|
|
secstream_mqtt,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
};
|
|
|
|
/*
|
|
|
|
* Munge connect info according to protocol-specific considerations... this
|
|
|
|
* usually means interpreting aux in a protocol-specific way and using the
|
|
|
|
* pieces at connection setup time, eg, http url pieces.
|
|
|
|
*
|
|
|
|
* len bytes of buf can be used for things with scope until after the actual
|
|
|
|
* connect.
|
|
|
|
*
|
|
|
|
* For ws, protocol aux is <url path>;<ws subprotocol name>
|
|
|
|
*/
|
|
|
|
|
2020-06-15 15:02:52 +01:00
|
|
|
enum {
|
|
|
|
SSCMM_STRSUB_WILL_TOPIC,
|
|
|
|
SSCMM_STRSUB_WILL_MESSAGE,
|
|
|
|
SSCMM_STRSUB_SUBSCRIBE,
|
|
|
|
SSCMM_STRSUB_TOPIC
|
|
|
|
};
|
|
|
|
|
2020-02-29 12:37:24 +00:00
|
|
|
static int
|
|
|
|
secstream_connect_munge_mqtt(lws_ss_handle_t *h, char *buf, size_t len,
|
|
|
|
struct lws_client_connect_info *i,
|
|
|
|
union lws_ss_contemp *ct)
|
|
|
|
{
|
2020-06-15 15:02:52 +01:00
|
|
|
const char *sources[4] = {
|
|
|
|
/* we're going to string-substitute these before use */
|
|
|
|
h->policy->u.mqtt.will_topic,
|
|
|
|
h->policy->u.mqtt.will_message,
|
|
|
|
h->policy->u.mqtt.subscribe,
|
|
|
|
h->policy->u.mqtt.topic
|
|
|
|
};
|
|
|
|
size_t used_in, olen[4] = { 0, 0, 0, 0 }, tot = 0;
|
|
|
|
lws_strexp_t exp;
|
2020-11-17 16:29:48 +00:00
|
|
|
char *ps[4];
|
|
|
|
uint8_t *p = NULL;
|
|
|
|
int n = -1;
|
|
|
|
size_t blen;
|
|
|
|
lws_system_blob_t *b = NULL;
|
2020-06-15 15:02:52 +01:00
|
|
|
|
2020-02-29 12:37:24 +00:00
|
|
|
memset(&ct->ccp, 0, sizeof(ct->ccp));
|
2020-11-17 16:29:48 +00:00
|
|
|
b = lws_system_get_blob(i->context,
|
|
|
|
LWS_SYSBLOB_TYPE_MQTT_CLIENT_ID, 0);
|
|
|
|
|
|
|
|
/* If LWS_SYSBLOB_TYPE_MQTT_CLIENT_ID is set */
|
|
|
|
if (b && (blen = lws_system_blob_get_size(b))) {
|
|
|
|
if (blen > LWS_MQTT_MAX_CIDLEN) {
|
|
|
|
lwsl_err("%s - Client ID too long.\n",
|
|
|
|
__func__);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
p = (uint8_t *)lws_zalloc(blen+1, __func__);
|
|
|
|
n = lws_system_blob_get(b, p, &blen, 0);
|
|
|
|
if (n) {
|
|
|
|
ct->ccp.client_id = NULL;
|
|
|
|
} else {
|
|
|
|
ct->ccp.client_id = (const char *)p;
|
|
|
|
lwsl_notice("%s - Client ID = %s\n",
|
|
|
|
__func__, ct->ccp.client_id);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* Default (Random) client ID */
|
|
|
|
ct->ccp.client_id = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
b = lws_system_get_blob(i->context,
|
|
|
|
LWS_SYSBLOB_TYPE_MQTT_USERNAME, 0);
|
|
|
|
|
|
|
|
/* If LWS_SYSBLOB_TYPE_MQTT_USERNAME is set */
|
|
|
|
if (b && (blen = lws_system_blob_get_size(b))) {
|
|
|
|
p = (uint8_t *)lws_zalloc(blen+1, __func__);
|
|
|
|
n = lws_system_blob_get(b, p, &blen, 0);
|
|
|
|
if (n) {
|
|
|
|
ct->ccp.username = NULL;
|
|
|
|
} else {
|
|
|
|
ct->ccp.username = (const char *)p;
|
|
|
|
lwsl_notice("%s - Username ID = %s\n",
|
|
|
|
__func__, ct->ccp.username);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
b = lws_system_get_blob(i->context,
|
|
|
|
LWS_SYSBLOB_TYPE_MQTT_PASSWORD, 0);
|
|
|
|
|
|
|
|
/* If LWS_SYSBLOB_TYPE_MQTT_PASSWORD is set */
|
|
|
|
if (b && (blen = lws_system_blob_get_size(b))) {
|
|
|
|
p = (uint8_t *)lws_zalloc(blen+1, __func__);
|
|
|
|
n = lws_system_blob_get(b, p, &blen, 0);
|
|
|
|
if (n) {
|
|
|
|
ct->ccp.password = NULL;
|
|
|
|
} else {
|
|
|
|
ct->ccp.password = (const char *)p;
|
|
|
|
lwsl_notice("%s - Password ID = %s\n",
|
|
|
|
__func__, ct->ccp.password);
|
|
|
|
}
|
|
|
|
}
|
2020-02-29 12:37:24 +00:00
|
|
|
|
2020-03-02 15:23:59 +00:00
|
|
|
ct->ccp.keep_alive = h->policy->u.mqtt.keep_alive;
|
|
|
|
ct->ccp.clean_start = h->policy->u.mqtt.clean_start;
|
|
|
|
ct->ccp.will_param.qos = h->policy->u.mqtt.will_qos;
|
|
|
|
ct->ccp.will_param.retain = h->policy->u.mqtt.will_retain;
|
2021-03-29 07:46:14 -07:00
|
|
|
ct->ccp.aws_iot = h->policy->u.mqtt.aws_iot;
|
2020-06-15 15:02:52 +01:00
|
|
|
h->u.mqtt.topic_qos.qos = h->policy->u.mqtt.qos;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We're going to string-substitute several of these parameters, which
|
|
|
|
* have unknown, possibly large size. And, as their usage is deferred
|
|
|
|
* inside the asynchronous lifetime of the MQTT connection, they need
|
|
|
|
* to live on the heap.
|
|
|
|
*
|
|
|
|
* Notice these allocations at h->u.mqtt.heap_baggage belong to the
|
|
|
|
* underlying MQTT stream lifetime, not the logical SS lifetime, and
|
|
|
|
* are destroyed if present at connection error or close of the
|
|
|
|
* underlying connection.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* First, compute the length of each without producing strsubst output,
|
|
|
|
* and keep a running total.
|
|
|
|
*/
|
|
|
|
|
|
|
|
for (n = 0; n < (int)LWS_ARRAY_SIZE(sources); n++) {
|
|
|
|
lws_strexp_init(&exp, (void *)h, lws_ss_exp_cb_metadata,
|
|
|
|
NULL, (size_t)-1);
|
|
|
|
if (lws_strexp_expand(&exp, sources[n], strlen(sources[n]),
|
|
|
|
&used_in, &olen[n]) != LSTRX_DONE) {
|
|
|
|
lwsl_err("%s: failed to subsitute %s\n", __func__,
|
|
|
|
sources[n]);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
tot += olen[n] + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Then, allocate enough space on the heap for the total of the
|
|
|
|
* substituted results
|
|
|
|
*/
|
|
|
|
|
|
|
|
h->u.mqtt.heap_baggage = lws_malloc(tot, __func__);
|
|
|
|
if (!h->u.mqtt.heap_baggage)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Finally, issue the subsitutions one after the other into the single
|
|
|
|
* allocated result buffer and prepare pointers into them
|
|
|
|
*/
|
|
|
|
|
|
|
|
p = h->u.mqtt.heap_baggage;
|
|
|
|
for (n = 0; n < (int)LWS_ARRAY_SIZE(sources); n++) {
|
|
|
|
lws_strexp_init(&exp, (void *)h, lws_ss_exp_cb_metadata,
|
2020-11-17 16:29:48 +00:00
|
|
|
(char *)p, (size_t)-1);
|
2021-07-08 10:11:09 +01:00
|
|
|
if (!sources[n]) {
|
|
|
|
ps[n] = NULL;
|
|
|
|
continue;
|
|
|
|
}
|
2020-11-17 16:29:48 +00:00
|
|
|
ps[n] = (char *)p;
|
2020-06-15 15:02:52 +01:00
|
|
|
if (lws_strexp_expand(&exp, sources[n], strlen(sources[n]),
|
|
|
|
&used_in, &olen[n]) != LSTRX_DONE)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
p += olen[n] + 1;
|
|
|
|
}
|
2020-02-29 12:37:24 +00:00
|
|
|
|
2020-06-15 15:02:52 +01:00
|
|
|
/*
|
|
|
|
* Point the guys who want the substituted content at the substituted
|
|
|
|
* strings
|
|
|
|
*/
|
2020-02-29 12:37:24 +00:00
|
|
|
|
2020-06-15 15:02:52 +01:00
|
|
|
ct->ccp.will_param.topic = ps[SSCMM_STRSUB_WILL_TOPIC];
|
|
|
|
ct->ccp.will_param.message = ps[SSCMM_STRSUB_WILL_MESSAGE];
|
|
|
|
h->u.mqtt.subscribe_to = ps[SSCMM_STRSUB_SUBSCRIBE];
|
|
|
|
h->u.mqtt.subscribe_to_len = olen[SSCMM_STRSUB_SUBSCRIBE];
|
|
|
|
h->u.mqtt.topic_qos.name = ps[SSCMM_STRSUB_TOPIC];
|
2020-02-29 12:37:24 +00:00
|
|
|
|
|
|
|
i->method = "MQTT";
|
|
|
|
i->mqtt_cp = &ct->ccp;
|
|
|
|
|
|
|
|
i->alpn = "x-amzn-mqtt-ca";
|
|
|
|
|
|
|
|
/* share connections where possible */
|
|
|
|
i->ssl_connection |= LCCSCF_PIPELINE;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const struct ss_pcols ss_pcol_mqtt = {
|
|
|
|
"MQTT",
|
|
|
|
"x-amzn-mqtt-ca", //"mqtt/3.1.1",
|
2020-07-27 10:03:12 +01:00
|
|
|
&protocol_secstream_mqtt,
|
2020-02-29 12:37:24 +00:00
|
|
|
secstream_connect_munge_mqtt
|
|
|
|
};
|