Subversion Repositories general

Rev

Rev 1194 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

using System;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;

namespace TCPproxy
{
        abstract class HostUtils
        {
                public static IPAddress ResendHostToIp(string host)
                {
                        IPAddress ip;

                        if(host == "")
                        {
                                throw new Exception("Please enter the resend host");
                        }

                        Regex ipRegex = new Regex(@"^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$");
                        if(ipRegex.Match(host).Success)
                        {
                                try
                                {
                                        ip = IPAddress.Parse(host);
                                }
                                catch(FormatException)
                                {
                                        throw new Exception("Wrong IP address of the resend host");
                                }
                        }
                        else
                        {
                                IPHostEntry hostInfo = Dns.GetHostByName(host);

                                if(hostInfo.AddressList.Length > 0)
                                {
                                        ip = hostInfo.AddressList[0];
                                }
                                else
                                {
                                        throw new Exception("Cannot find IP address of the resend host");
                                }
                        }

                        return ip;
                }
        }
}