12 #include <arpa/inet.h>
16 #include <libnftnl/common.h>
18 #include <linux/netfilter.h>
19 #include <linux/netfilter/nf_tables.h>
21 static const char *
const nftnl_family_str[NFPROTO_NUMPROTO] = {
22 [NFPROTO_INET] =
"inet",
23 [NFPROTO_IPV4] =
"ip",
24 [NFPROTO_ARP] =
"arp",
25 [NFPROTO_NETDEV] =
"netdev",
26 [NFPROTO_BRIDGE] =
"bridge",
27 [NFPROTO_IPV6] =
"ip6",
30 const char *nftnl_family2str(uint32_t family)
32 if (family >= NFPROTO_NUMPROTO || !nftnl_family_str[family])
35 return nftnl_family_str[family];
38 const char *nftnl_verdict2str(uint32_t verdict)
68 enum nftnl_cmd_type nftnl_flag2cmd(uint32_t flags)
70 if (flags & NFTNL_OF_EVENT_NEW)
72 else if (flags & NFTNL_OF_EVENT_DEL)
73 return NFTNL_CMD_DELETE;
75 return NFTNL_CMD_UNSPEC;
78 int nftnl_fprintf(FILE *fp,
const void *obj, uint32_t cmd, uint32_t type,
80 int (*snprintf_cb)(
char *buf,
size_t bufsiz,
const void *obj,
81 uint32_t cmd, uint32_t type,
84 char _buf[NFTNL_SNPRINTF_BUFSIZ];
86 size_t bufsiz =
sizeof(_buf);
89 ret = snprintf_cb(buf, bufsiz, obj, cmd, type, flags);
93 if (ret >= NFTNL_SNPRINTF_BUFSIZ) {
100 ret = snprintf_cb(buf, bufsiz, obj, cmd, type, flags);
105 ret = fprintf(fp,
"%s", buf);
114 void __nftnl_assert_attr_exists(uint16_t attr, uint16_t attr_max,
115 const char *filename,
int line)
117 fprintf(stderr,
"libnftnl: attribute %d > %d (maximum) assertion failed in %s:%d\n",
118 attr, attr_max, filename, line);
122 void __nftnl_assert_fail(uint16_t attr,
const char *filename,
int line)
124 fprintf(stderr,
"libnftnl: attribute %d assertion failed in %s:%d\n",
125 attr, filename, line);
129 void __noreturn __abi_breakage(
const char *file,
int line,
const char *reason)
131 fprintf(stderr,
"nf_tables kernel ABI is broken, contact your vendor.\n"
132 "%s:%d reason: %s\n", file, line, reason);
136 int nftnl_set_str_attr(
const char **dptr, uint32_t *flags,
137 uint16_t attr,
const void *data, uint32_t data_len)
139 if (*flags & (1 << attr))
142 *dptr = strndup(data, data_len);
146 *flags |= (1 << attr);