|
| 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 | + } |
0 commit comments