Skip to content

Commit

Permalink
closes #3 Load current settings on startup.
Browse files Browse the repository at this point in the history
Signed-off-by: D9ping <D9ping@users.noreply.github.com>
  • Loading branch information
D9ping committed Aug 6, 2016
1 parent 4dd2872 commit a11463d
Show file tree
Hide file tree
Showing 2 changed files with 202 additions and 51 deletions.
218 changes: 170 additions & 48 deletions IPv6Control/DisabledComponentValue.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security;
using System.Text;

namespace IPv6Control
{
class DisabledComponentValue
{
private int decvalue = 0;
private UInt32 decvalue = 0;
private bool docalculatevalue = false;

private bool disableAllTransitionTechnologies = false;
private bool disable6to4 = false;
private bool disableISATAP = false;
Expand All @@ -17,6 +16,21 @@ class DisabledComponentValue
private bool preferIPv4 = false;
private bool disableAllIpHttps = false;

/// <summary>
/// Creating a new instance of DisabledComponentValue class.
/// </summary>
public DisabledComponentValue()
{
}

public bool DoCalculateValue
{
set
{
this.docalculatevalue = value;
}
}

public bool DisableAllTransitionTechnologies
{
get
Expand All @@ -25,12 +39,16 @@ public bool DisableAllTransitionTechnologies
}
set
{
if (value)
{
this.decvalue += 1;
} else
if (this.docalculatevalue)
{
this.decvalue -= 1;
if (value)
{
this.decvalue += 1;
}
else
{
this.decvalue -= 1;
}
}

this.disableAllTransitionTechnologies = value;
Expand All @@ -45,14 +63,18 @@ public bool Disable6to4
}
set
{
if (value)
if (this.docalculatevalue)
{
this.decvalue += 2;
} else
{
this.decvalue -= 2;
if (value)
{
this.decvalue += 2;
}
else
{
this.decvalue -= 2;
}
}

this.disable6to4 = value;
}
}
Expand All @@ -65,13 +87,16 @@ public bool DisableISATAP
}
set
{
if (value)
{
this.decvalue += 4;
}
else
if (this.docalculatevalue)
{
this.decvalue -= 4;
if (value)
{
this.decvalue += 4;
}
else
{
this.decvalue -= 4;
}
}

this.disableISATAP = value;
Expand All @@ -86,13 +111,16 @@ public bool DisableTeredo
}
set
{
if (value)
{
this.decvalue += 8;
}
else
if (this.docalculatevalue)
{
this.decvalue -= 8;
if (value)
{
this.decvalue += 8;
}
else
{
this.decvalue -= 8;
}
}

this.disableTeredo = value;
Expand All @@ -107,13 +135,16 @@ public bool DisableIPv6OnAllNotTunnels
}
set
{
if (value)
if (this.docalculatevalue)
{
this.decvalue += 16;
}
else
{
this.decvalue -= 16;
if (value)
{
this.decvalue += 16;
}
else
{
this.decvalue -= 16;
}
}

this.disableIPv6OnAllNotTunnels = value;
Expand All @@ -128,13 +159,16 @@ public bool PreferIPv4
}
set
{
if (value)
{
this.decvalue += 32;
}
else
if (this.docalculatevalue)
{
this.decvalue -= 32;
if (value)
{
this.decvalue += 32;
}
else
{
this.decvalue -= 32;
}
}

this.preferIPv4 = value;
Expand All @@ -149,20 +183,23 @@ public bool DisableAllIpHttps
}
set
{
if (value)
if (this.docalculatevalue)
{
this.decvalue += 128;
}
else
{
this.decvalue -= 128;
if (value)
{
this.decvalue += 128;
}
else
{
this.decvalue -= 128;
}
}

this.disableAllIpHttps = value;
}
}

public int GetDecValue()
public UInt32 GetDecValue()
{
return this.decvalue;
}
Expand All @@ -179,6 +216,93 @@ public string GetHexdecValueStr()
return hexdec;
}

