Skip to content

Commit

Permalink
Merge pull request CosmosOS#1014 from CosmosOS/network-fixes
Browse files Browse the repository at this point in the history
Work on networking
  • Loading branch information
jp2masa authored Jul 28, 2018
2 parents 2698b05 + 69d4bcc commit 8a68222
Show file tree
Hide file tree
Showing 12 changed files with 375 additions and 218 deletions.
12 changes: 11 additions & 1 deletion source/Cosmos.HAL2/Drivers/PCI/Network/AMDPCNetII.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ protected void HandleNetworkInterrupt(ref INTs.IRQContext aContext)
{
UInt32 cur_status = StatusRegister;

//Console.WriteLine("AMD PCNet IRQ raised!");
if ((cur_status & 0x100) != 0)
{
mInitDone = true;
Expand Down Expand Up @@ -212,6 +211,10 @@ public override bool Ready
get { return mInitDone; }
}

public override CardType CardType => CardType.Ethernet;

public override string Name => "PCNETII";

public override bool Enable()
{
StatusRegister = 0x43;
Expand Down Expand Up @@ -275,6 +278,7 @@ public override bool IsReceiveBufferFull()

#region Helper Functions


protected bool SendBytes(ref byte[] aData)
{
int txd = mNextTXDesc++;
Expand Down Expand Up @@ -319,6 +323,12 @@ private void ReadRawData()
if ((status & 0x80000000) == 0)
{
recv_size = (UInt16)(mRxDescriptor.Read32(xOffset + 0) & 0xFFF);

if (recv_size > 64) // remove checksum
{
recv_size -= 4;
}

recv_data = new byte[recv_size];
for (uint b = 0; b < recv_size; b++)
{
Expand Down
20 changes: 17 additions & 3 deletions source/Cosmos.HAL2/NetworkDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@ namespace Cosmos.HAL
{
public delegate void DataReceivedHandler(byte[] packetData);

public abstract class NetworkDevice : Device
public enum CardType
{
Ethernet,
Wireless
}

public abstract class NetworkDevice
{
public static List<NetworkDevice> Devices { get; private set; }

Expand All @@ -26,17 +32,25 @@ protected NetworkDevice()
Devices.Add(this);
}

public abstract CardType CardType
{
get;
}

public abstract MACAddress MACAddress
{
get;
}

public abstract bool Ready
public abstract string Name
{
get;
}

//public DataReceivedHandler DataReceived;
public abstract bool Ready
{
get;
}

public virtual bool QueueBytes(byte[] buffer)
{
Expand Down
25 changes: 13 additions & 12 deletions source/Cosmos.System2/Network/ARP/ARPPacket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Cosmos.HAL.Network;
using Sys = System;
using Cosmos.HAL;
using Cosmos.System.Network.IPv4;

namespace Cosmos.System.Network.ARP
{
Expand All @@ -22,22 +23,22 @@ internal static void ARPHandler(byte[] packetData)
{
if ((arp_packet.HardwareType == 1) && (arp_packet.ProtocolType == 0x0800))
{
IPv4.ARPRequest_Ethernet arp_request = new IPv4.ARPRequest_Ethernet(packetData);
ARPRequest_Ethernet arp_request = new ARPRequest_Ethernet(packetData);
if (arp_request.SenderIP == null)
{
global::System.Console.WriteLine("SenderIP null in ARPHandler!");
NetworkStack.debugger.Send("SenderIP null in ARPHandler!");
}
arp_request = new IPv4.ARPRequest_Ethernet(packetData);
arp_request = new ARPRequest_Ethernet(packetData);

ARPCache.Update(arp_request.SenderIP, arp_request.SenderMAC);

if (NetworkStack.AddressMap.ContainsKey(arp_request.TargetIP.Hash) == true)
{
//Sys.Console.WriteLine("ARP Request Recvd from " + arp_request.SenderIP.ToString());
NetworkStack.debugger.Send("ARP Request Recvd from " + arp_request.SenderIP.ToString());
NetworkDevice nic = NetworkStack.AddressMap[arp_request.TargetIP.Hash];

IPv4.ARPReply_Ethernet reply =
new IPv4.ARPReply_Ethernet(nic.MACAddress, arp_request.TargetIP, arp_request.SenderMAC, arp_request.SenderIP);
ARPReply_Ethernet reply =
new ARPReply_Ethernet(nic.MACAddress, arp_request.TargetIP, arp_request.SenderMAC, arp_request.SenderIP);

nic.QueueBytes(reply.RawData);
}
Expand All @@ -47,13 +48,13 @@ internal static void ARPHandler(byte[] packetData)
{
if ((arp_packet.HardwareType == 1) && (arp_packet.ProtocolType == 0x0800))
{
IPv4.ARPReply_Ethernet arp_reply = new IPv4.ARPReply_Ethernet(packetData);
//Sys.Console.WriteLine("Received ARP Reply");
//Sys.Console.WriteLine(arp_reply.ToString());
//Sys.Console.WriteLine("ARP Reply Recvd from " + arp_reply.SenderIP.ToString());
ARPReply_Ethernet arp_reply = new ARPReply_Ethernet(packetData);
NetworkStack.debugger.Send("Received ARP Reply");
NetworkStack.debugger.Send(arp_reply.ToString());
NetworkStack.debugger.Send("ARP Reply Recvd from " + arp_reply.SenderIP.ToString());
ARPCache.Update(arp_reply.SenderIP, arp_reply.SenderMAC);

IPv4.OutgoingBuffer.ARPCache_Update(arp_reply);
OutgoingBuffer.ARPCache_Update(arp_reply);
}
}
}
Expand Down
6 changes: 2 additions & 4 deletions source/Cosmos.System2/Network/IPv4/ARPPacket_Ethernet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,12 @@ internal ARPPacket_Ethernet(byte[] rawData)

protected override void initFields()
{
Sys.Console.WriteLine("ARPPacket_Ethernet.initFields() called");
base.initFields();
mSenderMAC = new MACAddress(mRawData, 22);
mSenderIP = new Address(mRawData, 28);
global::System.Console.WriteLine("SenderIP created");
if (SenderIP == null)
{
global::System.Console.WriteLine("But its already null again");
NetworkStack.debugger.Send("But its already null again");
}
mTargetMAC = new MACAddress(mRawData, 32);
mTargetIP = new Address(mRawData, 38);
Expand Down Expand Up @@ -123,7 +121,7 @@ internal ARPRequest_Ethernet(byte[] rawData)
{
if (SenderIP == null)
{
global::System.Console.WriteLine("In ARPRequest_Ethernet, SenderIP is null!");
NetworkStack.debugger.Send("In ARPRequest_Ethernet, SenderIP is null!");
}
}

Expand Down
11 changes: 9 additions & 2 deletions source/Cosmos.System2/Network/IPv4/Address.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class Address : IComparable
/// Predefined 0.0.0.0 address
/// </summary>
public static Address Zero = new Address(0, 0, 0, 0);
public static Address Broadcast = new Address(255, 255, 255, 255);

internal byte[] address = new byte[4];

Expand Down Expand Up @@ -65,9 +66,15 @@ public bool IsLoopbackAddress()
return false;
}

public bool IsBroadcastAddress()
public bool IsBroadcastAddress() =>
address[0] == 0xFF
&& address[1] == 0xFF
&& address[2] == 0xFF
&& address[3] == 0xFF;

public bool IsAPIPA()
{
if (address[0] == 255)
if ((address[0] == 169) && (address[1] == 254))
{
return true;
}
Expand Down
19 changes: 11 additions & 8 deletions source/Cosmos.System2/Network/IPv4/ICMPPacket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,26 @@ internal class ICMPPacket : IPPacket
{
protected byte icmpType;
protected byte icmpCode;
protected UInt16 icmpCRC;
protected ushort icmpCRC;
public static ICMPEchoReply recvd_reply;

internal static void ICMPHandler(byte[] packetData)
{
NetworkStack.debugger.Send("ICMP Handler called");
ICMPPacket icmp_packet = new ICMPPacket(packetData);
switch (icmp_packet.ICMP_Type)
{
case 0:
ICMPEchoReply recvd_reply = new ICMPEchoReply(packetData);
Sys.Console.WriteLine("Received ICMP Echo reply from " + recvd_reply.SourceIP.ToString());
recvd_reply = new ICMPEchoReply(packetData);
NetworkStack.debugger.Send("Received ICMP Echo reply from " + recvd_reply.SourceIP.ToString());
break;
case 8:
ICMPEchoRequest request = new ICMPEchoRequest(packetData);
Sys.Console.WriteLine("Received " + request.ToString());
NetworkStack.debugger.Send("Received " + request.ToString());
ICMPEchoReply reply = new ICMPEchoReply(request);
Sys.Console.WriteLine("Sending ICMP Echo reply to " + reply.DestinationIP.ToString());
NetworkStack.debugger.Send("Sending ICMP Echo reply to " + reply.DestinationIP.ToString());
OutgoingBuffer.AddPacket(reply);
NetworkStack.Update();
break;
}
}
Expand Down Expand Up @@ -55,7 +58,7 @@ protected override void initFields()
}

internal ICMPPacket(Address source, Address dest, byte type, byte code, UInt16 id, UInt16 seq, UInt16 icmpDataSize)
: base(icmpDataSize, 1, source, dest)
: base(icmpDataSize, 1, source, dest, 0x00)
{
mRawData[this.dataOffset] = type;
mRawData[this.dataOffset + 1] = code;
Expand Down Expand Up @@ -129,9 +132,9 @@ internal ICMPEchoRequest(byte[] rawData)
internal ICMPEchoRequest(Address source, Address dest, UInt16 id, UInt16 sequence)
: base(source, dest, 8, 0, id, sequence, 40)
{
for (byte b = 8; b < this.ICMP_DataLength; b++)
for (int b = 8; b < this.ICMP_DataLength; b++)
{
mRawData[this.dataOffset + b] = b;
mRawData[this.dataOffset + b] = (byte)b;
}

mRawData[this.dataOffset + 2] = 0x00;
Expand Down
19 changes: 10 additions & 9 deletions source/Cosmos.System2/Network/IPv4/IPPacket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ internal static void IPv4Handler(byte[] packetData)
//Sys.Console.WriteLine(ip_packet.ToString());
if (ip_packet.SourceIP == null)
{
global::System.Console.WriteLine("SourceIP null in IPv4Handler!");
NetworkStack.debugger.Send("SourceIP null in IPv4Handler!");
}
ARPCache.Update(ip_packet.SourceIP, ip_packet.SourceMAC);

Expand All @@ -44,9 +44,9 @@ internal static void IPv4Handler(byte[] packetData)
case 1:
ICMPPacket.ICMPHandler(packetData);
break;
//case 6:
// IPv4_TCPHandler(packetData);
// break;
case 6:
//TCPPacket.TCPHandler(packetData);
break;
case 17:
UDPPacket.UDPHandler(packetData);
break;
Expand Down Expand Up @@ -88,24 +88,25 @@ protected override void initFields()
dataOffset = (UInt16)(14 + HeaderLength);
}

protected IPPacket(UInt16 dataLength, byte protocol, Address source, Address dest)
: this(MACAddress.None, MACAddress.None, dataLength, protocol, source, dest)
protected IPPacket(UInt16 dataLength, byte protocol, Address source, Address dest, byte Flags)
: this(MACAddress.None, MACAddress.None, dataLength, protocol, source, dest, Flags)
{ }

private IPPacket(MACAddress srcMAC, MACAddress destMAC, UInt16 dataLength, byte protocol,
Address source, Address dest)
public IPPacket(MACAddress srcMAC, MACAddress destMAC, UInt16 dataLength, byte protocol,
Address source, Address dest, byte Flags)
: base(destMAC, srcMAC, 0x0800, dataLength + 14 + 20)
{
mRawData[14] = 0x45;
mRawData[15] = 0;
ipLength = (UInt16)(dataLength + 20);
ipHeaderLength = 5;

mRawData[16] = (byte)((ipLength >> 8) & 0xFF);
mRawData[17] = (byte)((ipLength >> 0) & 0xFF);
fragmentID = NextIPFragmentID;
mRawData[18] = (byte)((fragmentID >> 8) & 0xFF);
mRawData[19] = (byte)((fragmentID >> 0) & 0xFF);
mRawData[20] = 0x00;
mRawData[20] = Flags;
mRawData[21] = 0x00;
mRawData[22] = 0x80;
mRawData[23] = protocol;
Expand Down
Loading

0 comments on commit 8a68222

Please sign in to comment.