/// <summary>
        /// Returns option content
        /// </summary>
        /// <param name="option">Option to retrieve</param>
        /// <returns>Option content</returns>
        public byte[] GetOptionData(DHCPOption option)
        {
            int  DHCPId = 0;
            byte DDataID, DataLength = 0;

            byte[] dumpData;

            DHCPId = (int)option;
            for (int i = 0; i < requestData.options.Length; i++)
            {
                DDataID = requestData.options[i];
                if (DDataID == (byte)DHCPOption.END_Option)
                {
                    break;
                }
                if (DDataID == DHCPId)
                {
                    DataLength = requestData.options[i + 1];
                    dumpData   = new byte[DataLength];
                    Array.Copy(requestData.options, i + 2, dumpData, 0, DataLength);
                    return(dumpData);
                }
                else if (DDataID == 0)
                {
                }
                else
                {
                    DataLength = requestData.options[i + 1];
                    i         += 1 + DataLength;
                }
            }

            return(null);
        }
Exemple #2
0
        public virtual bool isMessageOfType(UDP udp, IPv4 ipv4, int messageType)
        {
            if (opcode != DHCP_BOOT_REQUEST)
            {
                return(false);
            }
            if (udp.sourcePort != UDP_PORT_DHCP_CLIENT || udp.destinationPort != UDP_PORT_DHCP_SERVER)
            {
                return(false);
            }
            if (!Array.Equals(ipv4.sourceIPAddress, nullIPAddress))
            {
                return(false);
            }
            if (!Array.Equals(ipv4.destinationIPAddress, broadcastIPAddress))
            {
                return(false);
            }
            DHCPOption option = getOptionByTag(DHCP_OPTION_MESSAGE_TYPE);

            if (option == null || option.Length != 1)
            {
                return(false);
            }
            int optionMessageType = option.DataAsInt;

            if (optionMessageType != messageType)
            {
                return(false);
            }

            return(true);
        }
Exemple #3
0
        private static void CreateOptionElement(ref byte[] options, DHCPOption option, byte[] data)
        {
            byte[] optionData;

            optionData    = new byte[data.Length + 2];
            optionData[0] = (byte)option;
            optionData[1] = (byte)data.Length;
            Array.Copy(data, 0, optionData, 2, data.Length);
            if (options == null)
            {
                Array.Resize(ref options, optionData.Length);
            }
            else
            {
                Array.Resize(ref options, options.Length + optionData.Length);
            }
            Array.Copy(optionData, 0, options, options.Length - optionData.Length, optionData.Length);
        }
Exemple #4
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public void read(NetPacket packet) throws java.io.EOFException
        public virtual void read(NetPacket packet)
        {
            opcode = packet.read8();
            hardwareAddressType   = packet.read8();
            hardwareAddressLength = packet.read8();
            hops                  = packet.read8();
            transactionID         = packet.read32();
            seconds               = packet.read16();
            flagBroadcast         = packet.readBoolean();
            flagsZero             = packet.readBits(15);
            clientIPAddress       = packet.readBytes(IP_ADDRESS_LENGTH);
            yourIPAddress         = packet.readBytes(IP_ADDRESS_LENGTH);
            nextServerIPAddress   = packet.readBytes(IP_ADDRESS_LENGTH);
            relayAgentIPAddress   = packet.readBytes(IP_ADDRESS_LENGTH);
            clientHardwareAddress = packet.readBytes(16);
            serverHostName        = packet.readStringNZ(64);
            bootFileName          = packet.readStringNZ(128);

            int optionsLength = 312;
            int magicCookie   = packet.read32();

            optionsLength -= 4;
            if (magicCookie == DHCP_OPTION_MAGIC_COOKIE)
            {
                while (optionsLength > 0)
                {
                    DHCPOption option = new DHCPOption();
                    option.read(packet);
                    options.Add(option);
                    optionsLength -= option.sizeOf();

                    // END tag marks the end of the options
                    if (option.tag == DHCP_OPTION_END)
                    {
                        break;
                    }
                }
            }
            else
            {
                packet.skip8(optionsLength);
            }
        }
