對ip6tables rule操作

#include "libip6tc.h"


int getRulePackets()

{
    struct ip6tc_handle *handle;
    char *tables = "filter";
    const char *error = NULL;

       const char * chain = NULL;

       struct ip6t_counters counters;

       const char *pol = NULL;

       const struct ip6t_entry* rule;
       handle= ip6tc_init( tables );

/*
        int ret = 0;
       ret= xtables_init_all(&iptables_globals, NFPROTO_IPV6);

       if(ret < 0 )
       {
              printf("initerror\n");
              return-1;
       }
*/
       if(handle == NULL )
       {
              error= ip6tc_strerror(errno);
              printf("iptc_initerror:%s\n",error);
              return-1;
       }

 
    for(chain = ip6tc_first_chain(handle); chain; chain = ip6tc_next_chain(handle) )
    {
              printf("%s\t",chain);
              pol= ip6tc_get_policy(chain,&counters,handle);
              printf("%s\t",pol);
              printf("%llu\t",counters.pcnt);//\u7ecf\u8fc7\u8be5\u94fe\u7684\u5305\u7684\u6570\u91cf
              printf("%llu\n",counters.bcnt);//\u7ecf\u8fc7\u8be5\u94fe\u7684\u5b57\u8282\u6570

              for(rule = ip6tc_first_rule(chain,handle); rule; rule = ip6tc_next_rule(rule,handle))
              {
                     const char *target = NULL;
                     target= ip6tc_get_target(rule,handle);
                     printf("%s\t",target);
                     printf("%llu\t",rule->counters.pcnt);//\u547d\u4e2d\u8be5\u89c4\u5219\u7684\u5305\u6570
                     printf("%llu\t",rule->counters.bcnt);//\u547d\u4e2d\u8be5\u89c4\u5219\u7684\u5b57\u8282\u6570
                     struct protoent *pro = NULL;

                     pro= getprotobynumber(rule->ipv6.proto);

                     if(pro != NULL )
                     {
                            printf("%s\t",pro->p_name);
                     }
                     if(rule->ipv6.iniface[0] == '\0' )//\u8f93\u5165\u7f51\u7edc\u63a5\u53e3\u9ed8\u8ba4\u4e0d\u6307\u5b9a\u53ef\u4ee5\u901a\u8fc7-i\u6307\u5b9a\u5982 \u2013I ehh0
                            printf("any\t");
                     else
                            printf("%s\t",rule->ipv6.iniface);

 

                     if(rule->ipv6.outiface[0] == '\0' )//\u8f93\u51fa\u7f51\u7edc\u63a5\u53e3\u9ed8\u8ba4\u4e0d\u6307\u5b9a\u53ef\u4ee5\u901a\u8fc7-o \u6307\u5b9a
                            printf("any\t");
                     else
                            printf("%s\t",rule->ipv6.outiface);

                     char addr[128] = {0};

                     printf("%s\t",inet_ntop(AF_INET6,&(rule->ipv6.src),addr,sizeof(addr)));

                     printf("%s\t",inet_ntop(AF_INET6,&(rule->ipv6.dst),addr,sizeof(addr)));

        }
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章