Skip to content

Commit

Permalink
Fixed some issues
Browse files Browse the repository at this point in the history
  • Loading branch information
vulpesfox committed Feb 25, 2020
1 parent 504f4d1 commit 49262cb
Show file tree
Hide file tree
Showing 4 changed files with 198 additions and 44 deletions.
1 change: 1 addition & 0 deletions PXEBoot/PXEBoot.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
</Compile>
<Compile Include="Sessions.cs" />
<Compile Include="Settings.cs" />
<Compile Include="Test.cs" />
<Compile Include="TFTP.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
70 changes: 48 additions & 22 deletions PXEBoot/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,9 @@ static int CreateDirStruct()

public static int SMain()
{
#if DEBUG
Test.DisplayIPv4NetworkInterfaces();
#endif
try
{
Settings.Load();
Expand Down Expand Up @@ -225,8 +228,27 @@ static void recv69(IAsyncResult res)
}
finally
{
if (RunService == true)
u.BeginReceive(new AsyncCallback(recv69), u);
try
{
if (RunService == true)
u.BeginReceive(new AsyncCallback(recv69), u);
}
catch
{
//reset port!
try
{
u.Close();
}
catch
{

}
UDP69 = new UdpClient(new IPEndPoint(IPAddress.Any, 69));
UDP69.EnableBroadcast = true;
UDP69.BeginReceive(new AsyncCallback(recv69), UDP69);
UDP69.DontFragment = true;
}
}
}

Expand Down Expand Up @@ -292,42 +314,46 @@ static void recv4011(IAsyncResult res)
if (dhcppacket.DHCP60ClassIdentifier.StartsWith("PXEClient") == false)
return;
detectedarch = DetectArch(dhcppacket.DHCP60ClassIdentifier);
if (detectedarch == DHCPArchitecture.Undefined)
return;

Session.RegisterSession(ip.Address, detectedarch);

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();
//if (detectedarch == DHCPArchitecture.Undefined)
// return;

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

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

o = key.GetValue("Path");
if (o != null)
{
BootPath = Convert.ToString(o);
}
break;
}
key.Close();
}
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";
Expand Down
60 changes: 38 additions & 22 deletions PXEBoot/Sessions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public void StopSessions()
}
}

