Skip to content

Commit

Permalink
[#5] Fixed basic issues in ICMP, now works, but bad checksum
Browse files Browse the repository at this point in the history
  • Loading branch information
John15321 committed May 25, 2021
1 parent aeb95bb commit d1108ea
Showing 1 changed file with 36 additions and 14 deletions.
50 changes: 36 additions & 14 deletions src/Renode/Network/NetworkServer/Modules/IcmpServerModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ public void HandleIcmpPacket(Action<EthernetFrame> FrameReady, IPv4Packet ipv4Pa
ParentServer.Log(LogLevel.Warning, "Failed to create an ICMPv4 response for this packet: {0}", (ICMPv4Packet)ipv4Packet.PayloadPacket);
return;
}

ParentServer.Log(LogLevel.Noisy, "Dupa 1");
var ipv4ResponsePacket = CreateIPv4Packet(ipv4Packet);

ParentServer.Log(LogLevel.Noisy, "Dupa 2");
if (!CreateEthernetFramePacket(ipv4ResponsePacket, icmpv4PacketResponse, icmpDestinationAddress, out var response))
{
ParentServer.Log(LogLevel.Warning, "Failed to create an EthernetFramePacket response for this packet: {0}", (ICMPv4Packet)ipv4Packet.PayloadPacket);
Expand Down Expand Up @@ -85,7 +85,7 @@ private bool CreateIcmpResponse(IPv4Packet ipv4Packet, out ICMPv4Packet icmpPack
}

icmpPacketResponse = CreateIcmpv4Packet(ipv4Packet, byteReply);
ParentServer.Log(LogLevel.Noisy, "Created an ICMPv4 response: {0}", icmpPacketResponse.PayloadPacket);
ParentServer.Log(LogLevel.Noisy, "Created an ICMPv4 response: {0}", icmpPacketResponse);
return true;
}

Expand All @@ -101,10 +101,9 @@ private bool GetReplyIfRequestSupported(IPv4Packet ipv4Packet, out byte[] byteRe
{
// byteReply = null;
byteReply = new byte[8];
for (var i=0; i<byteReply.Length; i++)
{
for (var i = 0; i < byteReply.Length; i++)
byteReply[i] = 0;
}


var ipv4PacketPayload = (ICMPv4Packet)ipv4Packet.PayloadPacket;
ParentServer.Log(LogLevel.Noisy, "Getting a reply if we support it");
Expand All @@ -124,8 +123,9 @@ private bool GetReplyIfRequestSupported(IPv4Packet ipv4Packet, out byte[] byteRe
}
ParentServer.Log(LogLevel.Noisy, "The ICMP code is supported so we service it: {0}", ipv4PacketPayload);

(BitConverter.GetBytes((ushort)ICMPv4TypeCodes.EchoReply)).CopyTo(byteReply, 0);
ParentServer.Log(LogLevel.Noisy, "Created a byte reply to the ICMP request: {0}", String.Join(",", byteReply.ToString()));
BitConverter.GetBytes((ushort)ICMPv4TypeCodes.EchoReply).CopyTo(byteReply, 0);
BitConverter.GetBytes(ipv4PacketPayload.Checksum).CopyTo(byteReply, 2);
ParentServer.Log(LogLevel.Noisy, "Created a byte reply to the ICMP request: {0}", byteReply.Length);
return true;
}

Expand All @@ -140,28 +140,50 @@ private ICMPv4Packet CreateIcmpv4Packet(IPv4Packet ipv4Packet, byte[] byteReply)
{
ParentServer.Log(LogLevel.Noisy, "Creating an ICMPv4 response packet");
var icmpv4Packet = (ICMPv4Packet)ipv4Packet.PayloadPacket;
var icmpv4PacketResponse = new ICMPv4Packet(new ByteArraySegment(byteReply));

ParentServer.Log(LogLevel.Noisy, "1");
var byteArrayReply = new ByteArraySegment(byteReply);
ParentServer.Log(LogLevel.Noisy, "2");
var icmpv4PacketResponse = new ICMPv4Packet(byteArrayReply);
ParentServer.Log(LogLevel.Noisy, "3");
/****************************************************************
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Type | Code | Checksum |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Identifier | Sequence Number |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
****************************************************************/
// We can copy that from request packet because they are the same in the request and the reply
ParentServer.Log(LogLevel.Noisy, "icmpv4PacketResponse.Data length: {0}", icmpv4PacketResponse.Data.Length);
ParentServer.Log(LogLevel.Noisy, "icmpv4Packet.Data length: {0}", icmpv4Packet.Data.Length);
//TODO: icmp4PacketResponse.Data = new byte[icmpv4Packet.Data.Length];
//for(var i=0; i<icmpv4PacketResponse.Data.Length; i++) icmp4PacketResponse.Data[i]=0;
icmpv4PacketResponse.Data = new byte[icmpv4Packet.Data.Length];
for (var i = 0; i < icmpv4PacketResponse.Data.Length; i++) icmpv4PacketResponse.Data[i] = 0;
icmpv4Packet.Data.CopyTo(icmpv4PacketResponse.Data, 0);
ParentServer.Log(LogLevel.Noisy, "4");
icmpv4PacketResponse.ID = icmpv4Packet.ID;
ParentServer.Log(LogLevel.Noisy, "5");
icmpv4PacketResponse.Sequence = icmpv4Packet.Sequence;
ParentServer.Log(LogLevel.Noisy, "6");

ParentServer.Log(LogLevel.Noisy, "Created ICMPv4 response packet: {0}", icmpv4PacketResponse.PayloadPacket);
return icmpv4PacketResponse;
}


/// <summary>
/// Creates an IPv4 response packet
/// Creates an IPv4 response packet from a given IPv4 packet
/// </summary>
/// <param name="ipv4Packet"></param>
/// <returns></returns>
private IPv4Packet CreateIPv4Packet(IPv4Packet ipv4Packet)
{
var ipv4PacketResponse = new IPv4Packet(IP,
((IPv4Packet)ipv4Packet.ParentPacket).SourceAddress);

ParentServer.Log(LogLevel.Noisy, "Creating IPv4 packet response");
// var ipv4PacketResponse = new IPv4Packet(IP,
// ((IPv4Packet)ipv4Packet.ParentPacket).SourceAddress);
var ipv4PacketResponse = new IPv4Packet(IP, ipv4Packet.SourceAddress);
ParentServer.Log(LogLevel.Noisy, "Created IPv4 packet response");
return ipv4PacketResponse;
}
Expand Down

0 comments on commit d1108ea

Please sign in to comment.