Skip to content

Commit

Permalink
Merge pull request #53 from PhilDay-CT/mqtt_retained
Browse files Browse the repository at this point in the history
Add MQTT support for publishing retained messages
  • Loading branch information
hlef authored Nov 29, 2024
2 parents f8dbfd9 + 5d0b38b commit 7ab4836
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
11 changes: 10 additions & 1 deletion include/mqtt.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,14 @@ int __cheri_compartment("MQTT")
*
* `qos` indicates the level of QoS (0, 1, or 2).
*
* `retain` indicates whether the message should be published as retained or
* not. The broker stores the last retained message and the corresponding QoS
* for that topic. Each client that subscribes to a topic pattern that matches
* the topic of the retained message receives the retained message immediately
* after they subscribe. The broker stores only one retained message per topic.
* Retained messages are cleared by publishing a zero length message with the
* retain flag set.
*
* Both the topic and payload buffers must remain valid during the execution of
* this function. If the caller frees them during the execution of this
* function, the publish may leak application data to the broker through the
Expand Down Expand Up @@ -170,7 +178,8 @@ int __cheri_compartment("MQTT") mqtt_publish(Timeout *t,
const char *topic,
size_t topicLength,
const void *payload,
size_t payloadLength);
size_t payloadLength,
bool retain = false);

/**
* Subscribe on a given MQTT connection.
Expand Down
11 changes: 8 additions & 3 deletions lib/mqtt/mqtt.cc
Original file line number Diff line number Diff line change
Expand Up @@ -469,12 +469,15 @@ namespace
"are not set.");

// The payload and topic are only valid within the
// context of the callback: make them read-only and
// non-capturable.
// context of the callback: make them read-only,
// non-capturable, and limits to the length of the
// topic and payload.
Capability topic{publishInfo->pTopicName};
Capability payload{publishInfo->pPayload};
topic.permissions() &= CHERI::Permission::Load;
topic.bounds() = publishInfo->topicNameLength;
payload.permissions() &= CHERI::Permission::Load;
payload.bounds() = publishInfo->payloadLength;

publishCallback(topic,
publishInfo->topicNameLength,
Expand Down Expand Up @@ -815,7 +818,8 @@ int mqtt_publish(Timeout *t,
const char *topic,
size_t topicLength,
const void *payload,
size_t payloadLength)
size_t payloadLength,
bool retain)
{
if (!CHERI::check_pointer(topic, topicLength))
{
Expand Down Expand Up @@ -872,6 +876,7 @@ int mqtt_publish(Timeout *t,
publishInfo.topicNameLength = topicLength;
publishInfo.pPayload = payload;
publishInfo.payloadLength = payloadLength;
publishInfo.retain = retain;

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

0 comments on commit 7ab4836

Please sign in to comment.