Signed-off-by: itoyuichi <[email protected]>
---
 ryu/lib/packet/ipv6.py |   45 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 45 insertions(+)

diff --git a/ryu/lib/packet/ipv6.py b/ryu/lib/packet/ipv6.py
index 060e678..f8538c1 100644
--- a/ryu/lib/packet/ipv6.py
+++ b/ryu/lib/packet/ipv6.py
@@ -170,6 +170,51 @@ class header(stringify.StringifyMixin):
 # TODO: implement a class for routing header


+class opt_header(header):
+    """an abstract class for Hop-by-Hop Options header and destination
+    header."""
+
+    _PACK_STR = '!BB'
+    _MIN_LEN = struct.calcsize(_PACK_STR)
+    _FIX_SIZE = 8
+
+    @abc.abstractmethod
+    def __init__(self, size, data):
+        super(opt_header, self).__init__()
+        assert not (size % 8)
+        self.size = size
+        self.data = data
+
+    @classmethod
+    def parser(cls, buf):
+        (nxt, len_) = struct.unpack_from(cls._PACK_STR, buf)
+        data_len = cls._FIX_SIZE + int(len_)
+        data = []
+        size = cls._MIN_LEN
+        while size < data_len:
+            (type_, ) = struct.unpack_from('!B', buf[size:])
+            if type_ == 0:
+                opt = option(type_, -1, None)
+                size += 1
+            else:
+                opt = option.parser(buf[size:])
+                size += len(opt)
+            data.append(opt)
+        ret = cls(len_, data)
+        ret.set_nxt(nxt)
+        return ret
+
+    def serialize(self):
+        buf = struct.pack(self._PACK_STR, self.nxt, self.size)
+        buf = bytearray(buf)
+        for opt in self.data:
+            buf.extend(opt.serialize())
+        return buf
+
+    def __len__(self):
+        return self._FIX_SIZE + self.size
+
+
 class option(stringify.StringifyMixin):
     """IPv6 (RFC 2460) Options header encoder/decoder class.

-- 
1.7.10.4


------------------------------------------------------------------------------
LIMITED TIME SALE - Full Year of Microsoft Training For Just $49.99!
1,500+ hours of tutorials including VisualStudio 2012, Windows 8, SharePoint
2013, SQL 2012, MVC 4, more. BEST VALUE: New Multi-Library Power Pack includes
Mobile, Cloud, Java, and UX Design. Lowest price ever! Ends 9/20/13. 
http://pubads.g.doubleclick.net/gampad/clk?id=58041151&iu=/4140/ostg.clktrk
_______________________________________________
Ryu-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ryu-devel

Reply via email to