File: UnixIPGlobalProperties.cs

package info (click to toggle)
mono 6.14.1%2Bds2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,282,732 kB
  • sloc: cs: 11,182,461; xml: 2,850,281; ansic: 699,123; cpp: 122,919; perl: 58,604; javascript: 30,841; asm: 21,845; makefile: 19,602; sh: 10,973; python: 4,772; pascal: 925; sql: 859; sed: 16; php: 1
file content (425 lines) | stat: -rw-r--r-- 12,777 bytes parent folder | download | duplicates (5)
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
//
// System.Net.NetworkInformation.IPGlobalProperties
//
// Authors:
//	Gonzalo Paniagua Javier (gonzalo@novell.com)
//	Atsushi Enomoto (atsushi@ximian.com)
//	Marek Safar (marek.safar@gmail.com)
//
// Copyright (c) 2006-2007 Novell, Inc. (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Globalization;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;

namespace System.Net.NetworkInformation {
	abstract class CommonUnixIPGlobalProperties : IPGlobalProperties
	{
		[DllImport ("libc")]
		static extern int gethostname ([MarshalAs (UnmanagedType.LPArray, SizeParamIndex = 1)] byte [] name, int len);

#if !ORBIS
		[DllImport ("libc")]
		static extern int getdomainname ([MarshalAs (UnmanagedType.LPArray, SizeParamIndex = 1)] byte [] name, int len);
#else
		static int getdomainname ([MarshalAs (UnmanagedType.LPArray, SizeParamIndex = 1)] byte [] name, int len)
		{
			throw new PlatformNotSupportedException ();
		}
#endif

		public override string DhcpScopeName {
			get { return String.Empty; }
		}

		public override string DomainName {
			get {
				byte [] bytes = new byte [256];
				if (getdomainname (bytes, 256) != 0)
					throw new NetworkInformationException ();
				int len = Array.IndexOf<byte> (bytes, 0);
				return Encoding.ASCII.GetString (bytes, 0, len < 0 ? 256 : len);
			}
		}

		public override string HostName {
			get {
				byte [] bytes = new byte [256];
				if (gethostname (bytes, 256) != 0)
					throw new NetworkInformationException ();
				int len = Array.IndexOf<byte> (bytes, 0);
				return Encoding.ASCII.GetString (bytes, 0, len < 0 ? 256 : len);
			}
		}

		public override bool IsWinsProxy {
			get { return false; } // no WINS
		}

		public override NetBiosNodeType NodeType {
			get { return NetBiosNodeType.Unknown; } // no NetBios
		}
	}

	class UnixIPGlobalProperties : CommonUnixIPGlobalProperties
	{
		public override TcpConnectionInformation [] GetActiveTcpConnections ()
		{
			throw new NotImplementedException ();
		}

		public override IPEndPoint [] GetActiveTcpListeners ()
		{
			throw new NotImplementedException ();
		}

		public override IPEndPoint [] GetActiveUdpListeners ()
		{
			throw new NotImplementedException ();
		}

		public override IcmpV4Statistics GetIcmpV4Statistics ()
		{
			throw new NotImplementedException ();
		}

		public override IcmpV6Statistics GetIcmpV6Statistics ()
		{
			throw new NotImplementedException ();
		}

		public override IPGlobalStatistics GetIPv4GlobalStatistics ()
		{
			throw new NotImplementedException ();
		}

		public override IPGlobalStatistics GetIPv6GlobalStatistics ()
		{
			throw new NotImplementedException ();
		}

		public override TcpStatistics GetTcpIPv4Statistics ()
		{
			throw new NotImplementedException ();
		}

		public override TcpStatistics GetTcpIPv6Statistics ()
		{
			throw new NotImplementedException ();
		}

		public override UdpStatistics GetUdpIPv4Statistics ()
		{
			throw new NotImplementedException ();
		}

		public override UdpStatistics GetUdpIPv6Statistics ()
		{
			throw new NotImplementedException ();
		}
	}

#if MONODROID
	sealed class AndroidIPGlobalProperties : UnixIPGlobalProperties
	{
		public override string DomainName {
			get {
				return String.Empty;
			}
		}
	}
#endif

	// It expects /proc/net/snmp (or /usr/compat/linux/proc/net/snmp),
	// formatted like:
	// http://www.linuxdevcenter.com/linux/2000/11/16/example5.html
	// http://www.linuxdevcenter.com/linux/2000/11/16/example2.html
	class MibIPGlobalProperties : UnixIPGlobalProperties
	{
		public const string ProcDir = "/proc";
		public const string CompatProcDir = "/usr/compat/linux/proc";

