2009-04-11 Sebastien Pouliot <sebastien@ximian.com>
[mono.git] / mcs / class / System.Net / System.Net / DnsEndPoint_2_1.cs
1 //
2 // System.Net.DnsEndPoint (for 2.1 profile)
3 //
4 // Authors:
5 //      Stephane Delcroix  <stephane@delcroix.org>
6 //
7 // (c) 2007 Novell, Inc. (http://www.novell.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 #if NET_2_1
32
33 using System.Net.Sockets;
34
35 namespace System.Net { 
36
37         public sealed class DnsEndPoint : EndPoint {
38                 string host;
39                 int port;
40                 AddressFamily addressFamily = AddressFamily.Unspecified;
41
42                 public DnsEndPoint (string host, int port)
43                 {
44                         if (host == null)
45                                 throw new ArgumentNullException ("host");
46                         if (host == String.Empty)
47                                 throw new ArgumentException ("host parameter contains an empty string");
48                         if (port < 0 || port > 0xffff)
49                                 throw new ArgumentOutOfRangeException ("port is less than 0 or greater than 0xffff");
50
51                         this.host = host;
52                         this.port = port;
53                 }
54
55                 public DnsEndPoint (string host, int port, AddressFamily addressFamily) : this (host, port)
56                 {
57                         switch (addressFamily) {
58                         case AddressFamily.InterNetwork:
59                         case AddressFamily.InterNetworkV6:
60                         case AddressFamily.Unspecified:
61                                 this.addressFamily = addressFamily;
62                                 break;
63                         default:
64                                 // throw for Unknown or any invalid value
65                                 throw new ArgumentException ("addressFamily");
66                         }
67                 }
68
69                 public override bool Equals (object other)
70                 {
71                         if (other is DnsEndPoint)
72                                 return Equals (other as DnsEndPoint);
73                         return false;
74                 }
75
76                 private bool Equals (DnsEndPoint other)
77                 {
78                         if (port != other.port || addressFamily != other.addressFamily|| host != other.host)
79                                 return false;
80                         return true;
81                 }
82
83                 public override int GetHashCode ()
84                 {
85                         return port ^ (int)addressFamily ^ host.GetHashCode ();
86                 }
87
88                 public override string ToString ()
89                 {
90                         return String.Format ("{0}/{1}:{2}", addressFamily, host, port);
91                 }
92
93                 public override AddressFamily AddressFamily {
94                         get { return addressFamily; }
95                 }
96
97                 public string Host {
98                         get { return host; }
99                 }
100
101                 public int Port {
102                         get { return port; }
103                 }
104         }
105 }
106
107 #endif