Ejemplo n.º 1
0
        // Updates the active/inactive status of all configured profiles, based on current network status.
        private void UpdateProfileStatus(bool force = false)
        {
            // Track whether anything changed, to avoid unnecessary GUI updates when nothing has changed.
            bool anything_changed = false;

            // Search all profiles
            foreach (var p in Profiles)
            {
                // Search all network interfaces
                bool found = false;
                for (int i = 0; (!found) && (i < dgNetworks.RowCount); i++)
                {
                    // Check whether the current network interface matches the configured
                    // recognition method for the profile.
                    switch (p.RecMethod)
                    {
                    case RecognizerMethod.DNS_Suffix:
                        if (NullSafeCompare(dgNetworks.Rows[i].Cells["DNS_Suffix"].Value, p.RecData, CaseInsensitive: true))
                        {
                            found = true;
                        }
                        break;

                    case RecognizerMethod.WiFi_SSID:
                        if (NullSafeCompare(dgNetworks.Rows[i].Cells["WiFi_SSID"].Value, p.RecData))
                        {
                            found = true;
                        }
                        break;

                    case RecognizerMethod.Subnet:
                        string[] tokens = p.RecData.Split(new[] { "/" }, StringSplitOptions.None);
                        if ((tokens.Length == 2) && (dgNetworks.Rows[i].Cells["IP_Address"].Value != null))
                        {
                            // Check if the interface's IP address is within a given CIDR subnet
                            IPAddress aip = IPAddress.Parse(dgNetworks.Rows[i].Cells["IP_Address"].Value.ToString());
                            if (aip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                            {
                                IPAddress pip  = IPAddress.Parse(tokens[0]);
                                IPAddress mask = pip.BitsToNetmask(Convert.ToInt32(tokens[1]));
                                if (pip.IsInSameSubnet(aip, mask))
                                {
                                    found = true;
                                }
                            }
                        }
                        break;

                    case RecognizerMethod.NLA_Description:
                        if (NullSafeCompare(dgNetworks.Rows[i].Cells["NLA_Description"].Value, p.RecData))
                        {
                            found = true;
                        }
                        break;

                    case RecognizerMethod.NLA_Category:
                        if (NullSafeCompare(dgNetworks.Rows[i].Cells["NLA_Category"].Value, p.RecData))
                        {
                            found = true;
                        }
                        break;
                    }
                }
                // "found" is true if and only if p.Active should now become true.
                if (p.Active != found)
                {
                    anything_changed = true;
                    p.Active         = found;
                    // If we got here and p.Active is now true, then this was a false-to-true transition,
                    // and profile actions should now be run.
                    if (p.Active)
                    {
                        p.RunActions();
                    }
                }
            }
            // Only if anything changed, regenerate the active profiles list and re-draw the list in the GUI.
            if (anything_changed || force)
            {
                dgProfiles.SuspendDrawing();
                ActiveProfiles.Clear();
                foreach (var p in Profiles)
                {
                    if (p.Active)
                    {
                        ActiveProfiles.Add(p);
                    }
                }
                dgProfiles.ResumeDrawing();
            }
        }