Пример #1
0
        public PingResult(Packet packet, DateTime ts, PingRequest req)
        {
            Result = PingResultType.Reply;
            var reply = packet.Ethernet.IpV4.Icmp as IcmpEchoReplyDatagram;

            Ttl            = packet.Ethernet.IpV4.Ttl;
            RespondingHost = new Common.IPv4Address(packet.Ethernet.IpV4.Source.ToString());
            Response       = (int)(ts - req.TimeStamp).TotalMilliseconds;
        }
Пример #2
0
        private void SendIcmpRequestPacket(Common.IPv4Address ip, byte ttl, ushort bytes, int seqNum, int timeout, PingResultHandler callback)
        {
            OSI.Layer3Packet packet = new OSI.Layer3Packet();
            packet.Destination = ip.AsString;
            packet.SourceIP    = _client.Configuration.IpAddress;
            packet.Ttl         = ttl;

            var data = new byte[bytes];

            _random.NextBytes(data);

            var shor = new byte[2];

            _random.NextBytes(shor);
            ushort id = BitConverter.ToUInt16(shor, 0);

            // Save ping
            if (CurrentPings.ContainsKey(id))
            {
                CurrentPings[id] = new PingRequest(callback);
            }
            else
            {
                PingRequest req = new PingRequest(callback);
                req.Bytes = bytes;
                CurrentPings.Add(id, req);
            }

            PcapDotNet.Packets.Icmp.IcmpEchoLayer icmpLayer =
                new PcapDotNet.Packets.Icmp.IcmpEchoLayer
            {
                Checksum       = null,
                Identifier     = id,
                SequenceNumber = (ushort)(seqNum),
            };
            PcapDotNet.Packets.PayloadLayer extra = new PcapDotNet.Packets.PayloadLayer()
            {
                Data = new PcapDotNet.Packets.Datagram(data)
            };

            packet.NextLayers.Add(icmpLayer);
            packet.NextLayers.Add(extra);



            SendPacket(packet, () =>
            {
                CurrentPings[id].TimeStamp = DateTime.Now;
            });

            DateTime now = DateTime.Now;

            while ((DateTime.Now - now).TotalMilliseconds < timeout)
            {
                if (CurrentPings[id].ReplyReceived)
                {
                    break;
                }
                System.Threading.Thread.Sleep(100);
            }

            if (!CurrentPings[id].ReplyReceived)
            {
                CurrentPings[id].Callback(new PingResult(PingResultType.RequestTimedOut));
            }
            CurrentPings.Remove(id);
        }