Example #1
0
        /// <summary>
        /// Retrieves the IPInfo for the machine on the local network with the specified MAC Address.
        /// </summary>
        /// <param name="macAddress">The MAC Address of the IPInfo to retrieve.</param>
        /// <returns></returns>
        public static ARP GetIPInfo(string macAddress)
        {
            ARP ipinfo = (from ip in GetIPInfo()
                          where ip.MacAddress.ToLowerInvariant() == macAddress.ToLowerInvariant()
                          select ip).FirstOrDefault();

            return(ipinfo);
        }
        public static DiscoveryResult DiscoverDevice(int port)
        {
            IEnumerable <string> ips = getInterfaceAddresses();
            var ret = new DiscoveryResult();

            _localEndPoint = new IPEndPoint(IPAddress.Any, 0);
            _client        = new UdpClient(port);

            var p = new ISCPPacket("!xECNQSTN");

            byte[] sendbuf = p.GetBytes();
            foreach (string networkaddress in ips)
            {
                _client.Send(sendbuf, sendbuf.Length, IPAddress.Parse(networkaddress).ToString(), port);
                _client.Send(sendbuf, sendbuf.Length, IPAddress.Parse(networkaddress).ToString(), port);
                _client.Send(sendbuf, sendbuf.Length, IPAddress.Parse(networkaddress).ToString(), port);
            }
            while (_client.Available > 0)
            {
                byte[] recv = _client.Receive(ref _localEndPoint);
                Thread.Sleep(100);
                var sb = new StringBuilder();
                foreach (byte t in recv)
                {
                    sb.Append(char.ConvertFromUtf32(Convert.ToInt32(string.Format("{0:x2}", t), 16)));
                }
                string stringData = sb.ToString();
                if (stringData.Contains("!1ECN"))
                {
                    int      idx   = stringData.IndexOf("!1ECN") + 5;
                    string[] parts = stringData.Substring(idx).Split('/');
                    string   mac   = parts[3].Substring(0, 12);
                    string   ip    = ARP.GetIPInfo(mac).IPAddress;
                    ret.IP     = ip;
                    ret.Port   = Convert.ToInt32(parts[1]);
                    ret.Region = stringData.Substring(idx + 14, 2);
                    ret.MAC    = mac;
                    ret.Model  = stringData.Substring(idx, 7);
                }
            }
            _client.Close();
            return(ret);
        }