Commit 02a7c395 authored by Boaz Brickner's avatar Boaz Brickner

Avoid returning negative Padding when the Ethernet's payload is big enough.

parent c30aa3b1
using System;
using System.Collections.ObjectModel;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using PcapDotNet.Base;
using PcapDotNet.Packets.Arp;
using PcapDotNet.Packets.Ethernet;
using PcapDotNet.Packets.IpV4;
using PcapDotNet.Packets.IpV6;
......@@ -132,5 +135,23 @@ namespace PcapDotNet.Packets.Test
Assert.IsTrue(packet.IsValid);
Assert.AreEqual(DataSegment.Empty, packet.Ethernet.Padding);
}
[TestMethod]
public void PayloadTooBigForPadding()
{
Packet packet = PacketBuilder.Build(DateTime.Now,
new EthernetLayer(),
new ArpLayer
{
ProtocolType = EthernetType.IpV4,
Operation = ArpOperation.DynamicReverseError,
SenderHardwareAddress = new byte[12].AsReadOnly(),
SenderProtocolAddress = new byte[22].AsReadOnly(),
TargetHardwareAddress = new byte[12].AsReadOnly(),
TargetProtocolAddress = new byte[22].AsReadOnly(),
});
Assert.IsTrue(packet.IsValid);
Assert.AreEqual(DataSegment.Empty, packet.Ethernet.Padding);
}
}
}
\ No newline at end of file
......@@ -45,9 +45,11 @@ namespace PcapDotNet.Packets.Ethernet
if (payloadByEtherType == null)
return null;
int payloadLength = PayloadByEtherType.Length;
return new DataSegment(Buffer, StartOffset + HeaderLength + payloadLength, 60 - HeaderLength - payloadLength);
int payloadLength = payloadByEtherType.Length;
int dataLength = HeaderLength + payloadLength;
if (dataLength >= 60)
return DataSegment.Empty;
return new DataSegment(Buffer, StartOffset + dataLength, 60 - dataLength);
}
}
......@@ -106,7 +108,7 @@ namespace PcapDotNet.Packets.Ethernet
if (payloadByEtherType == null)
return null;
int payloadLength = PayloadByEtherType.Length;
int payloadLength = payloadByEtherType.Length;
return new DataSegment(Buffer, StartOffset + HeaderLength + payloadLength, Length - HeaderLength - payloadLength);
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment