libnetfilter_conntrack  1.0.6
nfct-mnl-flush.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 #include <linux/netfilter/nf_conntrack_tcp.h>
11 
12 int main(void)
13 {
14  struct mnl_socket *nl;
15  struct nlmsghdr *nlh;
16  struct nfgenmsg *nfh;
17  char buf[MNL_SOCKET_BUFFER_SIZE];
18  unsigned int seq, portid;
19  int ret;
20 
21  nl = mnl_socket_open(NETLINK_NETFILTER);
22  if (nl == NULL) {
23  perror("mnl_socket_open");
24  exit(EXIT_FAILURE);
25  }
26 
27  if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
28  perror("mnl_socket_bind");
29  exit(EXIT_FAILURE);
30  }
31  portid = mnl_socket_get_portid(nl);
32 
33  nlh = mnl_nlmsg_put_header(buf);
34  nlh->nlmsg_type = (NFNL_SUBSYS_CTNETLINK << 8) | IPCTNL_MSG_CT_DELETE;
35  nlh->nlmsg_flags = NLM_F_REQUEST|NLM_F_ACK;
36  nlh->nlmsg_seq = seq = time(NULL);
37 
38  nfh = mnl_nlmsg_put_extra_header(nlh, sizeof(struct nfgenmsg));
39  nfh->nfgen_family = AF_INET;
40  nfh->version = NFNETLINK_V0;
41  nfh->res_id = 0;
42 
43  ret = mnl_socket_sendto(nl, nlh, nlh->nlmsg_len);
44  if (ret == -1) {
45  perror("mnl_socket_recvfrom");
46  exit(EXIT_FAILURE);
47  }
48 
49  ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
50  while (ret > 0) {
51  ret = mnl_cb_run(buf, ret, seq, portid, NULL, NULL);
52  if (ret <= MNL_CB_STOP)
53  break;
54  ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
55  }
56  if (ret == -1) {
57  perror("mnl_socket_recvfrom");
58  exit(EXIT_FAILURE);
59  }
60 
61  mnl_socket_close(nl);
62 
63  return 0;
64 }