Exemplo n.º 1
0
        private void AddOption(DHCPv4PacketOption option)
        {
            _options.Add(option);

            if (option is DHCPv4PacketMessageTypeOption option1)
            {
                MessageType = option1.Value;
            }
        }
Exemplo n.º 2
0
        public DHCPv4PacketOption GetOptionByIdentifier(byte optionIdentifier)
        {
            DHCPv4PacketOption option = _options.FirstOrDefault(x => x.OptionType == optionIdentifier);

            if (option == null)
            {
                return(DHCPv4PacketOption.NotPresented);
            }

            return(option);
        }
Exemplo n.º 3
0
        public static DHCPv4Packet FromByteArray(Byte[] rawData, IPv4Address source, IPv4Address destionation)
        {
            try
            {
                DHCPv4Packet packet = new DHCPv4Packet
                {
                    OpCode                = (DHCPv4PacketOperationCodes)rawData[0],
                    HardwareType          = (DHCPv4PacketHardwareAddressTypes)rawData[1],
                    HardwareAddressLength = rawData[2],
                    Hops            = rawData[3],
                    TransactionId   = ByteHelper.ConvertToUInt32FromByte(rawData, 4),
                    SecondsElapsed  = ByteHelper.ConvertToUInt16FromByte(rawData, 8),
                    Flags           = (DHCPv4PacketFlags)ByteHelper.ConvertToUInt16FromByte(rawData, 10),
                    ClientIPAdress  = IPv4Address.FromByteArray(rawData, 12),
                    YourIPAdress    = IPv4Address.FromByteArray(rawData, 16),
                    ServerIPAdress  = IPv4Address.FromByteArray(rawData, 20),
                    GatewayIPAdress = IPv4Address.FromByteArray(rawData, 24)
                };

                packet.Header = new IPv4HeaderInformation(source, destionation);

                packet.ClientHardwareAddress = ByteHelper.CopyData(rawData, 28, packet.HardwareAddressLength);
                Boolean serverhostnameFound = false;
                for (int i = 40; i < 40 + 64; i++)
                {
                    if (rawData[i] != 0)
                    {
                        serverhostnameFound = true;
                        break;
                    }
                }
                if (serverhostnameFound == true)
                {
                    packet.ServerHostname = new String(_textEncoding.GetChars(rawData, 40, 64));
                }
                else
                {
                    packet.ServerHostname = String.Empty;
                }

                Boolean fileNameFound = false;
                for (int i = 104; i < 104 + 128; i++)
                {
                    if (rawData[i] != 0)
                    {
                        fileNameFound = true;
                        break;
                    }
                }
                if (fileNameFound == true)
                {
                    packet.FileName = new String(_textEncoding.GetChars(rawData, 104, 128));
                }
                else
                {
                    packet.FileName = String.Empty;
                }

                Byte[] actualCookie = new Byte[] { rawData[236], rawData[237], rawData[238], rawData[239] };
                if (ByteHelper.AreEqual(_magicCookie, actualCookie) == false)
                {
                    throw new ArgumentException();
                }

                Int32 index = 240;
                while (index < rawData.Length)
                {
                    Byte optionCode = rawData[index];
                    if (optionCode == 255)
                    {
                        break;
                    }
                    Byte length = rawData[index + 1];

                    Byte[]             optionData = ByteHelper.CopyData(rawData, index, length + 2);
                    DHCPv4PacketOption option     = DHCPv4PacketOptionFactory.GetOption(optionCode, optionData);

                    packet.AddOption(option);


                    index += 2 + length;
                }

                packet._isValid = true;

                packet.SetClientIdentifier();

                packet._byteRepresentation = rawData;

                return(packet);
            }
            catch (Exception)
            {
                return(DHCPv4Packet.Invalid);
            }
        }