Exemple #5
0
        public virtual bool isRequest(UDP udp, IPv4 ipv4, sbyte[] requestedIpAddress)
        {
            if (!isMessageOfType(udp, ipv4, DHCP_OPTION_MESSAGE_TYPE_DHCPREQUEST))
            {
                return(false);
            }

            // Verify that the requested IP address is matching
            // the one specified in the options.
            DHCPOption requestedIpAddressOption = getOptionByTag(DHCP_OPTION_REQUESTED_IP_ADDRESS);

            if (requestedIpAddressOption == null || requestedIpAddressOption.Length != 4)
            {
                return(false);
            }
            if (!Array.Equals(requestedIpAddress, requestedIpAddressOption.data))
            {
                return(false);
            }

            return(true);
        }
        private byte[] CreateOptionStruct(DHCPMsgType msgType, DHCPReplyOptions replyOptions, Dictionary <DHCPOption, byte[]> otherForceOptions, IEnumerable <DHCPOption> forceOptions)
        {
            Dictionary <DHCPOption, byte[]> options = new Dictionary <DHCPOption, byte[]>();

            byte[] resultOptions = null;
            // Requested options
            var reqList = GetRequestedOptionsList();
            // Option82?
            var relayInfo = this.GetOptionData(DHCPOption.RelayInfo);

            CreateOptionElement(ref resultOptions, DHCPOption.DHCPMessageTYPE, new byte[] { (byte)msgType });
            // Server identifier - our IP address
            if ((replyOptions != null) && (replyOptions.ServerIdentifier != null))
            {
                options[DHCPOption.ServerIdentifier] = replyOptions.ServerIdentifier.GetAddressBytes();
            }

            if (reqList == null && forceOptions != null)
            {
                reqList = new DHCPOption[0];
            }

            // Requested options
            if ((reqList != null) && (replyOptions != null))
            {
                foreach (DHCPOption i in reqList.Union(forceOptions).Distinct().OrderBy(x => (int)x))
                {
                    byte[] optionData = null;
                    // If it's force option - ignore it. We'll send it later.
                    if ((otherForceOptions != null) && (otherForceOptions.TryGetValue(i, out optionData)))
                    {
                        continue;
                    }
                    switch (i)
                    {
                    case DHCPOption.SubnetMask:
                        if (replyOptions.SubnetMask != null)
                        {
                            optionData = replyOptions.SubnetMask.GetAddressBytes();
                        }
                        break;

                    case DHCPOption.Router:
                        if (replyOptions.RouterIP != null)
                        {
                            optionData = replyOptions.RouterIP.GetAddressBytes();
                        }
                        break;

                    case DHCPOption.DomainNameServers:
                        if (replyOptions.DomainNameServers != null)
                        {
                            optionData = new byte[] { };
                            foreach (var dns in replyOptions.DomainNameServers)
                            {
                                var dnsserv = dns.GetAddressBytes();
                                Array.Resize(ref optionData, optionData.Length + 4);
                                Array.Copy(dnsserv, 0, optionData, optionData.Length - 4, 4);
                            }
                        }
                        break;

                    case DHCPOption.DomainName:
                        if (!string.IsNullOrEmpty(replyOptions.DomainName))
                        {
                            optionData = System.Text.Encoding.ASCII.GetBytes(replyOptions.DomainName);
                        }
                        break;

                    case DHCPOption.ServerIdentifier:
                        if (replyOptions.ServerIdentifier != null)
                        {
                            optionData = replyOptions.ServerIdentifier.GetAddressBytes();
                        }
                        break;

                    case DHCPOption.LogServer:
                        if (replyOptions.LogServerIP != null)
                        {
                            optionData = replyOptions.LogServerIP.GetAddressBytes();
                        }
                        break;

                    case DHCPOption.StaticRoutes:
                    case DHCPOption.StaticRoutesWin:
                        if (replyOptions.StaticRoutes != null)
                        {
                            optionData = new byte[] { };
                            foreach (var route in replyOptions.StaticRoutes)
                            {
                                var routeData = route.BuildRouteData();
                                Array.Resize(ref optionData, optionData.Length + routeData.Length);
                                Array.Copy(routeData, 0, optionData, optionData.Length - routeData.Length, routeData.Length);
                            }
                        }
                        break;

                    default:
                        replyOptions.OtherRequestedOptions.TryGetValue(i, out optionData);
                        break;
                    }
                    if (optionData != null)
                    {
                        options[i] = optionData;
                    }
                }
            }

            if (GetMsgType() != DHCPMsgType.DHCPINFORM)
            {
                // Lease time
                if (replyOptions != null)
                {
                    var leaseTime = new byte[4];
                    leaseTime[3] = (byte)(replyOptions.IPAddressLeaseTime);
                    leaseTime[2] = (byte)(replyOptions.IPAddressLeaseTime >> 8);
                    leaseTime[1] = (byte)(replyOptions.IPAddressLeaseTime >> 16);
                    leaseTime[0] = (byte)(replyOptions.IPAddressLeaseTime >> 24);
                    options[DHCPOption.IPAddressLeaseTime] = leaseTime;
                    if (replyOptions.RenewalTimeValue_T1.HasValue)
                    {
                        leaseTime[3] = (byte)(replyOptions.RenewalTimeValue_T1);
                        leaseTime[2] = (byte)(replyOptions.RenewalTimeValue_T1 >> 8);
                        leaseTime[1] = (byte)(replyOptions.RenewalTimeValue_T1 >> 16);
                        leaseTime[0] = (byte)(replyOptions.RenewalTimeValue_T1 >> 24);
                        options[DHCPOption.RenewalTimeValue_T1] = leaseTime;
                    }
                    if (replyOptions.RebindingTimeValue_T2.HasValue)
                    {
                        leaseTime[3] = (byte)(replyOptions.RebindingTimeValue_T2);
                        leaseTime[2] = (byte)(replyOptions.RebindingTimeValue_T2 >> 8);
                        leaseTime[1] = (byte)(replyOptions.RebindingTimeValue_T2 >> 16);
                        leaseTime[0] = (byte)(replyOptions.RebindingTimeValue_T2 >> 24);
                        options[DHCPOption.RebindingTimeValue_T2] = leaseTime;
                    }
                }
            }
            // Other requested options
            if (otherForceOptions != null)
            {
                foreach (var option in otherForceOptions.Keys)
                {
                    options[option] = otherForceOptions[option];
                    if (option == DHCPOption.RelayInfo)
                    {
                        relayInfo = null;
                    }
                }
            }

            // Option 82? Send it back!
            if (relayInfo != null)
            {
                options[DHCPOption.RelayInfo] = relayInfo;
            }

            foreach (var option in options.OrderBy(x => (int)x.Key))
            {
                CreateOptionElement(ref resultOptions, option.Key, option.Value);
            }

            // Create the end option
            Array.Resize(ref resultOptions, resultOptions.Length + 1);
            Array.Copy(new byte[] { 255 }, 0, resultOptions, resultOptions.Length - 1, 1);
            return(resultOptions);
        }
