libnetfilter_cthelper 1.0.1
nfct-helper-get.c
1/* This example is in the public domain. */
2#include <stdlib.h>
3#include <string.h>
4#include <time.h>
5#include <libmnl/libmnl.h>
6#include <libnetfilter_cthelper/libnetfilter_cthelper.h>
7
8static int nfct_helper_cb(const struct nlmsghdr *nlh, void *data)
9{
11 char buf[4096];
12
14 if (nfct_helper == NULL) {
15 perror("OOM");
16 goto err;
17 }
18
20 perror("nfct_helper_parse_nl_msg");
21 goto err_free;
22 }
23
24 nfct_helper_snprintf(buf, sizeof(buf), nfct_helper, 0, 0);
25 printf("%s\n", buf);
26
27err_free:
29err:
30 return MNL_CB_OK;
31}
32
33int main(int argc, char *argv[])
34{
35 struct mnl_socket *nl;
36 char buf[MNL_SOCKET_BUFFER_SIZE];
37 struct nlmsghdr *nlh;
38 uint32_t portid, seq;
39 int ret, full = 1;
40
41 if (argc != 1) {
42 fprintf(stderr, "Usage: %s\n", argv[0]);
43 exit(EXIT_FAILURE);
44 }
45
46 seq = time(NULL);
47 nlh = nfct_helper_nlmsg_build_hdr(buf, NFNL_MSG_CTHELPER_GET,
48 NLM_F_DUMP, seq);
49
50 nl = mnl_socket_open(NETLINK_NETFILTER);
51 if (nl == NULL) {
52 perror("mnl_socket_open");
53 exit(EXIT_FAILURE);
54 }
55
56 if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
57 perror("mnl_socket_bind");
58 exit(EXIT_FAILURE);
59 }
60 portid = mnl_socket_get_portid(nl);
61
62 if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
63 perror("mnl_socket_send");
64 exit(EXIT_FAILURE);
65 }
66
67 ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
68 while (ret > 0) {
69 ret = mnl_cb_run(buf, ret, seq, portid, nfct_helper_cb, &full);
70 if (ret <= 0)
71 break;
72 ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
73 }
74 if (ret == -1) {
75 perror("error");
76 exit(EXIT_FAILURE);
77 }
78 mnl_socket_close(nl);
79
80 return EXIT_SUCCESS;
81}
int nfct_helper_snprintf(char *buf, size_t size, struct nfct_helper *nfct_helper, unsigned int type, unsigned int flags)
struct nfct_helper * nfct_helper_alloc(void)
void nfct_helper_free(struct nfct_helper *h)
int nfct_helper_nlmsg_parse_payload(const struct nlmsghdr *nlh, struct nfct_helper *nfct_helper)
struct nlmsghdr * nfct_helper_nlmsg_build_hdr(char *buf, uint8_t cmd, uint16_t flags, uint32_t seq)