Beispiel #1
0
        public void cDhcp_Request(cDHCPStruct d_DHCP, string MacId)
        {
            string str = string.Empty;

            if (MacId.StartsWith(MacMask) == true)
            {
                //announced so then send the offer
                d_DHCP.dData.IPAddr      = GetIPAdd();
                d_DHCP.dData.SubMask     = "255.255.0.0";
                d_DHCP.dData.LeaseTime   = 2000;
                d_DHCP.dData.ServerName  = "tiny DHCP Server";
                d_DHCP.dData.MyIP        = AdapterIP;
                d_DHCP.dData.RouterIP    = "0.0.0.0";
                d_DHCP.dData.LogServerIP = "0.0.0.0";
                d_DHCP.dData.DomainIP    = "0.0.0.0";
                cDhcp.SendDHCPMessage(DHCPMsgType.DHCPACK, d_DHCP);
                str = "IP " + d_DHCP.dData.IPAddr + " Assigned to Mac: " + MacId;
            }
            else
            {
                str = "Mac: " + MacId + " is not part of the mask!";
            }
            SimpleDelegate lst = delegate
            {
                listBox1.Items.Add(str);
            };

            Invoke(lst);
        }
Beispiel #2
0
        public void cDhcp_Announced(cDHCPStruct d_DHCP, string MacId)
        {
            string str = string.Empty;

            if (MacId.StartsWith(MacMask) == true)
            {
                //options should be filled with valid data
                d_DHCP.dData.IPAddr      = GetIPAdd();
                d_DHCP.dData.SubMask     = "255.255.0.0";
                d_DHCP.dData.LeaseTime   = 2000;
                d_DHCP.dData.ServerName  = "Small DHCP Server";
                d_DHCP.dData.MyIP        = AdapterIP;
                d_DHCP.dData.RouterIP    = "0.0.0.0";
                d_DHCP.dData.LogServerIP = "0.0.0.0";
                d_DHCP.dData.DomainIP    = "0.0.0.0";
                str = "IP requested for Mac: " + MacId;
            }
            else
            {
                str = "Mac: " + MacId + " is not part of the mask!";
            }
            cDhcp.SendDHCPMessage(DHCPMsgType.DHCPOFFER, d_DHCP);
            SimpleDelegate lst = delegate
            {
                listBox1.Items.Add(str);
            };

            Invoke(lst);
            //Application.DoEvents()
        }
