Пример #1
0
		/// <summary>
		///		Log a new ARP packet.
		/// </summary>
		/// <param name="packet">
		///		The packet to log.
		///	</param>
		public void LogPacket (ArpPacket packet)
		{
			lock (m_logger.XmlWriter)
			{
				// <ArpHeader>
				m_logger.XmlWriter.WriteStartElement ("ArpHeader");
					
					m_logger.XmlWriter.WriteElementString ("Type",				packet.Type.ToString());
					m_logger.XmlWriter.WriteElementString ("MediaType",			packet.MediaType.ToString());
					m_logger.XmlWriter.WriteElementString ("Protocol",			packet.Protocol.ToString());
					m_logger.XmlWriter.WriteElementString ("SourceMAC",			packet.SourceMACAddress.ToString('-'));
					m_logger.XmlWriter.WriteElementString ("SourceIP",			packet.SourceIPAddress.ToString());
					m_logger.XmlWriter.WriteElementString ("TargetMAC",			packet.DestinationMACAddress.ToString('-'));
					m_logger.XmlWriter.WriteElementString ("TargetIP",			packet.DestinationIPAddress.ToString());
					
				m_logger.XmlWriter.WriteEndElement ();
				// </ArpHeader>

			}
		}
Пример #2
0
        /// <summary>
        ///		Resolve a physical address to an IP address.
        /// </summary>
        /// <param name="address">
        ///		The physical address to resolve.
        ///	</param>
        /// <returns>
        ///		Returns the IP address belonging to the physical address.
        ///	</returns>
        /// <exception cref="ObjectDisposedException">
        ///		If the object has already been disposed then an ObjectDisposedException
        ///		will be thrown
        ///	</exception>
        /// <exception cref="Exception">
        ///		If the driver failed to start or was not bound, an exception will be thrown.
        ///	</exception>
        public IPAddress ResolveMACAddress(MACAddress address)
        {
            if (m_disposed)
            {
                throw new ObjectDisposedException (this.ToString());
            }

            if (!m_driver.DriverStarted)
            {
                throw new Exception ("The driver has not been started.");
            }

            if (!m_driver.DriverBound)
            {
                throw new Exception ("The driver has not yet been bound to a device.");
            }

            Ethernet802_3 ethernet = new Ethernet802_3 ();

            // construct the ethernet header
            ethernet.SourceMACAddress		= m_driver.BoundAdapter.MediaAccessControlAddress;
            ethernet.DestinationMACAddress	= MACAddress.BroadcastAddress;
            ethernet.NetworkProtocol		= NetworkLayerProtocol.ARP;

            ArpPacket arp = new ArpPacket ();

            // construct the ARP header
            arp.Type					= ArpOpcode.ReverseRequest;
            arp.Protocol				= NetworkLayerProtocol.IP;
            arp.MediaType				= MediaType.Ethernet;
            arp.SourceMACAddress		= ethernet.SourceMACAddress;
            arp.SourceIPAddress			= m_driver.BoundAdapter.Interfaces[0].Address;
            arp.DestinationMACAddress	= address;

            // serialize and send the packet
            ethernet.Data = arp.Serialize ();
            m_driver.SendPacket (ethernet.Serialize ());

            m_querying = true;

            // wait for the reply
            while (m_querying)
            {
                byte[] packet = m_driver.RecievePacket ();

                Ethernet802_3 ethReply = new Ethernet802_3 (packet);

                // if this is an ARP packet
                if (ethReply.NetworkProtocol == NetworkLayerProtocol.ARP)
                {
                    ArpPacket arpReply = new ArpPacket (ethReply.Data);

                    // if this is an ARP reply
                    if (arpReply.Type == ArpOpcode.Reply)
                    {
                        // if the address matches the one we requested
                        if (arpReply.DestinationIPAddress.Equals (address))
                        {
                            // return the IP address
                            return arpReply.DestinationIPAddress;
                        }
                    }
                }
            }

            return IPAddress.Any;
        }
Пример #3
0
        /// <summary>
        ///		Resolve an IP address to a physical address.
        /// </summary>
        /// <param name="address">
        ///		The IP address to resolve.
        ///	</param>
        /// <returns>
        ///		Returns the physical address belonging to the IP address.
        ///	</returns>
        /// <exception cref="ObjectDisposedException">
        ///		If the object has already been disposed then an ObjectDisposedException
        ///		will be thrown
        ///	</exception>
        /// <exception cref="Exception">
        ///		If the driver failed to start or was not bound, an exception will be thrown.
        ///	</exception>
        public MACAddress ResolveIPAddress(IPAddress address)
        {
            if (m_disposed)
            {
                throw new ObjectDisposedException(this.ToString());
            }

            if (!m_driver.DriverStarted)
            {
                throw new Exception("The driver has not been started.");
            }

            if (!m_driver.DriverBound)
            {
                throw new Exception("The driver has not yet been bound to a device.");
            }

            Ethernet802_3 ethernet = new Ethernet802_3();

            // construct the ethernet header
            ethernet.SourceMACAddress      = m_driver.BoundAdapter.MediaAccessControlAddress;
            ethernet.DestinationMACAddress = MACAddress.BroadcastAddress;
            ethernet.NetworkProtocol       = NetworkLayerProtocol.ARP;

            ArpPacket arp = new ArpPacket();

            // construct the ARP header
            arp.Type                 = ArpOpcode.Request;
            arp.Protocol             = NetworkLayerProtocol.IP;
            arp.MediaType            = MediaType.Ethernet;
            arp.SourceMACAddress     = ethernet.SourceMACAddress;
            arp.SourceIPAddress      = m_driver.BoundAdapter.Interfaces[0].Address;
            arp.DestinationIPAddress = address;

            // serialize and send the packet
            ethernet.Data = arp.Serialize();
            m_driver.SendPacket(ethernet.Serialize());

            m_querying = true;

            // wait for the reply
            while (m_querying)
            {
                byte[] packet = m_driver.RecievePacket();

                Ethernet802_3 ethReply = new Ethernet802_3(packet);

                // if this is an ARP packet
                if (ethReply.NetworkProtocol == NetworkLayerProtocol.ARP)
                {
                    ArpPacket arpReply = new ArpPacket(ethReply.Data);

                    // if this is an ARP reply
                    if (arpReply.Type == ArpOpcode.Reply)
                    {
                        // if the address matches the one we requested
                        if (arpReply.SourceIPAddress.Equals(address))
                        {
                            // return the MAC address
                            return(arpReply.SourceMACAddress);
                        }
                    }
                }
            }

            return(MACAddress.BroadcastAddress);
        }