Many small changes working towards renumbering a subnet. If many items are selected, we can right-click and renumber the lot of them.

This commit is contained in:
Tim Young 2019-03-06 12:32:00 +03:00
parent c0b655386e
commit 4014439162
4 changed files with 364 additions and 48 deletions

View File

@ -119,6 +119,7 @@
<DependentUpon>ListBoxWindow.cs</DependentUpon>
</Compile>
<Compile Include="NB.cs" />
<Compile Include="NBRenumberData.cs" />
<Compile Include="NBSettings.cs" />
<Compile Include="NetShape.cs" />
<Compile Include="NetTest.cs" />

View File

@ -379,6 +379,8 @@ namespace EduNetworkBuilder
public static int NormalRotation = 5;
public static int PacketDamageDistance = 25;
public static int InvalidVLAN = -999; //What we return when it is invalid
public static bool DebugActions = false;
public static bool DebugTests = false;
@ -934,6 +936,46 @@ namespace EduNetworkBuilder
return tbEvent.Text;
}
public static string ComboBoxPromptBox(string title, string prompt, List<string> Options, string value)
{
Form QuestionForm = new Form();
QuestionForm.Text = title;
Label lbPrompt = new Label();
lbPrompt.Text = prompt;
lbPrompt.Location = new Point(1, 1);
lbPrompt.Size = new Size(200, lbPrompt.Size.Height);
ComboBox Choice = new ComboBox();
List<string> tlist = new List<string>();
foreach (string tstring in Options)
{
Choice.Items.Add(tstring);
}
if (value != "" && Choice.Items.Contains(value))
{
Choice.SelectedItem = value;
}
Choice.Location = new Point(1, lbPrompt.Location.Y + lbPrompt.Height + 5);
Button Done = new Button();
Done.Click += (s, g) => { Button b = (Button)s; Form f = (Form)b.Parent; f.Close(); };
Choice.DoubleClick += (s, g) => { Button b = (Button)s; Form f = (Form)b.Parent; Choice.SelectedIndex = -1; f.Close(); }; ;
Done.Text = NB.Translate("_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();
if (Choice.SelectedIndex >= 0) return Choice.SelectedItem.ToString();
return "";
}
public static Dictionary<string, bool> CheckPromptBox(Dictionary<string, bool> Checks)
{

View File

@ -919,6 +919,12 @@ namespace EduNetworkBuilder
pbNetworkView.ContextMenuStrip.Items.Add(NB.Translate("_Delete"));
pbNetworkView.ContextMenuStrip.Items[index++].Click += pbNetworkView_Delete_Click;
if(!myNetwork.LoadedFromResource)
{
pbNetworkView.ContextMenuStrip.Items.Add(NB.Translate("NB_Renumber"));
pbNetworkView.ContextMenuStrip.Items[index++].Click += pbNetworkRenumber;
}
//We can color-code multiple items
if (myNetwork.VLANsEnabled)
{
@ -1129,6 +1135,101 @@ namespace EduNetworkBuilder
}
/// <summary>
/// Try to renumber a chunk of a network
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void pbNetworkRenumber(object sender, EventArgs e)
{
//come up with a plan
// sort items by type
// loop through the items
// come up with the numbers we want to use
// Decide which nics to change
// On a router, find the nic that is connected to other items in the list
// On other devices, find first nic that is connected that is not DHCP
// track hostname, nicname, ip_address
//print out a list of what we are going to do
//Ask if we want to do it. Yes/No
//do it
if (ItemsSelected.Count > 0)
{
int ChosenVlanID = 1;
if(myNetwork != null && myNetwork.VLANsEnabled)
{
List<string> vnames = new List<string>();
foreach(VLANName vn in myNetwork.VlanNames)
{
vnames.Add(vn.Name);
}
string ChosenVlanName = "";
if (vnames.Count > 0) ChosenVlanName = vnames[0]; //Usually "Default"
if (vnames.Count > 1)
{
//we need to prompt
ChosenVlanName = NB.ComboBoxPromptBox(NB.Translate("NB_ChooseVLANRenumberTitle"), NB.Translate("NB_ChooseVLANRenumber"), vnames, vnames[0]);
}
ChosenVlanID = myNetwork.VLANIDFromName(ChosenVlanName);
}
NB_IPAddress prompted = new NB_IPAddress("192.168.1.0","255.255.255.0","192.168.1.1");
IPAddressEntry IPE = new IPAddressEntry(prompted, null, this, false);
//This next entry should never happen.
if (ItemsSelected.Count < 2)
{
MessageBox.Show("You need to have more items selected");
return;
}
//Read in the settings we will use
IPE.ShowAsSubnetGateway();
//pull out the gateway address
NB_IPAddress gw = new NB_IPAddress(prompted.GetGateway.ToIpString(), prompted._mask.ToIpString(),IPAddressType.gw);
//pull out the network address
NB_IPAddress network = new NB_IPAddress(prompted.NetworkAddress.ToIpString(), prompted._mask.ToIpString(), IPAddressType.gw);
//We need to find the gateway device. If no such device exists, find the most likely candidate
string gateway_name = myNetwork.ReverseDNSLookup(ItemsSelected[0],gw);
NetworkDevice gwDevice = myNetwork.DeviceFromName(gateway_name);
if(gwDevice == null)
{
//Make a list of possibilities
//Prompt user to select one of them
List<NetworkDevice> gwdevices = new List<NetworkDevice>();
for (int i = ItemsSelected.Count - 1; i >= 0; i--)
{
if (ItemsSelected[i].RoutesPackets()) gwdevices.Add(ItemsSelected[i]);
}
if (gwdevices.Count == 1) gwDevice = gwdevices[0];
else
{
//We need to prompt which device
List<string> names = new List<string>();
foreach(NetworkDevice one in gwdevices)
{
names.Add(one.hostname);
}
names.Sort();
gateway_name = NB.ComboBoxPromptBox(NB.Translate("_Gateway"), NB.Translate("NB_ChooseGateway"), names, names[0]);
gwDevice = myNetwork.DeviceFromName(gateway_name);
}
}
if (gwDevice == null) return; //No gateway device selected
//If we get here, we have a gateway and a list of items to renumber.
for (int i = ItemsSelected.Count - 1; i >= 0; i--)
{
}
}
}
private void pbNetworkView_Hide_Click(object sender, EventArgs e)
{
if (ItemClickedOn != null)

View File

@ -702,6 +702,139 @@ namespace EduNetworkBuilder
return nic.ConnectedTo();
}
public int VLANFromNIF(NetworkInterface nif)
{
if (DoesVLANs())
{
//This device itself does vlans
int vlan = nif.GetUntaggedVLAN();
if (vlan != NB.InvalidVLAN)
{
return vlan;
}
}
if (nif == null) return NB.InvalidVLAN;
NetworkCard nic = NicFromID(nif.AttachedToHostNic);
if (nic == null) return NB.InvalidVLAN; //Nothing
return VLANFromNIC(nic);
}
public int VLANFromNIC(NetworkCard nic)
{
int vlan = NB.InvalidVLAN;
if (DoesVLANs())
{
foreach (NetworkInterface nif in nic.interfaces)
{
int tvlan = VLANFromNIF(nif);
if (tvlan != NB.InvalidVLAN)
{
if (vlan != NB.InvalidVLAN && vlan != tvlan)
return NB.InvalidVLAN; //We have multiple VLANs defined on this nic. Cannot determine it from the nic. Need to look using nif
vlan = tvlan;
}
}
return vlan;
}
//If the device does not do vlans, we need to trace the wire to the device.
NetworkDevice FarEnd = ConnectedTo(nic);
if (FarEnd == null) return NB.InvalidVLAN; //Nothing connected to this link
//if it is a switch or wrouter(does vlans), return vlan for the device
if (FarEnd.myType == NetworkComponentType.net_switch || FarEnd.myType == NetworkComponentType.wrouter)
return FarEnd.VLANFromSwitchPort(hostname);
//if it is a hub, return invalid
if (FarEnd.myType == NetworkComponentType.net_hub)
return NB.InvalidVLAN;
//if it is a wireless device, find the vlan the device is attached to.
if (FarEnd.IsWirelessForwarder())
{
return FarEnd.PrimaryVLAN();
}
return NB.InvalidVLAN;
}
/// <summary>
/// Used by PrimaryVLAN
/// </summary>
/// <param name="tList"></param>
/// <returns></returns>
private int ProcessVLANlist(List<NetworkCard> tList)
{
int VLAN = NB.InvalidVLAN;
int tvlan = NB.InvalidVLAN;
foreach(NetworkCard nic in tList)
{
tvlan = VLANFromNIC(nic);
if(VLAN != NB.InvalidVLAN && tvlan != VLAN)
{
//We have multiple VLANs defined on these nics. We do not know what is primary
return NB.InvalidVLAN;
}
VLAN = tvlan;
}
return VLAN;
}
/// <summary>
/// Return the primary vlan of the device.
/// </summary>
/// <returns></returns>
public int PrimaryVLAN()
{
int tVLAN;
List<NetworkCard> Connections;
Connections = NICsFromTypeNic(NicType.management_interface);
tVLAN = ProcessVLANlist(Connections);
if (tVLAN != NB.InvalidVLAN) return tVLAN;
//Connections = NICsFromTypeNic(NicType.wan); //If the device has a wan vlan defined
//tVLAN = ProcessVLANlist(Connections);
//if (tVLAN != NB.InvalidVLAN) return tVLAN;
Connections = NICsFromTypeNic(NicType.eth);
tVLAN = ProcessVLANlist(Connections);
if (tVLAN != NB.InvalidVLAN) return tVLAN;
Connections = NICsFromTypeNic(NicType.wlan);
tVLAN = ProcessVLANlist(Connections);
if (tVLAN != NB.InvalidVLAN) return tVLAN;
return NB.InvalidVLAN;
}
/// <summary>
/// On a switch, return the vlan that hostname is attached to. Returns invalid if not found on this switch
/// </summary>
/// <param name="hostname">the hostname of the device</param>
/// <returns>nb.invalid if it is not attached to this switch, or if this is not a switch</returns>
public int VLANFromSwitchPort(string hostname)
{
if (myType == NetworkComponentType.net_hub) return NB.InvalidVLAN; //Hubs do not know what vlan they are on
if(myType == NetworkComponentType.net_switch || myType == NetworkComponentType.wrouter)
{
//We want to find the port the hostname is on.
foreach(NetworkCard nic in NICs)
{
NetworkDevice FarEnd = ConnectedTo(nic); //Get the device at the far end
if (FarEnd == null) continue;
if(FarEnd.hostname == hostname)
{
//This is the port the host is on
return VLANFromNIC(nic);
}
}
}
return NB.InvalidVLAN;
}
public int VLANFromIP(string ip)
{
NetworkInterface nif = InterfaceFromIP(ip);
if (nif == null) return NB.InvalidVLAN; //Nothing
return VLANFromNIF(nif);
}
/// <summary>
/// Check to see if the device has a link to the specified host
/// </summary>
@ -1211,6 +1344,32 @@ namespace EduNetworkBuilder
}
return null;
}
public NetworkInterface InterfaceFromIP(string IP)
{
foreach (NetworkCard nic in NICs)
{
foreach (NetworkInterface nif in nic.interfaces)
{
if (nif.myIP.GetIPString == IP)
return nif;
}
}
return null;
}
public NetworkCard NicFromIP(string IP)
{
foreach (NetworkCard nic in NICs)
{
foreach(NetworkInterface nif in nic.interfaces)
{
if (nif.myIP.GetIPString == IP)
return nic;
}
}
return null;
}
public NetworkCard NicFromID(HostNicID ID)
{
return NicFromID(ID.NicID);
@ -2837,6 +2996,19 @@ namespace EduNetworkBuilder
return null;
}
private List<NetworkCard> NICsFromTypeNic(NicType What)
{
List<NetworkCard> tList = new List<NetworkCard>();
foreach (NetworkCard nic in NB.Randomize(NICs).ToList())
{
if (nic.GetNicType == What)
{
tList.Add(nic);
}
}
return tList;
}
public NB_IPAddress BestIPForThis(NetworkDevice Source=null)
{
NB_IPAddress answer=null;