Getting up to date and moving translation stuff to separate function

This commit is contained in:
Tim Young 2015-08-01 11:58:53 -06:00
parent a18f760a9f
commit 5775c35020
20 changed files with 9365 additions and 3 deletions

View File

@ -0,0 +1,449 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.Globalization;
using System.Resources;
namespace EduNetworkBuilder
{
public partial class DeviceConfig : Form
{
NetworkComponent OriginalItem;
NetworkComponent ClonedItem;
bool processing = false;
ToolTip mytooltips = new ToolTip();
public DeviceConfig(NetworkComponent Original_Item)
{
InitializeComponent();
LanguagifyComponents();
OriginalItem = Original_Item;
ClonedItem = NetworkComponent.Clone(OriginalItem);
UpdateForm();
lblHelp.Text = "Select the network card or interface for that network card that you wish to edit. " +
"Then press the [edit] button to make changes to that. Press the [-] to remove it, and press [+] to add another.";
}
private void LoadFromOrig()
{
ClonedItem.UpdateFromComponent(OriginalItem);
}
private void LoadFromClone()
{
OriginalItem.UpdateFromComponent(ClonedItem);
}
private void LanguagifyComponents()
{
ResourceManager RM = NB.GetResource();
CultureInfo CI = NB.GetCulture();
Text = NB.Translate("DC_lblHostname");
Text = NB.Translate("DC_lblHelp");
Text = NB.Translate("DC_btnCancel");
Text = NB.Translate("DC_btnOK");
Text = NB.Translate("DC_btnNicPlus");
Text = NB.Translate("DC_btnNicMinus");
Text = NB.Translate("DC_btnNicEdit");
Text = NB.Translate("DC_btnIfEdit");
Text = NB.Translate("DC_btnIfMinus");
Text = NB.Translate("DC_btnIfAdd");
Text = NB.Translate("DC_btnGateway");
Text = NB.Translate("DC_btnRoutes");
Text = NB.Translate("DC_cbDHCP");
Text = NB.Translate("DC_btnDHCP");
Text = NB.Translate("DC_Form");
}
private void UpdateForm()
{
Network theNet = NB.GetNetwork();
if (theNet.ItemIsCritical(OriginalItem.hostname))
{
tbHostname.Enabled = false;
mytooltips.SetToolTip(tbHostname, "This item is part of the puzzle and cannot be renamed.");
}
if (processing) return;
processing = true;
if (ClonedItem.GetType().ToString() == "EduNetworkBuilder.NetworkDevice")
{
NetworkDevice ndCLonedItem = (NetworkDevice)ClonedItem;
if(ndCLonedItem.CanAddNics)
{
btnNicPlus.Visible = true;
btnNicMinus.Visible = true;
}else
{
btnNicPlus.Visible = false;
btnNicMinus.Visible = false;
}
if(ndCLonedItem.CanServeDHCP)
{
if (ndCLonedItem.GetIsDHCPServer())
cbDHCP.Checked = true;
cbDHCP.Visible = true;
if(cbDHCP.Checked)
btnDHCP.Visible = true;
else
btnDHCP.Visible = false;
}
else
{
btnDHCP.Visible = false;
cbDHCP.Visible = false;
}
btnGateway.Visible = true;
btnGateway.Text = "Gateway: " + ndCLonedItem.GetGateway().GetIP.ToIpString();
tbHostname.Text = ndCLonedItem.hostname;
//Network cards are changed with the [edit] button and do not need to be updated here
//IPAddresses are changed with the [edit] button and do not need to be updated here
//The Nics
int selectedLb = lbNics.SelectedIndex;
lbNics.Items.Clear();
List<string> lists = ndCLonedItem.NetworkCardStrings(false);
foreach (string tstring in lists)
{
lbNics.Items.Add(tstring);
}
if(selectedLb < 0)
{
if (lbNics.Items.Count > 0)
{
lbNics.SelectedIndex = 0;
selectedLb = 0;
}
}
else
{
if (selectedLb >= lbNics.Items.Count) selectedLb = -1;
lbNics.SelectedIndex = selectedLb;
}
if (lbNics.SelectedIndex >= 0)
{
btnNicEdit.Enabled = true;
btnNicMinus.Enabled = true;
}
else
{
btnNicEdit.Enabled = false;
btnNicMinus.Enabled = false;
}
lbArpTable.Items.Clear();
lbArpTable.Items.Add("Arp Table MAC\tIPAddress");
foreach(ArpEntry ae in ndCLonedItem.GetArps())
{
lbArpTable.Items.Add(ae.MACAddress + "\t" + ae.IPAddr);
}
//The IP Addresses for the given nic
//Use the index of the nic
string selectedNicName = "";
if (lbNics.SelectedItem != null) selectedNicName = lbNics.SelectedItem.ToString();
selectedNicName = Regex.Replace(selectedNicName, " .*", "");
selectedNicName = Regex.Replace(selectedNicName, "\\*", "");
lists = ndCLonedItem.NetworkCardInterfaceStrings(selectedNicName);
//get the IPAddress string list from the nic
selectedLb = lbAddresses.SelectedIndex;
lbAddresses.Items.Clear();
foreach (string tstring in lists)
{
lbAddresses.Items.Add(tstring);
}
if (selectedLb < 0)
{
if (lbAddresses.Items.Count > 0)
lbAddresses.SelectedIndex = 0;
}
else
{
if (lbAddresses.Items.Count > 0 && selectedLb < lbAddresses.Items.Count)
lbAddresses.SelectedIndex = selectedLb;
}
if (lbNics.SelectedIndex >= 0)
{
btnIfEdit.Enabled = true;
btnIfMinus.Enabled = true;
}
else
{
btnIfEdit.Enabled = false;
btnIfMinus.Enabled = false;
}
}
processing = false;
}
private void StoreInCloneFromForm()
{
if(NB.GetComponentType(ClonedItem) == GeneralComponentType.device)
{
NetworkDevice ndCLonedItem = (NetworkDevice)ClonedItem;
ndCLonedItem.hostname = tbHostname.Text;
//Network cards are changed with the [edit] button and do not need to be updated here
//IPAddresses are changed with the [edit] button and do not need to be updated here
}
}
private void tbHostname_Validating(object sender, CancelEventArgs e)
{
//Make sure something else does not already have this hostname
Network theNet = NB.GetNetwork();
string otext;
if(theNet.ItemIsCritical(tbHostname.Text))
{
if(tbHostname.Text != OriginalItem.hostname)
{
e.Cancel = true;
otext = tbHostname.Text;
tbHostname.Text = OriginalItem.hostname;
MessageBox.Show("The name '" + otext + "' is being used as part of this puzzle and this machine cannot be named that.");
}
}
}
private void btnNicPlus_Click(object sender, EventArgs e)
{
List<NicType> WhatToAdd = new List<NicType>();
//generate a new nic and add it to the device
if (ClonedItem.GetType().ToString() == "EduNetworkBuilder.NetworkDevice")
{
NetworkDevice ndCLonedItem = (NetworkDevice)ClonedItem;
if (!ndCLonedItem.CanAddNics) return;
if(ndCLonedItem.GetNetType() == NetworkComponentType.firewall)
{
WhatToAdd.Add(NicType.eth);
WhatToAdd.Add(NicType.tun);
WhatToAdd.Add(NicType.vpn);
}
if (ndCLonedItem.GetNetType() == NetworkComponentType.net_hub || ndCLonedItem.GetNetType() == NetworkComponentType.net_switch)
{
WhatToAdd.Add(NicType.port);
}
if (ndCLonedItem.GetNetType() == NetworkComponentType.router )
{
WhatToAdd.Add(NicType.eth);
}
if (ndCLonedItem.GetNetType() == NetworkComponentType.pc && ndCLonedItem.CountNics(NicType.eth) < 1)
{
WhatToAdd.Add(NicType.eth);
}
if (WhatToAdd.Count == 1)
{
ndCLonedItem.AddNic(WhatToAdd[0]);
}
if(WhatToAdd.Count > 1)
{
//we need to choose a type:
Form QuestionForm = new Form();
QuestionForm.Text = "Network Card to Add";
QuestionForm.Icon = Properties.Resources.NBIco;
Label lblText = new Label();
lblText.Location = new Point(5, 5);
lblText.Text = "Choose a type:";
ComboBox cbQuestions = new ComboBox();
cbQuestions.Location = new Point(lblText.Location.X + lblText.Width + 5, lblText.Location.Y);
cbQuestions.Width = 60;
foreach(NicType nt in WhatToAdd)
{
cbQuestions.Items.Add(nt.ToString());
}
cbQuestions.SelectedIndex = 0;
QuestionForm.Width = cbQuestions.Location.X + cbQuestions.Width + 70;
QuestionForm.Height = 90;
QuestionForm.AutoSize = true;
Button btnAccept = new Button();
btnAccept.Location = new Point(cbQuestions.Location.X, cbQuestions.Location.Y + cbQuestions.Height + 10);
btnAccept.Text = "Add";
btnAccept.Click += (s, g) => { Button b = (Button)s; Form f = (Form)b.Parent; f.Close(); };
QuestionForm.Controls.Add(lblText);
QuestionForm.Controls.Add(cbQuestions);
QuestionForm.Controls.Add(btnAccept);
QuestionForm.ShowDialog();
if(cbQuestions.SelectedIndex >=0)
{
ndCLonedItem.AddNic(WhatToAdd[cbQuestions.SelectedIndex]);
}
}
}
UpdateForm();
}
private void btnNicMinus_Click(object sender, EventArgs e)
{
//delete the specified nic
if (ClonedItem.GetType().ToString() == "EduNetworkBuilder.NetworkDevice")
{
NetworkDevice ndCLonedItem = (NetworkDevice)ClonedItem;
ndCLonedItem.DeleteNic(lbNics.SelectedIndex + 1); //Skip the loopback nic
UpdateForm();
}
}
private void btnNicEdit_Click(object sender, EventArgs e)
{
//open a window to change the settings on the nic
if (ClonedItem.GetType().ToString() == "EduNetworkBuilder.NetworkDevice")
{
NetworkDevice ndCLonedItem = (NetworkDevice)ClonedItem;
ndCLonedItem.EditNic(lbNics.SelectedIndex +1); //Skip the loopback nic
UpdateForm();
}
}
private void btnIfAdd_Click(object sender, EventArgs e)
{
if (NB.GetComponentType(ClonedItem) == GeneralComponentType.device)
{
NetworkDevice ND = (NetworkDevice)ClonedItem;
ND.AddNicInterface(lbNics.SelectedIndex + 1); //we do not show loopback
}
UpdateForm();
}
private void btnIfMinus_Click(object sender, EventArgs e)
{
//if one is selected and there are more than one, delete it
if(lbAddresses.SelectedIndex >= 0)
{
if (lbAddresses.Items.Count > 1)
{
//we can delete it
if(NB.GetComponentType(ClonedItem) == GeneralComponentType.device)
{
NetworkDevice ND = (NetworkDevice)ClonedItem;
ND.DeleteNicInterface(lbNics.SelectedIndex + 1, lbAddresses.SelectedIndex);
}
}
else
MessageBox.Show("You cannot delete the sole remaining interface.");
}
else
MessageBox.Show("Please select an interface to delete.");
UpdateForm();
}
private void editInterface()
{
if (ClonedItem.GetType().ToString() == "EduNetworkBuilder.NetworkDevice")
{
NetworkDevice ndCLonedItem = (NetworkDevice)ClonedItem;
if (lbAddresses.SelectedIndex >= 0 && lbNics.SelectedIndex >= 0)
{
string selectedNIC = lbNics.SelectedItem.ToString();
selectedNIC = Regex.Replace(selectedNIC, " .*", "");
selectedNIC = Regex.Replace(selectedNIC, "\\*", "");
ndCLonedItem.EditNicInterface(selectedNIC, lbAddresses.SelectedIndex);
UpdateForm();
}
}
}
private void btnIfEdit_Click(object sender, EventArgs e)
{
editInterface();
}
private void btnCancel_Click(object sender, EventArgs e)
{
//we should prompt to save if we made changes
Close();
}
private void btnOK_Click(object sender, EventArgs e)
{
StoreInCloneFromForm();
LoadFromClone(); //this saves the values in the object we passed it
OriginalItem.IsDirty = true; //re-draw it
Close();
}
private void lbNics_SelectedIndexChanged(object sender, EventArgs e)
{
UpdateForm();
processing = false;
}
private void lbAddresses_DoubleClick(object sender, EventArgs e)
{
if(lbAddresses.SelectedIndex >=0)
{
//We have one to edit
editInterface();
UpdateForm();
}
}
private void lbNics_DoubleClick(object sender, EventArgs e)
{
if (NB.GetComponentType(ClonedItem) == GeneralComponentType.device)
{
NetworkDevice nd = (NetworkDevice)ClonedItem;
nd.EditNic(lbNics.SelectedIndex + 1); //skip the loopback device
UpdateForm();
}
}
private void btnGateway_Click(object sender, EventArgs e)
{
if (NB.GetComponentType(ClonedItem) == GeneralComponentType.device)
{
NetworkDevice nd = (NetworkDevice)ClonedItem;
nd.EditGateway();
UpdateForm();
}
}
private void btnRoutes_Click(object sender, EventArgs e)
{
if (ClonedItem == null) return;
if (NB.GetComponentType(ClonedItem) == GeneralComponentType.link) return;
NetworkDevice tItem = (NetworkDevice)ClonedItem;
ListBoxWindow lbwindow = new ListBoxWindow(tItem,LBContents.routes);
lbwindow.ShowDialog();
}
private void btnDHCP_Click(object sender, EventArgs e)
{
if (ClonedItem == null) return;
if (NB.GetComponentType(ClonedItem) == GeneralComponentType.link) return;
NetworkDevice tItem = (NetworkDevice)ClonedItem;
ListBoxWindow lbwindow = new ListBoxWindow(tItem, LBContents.dhcp);
lbwindow.ShowDialog();
}
private void cbDHCP_CheckedChanged(object sender, EventArgs e)
{
if(NB.GetComponentType(ClonedItem) == GeneralComponentType.device)
{
NetworkDevice ndClonedItem = (NetworkDevice)ClonedItem;
ndClonedItem.SetDHCPServer(cbDHCP.Checked);
}
UpdateForm();
}
}
}

View File

