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);
120 case AddressFamily.InterNetworkV6:
125 port = (((int)sockaddr[2])<<8) + (int)sockaddr[3];
129 int unknown = (int)sockaddr[4] +
130 (((int)sockaddr[5])<<8) +
131 (((int)sockaddr[6])<<16) +
132 (((int)sockaddr[7])<<24);
135 int scopeId = (int)sockaddr[24] +
136 (((int)sockaddr[25])<<8) +
137 (((int)sockaddr[26])<<16) +
138 (((int)sockaddr[27])<<24);
140 ushort[] addressData = new ushort[8];
141 for(int i=0; i<8; i++)
142 addressData[i] = (ushort)((sockaddr[8+i*2] << 8) + sockaddr[8+i*2+1]);
144 ipe = new IPEndPoint (new IPAddress(addressData, scopeId), port);
153 public override SocketAddress Serialize() {
154 SocketAddress sockaddr = null;
156 switch (address.AddressFamily)
158 case AddressFamily.InterNetwork:
159 // .net produces a 16 byte buffer, even though
160 // only 8 bytes are used. I guess its just a
161 // holdover from struct sockaddr padding.
162 sockaddr = new SocketAddress(AddressFamily.InterNetwork, 16);
164 // bytes 2 and 3 store the port, the rest
165 // stores the address
166 sockaddr [2] = (byte) ((port>>8) & 0xff);
167 sockaddr [3] = (byte) (port & 0xff);
168 long addr = address.InternalIPv4Address;
169 sockaddr [4] = (byte) (addr & 0xff);
170 sockaddr [5] = (byte) ((addr >> 8) & 0xff);
171 sockaddr [6] = (byte) ((addr >> 16) & 0xff);
172 sockaddr [7] = (byte) ((addr >> 24) & 0xff);
175 case AddressFamily.InterNetworkV6:
176 sockaddr = new SocketAddress(AddressFamily.InterNetworkV6, 28);
178 sockaddr [2] = (byte) ((port>>8) & 0xff);
179 sockaddr [3] = (byte) (port & 0xff);
181 byte[] addressBytes = address.GetAddressBytes();
182 for(int i=0; i<16; i++)
183 sockaddr[8+i] = addressBytes[i];
185 sockaddr [24] = (byte) (address.ScopeId & 0xff);
186 sockaddr [25] = (byte) ((address.ScopeId >> 8) & 0xff);
187 sockaddr [26] = (byte) ((address.ScopeId >> 16) & 0xff);
188 sockaddr [27] = (byte) ((address.ScopeId >> 24) & 0xff);
195 public override string ToString() {
196 return(address.ToString() + ":" + port);
199 public override bool Equals (object comparand)
201 IPEndPoint p = comparand as IPEndPoint;
204 p.address.Equals (address);
207 public override int GetHashCode ()
209 return address.GetHashCode () + port;