public bool RegisterSession(IPAddress Client, DHCPArchitecture Architecture)
public bool RegisterSession(IPAddress Client, DHCPArchitecture Architecture, string PathOverride)
{
if (Client == IPAddress.Any)
return (false);
Expand All @@ -116,28 +116,44 @@ public bool RegisterSession(IPAddress Client, DHCPArchitecture Architecture)
ses.Architecture = Architecture;
ses.IP = Client;
ses.TFTPRootPath = Settings.TFTPRootPath;
switch (Architecture)

if (string.IsNullOrWhiteSpace(PathOverride) == false)
{
case DHCPArchitecture.ARC_x86:
ses.TFTPRootPath += "ARC x86\\"; break;
case DHCPArchitecture.DEC_ALPHA:
ses.TFTPRootPath += "DEC Alpha\\"; break;
case DHCPArchitecture.EFI_ByteCode:
ses.TFTPRootPath += "EFI BC\\"; break;
case DHCPArchitecture.EFI_EM64T:
ses.TFTPRootPath += "EFI X64\\"; break;
case DHCPArchitecture.EFI_IA32:
ses.TFTPRootPath += "EFI X86\\"; break;
case DHCPArchitecture.EFI_ITANIUM:
ses.TFTPRootPath += "EFI ITANIUM\\"; break;
case DHCPArchitecture.EFI_XScale:
ses.TFTPRootPath += "EFI XScale\\"; break;
case DHCPArchitecture.IA32Legacy:
ses.TFTPRootPath += "BIOS\\"; break;
case DHCPArchitecture.NEC_PC98:
ses.TFTPRootPath += "NEC PC98\\"; break;
default:
ses.TFTPRootPath += "Unknown\\"; break;
ses.TFTPRootPath += PathOverride;
if (ses.TFTPRootPath.EndsWith("\\") == false)
ses.TFTPRootPath += "\\";
if (Directory.Exists(ses.TFTPRootPath) == false)
{
PathOverride = "";
ses.TFTPRootPath = Settings.TFTPRootPath;
}
}

if (string.IsNullOrWhiteSpace(PathOverride) == true)
{
switch (Architecture)
{
case DHCPArchitecture.ARC_x86:
ses.TFTPRootPath += "ARC x86\\"; break;
case DHCPArchitecture.DEC_ALPHA:
ses.TFTPRootPath += "DEC Alpha\\"; break;
case DHCPArchitecture.EFI_ByteCode:
ses.TFTPRootPath += "EFI BC\\"; break;
case DHCPArchitecture.EFI_EM64T:
ses.TFTPRootPath += "EFI X64\\"; break;
case DHCPArchitecture.EFI_IA32:
ses.TFTPRootPath += "EFI X86\\"; break;
case DHCPArchitecture.EFI_ITANIUM:
ses.TFTPRootPath += "EFI ITANIUM\\"; break;
case DHCPArchitecture.EFI_XScale:
ses.TFTPRootPath += "EFI XScale\\"; break;
case DHCPArchitecture.IA32Legacy:
ses.TFTPRootPath += "BIOS\\"; break;
case DHCPArchitecture.NEC_PC98:
ses.TFTPRootPath += "NEC PC98\\"; break;
default:
ses.TFTPRootPath += "Unknown\\"; break;
}
}

lock (RunningSessions)
Expand Down
111 changes: 111 additions & 0 deletions PXEBoot/Test.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading.Tasks;

namespace PXEBoot
{
#if DEBUG
class Test
{
static byte[] DHCPdata = {
0x01, 0x01, 0x06, 0x00, 0x2B, 0x9C, 0x4E, 0xCD, 0x00, 0x06, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C, 0x29, 0x9C,
0x4E, 0xCD, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x63, 0x82, 0x53, 0x63,
0x35, 0x01, 0x01, 0x37, 0x18, 0x01, 0x02, 0x03, 0x05, 0x06, 0x0B, 0x0C, 0x0D, 0x0F, 0x10, 0x11,
0x12, 0x2B, 0x36, 0x3C, 0x43, 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x39, 0x02, 0x04,
0xEC, 0x61, 0x11, 0x00, 0x56, 0x4D, 0x50, 0x81, 0x74, 0x47, 0xF0, 0x6A, 0x98, 0xFD, 0xA3, 0xA3,
0xED, 0x9C, 0x4E, 0xCD, 0x5D, 0x02, 0x00, 0x00, 0x5E, 0x03, 0x01, 0x02, 0x01, 0x3C, 0x20, 0x50,
0x58, 0x45, 0x43, 0x6C, 0x69, 0x65, 0x6E, 0x74, 0x3A, 0x41, 0x72, 0x63, 0x68, 0x3A, 0x30, 0x30,
0x30, 0x30, 0x30, 0x3A, 0x55, 0x4E, 0x44, 0x49, 0x3A, 0x30, 0x30, 0x32, 0x30, 0x30, 0x31, 0xFF,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00};

static byte[] TFTPReqData = {
0x00, 0x01, 0x62, 0x6F, 0x6F, 0x74, 0x6D, 0x67, 0x66, 0x77, 0x2E, 0x65, 0x66, 0x69, 0x00, 0x6F,
0x63, 0x74, 0x65, 0x74, 0x00, 0x74, 0x73, 0x69, 0x7A, 0x65, 0x00, 0x30, 0x00, 0x62, 0x6C, 0x6B,
0x73, 0x69, 0x7A, 0x65, 0x00, 0x31, 0x34, 0x36, 0x38, 0x00};

static byte[] TFTPErrorData ={
0x00,0x05,0x00,0x08,0x55,0x73,0x65,0x72,0x20,0x61,0x62,0x6f,0x72,0x74,0x65,0x64,0x20,0x74,0x68,
0x65,0x20,0x74,0x72,0x61,0x6e,0x73,0x66,0x65,0x72,0x00};



public static void Run()
{
DHCPPacket dh = new DHCPPacket(DHCPdata);
TFTPPacketReadReq pp = new TFTPPacketReadReq(TFTPReqData);
TFTPPacketError err = new TFTPPacketError(TFTPErrorData);
}

public static void DisplayIPv4NetworkInterfaces()
{
NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
IPGlobalProperties properties = IPGlobalProperties.GetIPGlobalProperties();
Console.WriteLine("IPv4 interface information for {0}.{1}", properties.HostName, properties.DomainName);
Console.WriteLine();

foreach (NetworkInterface adapter in nics)
{
// Only display informatin for interfaces that support IPv4.
if (adapter.Supports(NetworkInterfaceComponent.IPv4) == false)
{
continue;
}
Console.WriteLine(adapter.Description);
// Underline the description.
Console.WriteLine(String.Empty.PadLeft(adapter.Description.Length, '='));
IPInterfaceProperties adapterProperties = adapter.GetIPProperties();
// Try to get the IPv4 interface properties.
IPv4InterfaceProperties p = adapterProperties.GetIPv4Properties();

if (p == null)
{
Console.WriteLine("No IPv4 information is available for this interface.");
Console.WriteLine();
continue;
}
// Display the IPv4 specific data.
Console.WriteLine(" Index ............................. : {0}", p.Index);
Console.WriteLine(" MTU ............................... : {0}", p.Mtu);
Console.WriteLine(" APIPA active....................... : {0}",
p.IsAutomaticPrivateAddressingActive);
Console.WriteLine(" APIPA enabled...................... : {0}",
p.IsAutomaticPrivateAddressingEnabled);
Console.WriteLine(" Forwarding enabled................. : {0}",
p.IsForwardingEnabled);
Console.WriteLine(" Uses WINS ......................... : {0}",
p.UsesWins);
Console.WriteLine();
}
}
}
#endif
}

0 comments on commit 49262cb

Please sign in to comment.