[Odsdotnet-commit] SF.net SVN: odsdotnet: [16] trunk/Ods/Ods.cs
Status: Alpha
Brought to you by:
geniusj
|
From: <ge...@us...> - 2006-08-05 02:51:44
|
Revision: 16 Author: geniusj Date: 2006-08-04 19:51:39 -0700 (Fri, 04 Aug 2006) ViewCVS: http://svn.sourceforge.net/odsdotnet/?rev=16&view=rev Log Message: ----------- Rudimentary hosts/domains listing support. Uses a TreeView. Not bad, but needs to be faster. That'll come next. Modified Paths: -------------- trunk/Ods/Ods.cs Modified: trunk/Ods/Ods.cs =================================================================== --- trunk/Ods/Ods.cs 2006-08-03 06:02:28 UTC (rev 15) +++ trunk/Ods/Ods.cs 2006-08-05 02:51:39 UTC (rev 16) @@ -77,7 +77,6 @@ public OdsRR() { } - /// <summary> /// Create an ODS RR /// </summary> @@ -89,7 +88,6 @@ string ttl) { } - private string hostValue; /// <summary> /// ODS Hostname @@ -99,7 +97,6 @@ get { return hostValue; } set { hostValue = value; } } - private string rrtypeValue; /// <summary> /// RR Type @@ -109,7 +106,6 @@ get { return rrtypeValue; } set { rrtypeValue = value; } } - private string targetValue; /// <summary> /// Target (IP Address for A records, Host for CNAMEs, etc) @@ -119,7 +115,6 @@ get { return targetValue; } set { targetValue = value; } } - private string ttlValue; /// <summary> /// Time to Live for record @@ -129,7 +124,6 @@ get { return ttlValue; } set { ttlValue = value; } } - /// <summary> /// String representation /// </summary> @@ -143,7 +137,6 @@ return toReturn; } - /// <summary> /// Returns ToString() /// </summary> @@ -154,12 +147,64 @@ } /// <summary> + /// A Domain containing OdsRR(s) + /// </summary> + public class OdsDomain + { + private string domainNameValue; + private ArrayList hostsValue; + + /// <summary> + /// Initialize an OdsDomain + /// </summary> + public OdsDomain() + { + } + /// <summary> + /// Initialize an OdsDomain + /// </summary> + /// <param name="domainName">Domain name</param> + public OdsDomain(string domainName) + { + this.domainNameValue = domainName; + } + /// <summary> + /// Initialize an OdsDomain + /// </summary> + /// <param name="domainName">Domain name</param> + /// <param name="hosts">ArrayList of OdsRR objects</param> + public OdsDomain(string domainName, ArrayList hosts) + { + this.domainNameValue = domainName; + this.hostsValue = hosts; + } + /// <summary> + /// Get/Set Domain Name + /// </summary> + public string DomainName + { + get { return domainNameValue; } + set { domainNameValue = value; } + } + /// <summary> + /// Get/Set Hosts List (ArrayList of OdsRRs) + /// </summary> + public ArrayList Hosts + { + get { return hostsValue; } + set { hostsValue = value; } + } + } + + /// <summary> /// Class for handling ODS connections and communicating with ODS. /// </summary> public class OdsConnection { private TcpClient client; private NetworkStream stream; + private StreamReader sr; + private StreamWriter sw; private String username; private String password; private String server; @@ -337,24 +382,93 @@ /// OdsConnection odsconnect = new OdsConnection(user, pass); /// ArrayList hosts = odsconnect.GetRRList(); /// - /// foreach (Hashtable host in hosts) + /// foreach (OdsRR host in hosts) /// { /// Console.Write("Host: {0} Type: {1} Target: {2} TTL: {3}\r\n", - /// host["host"], host["type"], host["target"], host["ttl"]); + /// host.Host, host.RRType, host.Target, host.TTL); /// } /// </code> /// </example> public ArrayList GetRRList() { + // Return all RRs + return (GetFilteredRRList("")); + } + public ArrayList GetRRListByDomain() + { + ArrayList domainList = new ArrayList(); + String cmdStr = "DOMAINS\r\n"; + String domainStr; + + // Make sure we're connected and open a StreamReader + ReConnect(); + + // Send our command to the ODS server + Send(cmdStr); + + // Retrieve our result + while (!(domainStr = sr.ReadLine()).StartsWith("189")) + { + // Check to see if domain list is empty or we're not logged in + if (domainStr.StartsWith("300")) + break; + + // We've found the meat of the output - Parse it out.. + if (domainStr.StartsWith("181") || domainStr.StartsWith("183")) + { + OdsDomain domainObj = new OdsDomain(domainStr.Substring(4)); + + domainList.Add(domainObj); + } + } + + // Get a list of hosts for all of our domains. + foreach (OdsDomain domainObj in domainList) + { + domainObj.Hosts = GetFilteredRRList(domainObj.DomainName); + } + + return (domainList); + } + // TODO: Add overloaded GetRRList() for limiting to a specific domain/pattern + /// <summary> + /// Get current internet facing IP (uses ODS server port 7069) + /// </summary> + /// <returns>String containing current internet facing IP</returns> + public String GetCurrentIP() + { + String ipaddr; + TcpClient ipClient = new TcpClient(this.server, 7069); + NetworkStream ipStream = ipClient.GetStream(); + StreamReader ipsr = new StreamReader(ipStream); + + ipaddr = ipsr.ReadLine(); + ipaddr.Trim(); + + ipStream.Close(); + ipClient.Close(); + + return (ipaddr); + } + /// <summary> + /// Get RR list using ODS filter syntax + /// </summary> + /// <param name="filter">Filter to use: e.g. domain.com, or s:domain, or an empty string</param> + /// <returns></returns> + private ArrayList GetFilteredRRList(string filter) + { ArrayList rrList = new ArrayList(); Regex rrSeparator = new Regex(" +"); - String cmdStr = "LISTRR\r\n"; - String rrStr; + String rrStr, cmdStr; + if (filter.Length == 0) + cmdStr = "LISTRR\r\n"; + else + cmdStr = String.Format("LISTRR {0}\r\n", filter); + // Make sure we're connected and open a StreamReader ReConnect(); - StreamReader sr = new StreamReader(stream); - + // Send our command to the ODS server Send(cmdStr); @@ -396,27 +510,7 @@ return (rrList); } - // TODO: Add overloaded GetRRList() for limiting to a specific domain/pattern /// <summary> - /// Get current internet facing IP (uses ODS server port 7069) - /// </summary> - /// <returns>String containing current internet facing IP</returns> - public String GetCurrentIP() - { - String ipaddr; - TcpClient ipClient = new TcpClient(this.server, 7069); - NetworkStream ipStream = ipClient.GetStream(); - StreamReader sr = new StreamReader(ipStream); - - ipaddr = sr.ReadLine(); - ipaddr.Trim(); - - ipStream.Close(); - ipClient.Close(); - - return (ipaddr); - } - /// <summary> /// Disconnect from ODS. It's a good idea to call this whenever you're done for a while. /// </summary> public void Disconnect() @@ -424,32 +518,25 @@ this.stream.Close(); this.client.Close(); } - private void Send(String command) { - byte[] writeBuf = Encoding.ASCII.GetBytes(command); - // If we've been disconnected, reconnect ReConnect(); - stream.Write(writeBuf, 0, writeBuf.Length); + sw.Write(command); return; } private void SendAndVerify(String command, UInt16 returncode) { String resultstr; - Int32 readlen; - byte[] result = new Byte[256]; - byte[] writeBuf = Encoding.ASCII.GetBytes(command); // If we've been disconnected, reconnect ReConnect(); - stream.Write(writeBuf, 0, writeBuf.Length); - readlen = stream.Read(result, 0, result.Length); + sw.Write(command); - resultstr = Encoding.ASCII.GetString(result, 0, readlen); + resultstr = sr.ReadLine(); // Verify that command was successful if (!CheckStringForReturnCode(resultstr, returncode)) @@ -460,7 +547,6 @@ private void Connect() { // Buffer for our read data - Byte[] buf = new Byte[256]; String bufstr; try @@ -469,11 +555,12 @@ // Initialize stream for socket this.stream = this.client.GetStream(); + this.sr = new StreamReader(this.stream); + this.sw = new StreamWriter(this.stream); + this.sw.AutoFlush = true; - Int32 bytes = this.stream.Read(buf, 0, buf.Length); + bufstr = this.sr.ReadLine(); - bufstr = Encoding.ASCII.GetString(buf, 0, bytes); - if (!CheckStringForReturnCode(bufstr, 100)) { throw new OdsServerException("No connection banner!? Wrong protocol??"); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |