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

                // Too many bytes
                if (count > 108)
                {
                    packet = null;
                    return false;
                }

                using (var ms = new MemoryStream(buffer, index, count, false))
                {
                    using (var br = new BinaryReader(ms))
                    {
                        for (var i = 0; i < 6; i++)
                        {
                            if (br.ReadByte() != 255)
                            {
                                // TODO: Option to ignore
                                // Invalid synchronization stream.
                                packet = null;
                                return false;
                            }
                        }

                        packet = new WakeOnLan();
                        packet.DstMac = br.ReadBytes(6);
                        for (var i = 0; i < 15; i++)
                        {
                            var tmpMac = br.ReadBytes(6);
                            if (!tmpMac.SequenceEqual(packet.DstMac))
                            {
                                // TODO: Option to ignore
                                // Invalid target mac repetition
                                packet = null;
                                return false;
                            }
                        }

                        if (count - br.BaseStream.Position >= 6)
                        {
                            packet.Password = br.ReadBytes(6);
                        }
                        else if (count - br.BaseStream.Position >= 4)
                        {
                            packet.Password = br.ReadBytes(4);
                        }
                        else
                        {
                            packet.Password = Array.Empty<byte>();
                        }

                        return true;
                    }
                }
            }
            catch (Exception)
            {
                packet = null;
                return false;
            }
        }
Esempio 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 WakeOnLan packet)
        {
            try
            {
                if (count < MinimumParseableBytes)
                {
                    packet = null;
                    return(false);
                }

                // Too many bytes
                if (count > 108)
                {
                    packet = null;
                    return(false);
                }

                using (var ms = new MemoryStream(buffer, index, count, false))
                {
                    using (var br = new BinaryReader(ms))
                    {
                        for (var i = 0; i < 6; i++)
                        {
                            if (br.ReadByte() != 255)
                            {
                                // TODO: Option to ignore
                                // Invalid synchronization stream.
                                packet = null;
                                return(false);
                            }
                        }

                        packet        = new WakeOnLan();
                        packet.DstMac = br.ReadBytes(6);
                        for (var i = 0; i < 15; i++)
                        {
                            var tmpMac = br.ReadBytes(6);
                            if (!tmpMac.SequenceEqual(packet.DstMac))
                            {
                                // TODO: Option to ignore
                                // Invalid target mac repetition
                                packet = null;
                                return(false);
                            }
                        }

                        if (count - br.BaseStream.Position >= 6)
                        {
                            packet.Password = br.ReadBytes(6);
                        }
                        else if (count - br.BaseStream.Position >= 4)
                        {
                            packet.Password = br.ReadBytes(4);
                        }
                        else
                        {
                            packet.Password = Array.Empty <byte>();
                        }

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