[System] Exclude code that tries to load System.Windows.Forms.dll dynamically on...
[mono-project.git] / mcs / class / System / System.Net / SocketPermissionAttribute.cs
blob1ccb8f107f93fb1486018a02ef86842b08825b45
1 //
2 // System.Net.SocketPermissionAttribute.cs
3 //
4 // Author:
5 // Lawrence Pit (loz@cable.a2000.nl)
6 // Sebastien Pouliot <sebastien@ximian.com>
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.
30 using System.Security;
31 using System.Security.Permissions;
33 namespace System.Net {
35 [AttributeUsage (AttributeTargets.Assembly
36 | AttributeTargets.Class
37 | AttributeTargets.Struct
38 | AttributeTargets.Constructor
39 | AttributeTargets.Method, AllowMultiple = true, Inherited = false)
41 [Serializable]
42 public sealed class SocketPermissionAttribute : CodeAccessSecurityAttribute {
44 // Fields
45 string m_access;
46 string m_host;
47 string m_port;
48 string m_transport;
50 // Constructors
51 public SocketPermissionAttribute (SecurityAction action)
52 : base (action)
56 // Properties
58 public string Access {
59 get { return m_access; }
60 set {
61 if (m_access != null)
62 AlreadySet ("Access");
64 m_access = value;
68 public string Host {
69 get { return m_host; }
70 set {
71 if (m_host != null)
72 AlreadySet ("Host");
74 m_host = value;
78 public string Port {
79 get { return m_port; }
80 set {
81 if (m_port != null)
82 AlreadySet ("Port");
84 m_port = value;
88 public string Transport {
89 get { return m_transport; }
90 set {
91 if (m_transport != null)
92 AlreadySet ("Transport");
94 m_transport = value;
98 // Methods
100 public override IPermission CreatePermission ()
102 if (this.Unrestricted)
103 return new SocketPermission (PermissionState.Unrestricted);
105 string missing = String.Empty;
106 if (m_access == null)
107 missing += "Access, ";
108 if (m_host == null)
109 missing += "Host, ";
110 if (m_port == null)
111 missing += "Port, ";
112 if (m_transport == null)
113 missing += "Transport, ";
114 if (missing.Length > 0) {
115 string msg = Locale.GetText ("The value(s) for {0} must be specified.");
116 missing = missing.Substring (0, missing.Length - 2); // remove last separator
117 throw new ArgumentException (String.Format (msg, missing));
120 NetworkAccess access;
121 TransportType transport;
122 int port = SocketPermission.AllPorts;
124 if (String.Compare (m_access, "Connect", true) == 0)
125 access = NetworkAccess.Connect;
126 else if (String.Compare (m_access, "Accept", true) == 0)
127 access = NetworkAccess.Accept;
128 else {
129 string msg = Locale.GetText ("The parameter value for 'Access', '{1}, is invalid.");
130 throw new ArgumentException (String.Format (msg, m_access));
133 if (String.Compare (m_port, "All", true) != 0) {
134 try {
135 port = Int32.Parse (m_port);
137 catch {
138 string msg = Locale.GetText ("The parameter value for 'Port', '{1}, is invalid.");
139 throw new ArgumentException (String.Format (msg, m_port));
141 // test whether port number is valid..
142 new IPEndPoint (1, port);
145 try {
146 transport = (TransportType) Enum.Parse (typeof (TransportType), m_transport, true);
148 catch {
149 string msg = Locale.GetText ("The parameter value for 'Transport', '{1}, is invalid.");
150 throw new ArgumentException (String.Format (msg, m_transport));
153 SocketPermission perm = new SocketPermission (PermissionState.None);
154 perm.AddPermission (access, transport, m_host, port);
155 return perm;
158 // helpers
160 internal void AlreadySet (string property)
162 string msg = Locale.GetText ("The parameter '{0}' can be set only once.");
163 throw new ArgumentException (String.Format (msg, property), property);