Skip to content

Commit 5e90538

Browse files
rheoneahsteele
authored andcommitted
Add MAC Address object, tests and documentation
Issue: #28
1 parent cd2ac39 commit 5e90538

File tree

9 files changed

+2443
-1
lines changed

9 files changed

+2443
-1
lines changed

docs/Acknowledgements.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,9 @@ Logo cropped from image `"Iris Carrying the Water of the River Styx to Olympus f
1313
This file is made available under the Creative Commons CC0 1.0 Universal Public Domain Dedication.
1414

1515
The person who associated a work with this deed has dedicated the work to the public domain by waiving all of their rights to the work worldwide under copyright law, including all related and neighboring rights, to the extent allowed by law. You can copy, modify, distribute and perform the work, even for commercial purposes, all without asking permission.
16+
17+
Structure of a 48-bit MAC address
18+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
19+
`Diagram showing the structure of a MAC-48 network address, explicitly showing the positions of the multicast/unicast bit and the OUI/local address type bit. <https://commons.wikimedia.org/wiki/File:MAC-48_Address.svg>`_
20+
21+
This file is licensed under the `Creative Commons <https://en.wikipedia.org/wiki/en:Creative_Commons>`_ Attribution-Share Alike `2.5 Generic <https://creativecommons.org/licenses/by-sa/2.5/deed.en>`_, 2.0 Generic and `1.0 Generic <https://creativecommons.org/licenses/by-sa/1.0/deed.en>`_ license.

