2019-06-17 13:43:44

by Jakub Witowski

[permalink] [raw]
Subject: [PATCH] mesh: Return dbus error code on Leave() with invalit token given

This implements MESH_ERROR_NOT_FOUND error when we try to call Leave()
from dbus api with incorrect token value (ex. Leave(0))
---
mesh/mesh.c | 10 +++++-----
mesh/node.c | 7 +++++--
mesh/node.h | 2 +-
mesh/storage.c | 4 ++--
4 files changed, 13 insertions(+), 10 deletions(-)

diff --git a/mesh/mesh.c b/mesh/mesh.c
index 231a6bca4..f10b73331 100644
--- a/mesh/mesh.c
+++ b/mesh/mesh.c
@@ -65,7 +65,7 @@ struct bt_mesh {
uint8_t max_filters;
};

-struct join_data{
+struct join_data {
struct l_dbus_message *msg;
struct mesh_agent *agent;
const char *sender;
@@ -355,7 +355,7 @@ static void free_pending_join_call(bool failed)
mesh_agent_remove(join_pending->agent);

if (failed)
- node_remove(join_pending->node);
+ (void)node_remove(join_pending->node);

l_free(join_pending);
join_pending = NULL;
@@ -530,8 +530,7 @@ static void node_init_cb(struct mesh_node *node, struct mesh_agent *agent)

if (!acceptor_start(num_ele, join_pending->uuid, mesh.algorithms,
mesh.prov_timeout, agent, prov_complete_cb,
- &mesh))
- {
+ &mesh)) {
reply = dbus_error(join_pending->msg, MESH_ERROR_FAILED,
"Failed to start provisioning acceptor");
goto fail;
@@ -696,7 +695,8 @@ static struct l_dbus_message *leave_call(struct l_dbus *dbus,
if (!l_dbus_message_get_arguments(msg, "t", &token))
return dbus_error(msg, MESH_ERROR_INVALID_ARGS, NULL);

- node_remove(node_find_by_token(token));
+ if (!node_remove(node_find_by_token(token)))
+ return dbus_error(msg, MESH_ERROR_NOT_FOUND, NULL);

return l_dbus_message_new_method_return(msg);
}
diff --git a/mesh/node.c b/mesh/node.c
index e99858623..2b9978908 100644
--- a/mesh/node.c
+++ b/mesh/node.c
@@ -256,11 +256,12 @@ static void free_node_resources(void *data)
/*
* This function is called to free resources and remove the
* configuration files for the specified node.
+ * The false is returned when there is no node to be removed
*/
-void node_remove(struct mesh_node *node)
+bool node_remove(struct mesh_node *node)
{
if (!node)
- return;
+ return false;

l_queue_remove(nodes, node);

@@ -268,6 +269,7 @@ void node_remove(struct mesh_node *node)
storage_remove_node_config(node);

free_node_resources(node);
+ return true;
}

static bool add_models(struct mesh_node *node, struct node_element *ele,
@@ -1078,6 +1080,7 @@ static bool validate_model_property(struct node_element *ele,
while (l_dbus_message_iter_next_entry(&ids, &vendor_id,
&mod_id)) {
struct mesh_model *mod;
+
mod = l_queue_find(ele->models, match_model_id,
L_UINT_TO_PTR((vendor_id << 16) | mod_id));
if (!mod)
diff --git a/mesh/node.h b/mesh/node.h
index 142527b30..5f045ee2d 100644
--- a/mesh/node.h
+++ b/mesh/node.h
@@ -34,7 +34,7 @@ typedef void (*node_join_ready_func_t) (struct mesh_node *node,
struct mesh_agent *agent);

struct mesh_node *node_new(const uint8_t uuid[16]);
-void node_remove(struct mesh_node *node);
+bool node_remove(struct mesh_node *node);
void node_join(const char *app_path, const char *sender, const uint8_t *uuid,
node_join_ready_func_t cb);
uint8_t *node_uuid_get(struct mesh_node *node);
diff --git a/mesh/storage.c b/mesh/storage.c
index 1a9945aa8..7ae0ac5b1 100644
--- a/mesh/storage.c
+++ b/mesh/storage.c
@@ -77,7 +77,7 @@ static bool read_node_cb(struct mesh_db_node *db_node, void *user_data)
uint8_t *uuid;

if (!node_init_from_storage(node, db_node)) {
- node_remove(node);
+ (void)node_remove(node);
return false;
}

@@ -220,7 +220,7 @@ static bool parse_config(char *in_file, char *out_dir, const uint8_t uuid[16])

if (!result) {
json_object_put(jnode);
- node_remove(node);
+ (void)node_remove(node);
}

node_jconfig_set(node, jnode);
--
2.20.1


2019-06-17 22:20:12

by Stotland, Inga

[permalink] [raw]
Subject: Re: [PATCH] mesh: Return dbus error code on Leave() with invalit token given

Hi Jakub,

On Mon, 2019-06-17 at 15:41 +0200, Jakub Witowski wrote:
> This implements MESH_ERROR_NOT_FOUND error when we try to call
> Leave()
> from dbus api with incorrect token value (ex. Leave(0))
> ---
> mesh/mesh.c | 10 +++++-----
> mesh/node.c | 7 +++++--
> mesh/node.h | 2 +-
> mesh/storage.c | 4 ++--
> 4 files changed, 13 insertions(+), 10 deletions(-)
>
> diff --git a/mesh/mesh.c b/mesh/mesh.c
> index 231a6bca4..f10b73331 100644
> --- a/mesh/mesh.c
> +++ b/mesh/mesh.c
> @@ -65,7 +65,7 @@ struct bt_mesh {
> uint8_t max_filters;
> };
>
> -struct join_data{
> +struct join_data {
> struct l_dbus_message *msg;
> struct mesh_agent *agent;
> const char *sender;
> @@ -355,7 +355,7 @@ static void free_pending_join_call(bool failed)
> mesh_agent_remove(join_pending->agent);
>
> if (failed)
> - node_remove(join_pending->node);
> + (void)node_remove(join_pending->node);
>
> l_free(join_pending);
> join_pending = NULL;
> @@ -530,8 +530,7 @@ static void node_init_cb(struct mesh_node *node,
> struct mesh_agent *agent)
>
> if (!acceptor_start(num_ele, join_pending->uuid,
> mesh.algorithms,
> mesh.prov_timeout, agent,
> prov_complete_cb,
> - &mesh))
> - {
> + &mesh)) {
> reply = dbus_error(join_pending->msg,
> MESH_ERROR_FAILED,
> "Failed to start provisioning
> acceptor");
> goto fail;
> @@ -696,7 +695,8 @@ static struct l_dbus_message *leave_call(struct
> l_dbus *dbus,
> if (!l_dbus_message_get_arguments(msg, "t", &token))
> return dbus_error(msg, MESH_ERROR_INVALID_ARGS, NULL);
>
> - node_remove(node_find_by_token(token));
> + if (!node_remove(node_find_by_token(token)))
> + return dbus_error(msg, MESH_ERROR_NOT_FOUND, NULL);
>
> return l_dbus_message_new_method_return(msg);
> }
> diff --git a/mesh/node.c b/mesh/node.c
> index e99858623..2b9978908 100644
> --- a/mesh/node.c
> +++ b/mesh/node.c
> @@ -256,11 +256,12 @@ static void free_node_resources(void *data)
> /*
> * This function is called to free resources and remove the
> * configuration files for the specified node.
> + * The false is returned when there is no node to be removed
> */
> -void node_remove(struct mesh_node *node)
> +bool node_remove(struct mesh_node *node)
> {
> if (!node)
> - return;
> + return false;
>
> l_queue_remove(nodes, node);
>
> @@ -268,6 +269,7 @@ void node_remove(struct mesh_node *node)
> storage_remove_node_config(node);
>
> free_node_resources(node);
> + return true;
> }
>
> static bool add_models(struct mesh_node *node, struct node_element
> *ele,
> @@ -1078,6 +1080,7 @@ static bool validate_model_property(struct
> node_element *ele,
> while (l_dbus_message_iter_next_entry(&ids, &vendor_id,
> &mod_id
> )) {
> struct mesh_model *mod;
> +
> mod = l_queue_find(ele->models, match_model_id,
> L_UINT_TO_PTR((vendor_id << 16) |
> mod_id));
> if (!mod)
> diff --git a/mesh/node.h b/mesh/node.h
> index 142527b30..5f045ee2d 100644
> --- a/mesh/node.h
> +++ b/mesh/node.h
> @@ -34,7 +34,7 @@ typedef void (*node_join_ready_func_t) (struct
> mesh_node *node,
> struct mesh_agent
> *agent);
>
> struct mesh_node *node_new(const uint8_t uuid[16]);
> -void node_remove(struct mesh_node *node);
> +bool node_remove(struct mesh_node *node);
> void node_join(const char *app_path, const char *sender, const
> uint8_t *uuid,
> node_join_ready_func_t
> cb);
> uint8_t *node_uuid_get(struct mesh_node *node);
> diff --git a/mesh/storage.c b/mesh/storage.c
> index 1a9945aa8..7ae0ac5b1 100644
> --- a/mesh/storage.c
> +++ b/mesh/storage.c
> @@ -77,7 +77,7 @@ static bool read_node_cb(struct mesh_db_node
> *db_node, void *user_data)
> uint8_t *uuid;
>
> if (!node_init_from_storage(node, db_node)) {
> - node_remove(node);
> + (void)node_remove(node);
> return false;
> }
>
> @@ -220,7 +220,7 @@ static bool parse_config(char *in_file, char
> *out_dir, const uint8_t uuid[16])
>
> if (!result) {
> json_object_put(jnode);
> - node_remove(node);
> + (void)node_remove(node);
> }
>
> node_jconfig_set(node, jnode);

A better fix would be to remove the "NotFound" error from doc/mesh-
api.txt altogether: effectively, if the node wasn't found, it's been
successfully removed.

Best regards,
Inga



Attachments:
smime.p7s (3.19 kB)