This commit is contained in:
kudzu_cp 2012-06-23 18:22:15 +00:00
parent c8c63693f3
commit 2bb77be3eb
4 changed files with 47 additions and 56 deletions

View file

@ -48,7 +48,6 @@
<Compile Include="DhcpPacket.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="TrivialFTP.cs" />
<Compile Include="TrivialFtpPacket.cs" />
</ItemGroup>
<ItemGroup>
<Content Include="Resources.html" />

View file

@ -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));
}

View file

@ -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<byte>();
while (true) {
byte xByte = aReader.ReadByte();
if (xByte == 0) {
break;
}
xResult.Add(xByte);
}
return Encoding.ASCII.GetString(xResult.ToArray());
}
}
}

View file

@ -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<byte>();
while (true) {
byte xByte = aReader.ReadByte();
if (xByte == 0) {
break;
}
xResult.Add(xByte);
}
return Encoding.ASCII.GetString(xResult.ToArray());
}
}
}