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 ("address");
52 public IPEndPoint (long address, int port)
54 Address = new IPAddress (address);
58 public IPAddress Address {
67 public override AddressFamily AddressFamily {
69 return address.AddressFamily;
78 // LAMESPEC: no mention of sanity checking
79 // PS: MS controls the range when setting the value
80 if (value < MinPort || value > MaxPort)
81 throw new ArgumentOutOfRangeException ("Invalid port");
87 // bytes 2 and 3 store the port, the rest
89 public override EndPoint Create (SocketAddress socketAddress)
91 if (socketAddress == null)
92 throw new ArgumentNullException ("socketAddress");
94 if (socketAddress.Family != AddressFamily)
95 throw new ArgumentException ("The IPEndPoint was created using " + AddressFamily +
96 " AddressFamily but SocketAddress contains " + socketAddress.Family +
97 " instead, please use the same type.");
99 SocketAddress sockaddr = socketAddress;
100 int size =sockaddr.Size;
101 AddressFamily family = sockaddr.Family;
104 IPEndPoint ipe = null;
107 case AddressFamily.InterNetwork:
112 port = (((int)sockaddr[2])<<8) + (int)sockaddr[3];
113 long address=(((long)sockaddr[7])<<24) +
114 (((long)sockaddr[6])<<16) +
115 (((long)sockaddr[5])<<8) +
118 ipe = new IPEndPoint(address, port);
121 case AddressFamily.InterNetworkV6:
126 port = (((int)sockaddr[2])<<8) + (int)sockaddr[3];
130 int unknown = (int)sockaddr[4] +
131 (((int)sockaddr[5])<<8) +
132 (((int)sockaddr[6])<<16) +
133 (((int)sockaddr[7])<<24);
136 int scopeId = (int)sockaddr[24] +
137 (((int)sockaddr[25])<<8) +
138 (((int)sockaddr[26])<<16) +
139 (((int)sockaddr[27])<<24);
141 ushort[] addressData = new ushort[8];
142 for(int i=0; i<8; i++)
143 addressData[i] = (ushort)((sockaddr[8+i*2] << 8) + sockaddr[8+i*2+1]);
145 ipe = new IPEndPoint (new IPAddress(addressData, scopeId), port);
155 public override SocketAddress Serialize() {
156 SocketAddress sockaddr = null;
158 switch (address.AddressFamily)
160 case AddressFamily.InterNetwork:
161 // .net produces a 16 byte buffer, even though
162 // only 8 bytes are used. I guess its just a
163 // holdover from struct sockaddr padding.
164 sockaddr = new SocketAddress(AddressFamily.InterNetwork, 16);
166 // bytes 2 and 3 store the port, the rest
167 // stores the address
168 sockaddr [2] = (byte) ((port>>8) & 0xff);
169 sockaddr [3] = (byte) (port & 0xff);
170 long addr = address.InternalIPv4Address;
171 sockaddr [4] = (byte) (addr & 0xff);
172 sockaddr [5] = (byte) ((addr >> 8) & 0xff);
173 sockaddr [6] = (byte) ((addr >> 16) & 0xff);
174 sockaddr [7] = (byte) ((addr >> 24) & 0xff);
177 case AddressFamily.InterNetworkV6:
178 sockaddr = new SocketAddress(AddressFamily.InterNetworkV6, 28);
180 sockaddr [2] = (byte) ((port>>8) & 0xff);
181 sockaddr [3] = (byte) (port & 0xff);
183 byte[] addressBytes = address.GetAddressBytes();
184 for(int i=0; i<16; i++)
185 sockaddr[8+i] = addressBytes[i];
187 sockaddr [24] = (byte) (address.ScopeId & 0xff);
188 sockaddr [25] = (byte) ((address.ScopeId >> 8) & 0xff);
189 sockaddr [26] = (byte) ((address.ScopeId >> 16) & 0xff);
190 sockaddr [27] = (byte) ((address.ScopeId >> 24) & 0xff);
198 public override string ToString() {
199 return(address.ToString() + ":" + port);
202 public override bool Equals (object comparand)
204 IPEndPoint p = comparand as IPEndPoint;
207 p.address.Equals (address);
210 public override int GetHashCode ()
212 return address.GetHashCode () + port;