[runtime] MonoError-ize mono_compile_method
[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 using System;
29 using System.Text;
30 using System.Globalization;
31
32 namespace System.Net.NetworkInformation {
33         public class PhysicalAddress {
34                 public static readonly PhysicalAddress None = new PhysicalAddress (new byte [0]);
35                 private const int numberOfBytes = 6;
36                 byte [] bytes;
37                 
38                 public PhysicalAddress (byte [] address)
39                 {
40                         this.bytes = address;
41                 }
42
43                 internal static PhysicalAddress ParseEthernet (string address)
44                 {
45                         if (address == null)
46                                 return None;
47
48                         string [] blocks = address.Split (':');
49                         byte [] bytes = new byte [blocks.Length];
50                         int i = 0;
51                         foreach (string b in blocks){
52                                 bytes [i++] = Byte.Parse (b, NumberStyles.HexNumber);
53                         }
54                         return new PhysicalAddress (bytes);
55                 }
56                 
57                 public static PhysicalAddress Parse (string address)
58                 {
59                         if (address == null)
60                                 return None;
61
62                         if (address == string.Empty)
63                                 throw new FormatException("An invalid physical address was specified.");
64
65                         string[] addrSplit = address.Split('-');
66
67                         if (addrSplit.Length == 1) {
68                                 if (address.Length != numberOfBytes * 2)
69                                         throw new FormatException("An invalid physical address was specified.");
70
71                                 addrSplit = new string[numberOfBytes];
72                                 for (int index = 0; index < addrSplit.Length; index++) {
73                                         addrSplit[index] = address.Substring(index * 2, 2);
74                                 }
75                         }
76
77                         if (addrSplit.Length == numberOfBytes) {
78                                 foreach (string str in addrSplit)
79                                         if (str.Length > 2)
80                                                 throw new FormatException("An invalid physical address was specified.");
81                                         else if (str.Length < 2)
82                                                 throw new IndexOutOfRangeException("An invalid physical address was specified.");
83                         }
84                         else
85                                 throw new FormatException("An invalid physical address was specified.");
86
87                         byte[] data = new byte[numberOfBytes];
88                         for (int i = 0; i < numberOfBytes; i++) {
89                                 byte b = (byte)(GetValue(addrSplit[i][0]) << 4);
90                                 b += GetValue(addrSplit[i][1]);
91                                 data[i] = b;
92                         }
93
94                         return new PhysicalAddress (data);
95                 }
96
97                 static byte GetValue (char c)
98                 {
99                         if (c >= '0' && c <= '9')
100                                 return (byte) (c - '0');
101
102                         if (c >= 'a' && c <= 'f')
103                                 return (byte) (c - 'a' + 10);
104
105                         if (c >= 'A' && c <= 'F')
106                                 return (byte) (c - 'A' + 10);
107
108                         throw new FormatException ("Invalid physical address.");
109                 }
110
111                 public override bool Equals (object comparand)
112                 {
113                         PhysicalAddress other = comparand as PhysicalAddress;
114                         if (other == null)
115                                 return false;
116
117                         if (bytes.Length != other.bytes.Length)
118                                 return false;
119
120                         for (int index = 0; index < bytes.Length; index++)
121                                 if (bytes[index] != other.bytes[index])
122                                         return false;
123
124                         return true;
125                 }
126
127                 public override int GetHashCode ()
128                 {
129                         return (bytes[5] << 8) ^ (bytes[4]) ^ (bytes[3] << 24) ^ (bytes[2] << 16) ^ (bytes[1] << 8) ^ (bytes[0]);
130                 }
131
132                 public byte [] GetAddressBytes ()
133                 {
134                         return bytes;
135                 }
136
137                 public override string ToString ()
138                 {
139                         if (bytes == null)
140                                 return "";
141
142                         StringBuilder sb = new StringBuilder ();
143                         foreach (byte b in bytes)
144                                 sb.AppendFormat("{0:X2}", b);
145                         return sb.ToString ();
146                 }
147         }
148 }