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

IPEndPoint.cs « System.Net « System « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a52539faf79937632f2ea24faedd13b3fa395d87 (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
//
// System.Net.IPEndPoint.cs
//
// Author:
//   Miguel de Icaza (miguel@ximian.com)
//
// (C) Ximian, Inc.  http://www.ximian.com
//

using System.Net.Sockets;

namespace System.Net {
	[Serializable]
	public class IPEndPoint : EndPoint {

		private IPAddress address;
		private int port;

		public const int MaxPort = 65535;
		public const int MinPort = 0;
		
		public IPEndPoint (IPAddress address, int port)
		{
			if (address == null)
				throw new ArgumentNullException ("Value cannot be null");

			Address = address;
			Port = port;
		}
		
		public IPEndPoint (long iaddr, int port) : this (new IPAddress (iaddr), port)
		{
		}

		public IPAddress Address {
			get {
				return (address);
			}
			set {
				address=value;
			}
		}

		public override AddressFamily AddressFamily {
			get {
				return AddressFamily.InterNetwork;
			}
		}

		public int Port {
			get {
				return port;
			}
			set {
				// LAMESPEC: no mention of sanity checking
				// PS: MS controls the range when setting the value
				if (value < MinPort || value > MaxPort)
					throw new ArgumentOutOfRangeException ("Invalid port");
			
				port = value;
			}
		}

		// bytes 2 and 3 store the port, the rest
		// stores the address
		public override EndPoint Create(SocketAddress sockaddr) {
			int size=sockaddr.Size;
			
			// LAMESPEC: no mention of what to do if
			// sockaddr is bogus
			if(size<8) {
				// absolute minimum amount needed for
				// an address family, buffer size,
				// port and address
				return(null);
			}
			AddressFamily family=(AddressFamily)sockaddr[0];
			if(family!=AddressFamily.InterNetwork) {
				return(null);
			}
			
			int port=(((int)sockaddr[2])<<8) + (int)sockaddr[3];
			long address=(((long)sockaddr[7])<<24) +
				(((long)sockaddr[6])<<16) +
				(((long)sockaddr[5])<<8) +
				(long)sockaddr[4];

			IPEndPoint ipe = new IPEndPoint(address, port);
			
			return(ipe);
		}

		public override SocketAddress Serialize() {
			// .net produces a 16 byte buffer, even though
			// only 8 bytes are used. I guess its just a
			// holdover from struct sockaddr padding.
			SocketAddress sockaddr = new SocketAddress(AddressFamily.InterNetwork, 16);

			// bytes 2 and 3 store the port, the rest
			// stores the address
			sockaddr [2] = (byte) ((port>>8) & 0xff);
			sockaddr [3] = (byte) (port & 0xff);

			sockaddr [4] = (byte) (address.Address & 0xff);
			sockaddr [5] = (byte) ((address.Address >> 8) & 0xff);
			sockaddr [6] = (byte) ((address.Address >> 16) & 0xff);
			sockaddr [7] = (byte) ((address.Address >> 24) & 0xff);
			
			return(sockaddr);
		}

		public override string ToString() {
			return(address.ToString() + ":" + port);
		}

		public override bool Equals (Object obj)
		{
			IPEndPoint p = obj as IPEndPoint;
			return p != null && 
			       p.port == port && 
			       p.address.Equals (address);
		}

		public override int GetHashCode ()
		{
			return address.GetHashCode () + port;
		}
	}
}