Signed-off-by: Yuichi Ito <[email protected]>
---
 ryu/lib/packet/ipv6.py             |   26 ++++++++----
 ryu/tests/unit/packet/test_ipv6.py |   78 ++++++++++++++++++++++++++++++++++++
 2 files changed, 97 insertions(+), 7 deletions(-)

diff --git a/ryu/lib/packet/ipv6.py b/ryu/lib/packet/ipv6.py
index e798f5e..b0207c2 100644
--- a/ryu/lib/packet/ipv6.py
+++ b/ryu/lib/packet/ipv6.py
@@ -68,8 +68,9 @@ class ipv6(packet_base.PacketBase):
             return cls
         return _register_header_type

-    def __init__(self, version, traffic_class, flow_label, payload_length,
-                 nxt, hop_limit, src, dst, ext_hdrs=None):
+    def __init__(self, version=6, traffic_class=0, flow_label=0,
+                 payload_length=0, nxt=inet.IPPROTO_NONE, hop_limit=0,
+                 src='::', dst='::', ext_hdrs=None):
         super(ipv6, self).__init__()
         self.version = version
         self.traffic_class = traffic_class
@@ -121,6 +122,12 @@ class ipv6(packet_base.PacketBase):
         if self.ext_hdrs:
             for ext_hdr in self.ext_hdrs:
                 hdr.extend(ext_hdr.serialize())
+        if 0 == self.payload_length:
+            payload_length = len(payload)
+            for ext_hdr in self.ext_hdrs:
+                payload_length += len(ext_hdr)
+            self.payload_length = payload_length
+            struct.pack_into('!H', hdr, 4, self.payload_length)
         return hdr

     def __len__(self):
@@ -194,6 +201,9 @@ class opt_header(header):
     def serialize(self):
         buf = struct.pack(self._PACK_STR, self.nxt, self.size)
         buf = bytearray(buf)
+        if self.data is None:
+            self.data = [option(type_=1, len_=6,
+                                data='\x00\x00\x00\x00\x00\x00')]
         for opt in self.data:
             buf.extend(opt.serialize())
         return buf
@@ -225,7 +235,7 @@ class hop_opts(opt_header):
     """
     TYPE = inet.IPPROTO_HOPOPTS

-    def __init__(self, nxt, size, data):
+    def __init__(self, nxt=inet.IPPROTO_NONE, size=0, data=None):
         super(hop_opts, self).__init__(nxt, size, data)


@@ -252,7 +262,7 @@ class dst_opts(opt_header):
     """
     TYPE = inet.IPPROTO_DSTOPTS

-    def __init__(self, nxt, size, data):
+    def __init__(self, nxt=inet.IPPROTO_NONE, size=0, data=None):
         super(dst_opts, self).__init__(nxt, size, data)


@@ -280,7 +290,7 @@ class option(stringify.StringifyMixin):
     _PACK_STR = '!BB'
     _MIN_LEN = struct.calcsize(_PACK_STR)

-    def __init__(self, type_, len_, data):
+    def __init__(self, type_=0, len_=-1, data=None):
         self.type_ = type_
         self.len_ = len_
         self.data = data
@@ -344,7 +354,7 @@ class fragment(header):
     _PACK_STR = '!BxHI'
     _MIN_LEN = struct.calcsize(_PACK_STR)

-    def __init__(self, nxt, offset, more, id_):
+    def __init__(self, nxt=inet.IPPROTO_NONE, offset=0, more=0, id_=0):
         super(fragment, self).__init__(nxt)
         self.offset = offset
         self.more = more
@@ -394,8 +404,10 @@ class auth(header):
     _PACK_STR = '!BB2xII'
     _MIN_LEN = struct.calcsize(_PACK_STR)

-    def __init__(self, nxt, size, spi, seq, data):
+    def __init__(self, nxt=inet.IPPROTO_NONE, size=3, spi=0, seq=0,
+                 data='\x00\x00\x00\x00'):
         super(auth, self).__init__(nxt)
+        assert data is not None
         self.size = size
         self.spi = spi
         self.seq = seq
diff --git a/ryu/tests/unit/packet/test_ipv6.py 
b/ryu/tests/unit/packet/test_ipv6.py
index 0810086..e701234 100644
--- a/ryu/tests/unit/packet/test_ipv6.py
+++ b/ryu/tests/unit/packet/test_ipv6.py
@@ -396,6 +396,35 @@ class Test_ipv6(unittest.TestCase):
         self.setUp_with_multi_headers()
         eq_(len(self.ip), 40 + len(self.hop_opts) + len(self.auth))

+    def test_default_args(self):
+        ip = ipv6.ipv6()
+        buf = ip.serialize(bytearray(), None)
+        res = struct.unpack(ipv6.ipv6._PACK_STR, str(buf))
+
+        eq_(res[0], 6 << 28)
+        eq_(res[1], 0)
+        eq_(res[2], 59)
+        eq_(res[3], 0)
+        eq_(res[4], addrconv.ipv6.text_to_bin('::'))
+        eq_(res[5], addrconv.ipv6.text_to_bin('::'))
+
+        # with extension header
+        ip = ipv6.ipv6(
+            nxt=0, ext_hdrs=[
+                ipv6.hop_opts(58, 0, [
+                    ipv6.option(5, 2, '\x00\x00'),
+                    ipv6.option(1, 0, None)])])
+        buf = ip.serialize(bytearray(), None)
+        res = struct.unpack(ipv6.ipv6._PACK_STR + '8s', str(buf))
+
+        eq_(res[0], 6 << 28)
+        eq_(res[1], 8)
+        eq_(res[2], 0)
+        eq_(res[3], 0)
+        eq_(res[4], addrconv.ipv6.text_to_bin('::'))
+        eq_(res[5], addrconv.ipv6.text_to_bin('::'))
+        eq_(res[6], '\x3a\x00\x05\x02\x00\x00\x01\x00')
+

 class Test_hop_opts(unittest.TestCase):

@@ -467,6 +496,16 @@ class Test_hop_opts(unittest.TestCase):
     def test_len(self):
         eq_(16, len(self.hop))

+    def test_default_args(self):
+        hdr = ipv6.hop_opts()
+        buf = hdr.serialize()
+        res = struct.unpack('!BB', str(buf[:2]))
+
+        eq_(res[0], 59)
+        eq_(res[1], 0)
+        opt = ipv6.option(type_=1, len_=6, data='\x00\x00\x00\x00\x00\x00')
+        eq_(str(buf[2:]), opt.serialize())
+

 class Test_dst_opts(unittest.TestCase):

@@ -538,6 +577,16 @@ class Test_dst_opts(unittest.TestCase):
     def test_len(self):
         eq_(16, len(self.dst))

+    def test_default_args(self):
+        hdr = ipv6.dst_opts()
+        buf = hdr.serialize()
+        res = struct.unpack('!BB', str(buf[:2]))
+
+        eq_(res[0], 59)
+        eq_(res[1], 0)
+        opt = ipv6.option(type_=1, len_=6, data='\x00\x00\x00\x00\x00\x00')
+        eq_(str(buf[2:]), opt.serialize())
+

 class Test_option(unittest.TestCase):

@@ -593,6 +642,13 @@ class Test_option_pad1(Test_option):
         res = struct.unpack_from(self.form, buf)
         eq_(self.type_, res[0])

+    def test_default_args(self):
+        opt = ipv6.option()
+        buf = opt.serialize()
+        res = struct.unpack('!B', buf)
+
+        eq_(res[0], 0)
+

 class Test_option_padN(Test_option):

@@ -652,6 +708,15 @@ class Test_fragment(unittest.TestCase):
     def test_len(self):
         eq_(8, len(self.fragment))

+    def test_default_args(self):
+        hdr = ipv6.fragment()
+        buf = hdr.serialize()
+        res = struct.unpack_from(ipv6.fragment._PACK_STR, buf)
+
+        eq_(res[0], 59)
+        eq_(res[1], 0)
+        eq_(res[2], 0)
+

 class Test_auth(unittest.TestCase):

@@ -697,3 +762,16 @@ class Test_auth(unittest.TestCase):

     def test_len(self):
         eq_((4 - 1) * 8, len(self.auth))
+
+    def test_default_args(self):
+        hdr = ipv6.auth()
+        buf = hdr.serialize()
+        LOG.info(repr(buf))
+        res = struct.unpack_from(ipv6.auth._PACK_STR, str(buf))
+        LOG.info(res)
+
+        eq_(res[0], 59)
+        eq_(res[1], 3)
+        eq_(res[2], 0)
+        eq_(res[3], 0)
+        eq_(buf[ipv6.auth._MIN_LEN:], '\x00\x00\x00\x00')
-- 
1.7.10.4


------------------------------------------------------------------------------
Android is increasing in popularity, but the open development platform that
developers love is also attractive to malware creators. Download this white
paper to learn more about secure code signing practices that can help keep
Android apps secure.
http://pubads.g.doubleclick.net/gampad/clk?id=65839951&iu=/4140/ostg.clktrk
_______________________________________________
Ryu-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ryu-devel

Reply via email to