Welcome to mirror list, hosted at ThFree Co, Russian Federation.

EndpointPermission.cs « System.Net « System « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ca895c7569488aae705b0928a463a6cce95882e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
//
// System.Net.EndpointPermission.cs
//
// Author:
//   Lawrence Pit (loz@cable.a2000.nl)
//

using System;
using System.Collections;
using System.Security;
using System.Security.Permissions;

namespace System.Net
{
	[Serializable]
	public class EndpointPermission // too bad about the lowercase p, not consistent with IPEndPoint ;)
	{
		private static char [] dot_char = new char [] { '.' };
		
		// Fields
		private string hostname;
		private int port;
		private TransportType transport;

		private bool resolved;		
		private bool hasWildcard;
		private IPAddress [] addresses;
		
		// Constructors
		internal EndpointPermission (string hostname, 
				   	     int port, 
				   	     TransportType transport) : base () 
		{			
			if (hostname == null)
				throw new ArgumentNullException ("hostname");
			this.hostname = hostname;
			this.port = port;
			this.transport = transport;
			this.resolved = false;
			this.hasWildcard = false;
			this.addresses = null;
		}		
		
		// Properties

		public string Hostname {
			get { return hostname; }
		}

		public int Port {
			get { return port; }
		}
		
		public TransportType Transport {
			get { return transport; }
		}
		
		// Methods
		
		public override bool Equals (object obj) 
		{
			EndpointPermission epp = obj as EndpointPermission;
			return ((epp != null) &&
			        (this.port == epp.port) &&
			        (this.transport == epp.transport) &&
			        (String.Compare (this.hostname, epp.hostname, true) == 0));
		}
		
		public override int GetHashCode () 
		{
			return ToString ().GetHashCode ();
		}
		
		public override string ToString () 
		{
			return hostname + "#" + port + "#" + (int) transport;
		}
		
		// Internal & Private Methods
		
		internal bool IsSubsetOf (EndpointPermission perm) 
		{
			if (perm == null)
				return false;
			
			if (perm.port != SocketPermission.AllPorts &&
			    this.port != perm.port)
			    	return false;
			
			if (perm.transport != TransportType.All &&
			    this.transport != perm.transport)
				return false;
			
			this.Resolve ();
			perm.Resolve ();
			
			if (this.hasWildcard) {
				if (perm.hasWildcard)
					return IsSubsetOf (this.hostname, perm.hostname);
				else 
					return false;
			} 
			
			if (this.addresses == null) 
				return false;
			
			if (perm.hasWildcard) 
				// a bit dubious... should they all be a subset or is one 
				// enough in this case?
				foreach (IPAddress addr in this.addresses)
					if (IsSubsetOf (addr.ToString (), perm.hostname))
						return true;
				
			if (perm.addresses == null) 
				return false;
				
			// a bit dubious... should they all be a subset or is one 
			// enough in this case?
			foreach (IPAddress addr in perm.addresses)
				if (IsSubsetOf (this.hostname, addr.ToString ())) 
					return true;	
					
			return false;		
		}		
		
		private bool IsSubsetOf (string addr1, string addr2)
		{
			string [] h1 = addr1.Split (dot_char);		
			string [] h2 = addr2.Split (dot_char);
				
			for (int i = 0; i < 4; i++) {
				int part1 = ToNumber (h1 [i]);
				if (part1 == -1) 
					return false;				

				int part2 = ToNumber (h2 [i]);
				if (part2 == -1)
					return false;				
				if (part1 != part2 && part2 != 256)
					return false;
			}
			return true;
		}
		
		internal EndpointPermission Intersect (EndpointPermission perm) 
		{
			if (perm == null)
				return null;
			
			int _port;
			if (this.port == perm.port)
				_port = this.port;
			else if (this.port == SocketPermission.AllPorts)
				_port = perm.port;
			else if (perm.port == SocketPermission.AllPorts)
				_port = this.port;
			else
				return null;

			TransportType _transport;
			if (this.transport == perm.transport)
				_transport = this.transport;
			else if (this.transport == TransportType.All)
				_transport = perm.transport;
			else if (perm.transport == TransportType.All)
				_transport = this.transport;
			else
				return null;

			string _hostname = IntersectHostname (perm);						
			
			if (_hostname == null)
				return null;

			if (!this.hasWildcard)
				return this;
				
			if (!perm.hasWildcard)
				return perm;
				
			EndpointPermission newperm = new EndpointPermission (_hostname, _port, _transport);
			newperm.hasWildcard = true;
			newperm.resolved = true;
			return newperm;
		}
		
