SocketPermissionAttribute.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. //
  2. // System.Net.SocketPermissionAttribute.cs
  3. //
  4. // Author:
  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.Security;
  30. using System.Security.Permissions;
  31. namespace System.Net {
  32. [AttributeUsage (AttributeTargets.Assembly
  33. | AttributeTargets.Class
  34. | AttributeTargets.Struct
  35. | AttributeTargets.Constructor
  36. | AttributeTargets.Method, AllowMultiple = true, Inherited = false)
  37. ]
  38. [Serializable]
  39. public sealed class SocketPermissionAttribute : CodeAccessSecurityAttribute {
  40. // Fields
  41. string m_access;
  42. string m_host;
  43. string m_port;
  44. string m_transport;
  45. // Constructors
  46. public SocketPermissionAttribute (SecurityAction action)
  47. : base (action)
  48. {
  49. }
  50. // Properties
  51. public string Access {
  52. get { return m_access; }
  53. set {
  54. if (m_access != null)
  55. AlreadySet ("Access");
  56. m_access = value;
  57. }
  58. }
  59. public string Host {
  60. get { return m_host; }
  61. set {
  62. if (m_host != null)
  63. AlreadySet ("Host");
  64. m_host = value;
  65. }
  66. }
  67. public string Port {
  68. get { return m_port; }
  69. set {
  70. if (m_port != null)
  71. AlreadySet ("Port");
  72. m_port = value;
  73. }
  74. }
  75. public string Transport {
  76. get { return m_transport; }
  77. set {
  78. if (m_transport != null)
  79. AlreadySet ("Transport");
  80. m_transport = value;
  81. }
  82. }
  83. // Methods
  84. public override IPermission CreatePermission ()
  85. {
  86. if (this.Unrestricted)
  87. return new SocketPermission (PermissionState.Unrestricted);
  88. string missing = String.Empty;
  89. if (m_access == null)
  90. missing += "Access, ";
  91. if (m_host == null)
  92. missing += "Host, ";
  93. if (m_port == null)
  94. missing += "Port, ";
  95. if (m_transport == null)
  96. missing += "Transport, ";
  97. if (missing.Length > 0) {
  98. string msg = Locale.GetText ("The value(s) for {0} must be specified.");
  99. missing = missing.Substring (0, missing.Length - 2); // remove last separator
  100. throw new ArgumentException (String.Format (msg, missing));
  101. }
  102. NetworkAccess access;
  103. TransportType transport;
  104. int port = SocketPermission.AllPorts;
  105. if (String.Compare (m_access, "Connect", true) == 0)
  106. access = NetworkAccess.Connect;
  107. else if (String.Compare (m_access, "Accept", true) == 0)
  108. access = NetworkAccess.Accept;
  109. else {
  110. string msg = Locale.GetText ("The parameter value for 'Access', '{1}, is invalid.");
  111. throw new ArgumentException (String.Format (msg, m_access));
  112. }
  113. if (String.Compare (m_port, "All", true) != 0) {
  114. try {
  115. port = Int32.Parse (m_port);
  116. }
  117. catch {
  118. string msg = Locale.GetText ("The parameter value for 'Port', '{1}, is invalid.");
  119. throw new ArgumentException (String.Format (msg, m_port));
  120. }
  121. // test whether port number is valid..
  122. new IPEndPoint (1, port);
  123. }
  124. try {
  125. transport = (TransportType) Enum.Parse (typeof (TransportType), m_transport, true);
  126. }
  127. catch {
  128. string msg = Locale.GetText ("The parameter value for 'Transport', '{1}, is invalid.");
  129. throw new ArgumentException (String.Format (msg, m_transport));
  130. }
  131. SocketPermission perm = new SocketPermission (PermissionState.None);
  132. perm.AddPermission (access, transport, m_host, port);
  133. return perm;
  134. }
  135. // helpers
  136. internal void AlreadySet (string property)
  137. {
  138. string msg = Locale.GetText ("The parameter '{0}' can be set only once.");
  139. throw new ArgumentException (String.Format (msg, property), property);
  140. }
  141. }
  142. }