move byteswap logic into checksum from caller.

Signed-off-by: Isaku Yamahata <[email protected]>
---
 ryu/lib/packet/icmp.py               |    4 ++--
 ryu/lib/packet/icmpv6.py             |    3 +--
 ryu/lib/packet/ipv4.py               |    4 ++--
 ryu/lib/packet/packet_utils.py       |    4 +++-
 ryu/lib/packet/tcp.py                |    4 ++--
 ryu/lib/packet/udp.py                |    4 ++--
 ryu/tests/unit/packet/test_icmpv6.py |    3 +--
 7 files changed, 13 insertions(+), 13 deletions(-)

diff --git a/ryu/lib/packet/icmp.py b/ryu/lib/packet/icmp.py
index 6fb7ebf..9fcd8b3 100644
--- a/ryu/lib/packet/icmp.py
+++ b/ryu/lib/packet/icmp.py
@@ -14,7 +14,7 @@
 # limitations under the License.
 
 import struct
-import socket
+
 from . import packet_base
 from . import packet_utils
 
@@ -72,7 +72,7 @@ class icmp(packet_base.PacketBase):
                 hdr += self.data
 
         if self.csum == 0:
-            self.csum = socket.htons(packet_utils.checksum(hdr))
+            self.csum = packet_utils.checksum(hdr)
             struct.pack_into('!H', hdr, 2, self.csum)
 
         return hdr
diff --git a/ryu/lib/packet/icmpv6.py b/ryu/lib/packet/icmpv6.py
index f0aef16..bcad884 100644
--- a/ryu/lib/packet/icmpv6.py
+++ b/ryu/lib/packet/icmpv6.py
@@ -14,7 +14,6 @@
 # limitations under the License.
 
 import struct
-import socket
 import sys
 import array
 import binascii
@@ -107,7 +106,7 @@ class icmpv6(packet_base.PacketBase):
             ph = struct.pack('!16s16sBBH', prev.src, prev.dst, 0, prev.nxt,
                              length)
             f = ph + hdr + payload
-            self.csum = socket.htons(packet_utils.checksum(f))
+            self.csum = packet_utils.checksum(f)
             struct.pack_into('!H', hdr, 2, self.csum)
 
         return hdr
diff --git a/ryu/lib/packet/ipv4.py b/ryu/lib/packet/ipv4.py
index 99140d4..daf4265 100644
--- a/ryu/lib/packet/ipv4.py
+++ b/ryu/lib/packet/ipv4.py
@@ -14,7 +14,7 @@
 # limitations under the License.
 
 import struct
-import socket
+
 from . import packet_base
 from . import packet_utils
 from . import icmp
@@ -76,7 +76,7 @@ class ipv4(packet_base.PacketBase):
             assert (self.length - ipv4._MIN_LEN) >= len(self.option)
             hdr[ipv4._MIN_LEN:ipv4._MIN_LEN + len(self.option)] = self.option
 
-        self.csum = socket.htons(packet_utils.checksum(hdr))
+        self.csum = packet_utils.checksum(hdr)
         struct.pack_into('!H', hdr, 10, self.csum)
         return hdr
 
diff --git a/ryu/lib/packet/packet_utils.py b/ryu/lib/packet/packet_utils.py
index 6584caa..2a7f221 100644
--- a/ryu/lib/packet/packet_utils.py
+++ b/ryu/lib/packet/packet_utils.py
@@ -13,6 +13,8 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
+import socket
+
 
 def carry_around_add(a, b):
     c = a + b
@@ -27,4 +29,4 @@ def checksum(data):
     for i in range(0, len(data), 2):
         w = data[i] + (data[i + 1] << 8)
         s = carry_around_add(s, w)
-    return ~s & 0xffff
+    return socket.ntohs(~s & 0xffff)
diff --git a/ryu/lib/packet/tcp.py b/ryu/lib/packet/tcp.py
index 99182c3..a225847 100644
--- a/ryu/lib/packet/tcp.py
+++ b/ryu/lib/packet/tcp.py
@@ -14,7 +14,7 @@
 # limitations under the License.
 
 import struct
-import socket
+
 from . import packet_base
 from . import packet_utils
 import ipv4
@@ -72,6 +72,6 @@ class tcp(packet_base.PacketBase):
                 ph = struct.pack('!16s16sBBH', prev.src, prev.dst, 0, 6,
                                  length)
             f = ph + h + payload
-            self.csum = socket.htons(packet_utils.checksum(f))
+            self.csum = packet_utils.checksum(f)
             struct.pack_into('!H', h, 16, self.csum)
         return h
diff --git a/ryu/lib/packet/udp.py b/ryu/lib/packet/udp.py
index addf436..2944f8a 100644
--- a/ryu/lib/packet/udp.py
+++ b/ryu/lib/packet/udp.py
@@ -14,7 +14,7 @@
 # limitations under the License.
 
 import struct
-import socket
+
 from . import packet_base
 from . import packet_utils
 import ipv4
@@ -48,7 +48,7 @@ class udp(packet_base.PacketBase):
             ph = struct.pack('!IIBBH', prev.src, prev.dst, 0, 17,
                              self.total_length)
             f = ph + h + payload
-            self.csum = socket.htons(packet_utils.checksum(f))
+            self.csum = packet_utils.checksum(f)
             h = struct.pack(udp._PACK_STR, self.src_port, self.dst_port,
                             self.total_length, self.csum)
         return h
diff --git a/ryu/tests/unit/packet/test_icmpv6.py 
b/ryu/tests/unit/packet/test_icmpv6.py
index bc447d4..9bedd06 100644
--- a/ryu/tests/unit/packet/test_icmpv6.py
+++ b/ryu/tests/unit/packet/test_icmpv6.py
@@ -19,7 +19,6 @@ import unittest
 import logging
 import struct
 import netaddr
-import socket
 
 from nose.tools import ok_, eq_, nottest, raises
 from nose.plugins.skip import Skip, SkipTest
@@ -40,7 +39,7 @@ def icmpv6_csum(prev, buf):
     h = bytearray(buf)
     struct.pack_into('!H', h, 2, 0)
 
-    return socket.htons(packet_utils.checksum(ph + h))
+    return packet_utils.checksum(ph + h)
 
 
 class Test_icmpv6_header(unittest.TestCase):
-- 
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