mscorlib(4.0.0.0) API with additions
SocketPermission.cs
1 using System.Collections;
3 using System.Security;
5 using System.Threading;
6 
7 namespace System.Net
8 {
10  [Serializable]
12  {
13  private ArrayList m_connectList;
14 
15  private ArrayList m_acceptList;
16 
17  private bool m_noRestriction;
18 
20  public const int AllPorts = -1;
21 
22  internal const int AnyPort = 0;
23 
26  public IEnumerator ConnectList => m_connectList.GetEnumerator();
27 
30  public IEnumerator AcceptList => m_acceptList.GetEnumerator();
31 
35  {
36  initialize();
37  m_noRestriction = (state == PermissionState.Unrestricted);
38  }
39 
40  internal SocketPermission(bool free)
41  {
42  initialize();
43  m_noRestriction = free;
44  }
45 
53  public SocketPermission(NetworkAccess access, TransportType transport, string hostName, int portNumber)
54  {
55  initialize();
56  m_noRestriction = false;
57  AddPermission(access, transport, hostName, portNumber);
58  }
59 
67  public void AddPermission(NetworkAccess access, TransportType transport, string hostName, int portNumber)
68  {
69  if (hostName == null)
70  {
71  throw new ArgumentNullException("hostName");
72  }
73  EndpointPermission endPoint = new EndpointPermission(hostName, portNumber, transport);
74  AddPermission(access, endPoint);
75  }
76 
77  internal void AddPermission(NetworkAccess access, EndpointPermission endPoint)
78  {
79  if (!m_noRestriction)
80  {
81  if ((access & NetworkAccess.Connect) != 0)
82  {
83  m_connectList.Add(endPoint);
84  }
85  if ((access & NetworkAccess.Accept) != 0)
86  {
87  m_acceptList.Add(endPoint);
88  }
89  }
90  }
91 
95  public bool IsUnrestricted()
96  {
97  return m_noRestriction;
98  }
99 
102  public override IPermission Copy()
103  {
104  SocketPermission socketPermission = new SocketPermission(m_noRestriction);
105  socketPermission.m_connectList = (ArrayList)m_connectList.Clone();
106  socketPermission.m_acceptList = (ArrayList)m_acceptList.Clone();
107  return socketPermission;
108  }
109 
110  private bool FindSubset(ArrayList source, ArrayList target)
111  {
112  foreach (EndpointPermission item in source)
113  {
114  bool flag = false;
115  foreach (EndpointPermission item2 in target)
116  {
117  if (item.SubsetMatch(item2))
118  {
119  flag = true;
120  break;
121  }
122  }
123  if (!flag)
124  {
125  return false;
126  }
127  }
128  return true;
129  }
130 
136  public override IPermission Union(IPermission target)
137  {
138  if (target == null)
139  {
140  return Copy();
141  }
142  SocketPermission socketPermission = target as SocketPermission;
143  if (socketPermission == null)
144  {
145  throw new ArgumentException(SR.GetString("net_perm_target"), "target");
146  }
147  if (m_noRestriction || socketPermission.m_noRestriction)
148  {
149  return new SocketPermission(free: true);
150  }
151  SocketPermission socketPermission2 = (SocketPermission)socketPermission.Copy();
152  for (int i = 0; i < m_connectList.Count; i++)
153  {
154  socketPermission2.AddPermission(NetworkAccess.Connect, (EndpointPermission)m_connectList[i]);
155  }
156  for (int j = 0; j < m_acceptList.Count; j++)
157  {
158  socketPermission2.AddPermission(NetworkAccess.Accept, (EndpointPermission)m_acceptList[j]);
159  }
160  return socketPermission2;
161  }
162 
169  public override IPermission Intersect(IPermission target)
170  {
171  if (target == null)
172  {
173  return null;
174  }
175  SocketPermission socketPermission = target as SocketPermission;
176  if (socketPermission == null)
177  {
178  throw new ArgumentException(SR.GetString("net_perm_target"), "target");
179  }
180  SocketPermission socketPermission2;
181  if (m_noRestriction)
182  {
183  socketPermission2 = (SocketPermission)socketPermission.Copy();
184  }
185  else if (socketPermission.m_noRestriction)
186  {
187  socketPermission2 = (SocketPermission)Copy();
188  }
189  else
190  {
191  socketPermission2 = new SocketPermission(free: false);
192  intersectLists(m_connectList, socketPermission.m_connectList, socketPermission2.m_connectList);
193  intersectLists(m_acceptList, socketPermission.m_acceptList, socketPermission2.m_acceptList);
194  }
195  if (!socketPermission2.m_noRestriction && socketPermission2.m_connectList.Count == 0 && socketPermission2.m_acceptList.Count == 0)
196  {
197  return null;
198  }
199  return socketPermission2;
200  }
201 
209  public override bool IsSubsetOf(IPermission target)
210  {
211  if (target == null)
212  {
213  if (!m_noRestriction && m_connectList.Count == 0)
214  {
215  return m_acceptList.Count == 0;
216  }
217  return false;
218  }
219  SocketPermission socketPermission = target as SocketPermission;
220  if (socketPermission == null)
221  {
222  throw new ArgumentException(SR.GetString("net_perm_target"), "target");
223  }
224  if (socketPermission.IsUnrestricted())
225  {
226  return true;
227  }
228  if (IsUnrestricted())
229  {
230  return false;
231  }
232  if (m_acceptList.Count + m_connectList.Count == 0)
233  {
234  return true;
235  }
236  if (socketPermission.m_acceptList.Count + socketPermission.m_connectList.Count == 0)
237  {
238  return false;
239  }
240  bool result = false;
241  try
242  {
243  if (!FindSubset(m_connectList, socketPermission.m_connectList))
244  {
245  return result;
246  }
247  if (FindSubset(m_acceptList, socketPermission.m_acceptList))
248  {
249  return true;
250  }
251  return result;
252  }
253  finally
254  {
255  CleanupDNS();
256  }
257  }
258 
259  private void CleanupDNS()
260  {
261  foreach (EndpointPermission connect in m_connectList)
262  {
263  if (!connect.cached)
264  {
265  connect.address = null;
266  }
267  }
268  foreach (EndpointPermission accept in m_acceptList)
269  {
270  if (!accept.cached)
271  {
272  accept.address = null;
273  }
274  }
275  }
276 
281  public override void FromXml(SecurityElement securityElement)
282  {
283  if (securityElement == null)
284  {
285  throw new ArgumentNullException("securityElement");
286  }
287  if (!securityElement.Tag.Equals("IPermission"))
288  {
289  throw new ArgumentException(SR.GetString("net_not_ipermission"), "securityElement");
290  }
291  string text = securityElement.Attribute("class");
292  if (text == null)
293  {
294  throw new ArgumentException(SR.GetString("net_no_classname"), "securityElement");
295  }
296  if (text.IndexOf(GetType().FullName) < 0)
297  {
298  throw new ArgumentException(SR.GetString("net_no_typename"), "securityElement");
299  }
300  initialize();
301  string text2 = securityElement.Attribute("Unrestricted");
302  if (text2 != null)
303  {
304  m_noRestriction = (string.Compare(text2, "true", StringComparison.OrdinalIgnoreCase) == 0);
305  if (m_noRestriction)
306  {
307  return;
308  }
309  }
310  m_noRestriction = false;
311  m_connectList = new ArrayList();
312  m_acceptList = new ArrayList();
313  SecurityElement securityElement2 = securityElement.SearchForChildByTag("ConnectAccess");
314  if (securityElement2 != null)
315  {
316  ParseAddXmlElement(securityElement2, m_connectList, "ConnectAccess, ");
317  }
318  securityElement2 = securityElement.SearchForChildByTag("AcceptAccess");
319  if (securityElement2 != null)
320  {
321  ParseAddXmlElement(securityElement2, m_acceptList, "AcceptAccess, ");
322  }
323  }
324 
325  private static void ParseAddXmlElement(SecurityElement et, ArrayList listToAdd, string accessStr)
326  {
327  foreach (SecurityElement child in et.Children)
328  {
329  if (child.Tag.Equals("ENDPOINT"))
330  {
331  Hashtable attributes = child.Attributes;
332  string text;
333  try
334  {
335  text = (attributes["host"] as string);
336  }
337  catch
338  {
339  text = null;
340  }
341  if (text == null)
342  {
343  throw new ArgumentNullException(accessStr + "host");
344  }
345  string epname = text;
346  try
347  {
348  text = (attributes["transport"] as string);
349  }
350  catch
351  {
352  text = null;
353  }
354  if (text == null)
355  {
356  throw new ArgumentNullException(accessStr + "transport");
357  }
358  TransportType trtype;
359  try
360  {
361  trtype = (TransportType)Enum.Parse(typeof(TransportType), text, ignoreCase: true);
362  }
363  catch (Exception ex)
364  {
365  if (ex is ThreadAbortException || ex is StackOverflowException || ex is OutOfMemoryException)
366  {
367  throw;
368  }
369  throw new ArgumentException(accessStr + "transport", ex);
370  }
371  try
372  {
373  text = (attributes["port"] as string);
374  }
375  catch
376  {
377  text = null;
378  }
379  if (text == null)
380  {
381  throw new ArgumentNullException(accessStr + "port");
382  }
383  if (string.Compare(text, "All", StringComparison.OrdinalIgnoreCase) == 0)
384  {
385  text = "-1";
386  }
387  int num;
388  try
389  {
390  num = int.Parse(text, NumberFormatInfo.InvariantInfo);
391  }
392  catch (Exception ex2)
393  {
394  if (ex2 is ThreadAbortException || ex2 is StackOverflowException || ex2 is OutOfMemoryException)
395  {
396  throw;
397  }
398  throw new ArgumentException(SR.GetString("net_perm_invalid_val", accessStr + "port", text), ex2);
399  }
400  if (!ValidationHelper.ValidateTcpPort(num) && num != -1)
401  {
402  throw new ArgumentOutOfRangeException("port", num, SR.GetString("net_perm_invalid_val", accessStr + "port", text));
403  }
404  listToAdd.Add(new EndpointPermission(epname, num, trtype));
405  }
406  }
407  }
408 
411  public override SecurityElement ToXml()
412  {
413  SecurityElement securityElement = new SecurityElement("IPermission");
414  securityElement.AddAttribute("class", GetType().FullName + ", " + GetType().Module.Assembly.FullName.Replace('"', '\''));
415  securityElement.AddAttribute("version", "1");
416  if (!IsUnrestricted())
417  {
418  if (m_connectList.Count > 0)
419  {
420  SecurityElement securityElement2 = new SecurityElement("ConnectAccess");
421  foreach (EndpointPermission connect in m_connectList)
422  {
423  SecurityElement securityElement3 = new SecurityElement("ENDPOINT");
424  securityElement3.AddAttribute("host", connect.Hostname);
425  securityElement3.AddAttribute("transport", connect.Transport.ToString());
426  securityElement3.AddAttribute("port", (connect.Port != -1) ? connect.Port.ToString(NumberFormatInfo.InvariantInfo) : "All");
427  securityElement2.AddChild(securityElement3);
428  }
429  securityElement.AddChild(securityElement2);
430  }
431  if (m_acceptList.Count > 0)
432  {
433  SecurityElement securityElement4 = new SecurityElement("AcceptAccess");
434  foreach (EndpointPermission accept in m_acceptList)
435  {
436  SecurityElement securityElement5 = new SecurityElement("ENDPOINT");
437  securityElement5.AddAttribute("host", accept.Hostname);
438  securityElement5.AddAttribute("transport", accept.Transport.ToString());
439  securityElement5.AddAttribute("port", (accept.Port != -1) ? accept.Port.ToString(NumberFormatInfo.InvariantInfo) : "All");
440  securityElement4.AddChild(securityElement5);
441  }
442  securityElement.AddChild(securityElement4);
443  }
444  }
445  else
446  {
447  securityElement.AddAttribute("Unrestricted", "true");
448  }
449  return securityElement;
450  }
451 
452  private void initialize()
453  {
454  m_noRestriction = false;
455  m_connectList = new ArrayList();
456  m_acceptList = new ArrayList();
457  }
458 
459  private static void intersectLists(ArrayList A, ArrayList B, ArrayList result)
460  {
461  bool[] array = new bool[A.Count];
462  bool[] array2 = new bool[B.Count];
463  int num = 0;
464  int num2 = 0;
465  foreach (EndpointPermission item in A)
466  {
467  num2 = 0;
468  foreach (EndpointPermission item2 in B)
469  {
470  if (!array2[num2] && item.Equals(item2))
471  {
472  result.Add(item);
473  array[num] = (array2[num2] = true);
474  break;
475  }
476  num2++;
477  }
478  num++;
479  }
480  num = 0;
481  foreach (EndpointPermission item3 in A)
482  {
483  if (!array[num])
484  {
485  num2 = 0;
486  foreach (EndpointPermission item4 in B)
487  {
488  if (!array2[num2])
489  {
490  EndpointPermission endpointPermission3 = item3.Intersect(item4);
491  if (endpointPermission3 != null)
492  {
493  bool flag = false;
494  foreach (EndpointPermission item5 in result)
495  {
496  if (item5.Equals(endpointPermission3))
497  {
498  flag = true;
499  break;
500  }
501  }
502  if (!flag)
503  {
504  result.Add(endpointPermission3);
505  }
506  }
507  }
508  num2++;
509  }
510  }
511  num++;
512  }
513  }
514  }
515 }
bool IsUnrestricted()
Checks the overall permission state of the object.
Allows a permission to expose an unrestricted state.
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
void AddPermission(NetworkAccess access, TransportType transport, string hostName, int portNumber)
Adds a permission to the set of permissions for a transport address.
StringComparison
Specifies the culture, case, and sort rules to be used by certain overloads of the M:System....
NetworkAccess
Specifies network access permissions.
Definition: NetworkAccess.cs:5
TransportType Transport
Gets the transport type that is associated with this endpoint.
virtual int Count
Gets the number of elements actually contained in the T:System.Collections.ArrayList.
Definition: ArrayList.cs:2255
Definition: __Canon.cs:3
SecurityElement SearchForChildByTag(string tag)
Finds a child by its tag name.
Defines an endpoint that is authorized by a T:System.Net.SocketPermission instance.
virtual object Clone()
Creates a shallow copy of the T:System.Collections.ArrayList.
Definition: ArrayList.cs:2473
string Tag
Gets or sets the tag name of an XML element.
int Port
Gets the network port number that is associated with this endpoint.
override IPermission Intersect(IPermission target)
Returns the logical intersection between two T:System.Net.SocketPermission instances.
void AddChild(SecurityElement child)
Adds a child element to the XML element.
override SecurityElement ToXml()
Creates an XML encoding of a T:System.Net.SocketPermission instance and its current state.
const int AllPorts
Defines a constant that represents all ports.
Represents the XML object model for encoding security objects. This class cannot be inherited.
Hashtable Attributes
Gets or sets the attributes of an XML element as name/value pairs.
Represents a collection of key/value pairs that are organized based on the hash code of the key....
Definition: Hashtable.cs:17
Defines the underlying structure of all code access permissions.
IEnumerator AcceptList
Gets a list of T:System.Net.EndpointPermission instances that identifies the endpoints that can be ac...
override IPermission Copy()
Creates a copy of a T:System.Net.SocketPermission instance.
Defines methods implemented by permission types.
Definition: IPermission.cs:7
virtual int Add(object value)
Adds an object to the end of the T:System.Collections.ArrayList.
Definition: ArrayList.cs:2381
Controls rights to make or accept connections on a transport address.
The exception that is thrown when one of the arguments provided to a method is not valid.
SocketPermission(PermissionState state)
Initializes a new instance of the T:System.Net.SocketPermission class that allows unrestricted access...
string Hostname
Gets the DNS host name or IP address of the server that is associated with this endpoint.
Attribute can be applied to a module.
TransportType
Defines transport types for the T:System.Net.SocketPermission and T:System.Net.Sockets....
Definition: TransportType.cs:4
Attribute can be applied to an enumeration.
PermissionState
Specifies whether a permission should have all or no access to resources at creation.
void AddAttribute(string name, string value)
Adds a name/value attribute to an XML element.
override bool IsSubsetOf(IPermission target)
Determines if the current permission is a subset of the specified permission.
Specifies that the class can be serialized.
ArrayList Children
Gets or sets the array of child elements of the XML element.
SocketPermission(NetworkAccess access, TransportType transport, string hostName, int portNumber)
Initializes a new instance of the T:System.Net.SocketPermission class for the given transport address...
virtual IEnumerator GetEnumerator()
Returns an enumerator for the entire T:System.Collections.ArrayList.
Definition: ArrayList.cs:2615
IEnumerator ConnectList
Gets a list of T:System.Net.EndpointPermission instances that identifies the endpoints that can be co...
override IPermission Union(IPermission target)
Returns the logical union between two T:System.Net.SocketPermission instances.
The exception that is thrown when a call is made to the M:System.Threading.Thread....
override void FromXml(SecurityElement securityElement)
Reconstructs a T:System.Net.SocketPermission instance for an XML encoding.
Supports a simple iteration over a non-generic collection.
Definition: IEnumerator.cs:9
static NumberFormatInfo InvariantInfo
Gets a read-only T:System.Globalization.NumberFormatInfo object that is culture-independent (invarian...
Implements the T:System.Collections.IList interface using an array whose size is dynamically increase...
Definition: ArrayList.cs:14
Provides culture-specific information for formatting and parsing numeric values.