PageRenderTime 3104ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/mcs/class/System/System.Net/EndpointPermission.cs

https://github.com/iainlane/mono
C# | 349 lines | 253 code | 57 blank | 39 comment | 112 complexity | 9c48415cab2c60cab0af998e13f2f1e2 MD5 | raw file
  1. //
  2. // System.Net.EndpointPermission.cs
  3. //
  4. // Author:
  5. // Lawrence Pit (loz@cable.a2000.nl)
  6. //
  7. //
  8. // Permission is hereby granted, free of charge, to any person obtaining
  9. // a copy of this software and associated documentation files (the
  10. // "Software"), to deal in the Software without restriction, including
  11. // without limitation the rights to use, copy, modify, merge, publish,
  12. // distribute, sublicense, and/or sell copies of the Software, and to
  13. // permit persons to whom the Software is furnished to do so, subject to
  14. // the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be
  17. // included in all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  20. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  21. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  22. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  23. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  24. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  25. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  26. //
  27. using System;
  28. using System.Collections;
  29. using System.Security;
  30. using System.Security.Permissions;
  31. namespace System.Net
  32. {
  33. [Serializable]
  34. public class EndpointPermission // too bad about the lowercase p, not consistent with IPEndPoint ;)
  35. {
  36. private static char [] dot_char = new char [] { '.' };
  37. // Fields
  38. private string hostname;
  39. private int port;
  40. private TransportType transport;
  41. private bool resolved;
  42. private bool hasWildcard;
  43. private IPAddress [] addresses;
  44. // Constructors
  45. internal EndpointPermission (string hostname,
  46. int port,
  47. TransportType transport) : base ()
  48. {
  49. if (hostname == null)
  50. throw new ArgumentNullException ("hostname");
  51. this.hostname = hostname;
  52. this.port = port;
  53. this.transport = transport;
  54. this.resolved = false;
  55. this.hasWildcard = false;
  56. this.addresses = null;
  57. }
  58. // Properties
  59. public string Hostname {
  60. get { return hostname; }
  61. }
  62. public int Port {
  63. get { return port; }
  64. }
  65. public TransportType Transport {
  66. get { return transport; }
  67. }
  68. // Methods
  69. public override bool Equals (object obj)
  70. {
  71. EndpointPermission epp = obj as EndpointPermission;
  72. return ((epp != null) &&
  73. (this.port == epp.port) &&
  74. (this.transport == epp.transport) &&
  75. (String.Compare (this.hostname, epp.hostname, true) == 0));
  76. }
  77. public override int GetHashCode ()
  78. {
  79. return ToString ().GetHashCode ();
  80. }
  81. public override string ToString ()
  82. {
  83. return hostname + "#" + port + "#" + (int) transport;
  84. }
  85. // Internal & Private Methods
  86. internal bool IsSubsetOf (EndpointPermission perm)
  87. {
  88. if (perm == null)
  89. return false;
  90. if (perm.port != SocketPermission.AllPorts &&
  91. this.port != perm.port)
  92. return false;
  93. if (perm.transport != TransportType.All &&
  94. this.transport != perm.transport)
  95. return false;
  96. this.Resolve ();
  97. perm.Resolve ();
  98. if (this.hasWildcard) {
  99. if (perm.hasWildcard)
  100. return IsSubsetOf (this.hostname, perm.hostname);
  101. else
  102. return false;
  103. }
  104. if (this.addresses == null)
  105. return false;
  106. if (perm.hasWildcard)
  107. // a bit dubious... should they all be a subset or is one
  108. // enough in this case?
  109. foreach (IPAddress addr in this.addresses)
  110. if (IsSubsetOf (addr.ToString (), perm.hostname))
  111. return true;
  112. if (perm.addresses == null)
  113. return false;
  114. // a bit dubious... should they all be a subset or is one
  115. // enough in this case?
  116. foreach (IPAddress addr in perm.addresses)
  117. if (IsSubsetOf (this.hostname, addr.ToString ()))
  118. return true;
  119. return false;
  120. }
  121. private bool IsSubsetOf (string addr1, string addr2)
  122. {
  123. string [] h1 = addr1.Split (dot_char);
  124. string [] h2 = addr2.Split (dot_char);
  125. for (int i = 0; i < 4; i++) {
  126. int part1 = ToNumber (h1 [i]);
  127. if (part1 == -1)
  128. return false;
  129. int part2 = ToNumber (h2 [i]);
  130. if (part2 == -1)
  131. return false;
  132. if (part1 == 256)
  133. continue;
  134. if (part1 != part2 && part2 != 256)
  135. return false;
  136. }
  137. return true;
  138. }
  139. internal EndpointPermission Intersect (EndpointPermission perm)
  140. {
  141. if (perm == null)
  142. return null;
  143. int _port;
  144. if (this.port == perm.port)
  145. _port = this.port;
  146. else if (this.port == SocketPermission.AllPorts)
  147. _port = perm.port;
  148. else if (perm.port == SocketPermission.AllPorts)
  149. _port = this.port;
  150. else
  151. return null;
  152. TransportType _transport;
  153. if (this.transport == perm.transport)
  154. _transport = this.transport;
  155. else if (this.transport == TransportType.All)
  156. _transport = perm.transport;
  157. else if (perm.transport == TransportType.All)
  158. _transport = this.transport;
  159. else
  160. return null;
  161. string _hostname = IntersectHostname (perm);
  162. if (_hostname == null)
  163. return null;
  164. if (!this.hasWildcard)
  165. return this;
  166. if (!perm.hasWildcard)
  167. return perm;
  168. EndpointPermission newperm = new EndpointPermission (_hostname, _port, _transport);
  169. newperm.hasWildcard = true;
  170. newperm.resolved = true;
  171. return newperm;
  172. }
  173. private string IntersectHostname (EndpointPermission perm)
  174. {
  175. if (this.hostname == perm.hostname)
  176. return this.hostname;
  177. this.Resolve ();
  178. perm.Resolve ();
  179. string _hostname = null;
  180. if (this.hasWildcard) {
  181. if (perm.hasWildcard) {
  182. _hostname = Intersect (this.hostname, perm.hostname);
  183. } else if (perm.addresses != null) {
  184. for (int j = 0; j < perm.addresses.Length; j++) {
  185. _hostname = Intersect (this.hostname, perm.addresses [j].ToString ());
  186. if (_hostname != null)
  187. break;
  188. }
  189. }
  190. } else if (this.addresses != null) {
  191. for (int i = 0; i < this.addresses.Length; i++) {
  192. string thisaddr = this.addresses [i].ToString ();
  193. if (perm.hasWildcard) {
  194. _hostname = Intersect (thisaddr, perm.hostname);
  195. } else if (perm.addresses != null) {
  196. for (int j = 0; j < perm.addresses.Length; j++) {
  197. _hostname = Intersect (thisaddr, perm.addresses [j].ToString ());
  198. if (_hostname != null)
  199. break;
  200. }
  201. }
  202. }
  203. }
  204. return _hostname;
  205. }
  206. // alas, currently we'll only support IPv4 as that's MS.Net behaviour
  207. // returns null when both host strings do not intersect
  208. private string Intersect (string addr1, string addr2)
  209. {
  210. string [] h1 = addr1.Split (dot_char);
  211. string [] h2 = addr2.Split (dot_char);
  212. string [] s = new string [7];
  213. for (int i = 0; i < 4; i++) {
  214. int part1 = ToNumber (h1 [i]);
  215. if (part1 == -1)
  216. return null;
  217. int part2 = ToNumber (h2 [i]);
  218. if (part2 == -1)
  219. return null;
  220. if (part1 == 256)
  221. s [i << 1] = (part2 == 256) ? "*" : String.Empty + part2;
  222. else if (part2 == 256)
  223. s [i << 1] = (part1 == 256) ? "*" : String.Empty + part1;
  224. else if (part1 == part2)
  225. s [i << 1] = String.Empty + part1;
  226. else
  227. return null;
  228. }
  229. s [1] = s [3] = s [5] = ".";
  230. return String.Concat (s);
  231. }
  232. // returns 256 if value is a '*' character
  233. // returns -1 if value isn't a number between 0 and 255
  234. private int ToNumber (string value)
  235. {
  236. if (value == "*")
  237. return 256;
  238. int len = value.Length;
  239. if (len < 1 || len > 3)
  240. return -1;
  241. int val = 0;
  242. for (int i = 0; i < len; i++) {
  243. char c = value [i];
  244. if ('0' <= c && c <= '9')
  245. val = checked (val * 10 + (c - '0'));
  246. else
  247. return -1;
  248. }
  249. return val <= 255 ? val : -1;
  250. }
  251. internal void Resolve ()
  252. {
  253. if (resolved)
  254. return;
  255. bool isHostname = false;
  256. bool hasWildcard = false;
  257. this.addresses = null;
  258. string [] s = hostname.Split (dot_char);
  259. if (s.Length != 4) {
  260. isHostname = true;
  261. } else {
  262. for (int i = 0; i < 4; i++) {
  263. int quad = ToNumber (s [i]);
  264. if (quad == -1) {
  265. isHostname = true;
  266. break;
  267. }
  268. if (quad == 256)
  269. hasWildcard = true;
  270. }
  271. }
  272. if (isHostname) {
  273. this.hasWildcard = false;
  274. try {
  275. this.addresses = Dns.GetHostAddresses (hostname);
  276. } catch (System.Net.Sockets.SocketException) {
  277. }
  278. } else {
  279. this.hasWildcard = hasWildcard;
  280. if (!hasWildcard) {
  281. addresses = new IPAddress [1];
  282. addresses [0] = IPAddress.Parse (hostname);
  283. }
  284. }
  285. this.resolved = true;
  286. }
  287. internal void UndoResolve ()
  288. {
  289. resolved = false;
  290. }
  291. }
  292. }