<s> """<STR_LIT>""" <EOL> import math <EOL> import socket <EOL> from bagpipe . exabgp . structure . address import AFI , SAFI <EOL> from bagpipe . exabgp . message . update . route import Route <EOL> _bgp = { } <EOL> for netmask in range ( <NUM_LIT:0> , <NUM_LIT> ) : <EOL> _bgp [ netmask ] = int ( math . ceil ( float ( netmask ) / <NUM_LIT:8> ) ) <EOL> def _detect_afi ( ip ) : <EOL> if ip . count ( '<STR_LIT::>' ) : <EOL> return AFI . ipv6 <EOL> return AFI . ipv4 <EOL> def to_IP ( ip ) : <EOL> afi = _detect_afi ( ip ) <EOL> af = Inet . _af [ afi ] <EOL> network = socket . inet_pton ( af , ip ) <EOL> return Inet ( afi , network ) <EOL> def to_Route ( ip , mask ) : <EOL> afi = _detect_afi ( ip ) <EOL> return Route ( Prefix ( afi , ip , mask ) ) <EOL> class Inet ( object ) : <EOL> _UNICAST = SAFI ( SAFI . unicast ) <EOL> _MULTICAST = SAFI ( SAFI . multicast ) <EOL> _unicast_range = set ( range ( <NUM_LIT> , <NUM_LIT> ) ) <EOL> """<STR_LIT>""" <EOL> _af = { <EOL> AFI . ipv4 : socket . AF_INET , <EOL> AFI . ipv6 : socket . AF_INET6 , <EOL> } <EOL> _afi = { <EOL> socket . AF_INET : AFI . ipv4 , <EOL> socket . AF_INET6 : AFI . ipv6 , <EOL> } <EOL> _length = { <EOL> AFI . ipv4 : <NUM_LIT:4> , <EOL> AFI . ipv6 : <NUM_LIT:16> , <EOL> } <EOL> def __init__ ( self , afi , raw ) : <EOL> self . afi = AFI ( afi ) <EOL> self . raw = raw <EOL> self . __update ( ) <EOL> def __update ( self ) : <EOL> self . ip = self . _ip ( ) <EOL> if self . afi == AFI . ipv4 and int ( self . ip . split ( '<STR_LIT:.>' ) [ <NUM_LIT:0> ] ) in self . _unicast_range : <EOL> self . safi = self . _MULTICAST <EOL> else : <EOL> self . safi = self . _UNICAST <EOL> def update_raw ( self , raw ) : <EOL> self . raw = raw <EOL> self . __update ( ) <EOL> def pack ( self ) : <EOL> return self . raw <EOL> def _ip ( self ) : <EOL> try : <EOL> return socket . inet_ntop ( self . _af [ self . afi ] , self . raw ) <EOL> except socket . error : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> def __len__ ( self ) : <EOL> return len ( self . raw ) <EOL> def __str__ ( self ) : <EOL> return self . ip <EOL> def __repr__ ( self ) : <EOL> return str ( self ) <EOL> def __eq__ ( self , other ) : <EOL> return self . raw == other . raw and self . safi == other . safi <EOL> class _Prefix ( Inet ) : <EOL> def __init__ ( self , af , ip , mask ) : <EOL> self . mask = int ( mask ) <EOL> Inet . __init__ ( self , af , ip ) <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" % ( self . ip , self . mask ) <EOL> def __repr__ ( self ) : <EOL> return str ( self ) <EOL> def pack ( self ) : <EOL> return chr ( self . mask ) + self . raw [ : _bgp [ self . mask ] ] <EOL> def __eq__ ( self , other ) : <EOL> return isinstance ( other , _Prefix ) and self . pack ( ) == other . pack ( ) <EOL> def __hash__ ( self ) : <EOL> return hash ( self . pack ( ) ) <EOL> def __len__ ( self ) : <EOL> return _bgp [ self . mask ] + <NUM_LIT:1> <EOL> class BGPPrefix ( _Prefix ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , afi , bgp ) : <EOL> end = _bgp [ ord ( bgp [ <NUM_LIT:0> ] ) ] <EOL> _Prefix . __init__ ( self , afi , bgp [ <NUM_LIT:1> : end + <NUM_LIT:1> ] + '<STR_LIT>' * ( self . _length [ afi ] - end ) , ord ( bgp [ <NUM_LIT:0> ] ) ) <EOL> class AFIPrefix ( _Prefix ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , afi , network , mask ) : <EOL> _Prefix . __init__ ( self , afi , network , mask ) <EOL> class Prefix ( _Prefix ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , afi , ip , mask ) : <EOL> af = self . _af [ afi ] <EOL> network = socket . inet_pton ( af , ip ) <EOL> _Prefix . __init__ ( self , afi , network , mask ) </s>