		public readonly string StatisticsFile, StatisticsFileIPv6, TcpFile, Tcp6File, UdpFile, Udp6File;

		public MibIPGlobalProperties (string procDir)
		{
			StatisticsFile = Path.Combine (procDir, "net/snmp");
			StatisticsFileIPv6 = Path.Combine (procDir, "net/snmp6");
			TcpFile = Path.Combine (procDir,"net/tcp");
			Tcp6File = Path.Combine (procDir,"net/tcp6");
			UdpFile = Path.Combine (procDir,"net/udp");
			Udp6File = Path.Combine (procDir,"net/udp6");
		}

		StringDictionary GetProperties4 (string item)
		{
			string file = StatisticsFile;

			string head = item + ": ";
			using (StreamReader sr = new StreamReader (file, Encoding.ASCII)) {
				string [] keys = null;
				string [] values = null;
				string s = String.Empty;
				do {
					s = sr.ReadLine ();
					if (String.IsNullOrEmpty (s))
						continue;
					if (s.Length <= head.Length || String.CompareOrdinal (s, 0, head, 0, head.Length) != 0)
						continue;
					if (keys == null)
						keys = s.Substring (head.Length).Split (' ');
					else if (values != null)
						// hmm, there may be better error type...
						throw CreateException (file, String.Format ("Found duplicate line for values for the same item '{0}'", item));
					else {
						values = s.Substring (head.Length).Split (' ');
						break;
					}
				} while (!sr.EndOfStream);

				if (values == null)
					throw CreateException (file, String.Format ("No corresponding line was not found for '{0}'", item));
				if (keys.Length != values.Length)
					throw CreateException (file, String.Format ("The counts in the header line and the value line do not match for '{0}'", item));
				StringDictionary dic = new StringDictionary ();
				for (int i = 0; i < keys.Length; i++)
					dic [keys [i]] = values [i];
				return dic;
			}
		}

		StringDictionary GetProperties6 (string item)
		{
			if (!File.Exists (StatisticsFileIPv6))
				throw new NetworkInformationException ();

			string file = StatisticsFileIPv6;

			string head = item;
			using (StreamReader sr = new StreamReader (file, Encoding.ASCII)) {
				StringDictionary dic = new StringDictionary ();
				string s = String.Empty;
				do {
					s = sr.ReadLine ();
					if (String.IsNullOrEmpty (s))
						continue;
					if (s.Length <= head.Length || String.CompareOrdinal (s, 0, head, 0, head.Length) != 0)
						continue;
					int idx = s.IndexOfAny (wsChars, head.Length);
					if (idx < 0)
						throw CreateException (file, null);
					dic [s.Substring (head.Length, idx - head.Length)] = s.Substring (idx + 1).Trim (wsChars);
				} while (!sr.EndOfStream);

				return dic;
			}
		}

		static readonly char [] wsChars = new char [] {' ', '\t'};

		Exception CreateException (string file, string msg)
		{
			return new InvalidOperationException (String.Format ("Unsupported (unexpected) '{0}' file format. ", file) + msg);
		}
		IPEndPoint [] GetLocalAddresses (List<string []> list)
		{
			IPEndPoint [] ret = new IPEndPoint [list.Count];
			for (int i = 0; i < ret.Length; i++)
				ret [i] = ToEndpoint (list [i] [1]);
			return ret;
		}

		IPEndPoint ToEndpoint (string s)
		{
			int idx = s.IndexOf (':');
			int port = int.Parse (s.Substring (idx + 1), NumberStyles.HexNumber);
			if (s.Length == 13)
				return new IPEndPoint (long.Parse (s.Substring (0, idx), NumberStyles.HexNumber), port);
			else {
				byte [] bytes = new byte [16];
				for (int i = 0; (i << 1) < idx; i++)
					bytes [i] = byte.Parse (s.Substring (i << 1, 2), NumberStyles.HexNumber);
				return new IPEndPoint (new IPAddress (bytes), port);
			}
		}

		void GetRows (string file, List<string []> list)
		{
			if (!File.Exists (file))
				return;
			using (StreamReader sr = new StreamReader (file, Encoding.ASCII)) {
				sr.ReadLine (); // skip first line
				while (!sr.EndOfStream) {
					string [] item = sr.ReadLine ().Split (wsChars, StringSplitOptions.RemoveEmptyEntries);
					if (item.Length < 4)
						throw CreateException (file, null);
					list.Add (item);
				}
			}
		}

