From 3c2f89c4414f5f9df88b3bc7d71197b0a069152d Mon Sep 17 00:00:00 2001 From: Ivan Santiago Paunovic Date: Fri, 27 Mar 2020 14:31:34 -0300 Subject: [PATCH] Use common code for rmw_get_node_names and rmw_get_node_names_with_security_contexts Signed-off-by: Ivan Santiago Paunovic --- rmw_cyclonedds_cpp/src/rmw_node.cpp | 130 +++++++++++----------------- 1 file changed, 50 insertions(+), 80 deletions(-) diff --git a/rmw_cyclonedds_cpp/src/rmw_node.cpp b/rmw_cyclonedds_cpp/src/rmw_node.cpp index 4a5176f9..dbc31a3e 100644 --- a/rmw_cyclonedds_cpp/src/rmw_node.cpp +++ b/rmw_cyclonedds_cpp/src/rmw_node.cpp @@ -3037,27 +3037,28 @@ static rmw_ret_t do_for_node_user_data( return do_for_node(node_impl, f); } -extern "C" rmw_ret_t rmw_get_node_names( +extern "C" rmw_ret_t rmw_get_node_names_impl( + std::regex re, const rmw_node_t * node, rcutils_string_array_t * node_names, - rcutils_string_array_t * node_namespaces) + rcutils_string_array_t * node_namespaces, + rcutils_string_array_t * security_contexts) { RET_WRONG_IMPLID(node); auto node_impl = static_cast(node->data); - if (rmw_check_zero_rmw_string_array(node_names) != RMW_RET_OK || - rmw_check_zero_rmw_string_array(node_namespaces) != RMW_RET_OK) + if (RMW_RET_OK != rmw_check_zero_rmw_string_array(node_names) || + RMW_RET_OK != rmw_check_zero_rmw_string_array(node_namespaces)) { return RMW_RET_ERROR; } - std::vector> ns; - const auto re = std::regex("^name=([^;]*);namespace=([^;]*);", std::regex::extended); + std::vector> ns; auto oper = [&ns, re](const dds_builtintopic_participant_t & sample, const char * ud) -> bool { std::cmatch cm; static_cast(sample); if (std::regex_search(ud, cm, re)) { - ns.push_back(std::make_pair(std::string(cm[1]), std::string(cm[2]))); + ns.push_back(std::make_tuple(std::string(cm[1]), std::string(cm[2]), std::string(cm[3]))); } return true; }; @@ -3073,75 +3074,7 @@ extern "C" rmw_ret_t rmw_get_node_names( RMW_SET_ERROR_MSG(rcutils_get_error_string().str); goto fail_alloc; } - size_t i; - i = 0; - for (auto && n : ns) { - node_names->data[i] = rcutils_strdup(n.first.c_str(), allocator); - node_namespaces->data[i] = rcutils_strdup(n.second.c_str(), allocator); - if (!node_names->data[i] || !node_namespaces->data[i]) { - RMW_SET_ERROR_MSG("rmw_get_node_names for name/namespace"); - goto fail_alloc; - } - i++; - } - return RMW_RET_OK; - -fail_alloc: - if (node_names) { - if (rcutils_string_array_fini(node_names) != RCUTILS_RET_OK) { - RCUTILS_LOG_ERROR_NAMED( - "rmw_cyclonedds_cpp", - "failed to cleanup during error handling: %s", rcutils_get_error_string().str); - rcutils_reset_error(); - } - } - if (node_namespaces) { - if (rcutils_string_array_fini(node_namespaces) != RCUTILS_RET_OK) { - RCUTILS_LOG_ERROR_NAMED( - "rmw_cyclonedds_cpp", - "failed to cleanup during error handling: %s", rcutils_get_error_string().str); - rcutils_reset_error(); - } - } - return RMW_RET_BAD_ALLOC; -} - -#if RMW_VERSION_GTE(0, 8, 2) -extern "C" rmw_ret_t rmw_get_node_names_with_security_contexts( - const rmw_node_t * node, - rcutils_string_array_t * node_names, - rcutils_string_array_t * node_namespaces, - rcutils_string_array_t * security_contexts) -{ - RET_WRONG_IMPLID(node); - auto node_impl = static_cast(node->data); - if (rmw_check_zero_rmw_string_array(node_names) != RMW_RET_OK || - rmw_check_zero_rmw_string_array(node_namespaces) != RMW_RET_OK || - rmw_check_zero_rmw_string_array(security_contexts) != RMW_RET_OK) - { - return RMW_RET_ERROR; - } - - std::vector> ns; - const auto re = std::regex( - "^name=([^;]*);namespace=([^;]*);securitycontext=([^;]*);", std::regex::extended); - auto oper = - [&ns, re](const dds_builtintopic_participant_t & sample, const char * ud) -> bool { - std::cmatch cm; - static_cast(sample); - if (std::regex_search(ud, cm, re)) { - ns.push_back(std::make_tuple(std::string(cm[1]), std::string(cm[2]), std::string(cm[3]))); - } - return true; - }; - rmw_ret_t ret; - if ((ret = do_for_node_user_data(node_impl, oper)) != RMW_RET_OK) { - return ret; - } - - rcutils_allocator_t allocator = rcutils_get_default_allocator(); - if (rcutils_string_array_init(node_names, ns.size(), &allocator) != RCUTILS_RET_OK || - rcutils_string_array_init(node_namespaces, ns.size(), &allocator) != RCUTILS_RET_OK || + if (security_contexts && rcutils_string_array_init(security_contexts, ns.size(), &allocator) != RCUTILS_RET_OK) { RMW_SET_ERROR_MSG(rcutils_get_error_string().str); @@ -3149,14 +3082,20 @@ extern "C" rmw_ret_t rmw_get_node_names_with_security_contexts( } size_t i; i = 0; - for (auto && n : ns) { + for (auto & n : ns) { node_names->data[i] = rcutils_strdup(std::get<0>(n).c_str(), allocator); node_namespaces->data[i] = rcutils_strdup(std::get<1>(n).c_str(), allocator); - security_contexts->data[i] = rcutils_strdup(std::get<2>(n).c_str(), allocator); - if (!node_names->data[i] || !node_namespaces->data[i] || !security_contexts->data[i]) { - RMW_SET_ERROR_MSG("rmw_get_node_names for name/namespace/security_context"); + if (!node_names->data[i] || !node_namespaces->data[i]) { + RMW_SET_ERROR_MSG("rmw_get_node_names for name/namespace"); goto fail_alloc; } + if (security_contexts) { + security_contexts->data[i] = rcutils_strdup(std::get<2>(n).c_str(), allocator); + if (!security_contexts->data[i]) { + RMW_SET_ERROR_MSG("rmw_get_node_names for security_context"); + goto fail_alloc; + } + } i++; } return RMW_RET_OK; @@ -3188,6 +3127,37 @@ extern "C" rmw_ret_t rmw_get_node_names_with_security_contexts( } return RMW_RET_BAD_ALLOC; } + +extern "C" rmw_ret_t rmw_get_node_names( + const rmw_node_t * node, + rcutils_string_array_t * node_names, + rcutils_string_array_t * node_namespaces) +{ + return rmw_get_node_names_impl( + std::regex("^name=([^;]*);namespace=([^;]*);securitycontext=([^;]*);", std::regex::extended), + node, + node_names, + node_namespaces, + nullptr); +} + +#if RMW_VERSION_GTE(0, 8, 2) +extern "C" rmw_ret_t rmw_get_node_names_with_security_contexts( + const rmw_node_t * node, + rcutils_string_array_t * node_names, + rcutils_string_array_t * node_namespaces, + rcutils_string_array_t * security_contexts) +{ + if (RMW_RET_OK != rmw_check_zero_rmw_string_array(security_contexts)) { + return RMW_RET_ERROR; + } + return rmw_get_node_names_impl( + std::regex("^name=([^;]*);namespace=([^;]*);securitycontext=([^;]*);", std::regex::extended), + node, + node_names, + node_namespaces, + security_contexts); +} #endif static rmw_ret_t rmw_collect_data_for_endpoint(