Skip to content

Commit

Permalink
Add CustomPayloadDecoder (dotpcap#144)
Browse files Browse the repository at this point in the history
  • Loading branch information
jgaulon committed Sep 30, 2021
1 parent 56e7140 commit 01affc1
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 3 deletions.
22 changes: 20 additions & 2 deletions PacketDotNet/TcpPacket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,16 @@ public TcpPacket(ByteArraySegment byteArraySegment)
// store the payload bytes
PayloadPacketOrData = new LazySlim<PacketOrByteArraySegment>(() =>
{
var result = new PacketOrByteArraySegment { ByteArraySegment = Header.NextSegment() };
PacketOrByteArraySegment result;
var payload = Header.NextSegment();
if (CustomPayloadDecoder != null && (result = CustomPayloadDecoder(payload, this)) != null)
{
Log.Debug("Use CustomPayloadDecoder");
return result;
}
result = new PacketOrByteArraySegment { ByteArraySegment = payload };
return result;
});
}
Expand Down Expand Up @@ -117,7 +126,16 @@ public TcpPacket
// store the payload bytes
PayloadPacketOrData = new LazySlim<PacketOrByteArraySegment>(() =>
{
var result = new PacketOrByteArraySegment { ByteArraySegment = Header.NextSegment() };
PacketOrByteArraySegment result;
var payload = Header.NextSegment();
if (CustomPayloadDecoder != null && (result = CustomPayloadDecoder(payload, this)) != null)
{
Log.Debug("Use CustomPayloadDecoder");
return result;
}
result = new PacketOrByteArraySegment { ByteArraySegment = payload };
// if the parent packet is an IPv4Packet we need to adjust
// the payload length because it is possible for us to have
Expand Down
9 changes: 9 additions & 0 deletions PacketDotNet/TransportPacket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ This Source Code Form is subject to the terms of the Mozilla Public
file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/

using System;
using PacketDotNet.Utils;

#if DEBUG
Expand All @@ -20,6 +21,14 @@ namespace PacketDotNet
/// </summary>
public abstract class TransportPacket : Packet
{
/// <summary>
/// Callback function for TransportPacket Payload decoding.
/// First parameter is the payload, second parameter if the TransportPacket itself
/// returned value is the decoded payload as PacketOrByteArraySegment or null if payload is invalid or not supported
/// then internal decoding will continue
/// </summary>
public static Func<ByteArraySegment, TransportPacket, PacketOrByteArraySegment> CustomPayloadDecoder;

#if DEBUG
private static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
#else
Expand Down
10 changes: 9 additions & 1 deletion PacketDotNet/UdpPacket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,19 @@ public UdpPacket(ByteArraySegment byteArraySegment)
const int wakeOnLanPort7 = 7;
const int wakeOnLanPort9 = 9;
var result = new PacketOrByteArraySegment();
PacketOrByteArraySegment result;
var destinationPort = DestinationPort;
var sourcePort = SourcePort;
var payload = Header.NextSegment();
if (CustomPayloadDecoder != null && (result = CustomPayloadDecoder(payload, this)) != null)
{
Log.Debug("Use CustomPayloadDecoder");
return result;
}
result = new PacketOrByteArraySegment();
// If this packet is going to port 0, 7 or 9, then it might be a WakeOnLan packet.
if (destinationPort == wakeOnLanPort0 || destinationPort == wakeOnLanPort7 || destinationPort == wakeOnLanPort9)
{
Expand Down
37 changes: 37 additions & 0 deletions Test/PacketType/L2tpPacketTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ This Source Code Form is subject to the terms of the Mozilla Public
using System;
using NUnit.Framework;
using PacketDotNet;
using PacketDotNet.Utils;
using SharpPcap;
using SharpPcap.LibPcap;

Expand Down Expand Up @@ -37,5 +38,41 @@ public void L2tpParsing()
Assert.AreEqual(l2tp.SessionID, 54110);
Console.WriteLine(l2tp.GetType());
}

[Test]
public void L2tpParsingWithUdpCustomDecoder()
{
TransportPacket.CustomPayloadDecoder = UdpPayloadCustomDecoderFunc;
var dev = new CaptureFileReaderDevice(NUnitSetupClass.CaptureDirectory + "l2tp.pcap");
dev.Open();
PacketCapture c;
dev.GetNextPacket(out c);
var rawCapture = c.GetPacket();
dev.Close();

var p = Packet.ParsePacket(rawCapture.GetLinkLayers(), rawCapture.Data);

Assert.IsNotNull(p);

var l2tp = p.Extract<L2tpPacket>();
Assert.AreEqual(l2tp.TunnelID, 18994);
Assert.AreEqual(l2tp.SessionID, 54110);
Console.WriteLine(l2tp.GetType());
}

private PacketOrByteArraySegment UdpPayloadCustomDecoderFunc(ByteArraySegment payload, TransportPacket udpPacket)
{
if (udpPacket is UdpPacket)
{
Console.WriteLine($"Udp Packet from {udpPacket.SourcePort} to {udpPacket.DestinationPort}");
if (udpPacket.DestinationPort == L2tpFields.Port || udpPacket.SourcePort == L2tpFields.Port)
{

return new PacketOrByteArraySegment { Packet = new L2tpPacket(payload, udpPacket) };
}
}

return null;
}
}
}

0 comments on commit 01affc1

Please sign in to comment.