2 // System.Net.DnsPermission.cs
\r
5 // Lawrence Pit (loz@cable.a2000.nl)
\r
6 // Sebastien Pouliot <sebastien@ximian.com>
\r
8 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
\r
10 // Permission is hereby granted, free of charge, to any person obtaining
\r
11 // a copy of this software and associated documentation files (the
\r
12 // "Software"), to deal in the Software without restriction, including
\r
13 // without limitation the rights to use, copy, modify, merge, publish,
\r
14 // distribute, sublicense, and/or sell copies of the Software, and to
\r
15 // permit persons to whom the Software is furnished to do so, subject to
\r
16 // the following conditions:
\r
18 // The above copyright notice and this permission notice shall be
\r
19 // included in all copies or substantial portions of the Software.
\r
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
\r
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
\r
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
\r
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
\r
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
\r
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
\r
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
30 using System.Collections;
\r
31 using System.Security;
\r
32 using System.Security.Permissions;
\r
34 namespace System.Net {
\r
37 public sealed class DnsPermission : CodeAccessPermission, IUnrestrictedPermission {
\r
39 private const int version = 1;
\r
42 bool m_noRestriction;
\r
45 public DnsPermission (PermissionState state)
\r
48 m_noRestriction = (state == PermissionState.Unrestricted);
\r
53 public override IPermission Copy ()
\r
55 return new DnsPermission (m_noRestriction ? PermissionState.Unrestricted : PermissionState.None);
\r
58 public override IPermission Intersect (IPermission target)
\r
60 DnsPermission dp = Cast (target);
\r
63 if (IsUnrestricted () && dp.IsUnrestricted ())
\r
64 return new DnsPermission (PermissionState.Unrestricted);
\r
68 public override bool IsSubsetOf (IPermission target)
\r
70 DnsPermission dp = Cast (target);
\r
74 return (dp.IsUnrestricted () || (m_noRestriction == dp.m_noRestriction));
\r
77 public bool IsUnrestricted ()
\r
79 return this.m_noRestriction;
\r
82 public override SecurityElement ToXml ()
\r
84 SecurityElement se = PermissionHelper.Element (typeof (DnsPermission), version);
\r
85 if (m_noRestriction)
\r
86 se.AddAttribute ("Unrestricted", "true");
\r
90 public override void FromXml (SecurityElement securityElement)
\r
92 PermissionHelper.CheckSecurityElement (securityElement, "securityElement", version, version);
\r
94 // LAMESPEC: it says to throw an ArgumentNullException in this case
\r
95 if (securityElement.Tag != "IPermission")
\r
96 throw new ArgumentException ("securityElement");
\r
98 this.m_noRestriction = PermissionHelper.IsUnrestricted (securityElement);
\r
101 public override IPermission Union (IPermission target)
\r
103 DnsPermission dp = Cast (target);
\r
106 if (IsUnrestricted () || dp.IsUnrestricted ())
\r
107 return new DnsPermission (PermissionState.Unrestricted);
\r
109 return new DnsPermission (PermissionState.None);
\r
112 // Internal helpers methods
\r
114 private bool IsEmpty ()
\r
116 return !m_noRestriction;
\r
119 private DnsPermission Cast (IPermission target)
\r
121 if (target == null)
\r
124 DnsPermission dp = (target as DnsPermission);
\r
126 PermissionHelper.ThrowInvalidPermission (target, typeof (DnsPermission));
\r