DnsPermission.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. //
  2. // System.Net.DnsPermission.cs
  3. //
  4. // Authors:
  5. // Lawrence Pit ([email protected])
  6. // Sebastien Pouliot <[email protected]>
  7. //
  8. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  9. //
  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:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  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.
  28. //
  29. using System.Collections;
  30. using System.Security;
  31. using System.Security.Permissions;
  32. namespace System.Net {
  33. [Serializable]
  34. public sealed class DnsPermission : CodeAccessPermission, IUnrestrictedPermission {
  35. private const int version = 1;
  36. // Fields
  37. bool m_noRestriction;
  38. // Constructors
  39. public DnsPermission (PermissionState state)
  40. : base ()
  41. {
  42. m_noRestriction = (state == PermissionState.Unrestricted);
  43. }
  44. // Methods
  45. public override IPermission Copy ()
  46. {
  47. return new DnsPermission (m_noRestriction ? PermissionState.Unrestricted : PermissionState.None);
  48. }
  49. public override IPermission Intersect (IPermission target)
  50. {
  51. DnsPermission dp = Cast (target);
  52. if (dp == null)
  53. return null;
  54. if (IsUnrestricted () && dp.IsUnrestricted ())
  55. return new DnsPermission (PermissionState.Unrestricted);
  56. return null;
  57. }
  58. public override bool IsSubsetOf (IPermission target)
  59. {
  60. DnsPermission dp = Cast (target);
  61. if (dp == null)
  62. return IsEmpty ();
  63. return (dp.IsUnrestricted () || (m_noRestriction == dp.m_noRestriction));
  64. }
  65. public bool IsUnrestricted ()
  66. {
  67. return this.m_noRestriction;
  68. }
  69. public override SecurityElement ToXml ()
  70. {
  71. SecurityElement se = PermissionHelper.Element (typeof (DnsPermission), version);
  72. if (m_noRestriction)
  73. se.AddAttribute ("Unrestricted", "true");
  74. return se;
  75. }
  76. public override void FromXml (SecurityElement securityElement)
  77. {
  78. PermissionHelper.CheckSecurityElement (securityElement, "securityElement", version, version);
  79. // LAMESPEC: it says to throw an ArgumentNullException in this case
  80. if (securityElement.Tag != "IPermission")
  81. throw new ArgumentException ("securityElement");
  82. this.m_noRestriction = PermissionHelper.IsUnrestricted (securityElement);
  83. }
  84. public override IPermission Union (IPermission target)
  85. {
  86. DnsPermission dp = Cast (target);
  87. if (dp == null)
  88. return Copy ();
  89. if (IsUnrestricted () || dp.IsUnrestricted ())
  90. return new DnsPermission (PermissionState.Unrestricted);
  91. else
  92. return new DnsPermission (PermissionState.None);
  93. }
  94. // Internal helpers methods
  95. private bool IsEmpty ()
  96. {
  97. return !m_noRestriction;
  98. }
  99. private DnsPermission Cast (IPermission target)
  100. {
  101. if (target == null)
  102. return null;
  103. DnsPermission dp = (target as DnsPermission);
  104. if (dp == null) {
  105. PermissionHelper.ThrowInvalidPermission (target, typeof (DnsPermission));
  106. }
  107. return dp;
  108. }
  109. }
  110. }