Added a port of BbfUdpDecap.
All checks were successful
🚀 Pack skyscraper8 / make-zip (push) Successful in 1m9s

This commit is contained in:
feyris-tan 2025-10-14 15:59:45 +02:00
parent 90ac87770d
commit c324a923de
6 changed files with 306 additions and 6 deletions

View File

@ -1,3 +1,4 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation"> <wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AFileInfo_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002Econfig_003FJetBrains_003FRider2025_002E1_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003Fe1ab690537c44e02a014076312b886b7b2e200_003F5a_003Fcf76af61_003FFileInfo_002Ecs/@EntryIndexedValue">ForceIncluded</s:String> <s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AFileInfo_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002Econfig_003FJetBrains_003FRider2025_002E1_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003Fe1ab690537c44e02a014076312b886b7b2e200_003F5a_003Fcf76af61_003FFileInfo_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AThrowHelper_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002Econfig_003FJetBrains_003FRider2025_002E1_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003Fe1ab690537c44e02a014076312b886b7b2e200_003F4f_003F7bfc5050_003FThrowHelper_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/Profiling/Configurations/=1/@EntryIndexedValue">&lt;data&gt;&lt;HostParameters type="LocalHostParameters" /&gt;&lt;Argument type="StandaloneArgument"&gt;&lt;Arguments IsNull="False"&gt;&lt;/Arguments&gt;&lt;FileName IsNull="False"&gt;&lt;/FileName&gt;&lt;WorkingDirectory IsNull="False"&gt;&lt;/WorkingDirectory&gt;&lt;Scope&gt;&lt;ProcessFilters /&gt;&lt;/Scope&gt;&lt;/Argument&gt;&lt;Info type="TimelineInfo" /&gt;&lt;CoreOptions type="CoreOptions"&gt;&lt;CoreTempPath IsNull="False"&gt;&lt;/CoreTempPath&gt;&lt;RemoteEndPoint IsNull="False"&gt;&lt;/RemoteEndPoint&gt;&lt;AdditionalEnvironmentVariables /&gt;&lt;/CoreOptions&gt;&lt;HostOptions type="HostOptions"&gt;&lt;HostTempPath IsNull="False"&gt;&lt;/HostTempPath&gt;&lt;/HostOptions&gt;&lt;/data&gt;</s:String></wpf:ResourceDictionary> <s:String x:Key="/Default/Profiling/Configurations/=1/@EntryIndexedValue">&lt;data&gt;&lt;HostParameters type="LocalHostParameters" /&gt;&lt;Argument type="StandaloneArgument"&gt;&lt;Arguments IsNull="False"&gt;&lt;/Arguments&gt;&lt;FileName IsNull="False"&gt;&lt;/FileName&gt;&lt;WorkingDirectory IsNull="False"&gt;&lt;/WorkingDirectory&gt;&lt;Scope&gt;&lt;ProcessFilters /&gt;&lt;/Scope&gt;&lt;/Argument&gt;&lt;Info type="TimelineInfo" /&gt;&lt;CoreOptions type="CoreOptions"&gt;&lt;CoreTempPath IsNull="False"&gt;&lt;/CoreTempPath&gt;&lt;RemoteEndPoint IsNull="False"&gt;&lt;/RemoteEndPoint&gt;&lt;AdditionalEnvironmentVariables /&gt;&lt;/CoreOptions&gt;&lt;HostOptions type="HostOptions"&gt;&lt;HostTempPath IsNull="False"&gt;&lt;/HostTempPath&gt;&lt;/HostOptions&gt;&lt;/data&gt;</s:String></wpf:ResourceDictionary>

View File

@ -0,0 +1,261 @@
using System.Net;
using System.Runtime.InteropServices;
using skyscraper5.Skyscraper.IO;
namespace skyscraper8.GSE;
public class BbfUdpDecap : IBbframeDeencapsulator
{
public IUdpDecapOutput Sink { get; set; }
public long NumPushed { get; private set; }
public void SetTargetIp(IPAddress ip)
{
HasTargetIp = ip.GetAddressBytes();
}
public void SetTargetPort(int tmp)
{
HasTargetPort = new byte[4];
HasTargetPort[0] = (byte)(tmp >> 8);
HasTargetPort[1] = (byte)(tmp & 0xff);
}
private byte? HasMis;
private byte[] HasSourceIp;
private byte[] HasTargetIp;
private byte[] HasSourcePort;
private byte[] HasTargetPort;
private byte[] inData;
[StructLayout(LayoutKind.Sequential, Pack = 1)]
struct bbheader
{
public byte MaType1;
public byte MaType2;
public byte Upl1;
public byte Upl2;
public byte Dfl1;
public byte Dfl2;
public byte Sync;
public byte SyncD1;
public byte SyncD2;
public byte Crc8;
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
unsafe struct Layer3
{
public byte l3sync;
public bbheader header;
public fixed byte Data[7254-10];
}
public unsafe void PushPacket(byte[] bbframe)
{
NumPushed++;
if (inData == null)
{
inData = new byte[8100];
}
if (fragmentor == null)
{
fragmentor = new byte[256][];
fragmentorLength = new int[256];
fragmentorPos = new int[256];
}
MemoryStream inbuf = new MemoryStream(bbframe);
Layer3 myLayer;
while (true)
{
if (inbuf.Peek() == 0xb8)
break;
inbuf.ReadByte();
}
while (true)
{
int fail = inbuf.Read(inData, 0, 11);
if (fail != 11)
break;
myLayer = MemoryMarshal.Read<Layer3>(inData);
if (myLayer.l3sync != 0xb8)
{
while (true)
{
if (inbuf.Peek() == 0xb8)
break;
fail = inbuf.Read(inData, 0, 1);
if (fail != 1)
break;
}
}
int bblength = myLayer.header.Dfl1 << 8 | myLayer.header.Dfl2;
bblength >>= 3;
fail = inbuf.Read(inData, 11, bblength);
if (fail != bblength)
break;
myLayer = MemoryMarshal.Read<Layer3>(inData);
if (HasMis.HasValue && (myLayer.header.MaType2 != HasMis.Value))
continue;
int pos = 0;
while (pos < bblength - 4)
{
int gseLength = ((myLayer.Data[pos] & 0x0f) << 8) | (myLayer.Data[pos + 1]);
if ((myLayer.Data[pos] & 0xf0) == 0) break;
if (gseLength + 2 > bblength - pos) break;
if (!ProcessBbFrame(&myLayer.Data[pos], gseLength))
break;
pos += gseLength += 2;
}
}
}
private byte[][] fragmentor;
private int[] fragmentorLength;
private int[] fragmentorPos;
private unsafe void Copy(byte* src, byte[] dst, int srcOffset, int dstOffset, int len)
{
for (int i = 0; i < len; i++)
{
dst[dstOffset + i] = src[srcOffset + i];
}
}
private unsafe bool ProcessBbFrame(byte* payload, int gseLength)
{
uint offset = 0;
uint fragId = 0;
if ((payload[0] & 0xc0) == 0x80)
{
fragId = payload[2];
int length=(payload[3]<<8) | payload[4];
if(fragmentor[fragId]!=null)
fragmentor[fragId] = null;
fragmentor[fragId] = new byte[length + 2];
fragmentorLength[fragId] = length + 2;
fragmentor[fragId][0] = payload[0];
fragmentor[fragId][1] = payload[1];
fragmentor[fragId][0] |= 0xc0;
Copy(payload, fragmentor[fragId], 5, 2, gseLength - 3);
fragmentorPos[fragId] = gseLength - 1;
}
else if ((payload[0] & 0xc0) == 0x00)
{
throw new NotImplementedException("inbetween packet");
}
else if ((payload[0] & 0xc0) == 0x40)
{
fragId = payload[2];
if (fragmentor[fragId] == null)
return true;
Copy(payload, fragmentor[fragId], 3, fragmentorPos[fragId], gseLength - 5);
fragmentorPos[fragId] += gseLength - 1;
fixed (byte* p = fragmentor[fragId])
{
ProcessBbFrame(p, fragmentorLength[fragId]);
}
fragmentor[fragId] = null;
}
else if ((payload[0] & 0xc0) == 0xc0)
{
if (payload[offset + 2] == 0x00 && payload[offset + 3] == 0x04)
{
throw new NotImplementedException("ethertype 0x0400");
}
else if (payload[offset + 2] == 0x00 && payload[offset + 3] == 0x00)
{
throw new NotImplementedException("ethertype 0x0000");
}
else if (payload[offset + 2] == 0x08 && payload[offset + 3] == 0x00)
{
if((payload[0]&0x30)==0x01) {
offset += 3; //3-byte label
}
else if((payload[0] & 0x30) == 0x00) {
offset += 6; //MAC address
}
offset += 0x10; //IPv4 header
if (IsSelected(&payload[offset]))
{
offset += 0x10;
}
else
{
return true;
}
long sinkSize = gseLength + 2 -offset;
byte[] sinkMe = new byte[sinkSize];
Copy(payload,sinkMe,(int)offset,0, sinkMe.Length);
Sink.Write(sinkMe);
return true;
}
//throw new NotImplementedException("complete packet");
}
else if ((payload[0] & 0x0f0) == 0x00)
{
throw new NotImplementedException("padding packet");
}
return true;
}
private unsafe bool IsSelected(byte* buf)
{
byte zero = buf[0];
byte one = buf[1];
byte two = buf[2];
byte three = buf[3];
byte four = buf[4];
byte five = buf[5];
byte six = buf[6];
byte seven = buf[7];
if(HasSourceIp != null && (!(zero==HasSourceIp[0] && one==HasSourceIp[1] && two==HasSourceIp[2] && three==HasSourceIp[3])))
return false;
if(HasTargetIp != null && (!(four==HasTargetIp[0] && five==HasTargetIp[1] && six==HasTargetIp[2] && seven==HasTargetIp[3])))
return false;
if(HasSourcePort != null &&(!(buf[0x8]==HasSourcePort[0] && buf[0x9]==HasSourcePort[1])))
return false;
if(HasTargetPort != null &&(!(buf[0xa]==HasTargetPort[0] && buf[0xb]==HasTargetPort[1])))
return false;
return true;
}
public interface IUdpDecapOutput
{
public void Write(byte[] buffer);
}
internal class UdpDecapFileOutput : IUdpDecapOutput
{
public UdpDecapFileOutput(FileInfo file)
{
file.Directory.EnsureExists();
ourStream = file.OpenWrite();
}
private FileStream ourStream;
public void Write(byte[] udpPacket)
{
ourStream.Write(udpPacket, 0, udpPacket.Length);
}
public void Dispose()
{
ourStream.Flush();
ourStream.Close();
ourStream.Dispose();
}
}
}

View File

@ -1,3 +1,4 @@
using System.Net;
using log4net; using log4net;
using skyscraper5.Mpeg2; using skyscraper5.Mpeg2;
using skyscraper5.Skyscraper.Scraper; using skyscraper5.Skyscraper.Scraper;
@ -10,7 +11,7 @@ public class Pts2Bbf
{ {
private static readonly ILog logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name); private static readonly ILog logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name);
public static void Run(FileInfo file) public static void Run(FileInfo file, bool useUdpDecap = false)
{ {
if (!file.Exists) if (!file.Exists)
{ {
@ -18,8 +19,23 @@ public class Pts2Bbf
return; return;
} }
string changeExtension = Path.ChangeExtension(file.FullName, ".sbbf"); IBbframeDeencapsulator dumper;
BbframeDumper dumper = new BbframeDumper(new FileInfo(changeExtension)); if (useUdpDecap)
{
string changeExtension = Path.ChangeExtension(file.FullName, ".sdecap");
BbfUdpDecap.UdpDecapFileOutput udpDecapSink = new BbfUdpDecap.UdpDecapFileOutput(new FileInfo(changeExtension));
BbfUdpDecap bbfUdpDecap = new BbfUdpDecap();
bbfUdpDecap.SetTargetPort(1234);
bbfUdpDecap.SetTargetIp(IPAddress.Parse("239.199.2.1"));
bbfUdpDecap.Sink = udpDecapSink;
dumper = bbfUdpDecap;
}
else
{
string changeExtension = Path.ChangeExtension(file.FullName, ".sbbf");
dumper = new BbframeDumper(new FileInfo(changeExtension));
}
FileStream fileStream = file.OpenRead(); FileStream fileStream = file.OpenRead();

View File

@ -310,9 +310,18 @@ namespace skyscraper5
if (args[0].ToLowerInvariant().Equals("pts2bbf")) if (args[0].ToLowerInvariant().Equals("pts2bbf"))
{ {
FileInfo fi = new FileInfo(args[1]); if (args[1].ToLowerInvariant().Equals("bbfudpdecap"))
Pts2Bbf.Run(fi); {
return; FileInfo fi = new FileInfo(args[2]);
Pts2Bbf.Run(fi, true);
return;
}
else
{
FileInfo fi = new FileInfo(args[1]);
Pts2Bbf.Run(fi, false);
return;
}
} }
} }

View File

@ -322,5 +322,16 @@ namespace skyscraper5.Skyscraper.IO
stream.Write(buffer, 0, 8); stream.Write(buffer, 0, 8);
} }
public static byte Peek(this Stream stream)
{
if (!stream.CanSeek)
throw new NotSupportedException("Stream is not peekable");
if (stream.Position == stream.Length)
throw new EndOfStreamException();
byte readUInt8 = stream.ReadUInt8();
stream.Position--;
return readUInt8;
}
} }
} }

View File

@ -13,11 +13,13 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<Deterministic>False</Deterministic> <Deterministic>False</Deterministic>
<Optimize>False</Optimize> <Optimize>False</Optimize>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<Deterministic>False</Deterministic> <Deterministic>False</Deterministic>
<Optimize>False</Optimize> <Optimize>False</Optimize>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>