Skip to content

Commit 5eaece5

Browse files
committed
Merge branch 'master' of https://github.com/dotpcap/packetnet
2 parents 972b69d + 99d680f commit 5eaece5

11 files changed

Lines changed: 510 additions & 6 deletions

File tree

.github/dependabot.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# To get started with Dependabot version updates, you'll need to specify which
2+
# package ecosystems to update and where the package manifests are located.
3+
# Please see the documentation for all configuration options:
4+
# https://docs.github.com/code-security/dependabot/dependabot-version-updates/configuration-options-for-the-dependabot.yml-file
5+
6+
version: 2
7+
updates:
8+
- package-ecosystem: "nuget" # See documentation for possible values
9+
directory: "/" # Location of package manifests
10+
schedule:
11+
interval: "daily"

PacketDotNet/LinkLayers.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,5 +162,8 @@ public enum LinkLayers : ushort
162162
IPv6 = 229,
163163

164164
/// <summary>Protocol for communication between host and guest machines in VMware and KVM hypervisors.</summary>
165-
VSock = 271
166-
}
165+
VSock = 271,
166+
167+
/// <summary>Linux "cooked" capture encapsulation v2.</summary>
168+
LinuxSll2 = 276,
169+
}

PacketDotNet/LinuxSll2Fields.cs

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
This file is part of PacketDotNet.
3+
4+
This Source Code Form is subject to the terms of the Mozilla Public
5+
License, v. 2.0. If a copy of the MPL was not distributed with this
6+
file, You can obtain one at https://mozilla.org/MPL/2.0/.
7+
*/
8+
9+
namespace PacketDotNet;
10+
11+
/// <summary>
12+
/// Lengths and offsets to the fields in the LinuxSll2 packet
13+
/// See http://github.com/mcr/libpcap/blob/master/pcap/sll.h
14+
/// </summary>
15+
public struct LinuxSll2Fields
16+
{
17+
/// <summary>
18+
/// Length of the ethernet protocol field
19+
/// </summary>
20+
public static readonly int EthernetProtocolTypeLength = 2;
21+
22+
/// <summary>
23+
/// Position of the ethernet protocol type field
24+
/// </summary>
25+
public static readonly int EthernetProtocolTypePosition = 0;
26+
27+
/// <summary>
28+
/// Link layer address length
29+
/// </summary>
30+
public static readonly int LinkLayerAddressLengthLength = 1;
31+
32+
/// <summary>
33+
/// Positino of the link layer address length field
34+
/// </summary>
35+
public static readonly int LinkLayerAddressLengthPosition;
36+
37+
/// <summary>
38+
/// The link layer address field length
39+
/// NOTE: the actual link layer address MAY be shorter than this
40+
/// </summary>
41+
public static readonly int LinkLayerAddressMaximumLength = 8;
42+
43+
/// <summary>
44+
/// Position of the link layer address field
45+
/// </summary>
46+
public static readonly int LinkLayerAddressPosition;
47+
48+
/// <summary>
49+
/// Link layer address type
50+
/// </summary>
51+
public static readonly int LinkLayerAddressTypeLength = 2;
52+
53+
/// <summary>
54+
/// Position of the link layer address type field
55+
/// </summary>
56+
public static readonly int LinkLayerAddressTypePosition;
57+
58+
/// <summary>
59+
/// Length of the packet type field
60+
/// </summary>
61+
public static readonly int PacketTypeLength = 1;
62+
63+
/// <summary>
64+
/// Position of the packet type field
65+
/// </summary>
66+
public static readonly int PacketTypePosition;
67+
68+
/// <summary>
69+
/// Reserved (MBZ)
70+
/// </summary>
71+
public static readonly int ReservedMBZLength = 2;
72+
73+
/// <summary>
74+
/// Position of the Reserved (MBZ) field
75+
/// </summary>
76+
public static readonly int ReservedMBZPosition = 0;
77+
78+
/// <summary>
79+
/// Length of the interface index field
80+
/// </summary>
81+
public static readonly int InterfaceIndexLength = 4;
82+
83+
/// <summary>
84+
/// Position of the interface index field
85+
/// </summary>
86+
public static readonly int InterfaceIndexPosition = 0;
87+
88+
/// <summary>
89+
/// Number of bytes in a SLL2 header
90+
/// </summary>
91+
public static readonly int SLL2HeaderLength = 20;
92+
93+
static LinuxSll2Fields()
94+
{
95+
ReservedMBZPosition = EthernetProtocolTypePosition + EthernetProtocolTypeLength;
96+
InterfaceIndexPosition = ReservedMBZPosition + ReservedMBZLength;
97+
LinkLayerAddressTypePosition = InterfaceIndexPosition + InterfaceIndexLength;
98+
PacketTypePosition = LinkLayerAddressTypePosition + LinkLayerAddressTypeLength;
99+
LinkLayerAddressLengthPosition = PacketTypePosition + PacketTypeLength;
100+
LinkLayerAddressPosition = LinkLayerAddressLengthPosition + LinkLayerAddressLengthLength;
101+
}
102+
}

