本文整理汇总了C#中System.Random.NextTcpLayer方法的典型用法代码示例。如果您正苦于以下问题:C# Random.NextTcpLayer方法的具体用法?C# Random.NextTcpLayer怎么用?C# Random.NextTcpLayer使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Random
的用法示例。
在下文中一共展示了Random.NextTcpLayer方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: RandomTcpTest
public void RandomTcpTest()
{
MacAddress ethernetSource = new MacAddress("00:01:02:03:04:05");
MacAddress ethernetDestination = new MacAddress("A0:A1:A2:A3:A4:A5");
EthernetLayer ethernetLayer = new EthernetLayer
{
Source = ethernetSource,
Destination = ethernetDestination
};
int seed = new Random().Next();
Console.WriteLine("Seed: " + seed);
Random random = new Random(seed);
for (int i = 0; i != 1000; ++i)
{
IpV4Layer ipV4Layer = random.NextIpV4Layer(null);
ipV4Layer.HeaderChecksum = null;
IpV6Layer ipV6Layer = random.NextIpV6Layer(IpV4Protocol.Tcp, false);
EthernetType ethernetType = random.NextBool() ? EthernetType.IpV4 : EthernetType.IpV6;
Layer ipLayer = (ethernetType == EthernetType.IpV4 ? (Layer)ipV4Layer : ipV6Layer);
TcpLayer tcpLayer = random.NextTcpLayer();
PayloadLayer payloadLayer = random.NextPayloadLayer(random.Next(60000));
Packet packet = PacketBuilder.Build(DateTime.Now, ethernetLayer, ipLayer, tcpLayer, payloadLayer);
Assert.IsTrue(packet.IsValid);
// Ethernet
ethernetLayer.EtherType = ethernetType;
Assert.AreEqual(ethernetLayer, packet.Ethernet.ExtractLayer(), "Ethernet Layer");
ethernetLayer.EtherType = EthernetType.None;
// Ip.
if (ipLayer == ipV4Layer)
{
// IpV4.
ipV4Layer.Protocol = IpV4Protocol.Tcp;
ipV4Layer.HeaderChecksum = ((IpV4Layer)packet.Ethernet.IpV4.ExtractLayer()).HeaderChecksum;
Assert.AreEqual(ipV4Layer, packet.Ethernet.IpV4.ExtractLayer(), "IPv4 Layer");
ipV4Layer.HeaderChecksum = null;
}
else
{
Assert.AreEqual(ipV6Layer, packet.Ethernet.IpV6.ExtractLayer(), "IPv6 Layer");
}
// TCP
tcpLayer.Checksum = packet.Ethernet.Ip.Tcp.Checksum;
Assert.AreEqual(tcpLayer, packet.Ethernet.Ip.Tcp.ExtractLayer(), "TCP Layer");
Assert.AreNotEqual(random.NextTcpLayer(), packet.Ethernet.Ip.Tcp.ExtractLayer(), "TCP Layer");
Assert.AreEqual(tcpLayer.GetHashCode(), packet.Ethernet.Ip.Tcp.ExtractLayer().GetHashCode(), "TCP Layer");
Assert.AreNotEqual(random.NextTcpLayer().GetHashCode(), packet.Ethernet.Ip.Tcp.ExtractLayer().GetHashCode(), "TCP Layer");
Assert.AreEqual((uint)(packet.Ethernet.Ip.Tcp.SequenceNumber + packet.Ethernet.Ip.Tcp.PayloadLength), packet.Ethernet.Ip.Tcp.NextSequenceNumber);
foreach (TcpOption option in packet.Ethernet.Ip.Tcp.Options.OptionsCollection)
{
Assert.AreEqual(option, option);
Assert.AreEqual(option.GetHashCode(), option.GetHashCode());
Assert.IsFalse(string.IsNullOrEmpty(option.ToString()));
Assert.IsFalse(option.Equals(null));
Assert.IsFalse(option.Equals(2));
}
Assert.AreEqual(tcpLayer.Options, packet.Ethernet.Ip.Tcp.Options, "Options");
Assert.AreEqual((tcpLayer.ControlBits & TcpControlBits.Acknowledgment) == TcpControlBits.Acknowledgment, packet.Ethernet.Ip.Tcp.IsAcknowledgment, "IsAcknowledgment");
Assert.AreEqual((tcpLayer.ControlBits & TcpControlBits.CongestionWindowReduced) == TcpControlBits.CongestionWindowReduced, packet.Ethernet.Ip.Tcp.IsCongestionWindowReduced, "IsCongestionWindowReduced");
Assert.AreEqual((tcpLayer.ControlBits & TcpControlBits.ExplicitCongestionNotificationEcho) == TcpControlBits.ExplicitCongestionNotificationEcho, packet.Ethernet.Ip.Tcp.IsExplicitCongestionNotificationEcho, "IsExplicitCongestionNotificationEcho");
Assert.AreEqual((tcpLayer.ControlBits & TcpControlBits.Fin) == TcpControlBits.Fin, packet.Ethernet.Ip.Tcp.IsFin, "IsFin");
Assert.AreEqual((tcpLayer.ControlBits & TcpControlBits.Push) == TcpControlBits.Push, packet.Ethernet.Ip.Tcp.IsPush, "IsPush");
Assert.AreEqual((tcpLayer.ControlBits & TcpControlBits.Reset) == TcpControlBits.Reset, packet.Ethernet.Ip.Tcp.IsReset, "IsReset");
Assert.AreEqual((tcpLayer.ControlBits & TcpControlBits.Synchronize) == TcpControlBits.Synchronize, packet.Ethernet.Ip.Tcp.IsSynchronize, "IsSynchronize");
Assert.AreEqual((tcpLayer.ControlBits & TcpControlBits.Urgent) == TcpControlBits.Urgent, packet.Ethernet.Ip.Tcp.IsUrgent, "IsUrgent");
Assert.AreEqual(0, packet.Ethernet.Ip.Tcp.Reserved);
Assert.IsFalse(packet.Ethernet.Ip.Tcp.IsChecksumOptional, "IsChecksumOptional");
Assert.AreEqual(TcpDatagram.HeaderMinimumLength + tcpLayer.Options.BytesLength + payloadLayer.Length, packet.Ethernet.Ip.Tcp.Length, "Total Length");
Assert.IsTrue(packet.Ethernet.Ip.IsTransportChecksumCorrect, "IsTransportChecksumCorrect");
Assert.AreEqual(payloadLayer.Data, packet.Ethernet.Ip.Tcp.Payload, "Payload");
}
}
示例2: BuildPacket
private static Packet BuildPacket(string httpString)
{
MacAddress ethernetSource = new MacAddress("00:01:02:03:04:05");
MacAddress ethernetDestination = new MacAddress("A0:A1:A2:A3:A4:A5");
EthernetLayer ethernetLayer = new EthernetLayer
{
Source = ethernetSource,
Destination = ethernetDestination
};
Random random = new Random();
IpV4Layer ipV4Layer = random.NextIpV4Layer(null);
ipV4Layer.HeaderChecksum = null;
TcpLayer tcpLayer = random.NextTcpLayer();
PayloadLayer payloadLayer = new PayloadLayer
{
Data = new Datagram(Encoding.ASCII.GetBytes(httpString))
};
Packet packet = PacketBuilder.Build(DateTime.Now, ethernetLayer, ipV4Layer, tcpLayer, payloadLayer);
Assert.IsTrue(packet.IsValid);
// Ethernet
ethernetLayer.EtherType = EthernetType.IpV4;
Assert.AreEqual(ethernetLayer, packet.Ethernet.ExtractLayer(), "Ethernet Layer");
// IpV4
ipV4Layer.Protocol = IpV4Protocol.Tcp;
ipV4Layer.HeaderChecksum = ((IpV4Layer)packet.Ethernet.IpV4.ExtractLayer()).HeaderChecksum;
Assert.AreEqual(ipV4Layer, packet.Ethernet.IpV4.ExtractLayer(), "IP Layer");
ipV4Layer.HeaderChecksum = null;
// TCP
tcpLayer.Checksum = packet.Ethernet.IpV4.Tcp.Checksum;
Assert.AreEqual(tcpLayer, packet.Ethernet.IpV4.Tcp.ExtractLayer(), "TCP Layer");
return packet;
}
示例3: 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.");
}
}
示例4: RandomHttpTest
public void RandomHttpTest()
{
int seed = new Random().Next();
Console.WriteLine("Seed: " + seed);
Random random = new Random(seed);
for (int i = 0; i != 200; ++i)
{
EthernetLayer ethernetLayer = random.NextEthernetLayer(EthernetType.None);
IpV4Layer ipV4Layer = random.NextIpV4Layer(null);
ipV4Layer.HeaderChecksum = null;
Layer ipLayer = random.NextBool() ? (Layer)ipV4Layer : random.NextIpV6Layer(true);
TcpLayer tcpLayer = random.NextTcpLayer();
tcpLayer.Checksum = null;
HttpLayer httpLayer = random.NextHttpLayer();
Packet packet = PacketBuilder.Build(DateTime.Now, ethernetLayer, ipLayer, tcpLayer, httpLayer);
Assert.IsTrue(packet.IsValid, "IsValid");
HttpDatagram httpDatagram = packet.Ethernet.Ip.Tcp.Http;
Assert.AreEqual(httpLayer.Version, httpDatagram.Version);
if (httpLayer.Version != null)
Assert.AreEqual(httpLayer.Version.GetHashCode(), httpDatagram.Version.GetHashCode());
if (httpLayer is HttpRequestLayer)
{
Assert.IsTrue(httpDatagram.IsRequest);
Assert.IsTrue(httpLayer.IsRequest);
Assert.IsFalse(httpDatagram.IsResponse);
Assert.IsFalse(httpLayer.IsResponse);
HttpRequestLayer httpRequestLayer = (HttpRequestLayer)httpLayer;
HttpRequestDatagram httpRequestDatagram = (HttpRequestDatagram)httpDatagram;
Assert.AreEqual(httpRequestLayer.Method, httpRequestDatagram.Method);
if (httpRequestLayer.Method != null)
{
Assert.AreEqual(httpRequestLayer.Method.GetHashCode(), httpRequestDatagram.Method.GetHashCode());
Assert.AreEqual(httpRequestLayer.Method.KnownMethod, httpRequestDatagram.Method.KnownMethod);
}
Assert.AreEqual(httpRequestLayer.Uri, httpRequestDatagram.Uri);
}
else
{
Assert.IsFalse(httpDatagram.IsRequest);
Assert.IsFalse(httpLayer.IsRequest);
Assert.IsTrue(httpDatagram.IsResponse);
Assert.IsTrue(httpLayer.IsResponse);
HttpResponseLayer httpResponseLayer = (HttpResponseLayer)httpLayer;
HttpResponseDatagram httpResponseDatagram = (HttpResponseDatagram)httpDatagram;
Assert.AreEqual(httpResponseLayer.StatusCode, httpResponseDatagram.StatusCode);
Assert.AreEqual(httpResponseLayer.ReasonPhrase, httpResponseDatagram.ReasonPhrase);
}
Assert.AreEqual(httpLayer.Header, httpDatagram.Header);
if (httpLayer.Header != null)
{
Assert.AreEqual(httpLayer.Header.GetHashCode(), httpDatagram.Header.GetHashCode());
foreach (var field in httpLayer.Header)
Assert.IsFalse(field.Equals("abc"));
foreach (var field in (IEnumerable)httpLayer.Header)
Assert.IsFalse(field.Equals("abc"));
MoreAssert.AreSequenceEqual(httpLayer.Header.Select(field => field.GetHashCode()), httpDatagram.Header.Select(field => field.GetHashCode()));
if (httpLayer.Header.ContentType != null)
{
var parameters = httpLayer.Header.ContentType.Parameters;
Assert.IsNotNull(((IEnumerable)parameters).GetEnumerator());
Assert.AreEqual<object>(parameters, httpDatagram.Header.ContentType.Parameters);
Assert.AreEqual(parameters.GetHashCode(), httpDatagram.Header.ContentType.Parameters.GetHashCode());
Assert.AreEqual(parameters.Count, httpDatagram.Header.ContentType.Parameters.Count);
int maxParameterNameLength = parameters.Any() ? parameters.Max(pair => pair.Key.Length) : 0;
Assert.IsNull(parameters[new string('a', maxParameterNameLength + 1)]);
}
}
Assert.AreEqual(httpLayer.Body, httpDatagram.Body);
Assert.AreEqual(httpLayer, httpDatagram.ExtractLayer(), "HTTP Layer");
Assert.AreEqual(httpLayer.Length, httpDatagram.Length);
}
}
示例5: 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.");
}
}