20#include <libnftnl/common.h>
22#include <linux/netfilter.h>
23#include <linux/netfilter/nf_tables.h>
25static const char *
const nftnl_family_str[NFPROTO_NUMPROTO] = {
26 [NFPROTO_INET] =
"inet",
27 [NFPROTO_IPV4] =
"ip",
28 [NFPROTO_ARP] =
"arp",
29 [NFPROTO_NETDEV] =
"netdev",
30 [NFPROTO_BRIDGE] =
"bridge",
31 [NFPROTO_IPV6] =
"ip6",
34const char *nftnl_family2str(uint32_t family)
36 if (family >= NFPROTO_NUMPROTO || !nftnl_family_str[family])
39 return nftnl_family_str[family];
42const char *nftnl_verdict2str(uint32_t verdict)
72enum nftnl_cmd_type nftnl_flag2cmd(uint32_t flags)
74 if (flags & NFTNL_OF_EVENT_NEW)
76 else if (flags & NFTNL_OF_EVENT_DEL)
77 return NFTNL_CMD_DELETE;
79 return NFTNL_CMD_UNSPEC;
82int nftnl_fprintf(FILE *fp,
const void *obj, uint32_t cmd, uint32_t type,
84 int (*snprintf_cb)(
char *buf,
size_t bufsiz,
const void *obj,
85 uint32_t cmd, uint32_t type,
88 char _buf[NFTNL_SNPRINTF_BUFSIZ];
90 size_t bufsiz =
sizeof(_buf);
93 ret = snprintf_cb(buf, bufsiz, obj, cmd, type, flags);
97 if (ret >= NFTNL_SNPRINTF_BUFSIZ) {
100 buf = malloc(bufsiz);
104 ret = snprintf_cb(buf, bufsiz, obj, cmd, type, flags);
109 ret = fprintf(fp,
"%s", buf);
118void __nftnl_assert_attr_exists(uint16_t attr, uint16_t attr_max,
119 const char *filename,
int line)
121 fprintf(stderr,
"libnftnl: attribute %d > %d (maximum) assertion failed in %s:%d\n",
122 attr, attr_max, filename, line);
126void __nftnl_assert_fail(uint16_t attr,
const char *filename,
int line)
128 fprintf(stderr,
"libnftnl: attribute %d assertion failed in %s:%d\n",
129 attr, filename, line);
133void __noreturn __abi_breakage(
const char *file,
int line,
const char *reason)
135 fprintf(stderr,
"nf_tables kernel ABI is broken, contact your vendor.\n"
136 "%s:%d reason: %s\n", file, line, reason);
140int nftnl_set_str_attr(
const char **dptr, uint32_t *flags,
141 uint16_t attr,
const void *data, uint32_t data_len)
143 if (*flags & (1 << attr))
146 *dptr = strndup(data, data_len);
150 *flags |= (1 << attr);