mscorlib(4.0.0.0) API with additions
EndpointPermission.cs
2 using System.Security;
3 
4 namespace System.Net
5 {
8  public class EndpointPermission
9  {
10  private enum EndPointType
11  {
12  Invalid,
13  IPv6,
14  DnsOrWildcard,
15  IPv4
16  }
17 
18  internal string hostname;
19 
20  internal int port;
21 
22  internal TransportType transport;
23 
24  internal bool wildcard;
25 
26  internal IPAddress[] address;
27 
28  internal bool cached;
29 
30  private static char[] DotSeparator = new char[1]
31  {
32  '.'
33  };
34 
35  private const string encSeperator = "#";
36 
39  public string Hostname => hostname;
40 
43  public TransportType Transport => transport;
44 
47  public int Port => port;
48 
49  internal bool IsDns
50  {
51  get
52  {
53  if (IsValidWildcard)
54  {
55  return false;
56  }
57  return CheckEndPointName(hostname) == EndPointType.DnsOrWildcard;
58  }
59  }
60 
61  private bool IsValidWildcard
62  {
63  get
64  {
65  int length = hostname.Length;
66  if (length < 3)
67  {
68  return false;
69  }
70  if (hostname[0] == '.' || hostname[length - 1] == '.')
71  {
72  return false;
73  }
74  int num = 0;
75  int num2 = 0;
76  for (int i = 0; i < hostname.Length; i++)
77  {
78  if (hostname[i] == '.')
79  {
80  num++;
81  }
82  else if (hostname[i] == '*')
83  {
84  num2++;
85  }
86  else if (!char.IsDigit(hostname[i]))
87  {
88  return false;
89  }
90  }
91  if (num == 3)
92  {
93  return num2 > 0;
94  }
95  return false;
96  }
97  }
98 
99  internal EndpointPermission(string epname, int port, TransportType trtype)
100  {
101  if (CheckEndPointName(epname) == EndPointType.Invalid)
102  {
103  throw new ArgumentException(SR.GetString("net_perm_epname", epname), "epname");
104  }
105  if (!ValidationHelper.ValidateTcpPort(port) && port != -1)
106  {
107  throw new ArgumentOutOfRangeException("port", SR.GetString("net_perm_invalid_val", "Port", port.ToString(NumberFormatInfo.InvariantInfo)));
108  }
109  hostname = epname;
110  this.port = port;
111  transport = trtype;
112  wildcard = false;
113  }
114 
119  public override bool Equals(object obj)
120  {
121  EndpointPermission endpointPermission = (EndpointPermission)obj;
122  if (string.Compare(hostname, endpointPermission.hostname, StringComparison.OrdinalIgnoreCase) != 0)
123  {
124  return false;
125  }
126  if (port != endpointPermission.port)
127  {
128  return false;
129  }
130  if (transport != endpointPermission.transport)
131  {
132  return false;
133  }
134  return true;
135  }
136 
139  public override int GetHashCode()
140  {
141  return ToString().GetHashCode();
142  }
143 
144  internal bool MatchAddress(EndpointPermission e)
145  {
146  if (Hostname.Length == 0 || e.Hostname.Length == 0)
147  {
148  return false;
149  }
150  if (Hostname.Equals("0.0.0.0"))
151  {
152  if (e.Hostname.Equals("*.*.*.*") || e.Hostname.Equals("0.0.0.0"))
153  {
154  return true;
155  }
156  return false;
157  }
158  if (IsDns && e.IsDns)
159  {
160  return string.Compare(hostname, e.hostname, StringComparison.OrdinalIgnoreCase) == 0;
161  }
162  Resolve();
163  e.Resolve();
164  if ((address == null && !wildcard) || (e.address == null && !e.wildcard))
165  {
166  return false;
167  }
168  if (wildcard && !e.wildcard)
169  {
170  return false;
171  }
172  if (e.wildcard)
173  {
174  if (wildcard)
175  {
176  if (MatchWildcard(e.hostname))
177  {
178  return true;
179  }
180  }
181  else
182  {
183  for (int i = 0; i < address.Length; i++)
184  {
185  if (e.MatchWildcard(address[i].ToString()))
186  {
187  return true;
188  }
189  }
190  }
191  }
192  else
193  {
194  for (int j = 0; j < address.Length; j++)
195  {
196  for (int k = 0; k < e.address.Length; k++)
197  {
198  if (address[j].Equals(e.address[k]))
199  {
200  return true;
201  }
202  }
203  }
204  }
205  return false;
206  }
207 
208  internal bool MatchWildcard(string str)
209  {
210  string[] array = hostname.Split(DotSeparator);
211  string[] array2 = str.Split(DotSeparator);
212  if (array2.Length != 4 || array.Length != 4)
213  {
214  return false;
215  }
216  for (int i = 0; i < 4; i++)
217  {
218  if (array2[i] != array[i] && array[i] != "*")
219  {
220  return false;
221  }
222  }
223  return true;
224  }
225 
226  internal void Resolve()
227  {
228  if (!cached && !wildcard)
229  {
230  IPAddress iPAddress;
231  if (IsValidWildcard)
232  {
233  wildcard = true;
234  cached = true;
235  }
236  else if (IPAddress.TryParse(hostname, out iPAddress))
237  {
238  address = new IPAddress[1];
239  address[0] = iPAddress;
240  cached = true;
241  }
242  else
243  {
244  try
245  {
246  if (Dns.TryInternalResolve(hostname, out IPHostEntry result))
247  {
248  address = result.AddressList;
249  }
250  }
251  catch (SecurityException)
252  {
253  throw;
254  }
255  catch
256  {
257  }
258  }
259  }
260  }
261 
262  internal bool SubsetMatch(EndpointPermission e)
263  {
264  if ((transport == e.transport || e.transport == TransportType.All) && (port == e.port || e.port == -1 || port == 0))
265  {
266  return MatchAddress(e);
267  }
268  return false;
269  }
270 
273  public override string ToString()
274  {
275  object[] obj = new object[5]
276  {
277  hostname,
278  "#",
279  port,
280  "#",
281  null
282  };
283  int num = (int)transport;
284  obj[4] = num.ToString(NumberFormatInfo.InvariantInfo);
285  return string.Concat(obj);
286  }
287 
288  internal EndpointPermission Intersect(EndpointPermission E)
289  {
290  string text = null;
291  TransportType trtype;
292  if (transport == E.transport)
293  {
294  trtype = transport;
295  }
296  else if (transport == TransportType.All)
297  {
298  trtype = E.transport;
299  }
300  else
301  {
302  if (E.transport != TransportType.All)
303  {
304  return null;
305  }
306  trtype = transport;
307  }
308  int num;
309  if (port == E.port)
310  {
311  num = port;
312  }
313  else if (port == -1)
314  {
315  num = E.port;
316  }
317  else
318  {
319  if (E.port != -1)
320  {
321  return null;
322  }
323  num = port;
324  }
325  if (Hostname.Equals("0.0.0.0"))
326  {
327  if (!E.Hostname.Equals("*.*.*.*") && !E.Hostname.Equals("0.0.0.0"))
328  {
329  return null;
330  }
331  text = Hostname;
332  }
333  else if (E.Hostname.Equals("0.0.0.0"))
334  {
335  if (!Hostname.Equals("*.*.*.*") && !Hostname.Equals("0.0.0.0"))
336  {
337  return null;
338  }
339  text = E.Hostname;
340  }
341  else if (IsDns && E.IsDns)
342  {
343  if (string.Compare(hostname, E.hostname, StringComparison.OrdinalIgnoreCase) != 0)
344  {
345  return null;
346  }
347  text = hostname;
348  }
349  else
350  {
351  Resolve();
352  E.Resolve();
353  if ((address == null && !wildcard) || (E.address == null && !E.wildcard))
354  {
355  return null;
356  }
357  if (wildcard && E.wildcard)
358  {
359  string[] array = hostname.Split(DotSeparator);
360  string[] array2 = E.hostname.Split(DotSeparator);
361  string text2 = "";
362  if (array2.Length != 4 || array.Length != 4)
363  {
364  return null;
365  }
366  for (int i = 0; i < 4; i++)
367  {
368  if (i != 0)
369  {
370  text2 += ".";
371  }
372  if (array2[i] == array[i])
373  {
374  text2 += array2[i];
375  continue;
376  }
377  if (array2[i] == "*")
378  {
379  text2 += array[i];
380  continue;
381  }
382  if (array[i] == "*")
383  {
384  text2 += array2[i];
385  continue;
386  }
387  return null;
388  }
389  text = text2;
390  }
391  else if (wildcard)
392  {
393  for (int j = 0; j < E.address.Length; j++)
394  {
395  if (MatchWildcard(E.address[j].ToString()))
396  {
397  text = E.hostname;
398  break;
399  }
400  }
401  }
402  else if (E.wildcard)
403  {
404  for (int k = 0; k < address.Length; k++)
405  {
406  if (E.MatchWildcard(address[k].ToString()))
407  {
408  text = hostname;
409  break;
410  }
411  }
412  }
413  else
414  {
415  if (address == E.address)
416  {
417  text = hostname;
418  }
419  int num2 = 0;
420  while (text == null && num2 < address.Length)
421  {
422  for (int l = 0; l < E.address.Length; l++)
423  {
424  if (address[num2].Equals(E.address[l]))
425  {
426  text = hostname;
427  break;
428  }
429  }
430  num2++;
431  }
432  }
433  if (text == null)
434  {
435  return null;
436  }
437  }
438  return new EndpointPermission(text, num, trtype);
439  }
440 
441  private static EndPointType CheckEndPointName(string name)
442  {
443  if (name == null)
444  {
445  return EndPointType.Invalid;
446  }
447  bool flag = false;
448  bool flag2 = false;
449  bool flag3 = false;
450  foreach (char c in name)
451  {
452  switch (c)
453  {
454  case '*':
455  case '-':
456  case '_':
457  flag2 = true;
458  continue;
459  case '%':
460  case ':':
461  flag = true;
462  continue;
463  case '.':
464  continue;
465  }
466  if ((c > 'f' && c <= 'z') || (c > 'F' && c <= 'Z'))
467  {
468  flag2 = true;
469  }
470  else if ((c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'))
471  {
472  flag3 = true;
473  }
474  else if (c < '0' || c > '9')
475  {
476  return EndPointType.Invalid;
477  }
478  }
479  if (!flag)
480  {
481  if (!flag2)
482  {
483  if (!flag3)
484  {
485  return EndPointType.IPv4;
486  }
487  return EndPointType.DnsOrWildcard;
488  }
489  return EndPointType.DnsOrWildcard;
490  }
491  if (!flag2)
492  {
493  return EndPointType.IPv6;
494  }
495  return EndPointType.Invalid;
496  }
497  }
498 }
The host name is a domain name system (DNS) style host name.
StringComparison
Specifies the culture, case, and sort rules to be used by certain overloads of the M:System....
TransportType Transport
Gets the transport type that is associated with this endpoint.
Definition: __Canon.cs:3
Defines an endpoint that is authorized by a T:System.Net.SocketPermission instance.
int Port
Gets the network port number that is associated with this endpoint.
override bool Equals(object obj)
Determines whether the specified Object is equal to the current Object.
Provides an Internet Protocol (IP) address.
Definition: IPAddress.cs:10
The host name is an Internet Protocol (IP) version 4 host address.
The host name is an Internet Protocol (IP) version 6 host address.
string Hostname
Gets the DNS host name or IP address of the server that is associated with this endpoint.
TransportType
Defines transport types for the T:System.Net.SocketPermission and T:System.Net.Sockets....
Definition: TransportType.cs:4
override int GetHashCode()
Serves as a hash function for a particular type.
override string ToString()
Returns a string that represents the current T:System.Net.EndpointPermission instance.
Specifies that the class can be serialized.
The exception that is thrown when a security error is detected.
static NumberFormatInfo InvariantInfo
Gets a read-only T:System.Globalization.NumberFormatInfo object that is culture-independent (invarian...
Provides culture-specific information for formatting and parsing numeric values.