This commit is contained in:
kudzu_cp 2008-06-22 22:42:22 +00:00
parent dee8a56ddc
commit 8fb7b9f262
4 changed files with 35 additions and 15 deletions

View file

@ -49,6 +49,7 @@
<Compile Include="Boot.cs" />
<Compile Include="Deboot.cs" />
<Compile Include="Global.cs" />
<Compile Include="Network\IP4Packet.cs" />
<Compile Include="Network\Packet.cs" />
<Compile Include="Network\UDPPacket.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />

View file

@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Cosmos.Sys.Network {
public abstract class IP4Packet : Packet {
protected int mHeaderSize = 20;
protected int mHeaderBegin;
protected override int Initialize(byte[] aData, int aHeaderSize) {
mHeaderBegin = base.Initialize(aData, mHeaderSize + aHeaderSize);
return mHeaderBegin;
}
}
}

View file

@ -11,16 +11,17 @@ namespace Cosmos.Sys.Network {
// user that it is not a direct access, but incurs a performance
// penalty to call
public byte[] GetData() {
Finalize();
Conclude();
return mData;
}
protected void Initialize(byte[] aData, int aHeaderSize) {
protected virtual int Initialize(byte[] aData, int aHeaderSize) {
mData = new byte[aData.Length + 8];
aData.CopyTo(mData, 8);
return 0;
}
protected virtual void Finalize() {
protected virtual void Conclude() {
}
}
}

View file

@ -5,26 +5,28 @@ using System.Text;
namespace Cosmos.Sys.Network {
// http://en.wikipedia.org/wiki/User_Datagram_Protocol
public class UDPPacket : Packet {
public class UDPPacket : IP4Packet {
public UDPPacket(int aSrcPort, int aDestPort, byte[] aData) {
Initialize(aData, 8);
mHeaderBegin = Initialize(aData, 8);
// Source Port
mData[0] = (byte)(aSrcPort >> 8);
mData[1] = (byte)(aSrcPort & 0xFF);
mData[mHeaderBegin] = (byte)(aSrcPort >> 8);
mData[mHeaderBegin + 1] = (byte)(aSrcPort & 0xFF);
// Destination Port
mData[2] = (byte)(aDestPort >> 8);
mData[3] = (byte)(aDestPort & 0xFF);
mData[mHeaderBegin + 2] = (byte)(aDestPort >> 8);
mData[mHeaderBegin + 3] = (byte)(aDestPort & 0xFF);
// Length
mData[4] = (byte)(mData.Length >> 8);
mData[5] = (byte)(mData.Length & 0xFF);
mData[mHeaderBegin + 4] = (byte)(mData.Length >> 8);
mData[mHeaderBegin + 5] = (byte)(mData.Length & 0xFF);
}
protected override void Finalize() {
base.Finalize();
protected new int mHeaderBegin = 0;
protected override void Conclude() {
base.Conclude();
// Checksum
//TODO: Uses info from IPHeader to create check sum as well
mData[6] = 0;
mData[7] = 0;
mData[mHeaderBegin + 6] = 0;
mData[mHeaderBegin + 7] = 0;
}
}
}