EndpointPermission.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. //
  2. // System.Net.EndpointPermission.cs
  3. //
  4. // Author:
  5. // Lawrence Pit ([email protected])
  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 != part2 && part2 != 256)
  133. return false;
  134. }
  135. return true;
  136. }
  137. internal EndpointPermission Intersect (EndpointPermission perm)
  138. {
  139. if (perm == null)
  140. return null;
  141. int _port;
  142. if (this.port == perm.port)
  143. _port = this.port;
  144. else if (this.port == SocketPermission.AllPorts)
  145. _port = perm.port;
  146. else if (perm.port == SocketPermission.AllPorts)
  147. _port = this.port;
  148. else
  149. return null;
  150. TransportType _transport;
  151. if (this.transport == perm.transport)
  152. _transport = this.transport;
  153. else if (this.transport == TransportType.All)
  154. _transport = perm.transport;
  155. else if (perm.transport == TransportType.All)
  156. _transport = this.transport;
  157. else
  158. return null;
  159. string _hostname = IntersectHostname (perm);
  160. if (_hostname == null)
  161. return null;
  162. if (!this.hasWildcard)
  163. return this;
  164. if (!perm.hasWildcard)
  165. return perm;
  166. EndpointPermission newperm = new EndpointPermission (_hostname, _port, _transport);
  167. newperm.hasWildcard = true;
  168. newperm.resolved = true;
  169. return newperm;
  170. }
  171. private string IntersectHostname (EndpointPermission perm)
  172. {
  173. if (this.hostname == perm.hostname)
  174. return this.hostname;
  175. this.Resolve ();
  176. perm.Resolve ();
  177. string _hostname = null;
  178. if (this.hasWildcard) {
  179. if (perm.hasWildcard) {
  180. _hostname = Intersect (this.hostname, perm.hostname);
  181. } else if (perm.addresses != null) {
  182. for (int j = 0; j < perm.addresses.Length; j++) {
  183. _hostname = Intersect (this.hostname, perm.addresses [j].ToString ());
  184. if (_hostname != null)
  185. break;
  186. }
  187. }
  188. } else if (this.addresses != null) {
  189. for (int i = 0; i < this.addresses.Length; i++) {
  190. string thisaddr = this.addresses [i].ToString ();
  191. if (perm.hasWildcard) {
  192. _hostname = Intersect (thisaddr, perm.hostname);
  193. } else if (perm.addresses != null) {
  194. for (int j = 0; j < perm.addresses.Length; j++) {
  195. _hostname = Intersect (thisaddr, perm.addresses [j].ToString ());
  196. if (_hostname != null)
  197. break;
  198. }
  199. }
  200. }
  201. }
  202. return _hostname;
  203. }
  204. // alas, currently we'll only support IPv4 as that's MS.Net behaviour
  205. // returns null when both host strings do not intersect
  206. private string Intersect (string addr1, string addr2)
  207. {
  208. string [] h1 = addr1.Split (dot_char);
  209. string [] h2 = addr2.Split (dot_char);
  210. string [] s = new string [7];
  211. for (int i = 0; i < 4; i++) {
  212. int part1 = ToNumber (h1 [i]);
  213. if (part1 == -1)
  214. return null;
  215. int part2 = ToNumber (h2 [i]);
  216. if (part2 == -1)
  217. return null;
  218. if (part1 == 256)
  219. s [i << 1] = (part2 == 256) ? "*" : String.Empty + part2;
  220. else if (part2 == 256)
  221. s [i << 1] = (part1 == 256) ? "*" : String.Empty + part1;
  222. else if (part1 == part2)
  223. s [i << 1] = String.Empty + part1;
  224. else
  225. return null;
  226. }
  227. s [1] = s [3] = s [5] = ".";
  228. return String.Concat (s);
  229. }
  230. // returns 256 if value is a '*' character
  231. // returns -1 if value isn't a number between 0 and 255
  232. private int ToNumber (string value)
  233. {
  234. if (value == "*")
  235. return 256;
  236. int len = value.Length;
  237. if (len < 1 || len > 3)
  238. return -1;
  239. int val = 0;
  240. for (int i = 0; i < len; i++) {
  241. char c = value [i];
  242. if ('0' <= c && c <= '9')
  243. val = checked (val * 10 + (c - '0'));
  244. else
  245. return -1;
  246. }
  247. return val <= 255 ? val : -1;
  248. }
  249. internal void Resolve ()
  250. {
  251. if (resolved)
  252. return;
  253. bool isHostname = false;
  254. bool hasWildcard = false;
  255. this.addresses = null;
  256. string [] s = hostname.Split (dot_char);
  257. if (s.Length != 4) {
  258. isHostname = true;
  259. } else {
  260. for (int i = 0; i < 4; i++) {
  261. int quad = ToNumber (s [i]);
  262. if (quad == -1) {
  263. isHostname = true;
  264. break;
  265. }
  266. if (quad == 256)
  267. hasWildcard = true;
  268. }
  269. }
  270. if (isHostname) {
  271. this.hasWildcard = false;
  272. try {
  273. this.addresses = Dns.GetHostByName (hostname).AddressList;
  274. } catch (System.Net.Sockets.SocketException) {
  275. }
  276. } else {
  277. this.hasWildcard = hasWildcard;
  278. if (!hasWildcard) {
  279. addresses = new IPAddress [1];
  280. addresses [0] = IPAddress.Parse (hostname);
  281. }
  282. }
  283. this.resolved = true;
  284. }
  285. internal void UndoResolve ()
  286. {
  287. resolved = false;
  288. }
  289. }
  290. }