@ -0,0 +1,274 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Globalization;
using System.Xml;
using System.Windows.Forms;
namespace EduNetworkBuilder
{
[Serializable]
public class IPAddress
{
private UInt32 _ip;
private UInt32 _mask;
private UInt32 _gw;
private IPAddressType myType;
public IPAddress(string ip, string mask, IPAddressType WhatType)
{
myType = WhatType;
_ip = ip.ParseIp();
_mask = mask.ParseIp();
}
public IPAddress(string ip, string mask, string gw)
{
myType = IPAddressType.route;
_ip = ip.ParseIp();
_mask = mask.ParseIp();
_gw = gw.ParseIp();
}
public IPAddress(XmlNode theNode)
{
foreach (XmlNode Individual in theNode.ChildNodes)
{
XmlNodeType myNodetype = Individual.NodeType;
if (myNodetype == XmlNodeType.Element)
{
switch (Individual.Name.ToLower())
{
case "ip":
_ip = Individual.InnerText.ParseIp();
break;
case "mask":
_mask = Individual.InnerText.ParseIp();
break;
case "gateway":
_gw = Individual.InnerText.ParseIp();
break;
case "type":
myType = NB.ParseEnum<IPAddressType>(Individual.InnerText);
break;
}
}
}
}
public void Save(XmlWriter writer, string tag)
{
writer.WriteStartElement(tag);
writer.WriteElementString("ip", _ip.ToIpString());
writer.WriteElementString("mask", _mask.ToIpString());
writer.WriteElementString("gateway", _gw.ToIpString());
writer.WriteElementString("type", myType.ToString());
writer.WriteEndElement();
}
public void Reparse(string ip, string mask, string gw)
{
_ip = ip.ParseIp();
_mask = mask.ParseIp();
_gw = gw.ParseIp();
}
public void Reparse(string ip, string mask)
{
_ip = ip.ParseIp();
_mask = mask.ParseIp();
}
public IPAddressType GetAddressType
{
get { return myType; }
}
public bool IsLocal(IPAddress dest)
{
if (dest == null) return false;
UInt32 answer = dest.GetIP & _mask;
if ((dest.GetIP & _mask) == NetworkAddress)
return true;
return false;
}
public bool Edit(NetworkDevice FromWhat, string message="")
{
IPAddressEntry IPe = new IPAddressEntry(this, FromWhat);
if (message != "")
IPe.Text = message;
return IPe.Edit();
}
public bool Edit(NetworkDevice FromWhat, IPAddress DHCPif)
{
IPAddressEntry IPe = new IPAddressEntry(this, FromWhat);
return IPe.Edit(FromWhat, DHCPif);
}
public IPAddress(string ip)
{
myType = IPAddressType.ip;
_ip = ip.ParseIp();
var mySplitVal = ip.Split('/');
if (mySplitVal.Count() < 2)
{
//We do not have a cidr. We need to guess
//For now, use 255.255.255.0
_mask = "255.255.255.0".ParseIp();
mySplitVal = ip.Split('.');
if(mySplitVal.Count() > 0)
{
//If it is not one of these three, we already use 255.255.255.0
if(mySplitVal[0] == "10")
{
_mask = "255.0.0.0".ParseIp();
}
if (mySplitVal[0] == "172")
{
_mask = "255.255.0.0".ParseIp();
}
if (mySplitVal[0] == "192")
{
_mask = "255.255.255.0".ParseIp();
}
}
}
else
{
UInt32 tInt = 0;
int cdr;
int.TryParse(mySplitVal[1], out cdr);
for (int loop = 0; loop < 32; loop++)
{
tInt = (tInt << 1);
if (loop < cdr) tInt++;
}
_mask = tInt;
}
}
public bool Equals(UInt32 IP)
{
return IP == _ip;
}
public bool Equals(UInt32 IP, UInt32 mask)
{
return (IP == _ip && mask == _mask);
}
public UInt32 NumberOfHosts
{
get { return ~_mask + 1; }
}
public UInt32 GetIP
{
get { return _ip; }
}
public string GetIPString
{
get { return _ip.ToIpString(); }
}
public string GetBroadcastString
{
get { return BroadcastAddress.ToIpString(); }
}
public string GetMaskString
{
get { return _mask.ToIpString(); }
}
public UInt32 GetMask
{
get { return _mask; }
}
public UInt32 GetGateway
{
get { return _gw; }
}
public UInt32 NetworkAddress
{
get { return _ip & _mask; }
}
public UInt32 BroadcastAddress
{
get { return NetworkAddress + ~_mask; }
}
public IEnumerable<UInt32> Hosts()
{
for (var host = NetworkAddress + 1; host < BroadcastAddress; host++)
{
yield return host;
}
}
public string IPFormat()
{
return IPFormat(_gw.ToIpString());
}
public string PadIt(string Addr)
{
string myaddr = Addr.PadRight(12);
return myaddr;
}
public string IPFormat(string gw)
{
string tstring = "IP:" +PadIt(_ip.ToIpString())+
" Mask:" + PadIt(_mask.ToIpString())+
" GW:" + PadIt(gw);
return tstring;
}
}
public static class IpHelpers
{
public static string ToIpString(this UInt32 value)
{
var bitmask = 0xff000000;
var parts = new string[4];
for (var i = 0; i < 4; i++)
{
var masked = (value & bitmask) >> ((3 - i) * 8);
bitmask >>= 8;
parts[i] = masked.ToString(CultureInfo.InvariantCulture);
}
return String.Join(".", parts);
}
public static string ToBitString(this UInt32 value)
{
var item = System.Convert.ToString(value, 2);
return (string)item;
}
public static UInt32 ParseIp(this string ipAddress)
{
var gw = ipAddress.Split('/'); //Pull off any cdr
var mySplitVal = gw[0].Split('.');
if (mySplitVal.Count() != 4)
return 0;
UInt32 ip = 0;
uint part;
if (mySplitVal.Count() == 4)
{
for (var i = 0; i < 4; i++)
{
part = 0;
UInt32.TryParse(mySplitVal[i], out part);
ip = (ip << 8) + part;
}
}
return ip;
}
}
}

View File

@ -0,0 +1,246 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Globalization;
using System.Resources;
namespace EduNetworkBuilder
{
public partial class IPAddressEntry : Form
{
IPAddress WhatToEdit;
IPAddress DHCPInterface=null;
bool WellDone = true;
NetworkDevice ParentDevice = null;
public IPAddressEntry(IPAddress toEdit, NetworkDevice ToEdit)
{
InitializeComponent();
LanguagifyComponents();
ParentDevice = ToEdit;
Network myNet = NB.GetNetwork();
IPAddress lastIP = myNet.RetrieveLastIP();
WhatToEdit = toEdit;
string hostname = "";
if (ToEdit != null)
hostname = ToEdit.hostname;
if (toEdit.GetIP.ToIpString() == NB.ZeroIPString)
{
string lIP = lastIP.GetIP.ToIpString();
string lNM = lastIP.GetMask.ToIpString();
string lGW = lastIP.GetGateway.ToIpString();
switch (WhatToEdit.GetAddressType)
{
case IPAddressType.gw:
lNM = NB.ZeroIPString;
lGW = NB.ZeroIPString;
break;
case IPAddressType.ip:
case IPAddressType.ip_only:
if (!lNM.Contains("255"))
lNM = "255.255.255.0";
lGW = NB.ZeroIPString;
break;
case IPAddressType.route:
break;
}
WhatToEdit.Reparse(lIP, lNM, lGW);
}
UpdateFieldsFromAddress();
switch (WhatToEdit.GetAddressType)
{
case IPAddressType.gw:
tbGateway.Enabled = false;
tbGateway.Visible = false; //This just confuses people
tbIPAddress.Enabled = true;
tbNetmask.Enabled = false;
break;
case IPAddressType.ip:
tbGateway.Enabled = false;
tbIPAddress.Enabled = true;
tbNetmask.Enabled = true;
break;
case IPAddressType.route:
tbGateway.Enabled = true;
tbIPAddress.Enabled = true;
tbNetmask.Enabled = true;
break;
case IPAddressType.ip_only:
tbGateway.Enabled = false;
tbIPAddress.Enabled = true;
tbNetmask.Enabled = false;
break;
}
//Disable anythingthatis locked
switch (WhatToEdit.GetAddressType)
{
case IPAddressType.gw:
if (myNet.ItemIsLocked(hostname, tbIPAddress.Text, NetTestType.LockGateway))
{
tbGateway.Enabled = false;
tbIPAddress.Enabled = false;
tbNetmask.Enabled = false;
}
break;
case IPAddressType.ip:
if (myNet.ItemIsLocked(hostname, tbIPAddress.Text, NetTestType.LockIP))
{
tbGateway.Enabled = false;
tbIPAddress.Enabled = false;
tbNetmask.Enabled = false;
}
break;
case IPAddressType.route:
if (myNet.ItemIsLocked(hostname, tbIPAddress.Text, NetTestType.LockRoute))
{
tbGateway.Enabled = false;
tbIPAddress.Enabled = false;
tbNetmask.Enabled = false;
}
break;
case IPAddressType.ip_only:
if (myNet.ItemIsLocked(hostname, tbIPAddress.Text, NetTestType.LockIP))
{
tbIPAddress.Enabled = false;
}
break;
}
}
private void LanguagifyComponents()
{
ResourceManager RM = NB.GetResource();
CultureInfo CI = NB.GetCulture();
Text = NB.Translate("IPAE_lblIP");
Text = NB.Translate("IPAE_lblNetmask");
Text = NB.Translate("IPAE_lblGateway");
Text = NB.Translate("IPAE_btnCancel");
Text = NB.Translate("IPAE_btnOK");
Text = NB.Translate("IPAE_Form");
}
private void UpdateFieldsFromAddress()
{
tbIPAddress.Text = WhatToEdit.GetIP.ToIpString();
tbNetmask.Text = WhatToEdit.GetMask.ToIpString();
tbGateway.Text = WhatToEdit.GetGateway.ToIpString();
}
private void btnOK_Click(object sender, EventArgs e)
{
WhatToEdit.Reparse(tbIPAddress.Text, tbNetmask.Text, tbGateway.Text);
Network myNet = NB.GetNetwork();
myNet.StoreLastIP(WhatToEdit);
Close();
}
public bool Edit()
{
ShowDialog();
return WellDone;
}
public bool Edit(NetworkDevice ToEdit, IPAddress DHCPif)
{
DHCPInterface = DHCPif;
lblIP.Text = "Interface";
lblNetmask.Text = "Start";
lblGateway.Text = "End";
tbIPAddress.Enabled = false;
tbGateway.Enabled = true;
tbNetmask.Enabled = true;
Network myNet = NB.GetNetwork();
if (myNet.ItemIsLocked(ToEdit.hostname, tbIPAddress.Text, NetTestType.LockDHCP))
{
tbGateway.Enabled = false;
tbIPAddress.Enabled = false;
tbNetmask.Enabled = false;
}
ShowDialog();
return WellDone;
}
private void btnCancel_Click(object sender, EventArgs e)
{
UpdateFieldsFromAddress();
WellDone = false;
Close();
}
private void tbGateway_Validating(object sender, CancelEventArgs e)
{
Network mynet = NB.GetNetwork();
if (ParentDevice != null)
{
IPAddress tAddress = mynet.DNSLookup(ParentDevice, tbGateway.Text);
}
UInt32 taddress = tbGateway.Text.ParseIp();
UInt32 startaddress = tbNetmask.Text.ParseIp();
tbGateway.Text = taddress.ToIpString();
if (DHCPInterface != null)
{
//We also check to verify that the address is in the correct range
if (!DHCPInterface.IsLocal(new IPAddress(tbGateway.Text)))
{
e.Cancel = true;
MessageBox.Show("The end IP in the range must be within the scope of the interface.");
}
if (taddress < startaddress)
{
e.Cancel = true;
MessageBox.Show("The end IP must be equal to or larger than the start IP.");
}
}
}
private void tbNetmask_Validating(object sender, CancelEventArgs e)
{
UInt32 taddress = tbNetmask.Text.ParseIp();
tbNetmask.Text = taddress.ToIpString();
if (DHCPInterface != null)
{
//We also check to verify that the address is in the correct range
if (!DHCPInterface.IsLocal(new IPAddress(tbNetmask.Text)))
{
e.Cancel = true;
MessageBox.Show("The start IP in the range must be within the scope of the interface.");
}
}
}
private void tbIPAddress_Validating(object sender, CancelEventArgs e)
{
UInt32 taddress;
Network mynet = NB.GetNetwork();
IPAddress tIPAddress = null;
if (ParentDevice != null)
{
tIPAddress = mynet.DNSLookup(ParentDevice, tbIPAddress.Text);
}
if(tIPAddress != null)
{
taddress = tIPAddress.GetIP;
}
else
taddress = tbIPAddress.Text.ParseIp();
tbIPAddress.Text = taddress.ToIpString();
}
public void SetText(string text)
{
this.Text = text;
}
}
}

View File

@ -60,9 +60,9 @@ namespace EduNetworkBuilder
{ {
ResourceManager RM = NB.GetResource(); ResourceManager RM = NB.GetResource();
CultureInfo CI = NB.GetCulture(); CultureInfo CI = NB.GetCulture();
Text = RM.GetString("LE_btnLink", CI); Text = NB.Translate("LE_btnLink");
Text = RM.GetString("LE_btnCancel", CI); Text = NB.Translate("LE_btnCancel");
Text = RM.GetString("LE_Form", CI); Text = NB.Translate("LE_Form");
} }

View File

