Example #1
0
        internal static PhysicalAddress Resolve(
            ILiveDevice device,
            System.Net.IPAddress destIP,
            System.Net.IPAddress localIP,
            PhysicalAddress localMAC,
            TimeSpan timeout)
        {
            //Build a new ARP request packet
            var 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();

            //set the filter
            device.Filter = arpFilter;

            // set a last request time that will trigger sending the
            // arp request immediately
            var lastRequestTime = DateTime.FromBinary(0);

            var requestInterval = new TimeSpan(0, 0, 1);

            PacketDotNet.ArpPacket arpPacket = null;

            // attempt to resolve the address with the current timeout
            var timeoutDateTime = DateTime.Now + timeout;

            while (DateTime.Now < timeoutDateTime)
            {
                if (requestInterval < (DateTime.Now - lastRequestTime))
                {
                    // inject the packet to the wire
                    device.SendPacket(request);
                    lastRequestTime = DateTime.Now;
                }

                //read the next packet from the network
                var retval = device.GetNextPacket(out PacketCapture e);
                if (retval != GetPacketStatus.PacketRead)
                {
                    continue;
                }
                var reply = e.GetPacket();

                // parse the packet
                var packet = PacketDotNet.Packet.ParsePacket(reply.LinkLayerType, reply.Data);

                // is this an arp packet?
                arpPacket = packet.Extract <PacketDotNet.ArpPacket>();
                if (arpPacket == null)
                {
                    continue;
                }

                //if this is the reply we're looking for, stop
                if (arpPacket.SenderProtocolAddress.Equals(destIP))
                {
                    break;
                }
            }

            // the timeout happened
            if (DateTime.Now >= timeoutDateTime)
            {
                return(null);
            }
            else
            {
                //return the resolved MAC address
                return(arpPacket.SenderHardwareAddress);
            }
        }
 private static void AllowPacket(ILiveDevice device, WinpkFilterHeader header, Packet packet)
 {
     device.SendPacket(packet.Bytes, header);
 }