2 // System.Net.NetworkInformation.PhysicalAddress
5 // Gonzalo Paniagua Javier (gonzalo@novell.com)
7 // Copyright (c) 2006 Novell, Inc. (http://www.novell.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
30 using System.Globalization;
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;
38 public PhysicalAddress (byte [] address)
43 internal static PhysicalAddress ParseEthernet (string address)
48 string [] blocks = address.Split (':');
49 byte [] bytes = new byte [blocks.Length];
51 foreach (string b in blocks){
52 bytes [i++] = Byte.Parse (b, NumberStyles.HexNumber);
54 return new PhysicalAddress (bytes);
57 public static PhysicalAddress Parse (string address)
62 if (address == string.Empty)
63 throw new FormatException("An invalid physical address was specified.");
65 string[] addrSplit = address.Split('-');
67 if (addrSplit.Length == 1) {
68 if (address.Length != numberOfBytes * 2)
69 throw new FormatException("An invalid physical address was specified.");
71 addrSplit = new string[numberOfBytes];
72 for (int index = 0; index < addrSplit.Length; index++) {
73 addrSplit[index] = address.Substring(index * 2, 2);
77 if (addrSplit.Length == numberOfBytes) {
78 foreach (string str in addrSplit)
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.");
85 throw new FormatException("An invalid physical address was specified.");
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]);
94 return new PhysicalAddress (data);
97 static byte GetValue (char c)
99 if (c >= '0' && c <= '9')
100 return (byte) (c - '0');
102 if (c >= 'a' && c <= 'f')
103 return (byte) (c - 'a' + 10);
105 if (c >= 'A' && c <= 'F')
106 return (byte) (c - 'A' + 10);
108 throw new FormatException ("Invalid physical address.");
111 public override bool Equals (object comparand)
113 PhysicalAddress other = comparand as PhysicalAddress;
117 if (bytes.Length != other.bytes.Length)
120 for (int index = 0; index < bytes.Length; index++)
121 if (bytes[index] != other.bytes[index])
127 public override int GetHashCode ()
129 return (bytes[5] << 8) ^ (bytes[4]) ^ (bytes[3] << 24) ^ (bytes[2] << 16) ^ (bytes[1] << 8) ^ (bytes[0]);
132 public byte [] GetAddressBytes ()
137 public override string ToString ()
142 StringBuilder sb = new StringBuilder ();
143 foreach (byte b in bytes)
144 sb.AppendFormat("{0:X2}", b);
145 return sb.ToString ();