/// <summary> /// Obtain list of monitored decvices that are to be included in list of network adapters that can be enabled/disabled /// </summary> /// <returns>Array of Adapter Names to be monitored</returns> private static MonitoredDevice[] ReadMonitoredDevicesFromConfig() { const string MemberName = "ReadMonitoredDevicesFromConfig"; using (new Tracer(MemberName)) { List <MonitoredDevice> monitoredDevices = new List <MonitoredDevice>(); try { MonitoredDevicesSection mds = (MonitoredDevicesSection)ConfigurationManager.GetSection("MonitoredDevices"); for (int i = 0; i < mds.Items.Count; i++) { MonitoredDevice monitoredDevice = new MonitoredDevice(mds.Items[i].Device, mds.Items[i].PnPDevice, mds.Items[i].DeviceType); monitoredDevices.Add(monitoredDevice); } } catch (Exception ex) { //Do not throw an exception just log it ConnectionMonitorException cme = new ConnectionMonitorException( "No Monitored Devices defined in the configuration file! " + MemberName, ex); Logger.Write(cme); } return(monitoredDevices.ToArray()); } }
/// <summary> /// Obtain list of VPN Adapter Descriptions that must be ignored because /// they errantly present a NetworkInterfaceType of "Ethernet" /// /// Note: We have included hard-coded exceptions for Cisco and Juniper /// because they are well known. However, we have also "policy-enabled" /// this method. If we find a policy-managed list of exceptions in the /// registry, we will ignore them, as well. /// /// HKLM\SOFTWARE\Policies\Microsoft\ConnectionMonitor\VPNExceptionsList /// </summary> /// <param></param> /// <returns>List of Errant VPN Adapter Descriptions.</returns> private static string GetVPNExceptionList() { const string MemberName = "GetVPNExceptionList"; using (new Tracer(MemberName)) { StringBuilder exceptionList = new StringBuilder(); try { DataSection ds = (DataSection)ConfigurationManager.GetSection("VPNExceptionList"); for (int i = 0; i < ds.Items.Count; i++) { if (i > 0) { exceptionList.Append(","); } exceptionList.Append(ds.Items[i].Data); } } catch (Exception ex) { //Do not throw an exception just log it ConnectionMonitorException cme = new ConnectionMonitorException( "No VPN Exceptions defined in the configuration file! " + MemberName, ex); Logger.Write(cme); } try { // See if the VPN Exceptions are being managed by policy string keyName = "HKEY_LOCAL_MACHINE\\SOFTWARE\\Policies\\Microsoft\\ConnectionMonitor"; string valueName = "VPNExceptionsList"; string vpnExceptionList = (string)Registry.GetValue(keyName, valueName, string.Empty); if (!String.IsNullOrEmpty(vpnExceptionList)) { //Add the policy exceptions if (exceptionList.Length > 0) { exceptionList.Append(","); } exceptionList.Append(vpnExceptionList); } } catch (ConnectionMonitorException cme) { Logger.Write(cme); throw cme; } catch (Exception ex) { Logger.Write(ex); throw ex; } return(exceptionList.ToString()); } }
/// <summary> /// Starts each dependent service listed in the configuration file /// </summary> void CheckForDependentServices() { const string MemberName = "CheckForDependentServices"; try { List <string> dependentSevices = new List <string>(); DataSection ds = (DataSection)ConfigurationManager.GetSection("DependentServiceList"); if (ds.Items == null || ds.Items.Count == 0) { ConnectionMonitorException cme = new ConnectionMonitorException( "No Dependent services defined in the configuration file! " + MemberName); } for (int i = 0; i < ds.Items.Count; i++) { string name = ds.Items[i].Data; try { ServiceHelper.StartService(name, name); dependentSevices.Add(name); } catch (Exception ex) { ConnectionMonitorException cme = new ConnectionMonitorException( "Cannot start dependent service " + name, ex); Logger.Write(cme); throw cme; } } if (dependentSevices != null && dependentSevices.Count > 0) { this.EnsureWCFClient(); this.ConMonServiceEventsWCFServiceClient.DependentServicesChecked(dependentSevices.ToArray()); } } catch (ConnectionMonitorException cme) { Logger.Write(cme); throw cme; } catch (Exception ex) { Logger.Write(ex); throw; } }
/// <summary> /// The main entry point for the application. /// </summary> static void Main(string[] args) { const string MemberName = "Main"; using (new Tracer(MemberName)) { try { if (args.Length > 0) { if (String.Compare(args[0], "-regadapters", true) == 0 || String.Compare(args[0], "/regadapters", true) == 0) { ConMonInstaller installer = new ConMonInstaller(); installer.UpdateAppConfigFile(); } } else { ServiceBase[] ServicesToRun; // More than one user Service may run within the same process. To add // another service to this process, change the following line to // create a second service object. For example, // // ServicesToRun = new ServiceBase[] {new Service1(), new MySecondUserService()}; // ServicesToRun = new ServiceBase[] { new ConMonService() }; ServiceBase.Run(ServicesToRun); } } catch (ConnectionMonitorException cme) { throw cme; } catch (Exception ex) { ConnectionMonitorException cme = new ConnectionMonitorException( "Exception caught in " + MemberName, ex); Logger.Write(ex); throw ex; } } }
/// <summary> /// Fired when the service is started /// </summary> /// <param name="args"></param> protected override void OnStart(string[] args) { const string MemberName = "OnStart"; using (new Tracer(MemberName)) { try { this.InitializeWCFServiceEventPublisher(); this.ConMonServiceEventsWCFServiceClient.ServiceStarted(); LogMessage("Service Started", TraceEventType.Information); //Set this service to delay start ServiceHelper.SetServiceStartupType("ConMon", "Connection Monitor", "delayed-auto"); //Check for services that need to be started CheckForDependentServices(); PopulateWirelessNicsAndController(false); Thread _networkEvalThread = new Thread(new ParameterizedThreadStart(ConMonService.ThreadStart)); _networkEvalThread.Start(this); BindEvent(); } catch (ConnectionMonitorException cme) { Logger.Write(cme); } catch (Exception ex) { ConnectionMonitorException cme = new ConnectionMonitorException( "Exception caught in " + MemberName, ex); Logger.Write(ex); } } }