Example #1
0
 private void Intersect(ArrayList list1, ArrayList list2, ArrayList result)
 {
     foreach (EndpointPermission perm1 in list1)
     {
         foreach (EndpointPermission perm2 in list2)
         {
             EndpointPermission perm = perm1.Intersect(perm2);
             if (perm != null)
             {
                 // instead of the below it's also okay to simply do:
                 //     result.Add (perm);
                 // below is only done to avoid double entries
                 bool replaced = false;
                 for (int i = 0; i < result.Count; i++)
                 {
                     EndpointPermission res     = (EndpointPermission)result [i];
                     EndpointPermission resperm = perm.Intersect(res);
                     if (resperm != null)
                     {
                         result [i] = resperm;
                         replaced   = true;
                         break;
                     }
                 }
                 if (!replaced)
                 {
                     result.Add(perm);
                 }
             }
         }
     }
 }
Example #2
0
 // Determine if this object contains a specific permission as a subset.
 private bool Contains(EndpointPermission info1)
 {
     foreach (EndpointPermission info2 in permissions)
     {
         if (info1.access != info2.access)
         {
             continue;
         }
         if (info2.transport != TransportType.All &&
             info1.transport != info2.transport)
         {
             continue;
         }
         if (info2.portNumber != AllPorts &&
             info1.portNumber != info2.portNumber)
         {
             continue;
         }
         if (info2.hostName != "*.*.*.*" &&
             info1.hostName != info2.hostName)
         {
             continue;
         }
         return(true);
     }
     return(false);
 }
Example #3
0
        // Determine if two endpoints are equal (ignoring access).
        public override bool Equals(Object obj)
        {
            EndpointPermission info = (obj as EndpointPermission);

            if (info != null)
            {
                if (transport != info.transport)
                {
                    return(false);
                }
                if (portNumber != info.portNumber)
                {
                    return(false);
                }
                                #if !ECMA_COMPAT
                return(String.Compare(hostName, info.hostName, true,
                                      CultureInfo.InvariantCulture) == 0);
                                #else
                return(String.Compare(hostName, info.hostName, true) == 0);
                                #endif
            }
            else
            {
                return(false);
            }
        }
 public void AddPermission(NetworkAccess access, TransportType transport, string hostName, int portNumber)
 {
     if (hostName == null)
     {
         throw new ArgumentNullException("hostName");
     }
     EndpointPermission endPoint = new EndpointPermission(hostName, portNumber, transport);
     this.AddPermission(access, endPoint);
 }
Example #5
0
        // Methods

        public override bool Equals(object obj)
        {
            EndpointPermission epp = obj as EndpointPermission;

            return((epp != null) &&
                   (this.port == epp.port) &&
                   (this.transport == epp.transport) &&
                   (String.Compare(this.hostname, epp.hostname, true) == 0));
        }
        private static void intersectLists(ArrayList A, ArrayList B, ArrayList result)
        {
            bool[] flagArray  = new bool[A.Count];
            bool[] flagArray2 = new bool[B.Count];
            int    index      = 0;
            int    num2       = 0;

            foreach (EndpointPermission permission in A)
            {
                num2 = 0;
                foreach (EndpointPermission permission2 in B)
                {
                    if (!flagArray2[num2] && permission.Equals(permission2))
                    {
                        result.Add(permission);
                        flagArray[index] = flagArray2[num2] = true;
                        break;
                    }
                    num2++;
                }
                index++;
            }
            index = 0;
            foreach (EndpointPermission permission3 in A)
            {
                if (!flagArray[index])
                {
                    num2 = 0;
                    foreach (EndpointPermission permission4 in B)
                    {
                        if (!flagArray2[num2])
                        {
                            EndpointPermission permission5 = permission3.Intersect(permission4);
                            if (permission5 != null)
                            {
                                bool flag = false;
                                foreach (EndpointPermission permission6 in result)
                                {
                                    if (permission6.Equals(permission5))
                                    {
                                        flag = true;
                                        break;
                                    }
                                }
                                if (!flag)
                                {
                                    result.Add(permission5);
                                }
                            }
                        }
                        num2++;
                    }
                }
                index++;
            }
        }
        public void AddPermission(NetworkAccess access, TransportType transport, string hostName, int portNumber)
        {
            if (hostName == null)
            {
                throw new ArgumentNullException("hostName");
            }
            EndpointPermission endPoint = new EndpointPermission(hostName, portNumber, transport);

            this.AddPermission(access, endPoint);
        }
Example #8
0
        private string IntersectHostname(EndpointPermission perm)
        {
            if (this.hostname == perm.hostname)
            {
                return(this.hostname);
            }

            this.Resolve();
            perm.Resolve();

            string _hostname = null;

            if (this.hasWildcard)
            {
                if (perm.hasWildcard)
                {
                    _hostname = Intersect(this.hostname, perm.hostname);
                }
                else if (perm.addresses != null)
                {
                    for (int j = 0; j < perm.addresses.Length; j++)
                    {
                        _hostname = Intersect(this.hostname, perm.addresses [j].ToString());
                        if (_hostname != null)
                        {
                            break;
                        }
                    }
                }
            }
            else if (this.addresses != null)
            {
                for (int i = 0; i < this.addresses.Length; i++)
                {
                    string thisaddr = this.addresses [i].ToString();
                    if (perm.hasWildcard)
                    {
                        _hostname = Intersect(thisaddr, perm.hostname);
                    }
                    else if (perm.addresses != null)
                    {
                        for (int j = 0; j < perm.addresses.Length; j++)
                        {
                            _hostname = Intersect(thisaddr, perm.addresses [j].ToString());
                            if (_hostname != null)
                            {
                                break;
                            }
                        }
                    }
                }
            }

            return(_hostname);
        }
		// Methods
		
		public void AddPermission (NetworkAccess access, TransportType transport,
					   string hostName, int portNumber)
		{
			if (m_noRestriction)
				return;
				
			EndpointPermission permission = new EndpointPermission (hostName, portNumber, transport);

			if (access == NetworkAccess.Accept)
				m_acceptList.Add (permission);
			else
				m_connectList.Add (permission);
		}		
 internal void AddPermission(NetworkAccess access, EndpointPermission endPoint)
 {
     if (!this.m_noRestriction)
     {
         if ((access & NetworkAccess.Connect) != 0)
         {
             this.m_connectList.Add(endPoint);
         }
         if ((access & NetworkAccess.Accept) != 0)
         {
             this.m_acceptList.Add(endPoint);
         }
     }
 }
 internal void AddPermission(NetworkAccess access, EndpointPermission endPoint)
 {
     if (!this.m_noRestriction)
     {
         if ((access & NetworkAccess.Connect) != 0)
         {
             this.m_connectList.Add(endPoint);
         }
         if ((access & NetworkAccess.Accept) != 0)
         {
             this.m_acceptList.Add(endPoint);
         }
     }
 }
 internal void AddPermission(NetworkAccess access, EndpointPermission endPoint)
 {
     if (m_noRestriction)    // Is the permission unrestricted?
     {
         return;             // YES-- then additional endpoints have no effect
     }
     if ((access & NetworkAccess.Connect) != 0)
     {
         m_connectList.Add(endPoint);
     }
     if ((access & NetworkAccess.Accept) != 0)
     {
         m_acceptList.Add(endPoint);
     }
 }
Example #13
0
        internal void AddPermission(NetworkAccess access, EndpointPermission endPoint)
        {
            if (m_noRestriction)    // Is the permission unrestricted?
            {
                return;             // YES-- then additional endpoints have no effect
            }
            switch (access)
            {
            case NetworkAccess.Connect:
                m_connectList.Add(endPoint);
                break;

            case NetworkAccess.Accept:
                m_acceptList.Add(endPoint);
                break;
            }
        }
Example #14
0
        //
        // This is ONLY a syntatic check on equality, hostnames are compared as strings!
        //
        /// <include file='doc\SocketPermission.uex' path='docs/doc[@for="EndpointPermission.Equals"]/*' />
        public override bool Equals(object obj)
        {
            EndpointPermission ep = (EndpointPermission)obj;

            if (String.Compare(hostname, ep.hostname, true, CultureInfo.InvariantCulture) != 0)
            {
                return(false);
            }
            if (port != ep.port)
            {
                return(false);
            }
            if (transport != ep.transport)
            {
                return(false);
            }
            return(true);
        }
Example #15
0
        public override bool Equals(object obj)
        {
            EndpointPermission permission = (EndpointPermission)obj;

            if (string.Compare(this.hostname, permission.hostname, StringComparison.OrdinalIgnoreCase) != 0)
            {
                return(false);
            }
            if (this.port != permission.port)
            {
                return(false);
            }
            if (this.transport != permission.transport)
            {
                return(false);
            }
            return(true);
        }
        //
        // This is ONLY a syntatic check on equality, hostnames are compared as strings!
        //
        public override bool Equals(object obj)
        {
            EndpointPermission ep = (EndpointPermission)obj;

            if (String.Compare(hostname, ep.hostname, StringComparison.OrdinalIgnoreCase) != 0)
            {
                return(false);
            }
            if (port != ep.port)
            {
                return(false);
            }
            if (transport != ep.transport)
            {
                return(false);
            }
            return(true);
        }
Example #17
0
        private void ToXml(SecurityElement root, string childName, IEnumerator enumerator)
        {
            SecurityElement child = new SecurityElement(childName);

            while (enumerator.MoveNext())
            {
                EndpointPermission perm       = enumerator.Current as EndpointPermission;
                SecurityElement    grandchild = new SecurityElement("ENDPOINT");
                grandchild.AddAttribute("host", perm.Hostname);
                grandchild.AddAttribute("transport", perm.Transport.ToString());
                grandchild.AddAttribute("port",
                                        perm.Port == AllPorts
                                                ? "All"
                                                : ((Int32)perm.Port).ToString());
                child.AddChild(grandchild);
            }
            root.AddChild(child);
        }