/// <summary>
/// Load the DisabledComponents registery value and set boolean properties of this class.
/// Administrator rights are required to read the registery value.
/// </summary>
/// <returns>True if loading from registery succesfull.</returns>
public bool LoadFromRegisteryValue()
{
Microsoft.Win32.RegistryKey keyip6param;
try
{
keyip6param = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SYSTEM\\CurrentControlSet\\Services\\Tcpip6\\Parameters", true);
}
catch (SecurityException)
{
return false;
}

if (keyip6param == null)
{
return false;
}

bool loadsucceeded = false;
try
{
Object objDisabledComponents = keyip6param.GetValue("DisabledComponents");
UInt32 newdecvalue;
if (UInt32.TryParse(objDisabledComponents.ToString(), out newdecvalue))
{
this.decvalue = newdecvalue;
if (newdecvalue - 128 <= 255)
{
newdecvalue -= 128;
this.DisableAllIpHttps = true;
}

if (newdecvalue - 32 <= 255)
{
newdecvalue -= 32;
this.preferIPv4 = true;
}

if (newdecvalue - 16 <= 255)
{
newdecvalue -= 16;
this.DisableIPv6OnAllNotTunnels = true;
}

if (newdecvalue - 8 <= 255)
{
newdecvalue -= 8;
this.DisableTeredo = true;
}

if (newdecvalue - 4 <= 255)
{
newdecvalue -= 4;
this.DisableISATAP = true;
}

if (newdecvalue - 2 <= 255)
{
newdecvalue -= 2;
this.Disable6to4 = true;
}

if (newdecvalue - 1 <= 255)
{
newdecvalue -= 1;
this.DisableAllTransitionTechnologies = true;
}

loadsucceeded = true;
}
} catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
}

return loadsucceeded;
}

/// <summary>
/// Write the DisabledComponents registery value.
/// Needs administrator rights to write the registery value.
/// </summary>
/// <returns>True if writing to registery is succesfull (needs administrator rights to do this).</returns>
public bool WriteRegisteryValue()
{
Microsoft.Win32.RegistryKey keyip6param;
Expand All @@ -199,13 +323,11 @@ public bool WriteRegisteryValue()
bool writesucceeded = true;
try
{
//keyip6param.GetValue()
keyip6param.SetValue("DisabledComponents", this.decvalue, Microsoft.Win32.RegistryValueKind.DWord);
}
catch (UnauthorizedAccessException)
{
writesucceeded = false;
//MessageBox.Show("No rights to edit registery key value", "No rights", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
Expand Down
35 changes: 32 additions & 3 deletions IPv6Control/FrmMain.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Security;
using System.Windows.Forms;

namespace IPv6Control
Expand All @@ -8,11 +7,25 @@ public partial class FrmMain : Form
{
private DisabledComponentValue disabledComponentValue;

/// <summary>
/// Creating a new instance of FrmMain class.
/// </summary>
public FrmMain()
{
InitializeComponent();
this.disabledComponentValue = new DisabledComponentValue();
this.Text = Program.AssemblyTitle + " v" + Program.AssemblyVersion;
if (this.disabledComponentValue.LoadFromRegisteryValue())
{
this.DisplayNewDisabledComponentValue();
this.SetCheckboxesByDisabledComponentValue();
}
else
{
MessageBox.Show("Could not load current settings.\r\nAdministrator rights are required to read to the registery key.");
}

this.disabledComponentValue.DoCalculateValue = true;
}

private void DisplayNewDisabledComponentValue()
Expand Down Expand Up @@ -64,18 +77,34 @@ private void chxDisableAllIpHttps_CheckedChanged(object sender, System.EventArgs

private void btnSave_Click(object sender, EventArgs e)
{
DialogResult result = MessageBox.Show("Are you sure you want to write the new DisabledComponents DWORD hexdec. value: "+this.lblDisabledComponentHEX.Text+" to Windows registery? A reboot is required for change to be applied.", "Apply settings?", MessageBoxButtons.YesNo);
DialogResult result = MessageBox.Show("Are you sure you want to write the new DisabledComponents DWORD hexdec. value: " + this.lblDisabledComponentHEX.Text + " to Windows registery?\r\nA reboot is required for change to be applied.",
"Apply settings?", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result == DialogResult.No)
{
return;
}

if (!this.disabledComponentValue.WriteRegisteryValue())
if (this.disabledComponentValue.WriteRegisteryValue())
{
MessageBox.Show("Writing settings succesfull.\r\nA reboot is required to apply new settings.", "Settings saved successfull", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("Cannot open registery key need administrator privilege.", "No administrator privilege", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}

private void SetCheckboxesByDisabledComponentValue()
{
this.chxDisable6to4.Checked = this.disabledComponentValue.Disable6to4;
this.chxDisableAllIpHttps.Checked = this.disabledComponentValue.DisableAllIpHttps;
this.chxDisableAllTransitionTechnologies.Checked = this.disabledComponentValue.DisableAllTransitionTechnologies;
this.chxDisableIPv6OnAllNotTunnels.Checked = this.disabledComponentValue.DisableIPv6OnAllNotTunnels;
this.chxDisableISATAP.Checked = this.disabledComponentValue.DisableISATAP;
this.chxDisableTeredo.Checked = this.disabledComponentValue.DisableTeredo;
this.chxPreferIPv4.Checked = this.disabledComponentValue.PreferIPv4;
}

private void CheckboxesEnabledAll(bool enabled)
{
this.chxDisable6to4.Enabled = enabled;
Expand Down

0 comments on commit a11463d

Please sign in to comment.