Progress on vlans

This commit is contained in:
Tim Young 2016-10-05 12:41:06 -05:00
parent 49c771fff2
commit 99d7cdfa85
4 changed files with 234 additions and 26 deletions

View File

@ -50,8 +50,8 @@ namespace EduNetworkBuilder
private int DefaultTimeout = 10;
private int NumberOfSecondsForTimeout = 10;
private List<Rectangle> PacketRectangles = new List<Rectangle>();
public List<VLANName> VlanNames = new List<VLANName>() { new VLANName(1,"Default") };
public BindingList<VLANName> VlanNames = new BindingList<VLANName>() { 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;

View File

@ -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");

View File

@ -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
//

View File

@ -17,45 +17,57 @@ namespace EduNetworkBuilder
BindingList<VLANName> VLANNames = new BindingList<VLANName>();
DataTable VlanBindings = new DataTable();
List<NetworkInterface> interfaces = new List<NetworkInterface>();
BindingList<VLANName> StashedVLANNameList = new BindingList<VLANName>();
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<VLANName> tList = theNet.VlanNames.OrderBy(x => x.ID).ToList();
StashedVLANNameList = new BindingList<VLANName>(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<string> 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<VLANName> 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<VLANName> tempitem = new BindingList<VLANName>();
VLANName current = ((BindingList<VLANName>)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<VLANName>)dgv_VLANNames.DataSource)[e.RowIndex] = new VLANName { ID = id, Name = value };
//BindingList<VLANName> tempitem = new BindingList<VLANName>();
//VLANName current = ((BindingList<VLANName>)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<VLANName>)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<string> 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<VLANTagType>(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();
}
}
}