| 1 |
#include <stdio.h> |
| 2 |
#include <stdlib.h> |
| 3 |
#include <string.h> |
| 4 |
#include <errno.h> |
| 5 |
#include <arpa/inet.h> |
| 6 |
|
| 7 |
#include <libnetfilter_conntrack/libnetfilter_conntrack.h> |
| 8 |
|
| 9 |
static int cb(const struct nlmsghdr *nlh, |
| 10 |
enum nf_conntrack_msg_type type, |
| 11 |
struct nf_conntrack *ct, |
| 12 |
void *data) |
| 13 |
{ |
| 14 |
char buf[1024]; |
| 15 |
|
| 16 |
if (!(nlh->nlmsg_flags & NLM_F_DUMP_FILTERED)) |
| 17 |
{ |
| 18 |
fprintf(stderr, "No filtering in kernel, do filtering in userspace\n"); |
| 19 |
return NFCT_CB_FAILURE; |
| 20 |
} |
| 21 |
|
| 22 |
nfct_snprintf(buf, sizeof(buf), ct, NFCT_T_UNKNOWN, NFCT_O_DEFAULT, NFCT_OF_SHOW_LAYER3 | NFCT_OF_TIMESTAMP); |
| 23 |
printf("%s\n", buf); |
| 24 |
|
| 25 |
return NFCT_CB_CONTINUE; |
| 26 |
} |
| 27 |
|
| 28 |
int main(void) |
| 29 |
{ |
| 30 |
int ret; |
| 31 |
struct nfct_handle *h; |
| 32 |
|
| 33 |
h = nfct_open(CONNTRACK, 0); |
| 34 |
if (!h) { |
| 35 |
perror("nfct_open"); |
| 36 |
return -1; |
| 37 |
} |
| 38 |
struct nfct_filter_dump *filter_dump = nfct_filter_dump_create(); |
| 39 |
if (filter_dump == NULL) { |
| 40 |
perror("nfct_filter_dump_alloc"); |
| 41 |
return -1; |
| 42 |
} |
| 43 |
|
| 44 |
struct nf_conntrack *ct; |
| 45 |
ct = nfct_new(); |
| 46 |
if (!ct) { |
| 47 |
perror("nfct_new"); |
| 48 |
return 0; |
| 49 |
} |
| 50 |
|
| 51 |
nfct_set_attr_u8(ct, ATTR_ORIG_L3PROTO, AF_INET); |
| 52 |
nfct_set_attr_u8(ct, ATTR_L4PROTO, IPPROTO_ICMP); |
| 53 |
nfct_set_attr_u32(ct, ATTR_ORIG_IPV4_DST, inet_addr("203.0.113.55")); |
| 54 |
nfct_filter_dump_set_attr(filter_dump, NFCT_FILTER_DUMP_TUPLE, ct); |
| 55 |
|
| 56 |
nfct_callback_register2(h, NFCT_T_ALL, cb, NULL); |
| 57 |
ret = nfct_query(h, NFCT_Q_DUMP_FILTER, filter_dump); |
| 58 |
|
| 59 |
nfct_filter_dump_destroy(filter_dump); |
| 60 |
|
| 61 |
printf("TEST: get conntrack "); |
| 62 |
if (ret == -1) |
| 63 |
printf("(%d)(%s)\n", ret, strerror(errno)); |
| 64 |
else |
| 65 |
printf("(OK)\n"); |
| 66 |
|
| 67 |
nfct_close(h); |
| 68 |
|
| 69 |
ret == -1 ? exit(EXIT_FAILURE) : exit(EXIT_SUCCESS); |
| 70 |
} |