public static void Load() { Config currentConfig; if (File.Exists("Network_Manager.xml")) { try { XmlSerializer reader = new XmlSerializer(typeof(Config)); using (StreamReader file = new StreamReader("Network_Manager.xml")) { currentConfig = (Config)reader.Deserialize(file); //Config defaultConfig = new Config(); //currentConfig = (Config)CheckIfNull(typeof(Config), currentConfig, defaultConfig); // not needed, XML does a nice job } } catch { DialogResult result = MessageBox.Show("Configuration file was corrupted.\n\nDo you want to reset it to default and lose all configurations?", "Config File Corrupted", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2, MessageBoxOptions.ServiceNotification); if (result == DialogResult.No) Global.Exit(); currentConfig = new Config(); File.Delete("Network_Manager.xml"); } } else currentConfig = new Config(); // load defaults if null if (currentConfig.SavedRoutes.Nodes.Count == 0) { currentConfig.SavedRoutes.Nodes.Add(new SavedRouteGroup()); currentConfig.SavedRoutes.Nodes[0].Name = "<Root Group>"; } if (currentConfig.InterfaceProfiles.Count == 0) { InterfaceProfile profile = new InterfaceProfile(); profile.Name = "Google DNS"; profile.IPv4DnsAddresses.Add("8.8.8.8"); profile.IPv4DnsAddresses.Add("8.8.4.4"); profile.IPv6DnsAddresses.Add("2001:4860:4860::8888"); profile.IPv6DnsAddresses.Add("2001:4860:4860::8844"); currentConfig.InterfaceProfiles.Add(profile); profile = new InterfaceProfile(); profile.Name = "Comodo Secure DNS"; profile.IPv4DnsAddresses.Add("8.26.56.26"); profile.IPv4DnsAddresses.Add("8.20.247.20"); currentConfig.InterfaceProfiles.Add(profile); profile = new InterfaceProfile(); profile.Name = "OpenDNS"; profile.IPv4DnsAddresses.Add("208.67.222.222"); profile.IPv4DnsAddresses.Add("208.67.220.220"); profile.IPv6DnsAddresses.Add("2620:0:ccc::2"); profile.IPv6DnsAddresses.Add("2620:0:ccd::2"); currentConfig.InterfaceProfiles.Add(profile); } if (currentConfig.LoadBalancer.IPv4LocalAddresses.Count == 0) currentConfig.LoadBalancer.IPv4LocalAddresses.Add(new NetworkInterface.IPHostAddress("192.168.200.2", "255.255.255.0")); if (currentConfig.LoadBalancer.IPv4GatewayAddresses.Count == 0) currentConfig.LoadBalancer.IPv4GatewayAddresses.Add(new NetworkInterface.IPGatewayAddress("192.168.200.1", 1)); if (currentConfig.LoadBalancer.IPv4DnsAddresses.Count == 0) currentConfig.LoadBalancer.IPv4DnsAddresses.Add("8.8.8.8"); // legacy upgrade support if (File.Exists("Network_Manager.ini")) { // recover Gadget settings string gadgetX = Kernel32.IniReadValue("Gadget", "GadgetX", "Network_Manager.ini"); string gadgetY = Kernel32.IniReadValue("Gadget", "GadgetY", "Network_Manager.ini"); string alwaysOnTop = Kernel32.IniReadValue("Gadget", "AlwaysOnTop", "Network_Manager.ini"); string checkForUpdates = Kernel32.IniReadValue("Gadget", "CheckForUpdates", "Network_Manager.ini"); string maxVerticalSlots = Kernel32.IniReadValue("Gadget", "MaxVerticalSlots", "Network_Manager.ini"); string maxHorizontalSlots = Kernel32.IniReadValue("Gadget", "MaxHorizontalSlots", "Network_Manager.ini"); if (Regex.IsMatch(gadgetX, @"^\d+$") && Regex.IsMatch(gadgetY, @"^\d+$")) currentConfig.Gadget.Location = new Point(int.Parse(gadgetX), int.Parse(gadgetY)); if (Regex.IsMatch(alwaysOnTop, @"^(0|1)$")) currentConfig.Gadget.AlwaysOnTop = alwaysOnTop == "1"; if (Regex.IsMatch(checkForUpdates, @"^(0|1)$")) currentConfig.Gadget.CheckForUpdates = checkForUpdates == "1"; if (Regex.IsMatch(maxVerticalSlots, @"^\d$")) currentConfig.Gadget.MaxVerticalSlots = int.Parse(maxVerticalSlots); if (Regex.IsMatch(maxHorizontalSlots, @"^\d$")) currentConfig.Gadget.MaxHorizontalSlots = int.Parse(maxHorizontalSlots); // recover routes string routeNames = Kernel32.IniReadValue("Routes", "RouteNames", "Network_Manager.ini"); if (routeNames.Contains('#')) { SavedRouteGroup group = new SavedRouteGroup(); group.Name = "Legacy recovered routes"; foreach (string routeName in routeNames.Split(new[] { '#' }, StringSplitOptions.RemoveEmptyEntries)) { string[] sRoute = Kernel32.IniReadValue("Routes", routeName, "Network_Manager.ini").Split(new[] { '#' }, StringSplitOptions.RemoveEmptyEntries); SavedRouteItem route = new SavedRouteItem(); route.Name = routeName; try { route.Destination = sRoute[0]; route.Prefix = sRoute[1]; route.Gateway = sRoute[2]; route.InterfaceGuid = Guid.Empty; route.Metric = ushort.Parse(sRoute[4]); route.IPVersion = 4; group.Nodes.Add(route); } catch { } } ((SavedRouteGroup)currentConfig.SavedRoutes.Nodes[0]).Nodes.Add(group); } // recover interface profiles // complete profiles string completeProfileNames = Kernel32.IniReadValue("ConfigInterfaceProfiles", "CompleteProfileNames", "Network_Manager.ini"); foreach (string profileName in completeProfileNames.Split(new [] {'#'}, StringSplitOptions.RemoveEmptyEntries)) { if (currentConfig.InterfaceProfiles.Any(i => i.Name == profileName)) continue; string[] sProfile = Kernel32.IniReadValue("ConfigInterfaceProfiles", profileName, "Network_Manager.ini").Split(new[] { '#' }, StringSplitOptions.RemoveEmptyEntries); InterfaceProfile profile = new InterfaceProfile(); profile.Name = profileName; try { profile.IPv4LocalAddresses.Add(new NetworkInterface.IPHostAddress(sProfile[0], sProfile[1])); profile.IPv4GatewayAddresses.Add(new NetworkInterface.IPGatewayAddress(sProfile[2], ushort.Parse(sProfile[3]))); profile.IPv4DnsAddresses.Add(sProfile[5]); //profile.DhcpEnabled = (NetworkInterface.Dhcp)(int.Parse(sProfile[4]) + int.Parse(sProfile[6])); profile.InterfaceMetric = int.Parse(sProfile[7]); profile.NetbiosEnabled = (NetworkInterface.Netbios)(int.Parse(sProfile[7]) == 1 ? 1 : 2); currentConfig.InterfaceProfiles.Add(profile); } catch { } } // IP profiles string ipProfileNames = Kernel32.IniReadValue("ConfigInterfaceProfiles", "IpProfileNames", "Network_Manager.ini"); foreach (string profileName in ipProfileNames.Split(new[] { '#' }, StringSplitOptions.RemoveEmptyEntries)) { if (currentConfig.InterfaceProfiles.Any(i => i.Name == profileName)) continue; string[] sProfile = Kernel32.IniReadValue("ConfigInterfaceProfiles", profileName, "Network_Manager.ini").Split(new[] { '#' }, StringSplitOptions.RemoveEmptyEntries); InterfaceProfile profile = new InterfaceProfile(); profile.Name = profileName; try { profile.IPv4LocalAddresses.Add(new NetworkInterface.IPHostAddress(sProfile[0], sProfile[1])); profile.IPv4GatewayAddresses.Add(new NetworkInterface.IPGatewayAddress(sProfile[2], ushort.Parse(sProfile[3]))); currentConfig.InterfaceProfiles.Add(profile); } catch { } } // DNS profiles string dnsProfileNames = Kernel32.IniReadValue("ConfigInterfaceProfiles", "DnsProfileNames", "Network_Manager.ini"); foreach (string profileName in dnsProfileNames.Split(new[] { '#' }, StringSplitOptions.RemoveEmptyEntries)) { if (currentConfig.InterfaceProfiles.Any(i => i.Name == profileName)) continue; string[] sProfile = Kernel32.IniReadValue("ConfigInterfaceProfiles", profileName, "Network_Manager.ini").Split(new[] { '#' }, StringSplitOptions.RemoveEmptyEntries); InterfaceProfile profile = new InterfaceProfile(); profile.Name = profileName; try { profile.IPv4DnsAddresses.Add(sProfile[0]); currentConfig.InterfaceProfiles.Add(profile); } catch { } } // Settings profiles string settingsProfileNames = Kernel32.IniReadValue("ConfigInterfaceProfiles", "SettingsProfileNames", "Network_Manager.ini"); foreach (string profileName in settingsProfileNames.Split(new[] { '#' }, StringSplitOptions.RemoveEmptyEntries)) { if (currentConfig.InterfaceProfiles.Any(i => i.Name == profileName)) continue; string[] sProfile = Kernel32.IniReadValue("ConfigInterfaceProfiles", profileName, "Network_Manager.ini").Split(new[] { '#' }, StringSplitOptions.RemoveEmptyEntries); InterfaceProfile profile = new InterfaceProfile(); profile.Name = profileName; try { profile.InterfaceMetric = int.Parse(sProfile[7]); profile.NetbiosEnabled = (NetworkInterface.Netbios)(int.Parse(sProfile[7]) == 1 ? 2 : 0); currentConfig.InterfaceProfiles.Add(profile); } catch { } } try { File.Move("Network_Manager.ini", "Network_Manager.old.ini"); } catch { } } Global.Config = currentConfig; }
/// <summary> /// Start Networking! If you want to use a static IP, make sure to set it before you start! /// Note: This call may block for up to 15 seconds waiting for the IP Address assignment. /// </summary> public static void Start(byte[] MacAddress, string name = "MIP", InterfaceProfile profile = InterfaceProfile.Hero_Socket1_ENC28) { // Setup connection from Profile switch (profile) { case InterfaceProfile.Hero_Socket1_ENC28: Start(MacAddress, name, SPI.SPI_module.SPI4, (Cpu.Pin)0x02, (Cpu.Pin)0x4A); // PA2=INT, PE10=CS break; case InterfaceProfile.Hero_Socket8_ENC28: Start(MacAddress, name, SPI.SPI_module.SPI4, (Cpu.Pin)0x20, (Cpu.Pin)0x34); // PC0=INT, PD14=CS break; default: Start(MacAddress, name, SPI.SPI_module.SPI4, (Cpu.Pin)0x02, (Cpu.Pin)0x4A); // PA2=INT, PE10=CS break; } }
public static void Load() { Config currentConfig; if (File.Exists("Network_Manager.xml")) { try { XmlSerializer reader = new XmlSerializer(typeof(Config)); using (StreamReader file = new StreamReader("Network_Manager.xml")) { XmlReader xmlReader = XmlReader.Create(file); currentConfig = (Config)reader.Deserialize(xmlReader); //Config defaultConfig = new Config(); //currentConfig = (Config)CheckIfNull(typeof(Config), currentConfig, defaultConfig); // not needed, XML does a nice job } } catch { DialogResult result = MessageBox.Show("Configuration file was corrupted.\n\nDo you want to reset it to default and lose all configurations?", "Config File Corrupted", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2, MessageBoxOptions.ServiceNotification); if (result == DialogResult.No) { Global.Exit(); } currentConfig = new Config(); File.Delete("Network_Manager.xml"); } } else { currentConfig = new Config(); } // load defaults if null if (currentConfig.SavedRoutes.Nodes.Count == 0) { currentConfig.SavedRoutes.Nodes.Add(new SavedRouteGroup()); currentConfig.SavedRoutes.Nodes[0].Name = "<Root Group>"; } if (currentConfig.InterfaceProfiles.Count == 0) { InterfaceProfile profile = new InterfaceProfile(); profile.Name = "Google DNS"; profile.IPv4DnsAddresses.Add("8.8.8.8"); profile.IPv4DnsAddresses.Add("8.8.4.4"); profile.IPv6DnsAddresses.Add("2001:4860:4860::8888"); profile.IPv6DnsAddresses.Add("2001:4860:4860::8844"); currentConfig.InterfaceProfiles.Add(profile); profile = new InterfaceProfile(); profile.Name = "Comodo Secure DNS"; profile.IPv4DnsAddresses.Add("8.26.56.26"); profile.IPv4DnsAddresses.Add("8.20.247.20"); currentConfig.InterfaceProfiles.Add(profile); profile = new InterfaceProfile(); profile.Name = "OpenDNS"; profile.IPv4DnsAddresses.Add("208.67.222.222"); profile.IPv4DnsAddresses.Add("208.67.220.220"); profile.IPv6DnsAddresses.Add("2620:0:ccc::2"); profile.IPv6DnsAddresses.Add("2620:0:ccd::2"); currentConfig.InterfaceProfiles.Add(profile); } if (currentConfig.LoadBalancer.IPv4LocalAddresses.Count == 0) { currentConfig.LoadBalancer.IPv4LocalAddresses.Add(new NetworkInterface.IPHostAddress("192.168.200.2", "255.255.255.0")); } if (currentConfig.LoadBalancer.IPv4GatewayAddresses.Count == 0) { currentConfig.LoadBalancer.IPv4GatewayAddresses.Add(new NetworkInterface.IPGatewayAddress("192.168.200.1", 1)); } if (currentConfig.LoadBalancer.IPv4DnsAddresses.Count == 0) { currentConfig.LoadBalancer.IPv4DnsAddresses.Add("8.8.8.8"); } // legacy upgrade support if (File.Exists("Network_Manager.ini")) { // recover Gadget settings string gadgetX = Kernel32.IniReadValue("Gadget", "GadgetX", "Network_Manager.ini"); string gadgetY = Kernel32.IniReadValue("Gadget", "GadgetY", "Network_Manager.ini"); string alwaysOnTop = Kernel32.IniReadValue("Gadget", "AlwaysOnTop", "Network_Manager.ini"); string checkForUpdates = Kernel32.IniReadValue("Gadget", "CheckForUpdates", "Network_Manager.ini"); string maxVerticalSlots = Kernel32.IniReadValue("Gadget", "MaxVerticalSlots", "Network_Manager.ini"); string maxHorizontalSlots = Kernel32.IniReadValue("Gadget", "MaxHorizontalSlots", "Network_Manager.ini"); if (Regex.IsMatch(gadgetX, @"^\d+$") && Regex.IsMatch(gadgetY, @"^\d+$")) { currentConfig.Gadget.Location = new Point(int.Parse(gadgetX), int.Parse(gadgetY)); } if (Regex.IsMatch(alwaysOnTop, @"^(0|1)$")) { currentConfig.Gadget.AlwaysOnTop = alwaysOnTop == "1"; } if (Regex.IsMatch(checkForUpdates, @"^(0|1)$")) { currentConfig.Gadget.CheckForUpdates = checkForUpdates == "1"; } if (Regex.IsMatch(maxVerticalSlots, @"^\d$")) { currentConfig.Gadget.MaxVerticalSlots = int.Parse(maxVerticalSlots); } if (Regex.IsMatch(maxHorizontalSlots, @"^\d$")) { currentConfig.Gadget.MaxHorizontalSlots = int.Parse(maxHorizontalSlots); } // recover routes string routeNames = Kernel32.IniReadValue("Routes", "RouteNames", "Network_Manager.ini"); if (routeNames.Contains('#')) { SavedRouteGroup group = new SavedRouteGroup(); group.Name = "Legacy recovered routes"; foreach (string routeName in routeNames.Split(new[] { '#' }, StringSplitOptions.RemoveEmptyEntries)) { string[] sRoute = Kernel32.IniReadValue("Routes", routeName, "Network_Manager.ini").Split(new[] { '#' }, StringSplitOptions.RemoveEmptyEntries); SavedRouteItem route = new SavedRouteItem(); route.Name = routeName; try { route.Destination = sRoute[0]; route.Prefix = sRoute[1]; route.Gateway = sRoute[2]; route.InterfaceGuid = Guid.Empty; route.Metric = ushort.Parse(sRoute[4]); route.IPVersion = 4; group.Nodes.Add(route); } catch { } } ((SavedRouteGroup)currentConfig.SavedRoutes.Nodes[0]).Nodes.Add(group); } // recover interface profiles // complete profiles string completeProfileNames = Kernel32.IniReadValue("ConfigInterfaceProfiles", "CompleteProfileNames", "Network_Manager.ini"); foreach (string profileName in completeProfileNames.Split(new [] { '#' }, StringSplitOptions.RemoveEmptyEntries)) { if (currentConfig.InterfaceProfiles.Any(i => i.Name == profileName)) { continue; } string[] sProfile = Kernel32.IniReadValue("ConfigInterfaceProfiles", profileName, "Network_Manager.ini").Split(new[] { '#' }, StringSplitOptions.RemoveEmptyEntries); InterfaceProfile profile = new InterfaceProfile(); profile.Name = profileName; try { profile.IPv4LocalAddresses.Add(new NetworkInterface.IPHostAddress(sProfile[0], sProfile[1])); profile.IPv4GatewayAddresses.Add(new NetworkInterface.IPGatewayAddress(sProfile[2], ushort.Parse(sProfile[3]))); profile.IPv4DnsAddresses.Add(sProfile[5]); //profile.DhcpEnabled = (NetworkInterface.Dhcp)(int.Parse(sProfile[4]) + int.Parse(sProfile[6])); profile.InterfaceMetric = int.Parse(sProfile[7]); profile.NetbiosEnabled = (NetworkInterface.Netbios)(int.Parse(sProfile[7]) == 1 ? 1 : 2); currentConfig.InterfaceProfiles.Add(profile); } catch { } } // IP profiles string ipProfileNames = Kernel32.IniReadValue("ConfigInterfaceProfiles", "IpProfileNames", "Network_Manager.ini"); foreach (string profileName in ipProfileNames.Split(new[] { '#' }, StringSplitOptions.RemoveEmptyEntries)) { if (currentConfig.InterfaceProfiles.Any(i => i.Name == profileName)) { continue; } string[] sProfile = Kernel32.IniReadValue("ConfigInterfaceProfiles", profileName, "Network_Manager.ini").Split(new[] { '#' }, StringSplitOptions.RemoveEmptyEntries); InterfaceProfile profile = new InterfaceProfile(); profile.Name = profileName; try { profile.IPv4LocalAddresses.Add(new NetworkInterface.IPHostAddress(sProfile[0], sProfile[1])); profile.IPv4GatewayAddresses.Add(new NetworkInterface.IPGatewayAddress(sProfile[2], ushort.Parse(sProfile[3]))); currentConfig.InterfaceProfiles.Add(profile); } catch { } } // DNS profiles string dnsProfileNames = Kernel32.IniReadValue("ConfigInterfaceProfiles", "DnsProfileNames", "Network_Manager.ini"); foreach (string profileName in dnsProfileNames.Split(new[] { '#' }, StringSplitOptions.RemoveEmptyEntries)) { if (currentConfig.InterfaceProfiles.Any(i => i.Name == profileName)) { continue; } string[] sProfile = Kernel32.IniReadValue("ConfigInterfaceProfiles", profileName, "Network_Manager.ini").Split(new[] { '#' }, StringSplitOptions.RemoveEmptyEntries); InterfaceProfile profile = new InterfaceProfile(); profile.Name = profileName; try { profile.IPv4DnsAddresses.Add(sProfile[0]); currentConfig.InterfaceProfiles.Add(profile); } catch { } } // Settings profiles string settingsProfileNames = Kernel32.IniReadValue("ConfigInterfaceProfiles", "SettingsProfileNames", "Network_Manager.ini"); foreach (string profileName in settingsProfileNames.Split(new[] { '#' }, StringSplitOptions.RemoveEmptyEntries)) { if (currentConfig.InterfaceProfiles.Any(i => i.Name == profileName)) { continue; } string[] sProfile = Kernel32.IniReadValue("ConfigInterfaceProfiles", profileName, "Network_Manager.ini").Split(new[] { '#' }, StringSplitOptions.RemoveEmptyEntries); InterfaceProfile profile = new InterfaceProfile(); profile.Name = profileName; try { profile.InterfaceMetric = int.Parse(sProfile[7]); profile.NetbiosEnabled = (NetworkInterface.Netbios)(int.Parse(sProfile[7]) == 1 ? 2 : 0); currentConfig.InterfaceProfiles.Add(profile); } catch { } } try { File.Move("Network_Manager.ini", "Network_Manager.old.ini"); } catch { } } Global.Config = currentConfig; }
/// <summary> /// Start Networking! If you want to use a static IP, make sure to set it before you start! /// Note: This call may block for up to 15 seconds waiting for the IP Address assignment. /// </summary> public static void Start(byte[] MacAddress, string name = "MIP", InterfaceProfile profile = InterfaceProfile.NetduinoGo_Socket3_ENC28) { // Setup connection from Profile switch (profile) { //TODO: Eventually, we'll want to allow any socket on the NetduinoGo to work. case InterfaceProfile.NetduinoGo_Socket3_ENC28: Start(MacAddress, name, SPI.SPI_module.SPI1, (Cpu.Pin)34, (Cpu.Pin)4); break; case InterfaceProfile.Cerb40_ENC28J60: case InterfaceProfile.Cerberus_Socket6_ENC28: case InterfaceProfile.CerbuinoBee_Socket1_ENC28: // MOSI(PB5), SCK(PB3), MISO(PB4), INT(PA14), CS(PA13) for CERB40 connection (RESET(PC1) was removed because it is unnecessary) Start(MacAddress, name, SPI.SPI_module.SPI1, (Cpu.Pin)14, (Cpu.Pin)13); break; case InterfaceProfile.NetduinoPlus2: // MOSI(PA7), SCK(PA5), MISO(PB4), INT(PA4), CS(PC8) Start(MacAddress, name, SPI.SPI_module.SPI1, (Cpu.Pin)4, (Cpu.Pin)38); break; default: Start(MacAddress, name, SPI.SPI_module.SPI2, (Cpu.Pin)14, (Cpu.Pin)13); break; } }