SocketPermissionAttribute.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. //
  2. // System.Net.SocketPermissionAttribute.cs
  3. //
  4. // Author:
  5. // Lawrence Pit ([email protected])
  6. //
  7. using System;
  8. using System.Security;
  9. using System.Security.Permissions;
  10. namespace System.Net
  11. {
  12. [AttributeUsage (AttributeTargets.Assembly
  13. | AttributeTargets.Class
  14. | AttributeTargets.Struct
  15. | AttributeTargets.Constructor
  16. | AttributeTargets.Method)
  17. ]
  18. [Serializable]
  19. public sealed class SocketPermissionAttribute : CodeAccessSecurityAttribute
  20. {
  21. // Fields
  22. string m_access;
  23. string m_host;
  24. string m_port;
  25. string m_transport;
  26. // Constructors
  27. public SocketPermissionAttribute (SecurityAction action) : base (action)
  28. {
  29. }
  30. // Properties
  31. public string Access {
  32. get { return m_access; }
  33. set {
  34. if (m_access != null)
  35. throw new ArgumentException ("The parameter 'Access' can be set only once.");
  36. if (value == null)
  37. throw new ArgumentException ("The parameter 'Access' cannot be null.");
  38. m_access = value;
  39. }
  40. }
  41. public string Host {
  42. get { return m_host; }
  43. set {
  44. if (m_host != null)
  45. throw new ArgumentException ("The parameter 'Host' can be set only once.");
  46. if (value == null)
  47. throw new ArgumentException ("The parameter 'Host' cannot be null.");
  48. m_host = value;
  49. }
  50. }
  51. public string Port {
  52. get { return m_port; }
  53. set {
  54. if (m_port != null)
  55. throw new ArgumentException ("The parameter 'Port' can be set only once.");
  56. if (value == null)
  57. throw new ArgumentException ("The parameter 'Port' cannot be null.");
  58. m_port = value;
  59. }
  60. }
  61. public string Transport {
  62. get { return m_transport; }
  63. set {
  64. if (m_transport != null)
  65. throw new ArgumentException ("The parameter 'Transport' can be set only once.");
  66. if (value == null)
  67. throw new ArgumentException ("The parameter 'Transport' cannot be null.");
  68. m_transport = value;
  69. }
  70. }
  71. // Methods
  72. public override IPermission CreatePermission () {
  73. if (this.Unrestricted)
  74. return new SocketPermission (PermissionState.Unrestricted);
  75. if (m_access == null)
  76. throw new ArgumentException ("The value for 'Access' must be specified.");
  77. if (m_host == null)
  78. throw new ArgumentException ("The value for 'Host' must be specified.");
  79. if (m_port == null)
  80. throw new ArgumentException ("The value for 'Port' must be specified.");
  81. if (m_transport == null)
  82. throw new ArgumentException ("The value for 'Transport' must be specified.");
  83. NetworkAccess access;
  84. TransportType transport;
  85. int port = SocketPermission.AllPorts;
  86. if (String.Compare (m_access, "Connect", true) == 0)
  87. access = NetworkAccess.Connect;
  88. else if (String.Compare (m_access, "Accept", true) == 0)
  89. access = NetworkAccess.Accept;
  90. else
  91. throw new ArgumentException ("The parameter value 'Access=" + m_access + "' is invalid.");
  92. if (String.Compare (m_port, "All", true) != 0) {
  93. try {
  94. port = Int32.Parse (m_port);
  95. } catch (Exception) {
  96. throw new ArgumentException ("The parameter value 'Port=" + port + "' is invalid.");
  97. }
  98. // test whether port number is valid..
  99. new IPEndPoint (1, port);
  100. }
  101. try {
  102. transport = (TransportType) Enum.Parse (typeof (TransportType), m_transport, true);
  103. } catch (Exception) {
  104. throw new ArgumentException ("The parameter value 'Transport=" + m_transport + "' is invalid.");
  105. }
  106. SocketPermission perm = new SocketPermission (PermissionState.None);
  107. perm.AddPermission (access, transport, m_host, port);
  108. return perm;
  109. }
  110. }
  111. }