# [Winpcap-users] UDP Checksum

leekyung moon gilgil1973 at hotmail.com
Mon Aug 25 10:45:54 GMT 2008

```Hi, all, this is code of calculation udp checksum.

http://www.gilgil.co.kr/snoop/

//
// All UDPHdr field except UDPHdr.Checksum
// IPHdr.Source, IPHdr.Destination, UDPHdrLen
//
function snoopUDPChecksum(IPHdr: PIP_HDR; UDPHdr: PUDP_HDR): UINT16;
var
i: Integer;
UDPHdrLen: Integer;
Source, Destination: UINT32;
Sum: UINT32;
p: PUINT16;
begin
UDPHdrLen := ntohs(UDPHdr.Len);
Sum := 0;

// Add UDPHdr and Data Buffer as array of UINT16
p := PUINT16(UDPHdr);
for i := 0 to UDPHdrLen div 2 - 1 do
begin
Sum := Sum + htons(p^);
inc(p);
end;

// if Len is OddNumber, Add Last Data
if (UDPHdrLen div 2) * 2 <> UDPHdrLen then
Sum := Sum + (htons(p^) and \$FF00);

// Decrease Checksum from Sum
Sum := Sum - ntohs(UDPHdr.Checksum);

Source := ntohl(IPHdr.Source);
Sum := Sum + ((Source and \$FFFF0000) shr 16) + (Source and \$0000FFFF);

Destination := ntohl(IPHdr.Destination);
Sum := Sum + ((Destination and \$FFFF0000) shr 16) + (Destination and \$0000FFFF);

Sum := Sum + PROTO_UDP + UINT32(UDPHdrLen);

// Recalculate Sum
while (Sum shr 16) > 0 do
Sum := (Sum and \$FFFF) + (Sum shr 16);
Sum := not Sum;

Result := UINT16(Sum);
end;

From: gianluca.varenni at cacetech.com
To: winpcap-users at winpcap.org
Subject: Re: [Winpcap-users] UDP Checksum
Date: Tue, 19 Aug 2008 13:12:32 -0700

I would have a look at
- the tcpdump source code
- the wireshark source code

Have a nice day
GV

----- Original Message -----
From:
To: winpcap-users at winpcap.org
Sent: Tuesday, August 19, 2008 4:01
AM
Subject: [Winpcap-users] UDP
Checksum

Anyone has a sample code to do udp checksum calculation?

_______________________________________________
Winpcap-users
mailing list
Winpcap-users at winpcap.org
https://www.winpcap.org/mailman/listinfo/winpcap-users

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.winpcap.org/pipermail/winpcap-users/attachments/20080825/7d034c50/attachment.htm
```