libnl  3.4.0
nf-ct-list.c
1 /*
2  * src/nf-ct-list.c List Conntrack Entries
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-2009 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 <netlink/cli/utils.h>
15 #include <netlink/cli/ct.h>
16 
17 #include <linux/netlink.h>
18 
19 static void print_usage(void)
20 {
21  printf(
22  "Usage: nf-ct-list [OPTION]... [CONNTRACK ENTRY]\n"
23  "\n"
24  "Options\n"
25  " -f, --format=TYPE Output format { brief | details | stats }\n"
26  " -h, --help Show this help\n"
27  " -v, --version Show versioning information\n"
28  "\n"
29  "Conntrack Selection\n"
30  " -i, --id=NUM Identifier\n"
31  " -p, --proto=PROTOCOL Protocol\n"
32  " --tcp-state=STATE TCP connection state\n"
33  " --orig-src=ADDR Original source address\n"
34  " --orig-sport=PORT Original source port\n"
35  " --orig-dst=ADDR Original destination address\n"
36  " --orig-dport=PORT Original destination port\n"
37  " --reply-src=ADDR Reply source address\n"
38  " --reply-sport=PORT Reply source port\n"
39  " --reply-dst=ADDR Reply destination address\n"
40  " --reply-dport=PORT Reply destination port\n"
41  " -F, --family=FAMILY Address family\n"
42  " --mark=NUM Mark value\n"
43  " --timeout=NUM Timeout value\n"
44  " --refcnt=NUM Use counter value\n"
45  " --flags Flags\n"
46  );
47  exit(0);
48 }
49 
50 int main(int argc, char *argv[])
51 {
52  struct nl_sock *sock;
53  struct nl_cache *ct_cache;
54  struct nfnl_ct *ct;
55  struct nl_dump_params params = {
57  .dp_fd = stdout,
58  };
59 
60  ct = nl_cli_ct_alloc();
61 
62  for (;;) {
63  int c, optidx = 0;
64  enum {
65  ARG_MARK = 257,
66  ARG_TCP_STATE = 258,
67  ARG_ORIG_SRC,
68  ARG_ORIG_SPORT,
69  ARG_ORIG_DST,
70  ARG_ORIG_DPORT,
71  ARG_REPLY_SRC,
72  ARG_REPLY_SPORT,
73  ARG_REPLY_DST,
74  ARG_REPLY_DPORT,
75  ARG_TIMEOUT,
76  ARG_REFCNT,
77  ARG_FLAGS,
78  };
79  static struct option long_opts[] = {
80  { "format", 1, 0, 'f' },
81  { "help", 0, 0, 'h' },
82  { "version", 0, 0, 'v' },
83  { "id", 1, 0, 'i' },
84  { "proto", 1, 0, 'p' },
85  { "tcp-state", 1, 0, ARG_TCP_STATE },
86  { "orig-src", 1, 0, ARG_ORIG_SRC },
87  { "orig-sport", 1, 0, ARG_ORIG_SPORT },
88  { "orig-dst", 1, 0, ARG_ORIG_DST },
89  { "orig-dport", 1, 0, ARG_ORIG_DPORT },
90  { "reply-src", 1, 0, ARG_REPLY_SRC },
91  { "reply-sport", 1, 0, ARG_REPLY_SPORT },
92  { "reply-dst", 1, 0, ARG_REPLY_DST },
93  { "reply-dport", 1, 0, ARG_REPLY_DPORT },
94  { "family", 1, 0, 'F' },
95  { "mark", 1, 0, ARG_MARK },
96  { "timeout", 1, 0, ARG_TIMEOUT },
97  { "refcnt", 1, 0, ARG_REFCNT },
98  { 0, 0, 0, 0 }
99  };
100 
101  c = getopt_long(argc, argv, "46f:hvi:p:F:", long_opts, &optidx);
102  if (c == -1)
103  break;
104 
105  switch (c) {
106  case '?': exit(NLE_INVAL);
107  case '4': nfnl_ct_set_family(ct, AF_INET); break;
108  case '6': nfnl_ct_set_family(ct, AF_INET6); break;
109  case 'f': params.dp_type = nl_cli_parse_dumptype(optarg); break;
110  case 'h': print_usage(); break;
111  case 'v': nl_cli_print_version(); break;
112  case 'i': nl_cli_ct_parse_id(ct, optarg); break;
113  case 'p': nl_cli_ct_parse_protocol(ct, optarg); break;
114  case ARG_TCP_STATE: nl_cli_ct_parse_tcp_state(ct, optarg); break;
115  case ARG_ORIG_SRC: nl_cli_ct_parse_src(ct, 0, optarg); break;
116  case ARG_ORIG_SPORT: nl_cli_ct_parse_src_port(ct, 0, optarg); break;
117  case ARG_ORIG_DST: nl_cli_ct_parse_dst(ct, 0, optarg); break;
118  case ARG_ORIG_DPORT: nl_cli_ct_parse_dst_port(ct, 0, optarg); break;
119  case ARG_REPLY_SRC: nl_cli_ct_parse_src(ct, 1, optarg); break;
120  case ARG_REPLY_SPORT: nl_cli_ct_parse_src_port(ct, 1, optarg); break;
121  case ARG_REPLY_DST: nl_cli_ct_parse_dst(ct, 1, optarg); break;
122  case ARG_REPLY_DPORT: nl_cli_ct_parse_dst_port(ct, 1, optarg); break;
123  case 'F': nl_cli_ct_parse_family(ct, optarg); break;
124  case ARG_MARK: nl_cli_ct_parse_mark(ct, optarg); break;
125  case ARG_TIMEOUT: nl_cli_ct_parse_timeout(ct, optarg); break;
126  case ARG_REFCNT: nl_cli_ct_parse_use(ct, optarg); break;
127  case ARG_FLAGS: nl_cli_ct_parse_status(ct, optarg); break;
128  }
129  }
130 
131  sock = nl_cli_alloc_socket();
132  nl_cli_connect(sock, NETLINK_NETFILTER);
133  ct_cache = nl_cli_ct_alloc_cache(sock);
134 
135  nl_cache_dump_filter(ct_cache, &params, OBJ_CAST(ct));
136 
137  return 0;
138 }
Dump object briefly on one line.
Definition: types.h:22
enum nl_dump_type dp_type
Specifies the type of dump that is requested.
Definition: types.h:38
void nl_cache_dump_filter(struct nl_cache *cache, struct nl_dump_params *params, struct nl_object *filter)
Dump all elements of a cache (filtered).
Definition: cache.c:1216
Dumping parameters.
Definition: types.h:33