Exemple #7
0
        private byte[] GetOptionData(DHCPOption type, DHCPTransaction transaction)
        {
            // Pass the option type that you require
            // Parse the option data
            // Return the data in a byte of what we need

            int id = 0;
            byte dataId = 0;
            byte dataLength = 0;
            byte[] dataDump;

            try
            {
                id = (int)type;
                //loop through look for the bit that states that the identifier is there
                for (int i = 0; i < transaction.Message.D_options.Length; i++)
                {
                    //at the start we have the code + length
                    //i has the code, i+1 = length of data, i+1+n = data skip
                    dataId = transaction.Message.D_options[i];
                    if (dataId == id)
                    {
                        dataLength = transaction.Message.D_options[i + 1];
                        dataDump = new byte[dataLength];
                        Array.Copy(transaction.Message.D_options, i + 2, dataDump, 0, dataLength);
                        return dataDump;
                    }
                    else
                    {
                        // Length of code
                        dataLength = transaction.Message.D_options[i + 1];
                        i += 1 + dataLength;
                    }
                }
            }
            catch (Exception)
            {
                // TODO: handle exception
                // Console.WriteLine(ex.Message);
            }
            finally
            {
                dataDump = null;
            }
            return null;
        }
Exemple #8
0
        private void CreateOptionElement(DHCPOption option, byte[] data, ref byte[] target)
        {
            //create an option message
            //shall always append at the end of the message

            byte[] tOption;

            try
            {
                tOption = new byte[data.Length + 2];
                //add the code, and data length
                tOption[0] = (byte)option;
                tOption[1] = (byte)data.Length;
                //add the code to put in
                Array.Copy(data, 0, tOption, 2, data.Length);
                //copy the data to the out array
                if (target == null)
                    Array.Resize(ref target, (int)tOption.Length);
                else
                    Array.Resize(ref target, target.Length + tOption.Length);
                Array.Copy(tOption, 0, target, target.Length - tOption.Length, tOption.Length);
            }
            catch (Exception)
            {
                // TODO: handle exception
                // Console.WriteLine(ex.Message);
            }
        }
