nf-ct-list.c 4.3 KB

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