Skip to content

Commit 7ab4836

Browse files
authored
Merge pull request #53 from PhilDay-CT/mqtt_retained
Add MQTT support for publishing retained messages
2 parents f8dbfd9 + 5d0b38b commit 7ab4836

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

include/mqtt.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,14 @@ int __cheri_compartment("MQTT")
137137
*
138138
* `qos` indicates the level of QoS (0, 1, or 2).
139139
*
140+
* `retain` indicates whether the message should be published as retained or
141+
* not. The broker stores the last retained message and the corresponding QoS
142+
* for that topic. Each client that subscribes to a topic pattern that matches
143+
* the topic of the retained message receives the retained message immediately
144+
* after they subscribe. The broker stores only one retained message per topic.
145+
* Retained messages are cleared by publishing a zero length message with the
146+
* retain flag set.
147+
*
140148
* Both the topic and payload buffers must remain valid during the execution of
141149
* this function. If the caller frees them during the execution of this
142150
* function, the publish may leak application data to the broker through the
@@ -170,7 +178,8 @@ int __cheri_compartment("MQTT") mqtt_publish(Timeout *t,
170178
const char *topic,
171179
size_t topicLength,
172180
const void *payload,
173-
size_t payloadLength);
181+
size_t payloadLength,
182+
bool retain = false);
174183

175184
/**
176185
* Subscribe on a given MQTT connection.

lib/mqtt/mqtt.cc

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -469,12 +469,15 @@ namespace
469469
"are not set.");
470470

471471
// The payload and topic are only valid within the
472-
// context of the callback: make them read-only and
473-
// non-capturable.
472+
// context of the callback: make them read-only,
473+
// non-capturable, and limits to the length of the
474+
// topic and payload.
474475
Capability topic{publishInfo->pTopicName};
475476
Capability payload{publishInfo->pPayload};
476477
topic.permissions() &= CHERI::Permission::Load;
478+
topic.bounds() = publishInfo->topicNameLength;
477479
payload.permissions() &= CHERI::Permission::Load;
480+
payload.bounds() = publishInfo->payloadLength;
478481

479482
publishCallback(topic,
480483
publishInfo->topicNameLength,
@@ -815,7 +818,8 @@ int mqtt_publish(Timeout *t,
815818
const char *topic,
816819
size_t topicLength,
817820
const void *payload,
818-
size_t payloadLength)
821+
size_t payloadLength,
822+
bool retain)
819823
{
820824
if (!CHERI::check_pointer(topic, topicLength))
821825
{
@@ -872,6 +876,7 @@ int mqtt_publish(Timeout *t,
872876
publishInfo.topicNameLength = topicLength;
873877
publishInfo.pPayload = payload;
874878
publishInfo.payloadLength = payloadLength;
879+
publishInfo.retain = retain;
875880

876881
// Packet ID is needed for QoS > 0.
877882
int packetId = MQTT_GetPacketId(coreMQTTContext);

0 commit comments

Comments
 (0)