Merge pull request #205 from m3rlinez/master
[mono.git] / mcs / class / System / System.Net / DnsPermission.cs
1 //\r
2 // System.Net.DnsPermission.cs\r
3 //\r
4 // Authors:\r
5 //      Lawrence Pit (loz@cable.a2000.nl)\r
6 //      Sebastien Pouliot  <sebastien@ximian.com>\r
7 //\r
8 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)\r
9 //\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
17 // \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
20 // \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
28 //\r
29 \r
30 using System.Collections;\r
31 using System.Security;\r
32 using System.Security.Permissions;\r
33 \r
34 namespace System.Net {\r
35 \r
36         [Serializable]\r
37         public sealed class DnsPermission : CodeAccessPermission, IUnrestrictedPermission {\r
38 \r
39                 private const int version = 1;\r
40 \r
41                 // Fields\r
42                 bool m_noRestriction;\r
43                 \r
44                 // Constructors\r
45                 public DnsPermission (PermissionState state)\r
46                         : base () \r
47                 {                                               \r
48                         m_noRestriction = (state == PermissionState.Unrestricted);\r
49                 }\r
50                 \r
51                 // Methods\r
52                                 \r
53                 public override IPermission Copy ()\r
54                 {\r
55                         return new DnsPermission (m_noRestriction ? PermissionState.Unrestricted : PermissionState.None);               \r
56                 }\r
57                 \r
58                 public override IPermission Intersect (IPermission target)\r
59                 {\r
60                         DnsPermission dp = Cast (target);\r
61                         if (dp == null)\r
62                                 return null;\r
63                         if (IsUnrestricted () && dp.IsUnrestricted ())\r
64                                 return new DnsPermission (PermissionState.Unrestricted);\r
65                         return null;\r
66                 }\r
67                 \r
68                 public override bool IsSubsetOf (IPermission target) \r
69                 {\r
70                         DnsPermission dp = Cast (target);\r
71                         if (dp == null)\r
72                                 return IsEmpty ();\r
73 \r
74                         return (dp.IsUnrestricted () || (m_noRestriction == dp.m_noRestriction));\r
75                 }\r
76 \r
77                 public bool IsUnrestricted () \r
78                 {\r
79                         return this.m_noRestriction;\r
80                 }\r
81 \r
82                 public override SecurityElement ToXml ()\r
83                 {\r
84                         SecurityElement se = PermissionHelper.Element (typeof (DnsPermission), version);\r
85                         if (m_noRestriction)\r
86                                 se.AddAttribute ("Unrestricted", "true");                               \r
87                         return se;\r
88                 }\r
89                 \r
90                 public override void FromXml (SecurityElement securityElement)\r
91                 {\r
92                         PermissionHelper.CheckSecurityElement (securityElement, "securityElement", version, version);\r
93                 \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
97                                 \r
98                         this.m_noRestriction = PermissionHelper.IsUnrestricted (securityElement);\r
99                 }               \r
100                 \r
101                 public override IPermission Union (IPermission target) \r
102                 {\r
103                         DnsPermission dp = Cast (target);\r
104                         if (dp == null)\r
105                                 return Copy ();\r
106                         if (IsUnrestricted () || dp.IsUnrestricted ())\r
107                                 return new DnsPermission (PermissionState.Unrestricted);\r
108                         else\r
109                                 return new DnsPermission (PermissionState.None);\r
110                 }\r
111 \r
112                 // Internal helpers methods\r
113 \r
114                 private bool IsEmpty ()\r
115                 {\r
116                         return !m_noRestriction;\r
117                 }\r
118 \r
119                 private DnsPermission Cast (IPermission target)\r
120                 {\r
121                         if (target == null)\r
122                                 return null;\r
123 \r
124                         DnsPermission dp = (target as DnsPermission);\r
125                         if (dp == null) {\r
126                                 PermissionHelper.ThrowInvalidPermission (target, typeof (DnsPermission));\r
127                         }\r
128 \r
129                         return dp;\r
130                 }\r
131         }\r
132 }\r