libnetfilter_cttimeout  1.0.0
nfct-timeout-add.c
00001 /*
00002  * (C) 2005-2012 by Pablo Neira Ayuso <pablo@netfilter.org>
00003  * (C) 2012 by Vyatta Inc. <http://www.vyatta.com>
00004  *
00005  * This program is free software; you can redistribute it and/or modify it
00006  * under the terms of the GNU General Public License as published by
00007  * the Free Software Foundation; either version 2 of the License, or
00008  * (at your option) any later version.
00009  */
00010 #include <stdlib.h>
00011 #include <time.h>
00012 #include <string.h>
00013 #include <netinet/in.h>
00014 
00015 #include <libmnl/libmnl.h>
00016 #include <linux/netfilter/nfnetlink_cttimeout.h>
00017 #include <libnetfilter_cttimeout/libnetfilter_cttimeout.h>
00018 
00019 int main(int argc, char *argv[])
00020 {
00021         struct mnl_socket *nl;
00022         char buf[MNL_SOCKET_BUFFER_SIZE];
00023         struct nlmsghdr *nlh;
00024         uint32_t portid, seq;
00025         struct nfct_timeout *t;
00026         int ret;
00027 
00028         if (argc != 4) {
00029                 fprintf(stderr, "Usage: %s [name] [l3proto] [l4proto]\n",
00030                         argv[0]);
00031                 fprintf(stderr, "Example: %s test 2 255\n", argv[0]);
00032                 exit(EXIT_FAILURE);
00033         }
00034 
00035         t = nfct_timeout_alloc();
00036         if (t == NULL) {
00037                 perror("OOM");
00038                 exit(EXIT_FAILURE);
00039         }
00040 
00041         nfct_timeout_attr_set(t, NFCT_TIMEOUT_ATTR_NAME, argv[1]);
00042         nfct_timeout_attr_set_u16(t, NFCT_TIMEOUT_ATTR_L3PROTO, atoi(argv[2]));
00043         nfct_timeout_attr_set_u8(t, NFCT_TIMEOUT_ATTR_L4PROTO, atoi(argv[3]));
00044 
00045         nfct_timeout_policy_attr_set_u32(t, NFCT_TIMEOUT_ATTR_GENERIC, 100);
00046 
00047         seq = time(NULL);
00048         nlh = nfct_timeout_nlmsg_build_hdr(buf, IPCTNL_MSG_TIMEOUT_NEW,
00049                                         NLM_F_CREATE | NLM_F_ACK, seq);
00050         nfct_timeout_nlmsg_build_payload(nlh, t);
00051 
00052         nfct_timeout_free(t);
00053 
00054         nl = mnl_socket_open(NETLINK_NETFILTER);
00055         if (nl == NULL) {
00056                 perror("mnl_socket_open");
00057                 exit(EXIT_FAILURE);
00058         }
00059 
00060         if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
00061                 perror("mnl_socket_bind");
00062                 exit(EXIT_FAILURE);
00063         }
00064         portid = mnl_socket_get_portid(nl);
00065 
00066         if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
00067                 perror("mnl_socket_send");
00068                 exit(EXIT_FAILURE);
00069         }
00070 
00071         ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
00072         while (ret > 0) {
00073                 ret = mnl_cb_run(buf, ret, seq, portid, NULL, NULL);
00074                 if (ret <= 0)
00075                         break;
00076                 ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
00077         }
00078         if (ret == -1) {
00079                 perror("error");
00080                 exit(EXIT_FAILURE);
00081         }
00082         mnl_socket_close(nl);
00083 
00084         return EXIT_SUCCESS;
00085 }