Merge pull request #347 from JamesB7/master
[mono.git] / mcs / class / System / System.Net / IPEndPoint.cs
1 //
2 // System.Net.IPEndPoint.cs
3 //
4 // Author:
5 //   Miguel de Icaza (miguel@ximian.com)
6 //
7 // (C) Ximian, Inc.  http://www.ximian.com
8 //
9
10 //
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:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
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.
29 //
30
31 using System.Net.Sockets;
32
33 namespace System.Net {
34         [Serializable]
35         public class IPEndPoint : EndPoint {
36
37                 private IPAddress address;
38                 private int port;
39
40                 public const int MaxPort = 65535;
41                 public const int MinPort = 0;
42                 
43                 public IPEndPoint (IPAddress address, int port)
44                 {
45                         if (address == null)
46                                 throw new ArgumentNullException ("address");
47
48                         Address = address;
49                         Port = port;
50                 }
51                 
52                 public IPEndPoint (long address, int port)
53                 {
54                         Address = new IPAddress (address);
55                         Port = port;
56                 }
57
58                 public IPAddress Address {
59                         get {
60                                 return (address);
61                         }
62                         set {
63                                 address=value;
64                         }
65                 }
66
67                 public override AddressFamily AddressFamily {
68                         get {
69                                 return address.AddressFamily;
70                         }
71                 }
72
73                 public int Port {
74                         get {
75                                 return port;
76                         }
77                         set {
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");
82                         
83                                 port = value;
84                         }
85                 }
86
87                 // bytes 2 and 3 store the port, the rest
88                 // stores the address
89                 public override EndPoint Create (SocketAddress socketAddress)
90                 {
91                         if (socketAddress == null)
92                                 throw new ArgumentNullException ("socketAddress");
93
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.");
98
99                         SocketAddress sockaddr = socketAddress;
100                         int size =sockaddr.Size;
101                         AddressFamily family = sockaddr.Family;
102                         int port;
103
104                         IPEndPoint ipe = null;
105                         switch(family)
106                         {
107                                 case AddressFamily.InterNetwork:
108                                         if (size < 8) {
109                                                 return(null);
110                                         }
111                                         
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) +
116                                                 (long)sockaddr[4];
117
118                                         ipe = new IPEndPoint(address, port);
119                                         break;
120                                 case AddressFamily.InterNetworkV6:
121                                         if (size < 28) {
122                                                 return(null);
123                                         }
124                                         
125                                         port    = (((int)sockaddr[2])<<8) + (int)sockaddr[3];
126
127                                         /// maybe flowid ?
128                                         /*
129                                         int unknown     = (int)sockaddr[4] +
130                                                 (((int)sockaddr[5])<<8) +
131                                                 (((int)sockaddr[6])<<16) +
132                                                 (((int)sockaddr[7])<<24);
133                                         */
134
135                                         int scopeId     = (int)sockaddr[24] +
136                                                 (((int)sockaddr[25])<<8) +
137                                                 (((int)sockaddr[26])<<16) +
138                                                 (((int)sockaddr[27])<<24);
139
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]);
143
144                                         ipe = new IPEndPoint (new IPAddress(addressData, scopeId), port);
145                                         break;
146                                 default:
147                                         return null;
148                         }
149
150                         return(ipe);
151                 }
152
153                 public override SocketAddress Serialize() {
154                         SocketAddress sockaddr = null;
155
156                         switch (address.AddressFamily)
157                         {
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);
163
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);
173                                         break;
174
175                                 case AddressFamily.InterNetworkV6:
176                                         sockaddr = new SocketAddress(AddressFamily.InterNetworkV6, 28);
177
178                                         sockaddr [2] = (byte) ((port>>8) & 0xff);
179                                         sockaddr [3] = (byte) (port & 0xff);
180
181                                         byte[] addressBytes = address.GetAddressBytes();
182                                         for(int i=0; i<16; i++)
183                                                 sockaddr[8+i] = addressBytes[i];
184                                         
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);
189                                         break;
190                         }
191
192                         return(sockaddr);
193                 }
194
195                 public override string ToString() {
196                         return(address.ToString() + ":" + port);
197                 }
198
199                 public override bool Equals (object comparand)
200                 {
201                         IPEndPoint p = comparand as IPEndPoint;
202                         return p != null && 
203                                p.port == port && 
204                                p.address.Equals (address);
205                 }
206
207                 public override int GetHashCode ()
208                 {
209                         return address.GetHashCode () + port;
210                 }
211         }
212 }