Friday, May 25, 2012

Public IP Address Finder in C#

This is a task i have been working on to find out the public ip of a computer running behind firewall or NAT.
It uses the trace route method using ping class and then filter out local IP address using regex and find the first available public IP and try to send it as an email, email part is not shown.

 public class IPFinder
    {
        internal void MyPublicIPAndSend()
        {
            Console.WriteLine("Wait finding Public IP...");
            var firstPublicIP = Traceroute("www.google.com").Where(r => !isIPLocal(r)).FirstOrDefault();
            if (firstPublicIP != null)
                SendEmail("My Public IP", firstPublicIP.ToString());

        }


        private bool isIPLocal(IPAddress ipaddress)
        {
            // find if ip is localhost
            // find if ip starts with 10.
            //find if ip falls in 172.1 to 172.31
            //find if ip is 192.168..
            var regEx = new Regex(@"(^127\.0\.0\.1)|(^10\.)|(^172\.1[6-9]\.)|(^172\.2[0-9]\.)|(^172\.3[0-1]\.)|(^192\.168\.)");
            return regEx.Match(ipaddress.ToString()).Success;
        }

        private IPAddress[] Traceroute(string ipAddressOrHostName)
        {
            List result = new List();
            //resolve name to ip
            IPAddress ipAddress = Dns.GetHostEntry(ipAddressOrHostName).AddressList[0];
            using (Ping pingSender = new Ping())
            {
                PingOptions pingOptions = new PingOptions();
                byte[] bytes = new byte[2];
                pingOptions.DontFragment = true;
                pingOptions.Ttl = 1;
                //30 hops just to safe guard infinite loop
                int maxHops = 30;
                for (int i = 1; i < maxHops + 1; i++)
                {
                    //send icmp echo to ipAddress with timout 5000 ms
                    PingReply pingReply = pingSender.Send(
                        ipAddress,
                        5000,
                        bytes, pingOptions);
                    result.Add(pingReply.Address == null ? string.Empty : pingReply.Address.ToString());
                    // keep looping until pingreply is success, means reached destination
                    if (pingReply.Status == IPStatus.Success)
                    {
                        break;
                    }
                    pingOptions.Ttl++;
                }
            }
            IPAddress temp = IPAddress.Parse("127.0.0.1");
            return result.Where(r => IPAddress.TryParse(r, out temp)).Select(r => IPAddress.Parse(r)).ToArray();
        }

        private bool SendEmail(string subject, string body)
        {
            Console.WriteLine(subject);
            Console.WriteLine(body);
            return true;

        }




    }

2 comments:

Unknown said...
This comment has been removed by the author.
Unknown said...

Nice information. I had checked my External ip address using this site IP-Details.com