@ -0,0 +1,442 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Globalization;
using System.Resources;
namespace EduNetworkBuilder
{
public partial class ListBoxWindow : Form
{
protected LBContents MyMode = LBContents.messages;
protected DebugLevel WhatToDisplay = DebugLevel.info; //used when viewing packet-messages
NetworkDevice myNetDevice;
PacketMessage myPacketMessage;
int EditableCount = -1;
bool processing = false;
/// <summary>
/// Instantiate a ListBoxWindow for use in choosing a network to load
/// </summary>
public ListBoxWindow()
{
InitializeComponent();
LanguagifyComponents();
MyMode = LBContents.puzzles;
UpdateForm();
CheckBox newCB;
btnAdd.Visible = false;
int count = 0;
lblInstructions.Text = "Filter the puzzles by checking one the boxes.";
panelCheckboxes.SuspendLayout();
string SelectedTag = GetSelectedTag();
if (SelectedTag == "ALL")
Text = Text + "(All Solved)";
foreach (string str in NB.GetPuzzleTags())
{
newCB = AddCheckBox(count, str);
if (str == SelectedTag || (SelectedTag == "ALL" && Regex.IsMatch(str,"Level")))
{
newCB.Checked = true; //The first level with an unsolved puzzle starts checked
}
else
{
newCB.Checked = false;
}
count++;
}
panelCheckboxes.ResumeLayout();
btnOK.Text = "Load";
btnAdd.Text = "Cancel";
btnAdd.Visible = true;
cbLoadPuzzlesAtStart.Checked = Properties.Settings.Default.AutoStartPuzzles;
UpdateForm();
}
/// <summary>
/// We are doing a routing table or dhcp. We pass it the device and pull the data from that
/// </summary>
/// <param name="MasterDevice">The master device to edit/view the routing table of</param>
public ListBoxWindow(NetworkDevice MasterDevice, LBContents mode)
{
InitializeComponent();
LanguagifyComponents();
if (mode == LBContents.routes)
{
MyMode = LBContents.routes;
myNetDevice = MasterDevice;
lblInstructions.Text = "Double-click a route to change it.";
lbWindowData.Font = new System.Drawing.Font("Courier New", 8);
btnAdd.Visible = true;
}
if (mode == LBContents.dhcp)
{
MyMode = LBContents.dhcp;
myNetDevice = MasterDevice;
lblInstructions.Text = "Double-Click the interface to add/edit the range of DHCP to serve.";
lbWindowData.Font = new System.Drawing.Font("Courier New", 8);
btnAdd.Visible = false;
}
UpdateForm();
}
/// <summary>
/// We are doing a packet message detail look.
/// </summary>
/// <param name="Messages">The packet message to look at</param>
public ListBoxWindow(PacketMessage Messages)
{
InitializeComponent();
LanguagifyComponents();
MyMode = LBContents.messages;
myPacketMessage = Messages;
UpdateForm();
btnAdd.Visible = false;
int count=0;
lblInstructions.Text = "For greater detail, click on the check-boxes";
panelCheckboxes.SuspendLayout();
foreach (string str in Enum.GetNames(typeof(DebugLevel)))
{
AddCheckBox(count, str);
count++;
}
panelCheckboxes.ResumeLayout();
UpdateChecked();
}
private void LanguagifyComponents()
{
ResourceManager RM = NB.GetResource();
CultureInfo CI = NB.GetCulture();
Text = NB.Translate("LBW_btnOK");
Text = NB.Translate("LBW_lblInstructions");
Text = NB.Translate("LBW_btnAdd");
Text = NB.Translate("LBW_cbLoadPuzzlesAtStart");
Text = NB.Translate("LBW_btnReset");
Text = NB.Translate("LBW_Form");
}
private CheckBox AddCheckBox(int count, string name)
{
int myY = 1;
int myX = 1;
int cdwidth = 70;
int cdHeight = 20;
int divisor = panelCheckboxes.Width / cdwidth;
myX = (cdwidth * count) % (divisor * cdwidth);
myY = ((cdwidth * count) / (divisor * cdwidth)) * cdHeight;
CheckBox newCB = new CheckBox();
newCB.Text = name;
Point mylocation = new Point(myX, myY);
newCB.AutoSize = true;
newCB.Name = "checkBox" + count.ToString();
newCB.Size = new System.Drawing.Size(98, 21);
newCB.TabIndex = count;
newCB.UseVisualStyleBackColor = true;
newCB.Location = mylocation;
newCB.CheckedChanged += genericCheckmarkChange;
panelCheckboxes.Controls.Add(newCB);
return newCB;
}
private string GetSelectedTag()
{
if (Properties.Settings.Default.ScoreList == null)
Properties.Settings.Default.ScoreList = new System.Collections.Specialized.StringCollection();
PuzzleInfo PI;
foreach (string str in NB.GetPuzzleNames())
{
if(!Properties.Settings.Default.ScoreList.Contains(str))
{
PI = NB.GetPuzzleInfoFromName(str);
return "Level_" + PI.Level;
}
}
return "ALL";
}
private void UpdateForm()
{
int selectedIndex = lbWindowData.SelectedIndex;
string selected = "";
if (lbWindowData.SelectedIndex != -1)
selected = lbWindowData.Items[lbWindowData.SelectedIndex].ToString();
lbWindowData.Items.Clear();
cbLoadPuzzlesAtStart.Visible = false;
btnReset.Visible = false;
List<string> itemstrings = new List<string>();
if (MyMode == LBContents.messages)
{
itemstrings.AddRange(myPacketMessage.GetMessagesSummary());
itemstrings.AddRange(myPacketMessage.GetMessagesLike(WhatToDisplay));
foreach (string str in itemstrings)
{
lbWindowData.Items.Add(str);
}
}
if(MyMode == LBContents.routes)
{
itemstrings.Add("--Static Routes (Read/Write)--");
itemstrings.AddRange(myNetDevice.DeviceRouteStrings());
EditableCount = itemstrings.Count();
itemstrings.Add("--Routes from NICs (Read-Only)--");
itemstrings.AddRange(myNetDevice.NICRouteStrings());
foreach (string str in itemstrings)
{
lbWindowData.Items.Add(str);
}
}
if (MyMode == LBContents.dhcp)
{
myNetDevice.CheckDHCPRangesAgainstInterfaces();
itemstrings.AddRange(myNetDevice.DHCPStrings());
foreach (string str in itemstrings)
{
lbWindowData.Items.Add(str);
}
}
if (MyMode == LBContents.puzzles)
{
cbLoadPuzzlesAtStart.Visible = true;
btnReset.Visible = true;
PuzzleInfo pi;
if (Properties.Settings.Default.ScoreList == null)
Properties.Settings.Default.ScoreList = new System.Collections.Specialized.StringCollection();
string shown_name;
List<string> Puzzles = NB.GetPuzzleNames();
if (Puzzles == null) return;
foreach (string str in Puzzles)
{
pi = NB.GetPuzzleInfoFromName(str);
shown_name = pi.PuzzleName;
if (Properties.Settings.Default.ScoreList.Contains(str))
shown_name = "* " + shown_name;
foreach(string tag in pi.PuzzleTags)
{
if(isChecked(tag))
{
lbWindowData.Items.Add(shown_name);
break;
}
}
}
if(selected != null && selected != "")
{
if (lbWindowData.Items.Contains(selected))
lbWindowData.SelectedItem = selected;
}
if(lbWindowData.SelectedIndex != -1)
{
btnOK.Enabled = true;
}
else
{
btnOK.Enabled = false;
}
}
}
private void btnOK_Click(object sender, EventArgs e)
{
if (MyMode == LBContents.puzzles && lbWindowData.SelectedItem != null)
{
string TheName = lbWindowData.SelectedItem.ToString();
TheName = Regex.Replace(TheName, @"^\* ", "");
Visible = false;
BuilderWindow myWin = (BuilderWindow)Application.OpenForms["BuilderWindow"];
if (myWin == null)
{
myWin.Activate();
}
if(Properties.Settings.Default.AutoStartPuzzles != cbLoadPuzzlesAtStart.Checked)
{
Properties.Settings.Default.AutoStartPuzzles = cbLoadPuzzlesAtStart.Checked;
Properties.Settings.Default.Save();
}
if (lbWindowData.SelectedItem != null)
NB.LoadNetworkFromResource(TheName);
}
Close();
}
/// <summary>
/// The checkmark was changed. Tally up everything
/// </summary>
/// <param name="sender">The checkmark that changed</param>
/// <param name="e"></param>
private void genericCheckmarkChange(object sender, EventArgs e)
{
if (processing) return;
CheckBox mybox = (CheckBox)sender;
if (MyMode == LBContents.messages)
{
if (mybox.Checked)
{
WhatToDisplay = WhatToDisplay | NB.ParseEnum<DebugLevel>(mybox.Text);
}
else
{
WhatToDisplay = WhatToDisplay ^ NB.ParseEnum<DebugLevel>(mybox.Text);
}
}
UpdateChecked();
}
private bool isChecked(string Name)
{
foreach (Control mycontrol in panelCheckboxes.Controls)
{
//pull out the name
CheckBox cb = (CheckBox)mycontrol;
//make an enum. See if that is set. If so, mark it as checked
if(cb.Text.ToLower() == Name.ToLower())
{
return cb.Checked;
}
}
return false;
}
private void UpdateChecked()
{
processing = true;
DebugLevel toFind;
if (MyMode == LBContents.messages)
{
foreach (Control mycontrol in panelCheckboxes.Controls)
{
//pull out the name
CheckBox cb = (CheckBox)mycontrol;
//make an enum. See if that is set. If so, mark it as checked
toFind = NB.ParseEnum<DebugLevel>(mycontrol.Text);
if ((toFind & WhatToDisplay) == toFind)
{
cb.Checked = true;
}
else //If not, mark it as not checked.
{
cb.Checked = false;
}
}
}
processing=false;
UpdateForm();
}
private void btnAdd_Click(object sender, EventArgs e)
{
if (MyMode == LBContents.routes)
{
AddRoute();
}
if(MyMode == LBContents.puzzles)
{
if (Properties.Settings.Default.AutoStartPuzzles != cbLoadPuzzlesAtStart.Checked)
{
Properties.Settings.Default.AutoStartPuzzles = cbLoadPuzzlesAtStart.Checked;
Properties.Settings.Default.Save();
}
Close();
}
}
private void AddRoute()
{
IPAddress newip = new IPAddress(NB.ZeroIPString, NB.ZeroIPString, NB.ZeroIPString);
newip.Edit(myNetDevice,"Create Route");
myNetDevice.AddRoute(newip);
UpdateForm();
}
private void lbWindowData_DoubleClick(object sender, EventArgs e)
{
if (MyMode == LBContents.routes)
{
if (lbWindowData.SelectedIndex >= EditableCount) return;
if (lbWindowData.SelectedIndex < 1) return;
//we always subtract 1 since we have added one line of comment to the list
myNetDevice.EditRoute(lbWindowData.SelectedIndex - 1);
}
if(MyMode == LBContents.dhcp)
{
//We always add one since we are skipping the loopback device
myNetDevice.EditDHCP(lbWindowData.SelectedIndex + 1);
}
if (MyMode == LBContents.puzzles)
{
//Just like pressing the OK button. We load the map
btnOK_Click(sender, e);
}
UpdateForm();
}
private void lbWindowData_Edit_Click(object sender, EventArgs e)
{
lbWindowData_DoubleClick(sender, e);
}
private void lbWindowData_Delete_Click(object sender, EventArgs e)
{
if (MyMode == LBContents.routes)
{
if (lbWindowData.SelectedIndex >= EditableCount) return;
if (lbWindowData.SelectedIndex < 1) return;
//we always subtract 1 since we have added one line of comment to the list
myNetDevice.DeleteRoute(lbWindowData.SelectedIndex - 1);
}
UpdateForm();
}
private void lbWindowData_RightMouseUp(object sender, MouseEventArgs e)
{
int index=0;
if(MyMode == LBContents.routes)
{
if (lbWindowData.ContextMenuStrip == null)
{
lbWindowData.ContextMenuStrip = new ContextMenuStrip();
}
lbWindowData.ContextMenuStrip.Items.Clear();
lbWindowData.ContextMenuStrip.Items.Add("Edit");
lbWindowData.ContextMenuStrip.Items[index++].Click += lbWindowData_Edit_Click;
lbWindowData.ContextMenuStrip.Items.Add("Delete");
lbWindowData.ContextMenuStrip.Items[index++].Click += lbWindowData_Delete_Click;
}
}
private void lbWindowData_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Right)
{
lbWindowData_RightMouseUp(sender, e);
return;
}
else
{
if(MyMode == LBContents.puzzles)
{
UpdateForm();
}
}
}
private void btnReset_Click(object sender, EventArgs e)
{
if (Properties.Settings.Default.ScoreList == null)
Properties.Settings.Default.ScoreList = new System.Collections.Specialized.StringCollection();
DialogResult answer = MessageBox.Show("Are sure you want to forget what puzzles you have done?", "Forget Puzzles?", MessageBoxButtons.YesNo);
if (answer == System.Windows.Forms.DialogResult.Yes)
{
Properties.Settings.Default.ScoreList.Clear();
Properties.Settings.Default.Save();
UpdateForm();
}
}
}
}

570
EduNetworkBuilder/NB.cs Normal file
View File

@ -0,0 +1,570 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Deployment.Application;
using System.Windows.Forms;
using System.Globalization;
using System.Resources;
using System.Xml;
using System.Drawing;
using System.Media;
using System.Reflection;
namespace EduNetworkBuilder
{
public enum PacketStatus {
processing, //It is inside a device. It needs to be routed, masqueraded, or whatever
input, //It is hitting the interface/nic of a device.
output, //It is leaving a device. Make sure the mac address is what it should be & that we are going out the right link
waiting_for_arp, //It is in the output chain, has been procesed, but is waiting for an arp packet to give it the dest MAC
moving, //It is traveling along a link. We increment the distance it has gone and re-draw the packet.
encapsulated, //It has been put inside another packet.
finished_ok,//The packet has finished and is ready to be removed from the network
finished_failed, //The packet is finished (failed) and is ready to be removed from the network.
finished //There was an appropriate reason this packet finished. It stopped
}
public enum LinkType { normal, wireless, broken }
public enum PacketType { none, ping_request, ping_answer, arp_request, arp_answer, dhcp_request, dhcp_answer, vpn_packet, tun_packet }
public enum ResponseToPacket { none, accept, masq, drop, reject }
public enum DebugLevel { none=0, info=1, routing=2, switching=4, natting=8, filtering=16, debug=32 , packet=64, all=127}
public enum NetworkComponentType { none, router, net_switch, net_hub, laptop, pc, server, wap ,
wrouter, wbridge, wrepeater, link, firewall, ip_phone, printer, copier }
public enum NicType { none, lo, eth, wlan, wan, vpn, tun, management_interface, port, wport }
public enum IPAddressType { ip, gw, route, ip_only }
public enum nb_direction { none, to_src, to_dst }
public enum GeneralComponentType { none, link, device }
public enum NBSoundType { none, success }
public enum RTFWindowContents { help, about, release_notes }
public enum NetTestType { NeedsLocalIPTo, NeedsDefaultGW, NeedsLinkToDevice, NeedsRouteToNet,
SuccessfullyPings, SuccessfullyArps, SuccessfullyDHCPs, HelpRequest, FailedPing,
LockAll, LockIP, LockRoute, LockNic, LockDHCP, LockGateway
}
public enum NetTestVerbosity { none, basic, hints, full }
public enum LBContents { routes, messages, dhcp, puzzles }
public enum HelpTopics {
DHCP, DHCPServer, Firewall, Gateway, Help, IPAddress, Link, Subnet, Ping,
VPN, Hub, Switch, ARP, StaticRoute, Subnetting, WhenToSubnet, ComparingAddresses, MACAddress,
Network, Packet, NIC, Interface
}
public enum PuzzleNames
{
Level0_IP, Level1_NoGateway, Level0_NeedsLink, Level0_NoSwitch, Level1_BadDHCP, Level1_BadGateway,
Level0_SimpleDHCP, Level1_BadIP, Level0_Help, Level0_Ping, Level0_HubVsSwitch, Level1_MixedNetwork,
Level1_MidDHCP, Level1_OneNetTwoSubnets, Level1_DuplicateIPs, Level0_NetworkLoop, Level1_DuplicateMAC,
Level2_FirewallDemo, Level1_OneNetTwoSubnets2, Level2_VPN_Demo, Level2_Bad_VPN_IP, Level2_Bad_Encryption,
Level2_Bad_Route, Level2_Blast_From_Past, Level2_Not_Working, Level2_Build_A_VPN, Level2_Connect_The_Dots,
Level3_BlackHole, Level3_Busted, Level3_Middle_Man_Out, Level3_PhoneyNetwork, Level3_VPNify, Level3_EncryptionTroubles,
Level3_NowhereToGo, Level3_GrandCentralStation, Level3_Dead, Level0_NetworkLoop2, Level0_BrokenLink,
Level4_DualWans, Level4_SinglesLife, Level4_SmallSubnets, Level4_OneRoute, Level4_RouterReplacement,
Level4_InternalSubnetting, Level4_Internalhemorrhage,
}
public enum DebugPausePoint { none=0, packet_create=1, packet_kill=2,
packet_in=4, packet_out=8, packet_duplicate=16, all=63,
dump=256, pause=512}
[Serializable]
public struct HostNicID //This holds a unique identifier for a network card. Links use this to know what it is connected to
{
public int HostID;
public int NicID;
public string HostName;
public string NicName;
public HostNicID(int host, int nic, string hostname, string nicname)
{
HostID = host;
NicID = nic;
HostName = hostname;
NicName = nicname;
}
public HostNicID(XmlNode theNode)
{
HostID = 0;
NicID = 0;
HostName = "";
NicName = "";
foreach (XmlNode Individual in theNode.ChildNodes)
{
XmlNodeType myNodetype = Individual.NodeType;
if (myNodetype == XmlNodeType.Element)
{
switch (Individual.Name.ToLower())
{
case "hostid":
int.TryParse(Individual.InnerText, out HostID);
break;
case "nicid":
int.TryParse(Individual.InnerText, out NicID);
break;
case "hostname":
HostName = Individual.InnerText;
break;
case "nicname":
NicName = Individual.InnerText;
break;
}
}
}
}
public void Save(XmlWriter writer, string tag)
{
writer.WriteStartElement(tag);
writer.WriteElementString("hostid", HostID.ToString());
writer.WriteElementString("nicid", NicID.ToString());
writer.WriteElementString("hostname", HostName);
writer.WriteElementString("nicname", NicName);
writer.WriteEndElement();
}
public string HostNicIDString
{
get { return HostID.ToString()+"-"+NicID.ToString(); }
}
public bool Equals(HostNicID toCheck)
{
if (HostID != toCheck.HostID) return false;
if (NicID != toCheck.NicID) return false;
if (HostName != toCheck.HostName) return false;
if (NicName != toCheck.NicName) return false;
return true;
}
}
[Serializable]
public struct IPConnectionEntry
{
public IPAddress destIP; //where we are sending the packet to. We expect a response to come from here
public PacketType What; //ping, dhcp, etc
public ResponseToPacket Response; //accept, masq
public IPAddress internalIP; //Used if we are masquerading
public IPConnectionEntry(IPAddress dest, PacketType what, ResponseToPacket response)
{
destIP = dest;
What = what;
Response = response;
internalIP = null;
}
public IPConnectionEntry(IPAddress dest, PacketType what, ResponseToPacket response, IPAddress internal_ip)
{
destIP = dest;
What = what;
Response = response;
internalIP = internal_ip;
}
}
[Serializable]
public struct ArpEntry
{
public string MACAddress;
public string IPAddr;
public HostNicID NicOnWhichItIsFound;
public ArpEntry(string mac, string ip, HostNicID thenic)
{
MACAddress = mac;
IPAddr = ip;
NicOnWhichItIsFound = thenic;
}
}
public class PuzzleInfo
{
public string PuzzleName;
public string PuzzleTitle;
public string PuzzleDescription;
public List<string> PuzzleTags = new List<string>();
public int Level=0;
public double SortOrder=0;
public void Load(XmlNode TheNode, string Name)
{
PuzzleName = Name;
foreach (XmlNode Individual in TheNode.ChildNodes)
{
XmlNodeType myNodetype = Individual.NodeType;
if (myNodetype == XmlNodeType.Element)
{
switch (Individual.Name.ToLower())
{
case "edunetworkbuilder":
case "network":
Load(Individual, PuzzleName);
break;
case "message":
PuzzleDescription = Individual.InnerText;
break;
case "title":
PuzzleTitle = Individual.InnerText;
break;
case "tag":
PuzzleTags.Add(Individual.InnerText);
break;
case "level":
PuzzleTags.Add("Level_" + Individual.InnerText);
int.TryParse(Individual.InnerText, out Level);
break;
case "sortorder":
double.TryParse(Individual.InnerText, out SortOrder);
break;
}
}
}
}
}
class NB
{
public static string BroadcastMACString = "FF:FF:FF:FF:FF:FF"; //The broadcast MAC address
public static string BroadcastIPString = "255.255.255.255"; //the generic broadcast IP address
public static string ZeroIPString = "0.0.0.0";
public static string LoopbackIPString = "127.0.0.1";
public static int LinkStep = 8;//The percentage of the link we move at each "tick"
public static int PacketPixelSize = 8; //The size of the packet pixel
public static int PacketVersionNum = 2; //2 uses the new stuff. Anything else uses the old stuff
public const int GridSize = 10;
public static string[,] LanguageChoices = { { "English", "en" }, { "French", "fr" } };
/// <summary>
/// Find the global random number generator.
/// </summary>
/// <returns>A valid random number generator</returns>
public static Random GetRandom()
{
BuilderWindow myWin = (BuilderWindow)Application.OpenForms["BuilderWindow"];
if (myWin == null) return new Random();
if (myWin.GameRandomGen == null) return new Random();
return myWin.GameRandomGen;
}
/// <summary>
/// Find the global random number generator.
/// </summary>
/// <returns>A valid random number generator</returns>
public static CultureInfo GetCulture()
{
BuilderWindow myWin = (BuilderWindow)Application.OpenForms["BuilderWindow"];
CultureInfo CI=null;
if (myWin != null)
{
CI = myWin.GetCulture();
}
if(CI == null || myWin == null)
{
string CL = Properties.Settings.Default.ChosenLanguage;
CI = CultureInfo.CreateSpecificCulture(CL);
}
return CI;
}
/// <summary>
/// Find the global random number generator.
/// </summary>
/// <returns>A valid random number generator</returns>
public static ResourceManager GetResource()
{
ResourceManager myresource = null;
BuilderWindow myWin = (BuilderWindow)Application.OpenForms["BuilderWindow"];
if (myWin != null)
{
myresource = myWin.GetResource();
}
if(myresource == null || myWin == null)
{
System.Reflection.Assembly MyAssembly;
MyAssembly = Assembly.GetCallingAssembly();
myresource = new ResourceManager("EduNetworkBuilder.Resources.languages.edustrings", MyAssembly);
}
return myresource;
}
public static Point GetSnapped(Point Location)
{
int x = (Location.X / NB.GridSize) * NB.GridSize;
int y = (Location.Y / NB.GridSize) * NB.GridSize;
Point newlocation = new Point(x, y);
return newlocation;
}
public static Network GetNetwork()
{
BuilderWindow myWin = (BuilderWindow)Application.OpenForms["BuilderWindow"];
if (myWin == null) return null;
if (myWin.GameRandomGen == null) return null;
return myWin.myNetwork;
}
/// <summary>
/// Return a translated string in the current chosen language
/// </summary>
/// <param name="key">The key for the item we are translating</param>
/// <returns>A string of the translated information</returns>
public static string Translate(string key)
{
BuilderWindow myWin = (BuilderWindow)Application.OpenForms["BuilderWindow"];
if (myWin == null)
{
ResourceManager RM = GetResource();
CultureInfo CI = GetCulture();
string answer;
answer = RM.GetString(key, CI);
return answer;
}
return myWin.Translate(key);
}
public static DebugPausePoint GetDebugPauseSetting()
{
BuilderWindow myWin = (BuilderWindow)Application.OpenForms["BuilderWindow"];
if (myWin == null) return DebugPausePoint.none;
if (myWin.GameRandomGen == null) return DebugPausePoint.none;
return myWin.DebugSetting;
}
public static PuzzleInfo GetPuzzleInfoFromName(string PuzzleName)
{
BuilderWindow myWin = (BuilderWindow)Application.OpenForms["BuilderWindow"];
if (myWin == null) return null;
return myWin.PuzzleInfoFromName(PuzzleName);
}
public static void ChangeLanguage()
{
//Find the window. If it exists, use /set the language setting there. If not, use / set the default.
BuilderWindow myWin = (BuilderWindow)Application.OpenForms["BuilderWindow"];
string lang = Properties.Settings.Default.ChosenLanguage;
if (lang == "") lang = "en";
string StartingItem = "";
for (int i = 0; i < LanguageChoices.GetLength(0); i++)
{
if (lang == LanguageChoices[i, 1])
{
StartingItem = LanguageChoices[i, 0];
}
}
//we need to choose a language:
Form LanguageForm = new Form();
LanguageForm.Text = "Choose Language";
Label lblText = new Label();
lblText.Location = new Point(5, 5);
lblText.AutoSize = true;
lblText.Text = "Choose a Language:";
LanguageForm.Icon = Properties.Resources.NBIco;
ComboBox cbQuestions = new ComboBox();
cbQuestions.Location = new Point(lblText.Location.X + lblText.Width + 10, lblText.Location.Y);
cbQuestions.Width = 60;
for (int i = 0; i < LanguageChoices.GetLength(0); i++ )
{
cbQuestions.Items.Add(LanguageChoices[i,0]);
}
if(cbQuestions.Items.Contains(StartingItem))
{
cbQuestions.SelectedItem = StartingItem;
}
else
cbQuestions.SelectedIndex = 0;
LanguageForm.Width = cbQuestions.Location.X + cbQuestions.Width + 70;
LanguageForm.Height = 90;
LanguageForm.AutoSize = true;
Button btnAccept = new Button();
btnAccept.Location = new Point(cbQuestions.Location.X, cbQuestions.Location.Y + cbQuestions.Height + 10);
btnAccept.Text = "OK";
btnAccept.Click += (s, g) => { Button b = (Button)s; Form f = (Form)b.Parent; f.Close(); };
LanguageForm.Controls.Add(lblText);
LanguageForm.Controls.Add(cbQuestions);
LanguageForm.Controls.Add(btnAccept);
LanguageForm.ShowDialog();
if (cbQuestions.SelectedIndex >= 0)
{
Properties.Settings.Default.LanguageHasBeenChosen = true;
string mychoice = LanguageChoices[cbQuestions.SelectedIndex, 1];
if (myWin == null)
{
Properties.Settings.Default.ChosenLanguage = mychoice;
//Properties.Settings.Default.Save(); //We do this when we exit. No need to save it right this instant.
}
else
{
myWin.ChosenLanguage = mychoice;
}
}
}
public static void PlaySound(NBSoundType What)
{
SoundPlayer sndClick;
switch (What)
{
case NBSoundType.none:
break;
case NBSoundType.success:
sndClick = new SoundPlayer(Properties.Resources.wavBellDing);
sndClick.Play();
break;
}
}
public static List<string> GetPuzzleTags()
{
BuilderWindow myWin = (BuilderWindow)Application.OpenForms["BuilderWindow"];
if (myWin == null) return null;
return myWin.GetPuzzleTags();
}
public static List<string> GetPuzzleNames()
{
BuilderWindow myWin = (BuilderWindow)Application.OpenForms["BuilderWindow"];
if (myWin == null) return null;
return myWin.GetPuzzleNames();
}
public static void LoadNetworkFromResource(string resource)
{
BuilderWindow myWin = (BuilderWindow)Application.OpenForms["BuilderWindow"];
if (myWin == null) return;
myWin.LoadNetworkFromResource(resource);
}
public static void SetProgress(double HowFar, double total)
{
BuilderWindow myWin = (BuilderWindow)Application.OpenForms["BuilderWindow"];
if (myWin == null) return;
myWin.SetProgress(HowFar, total);
}
public static int GetUniqueIdentifier()
{
Network myNet = GetNetwork();
if(myNet == null)
{
//We should never get here. If so, we are in a bad sort of way.
Random rnd = GetRandom();
return rnd.Next(1,100);
}
return myNet.GetUniqueIdentifier();
}
public static void Delay(int amount)
{
System.Threading.Thread.Sleep(amount);
}
public static T ParseEnum<T>(string value)
{
return (T)Enum.Parse(typeof(T), value, true);
}
public static GeneralComponentType GetComponentType(NetworkComponent What)
{
GeneralComponentType theType = GeneralComponentType.none;
if(What.GetType().ToString() == "EduNetworkBuilder.NetworkDevice")
theType = GeneralComponentType.device;
if (What.GetType().ToString() == "EduNetworkBuilder.NetworkLink")
theType = GeneralComponentType.link;
return theType;
}
public static string GenerateMACAddress()
{
var sBuilder = new StringBuilder();
var random = GetRandom();
int number;
byte b;
for (int i = 0; i < 6; i++)
{
number = random.Next(0, 255);
b = Convert.ToByte(number);
if (i == 0)
{
b = setBit(b, 6); //--> set locally administered
b = unsetBit(b, 7); // --> set unicast
}
sBuilder.Append(number.ToString("X2"));
}
string mac= sBuilder.ToString().ToUpper();
if (MAC_Exists(mac))
return GenerateMACAddress(); //If it already exists, generate another
return mac;
}
public static bool MAC_Exists(string MAC)
{
Network myNet = GetNetwork();
if(myNet == null) return false;
return myNet.MAC_Exists(MAC);
}
public static List<string> arp(UInt32 IP)
{
List<string> arps = new List<string>();
Network myNet = GetNetwork();
if (myNet == null) return arps;
return myNet.arp(IP);
}
private static byte setBit(byte b, int BitNumber)
{
if (BitNumber < 8 && BitNumber > -1)
{
return (byte)(b | (byte)(0x01 << BitNumber));
}
else
{
throw new InvalidOperationException(
"Invalid Bit " + BitNumber.ToString() + "! (Should be from 0 to 7)");
}
}
private static byte unsetBit(byte b, int BitNumber)
{
if (BitNumber < 8 && BitNumber > -1)
{
return (byte)(b | (byte)(0x00 << BitNumber));
}
else
{
throw new InvalidOperationException(
"Invalid Bit" + BitNumber.ToString() + "! (Should be from 0 to 7)");
}
}
/// <summary>
/// Randomize a list
/// </summary>
/// <typeparam name="T">The type of list to return</typeparam>
/// <param name="list">The list to randomize</param>
/// <returns>a new list that is randomized</returns>
public static List<T> Randomize<T>(List<T> list)
{
List<T> randomizedList = new List<T>(list);
Random rnd = GetRandom();
int n = randomizedList.Count;
int counter = 0;
T value;
while (n > 1)
{
n--;
int k = rnd.Next(n + 1);
value = randomizedList[k];
randomizedList[k] = randomizedList[n];
randomizedList[n] = value;
counter++;
if (counter > 10)
{
counter = 0;
}
}
return randomizedList;
}
}
}

