448 lines
17 KiB
C#
448 lines
17 KiB
C#
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()
|
|
{
|
|
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();
|
|
}
|
|
|
|
}
|
|
}
|