EndpointPermission.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. //
  2. // System.Net.EndpointPermission.cs
  3. //
  4. // Author:
  5. // Lawrence Pit ([email protected])
  6. //
  7. using System;
  8. using System.Collections;
  9. using System.Security;
  10. using System.Security.Permissions;
  11. namespace System.Net
  12. {
  13. [Serializable]
  14. public class EndpointPermission // too bad about the lowercase p, not consistent with IPEndPoint ;)
  15. {
  16. private static char [] dot_char = new char [] { '.' };
  17. // Fields
  18. private string hostname;
  19. private int port;
  20. private TransportType transport;
  21. private bool resolved;
  22. private bool hasWildcard;
  23. private IPAddress [] addresses;
  24. // Constructors
  25. internal EndpointPermission (string hostname,
  26. int port,
  27. TransportType transport) : base ()
  28. {
  29. if (hostname == null)
  30. throw new ArgumentNullException ("hostname");
  31. this.hostname = hostname;
  32. this.port = port;
  33. this.transport = transport;
  34. this.resolved = false;
  35. this.hasWildcard = false;
  36. this.addresses = null;
  37. }
  38. // Properties
  39. public string Hostname {
  40. get { return hostname; }
  41. }
  42. public int Port {
  43. get { return port; }
  44. }
  45. public TransportType Transport {
  46. get { return transport; }
  47. }
  48. // Methods
  49. public override bool Equals (object obj)
  50. {
  51. EndpointPermission epp = obj as EndpointPermission;
  52. return ((epp != null) &&
  53. (this.port == epp.port) &&
  54. (this.transport == epp.transport) &&
  55. (String.Compare (this.hostname, epp.hostname, true) == 0));
  56. }
  57. public override int GetHashCode ()
  58. {
  59. return ToString ().GetHashCode ();
  60. }
  61. public override string ToString ()
  62. {
  63. return hostname + "#" + port + "#" + (int) transport;
  64. }
  65. // Internal & Private Methods
  66. internal bool IsSubsetOf (EndpointPermission perm)
  67. {
  68. if (perm == null)
  69. return false;
  70. if (perm.port != SocketPermission.AllPorts &&
  71. this.port != perm.port)
  72. return false;
  73. if (perm.transport != TransportType.All &&
  74. this.transport != perm.transport)
  75. return false;
  76. this.Resolve ();
  77. perm.Resolve ();
  78. if (this.hasWildcard) {
  79. if (perm.hasWildcard)
  80. return IsSubsetOf (this.hostname, perm.hostname);
  81. else
  82. return false;
  83. }
  84. if (this.addresses == null)
  85. return false;
  86. if (perm.hasWildcard)
  87. // a bit dubious... should they all be a subset or is one
  88. // enough in this case?
  89. foreach (IPAddress addr in this.addresses)
  90. if (IsSubsetOf (addr.ToString (), perm.hostname))
  91. return true;
  92. if (perm.addresses == null)
  93. return false;
  94. // a bit dubious... should they all be a subset or is one
  95. // enough in this case?
  96. foreach (IPAddress addr in perm.addresses)
  97. if (IsSubsetOf (this.hostname, addr.ToString ()))
  98. return true;
  99. return false;
  100. }
  101. private bool IsSubsetOf (string addr1, string addr2)
  102. {
  103. string [] h1 = addr1.Split (dot_char);
  104. string [] h2 = addr2.Split (dot_char);
  105. for (int i = 0; i < 4; i++) {
  106. int part1 = ToNumber (h1 [i]);
  107. if (part1 == -1)
  108. return false;
  109. int part2 = ToNumber (h2 [i]);
  110. if (part2 == -1)
  111. return false;
  112. if (part1 != part2 && part2 != 256)
  113. return false;
  114. }
  115. return true;
  116. }
  117. internal EndpointPermission Intersect (EndpointPermission perm)
  118. {
  119. if (perm == null)
  120. return null;
  121. int _port;
  122. if (this.port == perm.port)
  123. _port = this.port;
  124. else if (this.port == SocketPermission.AllPorts)
  125. _port = perm.port;
  126. else if (perm.port == SocketPermission.AllPorts)
  127. _port = this.port;
  128. else
  129. return null;
  130. TransportType _transport;
  131. if (this.transport == perm.transport)
  132. _transport = this.transport;
  133. else if (this.transport == TransportType.All)
  134. _transport = perm.transport;
  135. else if (perm.transport == TransportType.All)
  136. _transport = this.transport;
  137. else
  138. return null;
  139. string _hostname = IntersectHostname (perm);
  140. if (_hostname == null)
  141. return null;
  142. if (!this.hasWildcard)
  143. return this;
  144. if (!perm.hasWildcard)
  145. return perm;
  146. EndpointPermission newperm = new EndpointPermission (_hostname, _port, _transport);
  147. newperm.hasWildcard = true;
  148. newperm.resolved = true;
  149. return newperm;
  150. }
  151. private string IntersectHostname (EndpointPermission perm)
  152. {
  153. if (this.hostname == perm.hostname)
  154. return this.hostname;
  155. this.Resolve ();
  156. perm.Resolve ();
  157. string _hostname = null;
  158. if (this.hasWildcard) {
  159. if (perm.hasWildcard) {
  160. _hostname = Intersect (this.hostname, perm.hostname);
  161. } else if (perm.addresses != null) {
  162. for (int j = 0; j < perm.addresses.Length; j++) {
  163. _hostname = Intersect (this.hostname, perm.addresses [j].ToString ());
  164. if (_hostname != null)
  165. break;
  166. }
  167. }
  168. } else if (this.addresses != null) {
  169. for (int i = 0; i < this.addresses.Length; i++) {
  170. string thisaddr = this.addresses [i].ToString ();
  171. if (perm.hasWildcard) {
  172. _hostname = Intersect (thisaddr, perm.hostname);
  173. } else if (perm.addresses != null) {
  174. for (int j = 0; j < perm.addresses.Length; j++) {
  175. _hostname = Intersect (thisaddr, perm.addresses [j].ToString ());
  176. if (_hostname != null)
  177. break;
  178. }
  179. }
  180. }
  181. }
  182. return _hostname;
  183. }
  184. // alas, currently we'll only support IPv4 as that's MS.Net behaviour
  185. // returns null when both host strings do not intersect
  186. private string Intersect (string addr1, string addr2)
  187. {
  188. string [] h1 = addr1.Split (dot_char);
  189. string [] h2 = addr2.Split (dot_char);
  190. string [] s = new string [7];
  191. for (int i = 0; i < 4; i++) {
  192. int part1 = ToNumber (h1 [i]);
  193. if (part1 == -1)
  194. return null;
  195. int part2 = ToNumber (h2 [i]);
  196. if (part2 == -1)
  197. return null;
  198. if (part1 == 256)
  199. s [i << 1] = (part2 == 256) ? "*" : String.Empty + part2;
  200. else if (part2 == 256)
  201. s [i << 1] = (part1 == 256) ? "*" : String.Empty + part1;
  202. else if (part1 == part2)
  203. s [i << 1] = String.Empty + part1;
  204. else
  205. return null;
  206. }
  207. s [1] = s [3] = s [5] = ".";
  208. return String.Concat (s);
  209. }
  210. // returns 256 if value is a '*' character
  211. // returns -1 if value isn't a number between 0 and 255
  212. private int ToNumber (string value)
  213. {
  214. if (value == "*")
  215. return 256;
  216. int len = value.Length;
  217. if (len < 1 || len > 3)
  218. return -1;
  219. int val = 0;
  220. for (int i = 0; i < len; i++) {
  221. char c = value [i];
  222. if ('0' <= c && c <= '9')
  223. val = checked (val * 10 + (c - '0'));
  224. else
  225. return -1;
  226. }
  227. return val <= 255 ? val : -1;
  228. }
  229. internal void Resolve ()
  230. {
  231. if (resolved)
  232. return;
  233. bool isHostname = false;
  234. bool hasWildcard = false;
  235. this.addresses = null;
  236. string [] s = hostname.Split (dot_char);
  237. if (s.Length != 4) {
  238. isHostname = true;
  239. } else {
  240. for (int i = 0; i < 4; i++) {
  241. int quad = ToNumber (s [i]);
  242. if (quad == -1) {
  243. isHostname = true;
  244. break;
  245. }
  246. if (quad == 256)
  247. hasWildcard = true;
  248. }
  249. }
  250. if (isHostname) {
  251. this.hasWildcard = false;
  252. try {
  253. this.addresses = Dns.GetHostByName (hostname).AddressList;
  254. } catch (System.Net.Sockets.SocketException) {
  255. }
  256. } else {
  257. this.hasWildcard = hasWildcard;
  258. if (!hasWildcard) {
  259. addresses = new IPAddress [1];
  260. addresses [0] = IPAddress.Parse (hostname);
  261. }
  262. }
  263. this.resolved = true;
  264. }
  265. internal void UndoResolve ()
  266. {
  267. resolved = false;
  268. }
  269. }
  270. }