docs/MacAddress.rst

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
.. _MacAddress:
2+
3+
MacAddress
4+
==========
5+
6+
The ``MacAddress`` type represents a 48-bit MAC Address [#48-BitMAC]_ as per the IEEE EUI standard [#IEEE-Eui]_. It serves the purpose of a Networking Adjacent worker class, and as a handy way to represent, store, format, and compare MAC addresses.
7+
8+
The ``MacAddress`` class implements ``IEquatable<MacAddress>``, ``IComparable<MacAddress>``, ``IComparable``, ``IFormattable``, and ``ISerializable``.
9+
10+
.. note::
11+
12+
Unless otherwise stated recognized readable MAC Address formats include only the following formats:
13+
14+
- **IEEE 802** format for printing **EUI-48** and **MAC-48** addresses in six groups of two hexadecimal digits, separated by a dash (``-``). *E.g.* ``AA-BB-CC-DD-EE-FF``
15+
- **Common** Six groups of two hexadecimal digits separated by colons (``:``). *E.g.* ``AA:BB:CC:DD:EE:FF``
16+
- Six groups of two hexadecimal digits separated by a space character. *E.g.* ``AA BB CC DD EE FF``
17+
- 12 hexadecimal digits with no delimitation. *E.g.* ``AABBCCDDEEFF``
18+
- **Cisco** three groups of four hexadecimal digits separated by dots (``.``). *E.g.* ``AABB.CCDD.EEFF``
19+
20+
For the sake of parsing and reading these formats are case insensitive.
21+
22+
.. figure:: img/MAC-48_Address.svg
23+
:scale: 50 %
24+
:align: center
25+
:alt: MAC-48 Address.svg This file is licensed under the Creative Commons Attribution-Share Alike 2.5 Generic, 2.0 Generic and 1.0 Generic license.
26+
:target: https://commons.wikimedia.org/wiki/File:MAC-48_Address.svg
27+
28+
Structure of a MAC-48 Address
29+
30+
Creation
31+
--------
32+
33+
constructor ``IEnumerable<byte>``
34+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
35+
36+
A new ``MacAddress`` may be constructed by providing an ``IEnumerable<byte>`` of six bytes to the constructor.
37+
38+
.. code-block:: c#
39+
40+
public MacAddress(IEnumerable<byte> bytes)
41+
42+
parse Parse ``string``
43+
^^^^^^^^^^^^^^^^^^^^^^
44+
45+
A ``MacAddress`` may also be created via either the ``Parse`` or safe ``TryParse`` method. Not that these methods are strict in that they will only succeed with a MAC address in a known format. If you wish to more liberally parse a string into a ``MacAddress`` see the ``ParseAny`` and ``TryParseAny`` defined below.
46+
47+
.. code-block:: c#
48+
49+
public static MacAddress Parse(string input)
50+
51+
.. code-block:: c#
52+
53+
public static bool TryParse(string input, out MacAddress macAddress)
54+
55+
parse ParseAny ``string``
56+
^^^^^^^^^^^^^^^^^^^^^^^^^
57+
58+
``ParseAny`` and the safe ``TryParseAny`` allow the parsing of an arbitrary string that may be a Mac address into a ``MacAddress``. It looks for six hexadecimal digits within the string, joins them and interprets the result as consecutive big-endian hextets. If six, and only six, hexadecimal digits are not found the parse will fail.
59+
60+
.. code-block:: c#
61+
62+
public static MacAddress ParseAny(string input)
63+
64+
.. code-block:: c#
65+
66+
public static bool TryParseAny(string input, out MacAddress macAddress)
67+
68+
Functionality
69+
-------------
70+
71+
Properties
72+
^^^^^^^^^^
73+
:``bool`` IsDefault: returns ``true`` if, and only if, the MAC Address is the EUI-48 default [#EUI-48Default]_, meaning all bits of the MAC Address are set making it equivalent to ``FF:FF:FF:FF:FF:FF``.
74+
:``bool`` IsGloballyUnique: returns ``true`` if, and only if, is globally unique (OUI [#Eui-Oui]_ enforced).
75+
:``bool`` IsLocallyAdministered: returns ``true`` if, and only if, is locally administered.
76+
:``bool`` IsMulticast: returns ``true`` if, and only if, the MAC Address is multicast.
77+
:``bool`` IsUnicast: returns ``true`` if, and only if, the MAC Address is unicast.
78+
:``bool`` IsUnusable: returns ``true`` if, and only if, the MAC Address is "unusable", meaning all OUI bits of the MAC Address are unset.
79+
80+
:``MacAddress`` DefaultMacAddress: Provides a ``MacAddress`` that represents the default or ``null`` case MAC address.
81+
82+
:``Regex`` AllFormatMacAddressRegularExpression: Returns a regular expression for matching accepted MAC Address formats.
83+
:``Regex`` CommonFormatMacAddressRegularExpression: Returns a regular expression for matching the "common" six groups of two uppercase hexadecimal digits format.
84+
85+
:``string`` AllFormatMacAddressPattern: Returns a regular expression pattern for matching accepted MAC Address formats.
86+
:``string`` CommonFormatMacAddressPattern: Returns a regular expression pattern for matching the "common" six groups of two uppercase hexadecimal digits format.
87+
88+
Methods
89+
^^^^^^^
90+
91+
GetAddressBytes
92+
+++++++++++++++
93+
94+
``GetAddressBytes`` returns a copy of the underlying big-endian bytes of the ``MacAddress``. This will always be six bytes in length.
95+
96+
.. code-block:: c#
97+
98+
public byte[] GetAddressBytes()
99+
100+
GetOuiBytes
101+
+++++++++++
102+
103+
``GetOuiBytes`` returns the *Organizationally Unique Identifier (OUI)* [#Eui-Oui]_ of the ``MAcAddress``.
104+
105+
.. code-block:: c#
106+
107+
public byte[] GetOuiBytes()
108+
109+
GetCidBytes
110+
+++++++++++
111+
112+
``GetCidBytes`` returns the *Company ID (CID)* [#Eui-Cid]_ of the ``MAcAddress``.
113+
114+
.. code-block:: c#
115+
116+
public byte[] GetCidBytes()
117+
118+
IFormatable
119+
^^^^^^^^^^^
120+
121+
``MacAddress`` offers a number or preexisting formats that are accessible via the standard ``ToString`` method provided by ``IFormattable`` interface.
122+
123+
.. csv-table:: Subnet format values
124+
:file: macaddress-formats.csv
125+
:header-rows: 1
126+
127+
Operators
128+
^^^^^^^^^
129+
130+
``MacAddress`` implements all the standard C# equality and comparison operators. The comparison operators treat the ``MacAddress`` bytes as an unsigned big-endian integer value.
131+
132+
.. rubric:: Footnotes
133+
134+
.. [#48-BitMAC] **48-Bit MAC** is a A Media Access Control Address (MAC) following both the now deprecated *MAC-48* and the active *EUI-48* specifications.
135+
136+
.. [#EUI-48Default] The recommended null or default value for **EUI-48** is ``FF-FF-FF-FF-FF-FF``
137+
138+
.. [#IEEE-Eui] `Guidelines for Use of Extended Unique Identifier (EUI), Organizationally Unique Identifier (OUI), and Company ID (CID) <https://standards.ieee.org/content/dam/ieee-standards/standards/web/documents/tutorials/eui.pdf>`_
139+
140+
.. [#Eui-Oui] *Organizationally Unique Identifier (OUI)* is the first 3-bytes (24-bits) of a MAC-48 MAC Address.
141+
142+
.. [#Eui-Cid] *Company Id (Cid)* is the last 3-bytes (24-bits) of a MAC-48 MAC Address.
143+
144+
.. [#EUI-Usable] Usable **EUI-48** values are based on a zeroed OUI. A all zero EUI value, such as ``00-00-00-00-00-00`` and ``00-00-00-FA-BC-21``, according to spec shall not be used as an identifier.

0 commit comments

Comments
 (0)