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

@ -35,7 +35,7 @@ namespace EduNetworkBuilder
public bool IsBurned = false;
public Color BackgroundColor = Color.Empty;
protected Color MorphColor = Color.Empty;
public List<FirewallRule> FirewallRules = new List<FirewallRule>();
public List<FirewallRule> FirewallRules = new List<FirewallRule>();
public bool PowerOff = false;
private List<string> tracertmessages = new List<string>();
public int BadSprayCount = 0;
@ -49,7 +49,7 @@ namespace EduNetworkBuilder
myType = what;
SetImageFromType(what);
NetworkCard tnic = new NetworkCard(0,GetUniqueIdentifier,hostname,NicType.lo);
NetworkCard tnic = new NetworkCard(0, GetUniqueIdentifier, hostname, NicType.lo);
NICs.Add(tnic);
if (what == NetworkComponentType.net_switch || what == NetworkComponentType.net_hub)
{
@ -58,7 +58,7 @@ namespace EduNetworkBuilder
tnic = new NetworkCard(0, GetUniqueIdentifier, hostname, NicType.management_interface);
NICs.Add(tnic);
}
for(int looper=1; looper < 8; looper++)
for (int looper = 1; looper < 8; looper++)
{
tnic = new NetworkCard(looper, GetUniqueIdentifier, hostname, NicType.port);
NICs.Add(tnic);
@ -71,16 +71,16 @@ namespace EduNetworkBuilder
tnic = new NetworkCard(0, GetUniqueIdentifier, hostname);
NICs.Add(tnic);
}
if(what == NetworkComponentType.router)
if (what == NetworkComponentType.router)
{
tnic = new NetworkCard(1, GetUniqueIdentifier, hostname);
NICs.Add(tnic);
}
//**********Wireless***********
//Add a wireless network card to laptops, wireless bridge and wireless repeater
if (what == NetworkComponentType.laptop )
if (what == NetworkComponentType.laptop)
{
tnic = new NetworkCard(NICs.Count(), GetUniqueIdentifier, hostname,NicType.wlan); //Add a wireless nic
tnic = new NetworkCard(NICs.Count(), GetUniqueIdentifier, hostname, NicType.wlan); //Add a wireless nic
tnic.UsesDHCP = true;
NICs.Add(tnic);
}
@ -161,8 +161,8 @@ namespace EduNetworkBuilder
NICs.Add(tnic);
//Add one wan port
tnic = new NetworkCard(0, GetUniqueIdentifier, hostname, NicType.wan);
NICs.Insert(1,tnic); //Make this first in the list
}
NICs.Insert(1, tnic); //Make this first in the list
}
if (what == NetworkComponentType.cellphone || what == NetworkComponentType.tablet)
{
NICs.Clear();
@ -179,9 +179,9 @@ namespace EduNetworkBuilder
public int CountNics(NicType TheType)
{
int count = 0;
foreach(NetworkCard nic in NICs)
foreach (NetworkCard nic in NICs)
{
if(nic.GetNicType == TheType)
if (nic.GetNicType == TheType)
{
count++;
}
@ -197,7 +197,7 @@ namespace EduNetworkBuilder
string tname = TheType.ToString() + count.ToString();
foreach (NetworkCard nic in NICs)
{
if(nic.NicName() == tname)
if (nic.NicName() == tname)
{
found = true;
break;
@ -236,7 +236,7 @@ namespace EduNetworkBuilder
CanAddNics = true;
setNicsCanUseDHCP(false);
}
if (myType == NetworkComponentType.pc || myType == NetworkComponentType.laptop ||
if (myType == NetworkComponentType.pc || myType == NetworkComponentType.laptop ||
myType == NetworkComponentType.copier || myType == NetworkComponentType.printer ||
myType == NetworkComponentType.tablet || myType == NetworkComponentType.cellphone)
{
@ -276,7 +276,7 @@ namespace EduNetworkBuilder
private void setNicsCanUseDHCP(bool CanUse)
{
foreach(NetworkCard nic in NICs)
foreach (NetworkCard nic in NICs)
{
//if the device must use DHCP. we set it here.
if (MustUseDHCP && nic.CanUseDHCP)
@ -355,37 +355,37 @@ namespace EduNetworkBuilder
NetworkLink nl;
if (myNet != null)
{
foreach(NetworkCard nic in NICs)
{
if(nic.ConnectedLink != -1)
{
foreach (NetworkCard nic in NICs)
{
if (nic.ConnectedLink != -1)
{
nl = myNet.GetLinkFromID(nic.ConnectedLink);
if(nl != null)
if (nl != null)
{
myNet.RemoveComponent(nl);
nl.Destroy();
}
nic.ConnectedLink = -1;
}
}
}
}
}
}
public void EditGateway(Form ParentForm)
{
DefaultGW.Edit(this,ParentForm,NB.Translate("ND_EdtGteway"));
DefaultGW.Edit(this, ParentForm, NB.Translate("ND_EdtGteway"));
}
public NB_IPAddress GetGateway()
{
if(!HasNonDHCPConnected() && CanUseDHCP)
{
foreach(NetworkCard nic in NICs)
if (!HasNonDHCPConnected() && CanUseDHCP)
{
foreach (NetworkCard nic in NICs)
{
if(nic.UsesDHCP)
if (nic.UsesDHCP)
{
List<NB_IPAddress> tlist = nic.IPAddressList();
if(tlist.Count > 0)
if (tlist.Count > 0)
{
NB_IPAddress newIP = new NB_IPAddress(tlist[0].GetGateway.ToIpString());
return newIP;
@ -422,7 +422,7 @@ namespace EduNetworkBuilder
XmlNodeType myNodetype = Individual.NodeType;
if (myNodetype == XmlNodeType.Element)
{
switch(Individual.Name.ToLower())
switch (Individual.Name.ToLower())
{
case "location":
string[] pointstr = Individual.InnerText.Split(',');
@ -450,13 +450,13 @@ namespace EduNetworkBuilder
newNic = new NetworkCard(Individual);
//if (myType == NetworkComponentType.wap && newNic.GetNicType == NicType.eth)
//{
// //This happened once upon a time. Waps did not have management interfaces.
// //Make a management interface for the wap and move the IP from the eth to that.
// //Then, change the nic to a port instead of an eth.
// NetworkCard tNic = new NetworkCard(newNic);
// tNic.ChangeType(NicType.management_interface);
// NICs.Add(tNic);
// newNic.ChangeType(NicType.port);
// //This happened once upon a time. Waps did not have management interfaces.
// //Make a management interface for the wap and move the IP from the eth to that.
// //Then, change the nic to a port instead of an eth.
// NetworkCard tNic = new NetworkCard(newNic);
// tNic.ChangeType(NicType.management_interface);
// NICs.Add(tNic);
// newNic.ChangeType(NicType.port);
//}
NICs.Add(newNic);
break;
@ -474,7 +474,7 @@ namespace EduNetworkBuilder
bool.TryParse(Individual.InnerText, out isDHCPServer);
break;
case "dhcprange":
NB_IPAddress dhcpip = new NB_IPAddress(Individual);
NB_IPAddress dhcpip = new NB_IPAddress(Individual);
DHCPRanges.Add(dhcpip);
break;
case "hasadvfirewall":
@ -510,13 +510,13 @@ namespace EduNetworkBuilder
writer.WriteElementString("mytype", myType.ToString());
writer.WriteElementString("isdns", isDNSServer.ToString());
writer.WriteElementString("isdhcp", isDHCPServer.ToString());
if(IsBurned)
if (IsBurned)
writer.WriteElementString("isburned", IsBurned.ToString());
if (HasAdvFirewall)
writer.WriteElementString("hasadvfirewall", HasAdvFirewall.ToString());
if (MorphColor != Color.Empty)
if (MorphColor != Color.Empty)
writer.WriteElementString("morphcolor", MorphColor.Name);
if(PowerOff == true)
if (PowerOff == true)
writer.WriteElementString("poweroff", PowerOff.ToString());
if (isInvisible == true)
writer.WriteElementString("isinvisible", isInvisible.ToString());
@ -584,21 +584,21 @@ namespace EduNetworkBuilder
return NB.Translate("ND_MicrowaveTooltip");
foreach (NetworkCard nic in NICs)
{
if (nic.GetNicType == NicType.wport || nic.GetNicType == NicType.port)
if (nic.GetNicType == NicType.wport || nic.GetNicType == NicType.port)
continue;
foreach(string addr in nic.IPAddresses())
foreach (string addr in nic.IPAddresses())
{
answer += System.Environment.NewLine + addr;
answer += System.Environment.NewLine + addr;
}
}
if(BackgroundColor != Color.Empty)
if (BackgroundColor != Color.Empty)
{
//There is something wrong with it?
Network myNet = NB.GetNetwork();
List<string> DeviceMessages = myNet.GetTestMessages(hostname);
foreach (string tString in DeviceMessages)
{
answer += System.Environment.NewLine + tString;
answer += System.Environment.NewLine + tString;
}
}
return answer;
@ -607,18 +607,18 @@ namespace EduNetworkBuilder
public List<string> ListOfConnectedHosts()
{
List<string> thelist = new List<string>();
foreach(NetworkCard nic in NICs)
foreach (NetworkCard nic in NICs)
{
if(nic.ConnectedLink != -1)
if (nic.ConnectedLink != -1)
{
Network myNet = NB.GetNetwork();
NetworkLink Link = myNet.GetLinkFromID(nic.ConnectedLink);
if(Link != null)
if (Link != null)
{
NetworkDevice tmp = myNet.GetDeviceFromID(Link.Src);
if (tmp == null) continue;
if (tmp.hostname != hostname) thelist.Add(tmp.hostname);
tmp = myNet.GetDeviceFromID(Link.Dst);
tmp = myNet.GetDeviceFromID(Link.Dst);
if (tmp.hostname != hostname) thelist.Add(tmp.hostname);
}
}
@ -662,7 +662,7 @@ namespace EduNetworkBuilder
List<NetworkDevice> ConnectedTo()
{
List<NetworkDevice> connected = new List<NetworkDevice>();
foreach(NetworkCard nic in NICs)
foreach (NetworkCard nic in NICs)
{
NetworkDevice nd = ConnectedTo(nic);
if (nd != null) connected.Add(nd);
@ -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;