diff options
Diffstat (limited to 'examples/nfacct-add.c')
-rw-r--r-- | examples/nfacct-add.c | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/examples/nfacct-add.c b/examples/nfacct-add.c new file mode 100644 index 0000000..df3175e --- /dev/null +++ b/examples/nfacct-add.c @@ -0,0 +1,53 @@ +#include <stdlib.h> +#include <time.h> +#include <libmnl/libmnl.h> +#include <libnetfilter_acct/libnetfilter_acct.h> + +int main(void) +{ + struct mnl_socket *nl; + char buf[MNL_SOCKET_BUFFER_SIZE]; + struct nlmsghdr *nlh; + uint32_t portid, seq; + struct nfacct nfacct = { + .name = "example", + .pkts = 10, + .bytes = 10, + }; + int ret; + + nlh = nfacct_add(buf, &nfacct); + seq = nlh->nlmsg_seq = time(NULL); + + nl = mnl_socket_open(NETLINK_NETFILTER); + if (nl == NULL) { + perror("mnl_socket_open"); + exit(EXIT_FAILURE); + } + + if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) { + perror("mnl_socket_bind"); + exit(EXIT_FAILURE); + } + portid = mnl_socket_get_portid(nl); + + if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) { + perror("mnl_socket_send"); + exit(EXIT_FAILURE); + } + + ret = mnl_socket_recvfrom(nl, buf, sizeof(buf)); + while (ret > 0) { + ret = mnl_cb_run(buf, ret, seq, portid, nfacct_list_cb, NULL); + if (ret <= 0) + break; + ret = mnl_socket_recvfrom(nl, buf, sizeof(buf)); + } + if (ret == -1) { + perror("error"); + exit(EXIT_FAILURE); + } + mnl_socket_close(nl); + + return EXIT_SUCCESS; +} |