View File

@ -0,0 +1,361 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Xml;
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;
}
public NetTest(NetTest FromWhat)
{
sHost = FromWhat.sHost;
dHost = FromWhat.dHost;
TheTest = FromWhat.TheTest;
}
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;
}
}
}
}
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:
toreturn = "Has a problem";
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;
}
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;
}
break;
case NetTestVerbosity.none:
toreturn = "";
break;
}
return toreturn;
}
public string GetDescription(NetTestVerbosity amount)
{
string toreturn = "";
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())
{
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.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;
}
}
}

View File

@ -0,0 +1,259 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.Globalization;
using System.Resources;
namespace EduNetworkBuilder
{
public partial class NetTestEditor : Form
{
NetTest ToEdit;
NetTest OrigTest;
List<string> HostNames = new List<string>();
List<string> Networks = new List<string>();
List<string> Broadcasts = new List<string>();
bool processing = false;
public NetTestEditor(NetTest WhatToEdit)
{
InitializeComponent();
LanguagifyComponents();
OrigTest = WhatToEdit;
ToEdit = new NetTest(OrigTest);
Network mynet = NB.GetNetwork();
HostNames.AddRange(mynet.GetHostnames());
Networks.AddRange(mynet.GetSubnets());
Broadcasts.AddRange(mynet.GetBroadcasts());
UpdateForm();
}
private void LanguagifyComponents()
{
ResourceManager RM = NB.GetResource();
CultureInfo CI = NB.GetCulture();
Text = NB.Translate("NTE_lblSource");
Text = NB.Translate("NTE_lblTest");
Text = NB.Translate("NTE_lblDest");
Text = NB.Translate("NTE_btnOK");
Text = NB.Translate("NTE_btnCancel");
Text = NB.Translate("NTE_Form");
}
private void btnOK_Click(object sender, EventArgs e)
{
OrigTest.UpdateValuesFromAnother(ToEdit);
Close();
}
private void btnCancel_Click(object sender, EventArgs e)
{
Close();
}
private void UpdateForm()
{
NetworkDevice sItem;
processing = true;
Network theNet = NB.GetNetwork();
//We add all the hostnames as source
cbSource.Items.Clear();
foreach(string host in HostNames)
{
cbSource.Items.Add(host);
}
//The tests are just the list of available tests.
cbTest.Items.Clear();
List<string> tList = new List<string>();
foreach (string test in Enum.GetNames(typeof(NetTestType)))
{
tList.Add(test);
}
tList.Sort(); //Sort them alphabetically. Just to make things easier
foreach (string test in tList)
{
cbTest.Items.Add(test);
}
cbDest.Items.Clear();
if (ToEdit.TheTest == NetTestType.NeedsRouteToNet)
{ //List all the networks
foreach (string subnet in Networks)
{
cbDest.Items.Add(subnet);
}
}
else if (ToEdit.TheTest == NetTestType.HelpRequest)
{
foreach (string subnet in Enum.GetNames(typeof(NetTestVerbosity)))
{
cbDest.Items.Add(subnet);
}
cbDest.Items.Add("?Button");
}
else if (ToEdit.TheTest == NetTestType.LockAll || ToEdit.TheTest == NetTestType.LockGateway)
{
cbDest.Items.Add("All");
}
else if (ToEdit.TheTest == NetTestType.LockDHCP)
{
//return all the dhcp ranges
sItem = theNet.GetDeviceFromName(ToEdit.sHost);
foreach(string ip in sItem.DHCPStrings(false))
{
cbDest.Items.Add(ip);
}
}
else if (ToEdit.TheTest == NetTestType.LockIP)
{
//return all the dhcp ranges
sItem = theNet.GetDeviceFromName(ToEdit.sHost);
foreach (IPAddress ip in sItem.IPAddressList())
{
cbDest.Items.Add(ip.GetIPString);
}
}
else if (ToEdit.TheTest == NetTestType.LockRoute)
{
//return all the dhcp ranges
sItem = theNet.GetDeviceFromName(ToEdit.sHost);
foreach (IPAddress ip in sItem.ListRoutes())
{
cbDest.Items.Add(ip.GetIPString);
}
}
else if (ToEdit.TheTest == NetTestType.LockNic)
{
//return all the dhcp ranges
sItem = theNet.GetDeviceFromName(ToEdit.sHost);
foreach (string nname in sItem.NICNames())
{
cbDest.Items.Add(nname);
}
}
else
{ //List all the hosts
foreach (string host in HostNames)
{
cbDest.Items.Add(host);
}
if (ToEdit.TheTest == NetTestType.SuccessfullyPings)
{ //List all the network broadcasts
foreach (string subnet in Broadcasts)
{
cbDest.Items.Add(subnet);
}
}
}
//Now we select all the appropriate items.
if(cbSource.Items.Contains(ToEdit.sHost))
{
cbSource.SelectedItem = ToEdit.sHost;
}
else
{
if(cbSource.Items.Count > 0)
cbSource.SelectedIndex = 0; //select the first item
}
if (cbDest.Items.Contains(ToEdit.dHost))
{
cbDest.SelectedItem = ToEdit.dHost;
}
else
{
if (cbDest.Items.Count > 0)
cbDest.SelectedIndex = 0; //select the first item
}
if (cbTest.Items.Contains(ToEdit.TheTest.ToString()))
{
cbTest.SelectedItem = ToEdit.TheTest.ToString();
}
else
{
if (cbTest.Items.Count > 0)
cbTest.SelectedIndex = 0; //select the first item
}
processing = false;
}
private void NetTestEditor_Load(object sender, EventArgs e)
{
}
private bool validate_choices()
{
if (processing) return true; //If we are processing, we are all OK.
Network theNet = NB.GetNetwork();
NetTestType ntt = NB.ParseEnum<NetTestType>(cbTest.SelectedItem.ToString());
if (cbSource.SelectedItem.ToString() == cbDest.SelectedItem.ToString() && ntt != NetTestType.HelpRequest)
return false; //Source cannot equal dest
if (theNet.GetDeviceFromName(cbSource.SelectedItem.ToString()) == null)
return false; //This should never happen with a drop-down list, but just in case...
if (ntt == NetTestType.LockAll || ntt == NetTestType.LockDHCP || ntt == NetTestType.LockIP ||
ntt == NetTestType.LockNic || ntt == NetTestType.LockRoute)
return true;
if(ntt == NetTestType.NeedsRouteToNet)
{
//We should have a network, not a host.
}
else if (ntt == NetTestType.HelpRequest)
{
//This uses a verbosity
}
else
{
if (ntt == NetTestType.SuccessfullyPings) return true;
if (theNet.GetDeviceFromName(cbDest.SelectedItem.ToString()) == null)
return false; //This should never happen with a drop-down list, but just in case...
}
return true;
}
private void cbTest_SelectedValueChanged(object sender, EventArgs e)
{
ToEdit.TheTest = NB.ParseEnum<NetTestType>(cbTest.SelectedItem.ToString());
if (!processing)
UpdateForm();
}
private void cbSource_SelectedValueChanged(object sender, EventArgs e)
{
if (validate_choices())
{
ToEdit.sHost = cbSource.SelectedItem.ToString();
}
else
if (!processing)
UpdateForm();
}
private void cbDest_SelectedValueChanged(object sender, EventArgs e)
{
if (validate_choices())
{
ToEdit.dHost = cbDest.SelectedItem.ToString();
}
else
if (!processing)
UpdateForm();
}
}
}

