/* This ASP.NET sample demonstrates the use of the DNS control in the ASP.NET environment.
* The text entered is checked as to whether it is a url, an email address, or a dot address
* and resolved appropriately.
*/
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace Dns
{
///
/// Summary description for WebForm1.
///
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label lblResolve;
protected System.Web.UI.WebControls.TextBox txtResolve;
protected System.Web.UI.WebControls.Button cmdResolve;
protected System.Web.UI.WebControls.Label lblDnsServers;
protected System.Web.UI.WebControls.TextBox txtDns1;
protected System.Web.UI.WebControls.TextBox txtDns2;
protected System.Web.UI.WebControls.TextBox txtDns3;
protected System.Web.UI.WebControls.Label lblResults;
protected System.Web.UI.WebControls.TextBox txtResults;
// dns control
protected DartUtilDotnet.Dns Dns1 = new DartUtilDotnet.Dns();
// array to hold list of dns servers
protected System.Collections.ArrayList ArrListDnsServers = new System.Collections.ArrayList();
public WebForm1()
{
Page.Init += new System.EventHandler(Page_Init);
}
private void Page_Load(object sender, System.EventArgs e)
{
// init dns server array
if(txtDns1.Text != "")
ArrListDnsServers.Add(txtDns1.Text);
if(txtDns2.Text != "")
ArrListDnsServers.Add(txtDns2.Text);
if(txtDns3.Text != "")
ArrListDnsServers.Add(txtDns3.Text);
}
private void Page_Init(object sender, EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
}
#region Web Form Designer generated code
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
this.cmdResolve.Click += new System.EventHandler(this.cmdResolve_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void cmdResolve_Click(object sender, System.EventArgs e)
{
string url = "";
url = txtResolve.Text;
// check for null servers
if(ArrListDnsServers.Count == 0)
txtResults.Text = "Unable to resolve. No DNS Server specified.";
// check for null url
else if(url == "")
txtResults.Text = "Unable to resolve. No address was specified.";
else
{
// all fields appear to have data... let's process
Dns1.Timeout = 5000;
Dns1.Names.Delimiter = ", ";
Dns1.Addresses.Delimiter = ", ";
// this array is used in a clumsy attempt to determine
// if a url is in dot notation
char[] temp = new Char[15];
// read url into chararray
temp = url.ToCharArray(0, url.Length);
// check to see if first and last char is numeric, if so resolve as dot notation
if((temp[0] >= 48 && temp[0] <= 57) && (temp[temp.Length-1] >= 48 && temp[temp.Length-1] <= 57))
ResolveDotAddress(url);
// check to see if it contains an @ symbol, if so resolve as email
else if(url.IndexOf("@", 0, url.Length)!= -1)
ResolveEmailAddress(url);
// otherwise resolve as url
else
ResolveNameAddress(url);
}
}
private void ResolveEmailAddress(string url)
{
bool done = false;
int i=0;
// iterate through server list and attempt to resolve, if successful- quit, if not go on to next, if at end, display error
while(!done && i < ArrListDnsServers.Count)
{
Dns1.ServerName = ArrListDnsServers[i].ToString();
try
{
Dns1.GetMailServer(url);
if(Dns1.Names.Count != 0 || Dns1.Addresses.Count != 0)
{
txtResults.Text = "Email address " + url + " resolved.\n";
txtResults.Text = txtResults.Text + "DNS Used: " + Dns1.ServerName + "\n";
txtResults.Text = txtResults.Text + "Mail Server: " + Dns1.Names.All + "\n";
done = true;
}
else
{
if(i == ArrListDnsServers.Count - 1)
txtResults.Text = "Error, cannot resolve";
}
}
catch(Exception ResolveError)
{
if(i== ArrListDnsServers.Count - 1)
txtResults.Text = "Error: " + ResolveError.Message;
}
finally
{
i++;
}
}
}
private void ResolveDotAddress(string url)
{
bool done = false;
int i=0;
// iterate through server list and attempt to resolve, if successful- quit, if not go on to next, if at end, display error
while(!done && i < ArrListDnsServers.Count)
{
Dns1.ServerName = ArrListDnsServers[i].ToString();
try
{
Dns1.GetName(url);
if(Dns1.Names.Count != 0 || Dns1.Addresses.Count != 0)
{
txtResults.Text = "Dot address " + url + " resolved.\n";
txtResults.Text = txtResults.Text + "DNS Used: " + Dns1.ServerName + "\n";
txtResults.Text = txtResults.Text + "Name(s): " + Dns1.Names.All + "\n";
done = true;
}
else
{
if(i == ArrListDnsServers.Count - 1)
txtResults.Text = "Error, cannot resolve";
}
}
catch(Exception ResolveError)
{
if(i== ArrListDnsServers.Count - 1)
txtResults.Text = "Error: " + ResolveError.Message;
}
finally
{
i++;
}
}
}
private void ResolveNameAddress(string url)
{
bool done = false;
int i=0;
// iterate through server list and attempt to resolve, if successful- quit, if not go on to next, if at end, display error
while(!done && i < ArrListDnsServers.Count)
{
Dns1.ServerName = ArrListDnsServers[i].ToString();
try
{
Dns1.GetAddress(url);
if(Dns1.Names.Count != 0 || Dns1.Addresses.Count != 0)
{
txtResults.Text = "Address " + url + " resolved.\n";
txtResults.Text = txtResults.Text + "DNS Used: " + Dns1.ServerName + "\n";
txtResults.Text = txtResults.Text + "Address(es): " + Dns1.Addresses.All + "\n";
done = true;
}
else
{
if(i == ArrListDnsServers.Count - 1)
txtResults.Text = "Error, cannot resolve";
}
}
catch(Exception ResolveError)
{
if(i== ArrListDnsServers.Count - 1)
txtResults.Text = "Error: " + ResolveError.Message;
}
finally
{
i++;
}
}
}
}
}