public int removeEntry(hostsentry he)
 {
     int iRet =0;
     try
     {
         string[] lVals = rKeyTCPIP.OpenSubKey(he.sHost, true).GetValueNames();
         foreach (string s in lVals)
         {
             rKeyTCPIP.DeleteValue(s);
         }
         try
         {
             rKeyTCPIP.DeleteValue("");//delete default value
         }
         catch (Exception) { }
         rKeyTCPIP.Close();
         rKeyTCPIP = null;
         if (myregistry.RegDelKey(regSubKey, he.sHost) == 0)
         {
             iRet = 1;
             this.allHosts.Remove(he.sHost);
         }
         rKeyTCPIP = Registry.LocalMachine.OpenSubKey(regSubKey, true);
     }
     catch (Exception ex)
     {
         System.Diagnostics.Debug.WriteLine("Exception in remove(): " + ex.Message);
     }
     return iRet;
 }
Beispiel #2
0
        public int removeEntry(hostsentry he)
        {
            int iRet = 0;

            try
            {
                string[] lVals = rKeyTCPIP.OpenSubKey(he.sHost, true).GetValueNames();
                foreach (string s in lVals)
                {
                    rKeyTCPIP.DeleteValue(s);
                }
                try
                {
                    rKeyTCPIP.DeleteValue("");//delete default value
                }
                catch (Exception) { }
                rKeyTCPIP.Close();
                rKeyTCPIP = null;
                if (myregistry.RegDelKey(regSubKey, he.sHost) == 0)
                {
                    iRet = 1;
                    this.allHosts.Remove(he.sHost);
                }
                rKeyTCPIP = Registry.LocalMachine.OpenSubKey(regSubKey, true);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("Exception in remove(): " + ex.Message);
            }
            return(iRet);
        }
Beispiel #3
0
        private void listHosts_SelectedIndexChanged(object sender, EventArgs e)
        {
            hostsentry he = (hostsentry)listHosts.SelectedItem;

            txtHost.Text = he.sHost;
            listIP.Items.Clear();
            if (he.ipAddress.Count > 0)
            {
                foreach (System.Net.IPAddress ip in he.ipAddress)
                {
                    listIP.Items.Add(ip.ToString());
                }
                listIP.SelectedIndex = 0;
            }
            if (he.ipAddress6.Count > 0)
            {
                foreach (System.Net.IPAddress ip in he.ipAddress6)
                {
                    listIP.Items.Add(ip.ToString());
                }
                listIP.SelectedIndex = 0;
            }

            listAliases.Items.Clear();
            if (he.aliases.Count > 0)
            {
                foreach (string s in he.aliases)
                {
                    listAliases.Items.Add(s);
                }
                listAliases.SelectedIndex = 0;
            }

            txtExpires.Text = he.expireTime.ToString();
        }
Beispiel #4
0
        private void btnNew_Click(object sender, EventArgs e)
        {
            hostsentry he = new hostsentry();

            he.sHost = "NEW ENTRY - PLEASE CHANGE";
            he.ipAddress.Add(new System.Net.IPAddress(0));
            listHosts.Items.Insert(0, he);
            listHosts.SelectedIndex = 0;
        }
Beispiel #5
0
        public hostsentry getEntry(string sHost)
        {
            hostsentry he = new hostsentry();

            //test if entry exists
            if (this.allHosts.TryGetValue(sHost, out he))
            {
                return(he);
            }
            return(he);
        }
Beispiel #6
0
        public int saveEntry(hostsentry he)
        {
            int         iRet    = 0;
            RegistryKey regWork = null;

            //create subkey if not exist
            try
            {
                regWork = rKeyTCPIP.CreateSubKey(he.sHost);
                List <byte> b = new List <byte>();
                foreach (System.Net.IPAddress ip in he.ipAddress)
                {
                    b.AddRange(ip.GetAddressBytes());   //can be ip4 or ip6
                    iRet++;
                }
                if (b.Count >= 4)
                {
                    regWork.SetValue("ipaddr", b.ToArray(), RegistryValueKind.Binary);
                }

                b.Clear();
                foreach (System.Net.IPAddress ip6 in he.ipAddress6)
                {
                    b.AddRange(ip6.GetAddressBytes());   //can be ip4 or ip6
                    iRet++;
                }
                if (b.Count >= 4)
                {
                    regWork.SetValue("ipaddr6", b.ToArray(), RegistryValueKind.Binary);
                }

                if (he.aliases.Count > 0)
                {
                    regWork.SetValue("aliases", he.aliases.ToArray(), RegistryValueKind.MultiString);
                }

                if (he.expireTime != 0)
                {
                    regWork.SetValue("expireTime", BitConverter.GetBytes(he.expireTime), RegistryValueKind.Binary);
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("Exception in saveEntry(): " + ex.Message);
            }
            return(iRet);
        }
Beispiel #7
0
        private void mnuSaveChanges_Click(object sender, EventArgs e)
        {
            if (txtHost.Text.Length == 0 || listIP.Items.Count == 0)
            {
                return;
            }
            hostsentry heTmp = (hostsentry)listHosts.SelectedItem;

            List <string> lAliases           = new List <string>();
            List <System.Net.IPAddress> lIP4 = new List <System.Net.IPAddress>();
            List <System.Net.IPAddress> lIP6 = new List <System.Net.IPAddress>();
            ulong lExpireTime = Convert.ToUInt64(txtExpires.Text);

            foreach (object o in listAliases.Items)
            {
                lAliases.Add(o.ToString());
            }

            foreach (object o in listIP.Items)
            {
                try
                {
                    System.Net.IPAddress ipTemp = System.Net.IPAddress.Parse(o.ToString());
                    if (ipTemp.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                    {
                        lIP4.Add(ipTemp);
                    }
                    else if (ipTemp.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6)
                    {
                        lIP6.Add(ipTemp);
                    }
                }
                catch { }
            }

            heTmp.sHost   = txtHost.Text;
            heTmp.aliases = lAliases;
            if (lExpireTime != 0)
            {
                heTmp.expireTime = lExpireTime;
            }
            heTmp.ipAddress  = lIP4;
            heTmp.ipAddress6 = lIP6;
            hEntries.addChangeEntry(heTmp);
            hEntries.saveEntry(heTmp);
            loadItems();
        }
Beispiel #8
0
        /// <summary>
        /// add or change host entry
        /// </summary>
        /// <param name="he"></param>
        /// <returns>true for added a new entry
        /// false if existing entry is replaced</returns>
        public bool addChangeEntry(hostsentry he)
        {
            bool       bRet  = false;
            hostsentry hTest = new hostsentry();

            if (this.allHosts.TryGetValue(he.sHost, out hTest))
            {
                //there is already an entry
                this.allHosts.Remove(he.sHost);
                //replace old entry by new one
                this.allHosts.Add(he.sHost, he);
                bRet = false;
            }
            else
            {
                this.allHosts.Add(he.sHost, he);
                bRet = true;
            }
            return(bRet);
        }
Beispiel #9
0
        private void btnRemove_Click(object sender, EventArgs e)
        {
            if (listHosts.SelectedIndex == -1)
            {
                return;
            }
            hostsentry he = (hostsentry)listHosts.SelectedItem;

            if (MessageBox.Show("Are you sure to remove '" + he.sHost + "' now?", "Confirm", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Cancel)
            {
                return;
            }
            if (hEntries.removeEntry(he) == 1)
            {
                loadItems();
            }
            else
            {
                MessageBox.Show("Remove failed");
            }
        }
 private void btnNew_Click(object sender, EventArgs e)
 {
     hostsentry he = new hostsentry();
     he.sHost = "NEW ENTRY - PLEASE CHANGE";
     he.ipAddress.Add(new System.Net.IPAddress(0));
     listHosts.Items.Insert(0, he);
     listHosts.SelectedIndex = 0;
 }
Beispiel #11
0
        int loadRegistry()
        {
            int iRet = 0;

            string[] subKeys = rKeyTCPIP.GetSubKeyNames();
            allHosts.Clear();
            foreach (string sHost in subKeys)
            {
                hostsentry hEntry = new hostsentry();
                hEntry.sHost = sHost;

                if (existsValue(sHost, "Aliases"))
                {
                    string[] sAliases = (string[])rKeyTCPIP.OpenSubKey(sHost).GetValue("Aliases", null);
                    if (sAliases != null)
                    {
                        foreach (string s in sAliases)
                        {
                            hEntry.aliases.Add(s);
                        }
                    }
                }

                hEntry.expireTime = 0; //do not save!
                if (existsValue(sHost, "ExpireTime"))
                {
                    byte[] bExpires = (byte[])rKeyTCPIP.OpenSubKey(sHost).GetValue("ExpireTime ", null);
                    if (bExpires != null)
                    {
                        ulong ul = BitConverter.ToUInt64(bExpires, 0);
                        hEntry.expireTime = ul;
                    }
                }

                if (existsValue(sHost, "ipAddr"))
                {
                    byte[] bIpAddress4 = (byte[])rKeyTCPIP.OpenSubKey(sHost).GetValue("ipAddr", null);
                    if (bIpAddress4 != null && bIpAddress4.Length >= 4)
                    {
                        if (bIpAddress4.Length > 4)
                        {
                            for (int offset = 0; offset < bIpAddress4.Length; offset += 4)
                            {
                                byte[] bIp = new byte[4];
                                Array.Copy(bIpAddress4, offset, bIp, 0, 4);
                                System.Net.IPAddress ip = System.Net.IPAddress.Parse(
                                    bIp[0].ToString() + "." +
                                    bIp[1].ToString() + "." +
                                    bIp[2].ToString() + "." +
                                    bIp[3].ToString());
                                hEntry.ipAddress.Add(ip);
                            }
                        }
                        else
                        {
                            System.Net.IPAddress ip = System.Net.IPAddress.Parse(
                                bIpAddress4[0].ToString() + "." +
                                bIpAddress4[1].ToString() + "." +
                                bIpAddress4[2].ToString() + "." +
                                bIpAddress4[3].ToString());
                            hEntry.ipAddress.Add(ip);
                        }
                    }
                }

                //ip6 address is 16 bytes
                // ie 2001:0db8:ac10:fe01:0000:0000:0000:0000
                if (existsValue(sHost, "ipAddr6"))
                {
                    byte[] bIpAddress6 = (byte[])rKeyTCPIP.OpenSubKey(sHost).GetValue("ipAddr6", null);
                    if (bIpAddress6 != null && bIpAddress6.Length >= 16)
                    {
                        if (bIpAddress6.Length > 16)
                        {
                            for (int offset = 0; offset < bIpAddress6.Length; offset += 16)
                            {
                                byte[] bIp = new byte[16];
                                Array.Copy(bIpAddress6, offset, bIp, 0, 16);
                                System.Net.IPAddress ip6 = getIP6fromByte(bIp);
                                if (ip6 != System.Net.IPAddress.None)
                                {
                                    hEntry.ipAddress6.Add(ip6);
                                }
                            }
                        }
                        else
                        {
                            System.Net.IPAddress ip6 = getIP6fromByte(bIpAddress6);
                            if (ip6 != System.Net.IPAddress.None)
                            {
                                hEntry.ipAddress6.Add(ip6);
                            }
                        }
                    }
                }

                this.allHosts.Add(hEntry.sHost, hEntry);
            }


            return(iRet);
        }
        public int saveEntry(hostsentry he)
        {
            int iRet = 0;
            RegistryKey regWork=null;
            //create subkey if not exist
            try
            {
                regWork = rKeyTCPIP.CreateSubKey(he.sHost);
                List<byte> b = new List<byte>();
                foreach (System.Net.IPAddress ip in he.ipAddress)
                {
                    b.AddRange(ip.GetAddressBytes());   //can be ip4 or ip6
                    iRet++;
                }
                if(b.Count>=4)
                    regWork.SetValue("ipaddr", b.ToArray(), RegistryValueKind.Binary);

                b.Clear();
                foreach (System.Net.IPAddress ip6 in he.ipAddress6)
                {
                    b.AddRange(ip6.GetAddressBytes());   //can be ip4 or ip6
                    iRet++;
                }
                if (b.Count >= 4)
                    regWork.SetValue("ipaddr6", b.ToArray(), RegistryValueKind.Binary);

                if(he.aliases.Count>0)
                    regWork.SetValue("aliases", he.aliases.ToArray(), RegistryValueKind.MultiString);

                if(he.expireTime!=0)
                    regWork.SetValue("expireTime", BitConverter.GetBytes(he.expireTime), RegistryValueKind.Binary);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("Exception in saveEntry(): " + ex.Message);
            }
            return iRet;
        }
 public hostsentry getEntry(string sHost)
 {
     hostsentry he = new hostsentry();
     //test if entry exists
     if (this.allHosts.TryGetValue(sHost, out he))
         return he;
     return he;
 }
 /// <summary>
 /// add or change host entry
 /// </summary>
 /// <param name="he"></param>
 /// <returns>true for added a new entry
 /// false if existing entry is replaced</returns>
 public bool addChangeEntry(hostsentry he)
 {
     bool bRet = false;
     hostsentry hTest = new hostsentry();
     if (this.allHosts.TryGetValue(he.sHost, out hTest))
     {
         //there is already an entry
         this.allHosts.Remove(he.sHost);
         //replace old entry by new one
         this.allHosts.Add(he.sHost, he);
         bRet = false;
     }
     else
     {
         this.allHosts.Add(he.sHost, he);
         bRet = true;
     }
     return bRet;
 }
        int loadRegistry()
        {
            int iRet = 0;
            string[] subKeys = rKeyTCPIP.GetSubKeyNames();
            allHosts.Clear();
            foreach (string sHost in subKeys)
            {
                hostsentry hEntry = new hostsentry();
                hEntry.sHost = sHost;

                if (existsValue(sHost, "Aliases"))
                {
                    string[] sAliases = (string[])rKeyTCPIP.OpenSubKey(sHost).GetValue("Aliases", null);
                    if (sAliases != null)
                    {
                        foreach(string s in sAliases)
                            hEntry.aliases.Add(s);
                    }
                }

                hEntry.expireTime = 0; //do not save!
                if (existsValue(sHost, "ExpireTime"))
                {
                    byte[] bExpires = (byte[])rKeyTCPIP.OpenSubKey(sHost).GetValue("ExpireTime ", null);
                    if (bExpires != null)
                    {
                        ulong ul = BitConverter.ToUInt64(bExpires, 0);
                        hEntry.expireTime = ul;
                    }
                }

                if (existsValue(sHost, "ipAddr"))
                {
                    byte[] bIpAddress4 = (byte[])rKeyTCPIP.OpenSubKey(sHost).GetValue("ipAddr", null);
                    if (bIpAddress4 != null && bIpAddress4.Length >= 4)
                    {
                        if (bIpAddress4.Length > 4)
                        {
                            for (int offset = 0; offset < bIpAddress4.Length; offset += 4)
                            {
                                byte[] bIp = new byte[4];
                                Array.Copy(bIpAddress4, offset, bIp, 0, 4);
                                System.Net.IPAddress ip = System.Net.IPAddress.Parse(
                                    bIp[0].ToString() + "." +
                                    bIp[1].ToString() + "." +
                                    bIp[2].ToString() + "." +
                                    bIp[3].ToString());
                                hEntry.ipAddress.Add(ip);
                            }
                        }
                        else
                        {
                            System.Net.IPAddress ip = System.Net.IPAddress.Parse(
                                bIpAddress4[0].ToString() + "." +
                                bIpAddress4[1].ToString() + "." +
                                bIpAddress4[2].ToString() + "." +
                                bIpAddress4[3].ToString());
                            hEntry.ipAddress.Add(ip);
                        }
                    }
                }

                //ip6 address is 16 bytes
                // ie 2001:0db8:ac10:fe01:0000:0000:0000:0000
                if (existsValue(sHost, "ipAddr6"))
                {
                    byte[] bIpAddress6 = (byte[])rKeyTCPIP.OpenSubKey(sHost).GetValue("ipAddr6", null);
                    if (bIpAddress6 != null && bIpAddress6.Length >= 16)
                    {
                        if (bIpAddress6.Length > 16)
                        {
                            for (int offset = 0; offset < bIpAddress6.Length; offset += 16)
                            {
                                byte[] bIp = new byte[16];
                                Array.Copy(bIpAddress6, offset, bIp, 0, 16);
                                System.Net.IPAddress ip6 = getIP6fromByte(bIp);
                                if (ip6 != System.Net.IPAddress.None)
                                    hEntry.ipAddress6.Add(ip6);
                            }
                        }
                        else
                        {
                            System.Net.IPAddress ip6 = getIP6fromByte(bIpAddress6);
                            if(ip6!=System.Net.IPAddress.None)
                                hEntry.ipAddress6.Add(ip6);
                        }
                    }
                }

                this.allHosts.Add(hEntry.sHost, hEntry);
            }
            
            
            return iRet;
        }