1111
EduNetworkBuilder/Network.cs Normal file

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,627 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
using System.IO;
using System.Xml;
namespace EduNetworkBuilder
{
[Serializable]
public class NetworkCard
{
public string MAC = NB.GenerateMACAddress(); //Technically we should make sure it is unique
List<NetworkInterface> interfaces = new List<NetworkInterface>();
public bool UsesDHCP = false;
public bool CanUseDHCP = false;
public bool MustUseDHCP = false;
private NicType myNicType = NicType.eth;
public HostNicID myID;
public int ConnectedLink=-1; //The link that is connected to this nic.
private int UniqueIdentifier = NB.GetUniqueIdentifier();
private string _nic_name="";
public IPAddress TunnelEndpoint;
public string EncryptionKey;
public NetworkCard(int index, int HostID, string hostname, NicType theType = NicType.eth)
{
myNicType = theType;
_nic_name = myNicType.ToString() + index.ToString();
NetworkInterface nInterface = new NetworkInterface(NicName(), NB.ZeroIPString, NB.ZeroIPString, myID);
if(theType == NicType.lo)
nInterface = new NetworkInterface(NicName(), "127.0.0.1", "255.0.0.0", myID);
myID = new HostNicID(HostID, UniqueIdentifier,hostname,nInterface.nic_name);
interfaces.Add(nInterface);
ApplyNicRules();
SetIPForDHCP();
}
public NetworkCard(XmlNode theNode)
{
foreach (XmlNode Individual in theNode.ChildNodes)
{
XmlNodeType myNodetype = Individual.NodeType;
if (myNodetype == XmlNodeType.Element)
{
switch (Individual.Name.ToLower())
{
case "nictype":
myNicType = NB.ParseEnum<NicType>(Individual.InnerText);
break;
case "nicname":
_nic_name = Individual.InnerText;
break;
case "myid":
myID = new HostNicID(Individual);
break;
case "usesdhcp":
bool.TryParse(Individual.InnerText, out UsesDHCP);
break;
case "uniqueidentifier":
int.TryParse(Individual.InnerText, out UniqueIdentifier);
break;
case "interface":
NetworkInterface newIF = new NetworkInterface(Individual,myID);
interfaces.Add(newIF);
break;
case "mac":
string tmac = Individual.InnerText;
if (tmac.Length == MAC.Length)
MAC = tmac.ToUpper(); //Make sure it is all uppercase
break;
case "tunnelendpoint":
TunnelEndpoint = new IPAddress(Individual);
break;
case "encryptionkey":
EncryptionKey = Individual.InnerText;
break;
}
}
}
ApplyNicRules();
SetIPForDHCP();
}
private void ApplyNicRules()
{
//These are the defaults for the various nic types. They can be overridden by the device.
if(myNicType == NicType.eth || myNicType == NicType.management_interface || myNicType == NicType.wlan)
{
CanUseDHCP = true;
}
else
{
CanUseDHCP = false;
}
}
public void Save(XmlWriter writer)
{
writer.WriteStartElement("nic");
writer.WriteElementString("nictype", myNicType.ToString());
writer.WriteElementString("nicname", _nic_name);
myID.Save(writer, "myid");
writer.WriteElementString("nictype", myNicType.ToString());
writer.WriteElementString("uniqueidentifier", UniqueIdentifier.ToString());
writer.WriteElementString("usesdhcp", UsesDHCP.ToString());
if(EncryptionKey != "")
writer.WriteElementString("encryptionkey", EncryptionKey);
if (TunnelEndpoint != null)
{
TunnelEndpoint.Save(writer, "tunnelendpoint");
}
foreach (NetworkInterface nIF in interfaces)
{
nIF.Save(writer);
}
writer.WriteEndElement();
}
public int GetUniqueIdentifier
{
get { return UniqueIdentifier; }
}
public NicType GetNicType
{
get { return myNicType; }
}
public bool HasIP(UInt32 IP)
{
if (myNicType == NicType.port) return false;
if (myNicType == NicType.none) return false;
foreach (NetworkInterface IF in interfaces)
{
if (IF.myIP.Equals(IP))
return true;
}
return false;
}
public string NicName()
{
return _nic_name;
}
public string NicString(int index)
{
string connected = " ";
if (isConnected(true)) connected = "*";
if (myNicType == NicType.port)
return NicName() + connected;
return NicName() + connected + " " + MAC;
}
public List<string> NICRouteStrings(string GW)
{
List<string> thestrings = new List<string>();
if (myNicType == NicType.port) return thestrings;
if (myNicType == NicType.none) return thestrings;
foreach (NetworkInterface iface in interfaces)
{
thestrings.Add(iface.RouteString(_nic_name,GW));
}
return thestrings;
}
public List<string> IPAddresses(bool UseCidr = false)
{
List<string> theIPs = new List<string>();
if (myNicType == NicType.port) return theIPs;
if (myNicType == NicType.none) return theIPs;
string DHCPString = "";
if (UsesDHCP && CanUseDHCP) DHCPString = "DHCP: ";
foreach (NetworkInterface iface in interfaces)
{
theIPs.Add(DHCPString + iface.InterfaceString(UseCidr));
}
return theIPs;
}
public List<IPAddress> IPAddressList()
{
List<IPAddress> theIPs = new List<IPAddress>();
if (myNicType == NicType.port) return theIPs;
if (myNicType == NicType.none) return theIPs;
foreach (NetworkInterface iface in interfaces)
{
theIPs.Add(iface.myIP);
}
return theIPs;
}
public bool HasIPAddresses(IPAddress dest)
{
if (myNicType == NicType.port) return false;
if (myNicType == NicType.none) return false;
if (dest == null) return false;
foreach (NetworkInterface iface in interfaces)
{
if (iface.myIP.GetIP == dest.GetIP)
return true;
}
return false;
}
public bool HasBroadcastAddresses(IPAddress dest)
{
if (myNicType == NicType.port) return false;
if (myNicType == NicType.none) return false;
if (dest == null) return false;
foreach (NetworkInterface iface in interfaces)
{
if (iface.myIP.BroadcastAddress == dest.GetIP)
return true;//If they are pinging the broadcast IP
}
return false;
}
public void EditInterface(int index)
{
if (index < 0 || index > interfaces.Count())
return;
interfaces[index].EditAddress();
}
public void DeleteInterface(int index)
{
if (index < 0 || index > interfaces.Count())
return;
if (interfaces.Count < 2)
return; //We cannot delete the sole remaining interface
interfaces.RemoveAt(index);
}
public void AddInterface()
{
NetworkInterface iface = new NetworkInterface(NicName(), NB.ZeroIPString, NB.ZeroIPString, myID);
interfaces.Add(iface);
}
public NetworkInterface GetInterface(int index)
{
if (index < 0 || index > interfaces.Count())
return null;
return interfaces[index];
}
public bool isConnected(bool CheckForBrokenLink)
{
if(ConnectedLink != -1)
{
if (!CheckForBrokenLink) return true; //We only care if there is any link
Network myNet = NB.GetNetwork();
NetworkLink NL = myNet.GetLinkFromID(ConnectedLink);
if(NL != null)
{
if (NL.theLinkType != LinkType.broken) return true;
else return false;
}
}
return false;
}
public void Edit()
{
NetworkCardEditor nce = new NetworkCardEditor(this);
nce.ShowDialog();
//Now. update the interfaces if we need to do so.
SetIPForDHCP();
}
public void SetIPForDHCP()
{
if (UsesDHCP && CanUseDHCP)
{
//remove any extra interfaces.
//set the one interface to 0.0.0.0
switch(myNicType)
{
case NicType.eth:
case NicType.wlan:
interfaces.Clear(); //empty out all interfaces
//Give it an interface with an empty IP
NetworkInterface nInterface = new NetworkInterface(NicName(), NB.ZeroIPString, NB.ZeroIPString,myID);
interfaces.Add(nInterface);
break;
}
}
}
public void SetIPForDHCP(IPAddress newIP)
{
if (UsesDHCP && CanUseDHCP)
{
if(interfaces.Count > 0)
interfaces[0].myIP = newIP;
}
}
public bool HasLocalInterface(IPAddress theIP)
{
foreach(NetworkInterface nIF in interfaces)
{
if (nIF.isLocal(theIP))
return true;
}
return false;
}
/// <summary>
/// Return the interface that is considered "local" to the IP address we are trying to reach
/// </summary>
/// <param name="theIP">An IP address we are trying to send out</param>
/// <returns>null if no interface is local. Otherwise, it returns the one that matches the packet</returns>
public NetworkInterface LocalInterface(IPAddress theIP, PacketMessage Tracker)
{
if (myNicType == NicType.port) return null; //ports have no local interfaces
foreach (NetworkInterface nIF in interfaces)
{
if (nIF.isLocal(theIP))
{
if(Tracker != null)
Tracker.AddMessage(DebugLevel.routing, myID.HostName, "Found local interface: ip" + nIF.myIP.GetIP.ToIpString() +
" gw:" + nIF.myIP.GetMask.ToIpString());
return nIF;
}
}
return null;
}
/***************************************
*
* *************************************/
public bool SendPacketOutNIC(Packet tPacket)
{
bool madeprogress = false;
Packet nPacket = null;
Network myNet = NB.GetNetwork();
NetworkLink nl;
if (NB.GetComponentType(tPacket.WhereAmI) != GeneralComponentType.device) return false; //we cannot do this.
NetworkDevice WhereFrom = (NetworkDevice)tPacket.WhereAmI;
switch (GetNicType)
{
case NicType.lo:
case NicType.management_interface:
case NicType.none:
break; //Do nothing
case NicType.eth:
//see if it the packet dest is local to this nic
foreach (NetworkInterface nf in interfaces.ToList())
{
if (tPacket.MyType == PacketType.arp_request && !nf.isLocal(tPacket.destIP))
continue; //only send out arp requests on local networks
nPacket = new Packet(tPacket);//Creates a new packet but sets isfresh=false
if (nf.isLocal(tPacket.OutboundIP) || tPacket.OutboundIP.GetIPString == NB.BroadcastIPString)
{
if ((nf != null && nf.myIP.GetIPString != NB.ZeroIPString) || nPacket.MyType == PacketType.dhcp_request)
{
//this means we have a local interface to send it out of
nPacket.sourceMAC = MAC;
//If the source IP is empty then it is a new packet. We set the source to be us
if (nPacket.sourceIP == null || nPacket.sourceIP.GetIPString == NB.ZeroIPString)
nPacket.sourceIP = nf.myIP;
nPacket.TsourceIP = nf.myIP;
if (nPacket.destMAC == null || nPacket.destMAC == "")
{
nPacket.destMAC = WhereFrom.LookupArpFromIP(tPacket.OutboundIP.GetIPString);
if (nPacket.MyType == PacketType.arp_request)
{
nPacket.destMAC = NB.BroadcastMACString;
}
if(nPacket.destMAC == "")
{
nPacket.AddMessage(DebugLevel.debug, " No Machine matching that IP address on this subnet. " + nPacket.destIP.GetIPString);
Network mynet = NB.GetNetwork();
NetworkDevice nd = mynet.GetDeviceFromID(myID);
string hostname = "No Host";
if (nd != null) hostname = nd.hostname;
nPacket.Tracking.Status = hostname + " No Machine matching that IP address on this subnet. " + nPacket.destIP.GetIPString;
nPacket.MyStatus = PacketStatus.finished_failed;
return false;
}
}
nl = myNet.GetLinkFromID(ConnectedLink);
nPacket.StartOnLink(nl, WhereFrom); //This sends the packet down the link.
//Store outbound information here - som we expect the returning packet
WhereFrom.StoreOutgoingPacketInfo(nPacket);
myNet.addPacket(nPacket);
nPacket.PacketDump(myID.HostName + "-" + _nic_name, DebugPausePoint.packet_out);
madeprogress = true;
}
}
}
break;
case NicType.wan:
//see if it the packet dest is local to this nic
if (tPacket.MyType == PacketType.dhcp_answer && tPacket.isFresh)
break; //We do not broadcast out the WAN port
foreach (NetworkInterface nf in interfaces.ToList())
{
nPacket = new Packet(tPacket);//Creates a new packet but sets isfresh=false
if (nf.isLocal(tPacket.OutboundIP) || tPacket.OutboundIP.GetIPString == NB.BroadcastIPString)
{
if ((nf != null && nf.myIP != null && nf.myIP.GetIPString != NB.ZeroIPString) || nPacket.MyType == PacketType.dhcp_request)
{
//this means we have a local interface to send it out of
nPacket.sourceMAC = MAC;
//If the source IP is empty then it originated from here. We set the source to be us
if (nPacket.sourceIP == null || nPacket.sourceIP.GetIPString == NB.ZeroIPString)
{
nPacket.sourceIP = nf.myIP;
WhereFrom.StoreOutgoingPacketInfo(nPacket); //the packet is not masqueraded, just accepted
}
else
{
//When we leave the WAN port, we are masqueraded. Track that.
WhereFrom.StoreOutgoingPacketInfo(nPacket, ResponseToPacket.masq);
//Now, we masquerade the packet so it looks like it comes fromhere
nPacket.Tracking.AddMessage(DebugLevel.natting, WhereFrom.hostname, "MASQ: Changing outbound IP to: " + nf.myIP.GetIPString);
nPacket.sourceIP = nf.myIP;
}
nPacket.TsourceIP = nf.myIP;
if (nPacket.destMAC == null || nPacket.destMAC == "")
{
nPacket.destMAC = WhereFrom.LookupArpFromIP(tPacket.OutboundIP.GetIPString);
if (nPacket.destMAC == "")
{
nPacket.AddMessage(DebugLevel.debug, " No Machine matching that IP address on this subnet. " + nPacket.destIP.GetIPString);
Network mynet = NB.GetNetwork();
NetworkDevice nd = mynet.GetDeviceFromID(myID);
string hostname = "No Host";
if (nd != null) hostname = nd.hostname;
nPacket.Tracking.Status = hostname + " No Machine matching that IP address on this subnet. " + nPacket.destIP.GetIPString;
nPacket.MyStatus = PacketStatus.finished_failed;
return false;
}
}
nl = myNet.GetLinkFromID(ConnectedLink);
nPacket.StartOnLink(nl, WhereFrom); //This sends the packet down the link.
//Store outbound information here - som we expect the returning packet
WhereFrom.StoreOutgoingPacketInfo(nPacket);
myNet.addPacket(nPacket);
nPacket.PacketDump(myID.HostName + "-" + _nic_name, DebugPausePoint.packet_out);
madeprogress = true;
}
}
}
break;
case NicType.tun:
case NicType.vpn:
foreach (NetworkInterface nf in interfaces.ToList())
{
if (nf.isLocal(tPacket.OutboundIP))
{
//We need to tell the original packet that it is inside another packet
tPacket.MyStatus = PacketStatus.encapsulated;
tPacket.TsourceIP = nf.myIP;
tPacket.destMAC = WhereFrom.LookupArpFromIP(tPacket.OutboundIP.GetIPString);
//We need to make a new, tunnel packet
if (myNicType == NicType.tun)
EncryptionKey = "";
Packet rnPacket = new Packet(tPacket);
WhereFrom.TunnelPacketFromHere(TunnelEndpoint, rnPacket, EncryptionKey);
//We need to send the new packet on (pass it back to the device to process)
madeprogress = true;
}
}
break;
case NicType.port:
nPacket = new Packet(tPacket);
if (HasBroadcastAddresses(tPacket.destIP))
{
//Broadcast packets will go to everything and we want a response from all of them.
nPacket.Tracking = PacketMessage.Clone(tPacket.Tracking);
}
if (nPacket.sourceMAC == null || nPacket.sourceMAC == "")
{
//Need to find the managament interface MAC
nPacket.sourceMAC = WhereFrom.HubManagementMAC();
}
if(nPacket.sourceIP == null || nPacket.sourceIP.GetIPString == NB.ZeroIPString)
{
//set it to be the ip of management interface
nPacket.sourceIP = WhereFrom.HubManagementIP();
}
if (nPacket.destMAC == null || nPacket.destMAC == "")
{
nPacket.destMAC = WhereFrom.LookupArpFromIP(tPacket.OutboundIP.GetIPString);
}
if(nPacket.TsourceIP == null)
nPacket.TsourceIP = WhereFrom.HubManagementIP();
nl = myNet.GetLinkFromID(ConnectedLink);
if (nl == null) break;
nPacket.StartOnLink(nl, WhereFrom); //This sends the packet down the link.
myNet.addPacket(nPacket);
if(tPacket.isFresh)
WhereFrom.StoreOutgoingPacketInfo(nPacket); //if it originated from here...
madeprogress = true;
nPacket.PacketDump(myID.HostName, DebugPausePoint.packet_out);
break;
}
return madeprogress;
}
//********************Process Packet ********
public void ProcessOutboundPacket(Packet tPacket)
{
//We set the MAC addrss to this nic
tPacket.sourceMAC = MAC;
//If the nic has a special function, we need to do that too.
// VPN, etc
}
public void ProcessInboundPacket(Packet tPacket)
{
Network mynet;
NetworkDevice nd;
//We make sure the MAC matches.
if (myNicType == NicType.port)
{
//Try tracking the arp if we can
mynet = NB.GetNetwork();
nd = mynet.GetDeviceFromID(myID);
if (tPacket.TsourceIP == null) tPacket.TsourceIP = tPacket.sourceIP;
nd.StoreArp(tPacket.sourceMAC, tPacket.TsourceIP.GetIP.ToIpString(), myID);
//If it is a return DHCP packet. We should try to update the MAC
if (tPacket.MyType == PacketType.dhcp_answer)
{
HostNicID otherid = nd.NicIDFromArp(tPacket.destMAC);
if (otherid.HostID != -1)
nd.StoreArp(tPacket.destMAC, tPacket.payloadIP.GetIP.ToIpString(), otherid);
}
return;
}
if(myNicType == NicType.wan)
{
mynet = NB.GetNetwork();
nd = mynet.GetDeviceFromID(myID);
if(nd.HowToRespondToPacket(tPacket) == ResponseToPacket.masq)
{
IPAddress oAddress = nd.PacketMasqueradeSource(tPacket);
if(oAddress != null)
{
tPacket.Tracking.AddMessage(DebugLevel.natting, nd.hostname, "MASQ: Changing source IP back to: " + oAddress.GetIPString);
tPacket.destIP = oAddress;
}
}
else if(!HasIP(tPacket.destIP.GetIP))
{
tPacket.AddMessage(DebugLevel.routing, "The packet was rejected by the firewall.");
tPacket.AddMessage(DebugLevel.debug, " The packet was not expected by the firewall, so it was rejected.");
mynet = NB.GetNetwork();
nd = mynet.GetDeviceFromID(myID);
string hostname = "No Host";
if (nd != null) hostname = nd.hostname;
tPacket.Tracking.Status = hostname + " The packet was rejected by the firewall.. Dropped.";
tPacket.MyStatus = PacketStatus.finished_failed;
}
}
if(tPacket.destMAC == MAC || tPacket.destMAC == NB.BroadcastMACString || myNicType == NicType.port)
{
//It matches. We are ok. Anything to do?
//If the NIC is a vpn, do that here.
}
else
{
tPacket.AddMessage(DebugLevel.routing,"The Packet was destined for a different machine (MAC Address): Rejected");
tPacket.AddMessage(DebugLevel.debug, " Device MAC: " + MAC + " did not match packet: " + tPacket.destMAC);
mynet = NB.GetNetwork();
nd = mynet.GetDeviceFromID(myID);
string hostname = "No Host";
if (nd != null) hostname = nd.hostname;
tPacket.Tracking.Status = hostname + " Packet destined for another machine. Dropped.";
tPacket.MyStatus = PacketStatus.finished_failed;
}
}
public void ClearIPs()
{
foreach(NetworkInterface nf in interfaces)
{
if(myNicType != NicType.lo)
nf.myIP = new IPAddress(NB.ZeroIPString);
}
}
public IPAddress FirstIP()
{
List<IPAddress> addresses = IPAddressList();
if (addresses.Count > 0)
return addresses[0];
else
return new IPAddress(NB.ZeroIPString);
}
public static T Clone<T>(T source)
{
if (!typeof(T).IsSerializable)
{
throw new ArgumentException("The type must be serializable.", "source");
}
// Don't serialize a null object, simply return the default for that object
if (Object.ReferenceEquals(source, null))
{
return default(T);
}
IFormatter formatter = new BinaryFormatter();
Stream stream = new MemoryStream();
using (stream)
{
formatter.Serialize(stream, source);
stream.Seek(0, SeekOrigin.Begin);
return (T)formatter.Deserialize(stream);
}
}
}
}

