Skip to content

Commit 9fee956

Browse files
author
Noah Potash
committed
Fixed TCP checksums for odd length headers
1 parent f5f363b commit 9fee956

File tree

3 files changed

+30
-23
lines changed

3 files changed

+30
-23
lines changed

Diff for: src/LibPacketGremlin/LibPacketGremlin.csproj

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
<PackageProjectUrl>https://github.com/SapientGuardian/LibPacketGremlin</PackageProjectUrl>
1515
<PackageLicenseUrl>http://opensource.org/licenses/MIT</PackageLicenseUrl>
1616
<PackageTargetFallback>$(PackageTargetFallback);dnxcore50</PackageTargetFallback>
17-
<Version>1.1.0</Version>
18-
<AssemblyVersion>0.1.1.0</AssemblyVersion>
19-
<FileVersion>0.1.1.0</FileVersion>
17+
<Version>1.1.1</Version>
18+
<AssemblyVersion>0.1.1.1</AssemblyVersion>
19+
<FileVersion>0.1.1.1</FileVersion>
2020
</PropertyGroup>
2121

2222
<ItemGroup>

Diff for: src/LibPacketGremlin/Packets/TCP.cs

+9-1
Original file line numberDiff line numberDiff line change
@@ -330,12 +330,20 @@ internal static ushort ComputeChecksum(byte[] header, int start, int length)
330330
{
331331
ushort word16;
332332
long sum = 0;
333-
for (var i = start; i < length + start; i += 2)
333+
334+
int i;
335+
for (i = start; i < length + start - 1; i += 2)
334336
{
335337
word16 = (ushort)(((header[i] << 8) & 0xFF00) + (header[i + 1] & 0xFF));
336338
sum += word16;
337339
}
338340

341+
if (length % 2 != 0)
342+
{
343+
word16 = (ushort)((header[i] << 8) & 0xFF00);
344+
sum += word16;
345+
}
346+
339347
while (sum >> 16 != 0)
340348
{
341349
sum = (sum & 0xFFFF) + (sum >> 16);

Diff for: test/LibPacketGremlinTests/Packets/TCPTests.cs

+18-19
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public void ParsesBasicFields()
4343
packet.Fin.Should().BeFalse();
4444
packet.WindowSize.Should().Be(64078);
4545
packet.Checksum.Should().Be(0x1b5e);
46-
packet.UrgentPointer.Should().Be(0);
46+
packet.UrgentPointer.Should().Be(0);
4747
}
4848

4949
[Fact]
@@ -57,10 +57,10 @@ public void SerializesCorrectly()
5757
, 0xcb, 0xde, 0xa7, 0xcf, 0xeb, 0x46, 0xd4, 0x88, 0x48, 0x48, 0x91, 0xda
5858
, 0xb9, 0xd8, 0xd7, 0x52, 0xef, 0x36, 0x2a}, out packet).Should().BeTrue();
5959

60-
60+
6161
using (var ms = new MemoryStream())
6262
{
63-
packet.WriteToStream(ms);
63+
packet.WriteToStream(ms);
6464
ms.ToArray()
6565
.SequenceEqual(
6666
new byte[]
@@ -76,22 +76,21 @@ public void SerializesCorrectly()
7676
}
7777

7878
[Fact]
79-
public void CorrectsFields()
80-
{
81-
// This test is poor. It seems the original test for CorrectFields didn't set an IPv4 parent, and thus was basically worthless.
82-
83-
TCP packet;
84-
TCPFactory.Instance.TryParse(new byte[]{0x04, 0x0a
85-
, 0x14, 0x46, 0x45, 0x0e, 0x6e, 0xc8, 0xfb, 0xcd, 0x4f, 0xcc, 0x50, 0x18
86-
, 0xfa, 0x4e, 0x1b, 0x5e, 0x00, 0x00, 0x17, 0x03, 0x01, 0x00, 0x20, 0x0f
87-
, 0x52, 0xba, 0x8d, 0xda, 0x74, 0x2a, 0xd7, 0x3d, 0x59, 0x2f, 0x22, 0x5e
88-
, 0xcb, 0xde, 0xa7, 0xcf, 0xeb, 0x46, 0xd4, 0x88, 0x48, 0x48, 0x91, 0xda
89-
, 0xb9, 0xd8, 0xd7, 0x52, 0xef, 0x36, 0x2a}, out packet).Should().BeTrue();
90-
91-
var correctChecksum = packet.Checksum;
92-
packet.CorrectFields();
93-
packet.Checksum.Should().Be(correctChecksum);
94-
((int)packet.DataOffset).Should().Be(5);
79+
public void CalculatesChecksum()
80+
{
81+
IPv4 packet;
82+
IPv4Factory.Instance.TryParse(new byte[]{ 0x45, 0x00,0x00, 0x29,0x10, 0xe9,
83+
0x40, 0x00, 0x80, 0x06, 0x00, 0x00, 0x7f, 0x00,
84+
0x00, 0x01, 0x7f, 0x00, 0x00, 0x01, 0xd4, 0x68,
85+
0xd4, 0x69, 0xb6, 0x08, 0xff, 0x50, 0x01, 0x1e,
86+
0x37, 0xd9, 0x50, 0x18, 0x01, 0x00, 0xf8, 0xa5,
87+
0x00, 0x00, 0x21 }, out packet).Should().BeTrue();
88+
89+
var e2 = EthernetIIFactory.Instance.Default(packet);
90+
var tcp = packet.Layer<TCP>();
91+
var correctChecksum = tcp.Checksum;
92+
e2.CorrectFields();
93+
tcp.Checksum.Should().Be(correctChecksum);
9594
}
9695

9796
[Fact]

0 commit comments

Comments
 (0)