From 2bb77be3eb7b26321b60a6833913b0ebc025b718 Mon Sep 17 00:00:00 2001 From: kudzu_cp <6d05c8c8ef5431987001abfdb2eadc9593ac9498> Date: Sat, 23 Jun 2012 18:22:15 +0000 Subject: [PATCH] --- .../Cosmos.Deploy.Pixie.csproj | 1 - source2/Cosmos.Deploy.Pixie/DHCP.cs | 5 -- source2/Cosmos.Deploy.Pixie/TrivialFTP.cs | 57 +++++++++++++++---- .../Cosmos.Deploy.Pixie/TrivialFtpPacket.cs | 40 ------------- 4 files changed, 47 insertions(+), 56 deletions(-) delete mode 100644 source2/Cosmos.Deploy.Pixie/TrivialFtpPacket.cs diff --git a/source2/Cosmos.Deploy.Pixie/Cosmos.Deploy.Pixie.csproj b/source2/Cosmos.Deploy.Pixie/Cosmos.Deploy.Pixie.csproj index 607f55178..5ec1893d5 100644 --- a/source2/Cosmos.Deploy.Pixie/Cosmos.Deploy.Pixie.csproj +++ b/source2/Cosmos.Deploy.Pixie/Cosmos.Deploy.Pixie.csproj @@ -48,7 +48,6 @@ - diff --git a/source2/Cosmos.Deploy.Pixie/DHCP.cs b/source2/Cosmos.Deploy.Pixie/DHCP.cs index ac606ed77..5f5bd8349 100644 --- a/source2/Cosmos.Deploy.Pixie/DHCP.cs +++ b/source2/Cosmos.Deploy.Pixie/DHCP.cs @@ -15,7 +15,6 @@ namespace Cosmos.Deploy.Pixie { protected byte[] mServerIP; protected byte[] mClientIP; - protected byte[] mBroadcastIP; protected IPEndPoint mRecvEndPoint; public DHCP(byte[] aServerIP, string aBootFile) { @@ -24,9 +23,6 @@ namespace Cosmos.Deploy.Pixie { mClientIP = (byte[])mServerIP.Clone(); mClientIP[3] = 2; - // Certain DHCP clients require specific subnet broadcast so this is safer than 255.255.255.255 - mBroadcastIP = (byte[])mServerIP.Clone(); - mBroadcastIP[3] = 255; mUDP = new UdpClient(new IPEndPoint(new IPAddress(mServerIP), ServerPort)); @@ -45,7 +41,6 @@ namespace Cosmos.Deploy.Pixie { protected void Send(DhcpPacket aPacket) { var xBytes = aPacket.GetBytes(); - //mUDP.Send(xBytes, xBytes.Length, new IPEndPoint(new IPAddress(mBroadcastIP), 68)); mUDP.Send(xBytes, xBytes.Length, new IPEndPoint(IPAddress.Broadcast, 68)); } diff --git a/source2/Cosmos.Deploy.Pixie/TrivialFTP.cs b/source2/Cosmos.Deploy.Pixie/TrivialFTP.cs index b5dba9b64..f0fcc566a 100644 --- a/source2/Cosmos.Deploy.Pixie/TrivialFTP.cs +++ b/source2/Cosmos.Deploy.Pixie/TrivialFTP.cs @@ -4,14 +4,17 @@ using System.Linq; using System.Text; using System.Net; using System.Net.Sockets; +using System.IO; namespace Cosmos.Deploy.Pixie { public class TrivialFTP { + public enum OpType { Read = 1, Write, Data, Ack, Error }; protected byte[] mServerIP; protected string mPath; protected int Port = 69; protected UdpClient mUDP; protected IPEndPoint mRecvEndPoint; + string mFilename; public TrivialFTP(byte[] aServerIP, string aPath) { mServerIP = aServerIP; @@ -21,22 +24,56 @@ namespace Cosmos.Deploy.Pixie { mRecvEndPoint = new IPEndPoint(IPAddress.Any, Port); } - protected TrivialFtpPacket Receive() { + protected void ProcessRequest() { var xData = mUDP.Receive(ref mRecvEndPoint); - return new TrivialFtpPacket(xData); + var xReader = new BinaryReader(new MemoryStream(xData)); + + // Op MSB which is always 0 + xReader.ReadByte(); + var xOp = (OpType)xReader.ReadByte(); + if (xOp != OpType.Read) { + throw new Exception("[TFTP] Expected read"); + } + + mFilename = ReadString(xReader); + + string xMode = ReadString(xReader).ToLower(); + if (xMode != "octet") { + throw new Exception("[TFTP] Expected octet"); + } } public void Execute() { - TrivialFtpPacket xPacket; - - xPacket = Receive(); - if (xPacket.Op != TrivialFtpPacket.OpType.Read) { - throw new Exception("[TFTP] Expected read"); - } else if (xPacket.Mode.ToLower() != "octet") { - throw new Exception("[TFTP] Expected octet"); - } + ProcessRequest(); // TODO Read Option packet size for larger packet and faster speed + + var xDataEP = new IPEndPoint(mRecvEndPoint.Address, mRecvEndPoint.Port); + int xBlockSize = 512; + var xData = new byte[xBlockSize + 4]; + + // xData[0] MSB is 0 + xData[1] = (byte)OpType.Data; + + // xData[2] MSB is 0 + xData[3] = 1; + + mUDP.Send(xData, xData.Length, xDataEP); } + + public string ReadString(BinaryReader aReader) { + var xResult = new List(); + + while (true) { + byte xByte = aReader.ReadByte(); + if (xByte == 0) { + break; + } + xResult.Add(xByte); + } + + return Encoding.ASCII.GetString(xResult.ToArray()); + } + } } diff --git a/source2/Cosmos.Deploy.Pixie/TrivialFtpPacket.cs b/source2/Cosmos.Deploy.Pixie/TrivialFtpPacket.cs deleted file mode 100644 index 2271aebb5..000000000 --- a/source2/Cosmos.Deploy.Pixie/TrivialFtpPacket.cs +++ /dev/null @@ -1,40 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.IO; - -namespace Cosmos.Deploy.Pixie { - public class TrivialFtpPacket { - public enum OpType { Read = 1, Write, Data, Ack, Error }; - public OpType Op; - public string Filename; - public string Mode; - - public TrivialFtpPacket(byte[] aData) { - var xReader = new BinaryReader(new MemoryStream(aData)); - - // Op MSB which is always 0 - xReader.ReadByte(); - Op = (OpType)xReader.ReadByte(); - if (Op == OpType.Read || Op == OpType.Write) { - Filename = ReadString(xReader); - Mode = ReadString(xReader); - } - } - - public string ReadString(BinaryReader aReader) { - var xResult = new List(); - - while (true) { - byte xByte = aReader.ReadByte(); - if (xByte == 0) { - break; - } - xResult.Add(xByte); - } - - return Encoding.ASCII.GetString(xResult.ToArray()); - } - } -}