Example #18
0
        // Methods

        public void AddPermission(NetworkAccess access, TransportType transport,
                                  string hostName, int portNumber)
        {
            if (m_noRestriction)
            {
                return;
            }

            EndpointPermission permission = new EndpointPermission(hostName, portNumber, transport);

            if (access == NetworkAccess.Accept)
            {
                m_acceptList.Add(permission);
            }
            else
            {
                m_connectList.Add(permission);
            }
        }
		// Internal & Private Methods
		
		internal bool IsSubsetOf (EndpointPermission perm) 
		{
			if (perm == null)
				return false;
			
			if (perm.port != SocketPermission.AllPorts &&
			    this.port != perm.port)
			    	return false;
			
			if (perm.transport != TransportType.All &&
			    this.transport != perm.transport)
				return false;
			
			this.Resolve ();
			perm.Resolve ();
			
			if (this.hasWildcard) {
				if (perm.hasWildcard)
					return IsSubsetOf (this.hostname, perm.hostname);
				else 
					return false;
			} 
			
			if (this.addresses == null) 
				return false;
			
			if (perm.hasWildcard) 
				// a bit dubious... should they all be a subset or is one 
				// enough in this case?
				foreach (IPAddress addr in this.addresses)
					if (IsSubsetOf (addr.ToString (), perm.hostname))
						return true;
				
			if (perm.addresses == null) 
				return false;
				
			// a bit dubious... should they all be a subset or is one 
			// enough in this case?
			foreach (IPAddress addr in perm.addresses)
				if (IsSubsetOf (this.hostname, addr.ToString ())) 
					return true;	
					
			return false;		
		}		
Example #20
0
 internal bool MatchAddress(EndpointPermission e)
 {
     if ((this.Hostname.Length != 0) && (e.Hostname.Length != 0))
     {
         if (this.Hostname.Equals("0.0.0.0"))
         {
             if (!e.Hostname.Equals("*.*.*.*") && !e.Hostname.Equals("0.0.0.0"))
             {
                 return(false);
             }
             return(true);
         }
         if (this.IsDns && e.IsDns)
         {
             return(string.Compare(this.hostname, e.hostname, StringComparison.OrdinalIgnoreCase) == 0);
         }
         this.Resolve();
         e.Resolve();
         if (((this.address == null) && !this.wildcard) || ((e.address == null) && !e.wildcard))
         {
             return(false);
         }
         if (this.wildcard && !e.wildcard)
         {
             return(false);
         }
         if (e.wildcard)
         {
             if (this.wildcard)
             {
                 if (this.MatchWildcard(e.hostname))
                 {
                     return(true);
                 }
             }
             else
             {
                 for (int i = 0; i < this.address.Length; i++)
                 {
                     if (e.MatchWildcard(this.address[i].ToString()))
                     {
                         return(true);
                     }
                 }
             }
         }
         else
         {
             for (int j = 0; j < this.address.Length; j++)
             {
                 for (int k = 0; k < e.address.Length; k++)
                 {
                     if (this.address[j].Equals(e.address[k]))
                     {
                         return(true);
                     }
                 }
             }
         }
     }
     return(false);
 }
Example #21
0
 internal bool SubsetMatch(EndpointPermission e)
 {
     return((((this.transport == e.transport) || (e.transport == TransportType.All)) && (((this.port == e.port) || (e.port == -1)) || (this.port == 0))) && this.MatchAddress(e));
 }
Example #22
0
        internal void AddPermission(NetworkAccess access, EndpointPermission endPoint) {
            if (m_noRestriction) {    // Is the permission unrestricted?
                return;             // YES-- then additional endpoints have no effect
            }
            switch (access) {
                case NetworkAccess.Connect:
                    m_connectList.Add(endPoint);
                    break;

                case NetworkAccess.Accept:
                    m_acceptList.Add(endPoint);
                    break;
            }
        }
Example #23
0
        internal EndpointPermission Intersect(EndpointPermission perm)
        {
            if (perm == null)
            {
                return(null);
            }

            int _port;

            if (this.port == perm.port)
            {
                _port = this.port;
            }
            else if (this.port == SocketPermission.AllPorts)
            {
                _port = perm.port;
            }
            else if (perm.port == SocketPermission.AllPorts)
            {
                _port = this.port;
            }
            else
            {
                return(null);
            }

            TransportType _transport;

            if (this.transport == perm.transport)
            {
                _transport = this.transport;
            }
            else if (this.transport == TransportType.All)
            {
                _transport = perm.transport;
            }
            else if (perm.transport == TransportType.All)
            {
                _transport = this.transport;
            }
            else
            {
                return(null);
            }

            string _hostname = IntersectHostname(perm);

            if (_hostname == null)
            {
                return(null);
            }

            if (!this.hasWildcard)
            {
                return(this);
            }

            if (!perm.hasWildcard)
            {
                return(perm);
            }

            EndpointPermission newperm = new EndpointPermission(_hostname, _port, _transport);

            newperm.hasWildcard = true;
            newperm.resolved    = true;
            return(newperm);
        }
