2003-06-11 Gonzalo Paniagua Javier <gonzalo@ximian.com>
[mono.git] / mcs / class / System / System.Net / SocketPermissionAttribute.cs
1 //\r
2 // System.Net.SocketPermissionAttribute.cs\r
3 //\r
4 // Author:\r
5 //   Lawrence Pit (loz@cable.a2000.nl)\r
6 //\r
7 \r
8 using System;\r
9 using System.Security;\r
10 using System.Security.Permissions;\r
11 \r
12 namespace System.Net\r
13 {\r
14         [AttributeUsage (AttributeTargets.Assembly \r
15                        | AttributeTargets.Class \r
16                        | AttributeTargets.Struct \r
17                        | AttributeTargets.Constructor \r
18                        | AttributeTargets.Method)\r
19         ]       \r
20         [Serializable]\r
21         public sealed class SocketPermissionAttribute : CodeAccessSecurityAttribute\r
22         {\r
23                 // Fields\r
24                 string m_access;\r
25                 string m_host;\r
26                 string m_port;\r
27                 string m_transport;\r
28                 \r
29                 // Constructors\r
30                 public SocketPermissionAttribute (SecurityAction action) : base (action)\r
31                 {\r
32                 }\r
33 \r
34                 // Properties\r
35 \r
36                 public string Access {\r
37                         get { return m_access; }\r
38                         set { \r
39                                 if (m_access != null)\r
40                                         throw new ArgumentException ("The parameter 'Access' can be set only once.");\r
41                                 if (value == null) \r
42                                         throw new ArgumentException ("The parameter 'Access' cannot be null.");\r
43                                 m_access = value;\r
44                         }\r
45                 }\r
46 \r
47                 public string Host {\r
48                         get { return m_host; }\r
49                         set { \r
50                                 if (m_host != null)\r
51                                         throw new ArgumentException ("The parameter 'Host' can be set only once.");\r
52                                 if (value == null) \r
53                                         throw new ArgumentException ("The parameter 'Host' cannot be null.");                                   \r
54                                 m_host = value;\r
55                         }\r
56                 }\r
57 \r
58                 public string Port {\r
59                         get { return m_port; }\r
60                         set { \r
61                                 if (m_port != null)\r
62                                         throw new ArgumentException ("The parameter 'Port' can be set only once.");\r
63                                 if (value == null) \r
64                                         throw new ArgumentException ("The parameter 'Port' cannot be null.");                                   \r
65                                 m_port = value;\r
66                         }\r
67                 }\r
68 \r
69                 public string Transport {\r
70                         get { return m_transport; }\r
71                         set { \r
72                                 if (m_transport != null)\r
73                                         throw new ArgumentException ("The parameter 'Transport' can be set only once.");\r
74                                 if (value == null) \r
75                                         throw new ArgumentException ("The parameter 'Transport' cannot be null.");                                      \r
76                                 m_transport = value;\r
77                         }\r
78                 }\r
79                 \r
80                 // Methods\r
81                 \r
82                 public override IPermission CreatePermission () {\r
83                         if (this.Unrestricted)\r
84                                 return new SocketPermission (PermissionState.Unrestricted);\r
85 \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
94 \r
95                         NetworkAccess access;\r
96                         TransportType transport;\r
97                         int port = SocketPermission.AllPorts;\r
98 \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
103                         else \r
104                                 throw new ArgumentException ("The parameter value 'Access=" + m_access + "' is invalid.");\r
105 \r
106                         if (String.Compare (m_port, "All", true) != 0) {\r
107                                 try {\r
108                                         port = Int32.Parse (m_port);                                    \r
109                                 } catch (Exception) {\r
110                                         throw new ArgumentException ("The parameter value 'Port=" + port + "' is invalid.");\r
111                                 }\r
112                                 // test whether port number is valid..\r
113                                 new IPEndPoint (1, port);\r
114                         }\r
115 \r
116                         try {\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
120                         }\r
121                                                 \r
122                         SocketPermission perm = new SocketPermission (PermissionState.None);\r
123                         perm.AddPermission (access, transport, m_host, port);\r
124                         return perm;\r
125                 }               \r
126         }\r
127 }\r