diff options
Diffstat (limited to 'src/utils.c')
-rw-r--r-- | src/utils.c | 125 |
1 files changed, 62 insertions, 63 deletions
diff --git a/src/utils.c b/src/utils.c index 728ae30..c241e5f 100644 --- a/src/utils.c +++ b/src/utils.c @@ -22,7 +22,7 @@ #include <linux/netfilter.h> #include <linux/netfilter/nf_tables.h> -static const char *const nft_family_str[NFPROTO_NUMPROTO] = { +static const char *const nftnl_family_str[NFPROTO_NUMPROTO] = { [NFPROTO_INET] = "inet", [NFPROTO_IPV4] = "ip", [NFPROTO_ARP] = "arp", @@ -30,23 +30,23 @@ static const char *const nft_family_str[NFPROTO_NUMPROTO] = { [NFPROTO_IPV6] = "ip6", }; -const char *nft_family2str(uint32_t family) +const char *nftnl_family2str(uint32_t family) { - if (nft_family_str[family] == NULL) + if (nftnl_family_str[family] == NULL) return "unknown"; - return nft_family_str[family]; + return nftnl_family_str[family]; } -int nft_str2family(const char *family) +int nftnl_str2family(const char *family) { int i; for (i = 0; i < NFPROTO_NUMPROTO; i++) { - if (nft_family_str[i] == NULL) + if (nftnl_family_str[i] == NULL) continue; - if (strcmp(nft_family_str[i], family) == 0) + if (strcmp(nftnl_family_str[i], family) == 0) return i; } @@ -59,27 +59,26 @@ static struct { int64_t min; uint64_t max; } basetype[] = { - [NFT_TYPE_U8] = { .len = sizeof(uint8_t), .max = UINT8_MAX }, - [NFT_TYPE_U16] = { .len = sizeof(uint16_t), .max = UINT16_MAX }, - [NFT_TYPE_U32] = { .len = sizeof(uint32_t), .max = UINT32_MAX }, - [NFT_TYPE_U64] = { .len = sizeof(uint64_t), .max = UINT64_MAX }, - [NFT_TYPE_S8] = { .len = sizeof(int8_t), .min = INT8_MIN, .max = INT8_MAX }, - [NFT_TYPE_S16] = { .len = sizeof(int16_t), .min = INT16_MIN, .max = INT16_MAX }, - [NFT_TYPE_S32] = { .len = sizeof(int32_t), .min = INT32_MIN, .max = INT32_MAX }, - [NFT_TYPE_S64] = { .len = sizeof(int64_t), .min = INT64_MIN, .max = INT64_MAX }, + [NFTNL_TYPE_U8] = { .len = sizeof(uint8_t), .max = UINT8_MAX }, + [NFTNL_TYPE_U16] = { .len = sizeof(uint16_t), .max = UINT16_MAX }, + [NFTNL_TYPE_U32] = { .len = sizeof(uint32_t), .max = UINT32_MAX }, + [NFTNL_TYPE_U64] = { .len = sizeof(uint64_t), .max = UINT64_MAX }, + [NFTNL_TYPE_S8] = { .len = sizeof(int8_t), .min = INT8_MIN, .max = INT8_MAX }, + [NFTNL_TYPE_S16] = { .len = sizeof(int16_t), .min = INT16_MIN, .max = INT16_MAX }, + [NFTNL_TYPE_S32] = { .len = sizeof(int32_t), .min = INT32_MIN, .max = INT32_MAX }, + [NFTNL_TYPE_S64] = { .len = sizeof(int64_t), .min = INT64_MIN, .max = INT64_MAX }, }; - -int nft_get_value(enum nft_type type, void *val, void *out) +int nftnl_get_value(enum nftnl_type type, void *val, void *out) { int64_t sval; uint64_t uval; switch (type) { - case NFT_TYPE_U8: - case NFT_TYPE_U16: - case NFT_TYPE_U32: - case NFT_TYPE_U64: + case NFTNL_TYPE_U8: + case NFTNL_TYPE_U16: + case NFTNL_TYPE_U32: + case NFTNL_TYPE_U64: uval = *((uint64_t *)val); if (uval > basetype[type].max) { errno = ERANGE; @@ -87,10 +86,10 @@ int nft_get_value(enum nft_type type, void *val, void *out) } memcpy(out, &uval, basetype[type].len); break; - case NFT_TYPE_S8: - case NFT_TYPE_S16: - case NFT_TYPE_S32: - case NFT_TYPE_S64: + case NFTNL_TYPE_S8: + case NFTNL_TYPE_S16: + case NFTNL_TYPE_S32: + case NFTNL_TYPE_S64: sval = *((int64_t *)val); if (sval < basetype[type].min || sval > (int64_t)basetype[type].max) { @@ -104,7 +103,7 @@ int nft_get_value(enum nft_type type, void *val, void *out) return 0; } -int nft_strtoi(const char *string, int base, void *out, enum nft_type type) +int nftnl_strtoi(const char *string, int base, void *out, enum nftnl_type type) { int ret; int64_t sval = 0; @@ -112,19 +111,19 @@ int nft_strtoi(const char *string, int base, void *out, enum nft_type type) char *endptr; switch (type) { - case NFT_TYPE_U8: - case NFT_TYPE_U16: - case NFT_TYPE_U32: - case NFT_TYPE_U64: + case NFTNL_TYPE_U8: + case NFTNL_TYPE_U16: + case NFTNL_TYPE_U32: + case NFTNL_TYPE_U64: uval = strtoll(string, &endptr, base); - ret = nft_get_value(type, &uval, out); + ret = nftnl_get_value(type, &uval, out); break; - case NFT_TYPE_S8: - case NFT_TYPE_S16: - case NFT_TYPE_S32: - case NFT_TYPE_S64: + case NFTNL_TYPE_S8: + case NFTNL_TYPE_S16: + case NFTNL_TYPE_S32: + case NFTNL_TYPE_S64: sval = strtoull(string, &endptr, base); - ret = nft_get_value(type, &sval, out); + ret = nftnl_get_value(type, &sval, out); break; default: errno = EINVAL; @@ -139,7 +138,7 @@ int nft_strtoi(const char *string, int base, void *out, enum nft_type type) return ret; } -const char *nft_verdict2str(uint32_t verdict) +const char *nftnl_verdict2str(uint32_t verdict) { switch (verdict) { case NF_ACCEPT: @@ -157,7 +156,7 @@ const char *nft_verdict2str(uint32_t verdict) } } -int nft_str2verdict(const char *verdict, int *verdict_num) +int nftnl_str2verdict(const char *verdict, int *verdict_num) { if (strcmp(verdict, "accept") == 0) { *verdict_num = NF_ACCEPT; @@ -179,53 +178,53 @@ int nft_str2verdict(const char *verdict, int *verdict_num) return -1; } -enum nft_cmd_type nft_flag2cmd(uint32_t flags) +enum nftnl_cmd_type nftnl_flag2cmd(uint32_t flags) { - if (flags & NFT_OF_EVENT_NEW) - return NFT_CMD_ADD; - else if (flags & NFT_OF_EVENT_DEL) - return NFT_CMD_DELETE; + if (flags & NFTNL_OF_EVENT_NEW) + return NFTNL_CMD_ADD; + else if (flags & NFTNL_OF_EVENT_DEL) + return NFTNL_CMD_DELETE; - return NFT_CMD_UNSPEC; + return NFTNL_CMD_UNSPEC; } -const char *cmd2tag[NFT_CMD_MAX] = { - [NFT_CMD_ADD] = ADD, - [NFT_CMD_INSERT] = INSERT, - [NFT_CMD_DELETE] = DELETE, - [NFT_CMD_REPLACE] = REPLACE, - [NFT_CMD_FLUSH] = FLUSH, +const char *cmd2tag[NFTNL_CMD_MAX] = { + [NFTNL_CMD_ADD] = ADD, + [NFTNL_CMD_INSERT] = INSERT, + [NFTNL_CMD_DELETE] = DELETE, + [NFTNL_CMD_REPLACE] = REPLACE, + [NFTNL_CMD_FLUSH] = FLUSH, }; -const char *nft_cmd2tag(enum nft_cmd_type cmd) +const char *nftnl_cmd2tag(enum nftnl_cmd_type cmd) { - if (cmd >= NFT_CMD_MAX) + if (cmd >= NFTNL_CMD_MAX) return "unknown"; return cmd2tag[cmd]; } -uint32_t nft_str2cmd(const char *cmd) +uint32_t nftnl_str2cmd(const char *cmd) { if (strcmp(cmd, ADD) == 0) - return NFT_CMD_ADD; + return NFTNL_CMD_ADD; else if (strcmp(cmd, INSERT) == 0) - return NFT_CMD_INSERT; + return NFTNL_CMD_INSERT; else if (strcmp(cmd, DELETE) == 0) - return NFT_CMD_DELETE; + return NFTNL_CMD_DELETE; else if (strcmp(cmd, REPLACE) == 0) - return NFT_CMD_REPLACE; + return NFTNL_CMD_REPLACE; else if (strcmp(cmd, FLUSH) == 0) - return NFT_CMD_FLUSH; + return NFTNL_CMD_FLUSH; - return NFT_CMD_UNSPEC; + return NFTNL_CMD_UNSPEC; } -int nft_fprintf(FILE *fp, void *obj, uint32_t cmd, uint32_t type, uint32_t flags, +int nftnl_fprintf(FILE *fp, void *obj, uint32_t cmd, uint32_t type, uint32_t flags, int (*snprintf_cb)(char *buf, size_t bufsiz, void *obj, uint32_t cmd, uint32_t type, uint32_t flags)) { - char _buf[NFT_SNPRINTF_BUFSIZ]; + char _buf[NFTNL_SNPRINTF_BUFSIZ]; char *buf = _buf; size_t bufsiz = sizeof(_buf); int ret; @@ -234,7 +233,7 @@ int nft_fprintf(FILE *fp, void *obj, uint32_t cmd, uint32_t type, uint32_t flags if (ret <= 0) goto out; - if (ret >= NFT_SNPRINTF_BUFSIZ) { + if (ret >= NFTNL_SNPRINTF_BUFSIZ) { bufsiz = ret + 1; buf = malloc(bufsiz); @@ -255,7 +254,7 @@ out: return ret; } -void __nft_assert_fail(uint16_t attr, const char *filename, int line) +void __nftnl_assert_fail(uint16_t attr, const char *filename, int line) { fprintf(stderr, "libnftnl: attribute %d assertion failed in %s:%d\n", attr, filename, line); |