diff options
author | ISHIDA Wataru <ishida.wataru@lab.ntt.co.jp> | 2014-05-06 09:57:39 +0000 |
---|---|---|
committer | FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp> | 2014-05-10 01:14:57 +0900 |
commit | abafab91493f40be60d3fe0e580ee0f01709f09f (patch) | |
tree | 71c1e6a2c376815ca0a4f96191f971bdff90ebf5 | |
parent | b65d6dc315490507a4d5a99e8afb94535e4687f8 (diff) |
packet/bgp: bug fix of route target nlri class
Signed-off-by: ISHIDA Wataru <ishida.wataru@lab.ntt.co.jp>
Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
-rw-r--r-- | ryu/lib/packet/bgp.py | 49 |
1 files changed, 43 insertions, 6 deletions
diff --git a/ryu/lib/packet/bgp.py b/ryu/lib/packet/bgp.py index a3c852c7..6882770c 100644 --- a/ryu/lib/packet/bgp.py +++ b/ryu/lib/packet/bgp.py @@ -790,22 +790,59 @@ class RouteTargetMembershipNLRI(StringifyMixin): def __init__(self, origin_as, route_target): # If given is not default_as and default_rt - if not (origin_as is RtNlri.DEFAULT_AS and - route_target is RtNlri.DEFAULT_RT): + if not (origin_as is self.DEFAULT_AS and + route_target is self.DEFAULT_RT): # We validate them - if (not is_valid_old_asn(origin_as) or - not is_valid_ext_comm_attr(route_target)): + if (not self._is_valid_old_asn(origin_as) or + not self._is_valid_ext_comm_attr(route_target)): raise ValueError('Invalid params.') self.origin_as = origin_as self.route_target = route_target + def _is_valid_old_asn(self, asn): + """Returns true if given asn is a 16 bit number. + + Old AS numbers are 16 but unsigned number. + """ + valid = True + # AS number should be a 16 bit number + if (not isinstance(asn, (int, long)) or (asn < 0) or + (asn > ((2 ** 16) - 1))): + valid = False + + return valid + + def _is_valid_ext_comm_attr(self, attr): + """Validates *attr* as string representation of RT or SOO. + + Returns True if *attr* is as per our convention of RT or SOO, else + False. Our convention is to represent RT/SOO is a string with format: + *global_admin_part:local_admin_path* + """ + is_valid = True + + if not isinstance(attr, str): + is_valid = False + else: + first, second = attr.split(':') + try: + if '.' in first: + socket.inet_aton(first) + else: + int(first) + int(second) + except (ValueError, socket.error): + is_valid = False + + return is_valid + @property def formatted_nlri_str(self): return "%s:%s" % (self.origin_as, self.route_target) def is_default_rtnlri(self): - if (self._origin_as is RtNlri.DEFAULT_AS and - self._route_target is RtNlri.DEFAULT_RT): + if (self._origin_as is self.DEFAULT_AS and + self._route_target is self.DEFAULT_RT): return True return False |