Exemple #9
0
        public bool IsRequestedParameter(DHCPOption optionType)
        {
            var dhcpOptionParameterRequestList = (DHCPOptionParameterRequestList)this.GetOption(DHCPOption.ParameterRequestList);

            return(dhcpOptionParameterRequestList?.RequestList.Contains(optionType) == true);
        }
        /// <summary>
        /// Returns option content
        /// </summary>
        /// <param name="option">Option to retrieve</param>
        /// <returns>Option content</returns>
        public byte[] GetOptionData(DHCPOption option)
        {
            int DHCPId = 0;
            byte DDataID, DataLength = 0;
            byte[] dumpData;

            DHCPId = (int)option;
            for (int i = 0; i < requestData.options.Length; i++)
            {
                DDataID = requestData.options[i];
                if (DDataID == (byte)DHCPOption.END_Option) break;
                if (DDataID == DHCPId)
                {
                    DataLength = requestData.options[i + 1];
                    dumpData = new byte[DataLength];
                    Array.Copy(requestData.options, i + 2, dumpData, 0, DataLength);
                    return dumpData;
                }
                else
                {
                    DataLength = requestData.options[i + 1];
                    i += 1 + DataLength;
                }
            }

            return null;
        }
        static private void CreateOptionElement(ref byte[] options, DHCPOption option, byte[] data)
        {
            byte[] optionData;

            optionData = new byte[data.Length + 2];
            optionData[0] = (byte)option;
            optionData[1] = (byte)data.Length;
            Array.Copy(data, 0, optionData, 2, data.Length);
            if (options == null)
                Array.Resize(ref options, (int)optionData.Length);
            else
                Array.Resize(ref options, options.Length + optionData.Length);
            Array.Copy(optionData, 0, options, options.Length - optionData.Length, optionData.Length);
        }
Exemple #12
0
 public DHCPOptionGeneric(DHCPOption option, byte[] data) : base(option)
 {
     this.Data = data;
 }
Exemple #13
0
 public DHCPOptionGeneric(DHCPOption option) : base(option)
 {
     this.Data = Array.Empty <byte>();
 }
Exemple #14
0
 protected DHCPOptionBase(DHCPOption optionType)
 {
     this.m_OptionType = optionType;
 }
Exemple #15
0
 public DHCPOptionFixedLength(DHCPOption option) : base(option)
 {
 }
Exemple #16
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();
        }
