Skip to content

Commit

Permalink
add ipv6 unicast nlri encode and decode basic func
Browse files Browse the repository at this point in the history
Signed-off-by: Peng Xiao <xiaoquwl@gmail.com>
  • Loading branch information
xiaopeng163 committed Dec 24, 2015
1 parent 2abf86e commit 7ac61dd
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 0 deletions.
58 changes: 58 additions & 0 deletions yabgp/message/attribute/nlri/ipv6_unicast.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Copyright 2015 Cisco Systems, Inc.
# All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

""" IPv6 Unicast """

import struct
import binascii
import netaddr


class IPv6Unicast(object):

@staticmethod
def parse(nlri_data):
"""
decode IPv6 NLRI data
:param nlri_data: NLRI raw hex data
:return: return the results after decoding.
"""
nlri_list = []
while nlri_data:
prefix_bit_len = struct.unpack('!B', nlri_data[0])[0]
if prefix_bit_len % 8 == 0:
prefix_byte_len = prefix_bit_len / 8
else:
prefix_byte_len = prefix_bit_len / 8 + 1
prefix_addr = netaddr.IPAddress(int(binascii.b2a_hex(nlri_data[1:prefix_byte_len + 1]), 16)).__str__() \
+ '/%s' % prefix_bit_len
nlri_list.append(prefix_addr)
nlri_data = nlri_data[prefix_byte_len + 1:]

return nlri_list

@staticmethod
def construct(nlri_list):
"""
Construct NLRI from list to hex data
:param nlri_list:
:return:
"""
nlri_hex = b''
for prefix in nlri_list:
prefix = netaddr.IPNetwork(prefix)
nlri_hex += struct.pack('!B', prefix.prefixlen)
nlri_hex += binascii.unhexlify(hex(prefix.ip)[2:])
return nlri_hex
53 changes: 53 additions & 0 deletions yabgp/tests/unit/message/attribute/nlri/test_ipv6_unicast.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Copyright 2015 Cisco Systems, Inc.
# All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

""" Test IPv6 Unicast """

import unittest

from yabgp.message.attribute.nlri.ipv6_unicast import IPv6Unicast


class TestIPv6Unicast(unittest.TestCase):
def test_parse(self):
nlri_data = b'\x80\x20\x01\x32\x32\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x40\x20\x01\x32' \
b'\x32\x00\x01\x00\x00\x7f\x20\x01\x48\x37\x16\x32\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02'
value_parsed = IPv6Unicast().parse(nlri_data)
value_hoped = ['2001:3232::1/128', '::2001:3232:1:0/64', '2001:4837:1632::2/127']
self.assertEqual(value_hoped, value_parsed)

def test_parse_withdraw(self):
nlri_data = b'\x40\x20\x01\x0d\xb8\x00\x01\x00\x02\x40\x20\x01\x0d\xb8\x00\x01\x00\x01\x40\x20\x01\x0d' \
b'\xb8\x00\x01\x00\x00'
value_parsed = IPv6Unicast.parse(nlri_data)
value_hoped = ['::2001:db8:1:2/64', '::2001:db8:1:1/64', '::2001:db8:1:0/64']
self.assertEqual(value_hoped, value_parsed)

def test_construct_nlri(self):
nlri_list = ['::2001:db8:1:2/64', '::2001:db8:1:1/64', '::2001:db8:1:0/64']
value_hex = IPv6Unicast.construct(nlri_list)
value_hoped = b'\x40\x20\x01\x0d\xb8\x00\x01\x00\x02\x40\x20\x01\x0d\xb8\x00\x01\x00\x01\x40\x20\x01\x0d' \
b'\xb8\x00\x01\x00\x00'
self.assertEqual(value_hoped, value_hex)

def test_construct_nlri_2(self):
nlri_list = ['2001:3232::1/128', '::2001:3232:1:0/64', '2001:4837:1632::2/127']
value_hex = IPv6Unicast.construct(nlri_list)
value_hoped = b'\x80\x20\x01\x32\x32\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x40\x20\x01\x32' \
b'\x32\x00\x01\x00\x00\x7f\x20\x01\x48\x37\x16\x32\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02'
self.assertEqual(value_hoped, value_hex)

if __name__ == '__main__':
unittest.main()

0 comments on commit 7ac61dd

Please sign in to comment.