<s> """<STR_LIT>""" <EOL> import os <EOL> import stat <EOL> from pprint import pformat <EOL> from copy import deepcopy <EOL> from bagpipe . exabgp . structure . ip import to_IP , to_Route <EOL> from bagpipe . exabgp . structure . asn import ASN <EOL> from bagpipe . exabgp . structure . neighbor import Neighbor <EOL> from bagpipe . exabgp . structure . protocol import NamedProtocol <EOL> from bagpipe . exabgp . structure . icmp import NamedICMPType , NamedICMPCode <EOL> from bagpipe . exabgp . structure . tcpflags import NamedTCPFlags <EOL> from bagpipe . exabgp . structure . fragments import NamedFragments <EOL> from bagpipe . exabgp . message . open import HoldTime , RouterID <EOL> from bagpipe . exabgp . message . update . flow import BinaryOperator , NumericOperator <EOL> from bagpipe . exabgp . message . update . flow import Flow , Source , Destination , SourcePort , DestinationPort , AnyPort , IPProtocol , TCPFlag , Fragment , PacketLength , ICMPType , ICMPCode , DSCP <EOL> from bagpipe . exabgp . message . update . attribute import AttributeID <EOL> from bagpipe . exabgp . message . update . attribute . origin import Origin <EOL> from bagpipe . exabgp . message . update . attribute . nexthop import NextHop <EOL> from bagpipe . exabgp . message . update . attribute . aspath import ASPath <EOL> from bagpipe . exabgp . message . update . attribute . med import MED <EOL> from bagpipe . exabgp . message . update . attribute . localpref import LocalPreference <EOL> from bagpipe . exabgp . message . update . attribute . communities import Community , Communities , ECommunity , ECommunities , to_ExtendedCommunity , to_FlowTrafficRate , to_RouteTargetCommunity_00 , to_RouteTargetCommunity_01 <EOL> from bagpipe . exabgp . log import Logger <EOL> logger = Logger ( ) <EOL> class Split ( int ) : <EOL> ID = AttributeID . INTERNAL_SPLIT <EOL> MULTIPLE = False <EOL> class Watchdog ( str ) : <EOL> ID = AttributeID . INTERNAL_WATCHDOG <EOL> MULTIPLE = False <EOL> class Withdrawn ( object ) : <EOL> ID = AttributeID . INTERNAL_WITHDRAWN <EOL> MULTIPLE = False <EOL> class Configuration ( object ) : <EOL> TTL_SECURITY = <NUM_LIT:255> <EOL> debug = os . environ . get ( '<STR_LIT>' , None ) != None <EOL> _str_route_error = '<STR_LIT>' '<STR_LIT>' '<STR_LIT:\n>' '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' '<STR_LIT:\n>' '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' <EOL> _str_flow_error = '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' <EOL> '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' <EOL> _str_process_error = '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' <EOL> def __init__ ( self , fname , text = False ) : <EOL> self . _text = text <EOL> self . _fname = fname <EOL> self . _clear ( ) <EOL> def _clear ( self ) : <EOL> self . process = { } <EOL> self . neighbor = { } <EOL> self . error = '<STR_LIT>' <EOL> self . _neighbor = { } <EOL> self . _scope = [ ] <EOL> self . _location = [ '<STR_LIT:root>' ] <EOL> self . _line = [ ] <EOL> self . _error = '<STR_LIT>' <EOL> self . _number = <NUM_LIT:1> <EOL> def reload ( self ) : <EOL> if self . _text : <EOL> self . _tokens = self . _tokenise ( self . _fname . split ( '<STR_LIT:\n>' ) ) <EOL> else : <EOL> try : <EOL> f = open ( self . _fname , '<STR_LIT:r>' ) <EOL> self . _tokens = self . _tokenise ( f ) <EOL> f . close ( ) <EOL> except IOError , e : <EOL> error = str ( e ) <EOL> if error . count ( '<STR_LIT:]>' ) : <EOL> self . error = error . split ( '<STR_LIT:]>' ) [ <NUM_LIT:1> ] . strip ( ) <EOL> else : <EOL> self . error = error <EOL> if self . debug : raise <EOL> return False <EOL> self . _clear ( ) <EOL> r = False <EOL> while not self . finished ( ) : <EOL> r = self . _dispatch ( self . _scope , '<STR_LIT>' , [ '<STR_LIT>' , '<STR_LIT>' ] , [ ] ) <EOL> if r is False : break <EOL> if r in [ True , None ] : <EOL> self . neighbor = self . _neighbor <EOL> return True <EOL> self . error = "<STR_LIT>" % ( self . _location [ - <NUM_LIT:1> ] , self . number ( ) , self . line ( ) , self . _error ) <EOL> return False <EOL> def parse_single_route ( self , command ) : <EOL> tokens = command . split ( '<STR_LIT:U+0020>' ) [ <NUM_LIT:1> : ] <EOL> if len ( tokens ) < <NUM_LIT:4> : <EOL> return False <EOL> if tokens [ <NUM_LIT:0> ] != '<STR_LIT>' : <EOL> return False <EOL> scope = [ { } ] <EOL> if not self . _single_static_route ( scope , tokens [ <NUM_LIT:1> : ] ) : <EOL> return None <EOL> return scope [ <NUM_LIT:0> ] [ '<STR_LIT>' ] [ <NUM_LIT:0> ] <EOL> def parse_single_flow ( self , command ) : <EOL> self . _tokens = self . _tokenise ( '<STR_LIT:U+0020>' . join ( command . split ( '<STR_LIT:U+0020>' ) [ <NUM_LIT:2> : ] ) . split ( '<STR_LIT>' ) ) <EOL> scope = [ { } ] <EOL> if not self . _dispatch ( scope , '<STR_LIT>' , [ '<STR_LIT>' , ] , [ ] ) : <EOL> return None <EOL> if not self . _check_flow_route ( scope ) : <EOL> return None <EOL> return scope [ <NUM_LIT:0> ] [ '<STR_LIT>' ] [ <NUM_LIT:0> ] <EOL> def add_route_all_peers ( self , route ) : <EOL> for neighbor in self . neighbor : <EOL> self . neighbor [ neighbor ] . add_route ( route ) <EOL> def remove_route_all_peers ( self , route ) : <EOL> result = False <EOL> for neighbor in self . neighbor : <EOL> if self . neighbor [ neighbor ] . remove_route ( route ) : <EOL> result = True <EOL> return result <EOL> def _tokenise ( self , text ) : <EOL> r = [ ] <EOL> config = '<STR_LIT>' <EOL> for line in text : <EOL> replaced = line . strip ( ) . replace ( '<STR_LIT:\t>' , '<STR_LIT:U+0020>' ) . replace ( '<STR_LIT:]>' , '<STR_LIT>' ) . replace ( '<STR_LIT:[>' , '<STR_LIT>' ) . lower ( ) <EOL> config += line <EOL> if not replaced : <EOL> continue <EOL> if replaced . startswith ( '<STR_LIT:#>' ) : <EOL> continue <EOL> if replaced [ : <NUM_LIT:3> ] == '<STR_LIT>' : <EOL> password = line . strip ( ) [ <NUM_LIT:3> : ] . strip ( ) <EOL> if password [ - <NUM_LIT:1> ] == '<STR_LIT:;>' : <EOL> password = password [ : - <NUM_LIT:1> ] <EOL> r . append ( [ '<STR_LIT>' , password , '<STR_LIT:;>' ] ) <EOL> else : <EOL> r . append ( [ t for t in replaced [ : - <NUM_LIT:1> ] . split ( '<STR_LIT:U+0020>' ) if t ] + [ replaced [ - <NUM_LIT:1> ] ] ) <EOL> logger . config ( config ) <EOL> return r <EOL> def tokens ( self ) : <EOL> self . _number += <NUM_LIT:1> <EOL> self . _line = self . _tokens . pop ( <NUM_LIT:0> ) <EOL> return self . _line <EOL> def number ( self ) : <EOL> return self . _number <EOL> def line ( self ) : <EOL> return '<STR_LIT:U+0020>' . join ( self . _line ) <EOL> def finished ( self ) : <EOL> return len ( self . _tokens ) == <NUM_LIT:0> <EOL> def _dispatch ( self , scope , name , multi , single ) : <EOL> try : <EOL> tokens = self . tokens ( ) <EOL> except IndexError : <EOL> self . _error = '<STR_LIT>' <EOL> if self . debug : raise <EOL> return False <EOL> logger . configuration ( '<STR_LIT>' % str ( tokens ) ) <EOL> logger . configuration ( '<STR_LIT>' % str ( multi ) ) <EOL> logger . configuration ( '<STR_LIT>' % str ( single ) ) <EOL> end = tokens [ - <NUM_LIT:1> ] <EOL> if multi and end == '<STR_LIT:{>' : <EOL> self . _location . append ( tokens [ <NUM_LIT:0> ] ) <EOL> return self . _multi_line ( scope , name , tokens [ : - <NUM_LIT:1> ] , multi ) <EOL> if single and end == '<STR_LIT:;>' : <EOL> return self . _single_line ( scope , name , tokens [ : - <NUM_LIT:1> ] , single ) <EOL> if end == '<STR_LIT:}>' : <EOL> if len ( self . _location ) == <NUM_LIT:1> : <EOL> self . _error = '<STR_LIT>' <EOL> return False <EOL> self . _location . pop ( - <NUM_LIT:1> ) <EOL> return None <EOL> return False <EOL> def _multi_line ( self , scope , name , tokens , valid ) : <EOL> command = tokens [ <NUM_LIT:0> ] <EOL> if valid and command not in valid : <EOL> self . _error = '<STR_LIT>' % command <EOL> return False <EOL> if name == '<STR_LIT>' : <EOL> if command == '<STR_LIT>' : <EOL> if self . _multi_neighbor ( scope , tokens [ <NUM_LIT:1> : ] ) : <EOL> return self . _make_neighbor ( scope ) <EOL> return False <EOL> if command == '<STR_LIT>' : <EOL> if len ( tokens ) != <NUM_LIT:2> : <EOL> self . _error = '<STR_LIT>' <EOL> return False <EOL> return self . _multi_group ( scope , tokens [ <NUM_LIT:1> ] ) <EOL> if name == '<STR_LIT>' : <EOL> if command == '<STR_LIT>' : <EOL> if self . _multi_neighbor ( scope , tokens [ <NUM_LIT:1> : ] ) : <EOL> return self . _make_neighbor ( scope ) <EOL> return False <EOL> if command == '<STR_LIT>' : return self . _multi_static ( scope , tokens [ <NUM_LIT:1> : ] ) <EOL> if command == '<STR_LIT>' : return self . _multi_flow ( scope , tokens [ <NUM_LIT:1> : ] ) <EOL> if command == '<STR_LIT>' : return self . _multi_process ( scope , tokens [ <NUM_LIT:1> : ] ) <EOL> if name == '<STR_LIT>' : <EOL> if command == '<STR_LIT>' : return self . _multi_static ( scope , tokens [ <NUM_LIT:1> : ] ) <EOL> if command == '<STR_LIT>' : return self . _multi_flow ( scope , tokens [ <NUM_LIT:1> : ] ) <EOL> if command == '<STR_LIT>' : return self . _multi_process ( scope , tokens [ <NUM_LIT:1> : ] ) <EOL> if name == '<STR_LIT>' : <EOL> if command == '<STR_LIT>' : <EOL> if self . _multi_static_route ( scope , tokens [ <NUM_LIT:1> : ] ) : <EOL> return self . _check_static_route ( scope ) <EOL> return False <EOL> if name == '<STR_LIT>' : <EOL> if command == '<STR_LIT>' : <EOL> if self . _multi_flow_route ( scope , tokens [ <NUM_LIT:1> : ] ) : <EOL> return self . _check_flow_route ( scope ) <EOL> return False <EOL> if name == '<STR_LIT>' : <EOL> if command == '<STR_LIT>' : <EOL> if self . _multi_match ( scope , tokens [ <NUM_LIT:1> : ] ) : <EOL> return True <EOL> return False <EOL> if command == '<STR_LIT>' : <EOL> if self . _multi_then ( scope , tokens [ <NUM_LIT:1> : ] ) : <EOL> return True <EOL> return False <EOL> return False <EOL> def _single_line ( self , scope , name , tokens , valid ) : <EOL> command = tokens [ <NUM_LIT:0> ] <EOL> if valid and command not in valid : <EOL> self . _error = '<STR_LIT>' % command <EOL> return False <EOL> if command == '<STR_LIT:description>' : return self . _set_description ( scope , tokens [ <NUM_LIT:1> : ] ) <EOL> if command == '<STR_LIT>' : return self . _set_router_id ( scope , '<STR_LIT>' , tokens [ <NUM_LIT:1> : ] ) <EOL> if command == '<STR_LIT>' : return self . _set_ip ( scope , '<STR_LIT>' , tokens [ <NUM_LIT:1> : ] ) <EOL> if command == '<STR_LIT>' : return self . _set_asn ( scope , '<STR_LIT>' , tokens [ <NUM_LIT:1> : ] ) <EOL> if command == '<STR_LIT>' : return self . _set_asn ( scope , '<STR_LIT>' , tokens [ <NUM_LIT:1> : ] ) <EOL> if command == '<STR_LIT>' : return self . _set_holdtime ( scope , '<STR_LIT>' , tokens [ <NUM_LIT:1> : ] ) <EOL> if command == '<STR_LIT>' : return self . _set_gracefulrestart ( scope , '<STR_LIT>' , tokens [ <NUM_LIT:1> : ] ) <EOL> if command == '<STR_LIT>' : return self . _set_md5 ( scope , '<STR_LIT>' , tokens [ <NUM_LIT:1> : ] ) <EOL> if command == '<STR_LIT>' : return self . _set_ttl ( scope , '<STR_LIT>' , tokens [ <NUM_LIT:1> : ] ) <EOL> if command == '<STR_LIT>' : return self . _set_multisession ( scope , '<STR_LIT>' , tokens [ <NUM_LIT:1> : ] ) <EOL> if command == '<STR_LIT>' : return self . _single_static_route ( scope , tokens [ <NUM_LIT:1> : ] ) <EOL> if command == '<STR_LIT>' : return self . _route_origin ( scope , tokens [ <NUM_LIT:1> : ] ) <EOL> if command == '<STR_LIT>' : return self . _route_aspath ( scope , tokens [ <NUM_LIT:1> : ] ) <EOL> if command == '<STR_LIT>' : return self . _route_aspath ( scope , tokens [ <NUM_LIT:1> : ] ) <EOL> if command == '<STR_LIT>' : return self . _route_med ( scope , tokens [ <NUM_LIT:1> : ] ) <EOL> if command == '<STR_LIT>' : return self . _route_next_hop ( scope , tokens [ <NUM_LIT:1> : ] ) <EOL> if command == '<STR_LIT>' : return self . _route_local_preference ( scope , tokens [ <NUM_LIT:1> : ] ) <EOL> if command == '<STR_LIT>' : return self . _route_community ( scope , tokens [ <NUM_LIT:1> : ] ) <EOL> if command == '<STR_LIT>' : return self . _route_split ( scope , tokens [ <NUM_LIT:1> : ] ) <EOL> if command == '<STR_LIT:label>' : return self . _route_label ( scope , tokens [ <NUM_LIT:1> : ] ) <EOL> if command == '<STR_LIT>' : return self . _route_watchdog ( scope , tokens [ <NUM_LIT:1> : ] ) <EOL> if command == '<STR_LIT>' : return self . _route_withdrawn ( scope , tokens [ <NUM_LIT:1> : ] ) <EOL> if command == '<STR_LIT:source>' : return self . _flow_source ( scope , tokens [ <NUM_LIT:1> : ] ) <EOL> if command == '<STR_LIT>' : return self . _flow_destination ( scope , tokens [ <NUM_LIT:1> : ] ) <EOL> if command == '<STR_LIT:port>' : return self . _flow_route_anyport ( scope , tokens [ <NUM_LIT:1> : ] ) <EOL> if command == '<STR_LIT>' : return self . _flow_route_source_port ( scope , tokens [ <NUM_LIT:1> : ] ) <EOL> if command == '<STR_LIT>' : return self . _flow_route_destination_port ( scope , tokens [ <NUM_LIT:1> : ] ) <EOL> if command == '<STR_LIT>' : return self . _flow_route_protocol ( scope , tokens [ <NUM_LIT:1> : ] ) <EOL> if command == '<STR_LIT>' : return self . _flow_route_tcp_flags ( scope , tokens [ <NUM_LIT:1> : ] ) <EOL> if command == '<STR_LIT>' : return self . _flow_route_icmp_type ( scope , tokens [ <NUM_LIT:1> : ] ) <EOL> if command == '<STR_LIT>' : return self . _flow_route_icmp_code ( scope , tokens [ <NUM_LIT:1> : ] ) <EOL> if command == '<STR_LIT>' : return self . _flow_route_fragment ( scope , tokens [ <NUM_LIT:1> : ] ) <EOL> if command == '<STR_LIT>' : return self . _flow_route_dscp ( scope , tokens [ <NUM_LIT:1> : ] ) <EOL> if command == '<STR_LIT>' : return self . _flow_route_packet_length ( scope , tokens [ <NUM_LIT:1> : ] ) <EOL> if command == '<STR_LIT>' : return self . _flow_route_discard ( scope , tokens [ <NUM_LIT:1> : ] ) <EOL> if command == '<STR_LIT>' : return self . _flow_route_rate_limit ( scope , tokens [ <NUM_LIT:1> : ] ) <EOL> if command == '<STR_LIT>' : return self . _flow_route_redirect ( scope , tokens [ <NUM_LIT:1> : ] ) <EOL> if command == '<STR_LIT>' : return self . _set_process_run ( scope , '<STR_LIT>' , tokens [ <NUM_LIT:1> : ] ) <EOL> if command == '<STR_LIT>' : return self . _set_process_parse_routes ( scope , '<STR_LIT>' , tokens [ <NUM_LIT:1> : ] ) <EOL> return False <EOL> def _multi_process ( self , scope , tokens ) : <EOL> if len ( tokens ) != <NUM_LIT:1> : <EOL> self . _error = self . _str_process_error <EOL> return False <EOL> while True : <EOL> r = self . _dispatch ( scope , '<STR_LIT>' , [ ] , [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> if r is False : return False <EOL> if r is None : break <EOL> self . process . setdefault ( tokens [ <NUM_LIT:0> ] , { } ) [ '<STR_LIT>' ] = scope [ - <NUM_LIT:1> ] . pop ( '<STR_LIT>' ) <EOL> self . process [ tokens [ <NUM_LIT:0> ] ] [ '<STR_LIT>' ] = scope [ - <NUM_LIT:1> ] . get ( '<STR_LIT>' , False ) <EOL> if '<STR_LIT>' in scope [ - <NUM_LIT:1> ] : <EOL> self . process [ tokens [ <NUM_LIT:0> ] ] [ '<STR_LIT>' ] = scope [ - <NUM_LIT:1> ] [ '<STR_LIT>' ] <EOL> else : <EOL> self . process [ tokens [ <NUM_LIT:0> ] ] [ '<STR_LIT>' ] = '<STR_LIT:*>' <EOL> return True <EOL> def _set_process_parse_routes ( self , scope , command , value ) : <EOL> scope [ - <NUM_LIT:1> ] [ command ] = True <EOL> return True <EOL> def _set_process_run ( self , scope , command , value ) : <EOL> line = '<STR_LIT:U+0020>' . join ( value ) . strip ( ) <EOL> if len ( line ) > <NUM_LIT:2> and line [ <NUM_LIT:0> ] == line [ - <NUM_LIT:1> ] and line [ <NUM_LIT:0> ] in [ '<STR_LIT:">' , "<STR_LIT:'>" ] : <EOL> line = line [ <NUM_LIT:1> : - <NUM_LIT:1> ] <EOL> if '<STR_LIT:U+0020>' in line : <EOL> prg , args = line . split ( '<STR_LIT:U+0020>' , <NUM_LIT:1> ) <EOL> else : <EOL> prg = line <EOL> args = '<STR_LIT>' <EOL> if not prg : <EOL> self . _error = '<STR_LIT>' <EOL> if self . debug : raise <EOL> return False <EOL> if prg [ <NUM_LIT:0> ] != '<STR_LIT:/>' : <EOL> if prg . startswith ( '<STR_LIT>' ) : <EOL> parts = prg . split ( '<STR_LIT:/>' ) <EOL> path = [ os . environ . get ( '<STR_LIT>' , '<STR_LIT>' ) , ] + parts [ <NUM_LIT:2> : ] <EOL> prg = os . path . join ( * path ) <EOL> else : <EOL> prg = os . path . join ( os . getcwd ( ) , prg ) <EOL> if not os . path . exists ( prg ) : <EOL> self . _error = '<STR_LIT>' % prg <EOL> if self . debug : raise <EOL> return False <EOL> s = os . stat ( prg ) <EOL> if stat . S_ISDIR ( s . st_mode ) : <EOL> self . _error = '<STR_LIT>' % prg <EOL> if self . debug : raise <EOL> return False <EOL> if s . st_mode & stat . S_ISUID : <EOL> self . _error = '<STR_LIT>' % prg <EOL> if self . debug : raise <EOL> return False <EOL> check = stat . S_IXOTH <EOL> if s . st_uid == os . getuid ( ) : <EOL> check |= stat . S_IXUSR <EOL> if s . st_gid == os . getgid ( ) : <EOL> check |= stat . S_IXGRP <EOL> if not check & s . st_mode : <EOL> self . _error = '<STR_LIT>' % prg <EOL> if self . debug : raise <EOL> return False <EOL> if args : <EOL> scope [ - <NUM_LIT:1> ] [ command ] = [ prg ] + args . split ( '<STR_LIT:U+0020>' ) <EOL> else : <EOL> scope [ - <NUM_LIT:1> ] [ command ] = [ prg , ] <EOL> return True <EOL> def _route_watchdog ( self , scope , tokens ) : <EOL> try : <EOL> scope [ - <NUM_LIT:1> ] [ '<STR_LIT>' ] [ - <NUM_LIT:1> ] . attributes . add ( Watchdog ( tokens . pop ( <NUM_LIT:0> ) ) ) <EOL> return True <EOL> except ValueError : <EOL> self . _error = self . _str_route_error <EOL> if self . debug : raise <EOL> return False <EOL> def _route_withdrawn ( self , scope , tokens ) : <EOL> try : <EOL> scope [ - <NUM_LIT:1> ] [ '<STR_LIT>' ] [ - <NUM_LIT:1> ] . attributes . add ( Withdrawn ( ) ) <EOL> return True <EOL> except ValueError : <EOL> self . _error = self . _str_route_error <EOL> if self . debug : raise <EOL> return False <EOL> def _multi_group ( self , scope , address ) : <EOL> scope . append ( { } ) <EOL> while True : <EOL> r = self . _dispatch ( scope , '<STR_LIT>' , [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , [ '<STR_LIT:description>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> if r is False : <EOL> return False <EOL> if r is None : <EOL> scope . pop ( - <NUM_LIT:1> ) <EOL> return True <EOL> def _make_neighbor ( self , scope ) : <EOL> if len ( scope ) > <NUM_LIT:1> : <EOL> for key , content in scope [ - <NUM_LIT:2> ] . iteritems ( ) : <EOL> if key not in scope [ - <NUM_LIT:1> ] : <EOL> scope [ - <NUM_LIT:1> ] [ key ] = deepcopy ( content ) <EOL> logger . configuration ( "<STR_LIT>" ) <EOL> for _key in scope [ - <NUM_LIT:1> ] . keys ( ) : <EOL> for _line in pformat ( scope [ - <NUM_LIT:1> ] [ _key ] , <NUM_LIT:3> , <NUM_LIT:3> , <NUM_LIT:3> ) . split ( '<STR_LIT:\n>' ) : <EOL> logger . configuration ( "<STR_LIT>" % ( _key , _line ) ) <EOL> logger . configuration ( "<STR_LIT:\n>" ) <EOL> neighbor = Neighbor ( ) <EOL> for local_scope in scope : <EOL> v = local_scope . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if v : neighbor . router_id = v <EOL> v = local_scope . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if v : neighbor . peer_address = v <EOL> v = local_scope . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if v : neighbor . local_address = v <EOL> v = local_scope . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if v : neighbor . local_as = v <EOL> v = local_scope . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if v : neighbor . peer_as = v <EOL> v = local_scope . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if v : neighbor . hold_time = v <EOL> neighbor . parse_routes = local_scope . get ( '<STR_LIT>' , False ) <EOL> v = local_scope . get ( '<STR_LIT>' , [ ] ) <EOL> for route in v : <EOL> neighbor . add_route ( route ) <EOL> local_scope = scope . pop ( - <NUM_LIT:1> ) <EOL> neighbor . description = local_scope . get ( '<STR_LIT:description>' , '<STR_LIT>' ) <EOL> neighbor . graceful_restart = local_scope . get ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> if neighbor . graceful_restart is None : <EOL> neighbor . graceful_restart = int ( neighbor . hold_time ) <EOL> neighbor . md5 = local_scope . get ( '<STR_LIT>' , None ) <EOL> neighbor . ttl = local_scope . get ( '<STR_LIT>' , None ) <EOL> neighbor . multisession = local_scope . get ( '<STR_LIT>' , False ) <EOL> missing = neighbor . missing ( ) <EOL> if missing : <EOL> self . _error = '<STR_LIT>' % missing <EOL> return False <EOL> if neighbor . local_address . afi != neighbor . peer_address . afi : <EOL> self . _error = '<STR_LIT>' <EOL> return False <EOL> if self . _neighbor . has_key ( neighbor . peer_address . ip ) : <EOL> self . _error = '<STR_LIT>' % neighbor . peer_address . ip <EOL> return False <EOL> if neighbor . multisession : <EOL> for family in neighbor . families ( ) : <EOL> afi , safi = family <EOL> family_n = deepcopy ( neighbor ) <EOL> for f in neighbor . families ( ) : <EOL> if f == family : <EOL> continue <EOL> family_n . remove_family ( f ) <EOL> self . _neighbor [ family_n . name ( ) ] = family_n <EOL> else : <EOL> self . _neighbor [ neighbor . name ( ) ] = neighbor <EOL> return True <EOL> def _multi_neighbor ( self , scope , tokens ) : <EOL> if len ( tokens ) != <NUM_LIT:1> : <EOL> self . _error = '<STR_LIT>' <EOL> return False <EOL> address = tokens [ <NUM_LIT:0> ] <EOL> scope . append ( { } ) <EOL> try : <EOL> scope [ - <NUM_LIT:1> ] [ '<STR_LIT>' ] = to_IP ( address ) <EOL> except : <EOL> self . _error = '<STR_LIT>' % address <EOL> if self . debug : raise <EOL> return False <EOL> while True : <EOL> r = self . _dispatch ( scope , '<STR_LIT>' , [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , [ '<STR_LIT:description>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> if r is False : return False <EOL> if r is None : return True <EOL> def _set_router_id ( self , scope , command , value ) : <EOL> try : <EOL> ip = RouterID ( value [ <NUM_LIT:0> ] ) <EOL> except ( IndexError , ValueError ) : <EOL> self . _error = '<STR_LIT>' % '<STR_LIT:U+0020>' . join ( value ) <EOL> if self . debug : raise <EOL> return False <EOL> scope [ - <NUM_LIT:1> ] [ command ] = ip <EOL> return True <EOL> def _set_description ( self , scope , tokens ) : <EOL> text = '<STR_LIT:U+0020>' . join ( tokens ) <EOL> if len ( text ) < <NUM_LIT:2> or text [ <NUM_LIT:0> ] != '<STR_LIT:">' or text [ - <NUM_LIT:1> ] != '<STR_LIT:">' or text [ <NUM_LIT:1> : - <NUM_LIT:1> ] . count ( '<STR_LIT:">' ) : <EOL> self . _error = '<STR_LIT>' <EOL> return False <EOL> scope [ - <NUM_LIT:1> ] [ '<STR_LIT:description>' ] = text [ <NUM_LIT:1> : - <NUM_LIT:1> ] <EOL> return True <EOL> def _newASN ( self , value ) : <EOL> if value . count ( '<STR_LIT:.>' ) : <EOL> high , low = value . split ( '<STR_LIT:.>' , <NUM_LIT:1> ) <EOL> asn = ( int ( high ) << <NUM_LIT:16> ) + int ( low ) <EOL> else : <EOL> asn = int ( value ) <EOL> return ASN ( asn ) <EOL> def _set_asn ( self , scope , command , value ) : <EOL> try : <EOL> scope [ - <NUM_LIT:1> ] [ command ] = self . _newASN ( value [ <NUM_LIT:0> ] ) <EOL> return True <EOL> except ValueError : <EOL> self . _error = '<STR_LIT>' % '<STR_LIT:U+0020>' . join ( value ) <EOL> if self . debug : raise <EOL> return False <EOL> def _set_ip ( self , scope , command , value ) : <EOL> try : <EOL> ip = to_IP ( value [ <NUM_LIT:0> ] ) <EOL> except ( IndexError , ValueError ) : <EOL> self . _error = '<STR_LIT>' % '<STR_LIT:U+0020>' . join ( value ) <EOL> if self . debug : raise <EOL> return False <EOL> scope [ - <NUM_LIT:1> ] [ command ] = ip <EOL> return True <EOL> def _set_holdtime ( self , scope , command , value ) : <EOL> try : <EOL> holdtime = HoldTime ( value [ <NUM_LIT:0> ] ) <EOL> if holdtime < <NUM_LIT:0> : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> if holdtime >= pow ( <NUM_LIT:2> , <NUM_LIT:16> ) : <EOL> raise ValueError ( '<STR_LIT>' % pow ( <NUM_LIT:2> , <NUM_LIT:16> ) ) <EOL> scope [ - <NUM_LIT:1> ] [ command ] = holdtime <EOL> return True <EOL> except ValueError : <EOL> self . _error = '<STR_LIT>' % '<STR_LIT:U+0020>' . join ( value ) <EOL> if self . debug : raise <EOL> return False <EOL> def _set_gracefulrestart ( self , scope , command , value ) : <EOL> if not len ( value ) : <EOL> scope [ - <NUM_LIT:1> ] [ command ] = None <EOL> return True <EOL> try : <EOL> grace = int ( value [ <NUM_LIT:0> ] ) <EOL> if grace < <NUM_LIT:0> : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> if grace >= pow ( <NUM_LIT:2> , <NUM_LIT:16> ) : <EOL> raise ValueError ( '<STR_LIT>' % pow ( <NUM_LIT:2> , <NUM_LIT:16> ) ) <EOL> scope [ - <NUM_LIT:1> ] [ command ] = grace <EOL> return True <EOL> except ValueError : <EOL> self . _error = '<STR_LIT>' % '<STR_LIT:U+0020>' . join ( value ) <EOL> if self . debug : raise <EOL> return False <EOL> return True <EOL> def _set_md5 ( self , scope , command , value ) : <EOL> md5 = value [ <NUM_LIT:0> ] <EOL> if len ( md5 ) > <NUM_LIT:2> and md5 [ <NUM_LIT:0> ] == md5 [ - <NUM_LIT:1> ] and md5 [ <NUM_LIT:0> ] in [ '<STR_LIT:">' , "<STR_LIT:'>" ] : <EOL> md5 = md5 [ <NUM_LIT:1> : - <NUM_LIT:1> ] <EOL> if len ( md5 ) > <NUM_LIT> : <EOL> self . _error = '<STR_LIT>' <EOL> if self . debug : raise <EOL> return False <EOL> if not md5 : <EOL> self . _error = '<STR_LIT>' <EOL> if self . debug : raise <EOL> return False <EOL> scope [ - <NUM_LIT:1> ] [ command ] = md5 <EOL> return True <EOL> def _set_ttl ( self , scope , command , value ) : <EOL> if not len ( value ) : <EOL> scope [ - <NUM_LIT:1> ] [ command ] = self . TTL_SECURITY <EOL> return True <EOL> try : <EOL> ttl = int ( value [ <NUM_LIT:0> ] ) <EOL> if ttl < <NUM_LIT:0> : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> if ttl >= <NUM_LIT:255> : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> scope [ - <NUM_LIT:1> ] [ command ] = ttl <EOL> return True <EOL> except ValueError : <EOL> self . _error = '<STR_LIT>' % '<STR_LIT:U+0020>' . join ( value ) <EOL> if self . debug : raise <EOL> return False <EOL> return True <EOL> def _set_multisession ( self , scope , command , value ) : <EOL> scope [ - <NUM_LIT:1> ] [ command ] = True <EOL> return True <EOL> def _multi_static ( self , scope , tokens ) : <EOL> if len ( tokens ) != <NUM_LIT:0> : <EOL> self . _error = '<STR_LIT>' <EOL> return False <EOL> while True : <EOL> r = self . _dispatch ( scope , '<STR_LIT>' , [ '<STR_LIT>' , ] , [ '<STR_LIT>' , ] ) <EOL> if r is False : return False <EOL> if r is None : return True <EOL> def _split_last_route ( self , scope ) : <EOL> route = scope [ - <NUM_LIT:1> ] [ '<STR_LIT>' ] [ - <NUM_LIT:1> ] <EOL> if not AttributeID . INTERNAL_SPLIT in route . attributes : <EOL> return True <EOL> mask = route . nlri . mask <EOL> split = route . attributes [ AttributeID . INTERNAL_SPLIT ] <EOL> if mask >= split : <EOL> return True <EOL> route = scope [ - <NUM_LIT:1> ] [ '<STR_LIT>' ] . pop ( - <NUM_LIT:1> ) <EOL> increment = pow ( <NUM_LIT:2> , ( len ( route . nlri ) * <NUM_LIT:8> ) - split ) <EOL> number = pow ( <NUM_LIT:2> , split - route . nlri . mask ) <EOL> ip = <NUM_LIT:0> <EOL> for c in route . nlri . raw : <EOL> ip = ip << <NUM_LIT:8> <EOL> ip += ord ( c ) <EOL> route . nlri . mask = split <EOL> for _ in range ( number ) : <EOL> r = deepcopy ( route ) <EOL> ipn = ip <EOL> i = '<STR_LIT>' <EOL> while ipn : <EOL> lower = ipn & <NUM_LIT> <EOL> ipn = ipn >> <NUM_LIT:8> <EOL> i = chr ( lower ) + i <EOL> r . nlri . update_raw ( i ) <EOL> ip += increment <EOL> scope [ - <NUM_LIT:1> ] [ '<STR_LIT>' ] . append ( r ) <EOL> del ( route ) <EOL> return True <EOL> def _insert_static_route ( self , scope , tokens ) : <EOL> try : <EOL> ip = tokens . pop ( <NUM_LIT:0> ) <EOL> except IndexError : <EOL> self . _error = self . _str_route_error <EOL> if self . debug : raise <EOL> return False <EOL> try : <EOL> ip , nm = ip . split ( '<STR_LIT:/>' ) <EOL> except ValueError : <EOL> nm = '<STR_LIT>' <EOL> try : <EOL> route = to_Route ( ip , nm ) <EOL> except ValueError : <EOL> self . _error = self . _str_route_error <EOL> if self . debug : raise <EOL> return False <EOL> if not scope [ - <NUM_LIT:1> ] . has_key ( '<STR_LIT>' ) : <EOL> scope [ - <NUM_LIT:1> ] [ '<STR_LIT>' ] = [ ] <EOL> scope [ - <NUM_LIT:1> ] [ '<STR_LIT>' ] . append ( route ) <EOL> return True <EOL> def pop_last_static_route ( self , scope ) : <EOL> route = scope [ - <NUM_LIT:1> ] [ '<STR_LIT>' ] [ - <NUM_LIT:1> ] <EOL> scope [ - <NUM_LIT:1> ] [ '<STR_LIT>' ] = scope [ - <NUM_LIT:1> ] [ '<STR_LIT>' ] [ : - <NUM_LIT:1> ] <EOL> return route <EOL> def remove_route ( self , route , scope ) : <EOL> for r in scope [ - <NUM_LIT:1> ] [ '<STR_LIT>' ] : <EOL> if r == route : <EOL> scope [ - <NUM_LIT:1> ] [ '<STR_LIT>' ] . remove ( r ) <EOL> return True <EOL> return False <EOL> def _check_static_route ( self , scope ) : <EOL> route = scope [ - <NUM_LIT:1> ] [ '<STR_LIT>' ] [ - <NUM_LIT:1> ] <EOL> if not route . attributes . has ( AttributeID . NEXT_HOP ) : <EOL> self . _error = '<STR_LIT>' <EOL> return False <EOL> return True <EOL> def _multi_static_route ( self , scope , tokens ) : <EOL> if len ( tokens ) != <NUM_LIT:1> : <EOL> self . _error = self . _str_route_error <EOL> return False <EOL> if not self . _insert_static_route ( scope , tokens ) : <EOL> return False <EOL> while True : <EOL> r = self . _dispatch ( scope , '<STR_LIT>' , [ ] , [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:label>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> if r is False : return False <EOL> if r is None : return self . _split_last_route ( scope ) <EOL> def _single_static_route ( self , scope , tokens ) : <EOL> if len ( tokens ) < <NUM_LIT:3> : <EOL> return False <EOL> if not self . _insert_static_route ( scope , tokens ) : <EOL> return False <EOL> if tokens . pop ( <NUM_LIT:0> ) != '<STR_LIT>' : <EOL> return False <EOL> if not self . _route_next_hop ( scope , tokens ) : <EOL> return False <EOL> while len ( tokens ) : <EOL> command = tokens . pop ( <NUM_LIT:0> ) <EOL> if command == '<STR_LIT>' : <EOL> if self . _route_withdrawn ( scope , tokens ) : <EOL> continue <EOL> return False <EOL> if len ( tokens ) < <NUM_LIT:1> : <EOL> return False <EOL> if command == '<STR_LIT>' : <EOL> if self . _route_origin ( scope , tokens ) : <EOL> continue <EOL> return False <EOL> if command == '<STR_LIT>' : <EOL> if self . _route_aspath ( scope , tokens ) : <EOL> continue <EOL> return False <EOL> if command == '<STR_LIT>' : <EOL> if self . _route_aspath ( scope , tokens ) : <EOL> continue <EOL> return False <EOL> if command == '<STR_LIT>' : <EOL> if self . _route_med ( scope , tokens ) : <EOL> continue <EOL> return False <EOL> if command == '<STR_LIT>' : <EOL> if self . _route_local_preference ( scope , tokens ) : <EOL> continue <EOL> return False <EOL> if command == '<STR_LIT>' : <EOL> if self . _route_community ( scope , tokens ) : <EOL> continue <EOL> return False <EOL> if command == '<STR_LIT>' : <EOL> if self . _route_extended_community ( scope , tokens ) : <EOL> continue <EOL> return False <EOL> if command == '<STR_LIT>' : <EOL> if self . _route_split ( scope , tokens ) : <EOL> continue <EOL> return False <EOL> if command == '<STR_LIT:label>' : <EOL> if self . _route_label ( scope , tokens ) : <EOL> continue <EOL> return False <EOL> if command == '<STR_LIT>' : <EOL> if self . _route_watchdog ( scope , tokens ) : <EOL> continue <EOL> return False <EOL> return False <EOL> return self . _split_last_route ( scope ) <EOL> def _route_next_hop ( self , scope , tokens ) : <EOL> try : <EOL> scope [ - <NUM_LIT:1> ] [ '<STR_LIT>' ] [ - <NUM_LIT:1> ] . attributes . add ( NextHop ( to_IP ( tokens . pop ( <NUM_LIT:0> ) ) ) ) <EOL> return True <EOL> except : <EOL> self . _error = self . _str_route_error <EOL> if self . debug : raise <EOL> return False <EOL> def _route_origin ( self , scope , tokens ) : <EOL> data = tokens . pop ( <NUM_LIT:0> ) . lower ( ) <EOL> if data == '<STR_LIT>' : <EOL> scope [ - <NUM_LIT:1> ] [ '<STR_LIT>' ] [ - <NUM_LIT:1> ] . attributes . add ( Origin ( <NUM_LIT> ) ) <EOL> return True <EOL> if data == '<STR_LIT>' : <EOL> scope [ - <NUM_LIT:1> ] [ '<STR_LIT>' ] [ - <NUM_LIT:1> ] . attributes . add ( Origin ( <NUM_LIT> ) ) <EOL> return True <EOL> if data == '<STR_LIT>' : <EOL> scope [ - <NUM_LIT:1> ] [ '<STR_LIT>' ] [ - <NUM_LIT:1> ] . attributes . add ( Origin ( <NUM_LIT> ) ) <EOL> return True <EOL> self . _error = self . _str_route_error <EOL> if self . debug : raise <EOL> return False <EOL> def _route_aspath ( self , scope , tokens ) : <EOL> aspath = ASPath ( ) <EOL> asn = tokens . pop ( <NUM_LIT:0> ) <EOL> try : <EOL> if asn == '<STR_LIT:[>' : <EOL> while True : <EOL> try : <EOL> asn = tokens . pop ( <NUM_LIT:0> ) <EOL> except IndexError : <EOL> self . _error = self . _str_route_error <EOL> if self . debug : raise <EOL> return False <EOL> if asn == '<STR_LIT:]>' : <EOL> break <EOL> aspath . add ( self . _newASN ( asn ) ) <EOL> else : <EOL> aspath . add ( self . _newASN ( asn ) ) <EOL> except ValueError : <EOL> self . _error = self . _str_route_error <EOL> if self . debug : raise <EOL> return False <EOL> scope [ - <NUM_LIT:1> ] [ '<STR_LIT>' ] [ - <NUM_LIT:1> ] . attributes . add ( aspath ) <EOL> return True <EOL> def _route_med ( self , scope , tokens ) : <EOL> try : <EOL> scope [ - <NUM_LIT:1> ] [ '<STR_LIT>' ] [ - <NUM_LIT:1> ] . attributes . add ( MED ( int ( tokens . pop ( <NUM_LIT:0> ) ) ) ) <EOL> return True <EOL> except ValueError : <EOL> self . _error = self . _str_route_error <EOL> if self . debug : raise <EOL> return False <EOL> def _route_local_preference ( self , scope , tokens ) : <EOL> try : <EOL> scope [ - <NUM_LIT:1> ] [ '<STR_LIT>' ] [ - <NUM_LIT:1> ] . attributes . add ( LocalPreference ( int ( tokens . pop ( <NUM_LIT:0> ) ) ) ) <EOL> return True <EOL> except ValueError : <EOL> self . _error = self . _str_route_error <EOL> if self . debug : raise <EOL> return False <EOL> def _parse_community ( self , scope , data ) : <EOL> separator = data . find ( '<STR_LIT::>' ) <EOL> if separator > <NUM_LIT:0> : <EOL> prefix = int ( data [ : separator ] ) <EOL> suffix = int ( data [ separator + <NUM_LIT:1> : ] ) <EOL> if prefix >= pow ( <NUM_LIT:2> , <NUM_LIT:16> ) : <EOL> raise ValueError ( '<STR_LIT>' % data ) <EOL> if suffix >= pow ( <NUM_LIT:2> , <NUM_LIT:16> ) : <EOL> raise ValueError ( '<STR_LIT>' % data ) <EOL> return Community ( ( prefix << <NUM_LIT:16> ) + suffix ) <EOL> elif len ( data ) >= <NUM_LIT:2> and data [ <NUM_LIT:1> ] in '<STR_LIT>' : <EOL> value = long ( data , <NUM_LIT:16> ) <EOL> if value >= pow ( <NUM_LIT:2> , <NUM_LIT:32> ) : <EOL> raise ValueError ( '<STR_LIT>' % data ) <EOL> return Community ( value ) <EOL> else : <EOL> low = data . lower ( ) <EOL> if low == '<STR_LIT>' : <EOL> data = <NUM_LIT> <EOL> elif low == '<STR_LIT>' : <EOL> data = <NUM_LIT> <EOL> elif low == '<STR_LIT>' : <EOL> data = <NUM_LIT> <EOL> value = long ( data ) <EOL> if value >= pow ( <NUM_LIT:2> , <NUM_LIT:32> ) : <EOL> raise ValueError ( '<STR_LIT>' % data ) <EOL> return Community ( value ) <EOL> def _route_community ( self , scope , tokens ) : <EOL> communities = Communities ( ) <EOL> community = tokens . pop ( <NUM_LIT:0> ) <EOL> try : <EOL> if community == '<STR_LIT:[>' : <EOL> while True : <EOL> try : <EOL> community = tokens . pop ( <NUM_LIT:0> ) <EOL> except IndexError : <EOL> self . _error = self . _str_route_error <EOL> if self . debug : raise <EOL> return False <EOL> if community == '<STR_LIT:]>' : <EOL> break <EOL> communities . add ( self . _parse_community ( scope , community ) ) <EOL> else : <EOL> communities . add ( self . _parse_community ( scope , community ) ) <EOL> except ValueError : <EOL> self . _error = self . _str_route_error <EOL> if self . debug : raise <EOL> return False <EOL> scope [ - <NUM_LIT:1> ] [ '<STR_LIT>' ] [ - <NUM_LIT:1> ] . attributes . add ( communities ) <EOL> return True <EOL> def _parse_extended_community ( self , scope , data ) : <EOL> if data [ : <NUM_LIT:2> ] . lower ( ) == '<STR_LIT>' : <EOL> try : <EOL> raw = '<STR_LIT>' <EOL> for i in range ( <NUM_LIT:2> , len ( data ) , <NUM_LIT:2> ) : <EOL> raw += chr ( int ( data [ i : i + <NUM_LIT:2> ] , <NUM_LIT:16> ) ) <EOL> except ValueError : <EOL> raise ValueError ( '<STR_LIT>' % data ) <EOL> if len ( raw ) != <NUM_LIT:8> : <EOL> raise ValueError ( '<STR_LIT>' % data ) <EOL> return ECommunity ( raw ) <EOL> elif data . count ( '<STR_LIT::>' ) : <EOL> return to_ExtendedCommunity ( data ) <EOL> else : <EOL> raise ValueError ( '<STR_LIT>' % data ) <EOL> def _route_extended_community ( self , scope , tokens ) : <EOL> extended_communities = ECommunities ( ) <EOL> extended_community = tokens . pop ( <NUM_LIT:0> ) <EOL> try : <EOL> if extended_community == '<STR_LIT:[>' : <EOL> while True : <EOL> try : <EOL> extended_community = tokens . pop ( <NUM_LIT:0> ) <EOL> except IndexError : <EOL> self . _error = self . _str_route_error <EOL> if self . debug : raise <EOL> return False <EOL> if extended_community == '<STR_LIT:]>' : <EOL> break <EOL> extended_communities . add ( self . _parse_extended_community ( scope , extended_community ) ) <EOL> else : <EOL> extended_communities . add ( self . _parse_extended_community ( scope , extended_community ) ) <EOL> except ValueError : <EOL> self . _error = self . _str_route_error <EOL> if self . debug : raise <EOL> return False <EOL> scope [ - <NUM_LIT:1> ] [ '<STR_LIT>' ] [ - <NUM_LIT:1> ] . attributes . add ( extended_communities ) <EOL> return True <EOL> def _route_split ( self , scope , tokens ) : <EOL> try : <EOL> size = tokens . pop ( <NUM_LIT:0> ) <EOL> if not size or size [ <NUM_LIT:0> ] != '<STR_LIT:/>' : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> scope [ - <NUM_LIT:1> ] [ '<STR_LIT>' ] [ - <NUM_LIT:1> ] . attributes . add ( Split ( int ( size [ <NUM_LIT:1> : ] ) ) ) <EOL> return True <EOL> except ValueError : <EOL> self . _error = self . _str_route_error <EOL> if self . debug : raise <EOL> return False <EOL> def _route_label ( self , scope , tokens ) : <EOL> communities = Communities ( ) <EOL> community = tokens . pop ( <NUM_LIT:0> ) <EOL> try : <EOL> if community == '<STR_LIT:[>' : <EOL> while True : <EOL> try : <EOL> community = tokens . pop ( <NUM_LIT:0> ) <EOL> except IndexError : <EOL> self . _error = self . _str_route_error <EOL> if self . debug : raise <EOL> return False <EOL> if community == '<STR_LIT:]>' : <EOL> break <EOL> communities . add ( self . _parse_community ( community ) ) <EOL> else : <EOL> communities . add ( self . _parse_community ( community ) ) <EOL> except ValueError : <EOL> self . _error = self . _str_route_error <EOL> if self . debug : raise <EOL> return False <EOL> scope [ - <NUM_LIT:1> ] [ '<STR_LIT>' ] [ - <NUM_LIT:1> ] . attributes . add ( communities ) <EOL> return True <EOL> def _multi_flow ( self , scope , tokens ) : <EOL> if len ( tokens ) != <NUM_LIT:0> : <EOL> self . _error = self . _str_flow_error <EOL> return False <EOL> while True : <EOL> r = self . _dispatch ( scope , '<STR_LIT>' , [ '<STR_LIT>' , ] , [ ] ) <EOL> if r is False : return False <EOL> if r is None : break <EOL> return True <EOL> def _insert_flow_route ( self , scope , tokens = None ) : <EOL> try : <EOL> flow = Flow ( ) <EOL> except ValueError : <EOL> self . _error = self . _str_flow_error <EOL> if self . debug : raise <EOL> return False <EOL> if not scope [ - <NUM_LIT:1> ] . has_key ( '<STR_LIT>' ) : <EOL> scope [ - <NUM_LIT:1> ] [ '<STR_LIT>' ] = [ ] <EOL> scope [ - <NUM_LIT:1> ] [ '<STR_LIT>' ] . append ( flow ) <EOL> return True <EOL> def _check_flow_route ( self , scope ) : <EOL> logger . configuration ( '<STR_LIT>' ) <EOL> return True <EOL> def _multi_flow_route ( self , scope , tokens ) : <EOL> if len ( tokens ) > <NUM_LIT:1> : <EOL> self . _error = self . _str_flow_error <EOL> return False <EOL> if not self . _insert_flow_route ( scope ) : <EOL> return False <EOL> r = self . _dispatch ( scope , '<STR_LIT>' , [ '<STR_LIT>' , ] , [ ] ) <EOL> if r is False : return False <EOL> r = self . _dispatch ( scope , '<STR_LIT>' , [ '<STR_LIT>' , ] , [ ] ) <EOL> return r <EOL> def _multi_match ( self , scope , tokens ) : <EOL> if len ( tokens ) != <NUM_LIT:0> : <EOL> self . _error = self . _str_flow_error <EOL> return False <EOL> while True : <EOL> r = self . _dispatch ( scope , '<STR_LIT>' , [ ] , [ '<STR_LIT:source>' , '<STR_LIT>' , '<STR_LIT:port>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> if r is False : return False <EOL> if r is None : break <EOL> return True <EOL> def _multi_then ( self , scope , tokens ) : <EOL> if len ( tokens ) != <NUM_LIT:0> : <EOL> self . _error = self . _str_flow_error <EOL> return False <EOL> while True : <EOL> r = self . _dispatch ( scope , '<STR_LIT>' , [ ] , [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> if r is False : return False <EOL> if r is None : break <EOL> return True <EOL> def _flow_source ( self , scope , tokens ) : <EOL> try : <EOL> ip , nm = tokens . pop ( <NUM_LIT:0> ) . split ( '<STR_LIT:/>' ) <EOL> scope [ - <NUM_LIT:1> ] [ '<STR_LIT>' ] [ - <NUM_LIT:1> ] . add_and ( Source ( ip , nm ) ) <EOL> return True <EOL> except ValueError : <EOL> self . _error = self . _str_route_error <EOL> if self . debug : raise <EOL> return False <EOL> def _flow_destination ( self , scope , tokens ) : <EOL> try : <EOL> ip , nm = tokens . pop ( <NUM_LIT:0> ) . split ( '<STR_LIT:/>' ) <EOL> scope [ - <NUM_LIT:1> ] [ '<STR_LIT>' ] [ - <NUM_LIT:1> ] . add_and ( Destination ( ip , nm ) ) <EOL> return True <EOL> except ValueError : <EOL> self . _error = self . _str_route_error <EOL> if self . debug : raise <EOL> return False <EOL> def _operator ( self , string ) : <EOL> try : <EOL> if string [ <NUM_LIT:0> ] == '<STR_LIT:=>' : <EOL> return NumericOperator . EQ , string [ <NUM_LIT:1> : ] <EOL> elif string [ <NUM_LIT:0> ] == '<STR_LIT:>>' : <EOL> operator = NumericOperator . GT <EOL> elif string [ <NUM_LIT:0> ] == '<STR_LIT:<>' : <EOL> operator = NumericOperator . LT <EOL> else : <EOL> raise ValueError ( '<STR_LIT>' % string ) <EOL> if string [ <NUM_LIT:1> ] == '<STR_LIT:=>' : <EOL> operator += NumericOperator . EQ <EOL> return operator , string [ <NUM_LIT:2> : ] <EOL> else : <EOL> return operator , string [ <NUM_LIT:1> : ] <EOL> except IndexError : <EOL> raise ( '<STR_LIT>' % string ) <EOL> def _value ( self , string ) : <EOL> l = <NUM_LIT:0> <EOL> for c in string : <EOL> if c not in [ '<STR_LIT:&>' , ] : <EOL> l += <NUM_LIT:1> <EOL> continue <EOL> break <EOL> return string [ : l ] , string [ l : ] <EOL> def _flow_generic_expression ( self , scope , tokens , converter , klass ) : <EOL> try : <EOL> for test in tokens : <EOL> AND = BinaryOperator . NOP <EOL> while test : <EOL> operator , _ = self . _operator ( test ) <EOL> value , test = self . _value ( _ ) <EOL> try : <EOL> number = int ( value ) <EOL> except ValueError : <EOL> number = converter ( value ) <EOL> scope [ - <NUM_LIT:1> ] [ '<STR_LIT>' ] [ - <NUM_LIT:1> ] . add_or ( klass ( AND | operator , number ) ) <EOL> if test : <EOL> if test [ <NUM_LIT:0> ] == '<STR_LIT:&>' : <EOL> AND = BinaryOperator . AND <EOL> test = test [ <NUM_LIT:1> : ] <EOL> if not test : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" % test [ <NUM_LIT:0> ] ) <EOL> return True <EOL> except ValueError : <EOL> self . _error = self . _str_route_error <EOL> if self . debug : raise <EOL> return False <EOL> def _flow_generic_list ( self , scope , tokens , converter , klass ) : <EOL> name = tokens . pop ( <NUM_LIT:0> ) <EOL> AND = BinaryOperator . NOP <EOL> try : <EOL> if name == '<STR_LIT:[>' : <EOL> while True : <EOL> name = tokens . pop ( <NUM_LIT:0> ) <EOL> if name == '<STR_LIT:]>' : <EOL> break <EOL> try : <EOL> try : <EOL> number = int ( name ) <EOL> except ValueError : <EOL> number = converter ( name ) <EOL> scope [ - <NUM_LIT:1> ] [ '<STR_LIT>' ] [ - <NUM_LIT:1> ] . add_or ( klass ( NumericOperator . EQ | AND , number ) ) <EOL> except IndexError : <EOL> self . _error = self . _str_flow_error <EOL> if self . debug : raise <EOL> return False <EOL> else : <EOL> try : <EOL> number = int ( name ) <EOL> except ValueError : <EOL> number = converter ( name ) <EOL> scope [ - <NUM_LIT:1> ] [ '<STR_LIT>' ] [ - <NUM_LIT:1> ] . add_or ( klass ( NumericOperator . EQ | AND , number ) ) <EOL> except ValueError : <EOL> self . _error = self . _str_flow_error <EOL> if self . debug : raise <EOL> return False <EOL> return True <EOL> def _flow_generic_condition ( self , scope , tokens , converter , klass ) : <EOL> if tokens [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] in [ '<STR_LIT:=>' , '<STR_LIT:>>' , '<STR_LIT:<>' ] : <EOL> return self . _flow_generic_expression ( scope , tokens , converter , klass ) <EOL> return self . _flow_generic_list ( scope , tokens , converter , klass ) <EOL> def _flow_route_anyport ( self , scope , tokens ) : <EOL> return self . _flow_generic_condition ( scope , tokens , int , AnyPort ) <EOL> def _flow_route_source_port ( self , scope , tokens ) : <EOL> return self . _flow_generic_condition ( scope , tokens , int , SourcePort ) <EOL> def _flow_route_destination_port ( self , scope , tokens ) : <EOL> return self . _flow_generic_condition ( scope , tokens , int , DestinationPort ) <EOL> def _flow_route_packet_length ( self , scope , tokens ) : <EOL> return self . _flow_generic_condition ( scope , tokens , int , PacketLength ) <EOL> def _flow_route_tcp_flags ( self , scope , tokens ) : <EOL> return self . _flow_generic_list ( scope , tokens , NamedTCPFlags , TCPFlag ) <EOL> def _flow_route_protocol ( self , scope , tokens ) : <EOL> return self . _flow_generic_list ( scope , tokens , NamedProtocol , IPProtocol ) <EOL> def _flow_route_icmp_type ( self , scope , tokens ) : <EOL> return self . _flow_generic_list ( scope , tokens , NamedICMPType , ICMPType ) <EOL> def _flow_route_icmp_code ( self , scope , tokens ) : <EOL> return self . _flow_generic_list ( scope , tokens , NamedICMPCode , ICMPCode ) <EOL> def _flow_route_fragment ( self , scope , tokens ) : <EOL> return self . _flow_generic_list ( scope , tokens , NamedFragments , Fragment ) <EOL> def _flow_route_dscp ( self , scope , tokens ) : <EOL> return self . _flow_generic_condition ( scope , tokens , int , DSCP ) <EOL> def _flow_route_discard ( self , scope , tokens ) : <EOL> try : <EOL> scope [ - <NUM_LIT:1> ] [ '<STR_LIT>' ] [ - <NUM_LIT:1> ] . add_action ( to_FlowTrafficRate ( ASN ( <NUM_LIT:0> ) , <NUM_LIT:0> ) ) <EOL> return True <EOL> except ValueError : <EOL> self . _error = self . _str_route_error <EOL> if self . debug : raise <EOL> return False <EOL> def _flow_route_rate_limit ( self , scope , tokens ) : <EOL> try : <EOL> speed = int ( tokens [ <NUM_LIT:0> ] ) <EOL> if speed < <NUM_LIT> and speed != <NUM_LIT:0> : <EOL> logger . warning ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> if speed > <NUM_LIT> : <EOL> speed = <NUM_LIT> <EOL> logger . warning ( "<STR_LIT>" % tokens [ <NUM_LIT:0> ] , "<STR_LIT>" ) <EOL> scope [ - <NUM_LIT:1> ] [ '<STR_LIT>' ] [ - <NUM_LIT:1> ] . add_action ( to_FlowTrafficRate ( ASN ( <NUM_LIT:0> ) , speed ) ) <EOL> return True <EOL> except ValueError : <EOL> self . _error = self . _str_route_error <EOL> if self . debug : raise <EOL> return False <EOL> def _flow_route_redirect ( self , scope , tokens ) : <EOL> try : <EOL> prefix , suffix = tokens [ <NUM_LIT:0> ] . split ( '<STR_LIT::>' , <NUM_LIT:1> ) <EOL> if prefix . count ( '<STR_LIT:.>' ) : <EOL> ip = prefix . split ( '<STR_LIT:.>' ) <EOL> if len ( ip ) != <NUM_LIT:4> : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> ipn = <NUM_LIT:0> <EOL> while ip : <EOL> ipn <<= <NUM_LIT:8> <EOL> ipn += int ( ip . pop ( <NUM_LIT:0> ) ) <EOL> number = int ( suffix ) <EOL> scope [ - <NUM_LIT:1> ] [ '<STR_LIT>' ] [ - <NUM_LIT:1> ] . add_action ( to_RouteTargetCommunity_01 ( ipn , number ) ) <EOL> return True <EOL> else : <EOL> asn = int ( prefix ) <EOL> route_target = int ( suffix ) <EOL> if asn >= pow ( <NUM_LIT:2> , <NUM_LIT:16> ) : <EOL> asn = asn & <NUM_LIT> <EOL> if route_target >= pow ( <NUM_LIT:2> , <NUM_LIT:32> ) : <EOL> raise ValueError ( '<STR_LIT>' % route_target ) <EOL> scope [ - <NUM_LIT:1> ] [ '<STR_LIT>' ] [ - <NUM_LIT:1> ] . add_action ( to_RouteTargetCommunity_00 ( asn , route_target ) ) <EOL> return True <EOL> except ValueError : <EOL> self . _error = self . _str_route_error <EOL> if self . debug : raise <EOL> return False </s>
