Exemplo n.º 1
0
        static void recv67(IAsyncResult res)
        {
            UdpClient u;

            try
            {
                u = (UdpClient)res.AsyncState;
            }
            catch
            {
                FoxEventLog.WriteEventLog("Invalid packet on UDP 67", System.Diagnostics.EventLogEntryType.Warning);
                return;
            }

            DHCPPacket       dhcppacket;
            DHCPArchitecture detectedarch;
            IPEndPoint       ip;

            try
            {
                ip = new IPEndPoint(IPAddress.Any, 0);
                byte[] buffer = u.EndReceive(res, ref ip);

                dhcppacket   = new DHCPPacket(buffer);
                detectedarch = DHCPArchitecture.Undefined;
            }
            catch
            {
                if (RunService == true) //Shutdown service causes an exception on u.EndReceive()
                {
                    FoxEventLog.WriteEventLog("Cannot process data on UDP 67", System.Diagnostics.EventLogEntryType.Warning);
                }
                return;
            }

            try
            {
                if (dhcppacket.Malformed == true)
                {
                    return;
                }
                if (dhcppacket.OperationCode != DHCPOperationCode.ClientToServer)
                {
                    return;
                }
                if (dhcppacket.DHCP53MessageType != DHCPMessageType.DHCPDISCOVER)
                {
                    return;
                }
                if (dhcppacket.DHCP60ClassIdentifier.StartsWith("PXEClient") == false)
                {
                    return;
                }
                detectedarch = DetectArch(dhcppacket.DHCP60ClassIdentifier);
                if (detectedarch == DHCPArchitecture.Undefined)
                {
                    return;
                }

                DHCPPacket send = new DHCPPacket();
                send.MacAddress                  = dhcppacket.MacAddress;
                send.XID                         = dhcppacket.XID;
                send.DHCP53MessageType           = DHCPMessageType.DHCPOFFER;
                send.WantedDHCP9ParameterList    = DHCPPacket.DHCP9ParameterListPXEClient;
                send.SupportedDHCP9ParameterList = dhcppacket.DHCP9ReqParameterList;
                send.DHCP60ClassIdentifier       = "PXEClient";

                byte[] data = send.GetBytes();
                if (data == null)
                {
                    return;
                }
                SendData(UDP67, 68, IPAddress.Broadcast, data);

                Console.WriteLine(ip.Address.ToString() + " DHCP offer sent");
            }
            catch
            {
                FoxEventLog.WriteEventLog("Cannot fully process/decode data on UDP 67", System.Diagnostics.EventLogEntryType.Warning);
            }
            finally
            {
                if (RunService == true)
                {
                    u.BeginReceive(new AsyncCallback(recv67), u);
                }
            }
        }
Exemplo n.º 2
0
        static void recv4011(IAsyncResult res)
        {
            UdpClient u;

            try
            {
                u = (UdpClient)res.AsyncState;
            }
            catch
            {
                FoxEventLog.WriteEventLog("Invalid packet on UDP 4011", System.Diagnostics.EventLogEntryType.Warning);
                return;
            }
            try
            {
                IPEndPoint ip = new IPEndPoint(IPAddress.Any, 0);
                byte[]     buffer;

                try
                {
                    buffer = u.EndReceive(res, ref ip);
                }
                catch
                {
                    return;
                }

                DHCPPacket       dhcppacket   = new DHCPPacket(buffer);
                DHCPArchitecture detectedarch = DHCPArchitecture.Undefined;

                if (dhcppacket.Malformed == true)
                {
                    return;
                }
                if (dhcppacket.OperationCode != DHCPOperationCode.ClientToServer)
                {
                    return;
                }
                if (dhcppacket.DHCP53MessageType != DHCPMessageType.DHCPREQUEST)
                {
                    return;
                }
                if (dhcppacket.DHCP60ClassIdentifier.StartsWith("PXEClient") == false)
                {
                    return;
                }
                detectedarch = DetectArch(dhcppacket.DHCP60ClassIdentifier);
                //if (detectedarch == DHCPArchitecture.Undefined)
                //    return;

                string MACAddr  = dhcppacket.GetMacAddress();
                string BootFile = "bootmgfw.efi";
                string BootPath = null;

                do
                {
                    using (RegistryKey key = Registry.LocalMachine.OpenSubKey("Software\\Fox\\PXEBoot\\MAC\\" + MACAddr))
                    {
                        if (key != null)
                        {
                            object o = key.GetValue("BootFile");
                            if (o != null)
                            {
                                BootFile = Convert.ToString(o);
                            }

                            o = key.GetValue("Path");
                            if (o != null)
                            {
                                BootPath = Convert.ToString(o);
                            }
                            break;
                        }
                    }
                    if (MACAddr.Length <= 2)
                    {
                        break;
                    }
                    MACAddr = MACAddr.Substring(0, MACAddr.Length - 2);
                } while (MACAddr.Length > 0);

                Session.RegisterSession(ip.Address, detectedarch, BootPath);

                DHCPPacket send = new DHCPPacket();
                send.MacAddress                  = dhcppacket.MacAddress;
                send.XID                         = dhcppacket.XID;
                send.DHCP53MessageType           = DHCPMessageType.DHCPACK;
                send.WantedDHCP9ParameterList    = DHCPPacket.DHCP9ParameterListBootFiles;
                send.SupportedDHCP9ParameterList = dhcppacket.DHCP9ReqParameterList;
                send.DHCP60ClassIdentifier       = "PXEClient";
                send.DHCP66BootServer            = GetCurrentIP().ToString();
                send.DHCP67BootFilename          = BootFile;

                //send.BootFile = "bootmgfw.efi";
                //send.Servername = GetCurrentIP().ToString();

                if (detectedarch == DHCPArchitecture.IA32Legacy)
                {
                    send.DHCP43VendorSpecificInfo = new byte[] { 0x01, 0x04, 0x00, 0x00, 0x00, 0x00, 0xff };
                    send.WantedDHCP9ParameterList.Add(43);
                }

                byte[] data = send.GetBytes();
                if (data == null)
                {
                    return;
                }

                SendData(UDP4011, 4011, ip.Address, data);
                SendData(UDP4011, 68, IPAddress.Broadcast, data);

                Console.WriteLine(ip.Address.ToString() + " DHCP ack sent (System: " + detectedarch.ToString() + ")");
            }
            finally
            {
                if (RunService == true)
                {
                    try
                    {
                        u.BeginReceive(new AsyncCallback(recv4011), u);
                    }
                    catch
                    {
                        try
                        {
                            Thread.Sleep(1000);
                            u.BeginReceive(new AsyncCallback(recv4011), u);
                        }
                        catch
                        {
                            FoxEventLog.WriteEventLog("Cannot reset port 4011", System.Diagnostics.EventLogEntryType.Error);
                            Process.GetCurrentProcess().Kill();
                        }
                    }
                }
            }
        }