<s> """<STR_LIT>""" <EOL> from struct import unpack <EOL> from exabgp . protocol . ip import IP <EOL> from exabgp . protocol . ip import NoNextHop <EOL> from exabgp . protocol . family import AFI <EOL> from exabgp . protocol . family import SAFI <EOL> from exabgp . bgp . message import IN <EOL> from exabgp . bgp . message import OUT <EOL> from exabgp . bgp . message . update . nlri . nlri import NLRI <EOL> from exabgp . bgp . message . update . nlri . cidr import CIDR <EOL> from exabgp . bgp . message . update . nlri . qualifier import Labels <EOL> from exabgp . bgp . message . update . nlri . qualifier import PathInfo <EOL> from exabgp . bgp . message . update . nlri . qualifier import RouteDistinguisher <EOL> from exabgp . bgp . message . notification import Notify <EOL> @ NLRI . register ( AFI . ipv4 , SAFI . unicast ) <EOL> @ NLRI . register ( AFI . ipv6 , SAFI . unicast ) <EOL> @ NLRI . register ( AFI . ipv4 , SAFI . multicast ) <EOL> @ NLRI . register ( AFI . ipv6 , SAFI . multicast ) <EOL> class INET ( NLRI ) : <EOL> __slots__ = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> def __init__ ( self , afi , safi , action = OUT . UNSET ) : <EOL> NLRI . __init__ ( self , afi , safi , action ) <EOL> self . path_info = PathInfo . NOPATH <EOL> self . cidr = CIDR . NOCIDR <EOL> self . nexthop = NoNextHop <EOL> def __len__ ( self ) : <EOL> return len ( self . cidr ) + len ( self . path_info ) <EOL> def __repr__ ( self ) : <EOL> return self . extensive ( ) <EOL> def __eq__ ( self , other ) : <EOL> return NLRI . __eq__ ( self , other ) and self . cidr == other . cidr and self . path_info == other . path_info and self . nexthop == other . nexthop <EOL> def __ne__ ( self , other ) : <EOL> return not self . __eq__ ( other ) <EOL> def prefix ( self ) : <EOL> return "<STR_LIT>" % ( self . cidr . prefix ( ) , str ( self . path_info ) ) <EOL> def pack ( self , negotiated = None ) : <EOL> addpath = self . path_info . pack ( ) if negotiated and negotiated . addpath . send ( self . afi , self . safi ) else '<STR_LIT>' <EOL> return addpath + self . cidr . pack_nlri ( ) <EOL> def index ( self ) : <EOL> addpath = '<STR_LIT>' if self . path_info is PathInfo . NOPATH else self . path_info . pack ( ) <EOL> return addpath + self . cidr . pack_nlri ( ) <EOL> def extensive ( self ) : <EOL> return "<STR_LIT>" % ( self . prefix ( ) , '<STR_LIT>' if self . nexthop is NoNextHop else '<STR_LIT>' % self . nexthop ) <EOL> def _internal ( self , announced = True ) : <EOL> return [ self . path_info . json ( ) ] <EOL> def json ( self , announced = True ) : <EOL> return '<STR_LIT>' % ( self . cidr . prefix ( ) , "<STR_LIT:U+002CU+0020>" . join ( self . _internal ( announced ) ) ) <EOL> @ classmethod <EOL> def _pathinfo ( cls , data , addpath ) : <EOL> if addpath : <EOL> return PathInfo ( data [ : <NUM_LIT:4> ] ) , data [ <NUM_LIT:4> : ] <EOL> return PathInfo . NOPATH , data <EOL> @ classmethod <EOL> def unpack_nlri ( cls , afi , safi , bgp , action , addpath ) : <EOL> nlri = cls ( afi , safi , action ) <EOL> if addpath : <EOL> nlri . path_info = PathInfo ( bgp [ : <NUM_LIT:4> ] ) <EOL> bgp = bgp [ <NUM_LIT:4> : ] <EOL> mask = ord ( bgp [ <NUM_LIT:0> ] ) <EOL> bgp = bgp [ <NUM_LIT:1> : ] <EOL> if cls . has_label ( ) : <EOL> labels = [ ] <EOL> while bgp and mask >= <NUM_LIT:8> : <EOL> label = int ( unpack ( '<STR_LIT>' , chr ( <NUM_LIT:0> ) + bgp [ : <NUM_LIT:3> ] ) [ <NUM_LIT:0> ] ) <EOL> bgp = bgp [ <NUM_LIT:3> : ] <EOL> mask -= <NUM_LIT> <EOL> labels . append ( label >> <NUM_LIT:4> ) <EOL> if label == <NUM_LIT> and action == IN . WITHDRAWN : <EOL> break <EOL> if label == <NUM_LIT> : <EOL> break <EOL> if label & <NUM_LIT:1> : <EOL> break <EOL> nlri . labels = Labels ( labels ) <EOL> if cls . has_rd ( ) : <EOL> mask -= <NUM_LIT:8> * <NUM_LIT:8> <EOL> rd = bgp [ : <NUM_LIT:8> ] <EOL> bgp = bgp [ <NUM_LIT:8> : ] <EOL> nlri . rd = RouteDistinguisher ( rd ) <EOL> if mask < <NUM_LIT:0> : <EOL> raise Notify ( <NUM_LIT:3> , <NUM_LIT:10> , '<STR_LIT>' ) <EOL> if not bgp and mask : <EOL> raise Notify ( <NUM_LIT:3> , <NUM_LIT:10> , '<STR_LIT>' ) <EOL> size = CIDR . size ( mask ) <EOL> if len ( bgp ) < size : <EOL> raise Notify ( <NUM_LIT:3> , <NUM_LIT:10> , '<STR_LIT>' % ( afi , safi ) ) <EOL> network , bgp = bgp [ : size ] , bgp [ size : ] <EOL> padding = '<STR_LIT>' * ( IP . length ( afi ) - size ) <EOL> nlri . cidr = CIDR ( network + padding , mask ) <EOL> return nlri , bgp </s>
