Ejemplo n.º 1
0
        /// <summary>
        ///		This event occurs whenever a new packet arrives.
        /// </summary>
        /// <param name="data">
        ///		The data in the packet.
        ///	</param>
        private void NewPacket(byte[] data)
        {
            if (!m_working)
            {
                return;
            }

            // store the time the packet arrived
            m_stop = Environment.TickCount;

            // parse out the ip header
            IpV4Packet ipPacket = new IpV4Packet (data);

            // double check to make sure this is an icmp packet
            if (ipPacket.TransportProtocol != ProtocolType.Icmp)
            {
                return;
            }

            // parse out the icmp packet
            IcmpPacket icmpPacket = new IcmpPacket (ipPacket.Data);

            // if this is an echo reply
            if (icmpPacket.MessageType == IcmpMessageType.EchoReply)
            {
                #region Reply From Destination

                // deserialize the packet
                IcmpEcho echo = new IcmpEcho (icmpPacket);

                // if this packet matches our identifier, and destination address
                if (echo.Identifier == m_id)
                {
                    // disable the timeout timer
                    m_timer.Enabled = false;

                    // raise the event
                    if (RouteUpdate != null)
                    {
                        RouteUpdate (ipPacket.SourceAddress, (int)((uint)m_stop - (uint)m_start), m_ttl);
                    }

                    // raise the event
                    if (TraceFinished != null)
                    {
                        TraceFinished ();
                    }

                    // signal the wait event
                    m_waitObject.Set ();

                    // allow new trace routes
                    m_working = false;
                }

                #endregion
            }

            // if the time to live exceeded then we have a new hop
            else if (icmpPacket.MessageType == IcmpMessageType.TimeExceeded)
            {
                #region Reply From Router

                IcmpTimeExceeded icmpTimeExceeded = new IcmpTimeExceeded (icmpPacket);

                // make sure this packet is for us
                if (m_ipId != icmpTimeExceeded.BadPacket.Identification)
                {
                    return;
                }

                // disable the timeout timer
                m_timer.Enabled = false;

                // raise the event
                if (RouteUpdate != null && m_working)
                {
                    RouteUpdate (ipPacket.SourceAddress, (int)((uint)m_stop - (uint)m_start), m_ttl);
                }

                // increment the time to live.
                m_ttl++;

                // if the max hop count is exceeded
                if (m_ttl > m_maxHops)
                {
                    // disable the timeout timer
                    m_timer.Enabled = false;

                    // allow new trace routes
                    m_working = false;

                    // raise the event
                    if (MaxHopsExceeded != null)
                    {
                        MaxHopsExceeded ();
                    }

                    // signal the wait event
                    m_waitObject.Set ();
                }
                else
                {
                    // send the next request
                    SendRequest ();
                }

                #endregion
            }
        }
        /// <summary>
        ///		This event occurs whenever a new packet arrives.
        /// </summary>
        /// <param name="data">
        ///		The data in the packet.
        ///	</param>
        private void NewPacket(byte[] data)
        {
            if (!m_working)
            {
                return;
            }

            // store the time the packet arrived
            m_stop = Environment.TickCount;

            // parse out the ip header
            IpV4Packet ipPacket = new IpV4Packet(data);

            // double check to make sure this is an icmp packet
            if (ipPacket.TransportProtocol != ProtocolType.Icmp)
            {
                return;
            }

            // parse out the icmp packet
            IcmpPacket icmpPacket = new IcmpPacket(ipPacket.Data);

            // if this is an echo reply
            if (icmpPacket.MessageType == IcmpMessageType.EchoReply)
            {
                #region Reply From Destination

                // deserialize the packet
                IcmpEcho echo = new IcmpEcho(icmpPacket);

                // if this packet matches our identifier, and destination address
                if (echo.Identifier == m_id)
                {
                    // disable the timeout timer
                    m_timer.Enabled = false;

                    // raise the event
                    if (RouteUpdate != null)
                    {
                        RouteUpdate(ipPacket.SourceAddress, (int)((uint)m_stop - (uint)m_start), m_ttl);
                    }

                    // raise the event
                    if (TraceFinished != null)
                    {
                        TraceFinished();
                    }

                    // signal the wait event
                    m_waitObject.Set();

                    // allow new trace routes
                    m_working = false;
                }

                #endregion
            }

            // if the time to live exceeded then we have a new hop
            else if (icmpPacket.MessageType == IcmpMessageType.TimeExceeded)
            {
                #region Reply From Router

                IcmpTimeExceeded icmpTimeExceeded = new IcmpTimeExceeded(icmpPacket);

                // make sure this packet is for us
                if (m_ipId != icmpTimeExceeded.BadPacket.Identification)
                {
                    return;
                }

                // disable the timeout timer
                m_timer.Enabled = false;

                // raise the event
                if (RouteUpdate != null && m_working)
                {
                    RouteUpdate(ipPacket.SourceAddress, (int)((uint)m_stop - (uint)m_start), m_ttl);
                }

                // increment the time to live.
                m_ttl++;

                // if the max hop count is exceeded
                if (m_ttl > m_maxHops)
                {
                    // disable the timeout timer
                    m_timer.Enabled = false;

                    // allow new trace routes
                    m_working = false;

                    // raise the event
                    if (MaxHopsExceeded != null)
                    {
                        MaxHopsExceeded();
                    }

                    // signal the wait event
                    m_waitObject.Set();
                }
                else
                {
                    // send the next request
                    SendRequest();
                }

                #endregion
            }
        }