Exemplo n.º 1
0
        /// <summary>
        ///     Attempts to parse raw data into a structured packet
        /// </summary>
        /// <param name="buffer">Raw data to parse</param>
        /// <param name="packet">Parsed packet</param>
        /// <param name="count">The length of the packet in bytes</param>
        /// <param name="index">The index into the buffer at which the packet begins</param>
        /// <returns>True if parsing was successful, false if it is not.</returns>
        internal static bool TryParse(byte[] buffer, int index, int count, out Radiotap packet)
        {
            try
            {
                using (var ms = new MemoryStream(buffer, index, count, false))
                {
                    using (var br = new BinaryReader(ms))
                    {
                        var version = br.ReadByte();
                        var pad     = br.ReadByte();
                        var length  = br.ReadUInt16();
                        var present = br.ReadUInt32();

                        if (count - br.BaseStream.Position < length - 8)
                        {
                            packet = null;
                            return(false);
                        }
                        var fieldData = br.ReadBytes(length - 8);

                        packet = null;

                        IEEE802_11 payload80211;
                        if (IEEE802_11.TryParse(
                                buffer,
                                index + (int)br.BaseStream.Position,
                                (int)(count - br.BaseStream.Position),
                                out payload80211))
                        {
                            packet = new Radiotap <IEEE802_11> {
                                Payload = payload80211
                            };
                        }

                        if (packet == null)
                        {
                            Generic payload;
                            Generic.TryParse(
                                buffer,
                                index + (int)br.BaseStream.Position,
                                (int)(count - br.BaseStream.Position),
                                out payload);

                            // This can never fail, so I'm not checking the output
                            packet = new Radiotap <Generic> {
                                Payload = payload
                            };
                        }

                        packet.Version        = version;
                        packet.Pad            = pad;
                        packet.LengthRadiotap = length;
                        packet.Present        = present;
                        packet.FieldData      = fieldData;

                        return(true);
                    }
                }
            }
            catch (Exception)
            {
                packet = null;
                return(false);
            }
        }
Exemplo n.º 2
0
 /// <summary>
 ///     Attempts to parse raw data into a structured packet
 /// </summary>
 /// <param name="buffer">Raw data to parse</param>
 /// <param name="packet">Parsed packet</param>
 /// <param name="count">The length of the packet in bytes</param>
 /// <param name="index">The index into the buffer at which the packet begins</param>
 /// <returns>True if parsing was successful, false if it is not.</returns>
 internal static bool TryParse(byte[] buffer, int index, int count, out Generic packet)
 {
     try
     {
         var data = new byte[count];
         Array.Copy(buffer, index, data, 0, count);
         packet = new Generic { Buffer = data };
         return true;
     }
     catch (Exception)
     {
         packet = null;
         return false;
     }
 }
        /// <summary>
        ///     Attempts to parse raw data into a structured packet
        /// </summary>
        /// <param name="buffer">Raw data to parse</param>
        /// <param name="packet">Parsed packet</param>
        /// <param name="count">The length of the packet in bytes</param>
        /// <param name="index">The index into the buffer at which the packet begins</param>
        /// <returns>True if parsing was successful, false if it is not.</returns>
        internal static bool TryParse(byte[] buffer, int index, int count, out EthernetII packet)
        {
            try
            {
                if (count < MinimumParseableBytes)
                {
                    packet = null;
                    return(false);
                }

                using (var ms = new MemoryStream(buffer, index, count, false))
                {
                    using (var br = new BinaryReader(ms))
                    {
                        var dstMac    = br.ReadBytes(6);
                        var srcMac    = br.ReadBytes(6);
                        var etherType = ByteOrder.NetworkToHostOrder(br.ReadUInt16());

                        packet = null;
                        switch (etherType)
                        {
                        case (ushort)EtherTypes.IPv4:
                        {
                            IPv4 payload;
                            if (IPv4.TryParse(
                                    buffer,
                                    index + (int)br.BaseStream.Position,
                                    (int)(count - br.BaseStream.Position),
                                    out payload))
                            {
                                packet = new EthernetII <IPv4> {
                                    Payload = payload
                                };
                            }
                        }
                        break;

                        case (ushort)EtherTypes.ARP:
                        {
                            ARP payload;
                            if (ARP.TryParse(
                                    buffer,
                                    index + (int)br.BaseStream.Position,
                                    (int)(count - br.BaseStream.Position),
                                    out payload))
                            {
                                packet = new EthernetII <ARP> {
                                    Payload = payload
                                };
                            }
                        }
                        break;
                        }

                        if (packet == null)
                        {
                            Generic payload;
                            Generic.TryParse(
                                buffer,
                                index + (int)br.BaseStream.Position,
                                (int)(count - br.BaseStream.Position),
                                out payload);

                            // This can never fail, so I'm not checking the output
                            packet = new EthernetII <Generic> {
                                Payload = payload
                            };
                        }

                        packet.DstMac    = dstMac;
                        packet.EtherType = etherType;
                        packet.SrcMac    = srcMac;

                        return(true);
                    }
                }
            }
            catch (Exception)
            {
                packet = null;
                return(false);
            }
        }