add ISafeSerializationData
[mcs.git] / class / System / System.Net.NetworkInformation / PhysicalAddress.cs
blob20954db74d68dd9c6eebe5d1e92a466008bf48ad
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.
28 #if NET_2_0
29 using System;
30 using System.Text;
31 using System.Globalization;
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;
39 public PhysicalAddress (byte [] address)
41 this.bytes = address;
44 internal static PhysicalAddress ParseEthernet (string address)
46 if (address == null)
47 return None;
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);
55 return new PhysicalAddress (bytes);
58 public static PhysicalAddress Parse (string address)
60 if (address == null)
61 return None;
63 if (address == string.Empty)
64 throw new FormatException("An invalid physical address was specified.");
66 string[] addrSplit = address.Split('-');
68 if (addrSplit.Length == 1) {
69 if (address.Length != numberOfBytes * 2)
70 throw new FormatException("An invalid physical address was specified.");
72 addrSplit = new string[numberOfBytes];
73 for (int index = 0; index < addrSplit.Length; index++) {
74 addrSplit[index] = address.Substring(index * 2, 2);
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.");
85 else
86 throw new FormatException("An invalid physical address was specified.");
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;
95 return new PhysicalAddress (data);
98 static byte GetValue (char c)
100 if (c >= '0' && c <= '9')
101 return (byte) (c - '0');
103 if (c >= 'a' && c <= 'f')
104 return (byte) (c - 'a' + 10);
106 if (c >= 'A' && c <= 'F')
107 return (byte) (c - 'A' + 10);
109 throw new FormatException ("Invalid physical address.");
112 public override bool Equals (object comparand)
114 PhysicalAddress other = comparand as PhysicalAddress;
115 if (other == null)
116 return false;
118 if (bytes.Length != other.bytes.Length)
119 return false;
121 for (int index = 0; index < bytes.Length; index++)
122 if (bytes[index] != other.bytes[index])
123 return false;
125 return true;
128 public override int GetHashCode ()
130 return (bytes[5] << 8) ^ (bytes[4]) ^ (bytes[3] << 24) ^ (bytes[2] << 16) ^ (bytes[1] << 8) ^ (bytes[0]);
133 public byte [] GetAddressBytes ()
135 return bytes;
138 public override string ToString ()
140 if (bytes == null)
141 return "";
143 StringBuilder sb = new StringBuilder ();
144 foreach (byte b in bytes)
145 sb.AppendFormat("{0:X2}", b);
146 return sb.ToString ();
150 #endif