Subversion Repositories general

Rev

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

Rev Author Line No. Line
1194 dev 1
using System;
2
using System.Net;
3
using System.Text;
4
using System.Text.RegularExpressions;
5
 
6
namespace TCPproxy
7
{
8
	abstract class HostUtils
9
	{
10
		public static IPAddress ResendHostToIp(string host)
11
		{
12
			IPAddress ip;
13
 
14
			if(host == "")
15
			{
16
				throw new Exception("Please enter the resend host");
17
			}
18
 
19
			Regex ipRegex = new Regex(@"^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$");
20
			if(ipRegex.Match(host).Success)
21
			{
22
				try
23
				{
24
					ip = IPAddress.Parse(host);
25
				}
26
				catch(FormatException)
27
				{
28
					throw new Exception("Wrong IP address of the resend host");
29
				}
30
			}
31
			else
32
			{
1197 dev 33
				IPHostEntry hostInfo = Dns.GetHostByName(host);
1194 dev 34
 
35
				if(hostInfo.AddressList.Length > 0)
36
				{
37
					ip = hostInfo.AddressList[0];
38
				}
39
				else
40
				{
41
					throw new Exception("Cannot find IP address of the resend host");
42
				}
43
			}
44
 
45
			return ip;
46
		}
47
	}
48
}