[PATCH nft 1/2] src: introduce simple hints on incorrect object

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



 # nft add counter x test
 # nft list counter x test
 Error: No such file or directory; did you mean obj ‘test’ in table ip ‘x’?
 list counter x text
                ^^^^

Signed-off-by: Pablo Neira Ayuso <pablo@xxxxxxxxxxxxx>
---
 include/rule.h |  3 +++
 src/evaluate.c | 23 ++++++++++++++++++++---
 src/rule.c     | 24 ++++++++++++++++++++++++
 3 files changed, 47 insertions(+), 3 deletions(-)

diff --git a/include/rule.h b/include/rule.h
index 85b9f80d04fe..88fed62e7cba 100644
--- a/include/rule.h
+++ b/include/rule.h
@@ -392,6 +392,9 @@ void obj_free(struct obj *obj);
 void obj_add_hash(struct obj *obj, struct table *table);
 struct obj *obj_lookup(const struct table *table, const char *name,
 		       uint32_t type);
+struct obj *obj_lookup_fuzzy(const char *obj_name,
+			     const struct nft_cache *cache,
+			     const struct table **t);
 void obj_print(const struct obj *n, struct output_ctx *octx);
 void obj_print_plain(const struct obj *obj, struct output_ctx *octx);
 const char *obj_type_name(uint32_t type);
diff --git a/src/evaluate.c b/src/evaluate.c
index 14c76420d524..329fd42dd0a8 100644
--- a/src/evaluate.c
+++ b/src/evaluate.c
@@ -3450,6 +3450,23 @@ static int cmd_evaluate_get(struct eval_ctx *ctx, struct cmd *cmd)
 	}
 }
 
+static int obj_not_found(struct eval_ctx *ctx, const struct location *loc,
+			 const char *obj_name)
+{
+	const struct table *table;
+	struct obj *obj;
+
+	obj = obj_lookup_fuzzy(obj_name, &ctx->nft->cache, &table);
+	if (obj == NULL)
+		return cmd_error(ctx, loc, "%s", strerror(ENOENT));
+
+	return cmd_error(ctx, loc,
+			 "%s; did you mean obj ‘%s’ in table %s ‘%s’?",
+			 strerror(ENOENT), obj->handle.obj.name,
+				 family2str(obj->handle.family),
+				 table->handle.table.name);
+}
+
 static int cmd_evaluate_list_obj(struct eval_ctx *ctx, const struct cmd *cmd,
 				 uint32_t obj_type)
 {
@@ -3463,9 +3480,9 @@ static int cmd_evaluate_list_obj(struct eval_ctx *ctx, const struct cmd *cmd,
 		return table_not_found(ctx);
 
 	if (obj_lookup(table, cmd->handle.obj.name, obj_type) == NULL)
-		return cmd_error(ctx, &cmd->handle.obj.location,
-				 "Could not process rule: %s",
-				 strerror(ENOENT));
+		return obj_not_found(ctx, &cmd->handle.obj.location,
+				     cmd->handle.obj.name);
+
 	return 0;
 }
 
diff --git a/src/rule.c b/src/rule.c
index c244d0ba6b02..0a3c1970c83a 100644
--- a/src/rule.c
+++ b/src/rule.c
@@ -1710,6 +1710,30 @@ struct obj *obj_lookup(const struct table *table, const char *name,
 	return NULL;
 }
 
+struct obj *obj_lookup_fuzzy(const char *obj_name,
+			     const struct nft_cache *cache,
+			     const struct table **t)
+{
+	struct string_misspell_state st;
+	struct table *table;
+	struct obj *obj;
+
+	string_misspell_init(&st);
+
+	list_for_each_entry(table, &cache->list, list) {
+		list_for_each_entry(obj, &table->objs, list) {
+			if (!strcmp(obj->handle.obj.name, obj_name)) {
+				*t = table;
+				return obj;
+			}
+			if (string_misspell_update(obj->handle.obj.name,
+						   obj_name, obj, &st))
+				*t = table;
+		}
+	}
+	return st.obj;
+}
+
 static void print_proto_name_proto(uint8_t l4, struct output_ctx *octx)
 {
 	const struct protoent *p = getprotobynumber(l4);
-- 
2.11.0





[Index of Archives]     [Netfitler Users]     [LARTC]     [Bugtraq]     [Yosemite Forum]

  Powered by Linux