Initial import package libnl: Convenience library for kernel netlink sockets
[external/libnl.git] / src / nf-ct-dump.c
1 /*
2  * src/nf-ct-dump.c     Dump conntrack attributes
3  *
4  *      This library is free software; you can redistribute it and/or
5  *      modify it under the terms of the GNU Lesser General Public
6  *      License as published by the Free Software Foundation version 2.1
7  *      of the License.
8  *
9  * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
10  * Copyright (c) 2007 Philip Craig <philipc@snapgear.com>
11  * Copyright (c) 2007 Secure Computing Corporation
12  */
13
14 #include "utils.h"
15 #include <netlink/netfilter/ct.h>
16 #include <linux/netfilter/nf_conntrack_common.h>
17
18 #include "f_ct.c"
19
20 static void print_usage(void)
21 {
22         printf(
23         "Usage: nf-ct-dump <mode> [<filter>]\n"
24         "  mode := { brief | detailed | stats | xml }\n"
25         "  filter := [family FAMILY] [proto PROTO] [tcpstate TCPSTATE]\n"
26         "            [status STATUS] [timeout TIMEOUT] [mark MARK] [use USE] [id ID]\n"
27         "            [origsrc ADDR] [origdst ADDR] [origsrcport PORT] [origdstport PORT]\n"
28         "            [origicmpid ID] [origicmptype TYPE] [origicmpcode CODE]\n"
29         "            [origpackets PACKETS] [origbytes BYTES]\n"
30         "            [replysrc ADDR] [replydst ADDR] [replysrcport PORT] [replydstport PORT]\n"
31         "            [replyicmpid ID] [replyicmptype TYPE] [replyicmpcode CODE]\n"
32         "            [replypackets PACKETS] [replybytes BYTES]\n"
33         "            [{ replied | unreplied }] [{ assured | unassured }]\n"
34         );
35         exit(1);
36 }
37
38 int main(int argc, char *argv[])
39 {
40         struct nl_handle *nlh;
41         struct nl_cache *ct_cache;
42         struct nfnl_ct *ct;
43         struct nl_dump_params params = {
44                 .dp_fd = stdout,
45                 .dp_type = NL_DUMP_BRIEF
46         };
47         int err = 1;
48
49         if (nltool_init(argc, argv) < 0)
50                 return -1;
51
52         if (argc < 2 || !strcmp(argv[1], "-h"))
53                 print_usage();
54
55         nlh = nltool_alloc_handle();
56         if (!nlh)
57                 return -1;
58
59         ct = nfnl_ct_alloc();
60         if (!ct)
61                 goto errout;
62
63         if (nltool_connect(nlh, NETLINK_NETFILTER) < 0)
64                 goto errout_free;
65
66         ct_cache = nfnl_ct_alloc_cache(nlh);
67         if (!ct_cache) {
68                 fprintf(stderr, "Unable to retrieve ct cache: %s\n",
69                         nl_geterror());
70                 goto errout_close;
71         }
72         nl_cache_mngt_provide(ct_cache);
73
74         params.dp_type = nltool_parse_dumptype(argv[1]);
75         if (params.dp_type < 0)
76                 goto errout_ct_cache;
77
78         get_filter(ct, argc, argv, 2);
79         nl_cache_dump_filter(ct_cache, &params, (struct nl_object *) ct);
80
81         err = 0;
82
83 errout_ct_cache:
84         nl_cache_free(ct_cache);
85 errout_close:
86         nl_close(nlh);
87 errout_free:
88         nfnl_ct_put(ct);
89 errout:
90         return err;
91 }