View File

@ -0,0 +1,161 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Globalization;
using System.Resources;
namespace EduNetworkBuilder
{
public partial class NetworkCardEditor : Form
{
NetworkCard MyNicToEdit;
Button btnVPNEnd = null;
TextBox tbVPNEncrypt = null;
Label lblVPNEnd = null;
Label lblVPNEncrypt = null;
private NetworkCardEditor()
{
InitializeComponent();
LanguagifyComponents();
}
public NetworkCardEditor(NetworkCard NicToEdit)
{
InitializeComponent();
LanguagifyComponents();
if (NicToEdit.GetNicType == NicType.tun || NicToEdit.GetNicType == NicType.vpn)
{
AutoSize = false;
btnVPNEnd = new Button();
btnVPNEnd.Location = new Point(TBMacAddress.Location.X, cbDHCP.Location.Y + 28);
string btnval = "0.0.0.0";
if (NicToEdit.TunnelEndpoint != null)
btnval = NicToEdit.TunnelEndpoint.GetIPString;
btnVPNEnd.Text = btnval;
btnVPNEnd.Click += btnVPNEnd_Click;
lblVPNEnd = new Label();
lblVPNEnd.Location = new Point(10, btnVPNEnd.Location.Y);
lblVPNEnd.Text = "VPN Endpoint:";
Controls.Add(btnVPNEnd);
Controls.Add(lblVPNEnd);
this.Height = lblVPNEnd.Location.Y + lblVPNEnd.Height + 80;
if (NicToEdit.GetNicType == NicType.vpn)
{
tbVPNEncrypt = new TextBox();
tbVPNEncrypt.Location = new Point(TBMacAddress.Location.X, btnVPNEnd.Location.Y + 28);
lblVPNEncrypt = new Label();
lblVPNEncrypt.Location = new Point(10, tbVPNEncrypt.Location.Y);
lblVPNEncrypt.Text = "Encryption Key:";
Controls.Add(tbVPNEncrypt);
Controls.Add(lblVPNEncrypt);
this.Height = tbVPNEncrypt.Location.Y + tbVPNEncrypt.Height + 80;
}
Refresh();
//AutoSize = true;
}
TBMacAddress.Enabled = false;
tbNicName.Enabled = false;
tbNicType.Enabled = false;
cbCanUseDHCP.Enabled = false;
TBMacAddress.Text = NicToEdit.MAC;
tbNicName.Text = NicToEdit.NicName();
tbNicType.Text = NicToEdit.GetNicType.ToString();
MyNicToEdit = NicToEdit;
cbCanUseDHCP.Checked = NicToEdit.CanUseDHCP;
cbDHCP.Checked = NicToEdit.UsesDHCP;
if (!NicToEdit.CanUseDHCP) cbDHCP.Enabled = false;
if(NicToEdit.MustUseDHCP)
{
cbDHCP.Enabled = false;
cbCanUseDHCP.Enabled = false;
}
Network theNet = NB.GetNetwork();
NetworkDevice nd = theNet.GetDeviceFromID(NicToEdit.myID);
if(theNet.ItemIsLocked(nd.hostname, NicToEdit.NicName(), NetTestType.LockNic))
{
cbDHCP.Enabled = false;
cbCanUseDHCP.Enabled = false;
TBMacAddress.Enabled = false;
tbNicName.Enabled = false;
tbNicType.Enabled = false;
}
UpdateForm();
}
private void LanguagifyComponents()
{
ResourceManager RM = NB.GetResource();
CultureInfo CI = NB.GetCulture();
Text = NB.Translate("NCE_cbCanUseDHCP");
Text = NB.Translate("NCE_cbDHCP");
Text = NB.Translate("NCE_label1");
Text = NB.Translate("NCE_label2");
Text = NB.Translate("NCE_label3");
Text = NB.Translate("NCE_btnDone");
Text = NB.Translate("NCE_lblLinkStatus");
Text = NB.Translate("NCE_Form");
}
private void UpdateForm()
{
if (btnVPNEnd != null)
{
string btnval = "0.0.0.0";
if (MyNicToEdit.TunnelEndpoint != null)
btnval = MyNicToEdit.TunnelEndpoint.GetIPString;
btnVPNEnd.Text = btnval;
}
if(tbVPNEncrypt != null)
{
tbVPNEncrypt.Text = MyNicToEdit.EncryptionKey;
}
if (MyNicToEdit.GetNicType == NicType.tun || MyNicToEdit.GetNicType == NicType.vpn ||
MyNicToEdit.GetNicType == NicType.management_interface || MyNicToEdit.GetNicType == NicType.lo)
{
lblLinkStatus.Visible = false;
}
else
lblLinkStatus.Visible = true;
if (MyNicToEdit.isConnected(true))
lblLinkStatus.Text = "Connected";
else
lblLinkStatus.Text = "Disconnected";
}
private void btnDone_Click(object sender, EventArgs e)
{
MyNicToEdit.CanUseDHCP = cbCanUseDHCP.Checked;
MyNicToEdit.UsesDHCP = cbDHCP.Checked;
if (tbVPNEncrypt != null)
{
MyNicToEdit.EncryptionKey = tbVPNEncrypt.Text;
}
Close();
}
private void btnVPNEnd_Click(object sender, EventArgs e)
{
Network theNet = NB.GetNetwork();
NetworkDevice nd = theNet.GetDeviceFromID(MyNicToEdit.myID);
if(MyNicToEdit.TunnelEndpoint == null)
{
MyNicToEdit.TunnelEndpoint = new IPAddress(NB.ZeroIPString, "255.255.255.0", IPAddressType.ip_only);
}
MyNicToEdit.TunnelEndpoint.Edit(nd,"Endpoint");
UpdateForm();
}
}
}

View File

@ -0,0 +1,120 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
</root>

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,224 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Xml;
namespace EduNetworkBuilder
{
[Serializable]
public class NetworkLink : NetworkComponent
{
HostNicID SrcNic;
HostNicID DstNic;
public LinkType theLinkType = LinkType.normal;
public bool isVisibleLink = true; //False for wireless. Skip drawing a line if it is there
public NetworkLink(HostNicID source, HostNicID dest, LinkType type = LinkType.normal)
{
SrcNic = source;
DstNic = dest;
Network myNet = NB.GetNetwork();
myNet.MarkAsLinked(source, GetUniqueIdentifier);
myNet.MarkAsLinked(dest, GetUniqueIdentifier);
theLinkType = type;
}
public NetworkLink(XmlNode theNode)
{
IsDirty = true;
Load(theNode);
}
public HostNicID Src
{
get { return SrcNic; }
}
public HostNicID Dst
{
get { return DstNic; }
}
public override void Load(XmlNode theNode)
{
foreach (XmlNode Individual in theNode.ChildNodes)
{
XmlNodeType myNodetype = Individual.NodeType;
if (myNodetype == XmlNodeType.Element)
{
switch (Individual.Name.ToLower())
{
case "srcnic":
SrcNic = new HostNicID(Individual);
break;
case "dstnic":
DstNic = new HostNicID(Individual);
break;
case "hostname":
hostname = Individual.InnerText;
break;
case "linktype":
theLinkType = NB.ParseEnum<LinkType>(Individual.InnerText);
break;
case "uniqueidentifier":
int.TryParse(Individual.InnerText,out UniqueIdentifier);
break;
}
}
}
Network myNet = NB.GetNetwork();
myNet.MarkAsLinked(SrcNic, GetUniqueIdentifier);
myNet.MarkAsLinked(DstNic, GetUniqueIdentifier);
}
public override void Save(XmlWriter writer)
{
writer.WriteStartElement("link");
SrcNic.Save(writer,"SrcNic");
DstNic.Save(writer,"DstNic");
writer.WriteElementString("hostname", hostname);
writer.WriteElementString("linktype", theLinkType.ToString());
writer.WriteElementString("uniqueidentifier", UniqueIdentifier.ToString());
writer.WriteEndElement();
}
public override void Destroy()
{
Network myNet = NB.GetNetwork();
if (myNet != null)
{
myNet.MarkAsUnlinked(SrcNic, GetUniqueIdentifier);
myNet.MarkAsUnlinked(DstNic, GetUniqueIdentifier);
}
}
public bool IsSource(int ID)
{
if (SrcNic.HostID == ID)
return true;
else return false;
}
public bool IsDest(int ID)
{
if (DstNic.HostID == ID)
return true;
else return false;
}
//Have func to verify both ends are powered on
public bool isLive()
{
return false;
}
/// <summary>
/// Check to see if the link is connected to the specified nic at either end
/// </summary>
/// <param name="toFind">The unique identifier of the link</param>
/// <returns>True if it has it</returns>
public bool HasLink(HostNicID toFind)
{
if (SrcNic.Equals(toFind) || DstNic.Equals(toFind))
return true;
return false;
}
public override void Print(Image BaseImage, bool DrawTitle)
{
//Find the XY of the connected items
Network myNet = NB.GetNetwork();
NetworkDevice Src = myNet.HostMatchingHostNicID(SrcNic);
NetworkDevice Dst = myNet.HostMatchingHostNicID(DstNic);
//Draw a line between them
if (Src == null || Dst == null) return;
Point sPoint = Src.GetCenter();
Point dPoint = Dst.GetCenter();
Pen tPen = new Pen(Color.Black, 4);
Graphics.FromImage(BaseImage).DrawLine(tPen,sPoint,dPoint);
IsDirty = false; //we have printed, we are no longer dirty.
}
public Point PositionOnLine(nb_direction direction, int Percentage)
{
Network myNet = NB.GetNetwork();
if(Percentage < 0) Percentage =0;
if(Percentage > 100) Percentage = 100;
NetworkDevice Src = myNet.HostMatchingHostNicID(SrcNic);
NetworkDevice Dst = myNet.HostMatchingHostNicID(DstNic);
//Draw a line between them
if (Src == null || Dst == null)
return new Point(-1,-1);
Point sPoint = Src.GetCenter();
Point dPoint = Dst.GetCenter();
double deltax = (sPoint.X - dPoint.X) / 100.0;
double deltay = (sPoint.Y - dPoint.Y) / 100.0;
//wearehere; //We need to start at an end. (we need to add sPoint.x, spoint.y depending on which dir we are going)
Point answer;
if(direction == nb_direction.to_dst)
answer = new Point(sPoint.X-(int)(deltax * Percentage), sPoint.Y-(int)(deltay * Percentage));
else
answer = new Point((int)(deltax * Percentage) + dPoint.X, (int)(deltay * Percentage) + dPoint.Y);
return answer;
}
public List<string> UsedNicIDStrings()
{
List<string> usedLinks = new List<string>();
//If the nic is a wireless end point, we should not count it. We can have multiple Nics linked to one AP
usedLinks.Add(SrcNic.HostNicIDString);
usedLinks.Add(DstNic.HostNicIDString);
return usedLinks;
}
/// <summary>
/// Have the packet traverse the network
/// </summary>
/// <param name="tPacket"></param>
public override void DoMoving(Packet tPacket)
{
NetworkDevice movingTo = null;
Network myNet = NB.GetNetwork();
HostNicID target;
tPacket.IncrementDistance();
if(theLinkType == LinkType.broken && tPacket.myLinkPercent > 50)
{
//The link is broken. Drop the packet
tPacket.Tracking.AddMessage(DebugLevel.info, this, "The packet tried to use a broken network wire and was corrupted.");
tPacket.Tracking.Status = "The packet got corrupted and was dropped.";
tPacket.MyStatus = PacketStatus.finished_failed;
}
if(tPacket.Arrived())
{
//We need to pass it to the recieving device
if (tPacket.myDirection == nb_direction.to_dst)
{
movingTo= myNet.GetDeviceFromID(DstNic.HostID);
target = DstNic;
}
else
{
movingTo = myNet.GetDeviceFromID(SrcNic.HostID);
target = SrcNic;
}
if(movingTo == null)
{
tPacket.AddMessage(DebugLevel.info, "Oops! We do not have anything at the end of the network wire. This should not happen!");
tPacket.Tracking.Status = hostname + " Nothing at far end of the wire. This should never happen..";
tPacket.MyStatus = PacketStatus.finished_failed;
}
else
{
movingTo.DoInputFromLink(tPacket, target);
}
}
}
}
}

View File

