From 99d7cdfa850b4363a935496cc389306ae4bdc138 Mon Sep 17 00:00:00 2001 From: Tim Young Date: Wed, 5 Oct 2016 12:41:06 -0500 Subject: [PATCH] Progress on vlans --- EduNetworkBuilder/Network.cs | 14 +- EduNetworkBuilder/NetworkInterface.cs | 20 ++ EduNetworkBuilder/VLANConfig.Designer.cs | 4 + EduNetworkBuilder/VLANConfig.cs | 222 ++++++++++++++++++++--- 4 files changed, 234 insertions(+), 26 deletions(-) diff --git a/EduNetworkBuilder/Network.cs b/EduNetworkBuilder/Network.cs index 652db86..1e4616c 100644 --- a/EduNetworkBuilder/Network.cs +++ b/EduNetworkBuilder/Network.cs @@ -50,8 +50,8 @@ namespace EduNetworkBuilder private int DefaultTimeout = 10; private int NumberOfSecondsForTimeout = 10; private List PacketRectangles = new List(); - public List VlanNames = new List() { new VLANName(1,"Default") }; - + public BindingList VlanNames = new BindingList() { new VLANName(1,"Default") }; + private bool previously_had_packets = false; //used on "tick" to determine if we are starting from scratch public Network(string Name) @@ -1346,6 +1346,16 @@ namespace EduNetworkBuilder return didanything; } + public int VLANIDFromName(string name) + { + foreach(VLANName VN in VlanNames) + { + if (VN.Name.Equals(name)) + return VN.ID; + } + return -1; + } + public void DoAllClearArp() { NetworkDevice nd; diff --git a/EduNetworkBuilder/NetworkInterface.cs b/EduNetworkBuilder/NetworkInterface.cs index 250e263..7ef0e92 100644 --- a/EduNetworkBuilder/NetworkInterface.cs +++ b/EduNetworkBuilder/NetworkInterface.cs @@ -72,6 +72,26 @@ namespace EduNetworkBuilder return VLANTagType.Forbidden; //if not defined, it is forbidden } + public void SetVLANTag(int id, VLANTagType Tag) + { + bool foundit = false; + foreach (VLANInfo one in VLANs) + { + if (one.ID == id) + { + one.Tag = Tag; + foundit = true; + } + else + { + if (Tag == VLANTagType.Untagged && one.Tag == VLANTagType.Untagged) + one.Tag = VLANTagType.Tagged; + } + } + if (!foundit) + VLANs.Add(new VLANInfo(id, Tag)); + } + public void Save(XmlWriter writer) { writer.WriteStartElement("interface"); diff --git a/EduNetworkBuilder/VLANConfig.Designer.cs b/EduNetworkBuilder/VLANConfig.Designer.cs index 26164f9..02b3249 100644 --- a/EduNetworkBuilder/VLANConfig.Designer.cs +++ b/EduNetworkBuilder/VLANConfig.Designer.cs @@ -49,6 +49,7 @@ this.dgv_VLANAssignments.RowTemplate.Height = 24; this.dgv_VLANAssignments.Size = new System.Drawing.Size(313, 195); this.dgv_VLANAssignments.TabIndex = 0; + this.dgv_VLANAssignments.CellValueChanged += new System.Windows.Forms.DataGridViewCellEventHandler(this.dgv_VLANAssignments_CellValueChanged); // // dgv_VLANNames // @@ -61,6 +62,9 @@ this.dgv_VLANNames.Size = new System.Drawing.Size(313, 151); this.dgv_VLANNames.TabIndex = 1; this.dgv_VLANNames.CellParsing += new System.Windows.Forms.DataGridViewCellParsingEventHandler(this.dgv_VLANNames_CellParsing); + this.dgv_VLANNames.CellValidating += new System.Windows.Forms.DataGridViewCellValidatingEventHandler(this.dgv_VLANNames_CellValidating); + this.dgv_VLANNames.CellValueChanged += new System.Windows.Forms.DataGridViewCellEventHandler(this.dgv_VLANNames_CellValueChanged); + this.dgv_VLANNames.UserDeletedRow += new System.Windows.Forms.DataGridViewRowEventHandler(this.dgv_VLANNames_UserDeletedRow); // // btnDone // diff --git a/EduNetworkBuilder/VLANConfig.cs b/EduNetworkBuilder/VLANConfig.cs index e5754f6..331c8c2 100644 --- a/EduNetworkBuilder/VLANConfig.cs +++ b/EduNetworkBuilder/VLANConfig.cs @@ -17,45 +17,57 @@ namespace EduNetworkBuilder BindingList VLANNames = new BindingList(); DataTable VlanBindings = new DataTable(); List interfaces = new List(); + BindingList StashedVLANNameList = new BindingList(); + + bool processing = false; public VLANConfig(NetworkDevice ToEdit) { + processing = true; InitializeComponent(); TheDevice = ToEdit; - LocalSetup(); + this.Icon = Properties.Resources.NBIco; + processing = false; + + UpdateForm(); } - private void LocalSetup() + private void UpdateForm() { + if (processing) return; + + UpdateNamesFromForm(); //Read in any changes so far + + processing = true; //Bind the vlan names Network theNet = NB.GetNetwork(); - //var _VLANNames = from row in theNet.VlanNames select new { VLAN_ID = row.Key, VLAN_Name = row.Value }; - //dgv_VLANNames.DataSource = _VLANNames.ToArray(); - theNet.VlanNames.Sort((x, y) => x.ID.CompareTo(y.ID)); + //theNet.VlanNames.Sort((x, y) => x.ID.CompareTo(y.ID)); + List tList = theNet.VlanNames.OrderBy(x => x.ID).ToList(); + StashedVLANNameList = new BindingList(tList); - dgv_VLANNames.DataSource = theNet.VlanNames; - //dgv_VLANNames.Columns.Add("ID", "ID"); - //dgv_VLANNames.Columns.Add("Name", "Name"); + dgv_VLANNames.DataSource = StashedVLANNameList; dgv_VLANNames.AllowUserToAddRows = true; - VlanBindings.Clear(); + VlanBindings.Columns.Clear(); VlanBindings.Columns.Add("IF", typeof(string)); - foreach(VLANName vn in theNet.VlanNames) + foreach (VLANName vn in theNet.VlanNames) { VlanBindings.Columns.Add(vn.Name, typeof(string)); } + + VlanBindings.Rows.Clear(); List theNics = TheDevice.NICNames(); - foreach(string oneNic in theNics) + foreach (string oneNic in theNics) { NetworkCard NIC = TheDevice.NicFromName(oneNic); - if (NIC.NicName() == NicType.lo.ToString()) continue; - if (NIC.NicName() == NicType.management_interface.ToString()) continue; - for (int i=0; i< NIC.IFCount; i++) + if (NIC.GetNicType == NicType.lo) continue; + if (NIC.GetNicType == NicType.management_interface) continue; + for (int i = 0; i < NIC.IFCount; i++) { - NetworkInterface nif = NIC.GetInterface(i); + NetworkInterface nif = NIC.GetInterface(i); DataRow DR = VlanBindings.NewRow(); DR["IF"] = nif.nic_name; @@ -67,6 +79,7 @@ namespace EduNetworkBuilder } } dgv_VLANAssignments.AutoGenerateColumns = false; + dgv_VLANAssignments.Columns.Clear(); //dgv_VLANAssignments.Columns.Add("IF","IF"); DataGridViewTextBoxColumn scol = new DataGridViewTextBoxColumn(); @@ -93,8 +106,67 @@ namespace EduNetworkBuilder } dgv_VLANAssignments.DataSource = VlanBindings; + int many = theNet.VlanNames.Count; + if (many > 3) many = 3; + int checksize = 50 + 120 + (many * 80) + 25; //the record indicator, and the size of the name. + width of each item + if (Width < checksize) Width = checksize; //Make it fit better + processing = false; } + bool NamesGridHasID(int id) + { + string IDString = id.ToString(); + foreach (DataRow DR in dgv_VLANNames.Rows) + { + if (DR["ID"].ToString() == IDString) + return true; + } + return false; + } + + bool VLANNamesHasID(BindingList names, int id) + { + foreach (VLANName VN in names) + { + if (VN.ID == id) + return true; + } + return false; + } + + void UpdateNamesFromForm() + { + Network theNet = NB.GetNetwork(); + if (dgv_VLANNames.RowCount == 0) return;//If we have not put anything in it yet. + + //if (dgv_VLANNames.RowCount == theNet.VlanNames.Count) return; //Nothing was added or removed. + + theNet.VlanNames.Clear(); //Remove them all. We will re-add them + + //add it if it was added + int id; + foreach (DataGridViewRow DR in dgv_VLANNames.Rows) + { + id = -1; + if (DR.Cells["ID"] == null || DR.Cells["ID"].Value == null) continue; + + int.TryParse(DR.Cells["ID"].Value.ToString(), out id); + if (id > 0) + { + if (!VLANNamesHasID(theNet.VlanNames, id)) + { + if (DR.Cells["Name"].Value == null) //When we are still adding + theNet.VlanNames.Add(new VLANName(id, "----")); + else + theNet.VlanNames.Add(new VLANName(id, DR.Cells["Name"].Value.ToString())); + } + } + } + + if (!VLANNamesHasID(theNet.VlanNames, 1)) + theNet.VlanNames.Add(new VLANName(1, "Default")); + + } private void btnDone_Click(object sender, EventArgs e) { @@ -104,16 +176,118 @@ namespace EduNetworkBuilder private void dgv_VLANNames_CellParsing(object sender, DataGridViewCellParsingEventArgs e) { - // BindingList tempitem = new BindingList(); - VLANName current = ((BindingList)dgv_VLANNames.DataSource)[e.RowIndex]; - string key = current.ID.ToString(); - int id = current.ID; - string value = current.Name; - string cellValue = e.Value.ToString(); - if (e.ColumnIndex == 0) key = cellValue; - else value = cellValue; - ((BindingList)dgv_VLANNames.DataSource)[e.RowIndex] = new VLANName { ID = id, Name = value }; + //BindingList tempitem = new BindingList(); + //VLANName current = ((BindingList)dgv_VLANNames.DataSource)[e.RowIndex]; + //string key = current.ID.ToString(); + //int id = current.ID; + //string value = current.Name; + //string cellValue = e.Value.ToString(); + //if (e.ColumnIndex == 0) key = cellValue; + //else value = cellValue; + //((BindingList)dgv_VLANNames.DataSource)[e.RowIndex] = new VLANName { ID = id, Name = value }; } + private void dgv_VLANNames_CellValueChanged(object sender, DataGridViewCellEventArgs e) + { + if (!processing) + UpdateForm(); + } + + private void dgv_VLANNames_RowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e) + { + //if (!processing) + //{ + // UpdateForm(); + //} + } + + private void dgv_VLANNames_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) + { + string headerText = + dgv_VLANNames.Columns[e.ColumnIndex].HeaderText; + if (processing) return; + + dgv_VLANNames.Rows[e.RowIndex].ErrorText = ""; //clear the old one out + Network theNet = NB.GetNetwork(); + + if (headerText.Equals("ID")) + { + // Confirm that the cell is not empty. + if (string.IsNullOrEmpty(e.FormattedValue.ToString())) + { + dgv_VLANNames.Rows[e.RowIndex].ErrorText = + "The ID cannot be empty"; + e.Cancel = true; + return; + } + else + { + + int value; + int.TryParse(e.FormattedValue.ToString(), out value); + if (value <= 0) + { + dgv_VLANNames.Rows[e.RowIndex].ErrorText = + "The ID cannot be negative or zero"; + e.Cancel = true; + return; + } + //Verify that the ID is unique. it is a zero-based index + for (int i = 0; i < StashedVLANNameList.Count; i++) + { + if (e.RowIndex == i) continue; //do not check ourself + if (StashedVLANNameList[i].ID == value) + { + dgv_VLANNames.Rows[e.RowIndex].ErrorText = + "You cannot duplicate VLAN IDs. They must be unique"; + e.Cancel = true; + return; + } + } + //MessageBox.Show(e.RowIndex.ToString() + " out of " + StashedVLANNameList.Count); + + } + } + } + + private void dgv_VLANNames_UserDeletedRow(object sender, DataGridViewRowEventArgs e) + { + if (!processing) + { + UpdateForm(); + } + } + + private void SetIFVLAN(string ifname, int ID, VLANTagType Tag) + { + List theNics = TheDevice.NICNames(); + + foreach (string oneNic in theNics) + { + NetworkCard NIC = TheDevice.NicFromName(oneNic); + if (NIC.GetNicType == NicType.lo) continue; + if (NIC.GetNicType == NicType.management_interface) continue; + for (int i = 0; i < NIC.IFCount; i++) + { + NetworkInterface nif = NIC.GetInterface(i); + if (nif.nic_name == ifname) + nif.SetVLANTag(ID, Tag); + } + } + } + + private void dgv_VLANAssignments_CellValueChanged(object sender, DataGridViewCellEventArgs e) + { + //We are working on a vlan tag. + Network theNet = NB.GetNetwork(); + if (e.ColumnIndex == 0) return; //we should never get here. + + VLANTagType VTT = NB.TryParseEnum(dgv_VLANAssignments.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString(), VLANTagType.Forbidden); + string Which = dgv_VLANAssignments.Rows[e.RowIndex].Cells[0].Value.ToString(); + string VLANName = dgv_VLANAssignments.Columns[e.ColumnIndex].HeaderText; + int ID = theNet.VLANIDFromName(VLANName); + SetIFVLAN(Which, ID, VTT); + UpdateForm(); + } } }