Beispiel #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 ARP packet)
        {
            try
            {
                if (count < MinimumParseableBytes)
                {
                    packet = null;
                    return(false);
                }

                using (var ms = new MemoryStream(buffer, index, count, false))
                {
                    using (var br = new BinaryReader(ms))
                    {
                        packet           = new ARP();
                        packet.HType     = ByteOrder.NetworkToHostOrder(br.ReadUInt16());
                        packet.PType     = ByteOrder.NetworkToHostOrder(br.ReadUInt16());
                        packet.HLen      = br.ReadByte();
                        packet.PLen      = br.ReadByte();
                        packet.Operation = ByteOrder.NetworkToHostOrder(br.ReadUInt16());
                        if (count < 6 + packet.HLen * 2 + packet.PLen * 2)
                        {
                            packet.SenderHardwareAddress = new byte[packet.HLen];
                            packet.SenderProtocolAddress = new byte[packet.PLen];
                            packet.TargetHardwareAddress = new byte[packet.HLen];
                            packet.TargetProtocolAddress = new byte[packet.PLen];
                        }
                        else
                        {
                            packet.SenderHardwareAddress = br.ReadBytes(packet.HLen);
                            packet.SenderProtocolAddress = br.ReadBytes(packet.PLen);
                            packet.TargetHardwareAddress = br.ReadBytes(packet.HLen);
                            packet.TargetProtocolAddress = br.ReadBytes(packet.PLen);
                        }

                        return(true);
                    }
                }
            }
            catch (Exception)
            {
                packet = null;
                return(false);
            }
        }
Beispiel #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 SNAP 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 organizationCode = br.ReadBytes(3);
                        var etherType        = ByteOrder.NetworkToHostOrder(br.ReadUInt16());

                        packet = null;
                        if (organizationCode[0] == 0 && organizationCode[1] == 0 && organizationCode[2] == 0)
                        {
                            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 SNAP <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 SNAP <ARP> {
                                        Payload = payload
                                    };
                                }
                            }
                            break;

                            case (ushort)EtherTypes.EAPoLAN:
                            {
                                IEEE802_1x payload;
                                if (IEEE802_1x.TryParse(
                                        buffer,
                                        index + (int)br.BaseStream.Position,
                                        (int)(count - br.BaseStream.Position),
                                        out payload))
                                {
                                    packet = new SNAP <IEEE802_1x> {
                                        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 SNAP <Generic> {
                                Payload = payload
                            };
                        }

                        packet.OrganizationCode = organizationCode;
                        packet.ProtocolID       = etherType;

                        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 ARP packet)
        {
            try
            {
                if (count < MinimumParseableBytes)
                {
                    packet = null;
                    return false;
                }

                using (var ms = new MemoryStream(buffer, index, count, false))
                {
                    using (var br = new BinaryReader(ms))
                    {
                        packet = new ARP();
                        packet.HType = ByteOrder.NetworkToHostOrder(br.ReadUInt16());
                        packet.PType = ByteOrder.NetworkToHostOrder(br.ReadUInt16());
                        packet.HLen = br.ReadByte();
                        packet.PLen = br.ReadByte();
                        packet.Operation = ByteOrder.NetworkToHostOrder(br.ReadUInt16());
                        if (count < 6 + packet.HLen * 2 + packet.PLen * 2)
                        {
                            packet.SenderHardwareAddress = new byte[packet.HLen];
                            packet.SenderProtocolAddress = new byte[packet.PLen];
                            packet.TargetHardwareAddress = new byte[packet.HLen];
                            packet.TargetProtocolAddress = new byte[packet.PLen];
                        }
                        else
                        {
                            packet.SenderHardwareAddress = br.ReadBytes(packet.HLen);
                            packet.SenderProtocolAddress = br.ReadBytes(packet.PLen);
                            packet.TargetHardwareAddress = br.ReadBytes(packet.HLen);
                            packet.TargetProtocolAddress = br.ReadBytes(packet.PLen);
                        }

                        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);
            }
        }