Example #24
0
        // Internal & Private Methods

        internal bool IsSubsetOf(EndpointPermission perm)
        {
            if (perm == null)
            {
                return(false);
            }

            if (perm.port != SocketPermission.AllPorts &&
                this.port != perm.port)
            {
                return(false);
            }

            if (perm.transport != TransportType.All &&
                this.transport != perm.transport)
            {
                return(false);
            }

            this.Resolve();
            perm.Resolve();

            if (this.hasWildcard)
            {
                if (perm.hasWildcard)
                {
                    return(IsSubsetOf(this.hostname, perm.hostname));
                }
                else
                {
                    return(false);
                }
            }

            if (this.addresses == null)
            {
                return(false);
            }

            if (perm.hasWildcard)
            {
                // a bit dubious... should they all be a subset or is one
                // enough in this case?
                foreach (IPAddress addr in this.addresses)
                {
                    if (IsSubsetOf(addr.ToString(), perm.hostname))
                    {
                        return(true);
                    }
                }
            }

            if (perm.addresses == null)
            {
                return(false);
            }

            // a bit dubious... should they all be a subset or is one
            // enough in this case?
            foreach (IPAddress addr in perm.addresses)
            {
                if (IsSubsetOf(this.hostname, addr.ToString()))
                {
                    return(true);
                }
            }

            return(false);
        }
 internal bool SubsetMatch(EndpointPermission e) {
     return ((transport == e.transport) || (e.transport == TransportType.All))
             && ((port == e.port) || (e.port == SocketPermission.AllPorts) || port == SocketPermission.AnyPort)
             && MatchAddress(e);
 }
        internal bool MatchAddress(EndpointPermission e) {

            // For Asp.Net config we made it valid empty string in a hostname,
            // but it will match to nothing.
            if(this.Hostname.Length == 0 || e.Hostname.Length == 0) {
                return false;
            }

            //
            // This is a fix for INADDR_ANY in Bind()
            // if this.Hostname == "0.0.0.0" then it matches only to e.Hostname="*.*.*.*"
            //
            // The reason is to not pass "0.0.0.0" into Resolve()
            if(this.Hostname.Equals("0.0.0.0"))
            {
                if(e.Hostname.Equals("*.*.*.*") || e.Hostname.Equals("0.0.0.0"))
                    return true;
                return false;
            }

            if (IsDns && e.IsDns) {

                //
                // <



                return (String.Compare(hostname, e.hostname, StringComparison.OrdinalIgnoreCase ) == 0);
            }
            Resolve();
            e.Resolve();

            //
            // if Resolve() didn't work for some reason then we're out of luck
            //

            if (((address == null) && !wildcard) || ((e.address == null) && !e.wildcard)) {
                return false;
            }

            //
            // try matching IP addresses against other wildcard address(es) or
            // wildcard
            //

            if (this.wildcard && !e.wildcard) {
                return false;                           // as a wildcard I cannot be subset of a host.

            }
            else if (e.wildcard) {
                if (this.wildcard) {
                    // check against my _wildcard_
                    if (MatchWildcard(e.hostname)) {
                        return true;
                    }
                }
                else {
                    // check against my _addresses_
                    for (int i = 0; i < address.Length; ++i) {
                        if (e.MatchWildcard(address[i].ToString())) {
                            return true;
                        }
                    }
                }
            } else {
                //both are _not_ wildcards
                for (int i = 0; i < address.Length; ++i) {
                    for (int j = 0; j < e.address.Length; ++j) {
                        if (address[i].Equals(e.address[j])) {
                            return true;
                        }
                    }
                }
            }
            return false;
        }
Example #27
0
        internal EndpointPermission Intersect(EndpointPermission E)
        {
            string        epname = null;
            TransportType transport;
            int           port;

            if (this.transport == E.transport)
            {
                transport = this.transport;
            }
            else if (this.transport == TransportType.All)
            {
                transport = E.transport;
            }
            else if (E.transport == TransportType.All)
            {
                transport = this.transport;
            }
            else
            {
                return(null);
            }
            if (this.port == E.port)
            {
                port = this.port;
            }
            else if (this.port == -1)
            {
                port = E.port;
            }
            else if (E.port == -1)
            {
                port = this.port;
            }
            else
            {
                return(null);
            }
            if (this.Hostname.Equals("0.0.0.0"))
            {
                if (!E.Hostname.Equals("*.*.*.*") && !E.Hostname.Equals("0.0.0.0"))
                {
                    return(null);
                }
                epname = this.Hostname;
            }
            else if (E.Hostname.Equals("0.0.0.0"))
            {
                if (!this.Hostname.Equals("*.*.*.*") && !this.Hostname.Equals("0.0.0.0"))
                {
                    return(null);
                }
                epname = E.Hostname;
            }
            else if (this.IsDns && E.IsDns)
            {
                if (string.Compare(this.hostname, E.hostname, StringComparison.OrdinalIgnoreCase) != 0)
                {
                    return(null);
                }
                epname = this.hostname;
            }
            else
            {
                this.Resolve();
                E.Resolve();
                if (((this.address == null) && !this.wildcard) || ((E.address == null) && !E.wildcard))
                {
                    return(null);
                }
                if (this.wildcard && E.wildcard)
                {
                    string[] strArray  = this.hostname.Split(DotSeparator);
                    string[] strArray2 = E.hostname.Split(DotSeparator);
                    string   str2      = "";
                    if ((strArray2.Length != 4) || (strArray.Length != 4))
                    {
                        return(null);
                    }
                    for (int i = 0; i < 4; i++)
                    {
                        if (i != 0)
                        {
                            str2 = str2 + ".";
                        }
                        if (strArray2[i] == strArray[i])
                        {
                            str2 = str2 + strArray2[i];
                        }
                        else if (strArray2[i] == "*")
                        {
                            str2 = str2 + strArray[i];
                        }
                        else if (strArray[i] == "*")
                        {
                            str2 = str2 + strArray2[i];
                        }
                        else
                        {
                            return(null);
                        }
                    }
                    epname = str2;
                }
                else if (this.wildcard)
                {
                    for (int j = 0; j < E.address.Length; j++)
                    {
                        if (this.MatchWildcard(E.address[j].ToString()))
                        {
                            epname = E.hostname;
                            break;
                        }
                    }
                }
                else if (E.wildcard)
                {
                    for (int k = 0; k < this.address.Length; k++)
                    {
                        if (E.MatchWildcard(this.address[k].ToString()))
                        {
                            epname = this.hostname;
                            break;
                        }
                    }
                }
                else
                {
                    if (this.address == E.address)
                    {
                        epname = this.hostname;
                    }
                    for (int m = 0; (epname == null) && (m < this.address.Length); m++)
                    {
                        for (int n = 0; n < E.address.Length; n++)
                        {
                            if (this.address[m].Equals(E.address[n]))
                            {
                                epname = this.hostname;
                                break;
                            }
                        }
                    }
                }
                if (epname == null)
                {
                    return(null);
                }
            }
            return(new EndpointPermission(epname, port, transport));
        }
