libnetfilter_cttimeout  1.0.0
nfct-timeout-add.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 <libmnl/libmnl.h>
16 #include <linux/netfilter/nfnetlink_cttimeout.h>
17 #include <libnetfilter_cttimeout/libnetfilter_cttimeout.h>
18 
19 int main(int argc, char *argv[])
20 {
21  struct mnl_socket *nl;
22  char buf[MNL_SOCKET_BUFFER_SIZE];
23  struct nlmsghdr *nlh;
24  uint32_t portid, seq;
25  struct nfct_timeout *t;
26  int ret;
27 
28  if (argc != 4) {
29  fprintf(stderr, "Usage: %s [name] [l3proto] [l4proto]\n",
30  argv[0]);
31  fprintf(stderr, "Example: %s test 2 255\n", argv[0]);
32  exit(EXIT_FAILURE);
33  }
34 
35  t = nfct_timeout_alloc();
36  if (t == NULL) {
37  perror("OOM");
38  exit(EXIT_FAILURE);
39  }
40 
41  nfct_timeout_attr_set(t, NFCT_TIMEOUT_ATTR_NAME, argv[1]);
42  nfct_timeout_attr_set_u16(t, NFCT_TIMEOUT_ATTR_L3PROTO, atoi(argv[2]));
43  nfct_timeout_attr_set_u8(t, NFCT_TIMEOUT_ATTR_L4PROTO, atoi(argv[3]));
44 
45  nfct_timeout_policy_attr_set_u32(t, NFCT_TIMEOUT_ATTR_GENERIC, 100);
46 
47  seq = time(NULL);
48  nlh = nfct_timeout_nlmsg_build_hdr(buf, IPCTNL_MSG_TIMEOUT_NEW,
49  NLM_F_CREATE | NLM_F_ACK, seq);
51 
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_timeout_free(struct nfct_timeout *)
int nfct_timeout_attr_set_u16(struct nfct_timeout *t, uint32_t type, uint16_t data)
int nfct_timeout_attr_set_u8(struct nfct_timeout *t, uint32_t type, uint8_t data)
int nfct_timeout_policy_attr_set_u32(struct nfct_timeout *, uint32_t type, uint32_t data)
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 *)
struct nlmsghdr * nfct_timeout_nlmsg_build_hdr(char *buf, uint8_t cmd, uint16_t flags, uint32_t seq)