Guest User

Untitled

a guest
Jul 18th, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.81 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. #include <errno.h>
  4.  
  5. #include "libiptc/libiptc.h"
  6.  
  7. #include <netdb.h>
  8.  
  9. #include <sys/types.h>
  10.  
  11. #include <sys/socket.h>
  12.  
  13. #include <arpa/inet.h>
  14.  
  15. int main( int argc ,char* argv[] )
  16.  
  17. {
  18.  
  19. char*tables = "filter";
  20.  
  21.  
  22.  
  23. if(argc > 2 )
  24.  
  25. {
  26.  
  27. printf("toomany argument\n");
  28.  
  29. return-1;
  30.  
  31. }
  32.  
  33.  
  34.  
  35. if(argc == 2 )
  36.  
  37. {
  38.  
  39. tables= argv[1];
  40.  
  41. }
  42.  
  43.  
  44.  
  45. structiptc_handle *handle;
  46.  
  47. constchar *error = NULL;
  48.  
  49. constchar * chain = NULL;
  50.  
  51. structipt_counters counters;
  52.  
  53. constchar *pol = NULL;
  54.  
  55. conststruct ipt_entry* rule;
  56.  
  57.  
  58.  
  59. handle= iptc_init( tables );
  60.  
  61.  
  62.  
  63. intret = 0;
  64.  
  65.  
  66.  
  67. ret= xtables_init_all(&iptables_globals, NFPROTO_IPV4);
  68.  
  69. if(ret < 0 )
  70.  
  71. {
  72.  
  73. printf("initerror\n");
  74.  
  75. return-1;
  76.  
  77. }
  78.  
  79.  
  80.  
  81.  
  82.  
  83. if(handle == NULL )
  84.  
  85. {
  86.  
  87. error= iptc_strerror(errno);
  88.  
  89. printf("iptc_initerror:%s\n",error);
  90.  
  91. return-1;
  92.  
  93. }
  94.  
  95.  
  96.  
  97. for(chain = iptc_first_chain(handle); chain; chain = iptc_next_chain(handle) )
  98.  
  99. {
  100.  
  101. printf("%s\t",chain);
  102.  
  103. pol= iptc_get_policy(chain,&counters,handle);
  104.  
  105. printf("%s\t",pol);
  106.  
  107. printf("%llu\t",counters.pcnt);//经过该链的包的数量
  108.  
  109. printf("%llu\n",counters.bcnt);//经过该链的字节数
  110.  
  111.  
  112.  
  113. for(rule = iptc_first_rule(chain,handle); rule; rule = iptc_next_rule(rule,handle))
  114.  
  115. {
  116.  
  117. constchar *target = NULL;
  118.  
  119. target= iptc_get_target(rule,handle);
  120.  
  121. printf("%s\t",target);
  122.  
  123. printf("%llu\t",rule->counters.pcnt);//命中该规则的包数
  124.  
  125. printf("%llu\t",rule->counters.bcnt);//命中该规则的字节数
  126.  
  127. structprotoent *pro = NULL;
  128.  
  129. pro= getprotobynumber(rule->ip.proto);
  130.  
  131.  
  132.  
  133. if(pro != NULL )
  134.  
  135. {
  136.  
  137. printf("%s\t",pro->p_name);
  138.  
  139. }
  140.  
  141.  
  142.  
  143. if(rule->ip.iniface[0] == '\0' )//输入网络接口默认不指定可以通过-i指定如 –I ehh0
  144.  
  145. printf("any\t");
  146.  
  147. else
  148.  
  149. printf("%s\t",rule->ip.iniface);
  150.  
  151.  
  152.  
  153. if(rule->ip.outiface[0] == '\0' )//输出网络接口默认不指定可以通过-o 指定
  154.  
  155. printf("any\t");
  156.  
  157. else
  158.  
  159. printf("%s\t",rule->ip.outiface);
  160.  
  161.  
  162.  
  163. charaddr[32] = {0};
  164.  
  165. printf("%s\t",inet_ntop(AF_INET,&(rule->ip.src),addr,sizeof(addr)));
  166.  
  167. printf("%s\t",inet_ntop(AF_INET,&(rule->ip.dst),addr,sizeof(addr)));
  168.  
  169. }
  170.  
  171. }
  172.  
  173. }
Add Comment
Please, Sign In to add comment