libnetfilter_cthelper  1.0.0
nfct-helper-add.c
1 /* This example is in the public domain. */
2 #include <stdlib.h>
3 #include <time.h>
4 #include <string.h>
5 #include <netinet/in.h>
6 #include <libmnl/libmnl.h>
7 #include <libnetfilter_cthelper/libnetfilter_cthelper.h>
8 
9 int main(int argc, char *argv[])
10 {
11  struct mnl_socket *nl;
12  char buf[MNL_SOCKET_BUFFER_SIZE];
13  struct nlmsghdr *nlh;
14  uint32_t portid, seq;
15  struct nfct_helper *nfct_helper;
16  struct nfct_helper_policy *p;
17  int ret;
18 
19  if (argc != 3) {
20  printf("Usage: %s [helper-name] [queue-num]\n", argv[0]);
21  exit(EXIT_FAILURE);
22  }
23 
24  nfct_helper = nfct_helper_alloc();
25  if (nfct_helper == NULL) {
26  perror("OOM");
27  exit(EXIT_FAILURE);
28  }
29 
30  nfct_helper_attr_set(nfct_helper, NFCTH_ATTR_NAME, argv[1]);
31  nfct_helper_attr_set_u32(nfct_helper, NFCTH_ATTR_QUEUE_NUM, atoi(argv[2]));
32  nfct_helper_attr_set_u16(nfct_helper, NFCTH_ATTR_PROTO_L3NUM, AF_INET);
33  nfct_helper_attr_set_u8(nfct_helper, NFCTH_ATTR_PROTO_L4NUM, IPPROTO_TCP);
34 
36  if (p == NULL) {
37  perror("OOM");
38  exit(EXIT_FAILURE);
39  }
40  nfct_helper_policy_attr_set(p, NFCTH_ATTR_POLICY_NAME, "test");
41  nfct_helper_policy_attr_set_u32(p, NFCTH_ATTR_POLICY_TIMEOUT, 100);
42  nfct_helper_policy_attr_set_u32(p, NFCTH_ATTR_POLICY_MAX, 100);
43 
44  nfct_helper_attr_set(nfct_helper, NFCTH_ATTR_POLICY, p);
45 
46  seq = time(NULL);
47  nlh = nfct_helper_nlmsg_build_hdr(buf, NFNL_MSG_CTHELPER_NEW,
48  NLM_F_CREATE | NLM_F_ACK, seq);
49  nfct_helper_nlmsg_build_payload(nlh, nfct_helper);
50 
51  nfct_helper_free(nfct_helper);
53 
54  nl = mnl_socket_open(NETLINK_NETFILTER);
55  if (nl == NULL) {
56  perror("mnl_socket_open");
57  exit(EXIT_FAILURE);
58  }
59 
60  if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
61  perror("mnl_socket_bind");
62  exit(EXIT_FAILURE);
63  }
64  portid = mnl_socket_get_portid(nl);
65 
66  if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
67  perror("mnl_socket_send");
68  exit(EXIT_FAILURE);
69  }
70 
71  ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
72  while (ret > 0) {
73  ret = mnl_cb_run(buf, ret, seq, portid, NULL, NULL);
74  if (ret <= 0)
75  break;
76  ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
77  }
78  if (ret == -1) {
79  perror("error");
80  exit(EXIT_FAILURE);
81  }
82  mnl_socket_close(nl);
83 
84  return EXIT_SUCCESS;
85 }
void nfct_helper_policy_attr_set(struct nfct_helper_policy *p, enum nfct_helper_policy_attr_type type, const void *data)
struct nfct_helper * nfct_helper_alloc(void)
void nfct_helper_policy_free(struct nfct_helper_policy *p)
struct nfct_helper_policy * nfct_helper_policy_alloc(void)
void nfct_helper_nlmsg_build_payload(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)
void nfct_helper_attr_set(struct nfct_helper *nfct_helper, enum nfct_helper_attr_type type, const void *data)
void nfct_helper_free(struct nfct_helper *h)