| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include <stdio.h> |
| 7 |
#include <stdlib.h> |
| 8 |
#include <string.h> |
| 9 |
#include <netinet/in.h> |
| 10 |
#include <linux/netfilter/nf_tables.h> |
| 11 |
|
| 12 |
#include <libnftnl/set.h> |
| 13 |
|
| 14 |
static int test_ok = 1; |
| 15 |
|
| 16 |
static void print_err(const char *msg) |
| 17 |
{ |
| 18 |
test_ok = 0; |
| 19 |
printf("\033[31mERROR:\e[0m %s\n", msg); |
| 20 |
} |
| 21 |
|
| 22 |
static void cmp_nftnl_set(struct nftnl_set *a, struct nftnl_set *b) |
| 23 |
{ |
| 24 |
const uint8_t *data_a, *data_b; |
| 25 |
uint32_t datalen_a, datalen_b; |
| 26 |
|
| 27 |
if (strcmp(nftnl_set_get_str(a, NFTNL_SET_TABLE), |
| 28 |
nftnl_set_get_str(b, NFTNL_SET_TABLE)) != 0) |
| 29 |
print_err("Set table mismatches"); |
| 30 |
if (strcmp(nftnl_set_get_str(a, NFTNL_SET_NAME), |
| 31 |
nftnl_set_get_str(b, NFTNL_SET_NAME)) != 0) |
| 32 |
print_err("Set name mismatches"); |
| 33 |
if (nftnl_set_get_u32(a, NFTNL_SET_FLAGS) != |
| 34 |
nftnl_set_get_u32(b, NFTNL_SET_FLAGS)) |
| 35 |
print_err("Set flags mismatches"); |
| 36 |
if (nftnl_set_get_u32(a, NFTNL_SET_KEY_TYPE) != |
| 37 |
nftnl_set_get_u32(b, NFTNL_SET_KEY_TYPE)) |
| 38 |
print_err("Set key-type mismatches"); |
| 39 |
if (nftnl_set_get_u32(a, NFTNL_SET_KEY_LEN) != |
| 40 |
nftnl_set_get_u32(b, NFTNL_SET_KEY_LEN)) |
| 41 |
print_err("Set key-len mismatches"); |
| 42 |
if (nftnl_set_get_u32(a, NFTNL_SET_DATA_TYPE) != |
| 43 |
nftnl_set_get_u32(b, NFTNL_SET_DATA_TYPE)) |
| 44 |
print_err("Set data-type mismatches"); |
| 45 |
if (nftnl_set_get_u32(a, NFTNL_SET_DATA_LEN) != |
| 46 |
nftnl_set_get_u32(b, NFTNL_SET_DATA_LEN)) |
| 47 |
print_err("Set data-len mismatches"); |
| 48 |
if (strcmp(nftnl_set_get_str(a, NFTNL_SET_USERDATA), |
| 49 |
nftnl_set_get_str(b, NFTNL_SET_USERDATA)) != 0) |
| 50 |
print_err("Set userdata mismatches"); |
| 51 |
|
| 52 |
data_a = nftnl_set_get_data(a, NFTNL_SET_DESC_CONCAT, &datalen_a); |
| 53 |
data_b = nftnl_set_get_data(b, NFTNL_SET_DESC_CONCAT, &datalen_b); |
| 54 |
if (datalen_a != datalen_b || |
| 55 |
memcmp(data_a, data_b, datalen_a)) |
| 56 |
print_err("Set desc concat mismatches"); |
| 57 |
} |
| 58 |
|
| 59 |
int main(int argc, char *argv[]) |
| 60 |
{ |
| 61 |
struct nftnl_set *a, *b = NULL; |
| 62 |
uint8_t field_lengths[16]; |
| 63 |
char buf[4096]; |
| 64 |
struct nlmsghdr *nlh; |
| 65 |
|
| 66 |
a = nftnl_set_alloc(); |
| 67 |
b = nftnl_set_alloc(); |
| 68 |
if (a == NULL || b == NULL) |
| 69 |
print_err("OOM"); |
| 70 |
|
| 71 |
nftnl_set_set_str(a, NFTNL_SET_TABLE, "test-table"); |
| 72 |
nftnl_set_set_str(a, NFTNL_SET_NAME, "test-name"); |
| 73 |
nftnl_set_set_u32(a, NFTNL_SET_FLAGS, 0x12345678); |
| 74 |
nftnl_set_set_u32(a, NFTNL_SET_KEY_TYPE, 0x12345678); |
| 75 |
nftnl_set_set_u32(a, NFTNL_SET_KEY_LEN, 0x12345678); |
| 76 |
nftnl_set_set_u32(a, NFTNL_SET_DATA_TYPE, 0x12345678); |
| 77 |
nftnl_set_set_u32(a, NFTNL_SET_DATA_LEN, 0x12345678); |
| 78 |
nftnl_set_set_u32(a, NFTNL_SET_FAMILY, 0x12345678); |
| 79 |
nftnl_set_set_str(a, NFTNL_SET_USERDATA, "testing user data"); |
| 80 |
|
| 81 |
memset(field_lengths, 0xff, sizeof(field_lengths)); |
| 82 |
if (!nftnl_set_set_data(a, NFTNL_SET_DESC_CONCAT, field_lengths, 17)) |
| 83 |
print_err("oversized NFTNL_SET_DESC_CONCAT data accepted"); |
| 84 |
if (nftnl_set_set_data(a, NFTNL_SET_DESC_CONCAT, field_lengths, 16)) |
| 85 |
print_err("setting NFTNL_SET_DESC_CONCAT failed"); |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
nlh = nftnl_nlmsg_build_hdr(buf, NFT_MSG_NEWSET, AF_INET, 0, 1234); |
| 90 |
nftnl_set_nlmsg_build_payload(nlh, a); |
| 91 |
|
| 92 |
if (nftnl_set_nlmsg_parse(nlh, b) < 0) |
| 93 |
print_err("parsing problems"); |
| 94 |
|
| 95 |
cmp_nftnl_set(a,b); |
| 96 |
|
| 97 |
nftnl_set_free(a); nftnl_set_free(b); |
| 98 |
|
| 99 |
if (!test_ok) |
| 100 |
exit(EXIT_FAILURE); |
| 101 |
|
| 102 |
printf("%s: \033[32mOK\e[0m\n", argv[0]); |
| 103 |
return EXIT_SUCCESS; |
| 104 |
} |