Commit bbaf71fd authored by Brickner_cp's avatar Brickner_cp

Packet building examples.

parent 97ea93e2
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;
using PcapDotNet.Base;
using PcapDotNet.Core; using PcapDotNet.Core;
using PcapDotNet.Packets; using PcapDotNet.Packets;
using PcapDotNet.Packets.Arp;
using PcapDotNet.Packets.Dns;
using PcapDotNet.Packets.Ethernet; using PcapDotNet.Packets.Ethernet;
using PcapDotNet.Packets.Gre;
using PcapDotNet.Packets.Http;
using PcapDotNet.Packets.Icmp; using PcapDotNet.Packets.Icmp;
using PcapDotNet.Packets.Igmp;
using PcapDotNet.Packets.IpV4; using PcapDotNet.Packets.IpV4;
using PcapDotNet.Packets.Transport;
using PcapDotNet.Packets.VLanTaggedFrame;
namespace SendingASinglePacketWithSendPacket namespace SendingASinglePacketWithSendPacket
{ {
...@@ -52,11 +62,11 @@ namespace SendingASinglePacketWithSendPacket ...@@ -52,11 +62,11 @@ namespace SendingASinglePacketWithSendPacket
PacketDeviceOpenAttributes.Promiscuous, // promiscuous mode PacketDeviceOpenAttributes.Promiscuous, // promiscuous mode
1000)) // read timeout 1000)) // read timeout
{ {
// Supposing to be on ethernet, set mac source to 1:1:1:1:1:1 // Supposing to be on ethernet, set mac source to 01:01:01:01:01:01
MacAddress source = new MacAddress("1:1:1:1:1:1"); MacAddress source = new MacAddress("01:01:01:01:01:01");
// set mac destination to 2:2:2:2:2:2 // set mac destination to 02:02:02:02:02:02
MacAddress destination = new MacAddress("2:2:2:2:2:2"); MacAddress destination = new MacAddress("02:02:02:02:02:02");
// Create the packets layers // Create the packets layers
...@@ -85,7 +95,7 @@ namespace SendingASinglePacketWithSendPacket ...@@ -85,7 +95,7 @@ namespace SendingASinglePacketWithSendPacket
for (int i = 0; i != 100; ++i) for (int i = 0; i != 100; ++i)
{ {
// Set IPv4 parameters // Set IPv4 parameters
ipV4Layer.Destination = new IpV4Address("2.3.4." + i); ipV4Layer.CurrentDestination = new IpV4Address("2.3.4." + i);
ipV4Layer.Identification = (ushort)i; ipV4Layer.Identification = (ushort)i;
// Set ICMP parameters // Set ICMP parameters
...@@ -98,7 +108,465 @@ namespace SendingASinglePacketWithSendPacket ...@@ -98,7 +108,465 @@ namespace SendingASinglePacketWithSendPacket
// Send down the packet // Send down the packet
communicator.SendPacket(packet); communicator.SendPacket(packet);
} }
communicator.SendPacket(BuildEthernetPacket());
communicator.SendPacket(BuildArpPacket());
communicator.SendPacket(BuildVLanTaggedFramePacket());
communicator.SendPacket(BuildIpV4Packet());
communicator.SendPacket(BuildIcmpPacket());
communicator.SendPacket(BuildIgmpPacket());
communicator.SendPacket(BuildGrePacket());
communicator.SendPacket(BuildUdpPacket());
communicator.SendPacket(BuildTcpPacket());
communicator.SendPacket(BuildDnsPacket());
communicator.SendPacket(BuildHttpPacket());
}
}
/// <summary>
/// This function build an Ethernet with payload packet.
/// </summary>
private static Packet BuildEthernetPacket()
{
EthernetLayer ethernetLayer = new EthernetLayer
{
Source = new MacAddress("01:01:01:01:01:01"),
Destination = new MacAddress("02:02:02:02:02:02"),
EtherType = EthernetType.IpV4,
};
PayloadLayer payloadLayer = new PayloadLayer
{
Data = new Datagram(Encoding.ASCII.GetBytes("hello world")),
};
PacketBuilder builder = new PacketBuilder(ethernetLayer, payloadLayer);
return builder.Build(DateTime.Now);
}
/// <summary>
/// This function build an ARP over Ethernet packet.
/// </summary>
private static Packet BuildArpPacket()
{
EthernetLayer ethernetLayer = new EthernetLayer
{
Source = new MacAddress("01:01:01:01:01:01"),
Destination = new MacAddress("02:02:02:02:02:02"),
EtherType = EthernetType.None, // Will be filled automatically.
};
ArpLayer arpLayer = new ArpLayer
{
ProtocolType = EthernetType.IpV4,
Operation = ArpOperation.Request,
SenderHardwareAddress = new byte[] {3, 3, 3, 3, 3, 3}.AsReadOnly(), // 03:03:03:03:03:03.
SenderProtocolAddress = new byte[] {1, 2, 3, 4}.AsReadOnly(), // 1.2.3.4.
TargetHardwareAddress = new byte[] {4, 4, 4, 4, 4, 4}.AsReadOnly(), // 04:04:04:04:04:04.
TargetProtocolAddress = new byte[] {11, 22, 33, 44}.AsReadOnly(), // 11.22.33.44.
};
PacketBuilder builder = new PacketBuilder(ethernetLayer, arpLayer);
return builder.Build(DateTime.Now);
} }
/// <summary>
/// This function build a VLanTaggedFrame over Ethernet with payload packet.
/// </summary>
private static Packet BuildVLanTaggedFramePacket()
{
EthernetLayer ethernetLayer = new EthernetLayer
{
Source = new MacAddress("01:01:01:01:01:01"),
Destination = new MacAddress("02:02:02:02:02:02"),
EtherType = EthernetType.None, // Will be filled automatically.
};
VLanTaggedFrameLayer vLanTaggedFrameLayer = new VLanTaggedFrameLayer
{
PriorityCodePoint = ClassOfService.Background,
CanonicalFormatIndicator = false,
VLanIdentifier = 50,
EtherType = EthernetType.IpV4,
};
PayloadLayer payloadLayer = new PayloadLayer
{
Data = new Datagram(Encoding.ASCII.GetBytes("hello world")),
};
PacketBuilder builder = new PacketBuilder(ethernetLayer, vLanTaggedFrameLayer, payloadLayer);
return builder.Build(DateTime.Now);
}
/// <summary>
/// This function build an IPv4 over Ethernet with payload packet.
/// </summary>
private static Packet BuildIpV4Packet()
{
EthernetLayer ethernetLayer = new EthernetLayer
{
Source = new MacAddress("01:01:01:01:01:01"),
Destination = new MacAddress("02:02:02:02:02:02"),
EtherType = EthernetType.None,
};
IpV4Layer ipV4Layer = new IpV4Layer
{
Source = new IpV4Address("1.2.3.4"),
CurrentDestination = new IpV4Address("11.22.33.44"),
Fragmentation = IpV4Fragmentation.None,
HeaderChecksum = null, // Will be filled automatically.
Identification = 123,
Options = IpV4Options.None,
Protocol = IpV4Protocol.Udp,
Ttl = 100,
TypeOfService = 0,
};
PayloadLayer payloadLayer = new PayloadLayer
{
Data = new Datagram(Encoding.ASCII.GetBytes("hello world")),
};
PacketBuilder builder = new PacketBuilder(ethernetLayer, ipV4Layer, payloadLayer);
return builder.Build(DateTime.Now);
}
/// <summary>
/// This function build an ICMP over IPv4 over Ethernet packet.
/// </summary>
private static Packet BuildIcmpPacket()
{
EthernetLayer ethernetLayer = new EthernetLayer
{
Source = new MacAddress("01:01:01:01:01:01"),
Destination = new MacAddress("02:02:02:02:02:02"),
EtherType = EthernetType.None, // Will be filled automatically.
};
IpV4Layer ipV4Layer = new IpV4Layer
{
Source = new IpV4Address("1.2.3.4"),
CurrentDestination = new IpV4Address("11.22.33.44"),
Fragmentation = IpV4Fragmentation.None,
HeaderChecksum = null, // Will be filled automatically.
Identification = 123,
Options = IpV4Options.None,
Protocol = null, // Will be filled automatically.
Ttl = 100,
TypeOfService = 0,
};
IcmpEchoLayer icmpLayer = new IcmpEchoLayer
{
Checksum = null, // Will be filled automatically.
Identifier = 456,
SequenceNumber = 800,
};
PacketBuilder builder = new PacketBuilder(ethernetLayer, ipV4Layer, icmpLayer);
return builder.Build(DateTime.Now);
}
/// <summary>
/// This function build an IGMP over IPv4 over Ethernet packet.
/// </summary>
private static Packet BuildIgmpPacket()
{
EthernetLayer ethernetLayer = new EthernetLayer
{
Source = new MacAddress("01:01:01:01:01:01"),
Destination = new MacAddress("02:02:02:02:02:02"),
EtherType = EthernetType.None, // Will be filled automatically.
};
IpV4Layer ipV4Layer = new IpV4Layer
{
Source = new IpV4Address("1.2.3.4"),
CurrentDestination = new IpV4Address("11.22.33.44"),
Fragmentation = IpV4Fragmentation.None,
HeaderChecksum = null, // Will be filled automatically.
Identification = 123,
Options = IpV4Options.None,
Protocol = null, // Will be filled automatically.
Ttl = 100,
TypeOfService = 0,
};
IgmpQueryVersion1Layer igmpLayer = new IgmpQueryVersion1Layer
{
GroupAddress = new IpV4Address("1.2.3.4"),
};
PacketBuilder builder = new PacketBuilder(ethernetLayer, ipV4Layer, igmpLayer);
return builder.Build(DateTime.Now);
}
/// <summary>
/// This function build an IPv4 over GRE over IPv4 over Ethernet packet.
/// </summary>
private static Packet BuildGrePacket()
{
EthernetLayer ethernetLayer = new EthernetLayer
{
Source = new MacAddress("01:01:01:01:01:01"),
Destination = new MacAddress("02:02:02:02:02:02"),
EtherType = EthernetType.None, // Will be filled automatically.
};
IpV4Layer ipV4Layer = new IpV4Layer
{
Source = new IpV4Address("1.2.3.4"),
CurrentDestination = new IpV4Address("11.22.33.44"),
Fragmentation = IpV4Fragmentation.None,
HeaderChecksum = null, // Will be filled automatically.
Identification = 123,
Options = IpV4Options.None,
Protocol = null, // Will be filled automatically.
Ttl = 100,
TypeOfService = 0,
};
GreLayer greLayer = new GreLayer
{
Version = GreVersion.Gre,
ProtocolType = EthernetType.None, // Will be filled automatically.
RecursionControl = 0,
FutureUseBits = 0,
ChecksumPresent = true,
Checksum = null, // Will be filled automatically.
Key = null,
SequenceNumber = 123,
AcknowledgmentSequenceNumber = null,
RoutingOffset = null,
Routing = null,
StrictSourceRoute = false,
};
IpV4Layer innerIpV4Layer = new IpV4Layer
{
Source = new IpV4Address("100.200.201.202"),
CurrentDestination = new IpV4Address("123.254.132.40"),
Fragmentation = IpV4Fragmentation.None,
HeaderChecksum = null, // Will be filled automatically.
Identification = 123,
Options = IpV4Options.None,
Protocol = IpV4Protocol.Udp,
Ttl = 120,
TypeOfService = 0,
};
PacketBuilder builder = new PacketBuilder(ethernetLayer, ipV4Layer, greLayer, innerIpV4Layer);
return builder.Build(DateTime.Now);
}
/// <summary>
/// This function build an UDP over IPv4 over Ethernet with payload packet.
/// </summary>
private static Packet BuildUdpPacket()
{
EthernetLayer ethernetLayer = new EthernetLayer
{
Source = new MacAddress("01:01:01:01:01:01"),
Destination = new MacAddress("02:02:02:02:02:02"),
EtherType = EthernetType.None, // Will be filled automatically.
};
IpV4Layer ipV4Layer = new IpV4Layer
{
Source = new IpV4Address("1.2.3.4"),
CurrentDestination = new IpV4Address("11.22.33.44"),
Fragmentation = IpV4Fragmentation.None,
HeaderChecksum = null, // Will be filled automatically.
Identification = 123,
Options = IpV4Options.None,
Protocol = null, // Will be filled automatically.
Ttl = 100,
TypeOfService = 0,
};
UdpLayer udpLayer = new UdpLayer
{
SourcePort = 4050,
DestinationPort = 25,
Checksum = null, // Will be filled automatically.
CalculateChecksumValue = true,
};
PayloadLayer payloadLayer = new PayloadLayer
{
Data = new Datagram(Encoding.ASCII.GetBytes("hello world")),
};
PacketBuilder builder = new PacketBuilder(ethernetLayer, ipV4Layer, udpLayer, payloadLayer);
return builder.Build(DateTime.Now);
}
/// <summary>
/// This function build an TCP over IPv4 over Ethernet with payload packet.
/// </summary>
private static Packet BuildTcpPacket()
{
EthernetLayer ethernetLayer = new EthernetLayer
{
Source = new MacAddress("01:01:01:01:01:01"),
Destination = new MacAddress("02:02:02:02:02:02"),
EtherType = EthernetType.None, // Will be filled automatically.
};
IpV4Layer ipV4Layer = new IpV4Layer
{
Source = new IpV4Address("1.2.3.4"),
CurrentDestination = new IpV4Address("11.22.33.44"),
Fragmentation = IpV4Fragmentation.None,
HeaderChecksum = null, // Will be filled automatically.
Identification = 123,
Options = IpV4Options.None,
Protocol = null, // Will be filled automatically.
Ttl = 100,
TypeOfService = 0,
};
TcpLayer tcpLayer = new TcpLayer()
{
SourcePort = 4050,
DestinationPort = 25,
Checksum = null, // Will be filled automatically.
SequenceNumber = 100,
AcknowledgmentNumber = 50,
ControlBits = TcpControlBits.Acknowledgment,
Window = 100,
UrgentPointer = 0,
Options = TcpOptions.None,
};
PayloadLayer payloadLayer = new PayloadLayer
{
Data = new Datagram(Encoding.ASCII.GetBytes("hello world")),
};
PacketBuilder builder = new PacketBuilder(ethernetLayer, ipV4Layer, tcpLayer, payloadLayer);
return builder.Build(DateTime.Now);
}
/// <summary>
/// This function build a DNS over UDP over IPv4 over Ethernet packet.
/// </summary>
private static Packet BuildDnsPacket()
{
EthernetLayer ethernetLayer = new EthernetLayer
{
Source = new MacAddress("01:01:01:01:01:01"),
Destination = new MacAddress("02:02:02:02:02:02"),
EtherType = EthernetType.None, // Will be filled automatically.
};
IpV4Layer ipV4Layer = new IpV4Layer
{
Source = new IpV4Address("1.2.3.4"),
CurrentDestination = new IpV4Address("11.22.33.44"),
Fragmentation = IpV4Fragmentation.None,
HeaderChecksum = null, // Will be filled automatically.
Identification = 123,
Options = IpV4Options.None,
Protocol = null, // Will be filled automatically.
Ttl = 100,
TypeOfService = 0,
};
UdpLayer udpLayer = new UdpLayer
{
SourcePort = 4050,
DestinationPort = 53,
Checksum = null, // Will be filled automatically.
CalculateChecksumValue = true,
};
DnsLayer dnsLayer = new DnsLayer
{
Id = 100,
IsResponse = false,
OpCode = DnsOpCode.Query,
IsAuthoritativeAnswer = false,
IsTruncated = false,
IsRecursionDesired = true,
IsRecursionAvailable = false,
FutureUse = false,
IsAuthenticData = false,
IsCheckingDisabled = false,
ResponseCode = DnsResponseCode.NoError,
Queries = new[] {new DnsQueryResourceRecord(new DnsDomainName("pcapdot.net"), DnsType.A, DnsClass.Internet),},
Answers = null,
Authorities = null,
Additionals = null,
DomainNameCompressionMode = DnsDomainNameCompressionMode.All,
};
PacketBuilder builder = new PacketBuilder(ethernetLayer, ipV4Layer, udpLayer, dnsLayer);
return builder.Build(DateTime.Now);
}
/// <summary>
/// This function build an HTTP over TCP over IPv4 over Ethernet packet.
/// </summary>
private static Packet BuildHttpPacket()
{
EthernetLayer ethernetLayer = new EthernetLayer
{
Source = new MacAddress("01:01:01:01:01:01"),
Destination = new MacAddress("02:02:02:02:02:02"),
EtherType = EthernetType.None, // Will be filled automatically.
};
IpV4Layer ipV4Layer = new IpV4Layer
{
Source = new IpV4Address("1.2.3.4"),
CurrentDestination = new IpV4Address("11.22.33.44"),
Fragmentation = IpV4Fragmentation.None,
HeaderChecksum = null, // Will be filled automatically.
Identification = 123,
Options = IpV4Options.None,
Protocol = null, // Will be filled automatically.
Ttl = 100,
TypeOfService = 0,
};
TcpLayer tcpLayer = new TcpLayer
{
SourcePort = 4050,
DestinationPort = 80,
Checksum = null, // Will be filled automatically.
SequenceNumber = 100,
AcknowledgmentNumber = 50,
ControlBits = TcpControlBits.Acknowledgment,
Window = 100,
UrgentPointer = 0,
Options = TcpOptions.None,
};
HttpRequestLayer httpLayer = new HttpRequestLayer
{
Version = HttpVersion.Version11,
Header = new HttpHeader(new HttpContentLengthField(11)),
Body = new Datagram(Encoding.ASCII.GetBytes("hello world")),
Method = new HttpRequestMethod(HttpRequestKnownMethod.Get),
Uri = @"http://pcapdot.net/",
};
PacketBuilder builder = new PacketBuilder(ethernetLayer, ipV4Layer, tcpLayer, httpLayer);
return builder.Build(DateTime.Now);
} }
} }
} }
...@@ -15,7 +15,7 @@ namespace PcapDotNet.Packets.Transport ...@@ -15,7 +15,7 @@ namespace PcapDotNet.Packets.Transport
/// </summary> /// </summary>
public override bool CalculateChecksum public override bool CalculateChecksum
{ {
get{return CalculateChecksumValue;} get { return CalculateChecksumValue; }
} }
/// <summary> /// <summary>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment