2 // System.Net.SocketPermissionAttribute.cs
\r
5 // Lawrence Pit (loz@cable.a2000.nl)
\r
9 using System.Security;
\r
10 using System.Security.Permissions;
\r
12 namespace System.Net
\r
14 [AttributeUsage (AttributeTargets.Assembly
\r
15 | AttributeTargets.Class
\r
16 | AttributeTargets.Struct
\r
17 | AttributeTargets.Constructor
\r
18 | AttributeTargets.Method)
\r
21 public sealed class SocketPermissionAttribute : CodeAccessSecurityAttribute
\r
30 public SocketPermissionAttribute (SecurityAction action) : base (action)
\r
36 public string Access {
\r
37 get { return m_access; }
\r
39 if (m_access != null)
\r
40 throw new ArgumentException ("The parameter 'Access' can be set only once.");
\r
42 throw new ArgumentException ("The parameter 'Access' cannot be null.");
\r
47 public string Host {
\r
48 get { return m_host; }
\r
51 throw new ArgumentException ("The parameter 'Host' can be set only once.");
\r
53 throw new ArgumentException ("The parameter 'Host' cannot be null.");
\r
58 public string Port {
\r
59 get { return m_port; }
\r
62 throw new ArgumentException ("The parameter 'Port' can be set only once.");
\r
64 throw new ArgumentException ("The parameter 'Port' cannot be null.");
\r
69 public string Transport {
\r
70 get { return m_transport; }
\r
72 if (m_transport != null)
\r
73 throw new ArgumentException ("The parameter 'Transport' can be set only once.");
\r
75 throw new ArgumentException ("The parameter 'Transport' cannot be null.");
\r
76 m_transport = value;
\r
82 public override IPermission CreatePermission () {
\r
83 if (this.Unrestricted)
\r
84 return new SocketPermission (PermissionState.Unrestricted);
\r
86 if (m_access == null)
\r
87 throw new ArgumentException ("The value for 'Access' must be specified.");
\r
88 if (m_host == null)
\r
89 throw new ArgumentException ("The value for 'Host' must be specified.");
\r
90 if (m_port == null)
\r
91 throw new ArgumentException ("The value for 'Port' must be specified.");
\r
92 if (m_transport == null)
\r
93 throw new ArgumentException ("The value for 'Transport' must be specified.");
\r
95 NetworkAccess access;
\r
96 TransportType transport;
\r
97 int port = SocketPermission.AllPorts;
\r
99 if (String.Compare (m_access, "Connect", true) == 0)
\r
100 access = NetworkAccess.Connect;
\r
101 else if (String.Compare (m_access, "Accept", true) == 0)
\r
102 access = NetworkAccess.Accept;
\r
104 throw new ArgumentException ("The parameter value 'Access=" + m_access + "' is invalid.");
\r
106 if (String.Compare (m_port, "All", true) != 0) {
\r
108 port = Int32.Parse (m_port);
\r
109 } catch (Exception) {
\r
110 throw new ArgumentException ("The parameter value 'Port=" + port + "' is invalid.");
\r
112 // test whether port number is valid..
\r
113 new IPEndPoint (1, port);
\r
117 transport = (TransportType) Enum.Parse (typeof (TransportType), m_transport, true);
\r
118 } catch (Exception) {
\r
119 throw new ArgumentException ("The parameter value 'Transport=" + m_transport + "' is invalid.");
\r
122 SocketPermission perm = new SocketPermission (PermissionState.None);
\r
123 perm.AddPermission (access, transport, m_host, port);
\r