System.Drawing: added email to icon and test file headers
[mono.git] / mcs / class / System / System.Net.NetworkInformation / PhysicalAddress.cs
1 //
2 // System.Net.NetworkInformation.PhysicalAddress
3 //
4 // Author:
5 //      Gonzalo Paniagua Javier (gonzalo@novell.com)
6 //
7 // Copyright (c) 2006 Novell, Inc. (http://www.novell.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28 #if NET_2_0
29 using System;
30 using System.Text;
31 using System.Globalization;
32
33 namespace System.Net.NetworkInformation {
34         public class PhysicalAddress {
35                 public static readonly PhysicalAddress None = new PhysicalAddress (new byte [0]);
36                 private const int numberOfBytes = 6;
37                 byte [] bytes;
38                 
39                 public PhysicalAddress (byte [] address)
40                 {
41                         this.bytes = address;
42                 }
43
44                 internal static PhysicalAddress ParseEthernet (string address)
45                 {
46                         if (address == null)
47                                 return None;
48
49                         string [] blocks = address.Split (':');
50                         byte [] bytes = new byte [blocks.Length];
51                         int i = 0;
52                         foreach (string b in blocks){
53                                 bytes [i++] = Byte.Parse (b, NumberStyles.HexNumber);
54                         }
55                         return new PhysicalAddress (bytes);
56                 }
57                 
58                 public static PhysicalAddress Parse (string address)
59                 {
60                         if (address == null)
61                                 return None;
62
63                         if (address == string.Empty)
64                                 throw new FormatException("An invalid physical address was specified.");
65
66                         string[] addrSplit = address.Split('-');
67
68                         if (addrSplit.Length == 1) {
69                                 if (address.Length != numberOfBytes * 2)
70                                         throw new FormatException("An invalid physical address was specified.");
71
72                                 addrSplit = new string[numberOfBytes];
73                                 for (int index = 0; index < addrSplit.Length; index++) {
74                                         addrSplit[index] = address.Substring(index * 2, 2);
75                                 }
76                         }
77
78                         if (addrSplit.Length == numberOfBytes) {
79                                 foreach (string str in addrSplit)
80                                         if (str.Length > 2)
81                                                 throw new FormatException("An invalid physical address was specified.");
82                                         else if (str.Length < 2)
83                                                 throw new IndexOutOfRangeException("An invalid physical address was specified.");
84                         }
85                         else
86                                 throw new FormatException("An invalid physical address was specified.");
87
88                         byte[] data = new byte[numberOfBytes];
89                         for (int i = 0; i < numberOfBytes; i++) {
90                                 byte b = (byte)(GetValue(addrSplit[i][0]) << 4);
91                                 b += GetValue(addrSplit[i][1]);
92                                 data[i] = b;
93                         }
94
95                         return new PhysicalAddress (data);
96                 }
97
98                 static byte GetValue (char c)
99                 {
100                         if (c >= '0' && c <= '9')
101                                 return (byte) (c - '0');
102
103                         if (c >= 'a' && c <= 'f')
104                                 return (byte) (c - 'a' + 10);
105
106                         if (c >= 'A' && c <= 'F')
107                                 return (byte) (c - 'A' + 10);
108
109                         throw new FormatException ("Invalid physical address.");
110                 }
111
112                 public override bool Equals (object comparand)
113                 {
114                         PhysicalAddress other = comparand as PhysicalAddress;
115                         if (other == null)
116                                 return false;
117
118                         if (bytes.Length != other.bytes.Length)
119                                 return false;
120
121                         for (int index = 0; index < bytes.Length; index++)
122                                 if (bytes[index] != other.bytes[index])
123                                         return false;
124
125                         return true;
126                 }
127
128                 public override int GetHashCode ()
129                 {
130                         return (bytes[5] << 8) ^ (bytes[4]) ^ (bytes[3] << 24) ^ (bytes[2] << 16) ^ (bytes[1] << 8) ^ (bytes[0]);
131                 }
132
133                 public byte [] GetAddressBytes ()
134                 {
135                         return bytes;
136                 }
137
138                 public override string ToString ()
139                 {
140                         if (bytes == null)
141                                 return "";
142
143                         StringBuilder sb = new StringBuilder ();
144                         foreach (byte b in bytes)
145                                 sb.AppendFormat("{0:X2}", b);
146                         return sb.ToString ();
147                 }
148         }
149 }
150 #endif