本文整理汇总了C#中System.Random.NextIcmpLayer方法的典型用法代码示例。如果您正苦于以下问题:C# Random.NextIcmpLayer方法的具体用法?C# Random.NextIcmpLayer怎么用?C# Random.NextIcmpLayer使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Random
的用法示例。
在下文中一共展示了Random.NextIcmpLayer方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CreateRandomIpV4Payload
private static void CreateRandomIpV4Payload(Random random, IpV4Layer ipV4Layer, List<ILayer> layers)
{
if (random.NextBool(20))
{
// Finish with payload.
PayloadLayer payloadLayer = random.NextPayloadLayer(random.Next(100));
layers.Add(payloadLayer);
return;
}
ipV4Layer.Protocol = null;
if (random.NextBool())
ipV4Layer.Fragmentation = IpV4Fragmentation.None;
switch (random.Next(0, 9))
{
case 0: // IpV4.
case 1:
IpV4Layer innerIpV4Layer = random.NextIpV4Layer();
layers.Add(innerIpV4Layer);
CreateRandomIpV4Payload(random, innerIpV4Layer, layers);
return;
case 2: // Igmp.
layers.Add(random.NextIgmpLayer());
return;
case 3: // Icmp.
IcmpLayer icmpLayer = random.NextIcmpLayer();
layers.Add(icmpLayer);
layers.AddRange(random.NextIcmpPayloadLayers(icmpLayer));
return;
case 4: // Gre.
GreLayer greLayer = random.NextGreLayer();
layers.Add(greLayer);
CreateRandomEthernetPayload(random, greLayer, layers);
return;
case 5: // Udp.
case 6:
UdpLayer udpLayer = random.NextUdpLayer();
layers.Add(udpLayer);
CreateRandomUdpPayload(random, udpLayer, layers);
return;
case 7: // Tcp.
case 8:
TcpLayer tcpLayer = random.NextTcpLayer();
layers.Add(tcpLayer);
CreateRandomTcpPayload(random, tcpLayer, layers);
return;
default:
throw new InvalidOperationException("Invalid value.");
}
}
示例2: CreateRandomIpPayload
private static void CreateRandomIpPayload(Random random, Layer ipLayer, List<ILayer> layers)
{
IpV6Layer ipV6Layer = ipLayer as IpV6Layer;
if (ipV6Layer != null)
{
var headers = ipV6Layer.ExtensionHeaders.Headers;
if (headers.Any() && headers.Last().Protocol == IpV4Protocol.EncapsulatingSecurityPayload)
return;
}
if (ipV6Layer != null ? ipV6Layer.LastNextHeader != null : random.NextBool(20))
{
// Finish with payload.
PayloadLayer payloadLayer = random.NextPayloadLayer(random.Next(100));
layers.Add(payloadLayer);
return;
}
IpV4Layer ipV4Layer = ipLayer as IpV4Layer;
if (ipV4Layer != null)
{
ipV4Layer.Protocol = null;
if (random.NextBool())
ipV4Layer.Fragmentation = IpV4Fragmentation.None;
}
switch (random.Next(0, 11))
{
case 0: // IpV4.
case 1:
IpV4Layer innerIpV4Layer = random.NextIpV4Layer();
layers.Add(innerIpV4Layer);
CreateRandomIpPayload(random, innerIpV4Layer, layers);
return;
case 2: // IpV6.
case 3:
IpV6Layer innerIpV6Layer = random.NextIpV6Layer(random.NextBool(20));
layers.Add(innerIpV6Layer);
CreateRandomIpPayload(random, innerIpV6Layer, layers);
return;
case 4: // Igmp.
layers.Add(random.NextIgmpLayer());
return;
case 5: // Icmp.
IcmpLayer icmpLayer = random.NextIcmpLayer();
layers.Add(icmpLayer);
layers.AddRange(random.NextIcmpPayloadLayers(icmpLayer));
return;
case 6: // Gre.
GreLayer greLayer = random.NextGreLayer();
layers.Add(greLayer);
CreateRandomEthernetPayload(random, greLayer, layers);
return;
case 7: // Udp.
case 8:
UdpLayer udpLayer = random.NextUdpLayer();
layers.Add(udpLayer);
CreateRandomUdpPayload(random, udpLayer, layers);
return;
case 9: // Tcp.
case 10:
TcpLayer tcpLayer = random.NextTcpLayer();
layers.Add(tcpLayer);
CreateRandomTcpPayload(random, tcpLayer, layers);
return;
default:
throw new InvalidOperationException("Invalid value.");
}
}
示例3: RandomIcmpTest
public void RandomIcmpTest()
{
EthernetLayer ethernetLayer = new EthernetLayer
{
Source = new MacAddress("00:01:02:03:04:05"),
Destination = new MacAddress("A0:A1:A2:A3:A4:A5")
};
int seed = new Random().Next();
Console.WriteLine("Seed: " + seed);
Random random = new Random(seed);
for (int i = 0; i != 2000; ++i)
{
IpV4Layer ipV4Layer = random.NextIpV4Layer(null);
ipV4Layer.HeaderChecksum = null;
Layer ipLayer = random.NextBool() ? (Layer)ipV4Layer : random.NextIpV6Layer(IpV4Protocol.InternetControlMessageProtocol, false);
IcmpLayer icmpLayer = random.NextIcmpLayer();
icmpLayer.Checksum = null;
if (icmpLayer.MessageType == IcmpMessageType.DestinationUnreachable &&
icmpLayer.MessageTypeAndCode != IcmpMessageTypeAndCode.DestinationUnreachableFragmentationNeededAndDoNotFragmentSet)
{
((IcmpDestinationUnreachableLayer)icmpLayer).NextHopMaximumTransmissionUnit = 0;
}
IEnumerable<ILayer> icmpPayloadLayers = random.NextIcmpPayloadLayers(icmpLayer);
int icmpPayloadLength = icmpPayloadLayers.Select(layer => layer.Length).Sum();
switch (icmpLayer.MessageType)
{
case IcmpMessageType.ParameterProblem:
if (icmpPayloadLength % 4 != 0)
icmpPayloadLayers = icmpPayloadLayers.Concat(new[] {new PayloadLayer {Data = random.NextDatagram(4 - icmpPayloadLength % 4)}});
icmpPayloadLength = icmpPayloadLayers.Select(layer => layer.Length).Sum();
IcmpParameterProblemLayer icmpParameterProblemLayer = (IcmpParameterProblemLayer)icmpLayer;
icmpParameterProblemLayer.Pointer = (byte)(icmpParameterProblemLayer.Pointer % icmpPayloadLength);
icmpParameterProblemLayer.OriginalDatagramLength = icmpPayloadLength - icmpPayloadLayers.First().Length;
break;
case IcmpMessageType.SecurityFailures:
((IcmpSecurityFailuresLayer)icmpLayer).Pointer %= (ushort)icmpPayloadLength;
break;
}
PacketBuilder packetBuilder = new PacketBuilder(new ILayer[] { ethernetLayer, ipLayer, icmpLayer }.Concat(icmpPayloadLayers));
Packet packet = packetBuilder.Build(DateTime.Now);
Assert.IsTrue(packet.IsValid, "IsValid");
byte[] buffer = (byte[])packet.Buffer.Clone();
buffer.Write(ethernetLayer.Length + ipLayer.Length, random.NextDatagram(icmpLayer.Length));
Packet illegalPacket = new Packet(buffer, DateTime.Now, packet.DataLink);
Assert.IsFalse(illegalPacket.IsValid, "IsInvalid");
if (illegalPacket.Ethernet.Ip.Icmp is IcmpUnknownDatagram)
{
byte[] icmpBuffer = new byte[illegalPacket.Ethernet.Ip.Icmp.ExtractLayer().Length];
ILayer layer = illegalPacket.Ethernet.Ip.Icmp.ExtractLayer();
layer.Write(icmpBuffer,0,icmpBuffer.Length, null,null);
layer.Finalize(icmpBuffer,0,icmpBuffer.Length,null);
MoreAssert.AreSequenceEqual(illegalPacket.Ethernet.Ip.Icmp.ToArray(),
icmpBuffer);
Assert.AreEqual(illegalPacket,
PacketBuilder.Build(DateTime.Now, ethernetLayer, ipLayer, illegalPacket.Ethernet.Ip.Icmp.ExtractLayer()));
}
// Ethernet
ethernetLayer.EtherType = ipLayer == ipV4Layer ? EthernetType.IpV4 : EthernetType.IpV6;
Assert.AreEqual(ethernetLayer, packet.Ethernet.ExtractLayer(), "Ethernet Layer");
ethernetLayer.EtherType = EthernetType.None;
// IP.
if (ipLayer == ipV4Layer)
{
// IPv4.
ipV4Layer.Protocol = IpV4Protocol.InternetControlMessageProtocol;
ipV4Layer.HeaderChecksum = ((IpV4Layer)packet.Ethernet.IpV4.ExtractLayer()).HeaderChecksum;
Assert.AreEqual(ipV4Layer, packet.Ethernet.IpV4.ExtractLayer());
ipV4Layer.HeaderChecksum = null;
Assert.AreEqual(ipV4Layer.Length, packet.Ethernet.IpV4.HeaderLength);
Assert.IsTrue(packet.Ethernet.IpV4.IsHeaderChecksumCorrect);
Assert.AreEqual(ipV4Layer.Length + icmpLayer.Length + icmpPayloadLength,
packet.Ethernet.IpV4.TotalLength);
Assert.AreEqual(IpV4Datagram.DefaultVersion, packet.Ethernet.IpV4.Version);
}
else
{
// IPv6.
Assert.AreEqual(ipLayer, packet.Ethernet.IpV6.ExtractLayer());
}
// ICMP
IcmpDatagram actualIcmp = packet.Ethernet.Ip.Icmp;
IcmpLayer actualIcmpLayer = (IcmpLayer)actualIcmp.ExtractLayer();
icmpLayer.Checksum = actualIcmpLayer.Checksum;
Assert.AreEqual(icmpLayer, actualIcmpLayer);
Assert.AreEqual(icmpLayer.GetHashCode(), actualIcmpLayer.GetHashCode());
if (actualIcmpLayer.MessageType != IcmpMessageType.RouterSolicitation)
//.........这里部分代码省略.........