PacketDotNet/LinuxSll2Packet.cs

Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
/*
2+
This file is part of PacketDotNet.
3+
4+
This Source Code Form is subject to the terms of the Mozilla Public
5+
License, v. 2.0. If a copy of the MPL was not distributed with this
6+
file, You can obtain one at https://mozilla.org/MPL/2.0/.
7+
*/
8+
9+
using System;
10+
using System.Collections.Generic;
11+
using System.Text;
12+
using PacketDotNet.Utils;
13+
using PacketDotNet.Utils.Converters;
14+
15+
namespace PacketDotNet;
16+
17+
/// <summary>
18+
/// Represents a Linux cooked capture packet, the kinds of packets
19+
/// received when capturing on an 'any' device
20+
/// See http://github.com/mcr/libpcap/blob/master/pcap/sll.h
21+
/// </summary>
22+
public class LinuxSll2Packet : InternetLinkLayerPacket
23+
{
24+
/// <summary>
25+
/// Constructor
26+
/// </summary>
27+
/// <param name="byteArraySegment">
28+
/// A <see cref="ByteArraySegment" />
29+
/// </param>
30+
public LinuxSll2Packet(ByteArraySegment byteArraySegment)
31+
{
32+
Header = new ByteArraySegment(byteArraySegment) { Length = LinuxSll2Fields.SLL2HeaderLength };
33+
34+
// parse the payload via an EthernetPacket method
35+
PayloadPacketOrData = new LazySlim<PacketOrByteArraySegment>(() => EthernetPacket.ParseNextSegment(Header, EthernetProtocolType));
36+
}
37+
38+
/// <value>
39+
/// The encapsulated protocol type
40+
/// </value>
41+
public EthernetType EthernetProtocolType
42+
{
43+
get => (EthernetType) EndianBitConverter.Big.ToInt16(Header.Bytes,
44+
Header.Offset + LinuxSll2Fields.EthernetProtocolTypePosition);
45+
set
46+
{
47+
var v = (short) value;
48+
EndianBitConverter.Big.CopyBytes(v,
49+
Header.Bytes,
50+
Header.Offset + LinuxSll2Fields.EthernetProtocolTypePosition);
51+
}
52+
}
53+
54+
/// <value>
55+
/// Link layer header bytes, maximum of 8 bytes
56+
/// </value>
57+
public byte[] LinkLayerAddress
58+
{
59+
get
60+
{
61+
var headerLength = LinkLayerAddressLength;
62+
var theHeader = new byte[headerLength];
63+
64+
Array.Copy(Header.Bytes,
65+
Header.Offset + LinuxSll2Fields.LinkLayerAddressPosition,
66+
theHeader,
67+
0,
68+
headerLength);
69+
70+
return theHeader;
71+
}
72+
set
73+
{
74+
// update the link layer length
75+
LinkLayerAddressLength = value.Length;
76+
77+
// copy in the new link layer header bytes
78+
Array.Copy(value,
79+
0,
80+
Header.Bytes,
81+
Header.Offset + LinuxSll2Fields.LinkLayerAddressPosition,
82+
value.Length);
83+
}
84+
}
85+
86+
/// <value>
87+
/// Number of bytes in the link layer address of the sender of the packet
88+
/// </value>
89+
public int LinkLayerAddressLength
90+
{
91+
get => Header.Bytes[Header.Offset + LinuxSll2Fields.LinkLayerAddressLengthPosition];
92+
set
93+
{
94+
// range check
95+
if (value is < 0 or > 8)
96+
{
97+
throw new InvalidOperationException("value of " + value + " out of range of 0 to 8");
98+
}
99+
100+
Header.Bytes[Header.Offset + LinuxSll2Fields.LinkLayerAddressLengthPosition] = (byte) value;
101+
}
102+
}
103+
104+
/// <value>
105+
/// The
106+
/// </value>
107+
public int LinkLayerAddressType
108+
{
109+
get => EndianBitConverter.Big.ToInt16(Header.Bytes,
110+
Header.Offset + LinuxSll2Fields.LinkLayerAddressTypePosition);
111+
set
112+
{
113+
var v = (short) value;
114+
EndianBitConverter.Big.CopyBytes(v,
115+
Header.Bytes,
116+
Header.Offset + LinuxSll2Fields.LinkLayerAddressTypePosition);
117+
}
118+
}
119+
120+
/// <value>
121+
/// Information about the packet direction
122+
/// </value>
123+
public LinuxSll2Type Type
124+
{
125+
get => (LinuxSll2Type)Header.Bytes[Header.Offset + LinuxSll2Fields.PacketTypePosition];
126+
set
127+
{
128+
Header.Bytes[Header.Offset + LinuxSll2Fields.PacketTypePosition] = (byte) value;
129+
}
130+
}
131+
132+
/// <value>
133+
/// Information about the interface index
134+
/// </value>
135+
public int InterfaceIndex
136+
{
137+
get => EndianBitConverter.Big.ToInt32(Header.Bytes,
138+
Header.Offset + LinuxSll2Fields.InterfaceIndexPosition);
139+
set
140+
{
141+
var v = (int)value;
142+
EndianBitConverter.Big.CopyBytes(v,
143+
Header.Bytes,
144+
Header.Offset + LinuxSll2Fields.InterfaceIndexPosition);
145+
}
146+
}
147+
148+
/// <inheritdoc cref="Packet.ToString(StringOutputType)" />
149+
public override string ToString(StringOutputType outputFormat)
150+
{
151+
var buffer = new StringBuilder();
152+
var color = "";
153+
var colorEscape = "";
154+
155+
if (outputFormat is StringOutputType.Colored or StringOutputType.VerboseColored)
156+
{
157+
color = Color;
158+
colorEscape = AnsiEscapeSequences.Reset;
159+
}
160+
161+
if (outputFormat is StringOutputType.Normal or StringOutputType.Colored)
162+
{
163+
// build the output string
164+
buffer.AppendFormat("[{0}LinuxSll2Packet{1}: ProtocolType={2}, InterfaceIndex={3}, LinkLayerAddressType={4}, Type={5}, LinkLayerAddressLength={6}, Source={7}]",
165+
color,
166+
colorEscape,
167+
EthernetProtocolType,
168+
InterfaceIndex,
169+
LinkLayerAddressType,
170+
Type,
171+
LinkLayerAddressLength,
172+
BitConverter.ToString(LinkLayerAddress, 0));
173+
}
174+
175+
if (outputFormat is StringOutputType.Verbose or StringOutputType.VerboseColored)
176+
{
177+
// collect the properties and their value
178+
var properties = new Dictionary<string, string>
179+
{
180+
{ "protocol", EthernetProtocolType + " (0x" + EthernetProtocolType.ToString("x") + ")" },
181+
{ "interface index", InterfaceIndex.ToString() },
182+
{ "link layer address type", LinkLayerAddressType.ToString() },
183+
{ "type", Type + " (" + (int) Type + ")" },
184+
{ "link layer address length", LinkLayerAddressLength.ToString() },
185+
{ "source", BitConverter.ToString(LinkLayerAddress) },
186+
};
187+
188+
// calculate the padding needed to right-justify the property names
189+
var padLength = RandomUtils.LongestStringLength(new List<string>(properties.Keys));
190+
191+
// build the output string
192+
buffer.AppendLine("LCC: ******* LinuxSll2 - \"Linux Cooked Capture v2\" - offset=? length=" + TotalPacketLength);
193+
buffer.AppendLine("LCC:");
194+
foreach (var property in properties)
195+
{
196+
buffer.AppendLine("LCC: " + property.Key.PadLeft(padLength) + " = " + property.Value);
197+
}
198+
199+
buffer.AppendLine("LCC:");
200+
}
201+
202+
// append the base output
203+
buffer.Append(base.ToString(outputFormat));
204+
205+
return buffer.ToString();
206+
}
207+
}