		private string IntersectHostname (EndpointPermission perm)
		{
			if (this.hostname == perm.hostname)
				return this.hostname;
				
			this.Resolve ();
			perm.Resolve ();
			
			string _hostname = null;
			
			if (this.hasWildcard) {
				if (perm.hasWildcard) {
					_hostname = Intersect (this.hostname, perm.hostname);
				} else if (perm.addresses != null) {
					for (int j = 0; j < perm.addresses.Length; j++) {
						_hostname = Intersect (this.hostname, perm.addresses [j].ToString ());
						if (_hostname != null) 
							break;
					}
				}
			} else if (this.addresses != null) {
				for (int i = 0; i < this.addresses.Length; i++) {
					string thisaddr = this.addresses [i].ToString ();
					if (perm.hasWildcard) {
						_hostname = Intersect (thisaddr, perm.hostname);
					} else if (perm.addresses != null) {
						for (int j = 0; j < perm.addresses.Length; j++) {
							_hostname = Intersect (thisaddr, perm.addresses [j].ToString ());
							if (_hostname != null) 
								break;
						}
					}
				}
			}
			
			return _hostname;
		}
		
		// alas, currently we'll only support IPv4 as that's MS.Net behaviour
		// returns null when both host strings do not intersect
		private string Intersect (string addr1, string addr2)
		{
			string [] h1 = addr1.Split (dot_char);		
			string [] h2 = addr2.Split (dot_char);
				
			string [] s = new string [7];
			for (int i = 0; i < 4; i++) {
				int part1 = ToNumber (h1 [i]);
				if (part1 == -1) 
					return null;				

				int part2 = ToNumber (h2 [i]);
				if (part2 == -1)
					return null;				

				if (part1 == 256) 
					s [i << 1] = (part2 == 256) ? "*" : String.Empty + part2;
				else if (part2 == 256)
					s [i << 1] = (part1 == 256) ? "*" : String.Empty + part1;				
				else if (part1 == part2)
					s [i << 1] = String.Empty + part1;
				else
					return null;
			}
			
			s [1] = s [3] = s [5] = ".";
			return String.Concat (s);
		}
		
		// returns 256 if value is a '*' character
		// returns -1 if value isn't a number between 0 and 255		
		private int ToNumber (string value)
		{
			if (value == "*")
				return 256;
				
			int len = value.Length;
			if (len < 1 || len > 3)
				return -1;
				
			int val = 0;				
			for (int i = 0; i < len; i++) {
				char c = value [i];
				if ('0' <= c && c <= '9') 
					val = checked (val * 10 + (c - '0'));
				else
					return -1;
			}
			
			return val <= 255 ? val : -1;
		}

		internal void Resolve ()
		{
			if (resolved) 	
				return;
				
			bool isHostname = false;				
			bool hasWildcard = false;
			this.addresses = null;
			
			string [] s = hostname.Split (dot_char);

			if (s.Length != 4) {
				isHostname = true;
			} else {
				for (int i = 0; i < 4; i++) {
					int quad = ToNumber (s [i]);
					if (quad == -1) {
						isHostname = true;
						break;
					}
					if (quad == 256)
						hasWildcard = true;
				}
			}
			
			if (isHostname) {
				this.hasWildcard = false;
				try {
					this.addresses = Dns.GetHostByName (hostname).AddressList;
				} catch (System.Net.Sockets.SocketException) {					
				}
			} else {
				this.hasWildcard = hasWildcard;
				if (!hasWildcard) {
					addresses = new IPAddress [1];
					addresses [0] = IPAddress.Parse (hostname);
				}
			}
			
			this.resolved = true;				
		}
		
		internal void UndoResolve ()
		{
			resolved = false;
		}
	}
}