mirror of
https://github.com/faucetsdn/ryu.git
synced 2026-05-10 23:06:10 +02:00
lib/packet/packet_utils: improve checksum byteswap
move byteswap logic into checksum from caller. Signed-off-by: Isaku Yamahata <yamahata@valinux.co.jp> Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
This commit is contained in:
parent
d0c5c14ad8
commit
565df6fc48
@ -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
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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
|
||||
|
||||
|
||||
@ -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)
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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):
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user