
Zc           @   s  d  d l  Z  d  d l Z d  d l Z d  d l Z d  d l Z d  d l Z d  d l Z d  d l Z d d l	 m
 Z
 m Z d d l m Z m Z m Z m Z m Z m Z m Z m Z d d d d d	 d
 d g Z y e d  Wn n Xe e d  re e d d  n  d Z e j d  Z e j Z e j e j  e _ e j Z  e j e j!  e  _ e j" g e  _# e j$ Z% e& e% _ e j e j!  g e% _# e e  d   Z' d e j( f d     YZ) d e j( f d     YZ* d e j( f d     YZ+ d e, f d     YZ- e
 d d  \ Z. Z/ d e0 f d     YZ1 d e0 f d     YZ2 d e0 f d     YZ3 d e2 f d      YZ4 d	 e2 f d!     YZ5 d
 e0 f d"     YZ6 d#   Z7 d$   Z8 d e0 f d%     YZ9 d e0 f d&     YZ: d'   Z; d e0 f d(     YZ< d S()   iNi   (   t   find_libraryt   load_kernel(   t   XT_INV_PROTOt   NFPROTO_IPV4t   XTablesErrort   xtablest   xt_alignt   xt_counterst   xt_entry_targett   xt_entry_matcht   Tablet   Chaint   Rulet   Matcht   Targett   Policyt	   IPTCErrort	   ip_tablest   IPPROTO_SCTPi   i   s	   libc.so.6c         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/ip4tc.pyt   is_table_available+   s    
