Signed-off-by: Isaku Yamahata <[email protected]>
---
 ryu/lib/packet/packet_utils.py |   61 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 61 insertions(+)

diff --git a/ryu/lib/packet/packet_utils.py b/ryu/lib/packet/packet_utils.py
index 2a7f221..b8f2e05 100644
--- a/ryu/lib/packet/packet_utils.py
+++ b/ryu/lib/packet/packet_utils.py
@@ -14,6 +14,7 @@
 # limitations under the License.
 
 import socket
+import struct
 
 
 def carry_around_add(a, b):
@@ -30,3 +31,63 @@ def checksum(data):
         w = data[i] + (data[i + 1] << 8)
         s = carry_around_add(s, w)
     return socket.ntohs(~s & 0xffff)
+
+
+# avoid circular import
+_IPV4_PSEUDO_HEADER_PACK_STR = '!IIxBH'
+_IPV6_PSEUDO_HEADER_PACK_STR = '!16s16sI3xB'
+
+
+def checksum_ip(ipvx, length, payload):
+    """
+    calculate checksum of IP pseudo header
+
+    IPv4 pseudo header
+    UDP RFC768
+    TCP RFC793 3.1
+
+     0      7 8     15 16    23 24    31
+    +--------+--------+--------+--------+
+    |          source address           |
+    +--------+--------+--------+--------+
+    |        destination address        |
+    +--------+--------+--------+--------+
+    |  zero  |protocol|    length       |
+    +--------+--------+--------+--------+
+
+
+    IPv6 pseudo header
+    RFC2460 8.1
+    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+    |                                                               |
+    +                                                               +
+    |                                                               |
+    +                         Source Address                        +
+    |                                                               |
+    +                                                               +
+    |                                                               |
+    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+    |                                                               |
+    +                                                               +
+    |                                                               |
+    +                      Destination Address                      +
+    |                                                               |
+    +                                                               +
+    |                                                               |
+    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+    |                   Upper-Layer Packet Length                   |
+    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+    |                      zero                     |  Next Header  |
+    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+    """
+    if ipvx.version == 4:
+        header = struct.pack(_IPV4_PSEUDO_HEADER_PACK_STR,
+                             ipvx.src, ipvx.dst, ipvx.proto, length)
+    elif ipvx.version == 6:
+        header = struct.pack(_IPV6_PSEUDO_HEADER_PACK_STR,
+                             ipvx.src, ipvx.dst, length, ipvx.nxt)
+    else:
+        raise ValueError('Unknown IP version %d' % ipvx.version)
+
+    buf = header + payload
+    return checksum(buf)
-- 
1.7.10.4


------------------------------------------------------------------------------
Own the Future-Intel(R) Level Up Game Demo Contest 2013
Rise to greatness in Intel's independent game demo contest. Compete 
for recognition, cash, and the chance to get your game on Steam. 
$5K grand prize plus 10 genre and skill prizes. Submit your demo 
by 6/6/13. http://altfarm.mediaplex.com/ad/ck/12124-176961-30367-2
_______________________________________________
Ryu-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ryu-devel

Reply via email to