		static TcpState UnixTcpStateToTcpState (int unixState)
		{
			//The values of these states in Linux are listed here:
			//https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/include/net/tcp_states.h?id=HEAD
			switch (unixState) {
			case 1:
				return TcpState.Established;
			case 2:
				return TcpState.SynSent;
			case 3:
				return TcpState.SynReceived;
			case 4:
				return TcpState.FinWait1;
			case 5:
				return TcpState.FinWait2;
			case 6:
				return TcpState.TimeWait;
			case 7:
				return TcpState.Closed;
			case 8:
				return TcpState.CloseWait;
			case 9:
				return TcpState.LastAck;
			case 10:
				return TcpState.Listen;
			case 11:
				return TcpState.Closing;
			default:
				return TcpState.Unknown;
			}
		}

		public override TcpConnectionInformation [] GetActiveTcpConnections ()
		{
			List<string []> list = new List<string []> ();
			GetRows (TcpFile, list);
			GetRows (Tcp6File, list);

			TcpConnectionInformation [] ret = new TcpConnectionInformation [list.Count];
			for (int i = 0; i < ret.Length; i++) {
				// sl  local_address rem_address   st tx_queue rx_queue tr tm->when retrnsmt   uid  timeout inode
				IPEndPoint local = ToEndpoint (list [i] [1]);
				IPEndPoint remote = ToEndpoint (list [i] [2]);
				TcpState state = UnixTcpStateToTcpState (int.Parse (list [i] [3], NumberStyles.HexNumber));
				ret [i] = new SystemTcpConnectionInformation (local, remote, state);
			}
			return ret;
		}

		public override IPEndPoint [] GetActiveTcpListeners ()
		{
			List<string []> list = new List<string []> ();
			GetRows (TcpFile, list);
			GetRows (Tcp6File, list);
			return GetLocalAddresses (list);
		}

		public override IPEndPoint [] GetActiveUdpListeners ()
		{
			List<string []> list = new List<string []> ();
			GetRows (UdpFile, list);
			GetRows (Udp6File, list);
			return GetLocalAddresses (list);
		}

		public override IcmpV4Statistics GetIcmpV4Statistics ()
		{
			return new MibIcmpV4Statistics (GetProperties4 ("Icmp"));
		}

		public override IcmpV6Statistics GetIcmpV6Statistics ()
		{
			return new MibIcmpV6Statistics (GetProperties6 ("Icmp6"));
		}

		public override IPGlobalStatistics GetIPv4GlobalStatistics ()
		{
			return new MibIPGlobalStatistics (GetProperties4 ("Ip"));
		}

		public override IPGlobalStatistics GetIPv6GlobalStatistics ()
		{
			return new MibIPGlobalStatistics (GetProperties6 ("Ip6"));
		}

		public override TcpStatistics GetTcpIPv4Statistics ()
		{
			return new MibTcpStatistics (GetProperties4 ("Tcp"));
		}

		public override TcpStatistics GetTcpIPv6Statistics ()
		{
			// There is no TCP info in /proc/net/snmp,
			// so it is shared with IPv4 info.
			return new MibTcpStatistics (GetProperties4 ("Tcp"));
		}

		public override UdpStatistics GetUdpIPv4Statistics ()
		{
			return new MibUdpStatistics (GetProperties4 ("Udp"));
		}

		public override UdpStatistics GetUdpIPv6Statistics ()
		{
			return new MibUdpStatistics (GetProperties6 ("Udp6"));
		}
	}

	internal static class UnixIPGlobalPropertiesFactoryPal {
		public static IPGlobalProperties Create ()
		{
#if MONODROID
			return new AndroidIPGlobalProperties ();
#elif MONOTOUCH || XAMMAC
			return new UnixIPGlobalProperties ();
#elif MONO
			switch (Environment.OSVersion.Platform) {
			case PlatformID.Unix:
				MibIPGlobalProperties impl = null;
				if (Directory.Exists (MibIPGlobalProperties.ProcDir)) {
					impl = new MibIPGlobalProperties (MibIPGlobalProperties.ProcDir);
					if (File.Exists (impl.StatisticsFile))
						return impl;
				}
				if (Directory.Exists (MibIPGlobalProperties.CompatProcDir)) {
					impl = new MibIPGlobalProperties (MibIPGlobalProperties.CompatProcDir);
					if (File.Exists (impl.StatisticsFile))
						return impl;
				}
				return new UnixIPGlobalProperties ();
			default:
#if !WIN_PLATFORM
				return new UnixIPGlobalProperties ();
#endif
				return null;
		}
#else
			(new NetworkInformationPermission (NetworkInformationAccess.Read)).Demand ();
			return new SystemIPGlobalProperties ();
#endif
		}
	}
}