Skip to content

Commit

Permalink
Add enclave to liveliness token and fix return expectation in get_ent…
Browse files Browse the repository at this point in the history
…ity_names_and_types_by_node (#208)

Signed-off-by: Yadunund <[email protected]>
  • Loading branch information
Yadunund authored Jun 24, 2024
1 parent 543738e commit b48713a
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 13 deletions.
8 changes: 3 additions & 5 deletions rmw_zenoh_cpp/src/detail/graph_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1038,15 +1038,15 @@ rmw_ret_t GraphCache::get_entity_names_and_types_by_node(
// Check if namespace exists.
NamespaceMap::const_iterator ns_it = graph_.find(node_namespace);
if (ns_it == graph_.end()) {
return RMW_RET_OK;
return RMW_RET_NODE_NAME_NON_EXISTENT;
}

// Check if node exists.
// Since NodeMap is a multimap, this will return the first node with the same
// name that is found.
NodeMap::const_iterator node_it = ns_it->second.find(node_name);
if (node_it == ns_it->second.end()) {
return RMW_RET_OK;
return RMW_RET_NODE_NAME_NON_EXISTENT;
}

// TODO(Yadunund): Support service and client when ready.
Expand All @@ -1059,10 +1059,8 @@ rmw_ret_t GraphCache::get_entity_names_and_types_by_node(
} else if (entity_type == EntityType::Client) {
return fill_names_and_types(node_it->second->clients_, allocator, names_and_types);
} else {
return RMW_RET_OK;
return RMW_RET_UNSUPPORTED;
}

return RMW_RET_OK;
}


Expand Down
11 changes: 8 additions & 3 deletions rmw_zenoh_cpp/src/detail/liveliness_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ enum KeyexprIndex
Nid,
Id,
EntityStr,
Enclave,
Namespace,
NodeName,
TopicName,
Expand Down Expand Up @@ -263,6 +264,7 @@ Entity::Entity(
// An empty namespace from rcl will contain "/" but zenoh does not allow keys with "//".
// Hence we mangle the empty namespace such that splitting the key
// will always result in 5 parts.
keyexpr_parts[KeyexprIndex::Enclave] = mangle_name(node_info_.enclave_);
keyexpr_parts[KeyexprIndex::Namespace] = mangle_name(node_info_.ns_);
keyexpr_parts[KeyexprIndex::NodeName] = mangle_name(node_info_.name_);
// If this entity has a topic info, append it to the token.
Expand Down Expand Up @@ -336,14 +338,16 @@ std::shared_ptr<Entity> Entity::make(const std::string & keyexpr)
if (parts.size() < KEYEXPR_INDEX_MIN + 1) {
RCUTILS_LOG_ERROR_NAMED(
"rmw_zenoh_cpp",
"Received invalid liveliness token");
"Received invalid liveliness token with %lu/%d parts: %s",
parts.size(),
KEYEXPR_INDEX_MIN + 1, keyexpr.c_str());
return nullptr;
}
for (const std::string & p : parts) {
if (p.empty()) {
RCUTILS_LOG_ERROR_NAMED(
"rmw_zenoh_cpp",
"Received invalid liveliness token");
"Received invalid liveliness token with empty parts: %s", keyexpr.c_str());
return nullptr;
}
}
Expand Down Expand Up @@ -371,6 +375,7 @@ std::shared_ptr<Entity> Entity::make(const std::string & keyexpr)
std::string & zid = parts[KeyexprIndex::Zid];
std::string & nid = parts[KeyexprIndex::Nid];
std::string & id = parts[KeyexprIndex::Id];
std::string enclave = demangle_name(std::move(parts[KeyexprIndex::Enclave]));
std::string ns = demangle_name(std::move(parts[KeyexprIndex::Namespace]));
std::string node_name = demangle_name(std::move(parts[KeyexprIndex::NodeName]));
std::optional<TopicInfo> topic_info = std::nullopt;
Expand Down Expand Up @@ -403,7 +408,7 @@ std::shared_ptr<Entity> Entity::make(const std::string & keyexpr)
std::move(nid),
std::move(id),
std::move(entity_type),
NodeInfo{std::move(domain_id), std::move(ns), std::move(node_name), ""},
NodeInfo{std::move(domain_id), std::move(ns), std::move(node_name), std::move(enclave)},
std::move(topic_info)});
}

Expand Down
3 changes: 3 additions & 0 deletions rmw_zenoh_cpp/src/detail/rmw_data_types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@
class rmw_context_impl_s final
{
public:
// Enclave, name used to find security artifacts in a sros2 keystore.
char * enclave;

// An owned session.
z_owned_session_t session;

Expand Down
3 changes: 3 additions & 0 deletions rmw_zenoh_cpp/src/rmw_init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,9 @@ rmw_init(const rmw_init_options_t * options, rmw_context_t * context)
}
});

// Set the enclave.
context->impl->enclave = options->enclave;

// Initialize context's implementation
context->impl->is_shutdown = false;