Exemple #17
0
        public byte[] GetBytes()
        {
            if (this.MacAddress == null)
            {
                return(null);
            }
            if (this.MacAddress.Length > 16)
            {
                return(null);
            }
            if (this.SupportedDHCP9ParameterList == null || this.SupportedDHCP9ParameterList.Count == 0)
            {
                return(null);
            }
            if (this.WantedDHCP9ParameterList == null || this.WantedDHCP9ParameterList.Count == 0)
            {
                return(null);
            }
            if (this.Servername != null && this.Servername.Length > 64)
            {
                return(null);
            }
            if (this.BootFile != null && this.BootFile.Length > 128)
            {
                return(null);
            }

            DHCPHeader hdr = new DHCPHeader();

            hdr.OperationCode = this.OperationCode;
            hdr.HardwareType  = this.HardwareType;
            hdr.Magic         = 0x63538263;
            hdr.HardwareType  = this.HardwareType;
            hdr.Flags         = this.Flags;
            hdr.CIAddr        = this.IPClient.GetAddressUint();
            hdr.GIAddr        = this.IPGateway.GetAddressUint();
            hdr.YIAddr        = this.IPYours.GetAddressUint();
            hdr.SIAddr        = this.IPServer.GetAddressUint();
            hdr.XID           = this.XID;
            hdr.MACLength     = (byte)this.MacAddress.Length;
            hdr.CHAddr        = new byte[16];
            Buffer.BlockCopy(this.MacAddress, 0, hdr.CHAddr, 0, this.MacAddress.Length);
            if (this.BootFile != null)
            {
                hdr.File = Encoding.ASCII.GetBytes(this.BootFile.PadRight(128, '\0'));
            }
            if (this.Servername != null)
            {
                hdr.SName = Encoding.ASCII.GetBytes(this.Servername.PadRight(64, '\0'));
            }

            byte[]            headerdata  = EncodeBytes(hdr);
            List <DHCPOption> DHCPOptions = new List <DHCPOption>();

            DHCPOption nonopto = new DHCPOption();

            nonopto.Type    = 53;
            nonopto.Size    = 1;
            nonopto.Data    = new byte[1];
            nonopto.Data[0] = (byte)this.DHCP53MessageType;
            DHCPOptions.Add(nonopto);

            nonopto      = new DHCPOption();
            nonopto.Type = 54;
            nonopto.Size = 4;
            nonopto.Data = this.IPServer.GetAddressBytes();
            DHCPOptions.Add(nonopto);


            foreach (byte code in SupportedDHCP9ParameterList)
            {
                if (WantedDHCP9ParameterList.Contains(code) == false)
                {
                    continue;
                }
                DHCPOption o = new DHCPOption();
                switch (code)
                {
                case 60:
                    if (DHCP60ClassIdentifier == null)
                    {
                        continue;
                    }
                    o.Type = 60;
                    o.Data = Encoding.ASCII.GetBytes(DHCP60ClassIdentifier);
                    if (o.Data.Length > 128)
                    {
                        continue;
                    }
                    o.Size = (byte)o.Data.Length;
                    break;

                case 66:
                    if (DHCP66BootServer == null)
                    {
                        continue;
                    }
                    o.Type = 66;
                    o.Data = Encoding.ASCII.GetBytes(DHCP66BootServer.Trim() + "\0");
                    if (o.Data.Length > 128)
                    {
                        continue;
                    }
                    o.Size = (byte)o.Data.Length;
                    break;

                case 67:
                    if (DHCP67BootFilename == null)
                    {
                        continue;
                    }
                    o.Type = 67;
                    o.Data = Encoding.ASCII.GetBytes(DHCP67BootFilename.Trim() + "\0");
                    if (o.Data.Length > 128)
                    {
                        continue;
                    }
                    o.Size = (byte)o.Data.Length;
                    break;

                case 43:
                    if (DHCP43VendorSpecificInfo == null)
                    {
                        continue;
                    }
                    o.Type = 43;
                    o.Data = DHCP43VendorSpecificInfo;
                    if (o.Data.Length > 128)
                    {
                        continue;
                    }
                    o.Size = (byte)o.Data.Length;
                    break;
                }
                DHCPOptions.Add(o);
            }

            int size = headerdata.Length;

            foreach (DHCPOption o in DHCPOptions)
            {
                size += 2 + o.Data.Length;
            }
            size++;

            byte[] data = new byte[size];
            Buffer.BlockCopy(headerdata, 0, data, 0, headerdata.Length);

            size = headerdata.Length;
            foreach (DHCPOption o in DHCPOptions)
            {
                data[size + 0] = o.Type;
                data[size + 1] = o.Size;
                Buffer.BlockCopy(o.Data, 0, data, size + 2, o.Data.Length);
                size += 2 + o.Data.Length;
            }

            data[data.Length - 1] = 0xFF;

            return(data);
        }
Exemple #18
0
 public virtual void addOption(DHCPOption option)
 {
     options.Add(option);
 }
Exemple #19
0
 public IDHCPOption GetOption(DHCPOption optionType) => this.Options.Find(v => v.OptionType == optionType);