libnetfilter_cttimeout  1.0.0
nfct-timeout-get.c
1 /*
2  * (C) 2005-2012 by Pablo Neira Ayuso <pablo@netfilter.org>
3  * (C) 2012 by Vyatta Inc. <http://www.vyatta.com>
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  */
10 #include <stdlib.h>
11 #include <time.h>
12 #include <string.h>
13 #include <netinet/in.h>
14 
15 #include <linux/netfilter/nfnetlink_cttimeout.h>
16 #include <libmnl/libmnl.h>
17 #include <libnetfilter_cttimeout/libnetfilter_cttimeout.h>
18 
19 static int timeout_cb(const struct nlmsghdr *nlh, void *data)
20 {
21  struct nfct_timeout *t;
22  char buf[4096];
23 
24  t = nfct_timeout_alloc();
25  if (t == NULL) {
26  perror("OOM");
27  goto err;
28  }
29 
30  if (nfct_timeout_nlmsg_parse_payload(nlh, t) < 0) {
31  perror("nfct_timeout_nlmsg_parse_payload");
32  goto err_free;
33  }
34 
35  nfct_timeout_snprintf(buf, sizeof(buf), t, NFCT_TIMEOUT_O_DEFAULT, 0);
36  printf("%s\n", buf);
37 
38 err_free:
40 err:
41  return MNL_CB_OK;
42 }
43 
44 int main(int argc, char *argv[])
45 {
46  struct mnl_socket *nl;
47  char buf[MNL_SOCKET_BUFFER_SIZE];
48  struct nlmsghdr *nlh;
49  uint32_t portid, seq;
50  struct nfct_timeout *t = NULL;
51  int ret;
52 
53  if (argc == 2) {
54  t = nfct_timeout_alloc();
55  if (t == NULL) {
56  perror("OOM");
57  exit(EXIT_FAILURE);
58  }
59  }
60 
61  seq = time(NULL);
62  if (t == NULL) {
63  nlh = nfct_timeout_nlmsg_build_hdr(buf, IPCTNL_MSG_TIMEOUT_GET,
64  NLM_F_DUMP, seq);
65  } else {
66  nlh = nfct_timeout_nlmsg_build_hdr(buf, IPCTNL_MSG_TIMEOUT_GET,
67  NLM_F_ACK, seq);
68  nfct_timeout_attr_set(t, NFCT_TIMEOUT_ATTR_NAME, argv[1]);
71  }
72 
73  nl = mnl_socket_open(NETLINK_NETFILTER);
74  if (nl == NULL) {
75  perror("mnl_socket_open");
76  exit(EXIT_FAILURE);
77  }
78 
79  if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
80  perror("mnl_socket_bind");
81  exit(EXIT_FAILURE);
82  }
83  portid = mnl_socket_get_portid(nl);
84 
85  if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
86  perror("mnl_socket_send");
87  exit(EXIT_FAILURE);
88  }
89 
90  ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
91  while (ret > 0) {
92  ret = mnl_cb_run(buf, ret, seq, portid, timeout_cb, NULL);
93  if (ret <= 0)
94  break;
95  ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
96  }
97  if (ret == -1) {
98  perror("error");
99  exit(EXIT_FAILURE);
100  }
101  mnl_socket_close(nl);
102 
103  return EXIT_SUCCESS;
104 }
int nfct_timeout_snprintf(char *buf, size_t size, const struct nfct_timeout *, unsigned int type, unsigned int flags)
void nfct_timeout_free(struct nfct_timeout *)
struct nfct_timeout * nfct_timeout_alloc(void)
int nfct_timeout_attr_set(struct nfct_timeout *t, uint32_t type, const void *data)
void nfct_timeout_nlmsg_build_payload(struct nlmsghdr *, const struct nfct_timeout *)
int nfct_timeout_nlmsg_parse_payload(const struct nlmsghdr *nlh, struct nfct_timeout *)
struct nlmsghdr * nfct_timeout_nlmsg_build_hdr(char *buf, uint8_t cmd, uint16_t flags, uint32_t seq)