PacketDotNet/LinuxSll2Type.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
This file is part of PacketDotNet.
3+
4+
This Source Code Form is subject to the terms of the Mozilla Public
5+
License, v. 2.0. If a copy of the MPL was not distributed with this
6+
file, You can obtain one at https://mozilla.org/MPL/2.0/.
7+
*/
8+
9+
namespace PacketDotNet;
10+
11+
/// <summary>
12+
/// The types of cooked packets v2
13+
/// See http://github.com/mcr/libpcap/blob/master/pcap/sll.h
14+
/// </summary>
15+
public enum LinuxSll2Type
16+
{
17+
/// <summary>
18+
/// Packet was sent to us by somebody else
19+
/// </summary>
20+
PacketSentToUs = 0x0,
21+
22+
/// <summary>
23+
/// Packet was broadcast by somebody else
24+
/// </summary>
25+
PacketBroadCast = 0x1,
26+
27+
/// <summary>
28+
/// Packet was multicast, but not broadcast
29+
/// </summary>
30+
PacketMulticast = 0x2,
31+
32+
/// <summary>
33+
/// Packet was sent by somebody else to somebody else
34+
/// </summary>
35+
PacketSentToSomeoneElse = 0x3,
36+
37+
/// <summary>
38+
/// Packet was sent by us
39+
/// </summary>
40+
PacketSentByUs = 0x4
41+
}

PacketDotNet/Packet.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,11 @@ public static Packet ParsePacket(LinkLayers linkLayers, byte[] packetData)
339339
p = new LinuxSllPacket(byteArraySegment);
340340
break;
341341
}
342+
case LinkLayers.LinuxSll2:
343+
{
344+
p = new LinuxSll2Packet(byteArraySegment);
345+
break;
346+
}
342347
case LinkLayers.Null:
343348
{
344349
p = new NullPacket(byteArraySegment);

PacketDotNet/RtcpPacket.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public bool HasPadding
117117
public int ReceptionReportCount
118118
{
119119
get => (Header.Bytes[Header.Offset] & RtcpFields.ReceptionReportCountMask);
120-
set => Header.Bytes[Header.Offset] |= (byte) (value & RtcpFields.ReceptionReportCountMask);
120+
set => Header.Bytes[Header.Offset] = (byte) ((Header.Bytes[Header.Offset] & ~RtcpFields.ReceptionReportCountMask) | (value & RtcpFields.ReceptionReportCountMask));
121121
}
122122

123123
/// <summary>

0 commit comments

Comments
 (0)