Expand Down
31 changes: 26 additions & 5 deletions rmw_zenoh_cpp/src/rmw_zenoh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,10 @@ rmw_create_node(
context->impl,
"expected initialized context",
return nullptr);
RMW_CHECK_FOR_NULL_WITH_MSG(
context->impl->enclave,
"expected initialized enclave",
return nullptr);
if (context->impl->is_shutdown) {
RCUTILS_SET_ERROR_MSG("context has been shutdown");
return nullptr;
Expand Down Expand Up @@ -279,7 +283,8 @@ rmw_create_node(
std::to_string(node_data->id),
std::to_string(node_data->id),
rmw_zenoh_cpp::liveliness::EntityType::Node,
rmw_zenoh_cpp::liveliness::NodeInfo{context->actual_domain_id, namespace_, name, ""});
rmw_zenoh_cpp::liveliness::NodeInfo{context->actual_domain_id, namespace_, name,
context->impl->enclave});
if (node_data->entity == nullptr) {
RCUTILS_LOG_ERROR_NAMED(
"rmw_zenoh_cpp",
Expand Down Expand Up @@ -470,6 +475,10 @@ rmw_create_publisher(
context_impl,
"unable to get rmw_context_impl_s",
return nullptr);
RMW_CHECK_FOR_NULL_WITH_MSG(
context_impl->enclave,
"expected initialized enclave",
return nullptr);
if (!z_check(context_impl->session)) {
RMW_SET_ERROR_MSG("zenoh session is invalid");
return nullptr;
Expand Down Expand Up @@ -630,7 +639,7 @@ rmw_create_publisher(
context_impl->get_next_entity_id()),
rmw_zenoh_cpp::liveliness::EntityType::Publisher,
rmw_zenoh_cpp::liveliness::NodeInfo{
node->context->actual_domain_id, node->namespace_, node->name, ""},
node->context->actual_domain_id, node->namespace_, node->name, context_impl->enclave},
rmw_zenoh_cpp::liveliness::TopicInfo{rmw_publisher->topic_name,
publisher_data->type_support->get_name(), publisher_data->adapted_qos_profile}
);
Expand Down Expand Up @@ -1258,6 +1267,10 @@ rmw_create_subscription(
context_impl,
"unable to get rmw_context_impl_s",
return nullptr);
RMW_CHECK_FOR_NULL_WITH_MSG(
context_impl->enclave,
"expected initialized enclave",
return nullptr);
if (!z_check(context_impl->session)) {
RMW_SET_ERROR_MSG("zenoh session is invalid");
return nullptr;
Expand Down Expand Up @@ -1440,7 +1453,7 @@ rmw_create_subscription(
context_impl->get_next_entity_id()),
rmw_zenoh_cpp::liveliness::EntityType::Subscription,
rmw_zenoh_cpp::liveliness::NodeInfo{
node->context->actual_domain_id, node->namespace_, node->name, ""},
node->context->actual_domain_id, node->namespace_, node->name, context_impl->enclave},
rmw_zenoh_cpp::liveliness::TopicInfo{rmw_subscription->topic_name,
sub_data->type_support->get_name(), sub_data->adapted_qos_profile}
);
Expand Down Expand Up @@ -1902,6 +1915,10 @@ rmw_create_client(
context_impl,
"unable to get rmw_context_impl_s",
return nullptr);
RMW_CHECK_FOR_NULL_WITH_MSG(
context_impl->enclave,
"expected initialized enclave",
return nullptr);
if (!z_check(context_impl->session)) {
RMW_SET_ERROR_MSG("zenoh session is invalid");
return nullptr;
Expand Down Expand Up @@ -2087,7 +2104,7 @@ rmw_create_client(
context_impl->get_next_entity_id()),
rmw_zenoh_cpp::liveliness::EntityType::Client,
rmw_zenoh_cpp::liveliness::NodeInfo{
node->context->actual_domain_id, node->namespace_, node->name, ""},
node->context->actual_domain_id, node->namespace_, node->name, context_impl->enclave},
rmw_zenoh_cpp::liveliness::TopicInfo{rmw_client->service_name,
std::move(service_type), client_data->adapted_qos_profile}
);
Expand Down Expand Up @@ -2464,6 +2481,10 @@ rmw_create_service(
context_impl,
"unable to get rmw_context_impl_s",
return nullptr);
RMW_CHECK_FOR_NULL_WITH_MSG(
context_impl->enclave,
"expected initialized enclave",
return nullptr);
if (!z_check(context_impl->session)) {
RMW_SET_ERROR_MSG("zenoh session is invalid");
return nullptr;
Expand Down Expand Up @@ -2645,7 +2666,7 @@ rmw_create_service(
context_impl->get_next_entity_id()),
rmw_zenoh_cpp::liveliness::EntityType::Service,
rmw_zenoh_cpp::liveliness::NodeInfo{
node->context->actual_domain_id, node->namespace_, node->name, ""},
node->context->actual_domain_id, node->namespace_, node->name, context_impl->enclave},
rmw_zenoh_cpp::liveliness::TopicInfo{rmw_service->service_name,
std::move(service_type), service_data->adapted_qos_profile}
);
Expand Down

0 comments on commit b48713a

Please sign in to comment.