Пример #1
0
        /// <summary>
        /// ARP replies are handled here.
        /// </summary>
        /// <param name="e">ARP Reply event args</param>
        private static void ARP_OnARPReply(ARPReplyArgs e)
        {
            // get hostname of IP
            Console.WriteLine("ARP reply received from " + e.IPV4.ToString());
            Console.WriteLine("Attempting to look up hostname.");
            String hostname;
            try {
                IPHostEntry iphe = Dns.GetHostByAddress(e.IPV4);
                hostname = iphe.HostName;
            }
            catch (SocketException) {
                Console.WriteLine("Hostname lookup failed.");
                hostname = "UNKNOWN";
            }

            NetworkDevice nd = new NetworkDevice(e.IPV4, e.MAC, hostname);
            HostFoundArgs ha = new HostFoundArgs(nd);
            OnHostFound(ha);
        }
Пример #2
0
        /// <summary>
        /// Send an ARP broadcast to a specified destination.
        /// The OnARPReply Event will be raised when a message is received.
        /// </summary>
        /// <param name="ipv4">A valid IPv4 address.</param>
        public static void SendARPMessageIPv4(object ipv4)
        {
            IPAddress dst = IPAddress.Parse(ipv4.ToString());
            byte[] macAddr = new byte[6];
            uint macAddrLen = (uint)macAddr.Length;

            //Console.WriteLine("ARP message sent to " + dst);
            if (SendARP((int)dst.Address, 0, macAddr, ref macAddrLen) == 0) {
                // arp reply received
                string[] str = new string[(int)macAddrLen];
                for (int i = 0; i < macAddrLen; i++)
                    str[i] = macAddr[i].ToString("x2");

                String mac = string.Join("-", str);

                ARPReplyArgs arpArg = new ARPReplyArgs(mac, dst);
                OnARPReply(arpArg);
            }
        }