Ejemplo n.º 1
0
 public DHCPPacket()
 {
     IPServer      = GetCurrentIP();
     OperationCode = DHCPOperationCode.ServerToClient;
     HardwareType  = DHCPHardwareType.Ethernet10;
     Flags         = 0;
 }
Ejemplo n.º 2
0
        public DHCPPacket(byte[] data)
        {
            DHCPHdr = DecodeBytes(data);

            if (DHCPHdr.Magic != 0x63538263)
            {
                Malformed = true;
                return;
            }

            if (DHCPHdr.MACLength > 16)
            {
                Malformed = true;
                return;
            }

            for (int i = 0xF0; i < data.Length;)
            {
                if (data[i] == 0xFF)
                {
                    break;
                }

                if (data.Length - i < 2)
                {
                    Malformed = true;
                    break;
                }

                DHCPOption o = new DHCPOption();
                o.Type = data[i + 0];
                o.Size = data[i + 1];

                if (o.Type == 0 || o.Size == 0)
                {
                    Malformed = true;
                    break;
                }

                if (data.Length - (i + o.Size) < 0)
                {
                    Malformed = true;
                    break;
                }

                o.Data = new byte[o.Size];
                Buffer.BlockCopy(data, i + 2, o.Data, 0, o.Size);

                DHCPOptions.Add(o);

                i += 2 + o.Size;
            }

            foreach (DHCPOption o in DHCPOptions)
            {
                switch (o.Type)
                {
                case 97:
                    if (o.Size != 17)
                    {
                        Malformed = true;
                        continue;
                    }
                    DHCP97ClientUUID = BytesToString(o.Data, 1);
                    break;

                case 61:
                    //if (o.Size != 17)
                    //{
                    //    Malformed = true;
                    //    continue;
                    //}
                    DHCP61ClientGUID = BytesToString(o.Data, 1);
                    break;

                case 55:
                    DHCP9ReqParameterList = new List <byte>();
                    foreach (byte b in o.Data)
                    {
                        if (b == 0 || b == 0xff)
                        {
                            Malformed = true;
                        }
                        if (DHCP9ReqParameterList.Contains(b) == false)
                        {
                            DHCP9ReqParameterList.Add(b);
                        }
                    }
                    break;

                case 53:
                    if (o.Size != 1)
                    {
                        Malformed = true;
                        continue;
                    }
                    DHCP53MessageType = (DHCPMessageType)o.Data[0];
                    break;

                case 57:
                    if (o.Size != 2)
                    {
                        Malformed = true;
                        continue;
                    }
                    DHCP57MessageLength = BitConverter.ToUInt16(o.Data.Reverse().ToArray(), 0);
                    break;

                case 60:
                    if (o.Size != 32)
                    {
                        Malformed = true;
                        continue;
                    }
                    DHCP60ClassIdentifier = Encoding.ASCII.GetString(o.Data);
                    break;

                case 66:
                    DHCP66BootServer = Encoding.ASCII.GetString(o.Data).NullTrim();
                    break;

                case 67:
                    DHCP67BootFilename = Encoding.ASCII.GetString(o.Data).NullTrim();
                    break;

                case 93:
                    if (o.Size != 2)
                    {
                        Malformed = true;
                        continue;
                    }
                    DHCP93Architecture = (DHCPArchitecture)BitConverter.ToUInt16(o.Data.Reverse().ToArray(), 0);
                    break;

                case 94:
                    if (o.Size != 3)
                    {
                        Malformed = true;
                        continue;
                    }
                    if (o.Data[0] == 1)
                    {
                        DHCP94ClientNIC = "UNDI.";
                    }
                    else
                    {
                        DHCP94ClientNIC = "UNKN.";
                    }
                    DHCP94ClientNIC += o.Data[1].ToString("0") + "." + o.Data[2].ToString("0");
                    break;

                default:
                    Debug.WriteLine("Unknown code: " + o.Type.ToString() + " (0x" + o.Type.ToString("X2") + ")");
                    break;
                }
            }

            Flags         = DHCPHdr.Flags;
            OperationCode = DHCPHdr.OperationCode;
            HardwareType  = DHCPHdr.HardwareType;
            XID           = DHCPHdr.XID;
            IPClient      = new IPAddress(DHCPHdr.CIAddr);
            IPYours       = new IPAddress(DHCPHdr.YIAddr);
            IPServer      = new IPAddress(DHCPHdr.SIAddr);
            IPGateway     = new IPAddress(DHCPHdr.GIAddr);
            MacAddress    = new byte[DHCPHdr.MACLength];
            Buffer.BlockCopy(DHCPHdr.CHAddr, 0, MacAddress, 0, DHCPHdr.MACLength);
            Servername = Encoding.ASCII.GetString(DHCPHdr.SName).NullTrim();
            BootFile   = Encoding.ASCII.GetString(DHCPHdr.File).NullTrim();
        }