7 #include <libmnl/libmnl.h>
8 #include <libnetfilter_conntrack/libnetfilter_conntrack.h>
10 #include <linux/netfilter/nf_conntrack_tcp.h>
12 static int data_cb(
const struct nlmsghdr *nlh,
void *data)
14 struct nf_conntrack *ct;
21 nfct_nlmsg_parse(nlh, ct);
23 nfct_snprintf(buf,
sizeof(buf), ct, NFCT_T_UNKNOWN, NFCT_O_DEFAULT, 0);
33 struct mnl_socket *nl;
36 char buf[MNL_SOCKET_BUFFER_SIZE];
37 unsigned int seq, portid;
38 struct nf_conntrack *ct;
41 nl = mnl_socket_open(NETLINK_NETFILTER);
43 perror(
"mnl_socket_open");
47 if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
48 perror(
"mnl_socket_bind");
51 portid = mnl_socket_get_portid(nl);
53 nlh = mnl_nlmsg_put_header(buf);
54 nlh->nlmsg_type = (NFNL_SUBSYS_CTNETLINK << 8) | IPCTNL_MSG_CT_GET;
55 nlh->nlmsg_flags = NLM_F_REQUEST|NLM_F_ACK;
56 nlh->nlmsg_seq = seq = time(NULL);
58 nfh = mnl_nlmsg_put_extra_header(nlh,
sizeof(
struct nfgenmsg));
59 nfh->nfgen_family = AF_INET;
60 nfh->version = NFNETLINK_V0;
77 nfct_nlmsg_build(nlh, ct);
79 ret = mnl_socket_sendto(nl, nlh, nlh->nlmsg_len);
81 perror(
"mnl_socket_recvfrom");
85 ret = mnl_socket_recvfrom(nl, buf,
sizeof(buf));
87 ret = mnl_cb_run(buf, ret, seq, portid, data_cb, NULL);
88 if (ret <= MNL_CB_STOP)
90 ret = mnl_socket_recvfrom(nl, buf,
sizeof(buf));
93 perror(
"mnl_socket_recvfrom");
void nfct_set_attr_u32(struct nf_conntrack *ct, const enum nf_conntrack_attr type, uint32_t value)
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)
void nfct_set_attr_u16(struct nf_conntrack *ct, const enum nf_conntrack_attr type, uint16_t value)
struct nf_conntrack * nfct_new(void)
void nfct_set_attr_u8(struct nf_conntrack *ct, const enum nf_conntrack_attr type, uint8_t value)