Пример #1
0
        /// <summary>
        /// Resolves the MAC address of the specified IP address
        /// </summary>
        /// <param name="destIP">The IP address to resolve</param>
        /// <param name="deviceName">The local network device name on which to send the ARP request</param>
        /// <returns>The MAC address that matches to the given IP address</returns>
        public PhysicalAddress Resolve(System.Net.IPAddress destIP, string deviceName)
        {
            PhysicalAddress localMAC = LocalMAC;

            System.Net.IPAddress localIP = LocalIP;
            //NetworkDevice device = new NetworkDevice(DeviceName);
            PcapDevice device = Pcap.GetPcapDevice(DeviceName);

            //FIXME: PcapDevices don't have IpAddress
            //       These were present under Windows specific network adapters
            //       and may be present in pcap in the future with pcap-ng
            // if no local ip address is specified use the one from the
            // local device
#if false
            if (localIP == null)
            {
                localIP = device.IpAddress;
            }
#endif

            // if no local mac address is specified use the one from the device
            if (LocalMAC == null)
            {
                localMAC = device.Interface.MacAddress;
            }

            //Build a new ARP request packet
            ARPPacket request = BuildRequest(destIP, localMAC, localIP);

            //create a "tcpdump" filter for allowing only arp replies to be read
            String arpFilter = "arp and ether dst " + localMAC.ToString();

            //open the device with 20ms timeout
            device.Open(true, 20);
            //set the filter
            device.SetFilter(arpFilter);
            //inject the packet to the wire
            device.SendPacket(request);

            ARPPacket reply;

            while (true)
            {
                //read the next packet from the network
                reply = (ARPPacket)device.GetNextPacket();
                if (reply == null)
                {
                    continue;
                }

                //if this is the reply we're looking for, stop
                if (reply.ARPSenderProtoAddress.Equals(destIP))
                {
                    break;
                }
            }
            //free the device
            device.Close();
            //return the resolved MAC address
            return(reply.ARPSenderHwAddress);
        }
Пример #2
0
        /// <summary>
        /// Resolves the MAC address of the specified IP address
        /// </summary>
        /// <param name="destIP">The IP address to resolve</param>
        /// <param name="deviceName">The local network device name on which to send the ARP request</param>
        /// <returns>The MAC address that matches to the given IP address</returns>
        public PhysicalAddress Resolve(System.Net.IPAddress destIP, string deviceName)
        {
            PhysicalAddress localMAC = LocalMAC;

            System.Net.IPAddress localIP = LocalIP;
            //NetworkDevice device = new NetworkDevice(DeviceName);
            PcapDevice device = Pcap.GetPcapDevice(DeviceName);

            //FIXME: PcapDevices don't have IpAddress
            //       These were present under Windows specific network adapters
            //       and may be present in pcap in the future with pcap-ng
            // if no local ip address is specified use the one from the
            // local device
#if false
            if (localIP == null)
            {
                localIP = device.IpAddress;
            }
#endif

            // if no local mac address is specified use the one from the device
            if (LocalMAC == null)
            {
                localMAC = device.Interface.MacAddress;
            }

            //Build a new ARP request packet
            ARPPacket request = BuildRequest(destIP, localMAC, localIP);

            //create a "tcpdump" filter for allowing only arp replies to be read
            //String arpFilter = "arp and ether dst 00:1E:33:A6:FA:52";
            String arpFilter = "arp and ether dst ";
            byte[] mac       = localMAC.GetAddressBytes();
            for (int i = 0; i < mac.Length; i++)
            {
                // Display the physical address in hexadecimal.
                arpFilter += mac[i].ToString("X2");
                // Insert a hyphen after each byte, unless we are at the end of the
                // address.
                if (i != mac.Length - 1)
                {
                    arpFilter += ":";
                }
            }
            // arpFilter += mac;


            //open the device with 20ms timeout
            device.Open(true, 20);
            //set the filter
            device.SetFilter(arpFilter);
            //inject the packet to the wire
            device.SendPacket(request);

            ARPPacket reply;

            while (true)
            {
                //read the next packet from the network
                reply = (ARPPacket)device.GetNextPacket();
                if (reply == null)
                {
                    continue;
                }

                //if this is the reply we're looking for, stop
                if (reply.ARPSenderProtoAddress.Equals(destIP))
                {
                    break;
                }
            }
            //free the device
            device.Close();
            //return the resolved MAC address
            return(reply.ARPSenderHwAddress);
        }