From ea779e37b23bfd4802ac383c000a6ccd6155cea0 Mon Sep 17 00:00:00 2001 From: gpotter2 Date: Fri, 12 Jan 2018 20:01:43 +0100 Subject: [PATCH] Update UDP checksum manual computation doc --- doc/scapy/functions.rst | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/doc/scapy/functions.rst b/doc/scapy/functions.rst index 136ce542d..836ff9360 100644 --- a/doc/scapy/functions.rst +++ b/doc/scapy/functions.rst @@ -19,22 +19,16 @@ UDP checksum manually. The following steps must be performed: from scapy.all import * - def pseudo_header(ip_src, ip_dst, ip_proto, length): - """ - Return a pseudo header according to RFC768 - """ - # Prepare the binary representation of the pseudo header - return struct.pack("!4s4sHH", inet_aton(ip_src), inet_aton(ip_dst), ip_proto, length) - # Get the UDP checksum computed by Scapy packet = IP(dst="10.11.12.13", src="10.11.12.14")/UDP()/DNS() - packet_raw = raw(packet) - checksum_scapy = IP(packet_raw)[UDP].chksum + packet = IP(raw(packet)) # Build packet (automatically done when sending) + checksum_scapy = packet[UDP].chksum # Set the UDP checksum to 0 and compute the checksum 'manually' packet = IP(dst="10.11.12.13", src="10.11.12.14")/UDP(chksum=0)/DNS() packet_raw = raw(packet) udp_raw = packet_raw[20:] - phdr = pseudo_header(packet.src, packet.dst, socket.IPPROTO_UDP, len(udp_raw)) + # in4_chksum is used to automatically build a pseudo-header + chksum = in4_chksum(socket.IPPROTO_UDP, packet[IP], udp_raw) # For more infos, call "help(in4_chksum)" - assert(checksum_scapy == checksum(phdr + udp_raw)) + assert(checksum_scapy == chksum)