Subversion Repositories general

Compare Revisions

Ignore whitespace Rev 1193 → Rev 1194

/TCPproxy/trunk/HostUtils.cs
0,0 → 1,49
using System;
using System.Collections.Generic;
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.GetHostEntry(host);
 
if(hostInfo.AddressList.Length > 0)
{
ip = hostInfo.AddressList[0];
}
else
{
throw new Exception("Cannot find IP address of the resend host");
}
}
 
return ip;
}
}
}