libnetfilter_cttimeout
1.0.0
|
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 <linux/netfilter/nfnetlink_cttimeout.h> 00016 #include <libmnl/libmnl.h> 00017 #include <libnetfilter_cttimeout/libnetfilter_cttimeout.h> 00018 00019 static int timeout_cb(const struct nlmsghdr *nlh, void *data) 00020 { 00021 struct nfct_timeout *t; 00022 char buf[4096]; 00023 00024 t = nfct_timeout_alloc(); 00025 if (t == NULL) { 00026 perror("OOM"); 00027 goto err; 00028 } 00029 00030 if (nfct_timeout_nlmsg_parse_payload(nlh, t) < 0) { 00031 perror("nfct_timeout_nlmsg_parse_payload"); 00032 goto err_free; 00033 } 00034 00035 nfct_timeout_snprintf(buf, sizeof(buf), t, NFCT_TIMEOUT_O_DEFAULT, 0); 00036 printf("%s\n", buf); 00037 00038 err_free: 00039 nfct_timeout_free(t); 00040 err: 00041 return MNL_CB_OK; 00042 } 00043 00044 int main(int argc, char *argv[]) 00045 { 00046 struct mnl_socket *nl; 00047 char buf[MNL_SOCKET_BUFFER_SIZE]; 00048 struct nlmsghdr *nlh; 00049 uint32_t portid, seq; 00050 struct nfct_timeout *t = NULL; 00051 int ret; 00052 00053 if (argc == 2) { 00054 t = nfct_timeout_alloc(); 00055 if (t == NULL) { 00056 perror("OOM"); 00057 exit(EXIT_FAILURE); 00058 } 00059 } 00060 00061 seq = time(NULL); 00062 if (t == NULL) { 00063 nlh = nfct_timeout_nlmsg_build_hdr(buf, IPCTNL_MSG_TIMEOUT_GET, 00064 NLM_F_DUMP, seq); 00065 } else { 00066 nlh = nfct_timeout_nlmsg_build_hdr(buf, IPCTNL_MSG_TIMEOUT_GET, 00067 NLM_F_ACK, seq); 00068 nfct_timeout_attr_set(t, NFCT_TIMEOUT_ATTR_NAME, argv[1]); 00069 nfct_timeout_nlmsg_build_payload(nlh, t); 00070 nfct_timeout_free(t); 00071 } 00072 00073 nl = mnl_socket_open(NETLINK_NETFILTER); 00074 if (nl == NULL) { 00075 perror("mnl_socket_open"); 00076 exit(EXIT_FAILURE); 00077 } 00078 00079 if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) { 00080 perror("mnl_socket_bind"); 00081 exit(EXIT_FAILURE); 00082 } 00083 portid = mnl_socket_get_portid(nl); 00084 00085 if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) { 00086 perror("mnl_socket_send"); 00087 exit(EXIT_FAILURE); 00088 } 00089 00090 ret = mnl_socket_recvfrom(nl, buf, sizeof(buf)); 00091 while (ret > 0) { 00092 ret = mnl_cb_run(buf, ret, seq, portid, timeout_cb, NULL); 00093 if (ret <= 0) 00094 break; 00095 ret = mnl_socket_recvfrom(nl, buf, sizeof(buf)); 00096 } 00097 if (ret == -1) { 00098 perror("error"); 00099 exit(EXIT_FAILURE); 00100 } 00101 mnl_socket_close(nl); 00102 00103 return EXIT_SUCCESS; 00104 }