summaryrefslogtreecommitdiffstats
path: root/src/rule.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/rule.c')
-rw-r--r--src/rule.c80
1 files changed, 40 insertions, 40 deletions
diff --git a/src/rule.c b/src/rule.c
index 8d7e068..e16e2c1 100644
--- a/src/rule.c
+++ b/src/rule.c
@@ -115,21 +115,11 @@ int nftnl_rule_set_data(struct nftnl_rule *r, uint16_t attr,
switch(attr) {
case NFTNL_RULE_TABLE:
- if (r->flags & (1 << NFTNL_RULE_TABLE))
- xfree(r->table);
-
- r->table = strdup(data);
- if (!r->table)
- return -1;
- break;
+ return nftnl_set_str_attr(&r->table, &r->flags,
+ attr, data, data_len);
case NFTNL_RULE_CHAIN:
- if (r->flags & (1 << NFTNL_RULE_CHAIN))
- xfree(r->chain);
-
- r->chain = strdup(data);
- if (!r->chain)
- return -1;
- break;
+ return nftnl_set_str_attr(&r->chain, &r->flags,
+ attr, data, data_len);
case NFTNL_RULE_HANDLE:
memcpy(&r->handle, data, sizeof(r->handle));
break;
@@ -330,6 +320,12 @@ void nftnl_rule_add_expr(struct nftnl_rule *r, struct nftnl_expr *expr)
list_add_tail(&expr->head, &r->expr_list);
}
+EXPORT_SYMBOL(nftnl_rule_del_expr);
+void nftnl_rule_del_expr(struct nftnl_expr *expr)
+{
+ list_del(&expr->head);
+}
+
static int nftnl_rule_parse_attr_cb(const struct nlattr *attr, void *data)
{
const struct nlattr **tb = data;
@@ -539,50 +535,59 @@ int nftnl_rule_parse_file(struct nftnl_rule *r, enum nftnl_parse_type type,
return nftnl_rule_do_parse(r, type, fp, err, NFTNL_PARSE_FILE);
}
-static int nftnl_rule_snprintf_default(char *buf, size_t size,
+static int nftnl_rule_snprintf_default(char *buf, size_t remain,
const struct nftnl_rule *r,
uint32_t type, uint32_t flags)
{
struct nftnl_expr *expr;
- int ret, remain = size, offset = 0, i;
+ int ret, offset = 0, i;
+ const char *sep = "";
if (r->flags & (1 << NFTNL_RULE_FAMILY)) {
- ret = snprintf(buf + offset, remain, "%s ",
+ ret = snprintf(buf + offset, remain, "%s%s", sep,
nftnl_family2str(r->family));
SNPRINTF_BUFFER_SIZE(ret, remain, offset);
+ sep = " ";
}
if (r->flags & (1 << NFTNL_RULE_TABLE)) {
- ret = snprintf(buf + offset, remain, "%s ",
+ ret = snprintf(buf + offset, remain, "%s%s", sep,
r->table);
SNPRINTF_BUFFER_SIZE(ret, remain, offset);
+ sep = " ";
}
if (r->flags & (1 << NFTNL_RULE_CHAIN)) {
- ret = snprintf(buf + offset, remain, "%s ",
+ ret = snprintf(buf + offset, remain, "%s%s", sep,
r->chain);
SNPRINTF_BUFFER_SIZE(ret, remain, offset);
+ sep = " ";
}
if (r->flags & (1 << NFTNL_RULE_HANDLE)) {
- ret = snprintf(buf + offset, remain, "%llu ",
- (unsigned long long)r->handle);
+ ret = snprintf(buf + offset, remain, "%s%" PRIu64, sep,
+ r->handle);
SNPRINTF_BUFFER_SIZE(ret, remain, offset);
+ sep = " ";
}
if (r->flags & (1 << NFTNL_RULE_POSITION)) {
- ret = snprintf(buf + offset, remain, "%llu ",
- (unsigned long long)r->position);
+ ret = snprintf(buf + offset, remain, "%s%" PRIu64, sep,
+ r->position);
SNPRINTF_BUFFER_SIZE(ret, remain, offset);
+ sep = " ";
}
if (r->flags & (1 << NFTNL_RULE_ID)) {
- ret = snprintf(buf + offset, remain, "%u ", r->id);
+ ret = snprintf(buf + offset, remain, "%s%u", sep, r->id);
SNPRINTF_BUFFER_SIZE(ret, remain, offset);
+ sep = " ";
}
if (r->flags & (1 << NFTNL_RULE_POSITION_ID)) {
- ret = snprintf(buf + offset, remain, "%u ", r->position_id);
+ ret = snprintf(buf + offset, remain, "%s%u", sep,
+ r->position_id);
SNPRINTF_BUFFER_SIZE(ret, remain, offset);
+ sep = " ";
}
ret = snprintf(buf + offset, remain, "\n");
@@ -607,12 +612,13 @@ static int nftnl_rule_snprintf_default(char *buf, size_t size,
for (i = 0; i < r->user.len; i++) {
char *c = r->user.data;
- ret = snprintf(buf + offset, remain, "%c",
- isalnum(c[i]) ? c[i] : 0);
+ ret = snprintf(buf + offset, remain,
+ isprint(c[i]) ? "%c" : "\\x%02hhx",
+ c[i]);
SNPRINTF_BUFFER_SIZE(ret, remain, offset);
}
- ret = snprintf(buf + offset, remain, " }\n");
+ ret = snprintf(buf + offset, remain, " }");
SNPRINTF_BUFFER_SIZE(ret, remain, offset);
}
@@ -620,27 +626,21 @@ static int nftnl_rule_snprintf_default(char *buf, size_t size,
return offset;
}
-static int nftnl_rule_cmd_snprintf(char *buf, size_t size,
+static int nftnl_rule_cmd_snprintf(char *buf, size_t remain,
const struct nftnl_rule *r, uint32_t cmd,
uint32_t type, uint32_t flags)
{
- int ret, remain = size, offset = 0;
uint32_t inner_flags = flags;
+ int ret, offset = 0;
inner_flags &= ~NFTNL_OF_EVENT_ANY;
- switch(type) {
- case NFTNL_OUTPUT_DEFAULT:
- ret = nftnl_rule_snprintf_default(buf + offset, remain, r, type,
- inner_flags);
- SNPRINTF_BUFFER_SIZE(ret, remain, offset);
- break;
- case NFTNL_OUTPUT_JSON:
- case NFTNL_OUTPUT_XML:
- default:
+ if (type != NFTNL_OUTPUT_DEFAULT)
return -1;
- }
+ ret = nftnl_rule_snprintf_default(buf + offset, remain, r, type,
+ inner_flags);
+ SNPRINTF_BUFFER_SIZE(ret, remain, offset);
return offset;
}