libnetfilter_cthelper 1.0.1
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
9int 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;
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
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 nfct_helper_attr_set_u32(nfct_helper, NFCTH_ATTR_PRIV_DATA_LEN, 0);
35
36 /* Will be freed by nfct_helper_free. */
38 if (p == NULL) {
39 perror("OOM");
40 exit(EXIT_FAILURE);
41 }
42 nfct_helper_policy_attr_set(p, NFCTH_ATTR_POLICY_NAME, "test");
43 nfct_helper_policy_attr_set_u32(p, NFCTH_ATTR_POLICY_TIMEOUT, 100);
44 nfct_helper_policy_attr_set_u32(p, NFCTH_ATTR_POLICY_MAX, 100);
45
46 nfct_helper_attr_set(nfct_helper, NFCTH_ATTR_POLICY, p);
47
48 seq = time(NULL);
49 nlh = nfct_helper_nlmsg_build_hdr(buf, NFNL_MSG_CTHELPER_NEW,
50 NLM_F_CREATE | NLM_F_ACK, seq);
52
54
55 nl = mnl_socket_open(NETLINK_NETFILTER);
56 if (nl == NULL) {
57 perror("mnl_socket_open");
58 exit(EXIT_FAILURE);
59 }
60
61 if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
62 perror("mnl_socket_bind");
63 exit(EXIT_FAILURE);
64 }
65 portid = mnl_socket_get_portid(nl);
66
67 if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
68 perror("mnl_socket_send");
69 exit(EXIT_FAILURE);
70 }
71
72 ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
73 while (ret > 0) {
74 ret = mnl_cb_run(buf, ret, seq, portid, NULL, NULL);
75 if (ret <= 0)
76 break;
77 ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
78 }
79 if (ret == -1) {
80 perror("error");
81 exit(EXIT_FAILURE);
82 }
83 mnl_socket_close(nl);
84
85 return EXIT_SUCCESS;
86}
struct nfct_helper_policy * nfct_helper_policy_alloc(void)
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_free(struct nfct_helper *h)
void nfct_helper_attr_set(struct nfct_helper *nfct_helper, enum nfct_helper_attr_type type, const void *data)
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)