7 #include <libmnl/libmnl.h>
8 #include <libnetfilter_conntrack/libnetfilter_conntrack.h>
10 static int data_cb(
const struct nlmsghdr *nlh,
void *data)
12 struct nf_expect *exp;
13 u_int32_t type = NFCT_T_UNKNOWN;
20 if (nfexp_nlmsg_parse(nlh, exp) < 0) {
21 perror(
"failed to parse message from kernel");
35 struct mnl_socket *nl;
36 char buf[MNL_SOCKET_BUFFER_SIZE];
39 u_int32_t seq, portid;
40 struct nf_conntrack *master;
41 struct nf_expect *exp;
44 nl = mnl_socket_open(NETLINK_NETFILTER);
46 perror(
"mnl_socket_open");
50 if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
51 perror(
"mnl_socket_bind");
55 portid = mnl_socket_get_portid(nl);
57 nlh = mnl_nlmsg_put_header(buf);
58 nlh->nlmsg_type = (NFNL_SUBSYS_CTNETLINK_EXP << 8) | IPCTNL_MSG_EXP_GET;
59 nlh->nlmsg_flags = NLM_F_REQUEST|NLM_F_DUMP|NLM_F_ACK;
60 nlh->nlmsg_seq = seq = time(NULL);
62 nfh = mnl_nlmsg_put_extra_header(nlh,
sizeof(
struct nfgenmsg));
63 nfh->nfgen_family = AF_INET;
64 nfh->version = NFNETLINK_V0;
89 nfexp_nlmsg_build(nlh, exp);
91 ret = mnl_socket_sendto(nl, nlh, nlh->nlmsg_len);
93 perror(
"mnl_socket_recvfrom");
97 ret = mnl_socket_recvfrom(nl, buf,
sizeof(buf));
99 ret = mnl_cb_run(buf, ret, seq, portid, data_cb, NULL);
100 if (ret <= MNL_CB_STOP)
103 ret = mnl_socket_recvfrom(nl, buf,
sizeof(buf));
106 perror(
"mnl_socket_recvfrom");
110 mnl_socket_close(nl);
void nfexp_set_attr(struct nf_expect *exp, const enum nf_expect_attr type, const void *value)
void nfct_set_attr_u32(struct nf_conntrack *ct, const enum nf_conntrack_attr type, uint32_t value)
struct nf_expect * nfexp_new(void)
int nfexp_snprintf(char *buf, unsigned int size, const struct nf_expect *exp, 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)
void nfexp_destroy(struct nf_expect *exp)