t   in_addrc           B   s    e  Z d  Z d e j f g Z RS(   s7   This class is a representation of the C struct in_addr.t   s_addr(   t   __name__t
   __module__t   __doc__t   ctt   c_uint32t   _fields_(    (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyR   4   s   t   ipt_ipc           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 g 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(   s6   This class is a representation of the C struct ipt_ip.t   srct   dstt   smskt   dmskt   inifacet   outifacet   iniface_maskt   outiface_maskt   protot   flagst   invflagsi   i   i   i   i   i   i    i   c         C   s   d |  j  _ |  j _ d  S(   NI    (   R"   R   R#   (   t   self(    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyt   __init__V   s    (   R   R   R   R   R   t   c_chart	   _IFNAMSIZt   c_uint16t   c_uint8R   t
   IPT_F_FRAGt
   IPT_F_GOTOt
   IPT_F_MASKt   IPT_INV_VIA_INt   IPT_INV_VIA_OUTt   IPT_INV_TOSt   IPT_INV_SRCIPt   IPT_INV_DSTIPt   IPT_INV_FRAGR   t   IPT_INV_PROTOt   IPT_INV_MASKR,   (    (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyR   9   s0   				t	   ipt_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(	   s9   This class is a representation of the C struct ipt_entry.t   ipt   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/ip4tc.pyR<   [   s   		c           B   s   e  Z d  Z RS(   s   This exception is raised when a low-level libiptc error occurs.

    It contains a short description about the error that occurred while
    executing an iptables operation.
    (   R   R   R   (    (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyR   f   s   t   ip4tct   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 j Z e j e _ e j	 e j e  e j e j g e _
 e j  Z  e j e  _ e j	 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  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 j% e j	 e j g e& _
 e j' Z' e j e  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 j	 e j% e j e  e j g e) _
 e j* Z* e j	 e* _ e j g e* _
 RS(   s*   This class contains all libiptc API calls.N(+   R   R   R   t   _libiptct	   iptc_initR   t   POINTERt   c_intt   restypet   c_char_pt   argstypet	   iptc_freet   Nonet   c_void_pt   iptc_committ   iptc_builtint   iptc_first_chaint   iptc_next_chaint   iptc_is_chaint   iptc_create_chaint   iptc_delete_chaint   iptc_rename_chaint   iptc_flush_entriest   iptc_zero_entriest   iptc_get_policyR   t   iptc_set_policyt   iptc_first_ruleR<   t   iptc_next_rulet   iptc_get_targett   iptc_insert_entryt   iptc_replace_entryt   iptc_append_entryt   iptc_delete_entryRE   t   iptc_delete_num_entryRD   t   iptc_get_referencest   iptc_read_countert   iptc_zero_countert   iptc_set_countert   iptc_strerror(    (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyRG   q   s   																													t
   IPTCModulec           B   s   e  Z d  Z e j d  Z d   Z d d  Z d   Z	 d   Z
 d   Z d   Z d   Z d	   Z d
   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z e e  Z d   Z e e  Z d   Z d   Z e e e  Z RS(   s    Superclass for Match and Target.s>   \s*(!)?\s*--([-\w]+)\s+(!)?\s*"?([^"]*?)"?(?=\s*(?:!?\s*--|$))c         C   sC   d  |  _ d  |  _ d  |  _ d  |  _ d  |  _ d  |  _ t    d  S(   N(   RP   t   _namet   _rulet   _modulet	   _revisiont   _ptrt   _ptrptrt   NotImplementedError(   R+   (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyR,     s    						c         C   s1   | d k r d } n  |  j | j d d  |  S(   s/  
        Set a parameter for target or match extension, with an optional value.

        @param parameter: name of the parameter to set
        @type parameter: C{str}

        @param value: optional value of the parameter, defaults to C{None}
        @type value: C{str} or a C{list} of C{str}
        t    t   _t   -N(   RP   t   parset   replace(   R+   t	   parametert   value(    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyt   set_parameter  s    
	c         C   s  | j    } t j d  } t |  d k rY | d d k rY t j d  } | d } n  g  } t | t  } y | s t | t  } n  Wn n X| r | j    g } n9 y# g  | D] } | j    ^ q } Wn t d   n X|  j j	 r|  j j
 rt d |  j j | f   n  | j   } t |  } t j | d   } | | d <x& t |  D] }	 | |	 | |	 d <qaW|  j j rt j |  j j  pd  }
 |  j | | |
  d  S(   Ni    t   !i   s:   Invalid parameter value: must be string or list of stringss   %s: invalid parameter %s(   t   encodeR   RK   t   lent
   isinstancet   strt   unicodet	   TypeErrorRn   t
   extra_optst
   x6_optionst   AttributeErrorR   t   stripRM   t   rangeRm   t   entryt   pointerRP   t   _parse(   R+   Rx   Ry   t   invt   argst   is_strt   valt   Nt   argvt   iR   (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyRv     s:    "#
'c         C   s   t     d  S(   N(   Rr   (   R+   R   R   R   (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyR   N  s    c         C   s$   |  j  r  |  j   |  j   n  d  S(   N(   Rn   t   _update_parameterst   _final_check(   R+   (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyt   final_checkQ  s    	
c         C   s   t     d  S(   N(   Rr   (   R+   (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyR   V  s    c         C   s   |  j  s |  j  j r d  St j j   t j d  } z| t j   } t j	 | d d  |  j
 j |  j  | |  j  t j | d d  } t j | d  t j | d  | SWd  t j	 | d  t j |  Xd  S(   Ni   i    i   (   Rn   t   saveRP   t   syst   stdoutt   flusht   ost   dupt   pipet   dup2t   _xtRp   t   readt   close(   R+   R=   R   t   pipest   buf(    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyt   _get_saved_bufY  s    c         C   s   |  j  | |  j j    S(   N(   t   _savet   rulet   get_ip(   R+   R   (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyR   x  s    c         C   sg   |  j  |  j   } | d  k r% d  S|  j s< |  j j r@ d  S| rV |  j | |  S|  j |  Sd  S(   N(   R   t   decodeRP   Rn   R   t
   _get_valuet   _get_all_values(   R+   R   R=   R   (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyR   {  s    c         C   s   i  } t  j t j |  } x] | D]U } | d | d p@ | d } } d | rV d pY d | f | | d j d d	  <q" W| S(
   Ni   i    i   s   %s%sR{   Rs   i   Ru   Rt   (   t   ret   findallRk   t   patternRw   (   R+   R   t   tablet   rest   xRy   t   invert(    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyR     s    "c         C   s   i  } t  j t j |  } x4 | D], } | d | d p@ | d f | | d <q" Wy. | | \ } } d | rt d pw d | f SWn t k
 r d  SXd  S(   Ni   i    i   i   s   %s%sR{   Rs   (   R   R   Rk   R   t   KeyErrorRP   (   R+   R   R   R   R   R   Ry   R   (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyR     s    *c         C   s  i  } |  j  j   } |  j |  } | d  k r4 | St |  t k rU | j   } n  t j |  } | j	   t
 } x t |  d k r| j   } | d k r t } qw n  | j d  r | d } | r d g | | <n
 g  | | <t
 } qw n  | | j |  qw W| S(   Ni    R{   s   --i   (   R   R   R   RP   t   typeR   R   t   shlext   splitt   reverseR   R}   t   popR   t
   startswitht   append(   R+   t   paramsR=   R   R   R   R   t   key(    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyt   get_all_parameters  s0    


c         C   sG   |  j    j   } |  j   x$ | D] \ } } |  j | |  q# Wd  S(   N(   R   t   itemst   resetRz   (   R+   R   t   kt   v(    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyR     s    
c         C   sP   |  j  s |  j r d  St |  j  d d   } | s7 d  S|  j  j |  j  j   S(   Nt   alias(   Rn   Rp   RP   t   getattrR   R   (   R+   R   (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyt   _get_alias_name  s    c         C   sX   | j  d  rA | t |   k rA |  j | j d d  |  n t j |  | |  d  S(   NRt   Ru   (   R   t   dirRv   Rw   t   objectt   __setattr__(   R+   R   Ry   (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyR     s    "c         C   s,   | j  d  s( |  j | j d d   Sd  S(   NRt   Ru   (   R   R   Rw   (   R+   R   (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyt   __getattr__  s    c         C   s   |  j  d   S(   N(   R   RP   (   R+   (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyt   _get_parameters  s    c         C   s   |  j    } | r | p |  j S(   N(   R   Rl   (   R+   R   (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyt	   _get_name  s    c         C   s   |  j  S(   N(   Rm   (   R+   (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyt	   _get_rule  s    c         C   s   | |  _  d  S(   N(   Rm   (   R+   R   (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyt	   _set_rule  s    N(   R   R   R   R   t   compileR   R,   RP   Rz   Rv   R   R   R   R   R   R   R   R   R   R   R   R   R   R   t   propertyt
   parametersR   R   R   R   R   (    (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyRk     s6   				/																		t   _Bufferc           B   s   e  Z d  d  Z d   Z RS(   i    c         C   sI   | d k r< t  |  |  _ |  j d  k rE t d   qE n	 d  |  _ d  S(   Ni    s   Can't allocate buffer(   t   _malloct   bufferRP   t	   Exception(   R+   t   size(    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyR,     s
    c         C   s#   |  j  d  k	 r t |  j   n  d  S(   N(   R   RP   t   _free(   R+   (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyt   __del__  s    (   R   R   R,   R   (    (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyR     s   c           B   s   e  Z d  Z d d d d  Z d   Z d   Z d   Z d   Z d   Z	 d   Z
 d   Z e e  Z d	   Z e e  Z d
   Z d   Z d   Z d   Z e e  Z d   Z e e  Z RS(   sj  Matches are extensions which can match for special header fields or
    other attributes of a packet.

    Target and match extensions in iptables have parameters.  These parameters
    are implemented as instance attributes in python.  However, to make the
    names of parameters legal attribute names they have to be converted.  The
    rule is to cut the leading double dash from the name, and replace
    dashes in parameter names with underscores so they are accepted by
    python as attribute names.  E.g. the *TOS* target has parameters
    *--set-tos*, *--and-tos*, *--or-tos* and *--xor-tos*; they become
    *target.set_tos*, *target.and_tos*, *target.or_tos* and *target.xor_tos*,
    respectively.  The value of a parameter is always a string, if a parameter
    does not take any value in the iptables extension, an empty string *""*
    should be used.

    c         C   s  | r | r t  d   n  | s; | j j j j   } n  | |  _ | |  _ d |  _ d |  _	 t
 | j  |  _ |  j j |  } | r t | d d d  p d } | r| j   |  _ t | d d d  |  _ t | d d d  |  _	 |  j j |  } n  | st d |   n  | d |  _ d |  j _ | d k	 rK| |  _ n |  j j |  _ t j |  j   |  _ | rt j t j |  j  t j |  |  j  |  j   |  j   n
 |  j   d S(   sS  
        *rule* is the Rule object this match belongs to; it can be changed
        later via *set_rule()*.  *name* is the name of the iptables match
        extension (in lower case), *match* is the raw buffer of the match
        structure if the caller has it.  Either *name* or *match* must be
        provided.  *revision* is the revision number of the extension that
        should be used; different revisions use different structures in C and
        they usually only work with certain kernel versions. Python-iptables
        by default will use the latest revision available.
        s#   can't create match based on nothingi    t	   real_namet   x6_parseR   s   can't find match %sN(   t
   ValueErrort   ut   userR   R   Rl   Rm   RP   t   _orig_parset   _orig_optionsR   t   nfprotoR   t
   find_matchR   R   Rn   t   mflagsRo   t   revisionR   RE   R   t
   _match_buft   memmovet   byreft   _update_pointerst   _check_aliasR   (   R+   R   R   t   matchR   t   moduleR   (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyR,     s:    				"+
c         C   s   |  j    } | d  k r d  S|  j j |  } | d  k r> d  S| d |  _ t |  j d d   |  _ t |  j d d   |  _ d  S(   Ni    R   R   (   R   RP   R   R   t   _alias_moduleR   R   R   (   R+   R   t   alias_module(    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyR   4  s    c         C   s   t  j t  } |  j j j | j j j k r |  j j j j | j j j j k r |  j j j j | j j j j k r |  j	 | |  j
 !| j	 | | j
 !k r t St S(   N(   R   t   sizeofR	   R   R   t
   match_sizeR   R   R   t	   match_buft   usersizeR   R   (   R+   R   t   basesz(    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyt   __eq__?  s    $$c         C   sR   t  |  j j j  t  |  j j j j  At  |  j j j j  At  t |  j   AS(   N(	   t   hashR   R   R   R   R   R   t   bytesR   (   R+   (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyt   __hash__I  s    >c         C   s   |  j  |  S(   N(   R   (   R+   R   (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyt   __ne__O  s    c         C   s   |  j  j |  j  d  S(   N(   R   t   final_check_matchRn   (   R+   (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyR   R  s    c      	   C   sJ   |  j  j | | |  j | t j |  j t j t j   |  j |  j	  d  S(   N(
   R   t   parse_matchRn   R   t   castRq   RJ   RQ   R   R   (   R+   R   R   R   (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyR   U  s    c         C   s   t  |  j j t j t   S(   N(   R   Rn   R   R   R   R	   (   R+   (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyt	   _get_sizeZ  s    c         C   s   |  j  j t j t  S(   N(   Rn   t   userspacesizeR   R   R	   (   R+   (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyt   _get_user_size_  s    c         C   sz   t  j t  j |  j  t  j t   |  _ t  j t  j |  j  t  j t  j t    |  _ |  j |  j	 _
 |  j   d  S(   N(   R   R   R   R   RJ   R	   Rp   R   Rq   Rn   t   mt   _update_name(   R+   (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyR   e  s    c         C   s)   |  j  d } |  j j   | j j _ d  S(   Ni    (   Rp   R   R|   R   R   (   R+   R   (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyR   m  s    c         C   s   t  j t  j |  j  d |  j  |  j   |  j d } |  j | j _ |  j	 | j j
 _ |  j j r| |  j j |  j  n  d |  j _ t |  j d d  } | d k r t  j |   } t  j t  j |  t  j  |  j _ n  d S(   s\   Reset the match.

        Parameters are set to their default values, any flags are cleared.i    t
   udata_sizeN(   R   t   memsetR   R   R   R   Rp   R   R   Ro   R   R   Rn   t   initR   R   RE   R   RQ   t   udata(   R+   R   R   t	   udata_buf(    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyR   q  s    "
c         C   s)   t  j t  j |  j  t  j t   d S(   Ni    (   R   R   R   R   RJ   R	   (   R+   (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyt
   _get_match  s    c         C   s   |  j  S(   N(   R   (   R+   (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyt   _get_match_buf  s    N(   R   R   R   RP   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/ip4tc.pyR     s(   0		
											c           B   s   e  Z d  Z d d d d d  Z d   Z d   Z d   Z d   Z d   Z	 d   Z
 d   Z d	   Z e e  Z d
   Z e e  Z d   Z d   Z e e e  Z d   Z d   Z d   Z d   Z e e  Z d   Z e e  Z RS(   s  Targets specify what to do with a packet when a match is found while
    traversing the list of rule entries in a chain.

    Target and match extensions in iptables have parameters.  These parameters
    are implemented as instance attributes in python.  However, to make the
    names of parameters legal attribute names they have to be converted.  The
    rule is to cut the leading double dash from the name, and replace
    dashes in parameter names with underscores so they are accepted by
    python as attribute names.  E.g. the *TOS* target has parameters
    *--set-tos*, *--and-tos*, *--or-tos* and *--xor-tos*; they become
    *target.set_tos*, *target.and_tos*, *target.or_tos* and *target.xor_tos*,
    respectively.  The value of a parameter is always a string, if a parameter
    does not take any value in the iptables extension, an empty string i.e. ""
    should be used.
    c         C   s  | d
 k r' | d
 k r' t d   n  | d
 k rK | j j j j   } n  | |  _ | |  _ d
 |  _ d
 |  _	 | j
   } g  t |  D] } | j d  r | ^ q } t |  d k r t d   n  t | | d  }	 | d
 k	 s | d
 k rt | j |	 @ |  _ n  | d
 k	 rct | t  s/t  | |  _ | rP| j |	 O_ qc| j |	 M_ n  t | j  |  _ |  j   r|  j j d  p|  j j |  }
 |
 rt |
 d d d
  pd
 } | r#| j   |  _ t |
 d d d
  |  _ t |
 d d d
  |  _	 |  j j |  }
 n  |
 s<t d	 |   n  |
 d |  _ d |  j _ | d
 k	 rm| |  _ n |  j j |  _ |  j |  |  j   r| |  _  n | r|  j!   n  d
 S(   s  
        *rule* is the Rule object this match belongs to; it can be changed
        later via *set_rule()*.  *name* is the name of the iptables target
        extension (in upper case), *target* is the raw buffer of the target
        structure if the caller has it.  Either *name* or *target* must be
        provided.  *revision* is the revision number of the extension that
        should be used; different revisions use different structures in C and
        they usually only work with certain kernel versions. Python-iptables
        by default will use the latest revision available.
        If goto is True, then it converts '-j' to '-g'.
        s$   can't create target based on nothingt   _F_GOTOi    sB   What kind of struct is this? It does not have "*_F_GOTO" constant!Rs   R   R   R   s   can't find target %sN("   RP   R   R   R   R   R   Rl   Rm   R   R   R   R   t   endswithR}   t   RuntimeErrorR   t   boolR)   t   _gotoR~   t   AssertionErrorR   R   R   t   _is_standard_targett   find_targetR   Rn   t   tflagsRo   R   t   _create_buffert   standard_targetR   (   R+   R   R   t   targetR   t   gotot   ipstructt   at   f_goto_attrsR   R   R   (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyR,     sV    				.	"c         C   s   |  j    } | d  k r d  S|  j j |  } | d  k r> d  S| d |  _ t |  j d d   |  _ t |  j d d   |  _ d  S(   Ni    R   R   (   R   RP   R   R  R   R   R   R   (   R+   R   R   (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyR     s    c         C   sG  t  j t  } |  j j j | j j j k su |  j j j j | j j j j k su |  j j j j | j j j j k ry t	 S|  j j j j d k s|  j j j j d k s|  j j j j d k s|  j j j j d k s|  j j j j d k s|  j j j j d k s|  j
   rt S|  j | |  j !| j | | j !k rCt St	 S(   NRs   t   standardt   ACCEPTt   DROPt   RETURNt   ERROR(   R   R   R   R  R   t   target_sizeR   R   R   R   R  R   t   _target_bufR   (   R+   t   targR   (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyR     s"    $$c         C   s   |  j  |  S(   N(   R   (   R+   R  (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyR     s    c         C   sd   t  |  j  |  _ |  j j |  _ | rV t j |  j t j |  |  j  |  j   n
 |  j	   d  S(   N(
   R   R   t   _bufferR   R  R   R   R   R   R   (   R+   R  (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyR	    s    "c         C   s1   x* |  j  j D] } | j |  j  r t Sq Wt S(   N(   Rm   t   tablest   is_chainRl   R   R   (   R+   t   t(    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyR    s    c         C   s   |  j  j |  j  d  S(   N(   R   t   final_check_targetRn   (   R+   (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyR     s    c      	   C   s   |  j  j | | |  j | t j |  j t j t j   |  j |  j	  t j |  j j
 t j t j   |  _ |  j j |  j k r |  j |  j _ n  |  j   d  S(   N(   R   t   parse_targetRn   R   R   Rq   RJ   RQ   R   R   R  RE   R  R  R   R   (   R+   R   R   R   (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyR     s    'c         C   s   t  |  j j t j t   S(   N(   R   Rn   R   R   R   R   (   R+   (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyR   !  s    c         C   s   |  j  j t j t  S(   N(   Rn   R   R   R   R   (   R+   (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyR   &  s    c         C   s    |  j  d } | j j j j   S(   Ni    (   Rp   R   R   R   R   (   R+   R  (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyt   _get_standard_target,  s    c         C   se   |  j  d } t | t  r+ | j   } n  | | j j _ t | t  rX | j   } n  | |  _	 d  S(   Ni    (
   Rp   R~   R   R|   R   R   R   R   R   Rl   (   R+   R   R  (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyt   _set_standard_target0  s    c         C   sq   t  j |  j t  j t   |  _ t  j t  j |  j  t  j t  j t    |  _ |  j |  j _	 |  j
   d  S(   N(   R   R   R  RJ   R   Rp   R   Rq   Rn   R  R   (   R+   (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyR   =  s
    !c         C   s)   |  j  d } |  j j   | j j _ d  S(   Ni    (   Rp   R   R|   R   R   (   R+   R   (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyR   D  s    c         C   s   t  j |  j d |  j  |  j   |  j d } |  j | j _ |  j | j j	 _
 |  j j rs |  j j |  j  n  d |  j _ t |  j d d  } | d k r t  j |   } t  j t  j |  t  j  |  j _ n  d S(   s]   Reset the target.  Parameters are set to their default values, any
        flags are cleared.i    R   N(   R   R   R  R   R   Rp   R   R  Ro   R   R   Rn   R   R  R   RE   R   R   RQ   R   (   R+   R  R   R   (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyR   H  s    
c         C   s   |  j  d S(   Ni    (   Rp   (   R+   (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyt   _get_targetX  s    c         C   s   |  j  S(   N(   R  (   R+   (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyt	   _get_goto]  s    N(   R   R   R   RP   R,   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/ip4tc.pyR     s.   H																		c           B   sD   e  Z d  Z d Z d Z d Z d Z e j   Z	 d   Z
 d   Z RS(   s   
    If the end of a built-in chain is reached or a rule in a built-in chain
    with target RETURN is matched, the target specified by the chain policy
    determines the fate of the packet.
    R  R  t   QUEUER  c         C   s>   t  j j | d   } | s: t j |   } | t  j | <n  | S(   N(   R   t   _cachet   getRP   R   t   __new__(   t   clsR   t   obj(    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyR%  u  s
    c         C   s   | |  _  d  S(   N(   R   (   R+   R   (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyR,   |  s    (   R   R   R   R  R  R"  R  t   weakreft   WeakValueDictionaryR#  R%  R,   (    (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyR   b  s   	c         C   s   t  j d |   d S(   Nt   Ii    (   t   structt   unpack(   t   addr(    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyt   _a_to_i  s    c         C   s   t  j d t |  j   S(   NR*  (   R+  t   packt   intR   (   R=   (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyt   _i_to_a  s    c           B   s  e  Z d  Z i d d 6d e j 6d e j 6d e j 6d e j 6d e j 6d e j	 6d	 e j
 6d
 e j 6d e j 6d e j 6d e j 6d e j 6d e j 6d e j 6d e j 6d e j 6d e j 6d e j 6d e j 6d e j 6d e j 6d e j 6d e j 6d e j 6Z d; d; d  Z d   Z d   Z  d   Z! e" e!  Z# d   Z$ d; d   Z% d; e& d!  Z' d"   Z( d#   Z) d$   Z* d%   Z+ e" e+  Z, d&   Z- d'   Z. e" e- e.  Z/ d(   Z0 d)   Z1 e" e0 e1  Z2 d*   Z3 d+   Z4 e" e3 e4  Z5 d,   Z6 d-   Z7 e" e6 e7  Z8 d.   Z9 d/   Z: e" e9 e:  Z; d0   Z< d1   Z= e" e< e=  Z> d2   Z? d3   Z@ e" e? e@  ZA d4   ZB d5   ZC d6   ZD d7   ZE d8   ZF d9   ZG e" eF eG  ZH d:   ZI e" eI  ZJ RS(<   sH  Rules are entries in chains.

    Each rule has three parts:
        * An entry with protocol family attributes like source and destination
          address, transport protocol, etc.  If the packet does not match the
          attributes set here, then processing continues with the next rule or
          the chain policy is applied at the end of the chain.
        * Any number of matches.  They are optional, and make it possible to
          match for further packet attributes.
        * One target.  This determines what happens with the packet if it is
          matched.
    t   alli    t   aht   dstoptst   egpt   espt   fragmentt   gret   hopoptst   icmpt   icmpv6t   idpt   igmpR=   t   ipipt   ipv6t   nonet   pimt   pupt   rawt   routingt   rsvpt   sctpt   tcpt   tpt   udpc         C   s1   t  |  _ g  |  _ d |  _ | |  _ | |  _ d S(   s   
        *entry* is the ipt_entry buffer or None if the caller does not have
        it.  *chain* is the chain object this rule belongs to.
        N(   R   R   t   _matchesRP   t   _targett   chainR   (   R+   R   RL  (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.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 |  j
 | j
 k r t St S(   N(   RK  R   R}   RJ  t   setR    R!   t   protocolR7  t   in_interfacet   out_interfaceR   (   R+   R   R   (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyR     s    "$c         C   s   |  j  |  S(   N(   R   (   R+   R   (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyR     s    c         C   s,   g  t  j D] } t |  r
 t  |  ^ q
 S(   N(   R
   t   ALLR   (   R+   R  (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyt   _get_tables  s    c         C   s;   |  j  r |  j  j   n  x |  j D] } | j   q# Wd S(   s/   Do a final check on the target and the matches.N(   R  R   t   matches(   R+   R   (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyR     s    	c         C   s)   t  |  d | d | } |  j |  | S(   s   Create a *match*, and add it to the list of matches in this rule.
        *name* is the name of the match extension, *revision* is the revision
        to use.R   R   (   R   t	   add_match(   R+   R   R   R   (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyt   create_match  s    c         C   s+   t  |  d | d | d | } | |  _ | S(   s   Create a new *target*, and set it as this rule's target. *name* is
        the name of the target extension, *revision* is the revision to
        use. *goto* determines if target uses '-j' (default) or '-g'.R   R   R  (   R   R  (   R+   R   R   R  R  (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyt   create_target  s    	c         C   s   |  | _  |  j j |  d S(   s=   Adds a match to the rule.  One can add any number of matches.N(   R   RJ  R   (   R+   R   (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyRT    s    	c         C   s   |  j  j |  d S(   s)   Removes *match* from the list of matches.N(   RJ  t   remove(   R+   R   (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyt   remove_match  s    c         C   s
   |  j  j S(   N(   R   R=   (   R+   (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyR     s    c         C   s   |  j  S(   N(   RJ  (   R+   (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyt   _get_matches  s    c         C   s   |  j  S(   N(   RK  (   R+   (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyR     s    c         C   s   |  | _  | |  _ d  S(   N(   R   RK  (   R+   R  (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyt   _set_target  s    	c         C   s  d } |  j  j j t j @r4 d j | d g  } n  t |  j  j j  } y t j	 t j
 |  } Wn  t j k
 r t d   n Xd j | | d g  } t |  j  j j  } y t j	 t j
 |  } Wn  t j k
 r t d   n Xd j | | g  } | S(   NRs   R{   s(   error in internal state: invalid addresst   /s(   error in internal state: invalid netmask(   R   R=   R*   R   R7   t   joinR1  R    t   sockett	   inet_ntopt   AF_INETt   errorR   R"   (   R+   R    t   paddrR-  t   netmask(    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyt   get_src  s     c   
      C   s  | d d k r5 |  j  j j t j O_ | d } n  |  j  j j t j t j @M_ | j d  } | d k r | } d } n | |  } | | d } y t t j	 t j
 |   } Wn$ t j k
 r t d |   n Xt   } t j |  | _ | |  j  j _ | j   s\y t t j	 t j
 |   } Wqt j k
 rXt d |   qXnV t |  } | d	 k s| d k  rt d |   n  t j d
 | d d	 | > } t   }	 t j |  |	 _ |	 |  j  j _ d  S(   Ni    R{   i   R[  is   255.255.255.255s   invalid address %ss   invalid netmask %si    i   (   R   R=   R*   R   R7   R;   t   findR.  R]  t	   inet_ptonR_  R`  R   R   R   R   R   R    t   isdigitR0  t   htonlR"   (
   R+   R    t   slashR-  t   netmt   saddrt   inat   nmaskt   imaskt   neta(    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyt   set_src  s<    	
		c         C   s  d } |  j  j j t j @r4 d j | d g  } n  t |  j  j j  } y t j	 t j
 |  } Wn  t j k
 r t d   n Xd j | | d g  } t |  j  j j  } y t j	 t j
 |  } Wn  t j k
 r t d   n Xd j | | g  } | S(   NRs   R{   s(   error in internal state: invalid addressR[  s(   error in internal state: invalid netmask(   R   R=   R*   R   R8   R\  R1  R!   R]  R^  R_  R`  R   R#   (   R+   R!   Ra  R-  Rb  (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyt   get_dstB  s     c   
      C   s  | d d k r5 |  j  j j t j O_ | d } n  |  j  j j t j t j @M_ | j d  } | d k r | } d } n | |  } | | d } y t t j	 t j
 |   } Wn$ t j k
 r t d |   n Xt   } t j |  | _ | |  j  j _ | j   s\y t t j	 t j
 |   } Wqt j k
 rXt d |   qXnV t |  } | d	 k s| d k  rt d |   n  t j d
 | d d	 | > } t   }	 t j |  |	 _ |	 |  j  j _ d  S(   Ni    R{   i   R[  is   255.255.255.255s   invalid address %ss   invalid netmask %si    i   (   R   R=   R*   R   R8   R;   Rd  R.  R]  Re  R_  R`  R   R   R   R   R   R!   Rf  R0  Rg  R#   (
   R+   R!   Rh  R-  Ri  t   daddrRk  Rl  Rm  Rn  (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyt   set_dstT  s<    	
		c         C   s   d } |  j  j j t j @r% d } n  |  j  j j j   } |  j  j j } t |  d k r_ d  S| | 7} t |  t |  k r | d 7} n  | t
  } | S(   NRs   R{   i    t   +(   R   R=   R*   R   R4   R$   R   R&   R}   RP   R.   (   R+   t   intft   ifacet   mask(    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.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	 | j
   d	 t t |  g  |  j  j _ d j	 d
 | d	 t | g  |  j  j _ d  S(   Ni    R{   i   s   interface name %s too longRs  ii   Rs   s    s   (   R   R=   R*   R   R4   R;   R}   R.   R   R\  R|   R$   R&   (   R+   Rt  t   masklen(    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyt   set_in_interface  s     
#c         C   s   d } |  j  j j t j @r% d } n  |  j  j j j   } |  j  j j } t |  d k r_ d  S| | 7} t |  t |  k r | d 7} n  | t
  } | S(   NRs   R{   i    Rs  (   R   R=   R*   R   R5   R%   R   R'   R}   RP   R.   (   R+   Rt  Ru  Rv  (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.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	 | j
   d	 t t |  g  |  j  j _ d j	 d
 | d	 t | g  |  j  j _ d  S(   Ni    R{   i   s   interface name %s too longRs  ii   Rs   s    s   (   R   R=   R*   R   R5   R;   R}   R.   R   R\  R|   R%   R'   (   R+   Rt  Rx  (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyt   set_out_interface  s     
#c         C   s@   t  |  j j j t j @ } |  j j j t j @r< | } n  | S(   N(   R  R   R=   R)   R   R1   R*   R9   (   R+   t   frag(    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyt   get_fragment  s    
c         C   s^   |  j  j j t j t j @M_ | rA |  j  j j t j O_ n |  j  j j t j M_ d  S(   N(   R   R=   R*   R   R9   R;   R)   R1   (   R+   R|  (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyt   set_fragment  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{   Rs   (
   R   R=   R*   R   R:   R\  t	   protocolsR$  R(   R   (   R+   R(   (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyt   get_protocol  s
    	<c         C   s   t  |  } | d d k rA |  j j j t j O_ | d } n  |  j j j t j t j @M_ | 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   R=   R*   R   R:   R;   Rf  R0  R(   R  R   t   lowerR   (   R+   R(   t   p(    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyt   set_protocol  s    c         C   s   |  j  j } | j | j f S(   sU   This method returns a tuple pair of the packet and byte counters of
        the rule.(   R   RB   t   pcntt   bcnt(   R+   RB   (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyt   get_counters  s    c         C   s   t  t j t   S(   N(   R   R   R   R<   (   R+   (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyt   _entry_size  s    c         C   s   t  S(   N(   R<   (   R+   (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyt   _entry_type	  s    c         C   s   t    S(   N(   R<   (   R+   (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyt
   _new_entry  s    c   	      C   s  |  j  s! |  j s! |  j j r% d  S|  j   } d } x$ |  j D] } | t | j  7} qA Wt |  j j  } | | |  j  _ | | | |  j  _	 t
 j | | |   } t
 j t
 j |  j   t
 j t
 j   } | |  | | *d } xJ |  j D]? } t | j  } | j |  | | | | | | +| | 7} q Wt
 j t
 j |  j j  t
 j t
 j   } | |  | | | | | | +| S(   Ni    (   R   RK  R  RP   R  RJ  R   R   R?   R@   R   RE   R   R   RJ   R   (	   R+   t   entryszt   matchszR   t   targetszR   t   ptrt   offsett   sz(    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyR     s(    !* -c   	      C   s  | s |  j    |  _ d  St j t j |  t j |  j     d |  _ t | |  j    s~ t d | |  j   f   n  |  j	   } | j
 | } | rd } xt | | | j
 k  rt j t j | j |  t j t   d } t |  d | } |  j |  | | j 7} q Wn  t j t j | | j
  t j t   d } t |  d | |  _ |  j j j |  } | r| |  j _ n  d  S(   Ni    s!   Invalid rule type %s; expected %sR   R  (   R  R   R   R   R   RJ   R  R~   R   R  R?   R   RC   R	   R   RT  R   R   R   R  RL  R   t
   get_targetRK  R
  (	   R+   R   R  R  t   offR   R   R  t   jump(    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyR   0  s0    c         C   s  |  j  s d  S|  j   } |  j  j | } |  j  j |  j  j } t j | | |   } d } x% t | | |  D] } d | | <qq W| | 7} xI |  j D]> } x( t | | | j	  D] } d | | <q W| | j
 7} q Wx+ t | | |  j j	  D] } d | | <q W| S(   Ni    i   (   R   RP   R  R?   R@   R   RE   R   RJ  R   R   RK  (   R+   R  R  R  Rv  t   posR   R   (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyt	   _get_maskT  s"    	
 N(K   R   R   R   R]  t
   IPPROTO_AHt   IPPROTO_DSTOPTSt   IPPROTO_EGPt   IPPROTO_ESPt   IPPROTO_FRAGMENTt   IPPROTO_GREt   IPPROTO_HOPOPTSt   IPPROTO_ICMPt   IPPROTO_ICMPV6t   IPPROTO_IDPt   IPPROTO_IGMPt
   IPPROTO_IPt   IPPROTO_IPIPt   IPPROTO_IPV6t   IPPROTO_NONEt   IPPROTO_PIMt   IPPROTO_PUPt   IPPROTO_RAWt   IPPROTO_ROUTINGt   IPPROTO_RSVPR   t   IPPROTO_TCPt
   IPPROTO_TPt   IPPROTO_UDPR  RP   R,   R   R   RR  R   R  R   RU  R   RV  RT  RX  R   RY  RS  R   RZ  R  Rc  Ro  R    Rp  Rr  R!   Rw  Ry  RO  Rz  R{  RP  R}  R~  R7  R  R  RN  R  R  R  R  R   R   R   R  Rv  (    (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyR     s   























												'		&													!	!	c           B   s   e  Z d  Z e j   Z d   Z d   Z d   Z d   Z	 d   Z
 d   Z d   Z d d  Z d	   Z d
   Z d   Z d d  Z d d  Z d   Z d   Z d   Z e e  Z RS(   s  Rules are contained by chains.

    *iptables* has built-in chains for every table, and users can also create
    additional chains.  Rule targets can specify to jump into another chain
    and continue processing its rules, or return to the caller chain.
    c         C   sh   t  |  j d | j } t j j | d | d   } | sd t j |   } | t j | d | <n  | S(   Nt   .(	   R   R   R   R   R#  R$  RP   R   R%  (   R&  R   R   t
   table_nameR'  (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyR%  z  s    c         C   s   | |  _  | |  _ d S(   s   *table* is the table this chain belongs to, *name* is the chain's
        name.

        If a chain already exists with *name* in *table* it is returned.
        N(   R   R   (   R+   R   R   (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyR,     s    	c         C   s   |  j  j |  j  d S(   s   Delete chain from its table.N(   R   t   delete_chainR   (   R+   (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyt   delete  s    c         C   s   |  j  j |  j |  d S(   s   Rename chain to *new_name*.N(   R   t   rename_chainR   (   R+   t   new_name(    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyt   rename  s    c         C   s   |  j  j |  j  d S(   s   Flush all rules from the chain.N(   R   t   flush_entriesR   (   R+   (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyR     s    c         C   s   |  j  j |  j  \ } } | S(   sV   This method returns a tuple pair of the packet and byte counters of
        the chain.(   R   t
   get_policyR   (   R+   t   policyRB   (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyR    s    c         C   s   |  j  j |  j  d S(   s=   This method zeroes the packet and byte counters of the chain.N(   R   t   zero_entriesR   (   R+   (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyt   zero_counters  s    c         C   s8   t  | t  r | j } n  |  j j |  j | |  d S(   s   Set the chain policy to *policy*, which should either be a string
        or a Policy object.  If *counters* is not *None*, the chain counters
        are also adjusted. *Counters* is a list or tuple with two elements.N(   R~   R   R   R   t
   set_policy(   R+   R  RB   (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyR    s    c         C   s   |  j  j |  j  \ } } | S(   s3   Returns the policy of the chain as a Policy object.(   R   R  R   (   R+   R  RB   (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyR    s    c         C   s   |  j  j |  j  S(   s,   Returns whether the chain is a built-in one.(   R   t   builtin_chainR   (   R+   (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyt
   is_builtin  s    c         C   sB   | j    | j } | s( t d   n  |  j j |  j |  d S(   s&   Append *rule* to the end of the chain.s   invalid ruleN(   R   R   R   R   t   append_entryR   (   R+   R   t   rbuf(    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyt   append_rule  s
    
	i    c         C   sE   | j    | j } | s( t d   n  |  j j |  j | |  d S(   s   Insert *rule* as the first entry in the chain if *position* is 0 or
        not specified, else *rule* is inserted in the given position.s   invalid ruleN(   R   R   R   R   t   insert_entryR   (   R+   R   t   positionR  (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyt   insert_rule  s
    
	c         C   s;   | j  } | s t d   n  |  j j |  j | |  d S(   sJ   Replace existing rule in the chain at *position* with given
        *rule*s   invalid ruleN(   R   R   R   t   replace_entryR   (   R+   R   R  R  (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyt   replace_rule  s    	c         C   sH   | j    | j } | s( t d   n  |  j j |  j | | j  d S(   s   Removes *rule* from the chain.s   invalid ruleN(   R   R   R   R   t   delete_entryR   Rv  (   R+   R   R  (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyt   delete_rule  s
    
	c         C   s.   | j  } | s t d   n  |  j j |  S(   sd   This method returns the target of *rule* if it is a standard
        target, or *None* if it is not.s   invalid rule(   R   R   R   R  (   R+   R   R  (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyR    s    	c         C   sm   g  } |  j  j |  j  } x) | rF | j |  |  j  j |  } q Wg  | D] } |  j  j | |   ^ qN S(   N(   R   t
   first_ruleR   R   t	   next_rulet   create_rule(   R+   t   entriesR   t   e(    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyt
   _get_rules  s    	N(   R   R   R   R(  R)  R#  R%  R,   R  R  R   R  R  RP   R  R  R  R  R  R  R  R  R  R   t   rules(    (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyR   q  s(   															c            s     f d   } | S(   Nc             s0   |  d }   |    } | j  r, | j   n  | S(   Ni    (   t
   autocommitt   refresh(   R   R'  t   ret(   t   fn(    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyt   new  s
    
	(    (   R  R  (    (   R  s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyR    s    c           B   s  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   Z d	   Z d
   Z e d  Z d   Z d   Z d   Z d   Z e d    Z e d    Z e d    Z e d    Z e d    Z e d! d   Z e d    Z e d    Z e d    Z  e d    Z! e d    Z" d   Z# d   Z$ d   Z% d   Z& e' e&  Z( d   Z) d! d! d   Z* RS("   s7  A table is the most basic building block in iptables.

    There are four fixed tables:
        * **Table.FILTER**, the filter table,
        * **Table.NAT**, the NAT table,
        * **Table.MANGLE**, the mangle table and
        * **Table.RAW**, the raw 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:

    >>> table = iptc.Table(iptc.Table.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   mangleRC  t   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
   R#  R$  RP   R   R%  R   t   _initR  (   R&  R   R  R'  (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyR%    s    	c         C   s5   | |  _  | |  _ t   |  _ d |  _ |  j   d S(   s   
        *name* is the name of the table, if it already exists it is returned.
        *autocommit* specifies that any iptables operation that changes a
        rule, chain or table should be committed immediately.
        N(   R   R  RG   t   _iptcRP   t   _handleR  (   R+   R   R  (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyR  '  s
    			c         C   s   |  j    d  S(   N(   R   (   R+   (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyR   3  s    c         C   s   |  j  r |  j   n  d S(   s3   Close the underlying connection handle to iptables.N(   R  R   (   R+   (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyR   6  s    	c         C   s>   |  j  j |  j  } | d k r: t d |  j     n  d S(   s   Commit any pending operation.i   s   can't commit: %sN(   R  RR   R  R   t   strerror(   R+   t   rv(    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyt   commit;  s    c         C   s   |  j  d  k r t d   n  zC y |  j r: |  j   n  Wn" t k
 r_ } | s` |  q` n XWd  |  j j |  j   d  |  _  Xd  S(   Ns   table is not initialized(   R  RP   R   R  R  R  RO   (   R+   t
   ignore_excR  (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyR   A  s     	c         C   sf   |  j  r |  j   n  |  j j |  j j    } | sY t d |  j |  j   f   n  | |  _  d S(   s@   Commit any pending operation and refresh the status of iptables.s   can't initialize %s: %sN(   R  R   R  RI   R   R|   R   R  (   R+   t   handle(    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyR  N  s    	c         C   sE   t  | t  r | j } n  |  j j | j   |  j  r= t St Sd S(   s,   Returns *True* if *chain* exists as a chain.N(	   R~   R   R   R  RV   R|   R  R   R   (   R+   RL  (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyR  Y  s
    c         C   sE   t  | t  r | j } n  |  j j | j   |  j  r= t St Sd S(   s.   Returns *True* if *chain* is a built-in chain.N(	   R~   R   R   R  RS   R|   R  R   R   (   R+   RL  (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyR  b  s
    c         C   s-   t    d } | d k r d S|  j j |  S(   s?   Returns any pending iptables error from the previous operation.i    s   libiptc version error(   t   _get_errno_locR  Rj   (   R+   t   errno(    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyR  k  s    c         C   sq   t  | t  r | j } n  |  j j | j   |  j  } | d k rd t d | |  j   f   n  t |  |  S(   s   Create a new chain *chain*.i   s   can't create chain %s: %s(	   R~   R   R   R  RW   R|   R  R   R  (   R+   RL  R  (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyt   create_chainr  s    	c         C   sh   t  | t  r | j } n  |  j j | j   |  j  } | d k rd t d | |  j   f   n  d S(   s$   Delete chain *chain* from the table.i   s   can't delete chain %s: %sN(	   R~   R   R   R  RX   R|   R  R   R  (   R+   RL  R  (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyR  }  s    	c         C   sq   t  | t  r | j } n  |  j j | j   | j   |  j  } | d k rm t d | |  j   f   n  d S(   s#   Rename chain *chain* to *new_name*.i   s   can't rename chain %s: %sN(	   R~   R   R   R  RY   R|   R  R   R  (   R+   RL  R  R  (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyR    s    	c         C   sh   t  | t  r | j } n  |  j j | j   |  j  } | d k rd t d | |  j   f   n  d S(   s   Flush all rules from *chain*.i   s   can't flush chain %s: %sN(	   R~   R   R   R  RZ   R|   R  R   R  (   R+   RL  R  (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyR    s    	c         C   sh   t  | t  r | j } n  |  j j | j   |  j  } | d k rd t d | |  j   f   n  d S(   s-   Zero the packet and byte counters of *chain*.i   s    can't zero chain %s counters: %sN(	   R~   R   R   R  R[   R|   R  R   R  (   R+   RL  R  (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyR    s    c         C   s   t  | t  r | j } n  t  | t  r6 | j } n  | rq t   } | d | _ | d | _ t j |  } n d } |  j
 j | j   | j   | |  j  } | d k r t d | | |  j   f   n  d S(   si   Set the policy of *chain* to *policy*, and also update chain
        counters if *counters* is specified.i    i   s#   can't set policy %s on chain %s: %sN(   R~   R   R   R   R   R  R  R   R   RP   R  R]   R|   R  R   R  (   R+   RL  R  RB   t   cntrsR  (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyR    s    	c         C   s   t  | t  r | j } n  |  j |  s. d St   } |  j j | j   t	 j
 |  |  j  j   } | s t d | |  j   f   n  t |  | j | j f f S(   s*   Returns the policy of *chain* as a string.s    can't get policy on chain %s: %sN(   NN(   R~   R   R   R  RP   R   R  R\   R|   R   R   R  R   R   R  R   R  R  (   R+   RL  R  t   pol(    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyR    s    	c         C   s_   |  j  j | j   t j | t j  |  j  } | d k r[ t d | |  j   f   n  d S(   s    Appends rule *entry* to *chain*.i   s"   can't append entry to chain %s: %sN(	   R  Rc   R|   R   R   RQ   R  R   R  (   R+   RL  R   R  (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyR    s    c         C   sb   |  j  j | j   t j | t j  | |  j  } | d k r^ t d | |  j   f   n  d S(   s9   Inserts rule *entry* into *chain* at position *position*.i   s$   can't insert entry into chain %s: %sN(	   R  Ra   R|   R   R   RQ   R  R   R  (   R+   RL  R   R  R  (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyR    s    c         C   sb   |  j  j | j   t j | t j  | |  j  } | d k r^ t d | |  j   f   n  d S(   sA   Replace existing rule in *chain* at *position* with given *rule*.i   s#   can't replace entry in chain %s: %sN(	   R  Rb   R|   R   R   RQ   R  R   R  (   R+   RL  R   R  R  (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyR    s    c         C   sb   |  j  j | j   t j | t j  | |  j  } | d k r^ t d | |  j   f   n  d S(   s.   Removes rule *entry* with *mask* from *chain*.i   s$   can't delete entry from chain %s: %sN(	   R  Rd   R|   R   R   RQ   R  R   R  (   R+   RL  R   Rv  R  (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyR    s    c         C   s4   |  j  j | j   |  j  } | r, | d S| Sd S(   s;   Returns the first rule in *chain* or *None* if it is empty.i    N(   R  R^   R|   R  (   R+   RL  R   (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyR    s    c         C   s7   |  j  j t j |  |  j  } | r/ | d S| Sd S(   s(   Returns the next rule after *prev_rule*.i    N(   R  R_   R   R   R  (   R+   t	   prev_ruleR   (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyR    s    !c         C   s%   |  j  j t j |  |  j  } | S(   s'   Returns the standard target in *entry*.(   R  R`   R   R   R  (   R+   R   R  (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyR    s    !c         C   sc   g  } |  j  j |  j  } xA | r^ | j   } | j t |  |   |  j  j |  j  } q W| S(   N(   R  RT   R  R   R   R   RU   (   R+   t   chainsRL  (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyt   _get_chains  s    	c         C   sU   x |  j  D] } | j   q
 Wx0 |  j  D]% } |  j |  s( |  j |  q( q( Wd S(   s2   Flush and delete all non-builtin chains the table.N(   R  R   R  R  (   R+   RL  (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyR     s
    c         C   s   t  | |  S(   N(   R   (   R+   R   RL  (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyR    s    N(+   R   R   R   t   FILTERt   MANGLEt   RAWt   NATt   SECURITYRQ  t   dictR#  RP   R%  R  R   R   R  R   R   R  R  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/ip4tc.pyR
     sJ   											






						(=   R   R   R   R   t   ctypesR   R]  R+  R(  t   utilR    R   R   R   R   R   R   R   R   R	   t   __all__t   hasattrt   setattrR.   t   CDLLt   _libct   __errno_locationR  RJ   RK   RL   t   mallocR   RE   t   c_size_tt   argtypest   freeR   RP   R   t	   StructureR   R   R<   R   R   RH   Rt   R   RG   Rk   R   R   R   R   R.  R1  R   R   R  R
   (    (    (    s3   /home/vilmos/Projects/python-iptables/iptc/ip4tc.pyt   <module>   s\   :				
		"		 {	