@ -0,0 +1,347 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Globalization;
using System.Resources;
namespace EduNetworkBuilder
{
public partial class OptionsWindow : Form
{
private Network myNet;
private ToolTip myToolTip = new ToolTip();
Form QuestionForm = new Form();
private OptionsWindow()
{
InitializeComponent();
}
public OptionsWindow(Network theNet)
{
myNet = theNet;
InitializeComponent();
myToolTip.SetToolTip(tbItemSize, "The size of an item (switch, PC, etc). Default is 100.");
myToolTip.SetToolTip(lblItemSize, "The size of an item (switch, PC, etc). Default is 100.");
myToolTip.SetToolTip(tbMessage, "The message given when the network is loaded. Instructions.");
myToolTip.SetToolTip(lblNetMessage, "The message given when the network is loaded. Instructions.");
myToolTip.SetToolTip(tbNetworkTitle, "The title of the network window.");
myToolTip.SetToolTip(lblNetTitle, "The title of the network window.");
myToolTip.SetToolTip(lblNetSize, "The height and width of the network graphics area.");
myToolTip.SetToolTip(tbNetworkX, "The width of the network window graphics area.");
myToolTip.SetToolTip(tbNetworkY, "The Height of the network window graphics area.");
myToolTip.SetToolTip(lbTags, "The Tags that this puzzle is saved as (only for puzzles)");
myToolTip.SetToolTip(lblTags, "The Tags that this puzzle is saved as (only for puzzles)");
myToolTip.SetToolTip(lbTests, "The things that need to be solved before the puzzle is completed.");
myToolTip.SetToolTip(lblTests, "The things that need to be solved before the puzzle is completed.");
myToolTip.SetToolTip(lblLevel, "The Level that this puzzle is stored in.");
myToolTip.SetToolTip(tbLevel, "The Level that this puzzle is stored in.");
myToolTip.SetToolTip(lblSortOrder, "How this is sorted within the list of puzzles.");
myToolTip.SetToolTip(tbSortOrder, "How this is sorted within the list of puzzles.");
LoadValuesFromNetwork();
}
private void LanguagifyComponents()
{
ResourceManager RM = NB.GetResource();
CultureInfo CI = NB.GetCulture();
Text = NB.Translate("OW_btnDone");
Text = NB.Translate("OW_lblNetTitle");
Text = NB.Translate("OW_cbDisplayTitles");
Text = NB.Translate("OW_lblNetSize");
Text = NB.Translate("OW_lblItemSize");
Text = NB.Translate("OW_lblNetMessage");
Text = NB.Translate("OW_lblTests");
Text = NB.Translate("OW_lblTags");
Text = NB.Translate("OW_lblLevel");
Text = NB.Translate("OW_lblSortOrder");
Text = NB.Translate("OW_lblStartingHelp");
Text = NB.Translate("OW_Form");
}
private void LoadValuesFromNetwork()
{
tbItemSize.Text = myNet.itemsize.ToString();
tbMessage.Text = myNet.NetMessage;
tbNetworkTitle.Text = myNet.NetTitle;
cbDisplayTitles.Checked = myNet.ShowLabels;
tbNetworkX.Text = myNet.myWidth.ToString();
tbNetworkY.Text = myNet.myHeight.ToString();
tbLevel.Text = myNet.Level.ToString();
tbSortOrder.Text = myNet.SortOrder.ToString("00.000");
lbTests.Items.Clear();
string what;
foreach(NetTest NT in myNet.NetTests)
{
what = NT.GetDescription(NetTestVerbosity.full);
if (what != "")
{
lbTests.Items.Add(what);
}
}
lbTags.Items.Clear();
myNet.Tags.Sort();
foreach (string tag in myNet.Tags)
{
lbTags.Items.Add(tag);
}
cbStartingHelpLevel.Items.Clear();
foreach (string helplevel in Enum.GetNames(typeof(NetTestVerbosity)))
{
cbStartingHelpLevel.Items.Add(helplevel);
}
cbStartingHelpLevel.SelectedItem = myNet.StartingHelpLevel.ToString();
}
private void SaveValuesToNetwork()
{
int.TryParse(tbItemSize.Text, out myNet.itemsize);
myNet.NetMessage = tbMessage.Text;
myNet.NetTitle = tbNetworkTitle.Text;
myNet.ShowLabels = cbDisplayTitles.Checked;
int.TryParse(tbNetworkX.Text, out myNet.myWidth);
int.TryParse(tbNetworkY.Text, out myNet.myHeight);
myNet.InvalidateEverything();
int.TryParse(tbLevel.Text, out myNet.Level);
double.TryParse(tbSortOrder.Text, out myNet.SortOrder);
myNet.StartingHelpLevel = NB.ParseEnum<NetTestVerbosity>(cbStartingHelpLevel.SelectedItem.ToString());
}
private void btnDone_Click(object sender, EventArgs e)
{
SaveValuesToNetwork();
Close();
}
private void lbTests_MouseDoubleClick(object sender, MouseEventArgs e)
{
//if left-click.
//if on an item, edit it
//if not on an item, make a new one and edit it
NetTest NT;
SaveValuesToNetwork();
if(lbTests.SelectedIndex != -1)
{
//Something is selected. Edit it
NT = myNet.NetTests[lbTests.SelectedIndex];
NT.Edit();
}
else
{
//nothing is selected, create new one
NT = new NetTest("", "", NetTestType.NeedsDefaultGW);
if(NT.Edit())
{
//Returns true. Something was changed. Save it.
myNet.NetTests.Add(NT);
}
}
LoadValuesFromNetwork();
}
private void lbTests_Edit_Click(object sender, EventArgs e)
{
lbTests_MouseDoubleClick(sender, null);
}
private void lbTests_Add_Click(object sender, EventArgs e)
{
lbTests.SelectedIndex = -1;
lbTests_MouseDoubleClick(sender, null);
}
private void lbTests_Delete_Click(object sender, EventArgs e)
{
if (lbTests.SelectedIndex >= lbTests.Items.Count) return;
if (lbTests.SelectedIndex < 0 ) return;
SaveValuesToNetwork();
myNet.NetTests.RemoveAt(lbTests.SelectedIndex);
LoadValuesFromNetwork();
}
private void lbTests_MouseClick(object sender, MouseEventArgs e)
{
//see if it is right-click
// Add, delete, edit
int index = 0;
if(e.Button == MouseButtons.Right)
{
if (lbTests.ContextMenuStrip == null)
{
lbTests.ContextMenuStrip = new ContextMenuStrip();
}
lbTests.ContextMenuStrip.Items.Clear();
lbTests.ContextMenuStrip.Items.Add("Add");
lbTests.ContextMenuStrip.Items[index++].Click += lbTests_Add_Click;
lbTests.ContextMenuStrip.Items.Add("Edit");
lbTests.ContextMenuStrip.Items[index++].Click += lbTests_Edit_Click;
lbTests.ContextMenuStrip.Items.Add("Delete");
lbTests.ContextMenuStrip.Items[index++].Click += lbTests_Delete_Click;
}
}
private void lbTests_MouseDown(object sender, MouseEventArgs e)
{
//see if it is right-click
// Add, delete, edit
int index = 0;
if (e.Button == MouseButtons.Right)
{
if (lbTests.ContextMenuStrip == null)
{
lbTests.ContextMenuStrip = new ContextMenuStrip();
}
lbTests.ContextMenuStrip.Items.Clear();
lbTests.ContextMenuStrip.Items.Add("Add");
lbTests.ContextMenuStrip.Items[index++].Click += lbTests_Add_Click;
lbTests.ContextMenuStrip.Items.Add("Edit");
lbTests.ContextMenuStrip.Items[index++].Click += lbTests_Edit_Click;
lbTests.ContextMenuStrip.Items.Add("Delete");
lbTests.ContextMenuStrip.Items[index++].Click += lbTests_Delete_Click;
}
}
/*******************************************************/
/******* STUFF FOR TAGS *******************************/
private void lbTags_Edit_Click(object sender, EventArgs e)
{
lbTags_MouseDoubleClick(sender, null);
}
private void lbTags_Add_Click(object sender, EventArgs e)
{
lbTags.ClearSelected();
lbTags_MouseDoubleClick(sender, null);
}
private void lbTags_Delete_Click(object sender, EventArgs e)
{
if (lbTags.SelectedIndex >= lbTags.Items.Count) return;
if (lbTags.SelectedIndex < 0) return;
SaveValuesToNetwork();
myNet.Tags.RemoveAt(lbTags.SelectedIndex);
LoadValuesFromNetwork();
}
private void lbTags_MouseDown(object sender, MouseEventArgs e)
{
//see if it is right-click
// Add, delete, edit
int index = 0;
if (e.Button == MouseButtons.Right)
{
if (lbTags.ContextMenuStrip == null)
{
lbTags.ContextMenuStrip = new ContextMenuStrip();
}
lbTags.ContextMenuStrip.Items.Clear();
lbTags.ContextMenuStrip.Items.Add("Add");
lbTags.ContextMenuStrip.Items[index++].Click += lbTags_Add_Click;
if (lbTags.SelectedIndex != -1)
{
lbTags.ContextMenuStrip.Items.Add("Edit");
lbTags.ContextMenuStrip.Items[index++].Click += lbTags_Edit_Click;
lbTags.ContextMenuStrip.Items.Add("Delete");
lbTags.ContextMenuStrip.Items[index++].Click += lbTags_Delete_Click;
}
}
}
private string QuickPrompt(string title, string prompt, string value)
{
QuestionForm.Text = title;
Label lbPrompt = new Label();
lbPrompt.Text = prompt;
lbPrompt.Location = new Point(1, 1);
lbPrompt.Size = new Size(200, lbPrompt.Size.Height);
TextBox Choice = new TextBox();
Choice.Text = value;
Choice.Location = new Point(1, lbPrompt.Location.Y + lbPrompt.Height + 5);
Button Done = new Button();
Done.Click += btnClose_Click;
Done.Text = "Done";
Done.Location = new Point(1, Choice.Location.Y + Choice.Height + 5);
QuestionForm.Controls.Clear();
QuestionForm.Controls.Add(Choice);
QuestionForm.Controls.Add(Done);
QuestionForm.Controls.Add(lbPrompt);
QuestionForm.AcceptButton = Done;
QuestionForm.FormBorderStyle = FormBorderStyle.FixedDialog;
QuestionForm.AutoSize = true;
QuestionForm.Height = Done.Location.Y + Done.Height + 5; //This is too small for the form, it autosizes to "big enough"
QuestionForm.Width = Choice.Location.X + Choice.Width + 5;
QuestionForm.ShowDialog();
return Choice.Text;
}
private void btnClose_Click(object sender, EventArgs e)
{
if (QuestionForm != null)
QuestionForm.Close();
}
private void lbTags_MouseDoubleClick(object sender, MouseEventArgs e)
{
//if left-click.
//if on an item, edit it
//if not on an item, make a new one and edit it
SaveValuesToNetwork();
string newstring;
if (lbTags.SelectedIndex != -1)
{
//Something is selected. Edit it
newstring = QuickPrompt("Edit Tag", "Tag:", myNet.Tags[lbTags.SelectedIndex]);
if (newstring != "")
{
myNet.Tags[lbTags.SelectedIndex] = newstring;
}
else
{
myNet.Tags.RemoveAt(lbTags.SelectedIndex);
}
}
else
{
//nothing is selected, create new one
newstring = QuickPrompt("Edit Tag", "Tag:", "");
if (newstring != "")
{
myNet.Tags.Add(newstring);
}
}
LoadValuesFromNetwork();
}
private void tbSortOrder_Validating(object sender, CancelEventArgs e)
{
double value;
double.TryParse(tbSortOrder.Text, out value);
tbSortOrder.Text = value.ToString("00.000");
}
private void tbLevel_Validating(object sender, CancelEventArgs e)
{
int value;
int.TryParse(tbLevel.Text, out value);
tbLevel.Text = value.ToString();
}
}
}

456
EduNetworkBuilder/Packet.cs Normal file
View File