Example #28
0
        // Form the intersection of two EndpointPermission objects.
        // Returns null if no possible intersection between the two.
        private static EndpointPermission Intersect
            (EndpointPermission info1, EndpointPermission info2)
        {
            TransportType transport;
            int           portNumber;
            String        hostName;

            // Check the access values.
            if (info1.access != info2.access)
            {
                return(null);
            }

            // Check the transport values.
            if (info1.transport == TransportType.All)
            {
                transport = info2.transport;
            }
            else if (info2.transport == TransportType.All)
            {
                transport = info1.transport;
            }
            else if (info1.transport == info2.transport)
            {
                transport = info1.transport;
            }
            else
            {
                return(null);
            }

            // Check the port values.
            if (info1.portNumber == AllPorts)
            {
                portNumber = info2.portNumber;
            }
            else if (info2.portNumber == AllPorts)
            {
                portNumber = info1.portNumber;
            }
            else if (info1.portNumber == info2.portNumber)
            {
                portNumber = info1.portNumber;
            }
            else
            {
                return(null);
            }

            // Check the hostnames.
            if (info1.hostName == "*.*.*.*")
            {
                hostName = info2.hostName;
            }
            else if (info2.hostName == "*.*.*.*")
            {
                hostName = info1.hostName;
            }
            else if (String.Compare(info1.hostName, info2.hostName, true)
                     == 0)
            {
                hostName = info1.hostName;
            }
            else
            {
                return(null);
            }

            // Build a new object for the intersection.
            return(new EndpointPermission
                       (info1.access, transport, hostName, portNumber));
        }
 internal bool SubsetMatch(EndpointPermission e)
 {
     return(((transport == e.transport) || (e.transport == TransportType.All)) &&
            ((port == e.port) || (e.port == SocketPermission.AllPorts) || port == SocketPermission.AnyPort) &&
            MatchAddress(e));
 }
	// Determine if this object contains a specific permission as a subset.
	private bool Contains(EndpointPermission info1)
			{
				foreach(EndpointPermission info2 in permissions)
				{
					if(info1.access != info2.access)
					{
						continue;
					}
					if(info2.transport != TransportType.All &&
					   info1.transport != info2.transport)
					{
						continue;
					}
					if(info2.portNumber != AllPorts &&
					   info1.portNumber != info2.portNumber)
					{
						continue;
					}
					if(info2.hostName != "*.*.*.*" &&
					   info1.hostName != info2.hostName)
					{
						continue;
					}
					return true;
				}
				return false;
			}
	// Form the intersection of two EndpointPermission objects.
	// Returns null if no possible intersection between the two.
	private static EndpointPermission Intersect
				(EndpointPermission info1, EndpointPermission info2)
			{
				TransportType transport;
				int portNumber;
				String hostName;

				// Check the access values.
				if(info1.access != info2.access)
				{
					return null;
				}

				// Check the transport values.
				if(info1.transport == TransportType.All)
				{
					transport = info2.transport;
				}
				else if(info2.transport == TransportType.All)
				{
					transport = info1.transport;
				}
				else if(info1.transport == info2.transport)
				{
					transport = info1.transport;
				}
				else
				{
					return null;
				}

				// Check the port values.
				if(info1.portNumber == AllPorts)
				{
					portNumber = info2.portNumber;
				}
				else if(info2.portNumber == AllPorts)
				{
					portNumber = info1.portNumber;
				}
				else if(info1.portNumber == info2.portNumber)
				{
					portNumber = info1.portNumber;
				}
				else
				{
					return null;
				}

				// Check the hostnames.
				if(info1.hostName == "*.*.*.*")
				{
					hostName = info2.hostName;
				}
				else if(info2.hostName == "*.*.*.*")
				{
					hostName = info1.hostName;
				}
				else if(String.Compare(info1.hostName, info2.hostName, true)
							== 0)
				{
					hostName = info1.hostName;
				}
				else
				{
					return null;
				}

				// Build a new object for the intersection.
				return new EndpointPermission
					(info1.access, transport, hostName, portNumber);
			}
		internal EndpointPermission Intersect (EndpointPermission perm) 
		{
			if (perm == null)
				return null;
			
			int _port;
			if (this.port == perm.port)
				_port = this.port;
			else if (this.port == SocketPermission.AllPorts)
				_port = perm.port;
			else if (perm.port == SocketPermission.AllPorts)
				_port = this.port;
			else
				return null;

			TransportType _transport;
			if (this.transport == perm.transport)
				_transport = this.transport;
			else if (this.transport == TransportType.All)
				_transport = perm.transport;
			else if (perm.transport == TransportType.All)
				_transport = this.transport;
			else
				return null;

			string _hostname = IntersectHostname (perm);						
			
			if (_hostname == null)
				return null;

			if (!this.hasWildcard)
				return this;
				
			if (!perm.hasWildcard)
				return perm;
				
			EndpointPermission newperm = new EndpointPermission (_hostname, _port, _transport);
			newperm.hasWildcard = true;
			newperm.resolved = true;
			return newperm;
		}
        internal EndpointPermission Intersect(EndpointPermission E)
        {
            String        commonName = null;
            TransportType commonTransport;
            int           commonPort;

            //
            // Look at the transport
            //

            if (transport == E.transport)             // same transport
            {
                commonTransport = transport;
            }
            // NO: check if one of the permissions authorize all transports
            else if (transport == TransportType.All)
            {
                commonTransport = E.transport;
            }
            else if (E.transport == TransportType.All)
            {
                commonTransport = transport;
            }
            else     // transport dont match-- intersection is empty
            {
                return(null);
            }

            //
            // Determine common port
            //

            if (port == E.port)
            {
                commonPort = port;
            }
            else if (port == SocketPermission.AllPorts)
            {
                commonPort = E.port;
            }
            else if (E.port == SocketPermission.AllPorts)
            {
                commonPort = port;
            }
            else
            {
                return(null);
            }

            //Work out common hostname part
            //
            // This is a fix for INADDR_ANY in Bind()
            // if this.Hostname == "0.0.0.0" then it matches only to e.Hostname="*.*.*.*"
            //
            // The reason is to not pass "0.0.0.0" into Resolve()
            if (this.Hostname.Equals("0.0.0.0"))
            {
                if (E.Hostname.Equals("*.*.*.*") || E.Hostname.Equals("0.0.0.0"))
                {
                    commonName = this.Hostname;//i.e. 0.0.0.0
                }
                else
                {
                    return(null);
                }
            }
            else if (E.Hostname.Equals("0.0.0.0"))
            {
                if (this.Hostname.Equals("*.*.*.*") || this.Hostname.Equals("0.0.0.0"))
                {
                    commonName = E.Hostname; //i.e. 0.0.0.0
                }
                else
                {
                    return(null);
                }
            }
            else if (IsDns && E.IsDns)
            {
                //
                // If both are DNS names we compare names as strings
                //
                if (String.Compare(hostname, E.hostname, StringComparison.OrdinalIgnoreCase) != 0)
                {
                    return(null);
                }
                else
                {
                    commonName = hostname;
                }
            }
            else
            {
                Resolve();
                E.Resolve();

                //

                if (((address == null) && !wildcard) || ((E.address == null) && !E.wildcard))
                {
                    return(null);
                }


                //
                // Find intersection of address lists
                if (wildcard && E.wildcard)
                {
                    string [] wcPieces  = hostname.Split(DotSeparator);
                    string [] strPieces = E.hostname.Split(DotSeparator);
                    string    result    = "";

                    if ((strPieces.Length != 4) || (wcPieces.Length != 4))
                    {
                        return(null);
                    }
                    for (int i = 0; i < 4; i++)
                    {
                        if (i != 0)
                        {
                            result += ".";
                        }
                        if (strPieces[i] == wcPieces[i])
                        {
                            result += strPieces[i];
                        }
                        else
                        if (strPieces[i] == "*")
                        {
                            result += wcPieces[i];
                        }
                        else
                        if (wcPieces[i] == "*")
                        {
                            result += strPieces[i];
                        }
                        else
                        {
                            return(null);
                        }
                    }
                    commonName = result;
                }
                else
                if (wildcard)                                                   //if ME is a wildcard
                //
                //
                // Check for wildcard IP matching
                //
                {
                    for (int i = 0; i < E.address.Length; ++i)
                    {
                        if (MatchWildcard(E.address[i].ToString()))
                        {
                            commonName = E.hostname;    //SHE fits into my wildcard
                            break;
                        }
                    }
                }
                else if (E.wildcard)                                     //if SHE is a wildcard
                {
                    for (int i = 0; i < address.Length; ++i)
                    {
                        if (E.MatchWildcard(address[i].ToString()))
                        {
                            commonName = hostname;      //ME fit  into her wildcard
                            break;
                        }
                    }
                }
                else
                {
                    //
                    // Not wildcard: check aginst  IP addresses list
                    //

                    if (address == E.address)                   // they both are NOT null (already checked)
                    {
                        commonName = hostname;
                    }

                    //
                    // Search the IP addresses for match
                    //
                    for (int i = 0; commonName == null && i < address.Length; i++)
                    {
                        for (int k = 0; k < E.address.Length; k++)
                        {
                            if (address[i].Equals(E.address[k]))
                            {
                                commonName = hostname;
                                break;
                            }
                        }
                    }
                }
                if (commonName == null)
                {
                    return(null);
                }
            }

            return(new EndpointPermission(commonName, commonPort, commonTransport));
        }
		private string IntersectHostname (EndpointPermission perm)
		{
			if (this.hostname == perm.hostname)
				return this.hostname;
				
			this.Resolve ();
			perm.Resolve ();
			
			string _hostname = null;
			
			if (this.hasWildcard) {
				if (perm.hasWildcard) {
					_hostname = Intersect (this.hostname, perm.hostname);
				} else if (perm.addresses != null) {
					for (int j = 0; j < perm.addresses.Length; j++) {
						_hostname = Intersect (this.hostname, perm.addresses [j].ToString ());
						if (_hostname != null) 
							break;
					}
				}
			} else if (this.addresses != null) {
				for (int i = 0; i < this.addresses.Length; i++) {
					string thisaddr = this.addresses [i].ToString ();
					if (perm.hasWildcard) {
						_hostname = Intersect (thisaddr, perm.hostname);
					} else if (perm.addresses != null) {
						for (int j = 0; j < perm.addresses.Length; j++) {
							_hostname = Intersect (thisaddr, perm.addresses [j].ToString ());
							if (_hostname != null) 
								break;
						}
					}
				}
			}
			
			return _hostname;
		}
        private static void intersectLists(ArrayList A, ArrayList B, ArrayList result)
        {
            // The optimization is done according to the following truth
            // (A|B|C) intersect (B|C|E|D)) == B|C|(A inter E)|(A inter D)
            //
            // We also check on any duplicates in the result


            bool[] aDone = new bool[A.Count];            //used to avoid duplicates in result
            bool[] bDone = new bool[B.Count];
            int    ia    = 0;
            int    ib    = 0;

            // Round 1st
            // Getting rid of same permissons in the input arrays (assuming X /\ X = X)
            foreach (EndpointPermission a in  A)
            {
                ib = 0;
                foreach (EndpointPermission b in  B)
                {
                    // check to see if b is in the result already
                    if (!bDone[ib])
                    {
                        //if both elements are the same, copy it into result
                        if (a.Equals(b))
                        {
                            result.Add(a);
                            aDone[ia] = bDone[ib] = true;
                            //since permissions are ORed we can break and go to the next A
                            break;
                        }
                    }
                    ++ib;
                } //foreach b in B
                ++ia;
            }     //foreach a in A

            ia = 0;
            // Round second
            // Grab only intersections of objects not found in both A and B
            foreach (EndpointPermission a in  A)
            {
                if (!aDone[ia])
                {
                    ib = 0;
                    foreach (EndpointPermission b in B)
                    {
                        if (!bDone[ib])
                        {
                            EndpointPermission intesection = a.Intersect(b);
                            if (intesection != null)
                            {
                                bool found = false;
                                // check to see if we already have the same result
                                foreach (EndpointPermission res in result)
                                {
                                    if (res.Equals(intesection))
                                    {
                                        found = true;
                                        break;
                                    }
                                }
                                if (!found)
                                {
                                    result.Add(intesection);
                                }
                            }
                        } //!Done[ib]
                        ++ib;
                    }     //foreach b in B
                }         //!Done[ia]
                ++ia;
            }             //foreach a in A
        }
        internal EndpointPermission Intersect(EndpointPermission E) {

            String commonName=null;
            TransportType commonTransport;
            int commonPort;

            //
            // Look at the transport
            //

            if (transport == E.transport) {           // same transport
                commonTransport = transport;
            }
            // NO: check if one of the permissions authorize all transports
            else if (transport == TransportType.All) {
                commonTransport = E.transport;
            }
            else if (E.transport == TransportType.All) {
                commonTransport = transport;
            }
            else {   // transport dont match-- intersection is empty
                return null;
            }

            //
            // Determine common port
            //

            if (port == E.port) {
                commonPort = port;
            }
            else if (port == SocketPermission.AllPorts) {
                commonPort = E.port;
            }
            else if (E.port == SocketPermission.AllPorts) {
                commonPort = port;
            }
            else {
                return null;
            }

            //Work out common hostname part
            //
            // This is a fix for INADDR_ANY in Bind()
            // if this.Hostname == "0.0.0.0" then it matches only to e.Hostname="*.*.*.*"
            //
            // The reason is to not pass "0.0.0.0" into Resolve()
            if(this.Hostname.Equals("0.0.0.0"))
            {
                if(E.Hostname.Equals("*.*.*.*") || E.Hostname.Equals("0.0.0.0"))
                    commonName = this.Hostname;//i.e. 0.0.0.0
                else
                    return null;
            }
            else if(E.Hostname.Equals("0.0.0.0"))
            {
                if(this.Hostname.Equals("*.*.*.*") || this.Hostname.Equals("0.0.0.0"))
                    commonName = E.Hostname; //i.e. 0.0.0.0
                else
                    return null;
            }
            else if (IsDns && E.IsDns) {
                //
                // If both are DNS names we compare names as strings
                //
                if(String.Compare(hostname, E.hostname, StringComparison.OrdinalIgnoreCase ) != 0) {
                    return null;
                }
                else {
                    commonName = hostname;
                }
            }
            else
            {
                Resolve();
                E.Resolve();
                //after this step we got both clases updated with valid
                //wildcard and address members. It's safe now to access those members directly

                //
                // if Resolve() didn't work for some reason then we're out of luck
                //

                if (((address == null) && !wildcard) || ((E.address == null) && !E.wildcard)) {
                    return null;
                }


                //
                // Find intersection of address lists
                if(wildcard && E.wildcard) {
                    string [] wcPieces = hostname.Split(DotSeparator);
                    string [] strPieces = E.hostname.Split(DotSeparator);
                    string  result="";

                    if ((strPieces.Length != 4) || (wcPieces.Length != 4)) {
                        return null;
                    }
                    for (int i = 0; i < 4; i++) {
                        if(i != 0) {
                            result+=".";
                        }
                        if (strPieces[i] == wcPieces[i]) {
                            result+=strPieces[i];
                        }
                        else
                        if (strPieces[i] == "*") {
                            result+=wcPieces[i];
                        }
                        else
                        if (wcPieces[i] == "*") {
                            result+=strPieces[i];
                        }
                        else
                            return null;
                    }
                    commonName = result;
                }else
                if (wildcard) {                                                 //if ME is a wildcard
                    //
                    //
                    // Check for wildcard IP matching
                    //
                    for (int i = 0; i < E.address.Length; ++i) {
                        if (MatchWildcard(E.address[i].ToString())) {
                            commonName = E.hostname;    //SHE fits into my wildcard
                            break;
                        }
                    }
                }
                else if (E.wildcard) {                                   //if SHE is a wildcard
                    for (int i = 0; i < address.Length; ++i) {
                        if (E.MatchWildcard(address[i].ToString())) {
                            commonName = hostname;      //ME fit  into her wildcard
                            break;
                        }
                    }
                }
                else
                {
                    //
                    // Not wildcard: check aginst  IP addresses list
                    //

                    if (address == E.address) {                 // they both are NOT null (already checked)
                        commonName = hostname;
                    }

                    //
                    // Search the IP addresses for match
                    //
                    for (int i = 0; commonName == null && i < address.Length; i++) {
                        for (int k = 0; k < E.address.Length; k++) {
                            if (address[i].Equals(E.address[k])) {
                                commonName = hostname;
                                break;
                            }
                        }
                    }
                }
                if(commonName == null) {
                    return null;
                }
            }

            return new EndpointPermission(commonName, commonPort, commonTransport);
        }
        internal bool MatchAddress(EndpointPermission e)
        {
            // For Asp.Net config we made it valid empty string in a hostname,
            // but it will match to nothing.
            if (this.Hostname.Length == 0 || e.Hostname.Length == 0)
            {
                return(false);
            }

            //
            // This is a fix for INADDR_ANY in Bind()
            // if this.Hostname == "0.0.0.0" then it matches only to e.Hostname="*.*.*.*"
            //
            // The reason is to not pass "0.0.0.0" into Resolve()
            if (this.Hostname.Equals("0.0.0.0"))
            {
                if (e.Hostname.Equals("*.*.*.*") || e.Hostname.Equals("0.0.0.0"))
                {
                    return(true);
                }
                return(false);
            }

            if (IsDns && e.IsDns)
            {
                return(String.Compare(hostname, e.hostname, StringComparison.OrdinalIgnoreCase) == 0);
            }
            Resolve();
            e.Resolve();


            if (((address == null) && !wildcard) || ((e.address == null) && !e.wildcard))
            {
                return(false);
            }

            //
            // try matching IP addresses against other wildcard address(es) or
            // wildcard
            //

            if (this.wildcard && !e.wildcard)
            {
                return(false);                           // as a wildcard I cannot be subset of a host.
            }
            else if (e.wildcard)
            {
                if (this.wildcard)
                {
                    // check against my _wildcard_
                    if (MatchWildcard(e.hostname))
                    {
                        return(true);
                    }
                }
                else
                {
                    // check against my _addresses_
                    for (int i = 0; i < address.Length; ++i)
                    {
                        if (e.MatchWildcard(address[i].ToString()))
                        {
                            return(true);
                        }
                    }
                }
            }
            else
            {
                //both are _not_ wildcards
                for (int i = 0; i < address.Length; ++i)
                {
                    for (int j = 0; j < e.address.Length; ++j)
                    {
                        if (address[i].Equals(e.address[j]))
                        {
                            return(true);
                        }
                    }
                }
            }
            return(false);
        }
 internal void AddPermission(NetworkAccess access, EndpointPermission endPoint) {
     if (m_noRestriction) {    // Is the permission unrestricted?
         return;             // YES-- then additional endpoints have no effect
     }
     if ((access & NetworkAccess.Connect) != 0)
             m_connectList.Add(endPoint);
     if ((access & NetworkAccess.Accept) != 0)
             m_acceptList.Add(endPoint);
 }