Esempio n. 1
0
		/// <summary>
		///		Log a new ethernet packet.
		/// </summary>
		/// <param name="packet">
		///		The packet to log.
		///	</param>
		public void LogPacket (Ethernet802_3 packet)
		{
			lock (m_logger.XmlWriter)
			{
				// <EthernetHeader>
				m_logger.XmlWriter.WriteStartElement ("EthernetHeader");
					
					m_logger.XmlWriter.WriteElementString ("SourceMAC",			packet.SourceMACAddress.ToString('-'));
					m_logger.XmlWriter.WriteElementString ("DestinationMAC",	packet.DestinationMACAddress.ToString('-'));
					m_logger.XmlWriter.WriteElementString ("NetworkProtocol",	packet.NetworkProtocol.ToString());
					
				m_logger.XmlWriter.WriteEndElement ();
				// </EthernetHeader>

			}
		}
Esempio n. 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;
        }