libnftnl 1.2.9
nft-ct-timeout-get.c
1/* SPDX-License-Identifier: GPL-2.0-or-later */
2/*
3 * (C) 2012 by Pablo Neira Ayuso <pablo@netfilter.org>
4 *
5 * This software has been sponsored by Sophos Astaro <http://www.sophos.com>
6 */
7
8
9#include <stdlib.h>
10#include <time.h>
11#include <string.h>
12#include <netinet/in.h>
13
14#include <linux/netfilter.h>
15#include <linux/netfilter/nf_tables.h>
16
17#include <libmnl/libmnl.h>
18#include <libnftnl/object.h>
19
20static int obj_cb(const struct nlmsghdr *nlh, void *data)
21{
22 struct nftnl_obj *t;
23 char buf[4096];
24 uint32_t *type = data;
25
26 t = nftnl_obj_alloc();
27 if (t == NULL) {
28 perror("OOM");
29 goto err;
30 }
31
32 if (nftnl_obj_nlmsg_parse(nlh, t) < 0) {
33 perror("nftnl_obj_nlmsg_parse");
34 goto err_free;
35 }
36
37 nftnl_obj_snprintf(buf, sizeof(buf), t, *type, 0);
38 printf("%s\n", buf);
39
40err_free:
41 nftnl_obj_free(t);
42err:
43 return MNL_CB_OK;
44}
45
46int main(int argc, char *argv[])
47{
48 struct mnl_socket *nl;
49 char buf[MNL_SOCKET_BUFFER_SIZE];
50 struct nlmsghdr *nlh;
51 uint32_t portid, seq, family;
52 struct nftnl_obj *t = NULL;
53 int ret;
54 uint32_t type = NFTNL_OUTPUT_DEFAULT;
55
56 if (argc < 3 || argc > 5) {
57 fprintf(stderr, "%s <family> <table> [<obj>]\n", argv[0]);
58 return EXIT_FAILURE;
59 }
60
61 if (strcmp(argv[1], "ip") == 0)
62 family = NFPROTO_IPV4;
63 else if (strcmp(argv[1], "ip6") == 0)
64 family = NFPROTO_IPV6;
65 else if (strcmp(argv[1], "inet") == 0)
66 family = NFPROTO_INET;
67 else if (strcmp(argv[1], "unspec") == 0)
68 family = NFPROTO_UNSPEC;
69 else {
70 fprintf(stderr, "Unknown family: ip, ip6, inet, unspec");
71 exit(EXIT_FAILURE);
72 }
73
74 if (argc == 3 || argc == 4) {
75 t = nftnl_obj_alloc();
76 if (t == NULL) {
77 perror("OOM");
78 exit(EXIT_FAILURE);
79 }
80 }
81
82 seq = time(NULL);
83 nftnl_obj_set_u32(t, NFTNL_OBJ_TYPE, NFT_OBJECT_CT_TIMEOUT);
84 if (argc < 4) {
85 nlh = nftnl_nlmsg_build_hdr(buf, NFT_MSG_GETOBJ, family,
86 NLM_F_DUMP, seq);
87 if (argc == 3) {
88 nftnl_obj_set_str(t, NFTNL_OBJ_TABLE, argv[2]);
89 nftnl_obj_nlmsg_build_payload(nlh, t);
90 nftnl_obj_free(t);
91 }
92 } else {
93 nftnl_obj_set_str(t, NFTNL_OBJ_TABLE, argv[2]);
94 nftnl_obj_set_str(t, NFTNL_OBJ_NAME, argv[3]);
95
96 nlh = nftnl_nlmsg_build_hdr(buf, NFT_MSG_GETOBJ, family,
97 NLM_F_ACK, seq);
98 nftnl_obj_nlmsg_build_payload(nlh, t);
99 nftnl_obj_free(t);
100 }
101
102 nl = mnl_socket_open(NETLINK_NETFILTER);
103 if (nl == NULL) {
104 perror("mnl_socket_open");
105 exit(EXIT_FAILURE);
106 }
107
108 if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
109 perror("mnl_socket_bind");
110 exit(EXIT_FAILURE);
111 }
112 portid = mnl_socket_get_portid(nl);
113
114 if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
115 perror("mnl_socket_send");
116 exit(EXIT_FAILURE);
117 }
118
119 ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
120 while (ret > 0) {
121 ret = mnl_cb_run(buf, ret, seq, portid, obj_cb, &type);
122 if (ret <= 0)
123 break;
124 ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
125 }
126 if (ret == -1) {
127 perror("error");
128 exit(EXIT_FAILURE);
129 }
130 mnl_socket_close(nl);
131
132 return EXIT_SUCCESS;
133}