Beispiel #3
0
        //get the Message type
        //located in the options stream
        public DHCPMsgType GetMsgType(cDHCPStruct cdDHCPs)
        {
            byte[] DData;

            try
            {
                DData = GetOptionData(DHCPOptionEnum.DHCPMessageTYPE, cdDHCPs);
                if (DData != null)
                {
                    return((DHCPMsgType)DData[0]);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            return(0);
        }
Beispiel #4
0
        //pass the option type that you require
        //parse the option data
        //return the data in a byte of what we need
        private byte[] GetOptionData(DHCPOptionEnum DHCPTyp, cDHCPStruct cdDHCPs)
        {
            int  DHCPId = 0;
            byte DDataID, DataLength = 0;

            byte[] dumpData;

            try
            {
                DHCPId = (int)DHCPTyp;
                //loop through look for the bit that states that the identifier is there
                for (int i = 0; i < cdDHCPs.dStruct.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
                    DDataID = cdDHCPs.dStruct.D_options[i];
                    if (DDataID == DHCPId)
                    {
                        DataLength = cdDHCPs.dStruct.D_options[i + 1];
                        dumpData   = new byte[DataLength];
                        Array.Copy(cdDHCPs.dStruct.D_options, i + 2, dumpData, 0, DataLength);
                        return(dumpData);
                    }
                    else
                    {
                        DataLength = cdDHCPs.dStruct.D_options[i + 1]; //'length of code
                        i         += 1 + DataLength;
                    } //.endif
                } //for
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                dumpData = null;
            }
            return(null);
        }
Beispiel #5
0
        //mac announced itself, established IP etc....
        //send the offer to the mac
        public void SendDHCPMessage(DHCPMsgType msgType, cDHCPStruct ddHcpS)
        {
            byte[] Subn, HostID, DataToSend;


            //we shall leave everything as is structure wise
            //shall CHANGE the type to OFFER
            //shall set the client's IP-Address
            try
            {
                //change message type to reply
                ddHcpS.dStruct.D_op = 2; //reply
                //subnet
                Subn = IPAddress.Parse(ddHcpS.dData.SubMask).GetAddressBytes();
                //create your ip address
                ddHcpS.dStruct.D_yiaddr = IPAddress.Parse(ddHcpS.dData.IPAddr).GetAddressBytes();

                //Host ID
                HostID = System.Text.Encoding.ASCII.GetBytes(ddHcpS.dData.ServerName);

                CreateOptionStruct(ref ddHcpS, msgType);
                //send the data to the unit
                DataToSend = BuildDataStructure(ddHcpS.dStruct);
                cUdp.SendData(DataToSend);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                Subn = null;
                //LeaseTime= null;
                HostID = null;

                DataToSend = null;
            }
        }
Beispiel #6
0
        //private void
        public void cUdp_DataRcvd(byte[] DData, IPEndPoint RIpEndPoint)
        {
            cDHCPStruct ddHcpS;
            DHCPMsgType MsgTyp;
            //clsDHCP ccDHCP;
            string MacID;

            try
            {
                ddHcpS = new cDHCPStruct(DData);
                //ccDHCP = new clsDHCP();


                //data is now in the structure
                //get the msg type
                MsgTyp = GetMsgType(ddHcpS);
                MacID  = ByteToString(ddHcpS.dStruct.D_chaddr, ddHcpS.dStruct.D_hlen);// (string)ddHcpS.dStruct.D_chaddr;
                switch (MsgTyp)
                {
                case DHCPMsgType.DHCPDISCOVER:
                    //myClass.Process(myLogger);
                    // a Mac has requested an IP
                    // discover Msg Has been sent
                    Announced(ddHcpS, MacID);
                    break;

                case DHCPMsgType.DHCPREQUEST:
                    Request(ddHcpS, MacID);
                    break;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
Beispiel #7
0
        private void CreateOptionStruct(ref cDHCPStruct ddHcps, DHCPMsgType OptionReplyMsg)
        {
            byte[] PReqList, t1, LeaseTime, MyIp;

            try
            {
                //we look for the parameter request list
                PReqList = GetOptionData(DHCPOptionEnum.ParameterRequestList, ddHcps);
                //erase the options array, and set the message type to ack
                ddHcps.dStruct.D_options = null;
                CreateOptionElement(DHCPOptionEnum.DHCPMessageTYPE, new byte[] { (byte)OptionReplyMsg }, ref ddHcps.dStruct.D_options);
                //server identifier, my IP
                MyIp = IPAddress.Parse(ddHcps.dData.MyIP).GetAddressBytes();
                CreateOptionElement(DHCPOptionEnum.ServerIdentifier, MyIp, ref ddHcps.dStruct.D_options);


                //PReqList contains the option data in a byte that is requested by the unit
                foreach (byte i in PReqList)
                {
                    t1 = null;
                    switch ((DHCPOptionEnum)i)
                    {
                    case DHCPOptionEnum.SubnetMask:
                        t1 = IPAddress.Parse(ddHcps.dData.SubMask).GetAddressBytes();
                        break;

                    case DHCPOptionEnum.Router:
                        t1 = IPAddress.Parse(ddHcps.dData.RouterIP).GetAddressBytes();
                        break;

                    case DHCPOptionEnum.DomainNameServer:
                        t1 = IPAddress.Parse(ddHcps.dData.DomainIP).GetAddressBytes();
                        break;

                    case DHCPOptionEnum.DomainName:
                        t1 = System.Text.Encoding.ASCII.GetBytes(ddHcps.dData.ServerName);
                        break;

                    case DHCPOptionEnum.ServerIdentifier:
                        t1 = IPAddress.Parse(ddHcps.dData.MyIP).GetAddressBytes();
                        break;

                    case DHCPOptionEnum.LogServer:
                        t1 = System.Text.Encoding.ASCII.GetBytes(ddHcps.dData.LogServerIP);
                        break;

                    case DHCPOptionEnum.NetBIOSoverTCPIPNameServer:
                        break;
                    }
                    if (t1 != null)
                    {
                        CreateOptionElement((DHCPOptionEnum)i, t1, ref ddHcps.dStruct.D_options);
                    }
                }

                //lease time
                LeaseTime    = new byte[4];
                LeaseTime[3] = (byte)(ddHcps.dData.LeaseTime);
                LeaseTime[2] = (byte)(ddHcps.dData.LeaseTime >> 8);
                LeaseTime[1] = (byte)(ddHcps.dData.LeaseTime >> 16);
                LeaseTime[0] = (byte)(ddHcps.dData.LeaseTime >> 24);
                CreateOptionElement(DHCPOptionEnum.IPAddressLeaseTime, LeaseTime, ref ddHcps.dStruct.D_options);
                CreateOptionElement(DHCPOptionEnum.RenewalTimeValue_T1, LeaseTime, ref ddHcps.dStruct.D_options);
                CreateOptionElement(DHCPOptionEnum.RebindingTimeValue_T2, LeaseTime, ref ddHcps.dStruct.D_options);
                //create the end option
                Array.Resize(ref ddHcps.dStruct.D_options, ddHcps.dStruct.D_options.Length + 1);
                Array.Copy(new byte[] { 255 }, 0, ddHcps.dStruct.D_options, ddHcps.dStruct.D_options.Length - 1, 1);
                //send the data to the unit
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                LeaseTime = null;
                PReqList  = null;
                t1        = null;
            }
        }