mò
¼v'Bc           @   s/  d  Z  d Z d Z y e Wn d \ Z Z n Xy| d k Z d k Z d k Z d k Z d k	 Z	 e i
 i e i i e	 i e i ƒ  ƒ d d d g ƒ ƒ d	 k l Z l Z Wn1 y e i i d
 e ƒ Wn e GHn X‚  n Xe i d i e i ƒ d Z e i d ƒ d Z e i ƒ  d Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z e  d j oÈ e i! d ƒ or d k" Z" d k# Z# e" i$ ƒ  e# i% ƒ  Z& e& i' d ƒ o e e( e) e e( e) ƒ ƒ q'e d d e d d ƒ ƒ q+e* e i ƒ d j o e e+ e i d ƒ ƒ GHq+e i i d ƒ n d S(   s€   calculate ipv6 address using Tony Hain's draft provider-independent method

   refer to draft-hain-ipv6-PI-addr-01.txt Oct. 2001s>  
    hain_address.py -- calculate provider-independent ipv6 address
    Copyright (C) 2005  John Comeau <jc@jcomeau.com>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
    s5   Not all needed libraries found, upgrade or check pathi   i    Ni   t   libt   python(   s   gpls	   jclicenses   %s
iÿÿÿÿt   .c          G   s   t  S(   N(   t   False(   t   whatever(    (    t   ./hain_address.pyt
   DebugPrint1   s    c          G   s0   x) t  |  d ƒ t i j o |  d }  q W|  S(   Ni    (   t   typet   argst   typest	   TupleType(   R   (    (    R   t   scalars4   s      c    	      G   s  t  d |  ƒ y t |  ƒ \ } } } Wn t |  ƒ \ } } d } n Xt d t | ƒ ƒ } t d t | ƒ ƒ } t  d | | ƒ | d } d d d	 >} t | | d
 ƒ } t t | ƒ | d
 ƒ } | d j  o | d d >O} n t  | | ƒ t  d | | f ƒ d | t | | ƒ f S(   s  main routine, calculates address from input lat and long

 quote from draft:
 Creating the Provider-Independent address prefix is accomplished by 
  dividing the WGS-84 latitude and longitude readings in degrees (for 
  latitudes between +/- 15 degrees measured to 5 places right of the 
  decimal ie: deg.xxxxx) by the incremental angle. For 22-bit values 
  the incremental angle is 0.0000858306884765625 degrees (360/(2^22)). 
  This will result in uniquely identifiable areas with 2096832 along 
  the latitude axis and 4193664 along the longitude axis. The specific 
  sequence for address formation is: 
  1. for east/west : convert all values to east (ie: 90w = 270e) 
  2. divide demarcation degree values by 0.0000858306884765625 
  3. convert each of the integers to 22-digit binary 
  4. for north/south (set MSB for south) 
  5. bit interleave latitude, longitude into 44-bit result  
  6. prepend FP xxxx (IANA assigned) to form 48-bit prefix
   (0001 is recommended to avoid breaking up any of
   the unassigned 3-bit spaces)s   args:t   1t   latitudet	   longitudes	   lat, lon:i    f360.0i   i   f0.5i   s   %x %xs   %s%011xN(   R   R   R   R   R   t   prefixt   canonicalizet   strt   latt   lont	   convertert   intt   eastwestt   abst
   northsoutht
   interleave(	   R   R   R   R   R   R   R   R   R   (    (    R   t   hainaddr:   s&     

 c          G   s$  t  |  ƒ }  |  \ } } t } t | | | ƒ y… | d d g j o t d | ‚ n^ | d j o* t i	 d ƒ i
 | ƒ p t d ‚ q² n' t i	 d ƒ i
 | ƒ p t d ‚ n Wn- t j
 o! } t d | i d	 ƒ d
 Sn X| d j o‹ | i ƒ  i d ƒ o | d	 d !t } } n/ | i ƒ  i d ƒ o | d	 d !t } } n t | ƒ } | o | d	 j o | } q n¦ | i ƒ  i d ƒ o | d	 d !t } } n/ | i ƒ  i d ƒ o | d	 d !t } } n t | ƒ } | o | d	 j o d | } n | d	 j  o d | } n | S(   NR   R   s   cannot grok direction %ss   [+-]?\d+(?:\.\d{0,})?[NSns]?$s   improper latitude formats   [+-]?\d+(?:\.\d{0,})?[EWew]?$s   improper longitude formats   ValueError: i    f0.0t   siÿÿÿÿt   nt   wt   eih  (   R   R   t	   directiont   valueR   t   adjustR   t
   ValueErrort   ret   compilet   matcht   instancet   lowert   endswitht   Truet   float(   R   R   R&   R    R!   (    (    R   R   b   sB    	   c          G   sã   t  |  ƒ }  y+ t |  d d ƒ t |  d d ƒ } } Wn t j
 o |  \ } } n Xd } xe t d d ƒ D]T } d | } | | d | >@d j O} | d K} | | d | >@d j O} | d K} qo W| d L} t
 d | ƒ | S(   sF   interleave binary digits of two numbers according to Tony Hain's drafti    i   i   i   i   s
   result: %xN(   R   R   R   t   at   bt	   TypeErrort   resultt   ranget   positiont   shiftR   (   R   R+   R,   R1   R.   R0   (    (    R   R   „   s"     + 


c          G   sF   t  |  ƒ \ } } } d | | | d d !| d d !| d d !f GHd S(   s9   use HTML form to make this script available as CGI scripts0  Content-type: text/html



  <FORM METHOD="GET">
  Latitude: <INPUT NAME="lat" SIZE="10" VALUE="%s"> Example: 30.73N<BR>
  Longitude: <INPUT NAME="lon" SIZE="10" VALUE="%s"> Example: 117.18W<BR>
  IPv6 Address: <INPUT NAME="ignore" SIZE="16" VALUE="%s:%s:%s::">/48<BR>
  <INPUT TYPE="SUBMIT">
  </FORM>i    i   i   i   N(   R   R   R   R   t   ipv6addr(   R   R   R2   R   (    (    R   t   cgiform–   s     t   __main__t   SERVER_NAMER   i   s   Usage: %s LATITUDE LONGITUDE
(   i   i    (,   t   __doc__t	   Copyrightt   errormessageR)   R   t   syst   osR	   R#   t   pwdt   patht   appendt   sept   joint   getpwuidt   geteuidt   com.jcomeaut   gplt	   jclicenset   stderrt   writet   argvt   splitt   selft   commandt   originalselfR   R   R   R   R   R3   t   __name__t   getenvt   cgitbt   cgit   enablet   parset   cgivarst   has_keyR   R   t   lent   tuple(   R7   R   R3   R   R   RC   RI   R#   R)   RO   RR   RK   R   RN   R;   R	   R9   R   R8   R   RD   RJ   R:   (    (    R   t   ?   sH   -8				(	"		
