7 #include <linux/netfilter/nfnetlink_conntrack.h>
8 #include <libmnl/libmnl.h>
10 static int data_attr_cb(
const struct nlattr *attr,
void *data)
12 const struct nlattr **tb = data;
13 int type = mnl_attr_get_type(attr);
15 if (mnl_attr_type_valid(attr, CTA_STATS_MAX) < 0)
18 if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0) {
19 perror(
"mnl_attr_validate");
27 static int data_cb(
const struct nlmsghdr *nlh,
void *data)
29 struct nlattr *tb[CTA_STATS_MAX+1] = {};
30 struct nfgenmsg *nfg = mnl_nlmsg_get_payload(nlh);
33 mnl_attr_parse(nlh,
sizeof(*nfg), data_attr_cb, tb);
35 for (i=0; i<CTA_STATS_MAX; i++) {
37 printf(
"%08x ", ntohl(mnl_attr_get_u32(tb[i])));
46 struct mnl_socket *nl;
49 char buf[MNL_SOCKET_BUFFER_SIZE];
50 unsigned int seq, portid;
53 nl = mnl_socket_open(NETLINK_NETFILTER);
55 perror(
"mnl_socket_open");
59 if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
60 perror(
"mnl_socket_bind");
63 portid = mnl_socket_get_portid(nl);
65 nlh = mnl_nlmsg_put_header(buf);
66 nlh->nlmsg_type = (NFNL_SUBSYS_CTNETLINK << 8) |
67 IPCTNL_MSG_CT_GET_STATS_CPU;
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_INET;
73 nfh->version = NFNETLINK_V0;
76 ret = mnl_socket_sendto(nl, nlh, nlh->nlmsg_len);
78 perror(
"mnl_socket_recvfrom");
82 ret = mnl_socket_recvfrom(nl, buf,
sizeof(buf));
84 ret = mnl_cb_run(buf, ret, seq, portid, data_cb, NULL);
85 if (ret <= MNL_CB_STOP)
87 ret = mnl_socket_recvfrom(nl, buf,
sizeof(buf));
90 perror(
"mnl_socket_recvfrom");