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