libnetfilter_conntrack  1.0.6
nfexp-mnl-dump-ct.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <time.h>
5 #include <arpa/inet.h>
6 
7 #include <libmnl/libmnl.h>
8 #include <libnetfilter_conntrack/libnetfilter_conntrack.h>
9 
10 static int data_cb(const struct nlmsghdr *nlh, void *data)
11 {
12  struct nf_expect *exp;
13  u_int32_t type = NFCT_T_UNKNOWN;
14  char buf[4096];
15 
16  exp = nfexp_new();
17  if (exp == NULL)
18  return MNL_CB_OK;
19 
20  if (nfexp_nlmsg_parse(nlh, exp) < 0) {
21  perror("failed to parse message from kernel");
22  return MNL_CB_ERROR;
23  }
24 
25  nfexp_snprintf(buf, sizeof(buf), exp, type, NFCT_O_DEFAULT, 0);
26  printf("%s\n", buf);
27 
28  nfexp_destroy(exp);
29 
30  return MNL_CB_OK;
31 }
32 
33 int main(void)
34 {
35  struct mnl_socket *nl;
36  char buf[MNL_SOCKET_BUFFER_SIZE];
37  struct nlmsghdr *nlh;
38  struct nfgenmsg *nfh;
39  u_int32_t seq, portid;
40  struct nf_conntrack *master;
41  struct nf_expect *exp;
42  int ret;
43 
44  nl = mnl_socket_open(NETLINK_NETFILTER);
45  if (nl == NULL) {
46  perror("mnl_socket_open");
47  exit(EXIT_FAILURE);
48  }
49 
50  if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
51  perror("mnl_socket_bind");
52  exit(EXIT_FAILURE);
53  }
54 
55  portid = mnl_socket_get_portid(nl);
56 
57  nlh = mnl_nlmsg_put_header(buf);
58  nlh->nlmsg_type = (NFNL_SUBSYS_CTNETLINK_EXP << 8) | IPCTNL_MSG_EXP_GET;
59  nlh->nlmsg_flags = NLM_F_REQUEST|NLM_F_DUMP|NLM_F_ACK;
60  nlh->nlmsg_seq = seq = time(NULL);
61 
62  nfh = mnl_nlmsg_put_extra_header(nlh, sizeof(struct nfgenmsg));
63  nfh->nfgen_family = AF_INET;
64  nfh->version = NFNETLINK_V0;
65  nfh->res_id = 0;
66 
67  master = nfct_new();
68  if (master == NULL) {
69  perror("nfct_new");
70  return 0;
71  }
72 
73  nfct_set_attr_u8(master, ATTR_L3PROTO, AF_INET);
74  nfct_set_attr_u32(master, ATTR_IPV4_SRC, inet_addr("1.1.1.1"));
75  nfct_set_attr_u32(master, ATTR_IPV4_DST, inet_addr("2.2.2.2"));
76 
77  nfct_set_attr_u8(master, ATTR_L4PROTO, IPPROTO_TCP);
78  nfct_set_attr_u16(master, ATTR_PORT_SRC, htons(1025));
79  nfct_set_attr_u16(master, ATTR_PORT_DST, htons(21));
80 
81  exp = nfexp_new();
82  if (exp == NULL) {
83  perror("nfexp_new");
84  return 0;
85  }
86 
87  nfexp_set_attr(exp, ATTR_EXP_MASTER, master);
88 
89  nfexp_nlmsg_build(nlh, exp);
90 
91  ret = mnl_socket_sendto(nl, nlh, nlh->nlmsg_len);
92  if (ret == -1) {
93  perror("mnl_socket_recvfrom");
94  exit(EXIT_FAILURE);
95  }
96 
97  ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
98  while (1) {
99  ret = mnl_cb_run(buf, ret, seq, portid, data_cb, NULL);
100  if (ret <= MNL_CB_STOP)
101  break;
102 
103  ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
104  }
105  if (ret == -1) {
106  perror("mnl_socket_recvfrom");
107  exit(EXIT_FAILURE);
108  }
109 
110  mnl_socket_close(nl);
111 
112  return 0;
113 }
void nfexp_set_attr(struct nf_expect *exp, const enum nf_expect_attr type, const void *value)
Definition: expect/api.c:308
void nfct_set_attr_u32(struct nf_conntrack *ct, const enum nf_conntrack_attr type, uint32_t value)
struct nf_expect * nfexp_new(void)
Definition: expect/api.c:28
int nfexp_snprintf(char *buf, unsigned int size, const struct nf_expect *exp, const unsigned int msg_type, const unsigned int out_type, const unsigned int out_flags)
Definition: expect/api.c:774
void nfct_set_attr_u16(struct nf_conntrack *ct, const enum nf_conntrack_attr type, uint16_t value)
struct nf_conntrack * nfct_new(void)
Definition: conntrack/api.c:75
void nfct_set_attr_u8(struct nf_conntrack *ct, const enum nf_conntrack_attr type, uint8_t value)
void nfexp_destroy(struct nf_expect *exp)
Definition: expect/api.c:45