405 lines
16 KiB
C#
405 lines
16 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Drawing;
|
|
using System.Xml;
|
|
using System.Windows.Forms;
|
|
|
|
namespace EduNetworkBuilder
|
|
{
|
|
public class NetTest
|
|
{
|
|
public string sHost = "";
|
|
public string dHost = "";
|
|
public Color WrongColor = Color.Red;
|
|
public NetTestType TheTest = NetTestType.NeedsDefaultGW;
|
|
public bool TaskWasDone = false;
|
|
|
|
public NetTest(string srcHost, string dstHost, NetTestType tTest)
|
|
{
|
|
sHost = srcHost;
|
|
dHost = dstHost;
|
|
TheTest = tTest;
|
|
SetInitialDoneState();
|
|
}
|
|
|
|
public NetTest(NetTest FromWhat)
|
|
{
|
|
sHost = FromWhat.sHost;
|
|
dHost = FromWhat.dHost;
|
|
TheTest = FromWhat.TheTest;
|
|
SetInitialDoneState();
|
|
}
|
|
|
|
public NetTest(XmlNode theNode)
|
|
{
|
|
foreach (XmlNode Individual in theNode.ChildNodes)
|
|
{
|
|
XmlNodeType myNodetype = Individual.NodeType;
|
|
if (myNodetype == XmlNodeType.Element)
|
|
{
|
|
switch (Individual.Name.ToLower())
|
|
{
|
|
case "sourcehost":
|
|
case "shost":
|
|
sHost = Individual.InnerText;
|
|
break;
|
|
case "desthost":
|
|
case "dhost":
|
|
dHost = Individual.InnerText;
|
|
break;
|
|
case "thetest":
|
|
TheTest = NB.ParseEnum<NetTestType>(Individual.InnerText);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
SetInitialDoneState();
|
|
}
|
|
|
|
public void SetInitialDoneState()
|
|
{
|
|
switch (TheTest)
|
|
{
|
|
case NetTestType.LockAll:
|
|
case NetTestType.LockDHCP:
|
|
case NetTestType.LockGateway:
|
|
case NetTestType.LockIP:
|
|
case NetTestType.LockNic:
|
|
case NetTestType.LockRoute:
|
|
TaskWasDone = true;
|
|
break;
|
|
}
|
|
}
|
|
|
|
public void Save(XmlWriter writer)
|
|
{
|
|
writer.WriteStartElement("nettest");
|
|
writer.WriteElementString("shost", sHost);
|
|
writer.WriteElementString("dhost", dHost);
|
|
writer.WriteElementString("thetest", TheTest.ToString());
|
|
writer.WriteEndElement();
|
|
}
|
|
|
|
public void UpdateValuesFromAnother(NetTest WhatFrom)
|
|
{
|
|
dHost = WhatFrom.dHost;
|
|
sHost = WhatFrom.sHost;
|
|
TheTest = WhatFrom.TheTest;
|
|
WrongColor = WhatFrom.WrongColor;
|
|
}
|
|
|
|
public bool Equals(NetTest CompareTo)
|
|
{
|
|
if (sHost != CompareTo.sHost) return false;
|
|
if (dHost != CompareTo.dHost) return false;
|
|
if (TheTest != CompareTo.TheTest) return false;
|
|
if (WrongColor != CompareTo.WrongColor) return false;
|
|
return true;
|
|
}
|
|
|
|
public bool Edit()
|
|
{
|
|
NetTestEditor nte = new NetTestEditor(this);
|
|
NetTest copy = new NetTest(this);
|
|
nte.ShowDialog();
|
|
if (copy.Equals(this)) return false;
|
|
return true;
|
|
}
|
|
|
|
private string TestDescription(NetTestVerbosity amount)
|
|
{
|
|
string toreturn = "";
|
|
switch(amount)
|
|
{
|
|
case NetTestVerbosity.basic:
|
|
if(TheTest != NetTestType.ReadContextHelp)
|
|
toreturn = "Has a problem";
|
|
toreturn = NB.Translate("_ReadContext");
|
|
break;
|
|
case NetTestVerbosity.hints:
|
|
switch (TheTest)
|
|
{
|
|
case NetTestType.NeedsDefaultGW:
|
|
toreturn = "Needs the gateway set";
|
|
break;
|
|
// case NetTestType.NeedsPingToHost:
|
|
// toreturn = "Cannot ping";
|
|
// break;
|
|
case NetTestType.NeedsRouteToNet:
|
|
toreturn = "Needs a route set";
|
|
break;
|
|
case NetTestType.NeedsLocalIPTo:
|
|
toreturn = "Needs a local IP";
|
|
break;
|
|
case NetTestType.NeedsLinkToDevice:
|
|
toreturn = "Needs to be connected to the network.";
|
|
break;
|
|
case NetTestType.SuccessfullyArps:
|
|
toreturn = "Needs to find ARP from some device";
|
|
break;
|
|
case NetTestType.SuccessfullyDHCPs:
|
|
toreturn = "Needs a DHCP IP address";
|
|
break;
|
|
case NetTestType.SuccessfullyPings:
|
|
toreturn = "Must ping a host.";
|
|
break;
|
|
case NetTestType.HelpRequest:
|
|
toreturn = "Get mouse-over help";
|
|
break;
|
|
case NetTestType.FailedPing:
|
|
toreturn = "Should fail to ping a specific host";
|
|
break;
|
|
case NetTestType.LockAll:
|
|
toreturn = "Is Locked";
|
|
break;
|
|
case NetTestType.LockDHCP:
|
|
toreturn = "Has Locked DHCP";
|
|
break;
|
|
case NetTestType.LockIP:
|
|
toreturn = "Has Locked IP";
|
|
break;
|
|
case NetTestType.LockNic:
|
|
toreturn = "Has Locked NIC";
|
|
break;
|
|
case NetTestType.LockRoute:
|
|
toreturn = "Has Locked Route";
|
|
break;
|
|
case NetTestType.LockGateway:
|
|
toreturn = "Has Locked Gateway";
|
|
break;
|
|
case NetTestType.ReadContextHelp:
|
|
toreturn = NB.Translate("_ReadContext");
|
|
break;
|
|
|
|
}
|
|
break;
|
|
case NetTestVerbosity.full:
|
|
switch (TheTest)
|
|
{
|
|
case NetTestType.NeedsDefaultGW:
|
|
toreturn = "Needs the gateway set to:";
|
|
break;
|
|
// case NetTestType.NeedsPingToHost:
|
|
// toreturn = "Cannot ping host:";
|
|
// break;
|
|
case NetTestType.NeedsRouteToNet:
|
|
toreturn = "Needs a route to network:";
|
|
break;
|
|
case NetTestType.NeedsLocalIPTo:
|
|
toreturn = "Needs an IP local to host:";
|
|
break;
|
|
case NetTestType.NeedsLinkToDevice:
|
|
toreturn = "Needs a link to host:";
|
|
break;
|
|
case NetTestType.SuccessfullyArps:
|
|
toreturn = "Needs to find ARP from:";
|
|
break;
|
|
case NetTestType.SuccessfullyDHCPs:
|
|
toreturn = "Needs a DHCP IP address from server:";
|
|
break;
|
|
case NetTestType.SuccessfullyPings:
|
|
toreturn = "Must ping:";
|
|
break;
|
|
case NetTestType.HelpRequest:
|
|
toreturn = "Get mouse-over help of level:";
|
|
break;
|
|
case NetTestType.FailedPing:
|
|
toreturn = "Needs to try to ping (and fail):";
|
|
break;
|
|
case NetTestType.LockAll:
|
|
toreturn = "Is Locked:";
|
|
break;
|
|
case NetTestType.LockDHCP:
|
|
toreturn = "Has Locked DHCP:";
|
|
break;
|
|
case NetTestType.LockIP:
|
|
toreturn = "Has Locked IP:";
|
|
break;
|
|
case NetTestType.LockNic:
|
|
toreturn = "Has Locked NIC:";
|
|
break;
|
|
case NetTestType.LockRoute:
|
|
toreturn = "Has Locked Route:";
|
|
break;
|
|
case NetTestType.LockGateway:
|
|
toreturn = "Has Locked Gateway:";
|
|
break;
|
|
case NetTestType.ReadContextHelp:
|
|
toreturn = NB.Translate("_ReadContext");
|
|
break;
|
|
}
|
|
break;
|
|
case NetTestVerbosity.none:
|
|
toreturn = "";
|
|
break;
|
|
}
|
|
return toreturn;
|
|
}
|
|
|
|
public string GetDescription(NetTestVerbosity amount)
|
|
{
|
|
string toreturn = "";
|
|
if(TheTest == NetTestType.ReadContextHelp)
|
|
{
|
|
return TestDescription(amount) + " " + sHost;
|
|
}
|
|
switch(amount)
|
|
{
|
|
case NetTestVerbosity.basic:
|
|
toreturn = sHost + " " + TestDescription(amount);
|
|
break;
|
|
case NetTestVerbosity.hints:
|
|
toreturn = sHost + " " + TestDescription(amount);
|
|
break;
|
|
case NetTestVerbosity.full:
|
|
toreturn = sHost + " " + TestDescription(amount) + " " + dHost;
|
|
break;
|
|
case NetTestVerbosity.none:
|
|
toreturn = "";
|
|
break;
|
|
}
|
|
return toreturn;
|
|
}
|
|
|
|
|
|
public bool TestPiecesExist()
|
|
{
|
|
Network theNet = NB.GetNetwork();
|
|
NetworkDevice Source = theNet.GetDeviceFromName(sHost);
|
|
NetworkDevice Dest = theNet.GetDeviceFromName(dHost);
|
|
if (Source == null) return false;
|
|
if (Dest == null) return false;
|
|
return true;
|
|
}
|
|
|
|
public bool ColorItemsIfNeeded(bool ChangeColor)
|
|
{
|
|
bool WasDone = TaskWasDone;
|
|
if(!TestComplete())
|
|
{
|
|
if(TheTest == NetTestType.ReadContextHelp && ChangeColor)
|
|
{
|
|
BuilderWindow myWin = NB.GetBuilderWin();
|
|
if(myWin != null)
|
|
{
|
|
Control ctl = myWin.GetControlNamed(sHost);
|
|
if (ctl == null) return false;
|
|
ctl.BackColor = WrongColor;
|
|
}
|
|
return false;
|
|
}
|
|
Network theNet = NB.GetNetwork();
|
|
NetworkDevice Source = theNet.GetDeviceFromName(sHost);
|
|
if(Source!= null)
|
|
{
|
|
if (ChangeColor)
|
|
{
|
|
Source.BackgroundColor = WrongColor;
|
|
Source.IsDirty = true; //Make sure we re-draw it
|
|
}
|
|
return true; //We have a test that is not completed
|
|
}
|
|
return true;
|
|
}
|
|
if(WasDone == false)
|
|
{
|
|
//We just solved it for the first time
|
|
TaskWasDone = true;
|
|
NB.PlaySound(NBSoundType.success);
|
|
}
|
|
return false; //No need to color anything
|
|
}
|
|
|
|
public void SetDone()
|
|
{
|
|
if(TaskWasDone == false)
|
|
{
|
|
NB.PlaySound(NBSoundType.success);
|
|
}
|
|
TaskWasDone = true;
|
|
}
|
|
|
|
/// <summary>
|
|
/// See if the test has been solved
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public bool TestComplete()
|
|
{
|
|
Network theNet = NB.GetNetwork();
|
|
NetworkDevice Source = theNet.GetDeviceFromName(sHost);
|
|
NetworkDevice Dest = theNet.GetDeviceFromName(dHost);
|
|
IPAddress gw;
|
|
IPAddress tAddr;
|
|
|
|
switch(TheTest)
|
|
{
|
|
case NetTestType.NeedsDefaultGW:
|
|
if (Source == null) return false; //Unable to do it. Do not count it against them.
|
|
if (Dest == null) return false; //Unable to do it. Do not count it against them.
|
|
|
|
gw = Source.GetGateway();
|
|
if(Dest.HasIPAddress(gw))
|
|
{
|
|
//It has the IP. Is it local to the source?
|
|
if(Source.LocalNic(gw) != null)
|
|
{
|
|
//The default gw is set to the IP of the dest
|
|
//The IP address chosen is "local" to the source host. So it should ping between them
|
|
return true;
|
|
}
|
|
}
|
|
return false; //Something is not set right.
|
|
case NetTestType.NeedsRouteToNet:
|
|
if (Source == null) return false; //Unable to do it. Do not count it against them.
|
|
tAddr = theNet.DNSLookup(Source,dHost);
|
|
if (tAddr == null || tAddr.GetIPString == NB.ZeroIPString)
|
|
tAddr = new IPAddress(dHost);
|
|
if (Source.HasRouteMatching(tAddr))
|
|
{
|
|
IPAddress route = Source.RouteMatching(tAddr);
|
|
if (Source.LocalNic(new IPAddress(route.GetGateway.ToIpString())) == null)
|
|
return false; //The gateway specified is not local to the device. We cannot get to it
|
|
return true;
|
|
}
|
|
return false; //if we get here, it has failed somehow
|
|
case NetTestType.NeedsLocalIPTo:
|
|
if (Source == null) return false; //Unable to do it. Do not count it against them.
|
|
if (Dest == null) return false; //Unable to do it. Do not count it against them.
|
|
tAddr = Source.LocalDeviceIP(Dest);
|
|
IPAddress dAddress = Dest.LocalDeviceIP(Source);
|
|
if (Dest.HasIPAddress(tAddr)) return false; //They gave the same address to the source that the dest has.
|
|
if (!theNet.HasUniqueIP(tAddr, Source)) return false; //Verify we have not given the IP to someone else
|
|
if (tAddr != null &&
|
|
tAddr.GetIPString != NB.ZeroIPString)
|
|
{
|
|
if(dAddress != null & dAddress.GetMask == tAddr.GetMask)
|
|
return true;
|
|
}
|
|
return false; //Something is not set right.
|
|
case NetTestType.NeedsLinkToDevice:
|
|
if (Source == null) return false; //Unable to do it. Do not count it against them.
|
|
if (Dest == null) return false; //Unable to do it. Do not count it against them.
|
|
if (Source.HasLinkTo(dHost)) return true;
|
|
return false; //Something is not set right.
|
|
case NetTestType.SuccessfullyArps:
|
|
case NetTestType.SuccessfullyDHCPs:
|
|
case NetTestType.SuccessfullyPings:
|
|
case NetTestType.HelpRequest:
|
|
case NetTestType.ReadContextHelp:
|
|
case NetTestType.FailedPing:
|
|
return TaskWasDone; //This variable will tell us if these tests have been done.
|
|
case NetTestType.LockAll:
|
|
case NetTestType.LockDHCP:
|
|
case NetTestType.LockIP:
|
|
case NetTestType.LockNic:
|
|
case NetTestType.LockRoute:
|
|
case NetTestType.LockGateway:
|
|
return true; //Nothing to solve. We just lock it so it cannot be changed.
|
|
}
|
|
return false;
|
|
}
|
|
}
|
|
} |