5 #include <libmnl/libmnl.h>
6 #include <libnetfilter_conntrack/libnetfilter_conntrack.h>
8 static void print_label(
struct nf_conntrack *ct,
struct nfct_labelmap *map)
11 const struct nfct_bitmask *b =
nfct_get_attr(ct, ATTR_CONNLABELS);
16 max = nfct_bitmask_maxbit(b);
17 for (i = 0; i <= max; i++) {
18 if (nfct_bitmask_test_bit(b, i))
23 static int data_cb(
const struct nlmsghdr *nlh,
void *data)
25 struct nf_conntrack *ct;
32 nfct_nlmsg_parse(nlh, ct);
34 nfct_snprintf(buf,
sizeof(buf), ct, NFCT_T_UNKNOWN, NFCT_O_DEFAULT, 0);
37 print_label(ct, data);
46 struct mnl_socket *nl;
49 char buf[MNL_SOCKET_BUFFER_SIZE];
50 unsigned int seq, portid;
54 nl = mnl_socket_open(NETLINK_NETFILTER);
56 perror(
"mnl_socket_open");
60 if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
61 perror(
"mnl_socket_bind");
64 portid = mnl_socket_get_portid(nl);
66 nlh = mnl_nlmsg_put_header(buf);
67 nlh->nlmsg_type = (NFNL_SUBSYS_CTNETLINK << 8) | IPCTNL_MSG_CT_GET;
68 nlh->nlmsg_flags = NLM_F_REQUEST|NLM_F_DUMP;
69 nlh->nlmsg_seq = seq = time(NULL);
71 nfh = mnl_nlmsg_put_extra_header(nlh,
sizeof(
struct nfgenmsg));
72 nfh->nfgen_family = AF_UNSPEC;
73 nfh->version = NFNETLINK_V0;
77 ret = mnl_socket_sendto(nl, nlh, nlh->nlmsg_len);
79 perror(
"mnl_socket_sendto");
83 ret = mnl_socket_recvfrom(nl, buf,
sizeof(buf));
87 ret = mnl_cb_run(buf, ret, seq, portid, data_cb, l);
88 if (ret <= MNL_CB_STOP)
90 ret = mnl_socket_recvfrom(nl, buf,
sizeof(buf));
93 perror(
"mnl_socket_recvfrom");
100 mnl_socket_close(nl);
const char * nfct_labelmap_get_name(struct nfct_labelmap *m, unsigned int bit)
void nfct_destroy(struct nf_conntrack *ct)
int nfct_snprintf(char *buf, unsigned int size, const struct nf_conntrack *ct, const unsigned int msg_type, const unsigned int out_type, const unsigned int out_flags)
struct nfct_labelmap * nfct_labelmap_new(const char *mapfile)
struct nf_conntrack * nfct_new(void)
const void * nfct_get_attr(const struct nf_conntrack *ct, const enum nf_conntrack_attr type)
void nfct_labelmap_destroy(struct nfct_labelmap *map)