2 // System.Net.IPEndPoint.cs
5 // Miguel de Icaza (miguel@ximian.com)
7 // (C) Ximian, Inc. http://www.ximian.com
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using System.Net.Sockets;
33 namespace System.Net {
35 public class IPEndPoint : EndPoint {
37 private IPAddress address;
40 public const int MaxPort = 65535;
41 public const int MinPort = 0;
43 public IPEndPoint (IPAddress address, int port)
46 throw new ArgumentNullException ("Value cannot be null");
52 public IPEndPoint (long iaddr, int port) : this (new IPAddress (iaddr), port)
56 public IPAddress Address {
65 public override AddressFamily AddressFamily {
67 return address.AddressFamily;
76 // LAMESPEC: no mention of sanity checking
77 // PS: MS controls the range when setting the value
78 if (value < MinPort || value > MaxPort)
79 throw new ArgumentOutOfRangeException ("Invalid port");
85 // bytes 2 and 3 store the port, the rest
87 public override EndPoint Create(SocketAddress sockaddr) {
88 int size=sockaddr.Size;
90 // LAMESPEC: no mention of what to do if
93 // absolute minimum amount needed for
94 // an address family, buffer size,
99 AddressFamily family=(AddressFamily)sockaddr[0];
102 IPEndPoint ipe = null;
105 case AddressFamily.InterNetwork:
\r
106 port = (((int)sockaddr[2])<<8) + (int)sockaddr[3];
107 long address=(((long)sockaddr[7])<<24) +
108 (((long)sockaddr[6])<<16) +
109 (((long)sockaddr[5])<<8) +
112 ipe = new IPEndPoint(address, port);
\r
115 case AddressFamily.InterNetworkV6:
\r
116 port = (((int)sockaddr[2])<<8) + (int)sockaddr[3];
120 int unknown = (int)sockaddr[4] +
121 (((int)sockaddr[5])<<8) +
122 (((int)sockaddr[6])<<16) +
123 (((int)sockaddr[7])<<24);
126 int scopeId = (int)sockaddr[24] +
127 (((int)sockaddr[25])<<8) +
128 (((int)sockaddr[26])<<16) +
129 (((int)sockaddr[27])<<24);
131 ushort[] addressData = new ushort[8];
132 for(int i=0; i<8; i++)
133 addressData[i] = (ushort)((sockaddr[8+i*2] << 8) + sockaddr[8+i*2+1]);
135 ipe = new IPEndPoint (new IPAddress(addressData, scopeId), port);
\r
145 public override SocketAddress Serialize() {
146 SocketAddress sockaddr = null;
148 switch (address.AddressFamily)
150 case AddressFamily.InterNetwork:
\r
151 // .net produces a 16 byte buffer, even though
152 // only 8 bytes are used. I guess its just a
153 // holdover from struct sockaddr padding.
154 sockaddr = new SocketAddress(AddressFamily.InterNetwork, 16);
156 // bytes 2 and 3 store the port, the rest
157 // stores the address
158 sockaddr [2] = (byte) ((port>>8) & 0xff);
159 sockaddr [3] = (byte) (port & 0xff);
160 long addr = address.InternalIPv4Address;
161 sockaddr [4] = (byte) (addr & 0xff);
162 sockaddr [5] = (byte) ((addr >> 8) & 0xff);
163 sockaddr [6] = (byte) ((addr >> 16) & 0xff);
164 sockaddr [7] = (byte) ((addr >> 24) & 0xff);
167 case AddressFamily.InterNetworkV6:
\r
168 sockaddr = new SocketAddress(AddressFamily.InterNetworkV6, 28);
170 sockaddr [2] = (byte) ((port>>8) & 0xff);
171 sockaddr [3] = (byte) (port & 0xff);
173 byte[] addressBytes = address.GetAddressBytes();
174 for(int i=0; i<16; i++)
175 sockaddr[8+i] = addressBytes[i];
177 sockaddr [24] = (byte) (address.ScopeId & 0xff);
178 sockaddr [25] = (byte) ((address.ScopeId >> 8) & 0xff);
179 sockaddr [26] = (byte) ((address.ScopeId >> 16) & 0xff);
180 sockaddr [27] = (byte) ((address.ScopeId >> 24) & 0xff);
\r
188 public override string ToString() {
189 return(address.ToString() + ":" + port);
192 public override bool Equals (Object obj)
194 IPEndPoint p = obj as IPEndPoint;
197 p.address.Equals (address);
200 public override int GetHashCode ()
202 return address.GetHashCode () + port;