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.
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;
39 public PhysicalAddress (byte [] address
)
44 internal static PhysicalAddress
ParseEthernet (string address
)
49 string [] blocks
= address
.Split (':');
50 byte [] bytes
= new byte [blocks
.Length
];
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
)
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
)
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.");
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]);
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
;
118 if (bytes
.Length
!= other
.bytes
.Length
)
121 for (int index
= 0; index
< bytes
.Length
; index
++)
122 if (bytes
[index
] != other
.bytes
[index
])
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 ()
138 public override string ToString ()
143 StringBuilder sb
= new StringBuilder ();
144 foreach (byte b
in bytes
)
145 sb
.AppendFormat("{0:X2}", b
);
146 return sb
.ToString ();