
Zc           @   sE  d  d l  Z d  d l Z d d l m Z m Z m Z d d l m Z m	 Z	 d d l
 m Z m Z m Z m Z d d g Z y e	 d  Wn n Xd	 Z d
   Z d e j f d     YZ d e j f d     YZ d e j f d     YZ e d d  \ Z Z d e f d     YZ d e f d     YZ d e f d     YZ d S(   iNi   (   t   Rulet   Tablet	   IPTCError(   t   find_libraryt   load_kernel(   t   XT_INV_PROTOt   NFPROTO_IPV6t   xt_alignt   xt_counterst   Table6t   Rule6t
   ip6_tablesi   c         C   s*   y t  |   t SWn t k
 r% n Xt S(   N(   R	   t   TrueR   t   False(   t   name(    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip6tc.pyt   is_table6_available   s    
t   in6_addrc           B   s$   e  Z d  Z d e j d f g Z RS(   s8   This class is a representation of the C struct in6_addr.t   s6_addri   (   t   __name__t
   __module__t   __doc__t   ctt   c_uint8t   _fields_(    (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip6tc.pyR      s   t   ip6t_ip6c           B   s   e  Z d  Z d e f d e f d e f d e f d e j e f d e j e f d e j e f d e j e f d	 e j f d
 e j f d e j f d e j f g Z	 d Z
 d Z d Z d Z d Z d Z d Z d Z d Z d Z e Z d Z d   Z RS(   s8   This class is a representation of the C struct ip6t_ip6.t   srct   dstt   smskt   dmskt   inifacet   outifacet   iniface_maskt   outiface_maskt   protot   tost   flagst   invflagsi   i   i   i   i   i   i    i   c         C   s   d |  j  _ |  j _ d  S(   Ni   i   i  (   R   R   R   (   t   self(    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip6tc.pyt   __init__A   s    (   R   R   R   R   R   t   c_chart	   _IFNAMSIZt   c_uint16R   R   t   IP6T_F_PROTOt
   IP6T_F_TOSt   IP6T_F_GOTOt   IP6T_F_MASKt   IP6T_INV_VIA_INt   IP6T_INV_VIA_OUTt   IP6T_INV_TOSt   IP6T_INV_SRCIPt   IP6T_INV_DSTIPt   IP6T_INV_FRAGR   t   IP6T_INV_PROTOt   IP6T_INV_MASKR&   (    (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip6tc.pyR   "   s4   				t
   ip6t_entryc        	   B   sf   e  Z d  Z d e f d e j f d e j f d e j f d e j f d e f d e j d f g Z	 RS(	   s:   This class is a representation of the C struct ip6t_entry.t   ipv6t   nfcachet   target_offsett   next_offsett   comefromt   counterst   elemsi    (
   R   R   R   R   R   t   c_uintR)   R   t   c_ubyteR   (    (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip6tc.pyR6   F   s   		t   ip6tct   iptcc           B   s  e  Z d  Z e j Z e j e j  e _	 e j
 g e _ e j Z d e _	 e j g e _ e j Z e j e _	 e j g e _ e j Z e j e _	 e j
 e j g e _ e j Z e j
 e _	 e j g e _ e j Z e j
 e _	 e j g e _ e j Z e j e _	 e j
 e j g e _ e j Z e j e _	 e j
 e j g e _ e j Z e j e _	 e j
 e j g e _ e j Z e j e _	 e j
 e j
 e j g e _ e j  Z! e j e! _	 e j
 e j g e! _ e j" Z# e j e# _	 e j
 e j g e# _ e j$ Z% e j
 e% _	 e j
 e j e&  e j g e% _ e j' Z( e j e( _	 e j
 e j
 e j e&  e j g e( _ e j) Z* e j e+  e* _	 e j
 e j g e* _ e j, Z- e j e+  e- _	 e j e+  e j g e- _ e j. Z/ e j
 e/ _	 e j e+  e j g e/ _ e j0 Z1 e j e1 _	 e j
 e j e+  e j e j g e1 _ e j2 Z3 e j e3 _	 e j
 e j e+  e j e j g e3 _ e j4 Z5 e j e5 _	 e j
 e j e+  e j g e5 _ e j6 Z7 e j e7 _	 e j
 e j e+  e j e j8  e j g e7 _ e j9 Z: e j e: _	 e j
 e j; e j g e: _ e j< Z= e j e= _	 e j; e j
 e j g e= _ e j> Z? e j e&  e? _	 e j
 e j; e j g e? _ e j@ ZA e j eA _	 e j
 e j; e j g eA _ e jB ZC e j eC _	 e j
 e j; e j e&  e j g eC _ e jD ZE e j
 eE _	 e j g eE _ RS(   s+   This class contains all libip6tc API calls.N(F   R   R   R   t   _libiptct
   ip6tc_initt	   iptc_initR   t   POINTERt   c_intt   restypet   c_char_pt   argstypet
   ip6tc_freet	   iptc_freet   Nonet   c_void_pt   ip6tc_committ   iptc_committ   ip6tc_builtint   iptc_builtint   ip6tc_first_chaint   iptc_first_chaint   ip6tc_next_chaint   iptc_next_chaint   ip6tc_is_chaint   iptc_is_chaint   ip6tc_create_chaint   iptc_create_chaint   ip6tc_delete_chaint   iptc_delete_chaint   ip6tc_rename_chaint   iptc_rename_chaint   ip6tc_flush_entriest   iptc_flush_entriest   ip6tc_zero_entriest   iptc_zero_entriest   ip6tc_get_policyt   iptc_get_policyR   t   ip6tc_set_policyt   iptc_set_policyt   ip6tc_first_rulet   iptc_first_ruleR6   t   ip6tc_next_rulet   iptc_next_rulet   ip6tc_get_targett   iptc_get_targett   ip6tc_insert_entryt   iptc_insert_entryt   ip6tc_replace_entryt   iptc_replace_entryt   ip6tc_append_entryt   iptc_append_entryt   ip6tc_delete_entryt   iptc_delete_entryR?   t   ip6tc_delete_num_entryt   iptc_delete_num_entryR>   t   ip6tc_get_referencest   iptc_get_referencest   ip6tc_read_countert   iptc_read_countert   ip6tc_zero_countert   iptc_zero_countert   ip6tc_set_countert   iptc_set_countert   ip6tc_strerrort   iptc_strerror(    (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip6tc.pyR@   T   s   																													c           B   s1  e  Z d  Z d d d  Z d   Z d   Z d   Z e e  Z	 d   Z
 d   Z d   Z d   Z d	   Z d
   Z e e e  Z d   Z d   Z e e e  Z d   Z d   Z e e e  Z d   Z d   Z e e e  Z d   Z d   Z e e e  Z d   Z d   Z d   Z d   Z  RS(   s   This is an IPv6 rule.c         C   s1   t  |  _ g  |  _ d  |  _ | |  _ | |  _ d  S(   N(   R   t   nfprotot   _matchesRL   t   _targett   chaint   rule(   R%   t   entryR   (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip6tc.pyR&      s
    				c         C   s   |  j  | j  k r t St |  j  t | j  k r8 t St | j  t g  | j D] } | |  j k rQ | ^ qQ  k r| t S|  j | j k r |  j | j k r |  j | j k r |  j | j k r |  j	 | j	 k r t
 St S(   N(   R   R   t   lenR   t   setR   R   t   protocolt   in_interfacet   out_interfaceR   (   R%   R   t   x(    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip6tc.pyt   __eq__   s    !$c         C   s   |  j  | |  j j  S(   N(   t   _saveR   R7   (   R%   R   (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip6tc.pyt   save   s    c         C   s,   g  t  j D] } t |  r
 t  |  ^ q
 S(   N(   R	   t   ALLR   (   R%   t   t(    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip6tc.pyt   _get_tables  s    c         C   s>   d } x1 | d k r9 | d @r, | d 7} n  | d ?} q	 W| S(   Ni    i   (    (   R%   t   nt   bits(    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip6tc.pyt   _count_bits  s    
c         C   s   g  } xs t  d  D]e } | d k r5 | j d  n9 | d k ra | j d d | ?d | > n | j d  | d 8} q W| S(   Ni   i   i   i    (   t   ranget   append(   R%   t   plent   maskt   i(    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip6tc.pyt   _create_mask  s     c         C   s  d } |  j  j j t j @r4 d j | d g  } n  y% t j t j |  j  j j	 j
  } Wn  t j k
 r{ t d   n Xd j | | d g  } d } xG |  j  j j j
 D]3 } | d k r | d 7} q | |  j |  7} Pq Wd j | t |  g  } | S(   Nt    t   !s(   error in internal state: invalid addresst   /i    i   i   (   R   R7   R$   R   R1   t   joint   sockett	   inet_ntopt   AF_INET6R   R   t   errorR   R   R   t   str(   R%   R   t   addrR   R   (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip6tc.pyt   get_src  s"    c         C   sL   | j  d  } | d k r* | } d } n | |  } | | d } | | f S(   NR   is'   ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffffi   (   t   find(   R%   t   at   slashR   t   netm(    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip6tc.pyt   _get_address_netmask0  s    	
c         C   sl   t  j d } t   } y% | j t j t j |   | _ Wn$ t j k
 ra t	 d |   n X| | f S(   Ni   s   invalid address %s(
   R   R   R   t   from_buffer_copyR   t	   inet_ptonR   R   R   t
   ValueError(   R%   R   t   arrt   ina(    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip6tc.pyt   _addr2in6addr:  s    	c         C   sX  | d d k r5 |  j  j j t j O_ | d } n  |  j  j j t j t j @M_ |  j |  \ } } |  j |  \ } |  j  j _ | j	   r t
 |  } | d k  s | d k r t d |   n  | |  j |    |  j  j j _ d  St   } y% | j t j t j |   | _ Wn$ t j k
 rDt d |   n X| |  j  j _ d  S(   Ni    R   i   i   s   invalid prefix length %ds   invalid netmask %s(   R   R7   R$   R   R1   R5   R   R   R   t   isdigitt   intR   R   R   R   R   R   R   R   R   R   (   R%   R   R   R   R   R   t   neta(    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip6tc.pyt   set_srcD  s(    !	c         C   s  d } |  j  j j t j @r4 d j | d g  } n  y% t j t j |  j  j j	 j
  } Wn  t j k
 r{ t d   n Xd j | | d g  } d } xK |  j  j j j
 D]7 } | d @d k r | d 7} q | |  j |  7} Pq Wd j | t |  g  } | S(   NR   R   s(   error in internal state: invalid addressR   i    i   i   (   R   R7   R$   R   R2   R   R   R   R   R   R   R   R   R   R   R   (   R%   R   R   R   R   (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip6tc.pyt   get_dste  s"    c         C   sX  | d d k r5 |  j  j j t j O_ | d } n  |  j  j j t j t j @M_ |  j |  \ } } |  j |  \ } |  j  j _ | j	   r t
 |  } | d k  s | d k r t d |   n  | |  j |    |  j  j j _ d  St   } y% | j t j t j |   | _ Wn$ t j k
 rDt d |   n X| |  j  j _ d  S(   Ni    R   i   i   s   invalid prefix length %ds   invalid netmask %s(   R   R7   R$   R   R2   R5   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   (   R%   R   R   R   R   R   R   (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip6tc.pyt   set_dst{  s(    !	c         C   s(  d } |  j  j j t j @r4 d j d | g  } n  t t  } |  j  j j | t	 |  j  j j  *t t  } |  j  j j
 | t	 |  j  j j
  *| d d k r d  Sx} t t  D]o } | | d k r d j | t | |  g  } q | | d d k rd j | d g  } n
 | d  } Pq W| S(   NR   R   i    i   t   +i(   R   R7   R$   R   R.   R   t	   bytearrayR(   R   R   R   RL   R   t   chr(   R%   t   intft   ifaceR   R   (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip6tc.pyt   get_in_interface  s"    """
c         C   s!  | d d k r5 |  j  j j t j O_ | d } n  |  j  j j t j t j @M_ t |  t k rz t d |   n  t |  d } | t |  d d k r | d  } | d 8} n  d j	 | d	 t t |  g  j
   |  j  j _ d j	 d
 | d	 t | g  j
   |  j  j _ d  S(   Ni    R   i   s   interface name %s too longR   ii   R   s    s   (   R   R7   R$   R   R.   R5   R   R(   R   R   t   encodeR   R   (   R%   R   t   masklen(    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip6tc.pyt   set_in_interface  s    
,c         C   s(  d } |  j  j j t j @r4 d j d | g  } n  t t  } |  j  j j | t	 |  j  j j  *t t  } |  j  j j
 | t	 |  j  j j
  *| d d k r d  Sx} t t  D]o } | | d k r d j | t | |  g  } q | | d d k rd j | d g  } n
 | d  } Pq W| S(   NR   R   i    i   R   i(   R   R7   R$   R   R/   R   R   R(   R   R   R    RL   R   R   (   R%   R   R   R   R   (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip6tc.pyt   get_out_interface  s"    """
c         C   s!  | d d k r5 |  j  j j t j O_ | d } n  |  j  j j t j t j @M_ t |  t k rz t d |   n  t |  d } | t |  d d k r | d  } | d 8} n  d j	 | d	 t t |  g  j
   |  j  j _ d j	 d
 | d	 t | g  j
   |  j  j _ d  S(   Ni    R   i   s   interface name %s too longR   ii   R   s    s   (   R   R7   R$   R   R/   R5   R   R(   R   R   R   R   R    (   R%   R   R   (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip6tc.pyt   set_out_interface  s    
,c         C   se   |  j  j j t j @r d } n d } d j | |  j j |  j  j j t	 |  j  j j   g  } | S(   NR   R   (
   R   R7   R$   R   R4   R   t	   protocolst   getR!   R   (   R%   R!   (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip6tc.pyt   get_protocol  s
    	<c         C   s  t  |  } | d d k ra |  j j j t j O_ |  j j j t j t j @M_ | d } n8 |  j j j t j t j	 @M_ |  j j j t j O_ | j
   r t |  |  j j _ d  SxD |  j j   D]3 } | j   | d k r | d |  j j _ d  Sq Wt d |   d  S(   Ni    R   i   s   invalid protocol %s(   R   R   R7   R$   R   R4   R#   R*   R-   R5   R   R   R!   R   t   itemst   lowerR   (   R%   R!   t   p(    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip6tc.pyt   set_protocol  s"    c         C   s
   |  j  j S(   N(   R   R7   (   R%   (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip6tc.pyt   get_ip  s    c         C   s   t  t j t   S(   N(   R   R   t   sizeofR6   (   R%   (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip6tc.pyt   _entry_size  s    c         C   s   t  S(   N(   R6   (   R%   (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip6tc.pyt   _entry_type  s    c         C   s   t    S(   N(   R6   (   R%   (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip6tc.pyt
   _new_entry  s    N(!   R   R   R   RL   R&   R   R   R   t   propertyt   tablesR   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   (    (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip6tc.pyR
      s:   							
	
												c           B   sn   e  Z d  Z d Z d Z d Z d Z d Z d d d d d g Z e	   Z
 d	 d  Z d   Z d	 d	 d  Z RS(
   s<  The IPv6 version of Table.

    There are four fixed tables:
        * **Table.FILTER**, the filter table,
        * **Table.MANGLE**, the mangle table,
        * **Table.RAW**, the raw table and
        * **Table.SECURITY**, the security table.

    The four tables are cached, so if you create a new Table, and it has been
    instantiated before, then it will be reused. To get access to e.g. the
    filter table:

    >>> import iptc
    >>> table = iptc.Table6(iptc.Table6.FILTER)

    The interface provided by *Table* is rather low-level, in fact it maps to
    *libiptc* API calls one by one, and take low-level iptables structs as
    parameters.  It is encouraged to, when possible, use Chain, Rule, Match
    and Target to achieve what is wanted instead, since they hide the
    low-level details from the user.
    t   filtert   manglet   rawt   natt   securityc         C   s{   t  j j | d   } | s_ t j |   } | d  k r? t } n  | j | |  | t  j | <n | d  k	 rw | | _ n  | S(   N(	   R	   t   _cacheR   RL   t   objectt   __new__R   t   _initt
   autocommit(   t   clsR   R   t   obj(    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip6tc.pyR   G  s    	c         C   s5   t    |  _ d |  _ | |  _ | |  _ |  j   d S(   s.  
        Here *name* is the name of the table to instantiate, if it has already
        been instantiated the existing cached object is returned.
        *Autocommit* specifies that any low-level iptables operation should be
        committed immediately, making changes visible in the kernel.
        N(   R@   t   _iptcRL   t   _handleR   R   t   refresh(   R%   R   R   (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip6tc.pyR   S  s
    			c         C   s   t  | |  S(   N(   R
   (   R%   R   R   (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip6tc.pyt   create_rule`  s    N(   R   R   R   t   FILTERt   MANGLEt   RAWt   NATt   SECURITYR   t   dictR   RL   R   R   R   (    (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip6tc.pyR	   !  s   		(   t   ctypesR   R   t   ip4tcR    R   R   t   utilR   R   t   xtablesR   R   R   R   t   __all__R(   R   t	   StructureR   R   R6   RB   t   _R   R@   R
   R	   (    (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip6tc.pyt   <module>   s&   "		$ =