2 // System.Net.SocketPermissionAttribute.cs
5 // Lawrence Pit (loz@cable.a2000.nl)
6 // Sebastien Pouliot <sebastien@ximian.com>
8 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
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:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
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)
42 public sealed class SocketPermissionAttribute : CodeAccessSecurityAttribute {
51 public SocketPermissionAttribute (SecurityAction action)
58 public string Access {
59 get { return m_access; }
62 AlreadySet ("Access");
69 get { return m_host; }
79 get { return m_port; }
88 public string Transport {
89 get { return m_transport; }
91 if (m_transport != null)
92 AlreadySet ("Transport");
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, ";
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;
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) {
135 port = Int32.Parse (m_port);
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);
146 transport = (TransportType) Enum.Parse (typeof (TransportType), m_transport, true);
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);
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);