@ -0,0 +1,456 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
using System.IO;
using System.Drawing;
using System.Text.RegularExpressions;
namespace EduNetworkBuilder
{
//This is a network packet that can either contain data, or another packet
[Serializable]
public class Packet
{
public PacketType MyType = PacketType.none;
public PacketStatus _MyStatus = PacketStatus.processing; //new packets start by being processed by the source device
public PacketStatus MyStatus
{
get { return _MyStatus; }
set {
_MyStatus = value;
if (_MyStatus == PacketStatus.finished_failed || _MyStatus == PacketStatus.finished_ok)
{
string status = _MyStatus.ToString();
PacketDump(" " + status, DebugPausePoint.packet_kill);
}
if (_MyStatus == PacketStatus.finished_ok)
Tracking.Finished = true;
}
}
public int TTL = 20;
public int TickTTL = 50;
public IPAddress sourceIP;
public IPAddress OriginalDestIP;
public IPAddress TsourceIP;
public IPAddress destIP;
public string sourceMAC;
public string destMAC;
public Packet payloadPacket = null;
public string payloadData = ""; //Contains the mac-address, or ip-address string, or something else
public string EncryptionString = "";
public IPAddress payloadIP = null;
public PacketMessage Tracking = new PacketMessage();
public NetworkComponent WhereAmI = null;
public nb_direction myDirection = nb_direction.none;
public int myLinkPercent = 0; //How far along the path are we. 0%, 50%, 100%. For moving a dot along the link line
public bool packet_good = true;
DateTime StartTime = DateTime.Now;
public NetworkCard OutboundNic = null;
public NetworkInterface OutboundIF = null;
public IPAddress OutboundIP = new IPAddress(NB.ZeroIPString);
public string OutboundMAC = "";
public string OutboundDestMAC = "";
public bool isFresh = false; //Set to be true if the packet is new. Set false as soon as it is processed
public bool DebugOn = false;
public bool ready_to_delete
{
get {
Tracking.duration = DateTime.Now - StartTime;
if (MyStatus == PacketStatus.finished_failed)
{
packet_good = false;
return true;
}
if (MyStatus == PacketStatus.finished_ok)
{
packet_good = true; //It should already be good, but we state it just in case
return true;
}
if (MyStatus == PacketStatus.finished)
{
packet_good = true; //It should already be good, but we state it just in case
return true;
}
return false;
}
}
public Packet(Packet copyfrom)
{
MyType = copyfrom.MyType;
TTL = copyfrom.TTL;
sourceIP = copyfrom.sourceIP;
TsourceIP = copyfrom.TsourceIP;
destIP = copyfrom.destIP;
sourceMAC = copyfrom.sourceMAC;
destMAC = copyfrom.destMAC;
payloadData = copyfrom.payloadData;
payloadPacket = copyfrom.payloadPacket;
EncryptionString = copyfrom.EncryptionString;
payloadIP = copyfrom.payloadIP;
Tracking = copyfrom.Tracking;
WhereAmI = copyfrom.WhereAmI;
StartTime = copyfrom.StartTime;
OriginalDestIP = copyfrom.OriginalDestIP;
Tracking.AddMessage(DebugLevel.debug, WhereAmI, "Packet duplicated");
}
//Generate a packet with the given payload.
public Packet(NetworkComponent start, string source, string dest, string payload, PacketType theType)
{
WhereAmI = start;
payloadData = payload;
MyType = theType;
if(theType == PacketType.arp_answer || theType == PacketType.arp_request ||
theType == PacketType.dhcp_request || theType == PacketType.dhcp_answer)
{
sourceMAC = source;
destMAC = dest;
OutboundDestMAC = dest;
Tracking.AddMessage(DebugLevel.info, start, theType.ToString() + "Packet Created");
Tracking.AddMessage(DebugLevel.routing, start, " MAC:" + source + " -> " + dest);
}
else
{
sourceIP = new IPAddress(source);
TsourceIP = new IPAddress(source);
destIP = new IPAddress(dest);
if (destIP.BroadcastAddress == destIP.GetIP)
{
destMAC = NB.BroadcastMACString;
OutboundDestMAC = destMAC;
}
Tracking.AddMessage(DebugLevel.info, start, "Packet Created");
Tracking.AddMessage(DebugLevel.routing, start, " IP:" + source + " -> " + dest);
Tracking.AddMessage(DebugLevel.debug, start, " IPs Translated to" + sourceIP.GetIP.ToIpString() + " -> " + destIP.GetIP.ToIpString());
}
isFresh = true;
}
public Packet(NetworkComponent start, IPAddress dest, string payload, PacketType theType)
{
WhereAmI = start;
payloadData = payload;
MyType = theType;
if (theType != PacketType.arp_answer && theType != PacketType.arp_request)
{
sourceIP = new IPAddress(NB.ZeroIPString);
destIP = dest;
if (destIP != null && destIP.BroadcastAddress == destIP.GetIP)
destMAC = NB.BroadcastMACString;
Tracking.AddMessage(DebugLevel.info, start, "Packet Created");
Tracking.AddMessage(DebugLevel.routing, start, " IP:" + sourceIP.GetIP.ToIpString() + " -> " + dest.GetIP.ToIpString());
}
else
{
sourceIP = new IPAddress(NB.ZeroIPString);
destMAC = NB.BroadcastMACString;
destIP = dest;
Tracking.AddMessage(DebugLevel.info, start, "Packet Created");
//Tracking.AddMessage(DebugLevel.routing, start, " IP:" + sourceIP.GetIP.ToIpString() + " -> " + dest.GetIP.ToIpString());
}
isFresh = true;
}
public Point PacketLocation()
{
if (NB.GetComponentType(WhereAmI) != GeneralComponentType.link)
return new Point(-1,-1);
NetworkLink Nl = (NetworkLink)WhereAmI;
Point mylocation = Nl.PositionOnLine(myDirection, myLinkPercent);
return mylocation;
}
public Rectangle PacketRectangle()
{
Point start = PacketLocation();
int half = NB.PacketPixelSize / 2;
return new Rectangle(start.X - half, start.Y - half, NB.PacketPixelSize, NB.PacketPixelSize);
}
public void Print(Image BaseImage)
{
//we draw a packet on the map
Point thePoint = PacketLocation();
if (thePoint.X == -1 || thePoint.Y == -1) return; //It is an invalid location
Color pencolor = Color.Blue;
switch (MyType)
{
case PacketType.arp_answer:
case PacketType.arp_request:
pencolor = Color.Green;
break;
case PacketType.dhcp_answer:
case PacketType.dhcp_request:
pencolor = Color.Red;
break;
case PacketType.ping_answer:
case PacketType.ping_request:
pencolor = Color.Blue;
break;
case PacketType.tun_packet:
pencolor = Color.White;
break;
case PacketType.vpn_packet:
pencolor = Color.Orange;
break;
}
Pen myPen = new Pen(pencolor, NB.PacketPixelSize);
Graphics.FromImage(BaseImage).DrawEllipse(myPen, PacketRectangle());
}
/// <summary>
/// See if we need to do anything for a packet. If so, do one step in processing it
/// </summary>
public void ProcessTick()
{
TickTTL--;
switch(MyStatus)
{
case PacketStatus.processing:
//The packet is "inside" the device.
// Here we masquerade
// Here we route
// Here the packet is just about to exit. Maybe we need to set the "source IP"
if (NB.PacketVersionNum != 2)
{
DoProcessing();
}
else
{
DoMainProcessing();
}
break;
case PacketStatus.finished_ok:
case PacketStatus.finished_failed:
break; //we do not have anything to do.
case PacketStatus.input: //Just entering a device.
case PacketStatus.moving: //Traversing a link
DoMoving();
break;
case PacketStatus.output: //Just leaving a device
break;
case PacketStatus.waiting_for_arp: //We need to see if the arp information has shown up. If so, we resume
if (NB.GetComponentType(WhereAmI) == GeneralComponentType.device)
{
NetworkDevice ndWhere = (NetworkDevice)WhereAmI;
IPAddress dest = ndWhere.DestinationFromIP(destIP);
string dMAC = ndWhere.ArpFromIP(dest.GetIP.ToIpString());
if (dMAC != "" || destMAC == NB.BroadcastMACString)
{
MyStatus = PacketStatus.processing;
}
else
{
Network MyNet = NB.GetNetwork();
if(MyNet.CountPackets(PacketType.arp_answer) + MyNet.CountPackets(PacketType.arp_request) == 0)
{
//No more arps going. We do not have an answer!
Tracking.AddMessage(DebugLevel.info, WhereAmI, "Unable to find a mac address for the IP address: " + dest.GetIP.ToIpString());
Tracking.Status = WhereAmI.hostname + " Failed: Unable to find a MAC address for the specified IP.";
MyStatus = PacketStatus.finished_failed;
}
return;
}
}
break;
}
}
private void DoMainProcessing()
{
//We pass it to the device
if (WhereAmI == null)
{
AddMessage(DebugLevel.debug, "The packet was dropped because we lost where it was. WhereAmI == null!");
Tracking.Status = "NO_Host" + " Lost location of packet..";
MyStatus = PacketStatus.finished_failed;
return; //We cannot process the packet
}
WhereAmI.ProcessPacket(this);//The device knows how to do the processing
}
private void DoProcessing()
{
//The packet is "inside" the device.
// Here we masquerade
// Here we route
// Here the packet is just about to exit. Maybe we need to set the "source IP"
//We need to find out which interface we are leaving.
//Then we figure out what rules we need to follow from there
//So, we pass the packet to the device to process
if (WhereAmI == null)
{
AddMessage(DebugLevel.debug, "The packet was dropped because we lost where it was. WhereAmI == null!");
Tracking.Status = "NO_Host" + " Lost location of packet..";
MyStatus = PacketStatus.finished_failed;
return; //We cannot process the packet
}
WhereAmI.DoProcessing(this);//The device knows how to do the processing
}
private void DoMoving()
{
if (WhereAmI == null)
{
AddMessage(DebugLevel.debug, "The packet was dropped because we lost where it was. WhereAmI == null!");
Tracking.Status = "NO_Host" + " Lost location of packet..";
MyStatus = PacketStatus.finished_failed;
return; //We cannot process the packet
}
WhereAmI.DoMoving(this);//The device knows how to do the processing
}
public void ReplaceMessage(PacketMessage Tracker)
{
Tracking = Tracker; //We do this if we are making a new packet, but tracking it with the old one.
//We do this for arp requests.
}
public void StartOnLink(NetworkLink theLink, NetworkDevice start_device)
{
WhereAmI = theLink;
MyStatus = PacketStatus.moving;
myLinkPercent = 0;
TickTTL = 200; //We keep resetting this as we go.
OutboundIF = null;
OutboundIP = null;
OutboundMAC = "";
OutboundNic = null;
if (theLink.IsSource(start_device.GetUniqueIdentifier))
myDirection = nb_direction.to_dst;
else
myDirection = nb_direction.to_src;
AddMessage(DebugLevel.debug, " Starting on link");
}
public void StartOnDevice(NetworkDevice theDevice)
{
WhereAmI = theDevice;
MyStatus = PacketStatus.processing;
myLinkPercent = 0;
TickTTL = 200;
}
public void PrepareToDelete()
{
Network myNet = NB.GetNetwork();
if(myNet != null)
{
if (payloadPacket != null)
{
payloadPacket.PrepareToDelete();
payloadPacket.Tracking.AddMessage(DebugLevel.info, WhereAmI, Tracking.Status);
payloadPacket.Tracking.Status = Tracking.Status;
payloadPacket.MyStatus = MyStatus;
}
if(MyType == PacketType.arp_request && MyStatus == PacketStatus.finished_failed)
{
Tracking.AddMessage(DebugLevel.info, "Packet", "Packet failed to reach any IP. No such IP on network: " + destIP.GetIPString);
Tracking.Status = "Packet failed to reach IP: " + destIP.GetIPString;
MyStatus = PacketStatus.finished_failed;
}
if(MyType == PacketType.ping_answer && MyStatus == PacketStatus.finished_failed)
{
myNet.NoteActionDone(NetTestType.FailedPing, destIP.GetIPString, sourceIP.GetIPString);
}
if (MyType == PacketType.ping_request && MyStatus == PacketStatus.finished_failed)
{
myNet.NoteActionDone(NetTestType.FailedPing, sourceIP.GetIPString, destIP.GetIPString);
}
if (MyStatus != PacketStatus.finished)
myNet.AddMessage(Tracking); //We only store finished_failed, and finished_ok
}
}
public void AddMessage(DebugLevel tLevel, string tMessage)
{
Tracking.AddMessage(tLevel, WhereAmI, tMessage);
}
public void IncrementDistance()
{
myLinkPercent += NB.LinkStep;
}
public bool Arrived()
{
if (myLinkPercent >= 100)
return true;
return false;
}
public bool isFinshed()
{
if (_MyStatus == PacketStatus.finished_ok) return true;
if (_MyStatus == PacketStatus.finished_failed) return true;
if (_MyStatus == PacketStatus.finished_ok) return true;
return false;
}
private bool debugIsSet(DebugPausePoint what)
{
DebugPausePoint SetPoint = NB.GetDebugPauseSetting();
if ((SetPoint & what) == what)
return true;
return false;
}
public void PacketDump(string hostname, DebugPausePoint WhereWeAre)
{
DebugPausePoint WhatIsSet = NB.GetDebugPauseSetting();
string position = WhereWeAre.ToString();
string sIP = NB.ZeroIPString;
string dIP = NB.ZeroIPString;
if (sourceIP != null) sIP = sourceIP.GetIPString;
if (destIP != null) dIP = destIP.GetIPString;
position = Regex.Replace(position, "packet_", "");
if ((WhereWeAre & WhatIsSet) != WhereWeAre)
return; //We are not set to debug here.
if ((WhatIsSet & DebugPausePoint.pause) == DebugPausePoint.pause)
{
Console.WriteLine("Pausing");
}
if ((WhatIsSet & DebugPausePoint.dump) == DebugPausePoint.dump)
{
Console.WriteLine(hostname + ": " + MyType.ToString());
Console.Write(hostname + ": " + position + " PACKET: dstIP:" + dIP);
Console.WriteLine(" dstMAC:" + destMAC);
Console.Write(hostname + ": " + position + " PACKET: srcIP:" + sIP);
Console.WriteLine(" srcMAC:" + sourceMAC);
Console.WriteLine("---");
}
AddMessage(DebugLevel.packet, hostname + ": " + position + " PACKET: dstIP:" + dIP + " dstMAC:" + destMAC);
AddMessage(DebugLevel.packet, hostname + ": " + position + " PACKET: srcIP:" + sIP + " srcMAC:" + sourceMAC);
}
public static T Clone<T>(T source)
{
if (!typeof(T).IsSerializable)
{
throw new ArgumentException("The type must be serializable.", "source");
}
// Don't serialize a null object, simply return the default for that object
if (Object.ReferenceEquals(source, null))
{
return default(T);
}
IFormatter formatter = new BinaryFormatter();
Stream stream = new MemoryStream();
using (stream)
{
formatter.Serialize(stream, source);
stream.Seek(0, SeekOrigin.Begin);
return (T)formatter.Deserialize(stream);
}
}
}
}

View File

@ -0,0 +1,110 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
using System.IO;
namespace EduNetworkBuilder
{
/// <summary>
/// This tracks everything that happened to a packet in transit
/// </summary>
///
[Serializable]
public struct DebugMessage
{
public DebugLevel WhatLevel;
public string HostName;
public string Message;
public DebugMessage(DebugLevel tLevel, string tHost, string tMessage)
{
WhatLevel = tLevel;
HostName = tHost;
Message = tMessage;
}
}
[Serializable]
public class PacketMessage
{
List<DebugMessage> Messages = new List<DebugMessage>();
public string _Status = ""; //Usually "success" or "failed"
public bool Finished = false;
public TimeSpan duration;
public string Status
{
get { return _Status; }
set { if (!Finished) _Status = value; }
}
public static T Clone<T>(T source)
{
if (!typeof(T).IsSerializable)
{
throw new ArgumentException("The type must be serializable.", "source");
}
// Don't serialize a null object, simply return the default for that object
if (Object.ReferenceEquals(source, null))
{
return default(T);
}
IFormatter formatter = new BinaryFormatter();
Stream stream = new MemoryStream();
using (stream)
{
formatter.Serialize(stream, source);
stream.Seek(0, SeekOrigin.Begin);
return (T)formatter.Deserialize(stream);
}
}
public void AddMessage(DebugLevel tLevel, string tHost, string tMessage)
{
if(!Finished)
Messages.Add(new DebugMessage(tLevel, tHost, tMessage));
}
public void AddMessage(DebugLevel tLevel, NetworkComponent tHost, string tMessage)
{
string host = "unknown";
if (tHost != null)
host = tHost.hostname;
if(! Finished)
Messages.Add(new DebugMessage(tLevel, host, tMessage));
}
public List<string> GetMessagesLike(DebugLevel tLevel)
{
List<string> answer = new List<string>();
string tString;
foreach( DebugMessage tmessage in Messages)
{
if((tmessage.WhatLevel | tLevel) == tLevel)
{
tString = tmessage.HostName + " \t" + tmessage.WhatLevel.ToString() + "\t" + tmessage.Message;
answer.Add(tString);
}
}
return answer;
}
public List<string> GetMessagesSummary()
{
List<string> answer = new List<string>();
string tString;
int time = duration.Seconds * 1000 + duration.Milliseconds;
tString = time.ToString() + " ms " + Status;
answer.Add(tString);
return answer;
}
}
}

View File

@ -0,0 +1,91 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Globalization;
using System.Resources;
namespace EduNetworkBuilder
{
public partial class RTFWindow : Form
{
public RTFWindow()
{
InitializeComponent();
LanguagifyComponents();
}
/// <summary>
/// Used when viewing help from the ? button. This displays network specific help information
/// </summary>
/// <param name="theHelp"></param>
/// <param name="theTests"></param>
public RTFWindow(string title, string theHelp, List<NetTest> theTests)
{
InitializeComponent();
LanguagifyComponents();
BuilderWindow myWin = (BuilderWindow)Application.OpenForms["BuilderWindow"];
if (myWin != null)
{
StartPosition = FormStartPosition.Manual;
Point newlocation = new Point(myWin.Location.X + myWin.Width + 5, myWin.Location.Y);
if (newlocation.X > (Screen.PrimaryScreen.Bounds.Width + 5))
{
newlocation = new Point(Screen.PrimaryScreen.Bounds.Width - 5, newlocation.Y);
}
Location = newlocation;
}
System.Drawing.Font currentFont = rtbContent.SelectionFont;
Text = title;
rtbContent.Text = theHelp;
rtbContent.SelectAll();
rtbContent.SelectionFont = new Font(
currentFont.FontFamily,
currentFont.Size + 2, FontStyle.Regular);
rtbContent.DeselectAll();
}
public RTFWindow(RTFWindowContents WhatToShow)
{
InitializeComponent();
LanguagifyComponents();
string currentDir = Directory.GetCurrentDirectory();
string myRTF;
if (WhatToShow == RTFWindowContents.help)
{
myRTF = Properties.Resources.Help;
rtbContent.Rtf = myRTF;
}
else if (WhatToShow == RTFWindowContents.about)
{
myRTF = Properties.Resources.about;
rtbContent.Rtf = myRTF;
}
else if (WhatToShow == RTFWindowContents.release_notes)
{
myRTF = Properties.Resources.ReleaseNotes;
rtbContent.Rtf = myRTF;
}
}
private void LanguagifyComponents()
{
ResourceManager RM = NB.GetResource();
CultureInfo CI = NB.GetCulture();
Text = NB.Translate("RTFW_rtbContent");
Text = NB.Translate("RTFW_btnOK");
Text = NB.Translate("RTFW_Form");
}
private void btnOK_Click(object sender, EventArgs e)
{
Close();
}
}
}

View File

@ -0,0 +1,107 @@
#!/bin/perl
#
$filedir="Resources/languages/";
$langfile="edustrings.resx";
sub parse_file
{
my $filename = "@_";
my $front = $filename;
my %items;
my $program = $filename;
$program =~ s/\..*//g;
$front =~ s/\..*//g;
$front =~ s/[a-z]//g;
$front = "${front}_";
#print "$filename\n";
#print "$front\n";
my $count=0;
if ( -f $filename )
{
open(INFILE, $filename);
while ( <INFILE> )
{
if(/([^.]*)\.Text = (.*)$/)
{
$var=$1;
$text=$2;
$var=~ s/ *//g;
if($var eq "this") { $var="Form"};
$item=$var;
$var="$front$var";
$text=~ s/[";]//g;
$text=~ s/\r//g;
$text=~ s/\n//g;
#print "Var: $var Text: $text\n";
#now we process them. If it does not exist yet, make an entry for it.
if(!exists($existing{$var}))
{
#print("var $key item $items{$key}\n");
print EDUFILE (' <data name="' . $var . '" xml:space="preserve">' . "\r\n");
printf EDUFILE (' <value>%s</value>%s',$text,"\n");
printf EDUFILE (' <comment>%s</comment>%s',"$program $item = $text","\n");
print EDUFILE (" </data>\n");
if($count == 0)
{
print CFILE "$program\n";
print CFILE "private void LanguagifyComponents()
{
ResourceManager RM = NB.GetResource();
CultureInfo CI = NB.GetCulture();\n"
}
if($item == "Form")
{
print CFILE ("Text = RM.GetString(\"$var\", CI);\n");
}
else
{
print CFILE ("$item.Text = RM.GetString(\"$var\", CI);\n");
}
$count++;
}
}
}
close INFILE;
if($count > 0)
{
print CFILE ("}\n\n");
}
}
else
{
print "$filename does not exist\n";
}
}
#Read the language file to see what already exists.
$tfile="$filedir/$langfile";
open (IFILE, $tfile) or die "$tfile does not exist.";
while ( <IFILE> )
{
if(/data name="([^"]*)"/)
{
$tag=$1;
#print("TAG: $tag\n");
$existing{$tag}="here";
}
}
close(IFILE);
open(EDUFILE, ">add2edustrings.txt");
open(CFILE, ">add2cfiles.txt");
#parse the various Designer files and process them
my @files = glob("*.Designer.cs");
foreach my $file (@files)
{
print "$file\n";
parse_file( $file );
}
close(CFILE);
close(EDUFILE);