Subversion Repositories oidinfo_api

Rev

Rev 36 | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. <?php
  2.  
  3. /*
  4.  * MAC (EUI-48 and EUI-64) utils for PHP
  5.  * Copyright 2017 - 2024 Daniel Marschall, ViaThinkSoft
  6.  * Version 2024-06-30
  7.  *
  8.  * Licensed under the Apache License, Version 2.0 (the "License");
  9.  * you may not use this file except in compliance with the License.
  10.  * You may obtain a copy of the License at
  11.  *
  12.  *     http://www.apache.org/licenses/LICENSE-2.0
  13.  *
  14.  * Unless required by applicable law or agreed to in writing, software
  15.  * distributed under the License is distributed on an "AS IS" BASIS,
  16.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17.  * See the License for the specific language governing permissions and
  18.  * limitations under the License.
  19.  */
  20.  
  21. // Very good resources for information about OUI, EUI, MAC, ...
  22. // - https://mac-address.alldatafeeds.com/faq#how-to-recognise-mac-address-application
  23. // - https://standards.ieee.org/wp-content/uploads/import/documents/tutorials/eui.pdf
  24. // - https://en.m.wikipedia.org/wiki/Organizationally_unique_identifier
  25.  
  26. // Note about terminology: A NUI is an EUI, ELI, AAI, or SAI
  27.  
  28. const IEEE_MAC_REGISTRY = __DIR__ . '/../web-data';
  29.  
  30. if (!function_exists('_random_int')) {
  31.         function _random_int($min, $max) {
  32.                 // This function tries a CSRNG and falls back to a RNG if no CSRNG is available
  33.                 try {
  34.                         return random_int($min, $max);
  35.                 } catch (Exception $e) {
  36.                         return mt_rand($min, $max);
  37.                 }
  38.         }
  39. }
  40.  
  41. /**
  42.  * Generates a random new AAI.
  43.  * @param int $bits Must be 48 or 64
  44.  * @param bool $multicast Should it be multicast?
  45.  */
  46. function gen_aai(int $bits, bool $multicast): string {
  47.         if (($bits != 48) && ($bits != 64)) throw new Exception("Invalid bits for gen_aai(). Must be 48 or 64.");
  48.         $bytes = [];
  49.         for ($i=0; $i<($bits==48?6:8); $i++) {
  50.                 $val = _random_int(0x00, 0xFF);
  51.                 if ($i == 0) {
  52.                         // Make it an AAI
  53.                         $val = $val & 0xF0 | ($multicast ? 0x03 : 0x02);
  54.                 }
  55.                 $bytes[] = sprintf('%02x',$val);
  56.         }
  57.         return strtoupper(implode('-',$bytes));
  58. }
  59.  
  60. /**
  61.  * Checks if a MAC, EUI, ELI, AAI, SAI, or IPv6-Link-Local address is valid
  62.  * @param string $mac MAC, EUI, or IPv6-Link-Local Address
  63.  * @return bool True if it is valid
  64.  */
  65. function mac_valid(string $mac): bool {
  66.         $tmp = ipv6linklocal_to_mac48($mac);
  67.         if ($tmp !== false) $mac = $tmp;
  68.  
  69.         $mac = str_replace(array('-', ':'), '', $mac);
  70.         $mac = strtoupper($mac);
  71.  
  72.         if ((strlen($mac) != 12) && (strlen($mac) != 16)) return false;
  73.  
  74.         $mac = preg_replace('@[0-9A-F]@', '', $mac);
  75.  
  76.         return ($mac === '');
  77. }
  78.  
  79. /**
  80.  * Returns the amount of bits of a MAC, EUI, ELI, AAI, or SAI
  81.  * @param string $mac
  82.  * @return false|int
  83.  */
  84. function eui_bits(string $mac) {
  85.         if (!mac_valid($mac)) return false;
  86.         $mac = mac_canonize($mac, '');
  87.         return (int)(strlen($mac)*4);
  88. }
  89.  
  90. /**
  91.  * Canonizes a MAC, EUI, ELI, AAI, SAI, or IPv6-Link-Local address
  92.  * @param string $mac MAC, EUI, ELI, or IPv6-Link-Local Address
  93.  * @param string $delimiter Desired delimiter for inserting between each octet
  94.  * @return string|false The canonized address (Note: IPv6-Link-Local becomes EUI-64)
  95.  */
  96. function mac_canonize(string $mac, string $delimiter="-") {
  97.         if (!mac_valid($mac)) return false;
  98.  
  99.         $tmp = ipv6linklocal_to_mac48($mac);
  100.         if ($tmp !== false) $mac = $tmp;
  101.  
  102.         $mac = strtoupper($mac);
  103.         $mac = preg_replace('@[^0-9A-F]@', '', $mac);
  104.         if ((strlen($mac) != 12) && (strlen($mac) != 16)) return false;
  105.         $mac = preg_replace('@^(..)(..)(..)(..)(..)(..)(..)(..)$@', '\\1'.$delimiter.'\\2'.$delimiter.'\\3'.$delimiter.'\\4'.$delimiter.'\\5'.$delimiter.'\\6'.$delimiter.'\\7'.$delimiter.'\\8', $mac);
  106.         return preg_replace('@^(..)(..)(..)(..)(..)(..)$@', '\\1'.$delimiter.'\\2'.$delimiter.'\\3'.$delimiter.'\\4'.$delimiter.'\\5'.$delimiter.'\\6', $mac);
  107. }
  108.  
  109. /**
  110.  * @param string $file
  111.  * @param string $registry_name
  112.  * @param string $mac
  113.  * @return false|string
  114.  */
  115. function _lookup_ieee_registry(string $file, string $registry_name, string $mac) {
  116.         $mac = mac_canonize($mac, '');
  117.         if ($mac === false) return false;
  118.         $begin = substr($mac, 0, 2).'-'.substr($mac, 2, 2).'-'.substr($mac, 4, 2);
  119.         $f = file_get_contents($file);
  120.  
  121.         $f = str_replace("\r", '', $f);
  122.  
  123.         # We are using a positive-lookahead because entries like the MA-M references have a blank line between organization and address
  124.         preg_match_all('@^\s*'.preg_quote($begin, '@').'\s+\(hex\)\s+(\S+)\s+(.*)\n\n\s*(?=[0-9A-F])@ismU', "$f\n\nA", $m, PREG_SET_ORDER);
  125.         foreach ($m as $n) {
  126.                 preg_match('@(\S+)\s+\(base 16\)(.*)$@ism', $n[2], $m);
  127.  
  128.                 if (preg_match('@(.+)-(.+)@ism', $m[1], $o)) {
  129.                         $z = hexdec(substr($mac, 6, 6));
  130.                         $beg = hexdec($o[1]);
  131.                         $end = hexdec($o[2]);
  132.                         if (($z < $beg) || ($z > $end)) continue;
  133.                 } else {
  134.                         $beg = 0x000000;
  135.                         $end = 0xFFFFFF;
  136.                 }
  137.  
  138.                 $x = trim(preg_replace('@^\s+@im', '', $m[2]));
  139.  
  140.                 # "PRIVATE" entries are only marked at the "(hex)" line, but not at the "(base16)" line
  141.                 if ($x == '') $x = trim($n[1]);
  142.  
  143.                 $x = explode("\n", $x);
  144.  
  145.                 // The 12 is hardcoded and is valid for MAC-48 and MAC-64!
  146.                 // Reason: The length of the prefix is calculated from MAC-48. MAC-64 just extends the vendor-specific part
  147.                 // end-beg (= range)  OUI24 0xFFFFFF  len=6    12-6 = 6 nibbles prefix
  148.                 //                    OUI28 0xFFFFF   len=5    12-5 = 7 nibbles prefix
  149.                 //                    OUI36 0xFFF     len=3    12-3 = 9 nibbles prefix
  150.                 $prefix_len = 12-strlen(dechex($end-$beg));
  151.  
  152.                 $out = sprintf("%-32s 0x%s\n", "IEEE $registry_name:", substr($mac, 0, $prefix_len));
  153.                 $out .= sprintf("%-32s 0x%s\n", "Vendor-specific part:", substr($mac, $prefix_len));
  154.                 $out .= sprintf("%-32s %s\n", "Registrant:", $x[0]);
  155.  
  156.                 foreach ($x as $n => $y) {
  157.                         if ($n == 0) continue;
  158.                         else if ($n == 1) $out .= sprintf("%-32s %s\n", "Address of registrant:", $y);
  159.                         else if ($n >= 2) $out .= sprintf("%-32s %s\n", "", $y);
  160.                 }
  161.  
  162.                 return $out;
  163.         }
  164.  
  165.         return false;
  166. }
  167.  
  168. /**
  169.  * Try to Decapsulate EUI-64 into MAC-48 or EUI-48
  170.  * @param string $eui64
  171.  * @return false|string If EUI-64 can be converted into EUI-48, returns EUI-48, otherwise returns EUI-64. On invalid input, return false.
  172.  */
  173. function eui64_to_eui48(string $eui64) {
  174.         if (!mac_valid($eui64)) return false;
  175.         $eui64 = mac_canonize($eui64, '');
  176.         if (eui_bits($eui64) == 48) return mac_canonize($eui64);
  177.         if (($eui64[1] != '0') && ($eui64[1] != '4') && ($eui64[1] != '8') && ($eui64[1] != 'C')) return false; // only allow EUI
  178.  
  179.         if (substr($eui64, 6, 4) == 'FFFF') {
  180.                 // EUI-64 to MAC-48
  181.                 return mac_canonize(substr($eui64, 0, 6).substr($eui64, 10, 6));
  182.         } else if (substr($eui64, 6, 4) == 'FFFE') {
  183.                 if ((hexdec($eui64[1])&2) == 2) {
  184.                         // Modified EUI-64 to MAC/EUI-48
  185.                         $eui64[1] = dechex(hexdec($eui64[1])&253); // remove bit
  186.                         return mac_canonize(substr($eui64, 0, 6).substr($eui64, 10, 6));
  187.                 } else {
  188.                         // EUI-64 to EUI-48
  189.                         return mac_canonize(substr($eui64, 0, 6).substr($eui64, 10, 6));
  190.                 }
  191.         } else {
  192.                 return mac_canonize($eui64);
  193.         }
  194. }
  195.  
  196. /**
  197.  * MAC-48 to EUI-64 Encapsulation
  198.  * @param string $mac48 MAC-48 address
  199.  * @return false|string EUI-64 address
  200.  */
  201. function mac48_to_eui64(string $mac48) {
  202.         // Note: MAC-48 is used for network hardware; EUI-48 is used to identify other devices and software.
  203.         //       MAC48-to-EUI64 Encapsulation uses 0xFFFF middle part
  204.         if (!mac_valid($mac48)) return false;
  205.         $mac48 = mac_canonize($mac48, '');
  206.         if (eui_bits($mac48) == 64) return mac_canonize($mac48);
  207.         if (($mac48[1] != '0') && ($mac48[1] != '4') && ($mac48[1] != '8') && ($mac48[1] != 'C')) return false; // only allow EUI
  208.  
  209.         $eui64 = substr($mac48, 0, 6).'FFFF'.substr($mac48, 6, 6);
  210.         return mac_canonize($eui64);
  211. }
  212.  
  213. /**
  214.  * EUI-48 to EUI-64 Encapsulation
  215.  * @param string $eui48 EUI-48 address
  216.  * @return false|string EUI-64 address
  217.  */
  218. function eui48_to_eui64(string $eui48) {
  219.         // Note: MAC-48 is used for network hardware; EUI-48 is used to identify other devices and software.
  220.         //       EUI48-to-EUI64 Encapsulation uses 0xFFFF middle part
  221.         if (!mac_valid($eui48)) return false;
  222.         $eui48 = mac_canonize($eui48, '');
  223.         if (eui_bits($eui48) == 64) return mac_canonize($eui48);
  224.         if (($eui48[1] != '0') && ($eui48[1] != '4') && ($eui48[1] != '8') && ($eui48[1] != 'C')) return false; // only allow EUI
  225.  
  226.         $eui64 = substr($eui48, 0, 6).'FFFE'.substr($eui48, 6, 6);
  227.         return mac_canonize($eui64);
  228. }
  229.  
  230. /**
  231.  * MAC/EUI-48 to Modified EUI-64 Encapsulation
  232.  * @param string $eui48 MAC-48 or EUI-48 address
  233.  * @return false|string Modified EUI-64 address
  234.  */
  235. function maceui48_to_modeui64(string $eui48) {
  236.         // Note: MAC-48 is used for network hardware; EUI-48 is used to identify other devices and software.
  237.         //       EUI48-to-ModifiedEUI64 Encapsulation uses 0xFFFE middle part (SIC! This was a mistake by IETF, since it should actually be 0xFFFF!)
  238.         if (!mac_valid($eui48)) return false;
  239.         $eui48 = mac_canonize($eui48, '');
  240.         if (eui_bits($eui48) == 64) return mac_canonize($eui48);
  241.         if (($eui48[1] != '0') && ($eui48[1] != '4') && ($eui48[1] != '8') && ($eui48[1] != 'C')) return false; // only allow EUI
  242.  
  243.         $eui64 = substr($eui48, 0, 6).'FFFE'.substr($eui48, 6, 6);
  244.  
  245.         $eui64[1] = dechex(hexdec($eui64[1]) | 2); // flip seventh bit
  246.  
  247.         return mac_canonize($eui64);
  248. }
  249.  
  250. /**
  251.  * Try to convert IPv6-Link-Local address to MAC-48
  252.  * @param string $ipv6 IPv6-Link-Local address
  253.  * @return false|string MAC-48 (or IPv6 if it was no LinkLocal address, or Modified EUI-64 if it decapsulation failed)
  254.  */
  255. function ipv6linklocal_to_mac48(string $ipv6) {
  256.         // https://stackoverflow.com/questions/12095835/quick-way-of-expanding-ipv6-addresses-with-php (modified)
  257.         $tmp = @inet_pton($ipv6);
  258.         if ($tmp === false) return false;
  259.         $hex = unpack("H*hex", $tmp);
  260.         $ipv6 = substr(preg_replace("/([A-f0-9]{4})/", "$1:", $hex['hex']), 0, -1);
  261.  
  262.         // Remove "fe80::" to convert IPv6-Link-Local address back to EUI-64
  263.         // see https://support.lenovo.com/de/de/solutions/ht509925-how-to-convert-a-mac-address-into-an-ipv6-link-local-address-eui-64
  264.         $cnt = 0;
  265.         $mac = preg_replace('@^fe80:0000:0000:0000:@i', '', $ipv6, -1, $cnt);
  266.         if ($cnt == 0) return false;
  267.  
  268.         // Set LAA to UAA again
  269.         $mac_uaa_64 = $mac;
  270.         $mac_uaa_64[1] = dechex(hexdec($mac_uaa_64[1]) & 253);
  271.  
  272.         $mac_uaa_48 = eui64_to_eui48($mac_uaa_64);
  273.         if (eui_bits($mac_uaa_48) == 48) {
  274.                 return $mac_uaa_48; // Output MAC-48 (UAA)
  275.         } else {
  276.                 return $mac; // Failed decapsulation; output Modified EUI-64 instead
  277.         }
  278. }
  279.  
  280. /**
  281.  * Converts MAC-48 or EUI-48 to IPv6-Link-Local (based on Modified EUI-64)
  282.  * @param string $mac
  283.  * @return false|string
  284.  */
  285. function maceui_to_ipv6linklocal(string $mac) {
  286.         if (!mac_valid($mac)) return false;
  287.         if (eui_bits($mac) == 48) {
  288.                 $mac = maceui48_to_modeui64($mac);
  289.         }
  290.         $mac = mac_canonize($mac, '');
  291.         $mac = str_pad($mac, 16, '0', STR_PAD_LEFT);
  292.         return strtolower('fe80::'.substr($mac,0, 4).':'.substr($mac,4, 4).':'.substr($mac,8, 4).':'.substr($mac,12, 4));
  293. }
  294.  
  295. /**
  296.  * @param string $mac
  297.  * @return string
  298.  * @throws Exception
  299.  */
  300. function mac_type(string $mac): string {
  301.         // Format MAC for machine readability
  302.         $mac = mac_canonize($mac, '');
  303.  
  304.         /*
  305.          *
  306.          *      ZYXM
  307.          * 0    0000    EUI (OUI)
  308.          * 1    0001    [Multicast]
  309.          * 2    0010    AAI
  310.          * 3    0011    [Multicast]
  311.          * 4    0100    EUI (OUI)
  312.          * 5    0101    [Multicast]
  313.          * 6    0110    Reserved
  314.          * 7    0111    [Multicast]
  315.          * 8    1000    EUI (OUI)
  316.          * 9    1001    [Multicast]
  317.          * A    1010    ELI (CID)
  318.          * B    1011    [Multicast]
  319.          * C    1100    EUI (OUI)
  320.          * D    1101    [Multicast]
  321.          * E    1110    SAI
  322.          * F    1111    [Multicast]
  323.          *
  324.          */
  325.  
  326.         $type = '';
  327.         $tmp = ipv6linklocal_to_mac48($mac);
  328.         if ($tmp !== false) {
  329.                 $mac = $tmp;
  330.                 $type = 'IPv6-Link-Local';
  331.         }
  332.         if (!mac_valid($mac)) throw new Exception("Invalid MAC address");
  333.         if ($tmp === false) {
  334.                 if (($mac[1] == '2') || ($mac[1] == '3')) {
  335.                         /*
  336.                          * AAI: Administratively Assigned Identifier
  337.                          * Administrators who wish to assign local MAC addresses in an
  338.                          * arbitrary fashion (for example, randomly) and yet maintain
  339.                          * compatibility with other assignment protocols operating under the
  340.                          * SLAP on the same LAN may assign a local MAC address as AAI.
  341.                          */
  342.                         $type = 'AAI-' . eui_bits($mac).' (Administratively Assigned Identifier)';
  343.                 } else if (($mac[1] == '6') || ($mac[1] == '7')) {
  344.                         /*
  345.                          * Reserved
  346.                          * may be administratively used and assigned in accordance with the
  347.                          * considerations specified for AAI usage, without effect on SLAP
  348.                          * assignments. However, administrators should be cognizant of
  349.                          * possible future specifications… that would render administrative
  350.                          * assignment incompatible with the SLAP.
  351.                          */
  352.                         $type = 'Reserved-' . eui_bits($mac);
  353.                 } else if (($mac[1] == 'A') || ($mac[1] == 'B')) {
  354.                         /*
  355.                          * ELI: Extended Local Identifier
  356.                          * An ELI is based on a 24 bit CID
  357.                          * A CID has ZYXM bits set to 1010 (0b1010 = 0xA)
  358.                          * Since X=1 (U/L=1), the CID cannot be used to form a universal UAA MAC (only a local LAA MAC)
  359.                          */
  360.                         $type = 'ELI-' . eui_bits($mac).' (Extended Local Identifier)';
  361.                 } else if (($mac[1] == 'E') || ($mac[1] == 'F')) {
  362.                         /*
  363.                          * SAI: Standard Assigned Identifier
  364.                          * Specification of the use of the SAI quadrant for SLAP address
  365.                          * assignments is reserved for the standard forthcoming from IEEE
  366.                          * P802.1CQ.
  367.                          * An SAI is assigned by a protocol specified in an IEEE 802 standard.
  368.                          * Multiple protocols for assigning SAI may be specified within various
  369.                          * IEEE 802 standards. Coexistence of such protocols may be supported
  370.                          * by restricting each to assignments within a subspace of SAI space.
  371.                          * In some cases, an SAI assignment protocol may assign the SAI to convey
  372.                          * specific information. Such information may be interpreted by receivers
  373.                          * and bridges that recognize the specific SAI assignment protocol, as
  374.                          * identified by the subspace of the SAI. The functionality of receivers
  375.                          * and bridges that do not recognize the protocol is not affected.
  376.                          */
  377.                         $type = 'SAI-' . eui_bits($mac).' (Standard Assigned Identifier)';
  378.                 } else {
  379.                         /*
  380.                          * Extended Unique Identifier
  381.                          * Based on an OUI-24, OUI-28, or OUI-36
  382.                          */
  383.                         if (eui_bits($mac) == 48) {
  384.                                 // The name "MAC-48" has been deprecated by IEEE
  385.                                 //$type = 'MAC-48 (network hardware) or EUI-48 (other devices and software)';
  386.                                 $type = 'EUI-48 (Extended Unique Identifier)';
  387.                         } else if (eui_bits($mac) == 64) {
  388.                                 if (substr($mac, 6, 4) == 'FFFE') {
  389.                                         if ((hexdec($mac[1]) & 2) == 2) {
  390.                                                 $type = 'EUI-64 (Extended Unique Identifier, MAC/EUI-48 to Modified EUI-64 Encapsulation)';
  391.                                         } else {
  392.                                                 $type = 'EUI-64 (Extended Unique Identifier, EUI-48 to EUI-64 Encapsulation)';
  393.                                         }
  394.                                 } else if (substr($mac, 6, 4) == 'FFFF') {
  395.                                         $type = 'EUI-64 (Extended Unique Identifier, MAC-48 to EUI-64 Encapsulation)';
  396.                                 } else {
  397.                                         $type = 'EUI-64 (Extended Unique Identifier)';
  398.                                 }
  399.                         } else {
  400.                                 assert(false); /** @phpstan-ignore-line */
  401.                         }
  402.                 }
  403.         }
  404.  
  405.         if ((hexdec($mac[1])&1) == 1) {
  406.                 // see also https://networkengineering.stackexchange.com/questions/83121/can-eli-aai-sai-addresses-be-multicast
  407.  
  408.                 /* https://standards.ieee.org/wp-content/uploads/import/documents/tutorials/eui.pdf writes:
  409.                  * - The assignee of an OUI or OUI-36 is exclusively authorized to assign group
  410.                  *   MAC addresses, with I/G=1, by extending a modified version of the assigned
  411.                  *   OUI or OUI-36 in which the M bit is set to 1. Such addresses are not EUIs and
  412.                  *   do not globally identify hardware instances, even though U/L=0.
  413.                  * - The assignee of a CID may assign local group MAC addresses by extending a modified version of
  414.                  *   the assigned CID by setting the M bit to 1 (so that I/G=1). The resulting
  415.                  *   extended identifier is an ELI.
  416.                  */
  417.  
  418.                 // TODO: If "Multicast EUI" is not an EUI (as tutorials/eui.pdf states), how should we name it instead?!
  419.                 $type = "Multicast $type";
  420.         }
  421.  
  422.         return $type;
  423. }
  424.  
  425. /**
  426.  * Prints information about an IPv6-Link-Local address, MAC, EUI, ELI, AAI, or SAI.
  427.  * @param string $mac IPv6-Link-Local address, MAC, EUI, ELI, AAI, or SAI
  428.  * @return void
  429.  * @throws Exception
  430.  */
  431. function decode_mac(string $mac) {
  432.  
  433.         // TODO: Should we decode Multicast MAC to its IP (see https://ipcisco.com/lesson/multicast-mac-addresses/)?
  434.  
  435.         echo sprintf("%-32s %s\n", "Input:", $mac);
  436.  
  437.         // Format MAC for machine readability
  438.         $mac = mac_canonize($mac, '');
  439.  
  440.         $type = mac_type($mac);
  441.         echo sprintf("%-32s %s\n", "Type:", $type);
  442.  
  443.         echo "\n";
  444.  
  445.         $is_eli_unicast = (hexdec($mac[1]) & 0b1111) == 0b1010;  // ELI = 1010 (unicast)
  446.         $is_eli         = (hexdec($mac[1]) & 0b1110) == 0b1010;  // ELI = 101x (unicast and multicast)
  447.  
  448.         $is_eui_unicast = (hexdec($mac[1]) & 0b0011) == 0b0000;  // EUI = xx00 (unicast)
  449.         $is_eui         = (hexdec($mac[1]) & 0b0010) == 0b0000;  // EUI = xx0x (unicast and multicast)
  450.  
  451.         $is_sai         = (hexdec($mac[1]) & 0b1110) == 0b1110;  // SAI = 111x (unicast and multicast)
  452.  
  453.         // Show various representations
  454.         if ($is_sai) {
  455.                 // Information taken from this paper:
  456.                 // https://www.techrxiv.org/users/684395/articles/678862-link-layer-software-defined-addressing-using-block-address-registration-and-claiming-barc
  457.                 // DOI: 10.36227/techrxiv.19105118.v1
  458.                 // and
  459.                 // https://datatracker.ietf.org/meeting/112/materials/slides-112-intarea-p8021cq-00
  460.  
  461.                 $n0 = substr($mac,0,1);
  462.                 $n1 = substr($mac,1,1);
  463.                 $n2 = substr($mac,2,1);
  464.                 $r = (hexdec($n0)&0b1000) ? 1 : 0;
  465.                 $i = (hexdec($n0)&0b0100) ? 1 : 0;
  466.                 $jk = hexdec($n0)&0b0011;
  467.                 $m = ($n1=='F') ? 1 : 0;
  468.  
  469.                 if (($n2=="0") && ($r==0) && ($i==1)) {
  470.                         $barc_type = 'CA';
  471.                 } else if (($n2=="0"/*Only CA or also CABA?*/) && ($r==0) && ($i==0) && ($m==1)) {
  472.                         $barc_type = 'CABA';
  473.                 } else if (($n2=="0") && ($r==0) && ($i==0) && ($jk==0) && ($m==0)) {
  474.                         $barc_type = 'TUA';
  475.                 } else if ($r==1) {
  476.                         $barc_type = 'RA';
  477.                 } else {
  478.                         $barc_type = '';
  479.                 }
  480.  
  481.                 if ($barc_type == '') {
  482.                         echo sprintf("%-32s %s\n", "SAI Standard:", "Unknown IEEE 802 Standard");
  483.                 } else {
  484.                         echo sprintf("%-32s %s\n", "SAI Standard:", "BARC (IEEE 802.1CQ)"); // Block Address Registration and Claiming (BARC)
  485.                         if ($barc_type == 'CA') {
  486.                                 echo sprintf("%-32s %s\n", "BARC Address Type:", "Claimable Address (CA)");
  487.                                 echo sprintf("%-32s %s\n", "BARC CAB Size:", $jk);
  488.                         } else if ($barc_type == 'CABA') {
  489.                                 echo sprintf("%-32s %s\n", "BARC Address Type:", "Claimable Address Block Address (CABA)");
  490.                                 echo sprintf("%-32s %s\n", "BARC CAB Size:", $jk);
  491.                         } else if ($barc_type == 'TUA') {
  492.                                 echo sprintf("%-32s %s\n", "BARC Address Type:", "Temporary Unicast Address (TUA)");
  493.                         } else if ($barc_type == 'RA') { /** @phpstan-ignore-line */
  494.                                 echo sprintf("%-32s %s\n", "BARC Address Type:", "Registrable Address (RA)");
  495.                                 echo sprintf("%-32s %s\n", "BARC RABI Option:", $i);
  496.                                 echo sprintf("%-32s %s\n", "BARC RABI Size:", $jk);
  497.                         } else {
  498.                                 assert(false); /** @phpstan-ignore-line */
  499.                         }
  500.                         echo sprintf("%-32s %s\n", "BARC Semantic Prefix:", "0x".strtoupper(substr($mac,0,5)));
  501.                         echo sprintf("%-32s %s\n", "BARC Data:", "0x".strtoupper(substr($mac,5)));
  502.                 }
  503.                 echo "\n";
  504.         } else if ($is_eli) {
  505.                 // Note: There does not seem to exist an algorithm for encapsulating/converting ELI-48 <=> ELI-64
  506.                 echo sprintf("%-32s %s\n", "ELI-".eui_bits($mac).":", mac_canonize($mac));
  507.                 $mac48 = eui64_to_eui48($mac);
  508.                 echo sprintf("%-32s %s\n", "MAC-48 (Local):", (eui_bits($mac48) != 48) ? 'Not available' : $mac48);
  509.         } else if ($is_eui) {
  510.                 $eui48 = eui64_to_eui48($mac);
  511.                 echo sprintf("%-32s %s\n", "EUI-48 or MAC-48:", (eui_bits($eui48) != 48) ? 'Not available' : $eui48);
  512.                 if (eui_bits($mac) == 48) {
  513.                         $eui64 = mac48_to_eui64($mac);
  514.                         echo sprintf("%-32s %s\n", "EUI-64:", ((eui_bits($eui64) != 64) ? 'Not available' : $eui64).' (MAC-48 to EUI-64 Encapsulation)');
  515.                         $eui64 = eui48_to_eui64($mac);
  516.                         echo sprintf("%-32s %s\n", "", ((eui_bits($eui64) != 64) ? 'Not available' : $eui64).' (EUI-48 to EUI-64 Encapsulation)');
  517.                         $eui64 = maceui48_to_modeui64($mac);
  518.                         echo sprintf("%-32s %s\n", "", ((eui_bits($eui64) != 64) ? 'Not available' : $eui64).' (MAC/EUI-48 to Modified EUI-64 Encapsulation)');
  519.                         $ipv6 = maceui_to_ipv6linklocal($mac);
  520.                         echo sprintf("%-32s %s\n", "IPv6-Link-Local address:", $ipv6);
  521.                 } else {
  522.                         $eui64 = mac_canonize($mac);
  523.                         echo sprintf("%-32s %s\n", "EUI-64:", $eui64);
  524.                 }
  525.         }
  526.  
  527.         // Vergabestelle
  528.         $ul = hexdec($mac[1]) & 2; // Bit #LSB+1 of Byte 1
  529.         $ul_ = ($ul == 0) ? '[0] Universally Administered Address (UAA)' : '[1] Locally Administered Address (LAA)';
  530.         echo sprintf("%-32s %s\n", "Administration type (U/L flag):", $ul_);
  531.  
  532.         // Empfaengergruppe
  533.         $ig = hexdec($mac[1]) & 1; // Bit #LSB+0 of Byte 1
  534.         $ig_ = ($ig == 0) ? '[0] Unicast (Individual)' : '[1] Multicast (Group)';
  535.         echo sprintf("%-32s %s\n", "Transmission type (I/G flag):", $ig_);
  536.  
  537.         // Query IEEE registries
  538.         if (count(glob(IEEE_MAC_REGISTRY.DIRECTORY_SEPARATOR.'*.txt')) > 0) {
  539.                 $alt_mac = $mac;
  540.                 $alt_mac[1] = dechex(hexdec($alt_mac[1])^1); // switch Unicast<=>Multicast in order to find the vendor
  541.  
  542.                 if (is_dir(IEEE_MAC_REGISTRY)) {
  543.                         if ($is_eli) {
  544.                                 // Query the CID registry
  545.                                 if (
  546.                                         ($x = _lookup_ieee_registry(IEEE_MAC_REGISTRY . DIRECTORY_SEPARATOR . 'cid.txt', 'CID', $mac)) ||
  547.                                         ($x = _lookup_ieee_registry(IEEE_MAC_REGISTRY . DIRECTORY_SEPARATOR . 'cid.txt', 'CID', $alt_mac))
  548.                                 ) {
  549.                                         echo "\n";
  550.                                         echo $x;
  551.                                 } else {
  552.                                         $registry_name = 'CID';
  553.                                         echo "\n";
  554.                                         echo sprintf("%-32s 0x%s\n", "IEEE $registry_name:", substr($mac, 0, 6));
  555.                                         echo sprintf("%-32s 0x%s\n", "Vendor-specific part:", substr($mac, 6));
  556.                                         echo sprintf("%-32s %s\n", "Registrant:", "$registry_name not found in database");
  557.                                 }
  558.                         } else if ($is_eui) {
  559.                                 // Query the OUI registries
  560.                                 if (
  561.                                         # The IEEE Registration Authority distinguishes between IABs and OUI-36 values. Both are 36-bit values which may be used to generate EUI-48 values, but IABs may not be used to generate EUI-64 values.[6]
  562.                                         # Note: The Individual Address Block (IAB) is an inactive registry activity, which has been replaced by the MA-S registry product as of January 1, 2014.
  563.                                         ($x = _lookup_ieee_registry(IEEE_MAC_REGISTRY . DIRECTORY_SEPARATOR . 'iab.txt', 'IAB', $mac)) ||
  564.                                         ($x = _lookup_ieee_registry(IEEE_MAC_REGISTRY . DIRECTORY_SEPARATOR . 'oui36.txt', 'OUI-36 (MA-S)', $mac)) ||
  565.                                         ($x = _lookup_ieee_registry(IEEE_MAC_REGISTRY . DIRECTORY_SEPARATOR . 'mam.txt', '28 bit identifier (MA-M)', $mac)) ||
  566.                                         ($x = _lookup_ieee_registry(IEEE_MAC_REGISTRY . DIRECTORY_SEPARATOR . 'oui.txt', 'OUI (MA-L)', $mac)) ||
  567.                                         ($x = _lookup_ieee_registry(IEEE_MAC_REGISTRY . DIRECTORY_SEPARATOR . 'iab.txt', 'IAB', $alt_mac)) ||
  568.                                         ($x = _lookup_ieee_registry(IEEE_MAC_REGISTRY . DIRECTORY_SEPARATOR . 'oui36.txt', 'OUI-36 (MA-S)', $alt_mac)) ||
  569.                                         ($x = _lookup_ieee_registry(IEEE_MAC_REGISTRY . DIRECTORY_SEPARATOR . 'mam.txt', '28 bit identifier (MA-M)', $alt_mac)) ||
  570.                                         ($x = _lookup_ieee_registry(IEEE_MAC_REGISTRY . DIRECTORY_SEPARATOR . 'oui.txt', 'OUI (MA-L)', $alt_mac))
  571.                                 ) {
  572.                                         echo "\n";
  573.                                         echo $x;
  574.                                 } else {
  575.                                         $registry_name = 'OUI (MA-L)?';
  576.                                         echo "\n";
  577.                                         echo sprintf("%-32s 0x%s\n", "IEEE $registry_name:", substr($mac, 0, 6));
  578.                                         echo sprintf("%-32s 0x%s\n", "Vendor-specific part:", substr($mac, 6));
  579.                                         echo sprintf("%-32s %s\n", "Registrant:", "$registry_name not found in database");
  580.  
  581.                                         $registry_name = '28 bit identifier (MA-M)?';
  582.                                         echo "\n";
  583.                                         echo sprintf("%-32s 0x%s\n", "IEEE $registry_name:", substr($mac, 0, 7));
  584.                                         echo sprintf("%-32s 0x%s\n", "Vendor-specific part:", substr($mac, 7));
  585.                                         echo sprintf("%-32s %s\n", "Registrant:", "$registry_name not found in database");
  586.  
  587.                                         $registry_name = 'OUI-36 (MA-S)?';
  588.                                         echo "\n";
  589.                                         echo sprintf("%-32s 0x%s\n", "IEEE $registry_name:", substr($mac, 0, 9));
  590.                                         echo sprintf("%-32s 0x%s\n", "Vendor-specific part:", substr($mac, 9));
  591.                                         echo sprintf("%-32s %s\n", "Registrant:", "$registry_name not found in database");
  592.                                 }
  593.                         }
  594.                 }
  595.         }
  596.  
  597.         $vm = '';
  598.         // === FAQ "Detection rules which don't have their dedicated page yet" ===
  599.         // https://wiki.xenproject.org/wiki/Xen_Networking
  600.         // https://mcpmag.com/articles/2007/11/27/hey-vm-whats-your-hypervisor.aspx
  601.         // https://www.techrepublic.com/blog/data-center/mac-address-scorecard-for-common-virtual-machine-platforms
  602.         if (mac_between($mac, '00:16:3E:00:00:00', '00:16:3E:FF:FF:FF')) $vm = "Red Hat Xen, XenSource, Novell Xen";
  603.         // http://techgenix.com/mac-address-pool-duplication-hyper-v/
  604.         // https://docs.microsoft.com/en-us/system-center/vmm/network-mac?view=sc-vmm-1807
  605.         // https://blogs.technet.microsoft.com/gbanin/2014/08/27/how-to-solve-mac-address-conflict-on-hyper-v/
  606.         if (mac_between($mac, '00:1D:D8:B7:1C:00', '00:1D:D8:F4:1F:FF')) $vm = "Microsoft SCVMM (System Center Virtual Machine Manager)";
  607.         // https://mcpmag.com/articles/2007/11/27/hey-vm-whats-your-hypervisor.aspx
  608.         // https://www.techrepublic.com/blog/data-center/mac-address-scorecard-for-common-virtual-machine-platforms/
  609.         // https://blogs.technet.microsoft.com/medv/2011/01/24/how-to-manage-vm-mac-addresses-with-the-globalimagedata-xml-file-in-med-v-v1/
  610.         if (mac_between($mac, '00:03:FF:00:00:00', '00:03:FF:FF:FF:FF')) $vm = "Microsoft Virtual PC / Virtual Server";
  611.         // https://mcpmag.com/articles/2007/11/27/hey-vm-whats-your-hypervisor.aspx
  612.         if (mac_between($mac, '00:18:51:00:00:00', '00:18:51:FF:FF:FF')) $vm = "SWsoft";
  613.         // https://macaddress.io/statistics/company/17619
  614.         if (mac_between($mac, '58:9C:FC:00:00:00', '58:9C:FC:FF:FF:FF')) $vm = "bhyve by FreebsdF";
  615.         // https://macaddress.io/statistics/company/17388
  616.         if (mac_between($mac, '50:6B:8D:00:00:00', '50:6B:8D:FF:FF:FF')) $vm = "Nutanix AHV";
  617.         // https://www.centos.org/forums/viewtopic.php?t=26739
  618.         if (mac_between($mac, '54:52:00:00:00:00', '54:52:FF:FF:FF:FF')) $vm = "KVM (proxmox)";
  619.         // Self tested (alldatafeeds.com)
  620.         if (mac_between($mac, '96:00:00:00:00:00', '96:00:FF:FF:FF:FF')) $vm = "Hetzner vServer (based on KVM and libvirt)";
  621.         // === FAQ "How to recognise a VMware's virtual machine by its MAC address?" ===
  622.         if (mac_between($mac, '00:50:56:00:00:00', '00:50:56:FF:FF:FF')) $vm = "VMware vSphere, VMware Workstation, VMware ESX Server";
  623.         if (mac_between($mac, '00:50:56:80:00:00', '00:50:56:BF:FF:FF')) $vm = "VMware vSphere managed by vCenter Server";
  624.         if (mac_between($mac, '00:0C:29:00:00:00', '00:0C:29:FF:FF:FF')) $vm = "VMWare Standalone VMware vSphere, VMware Workstation, VMware Horizon";
  625.         if (mac_between($mac, '00:05:69:00:00:00', '00:05:69:FF:FF:FF')) $vm = "VMware ESX, VMware GSX Server";
  626.         if (mac_between($mac, '00:1C:14:00:00:00', '00:1C:14:FF:FF:FF')) $vm = "VMWare";
  627.         // === FAQ "machine by its MAC address?" ===
  628.         if (mac_between($mac, '00:1C:42:00:00:00', '00:1C:42:FF:FF:FF')) $vm = "Parallels Virtual Machine";
  629.         // === FAQ "How to recognise a Docker container by its MAC address?" ===
  630.         if (mac_between($mac, '02:42:00:00:00:00', '02:42:FF:FF:FF:FF')) $vm = "Docker container";
  631.         // === FAQ =How to recognise a Microsoft Hyper-V's virtual machine by its MAC address?" ===
  632.         if (mac_between($mac, '00:15:5D:00:00:00', '00:15:5D:FF:FF:FF')) $vm = "Microsoft Hyper-V";
  633.         // === FAQ "How to recognise an Oracle Virtual machine by its MAC address?" ===
  634.         if (mac_between($mac, '08:00:27:00:00:00', '08:00:27:FF:FF:FF')) $vm = "Oracle VirtualBox 5.2"; // Pcs Systemtechnik GmbH
  635.         if (mac_between($mac, '52:54:00:00:00:00', '52:54:00:FF:FF:FF')) $vm = "Oracle VirtualBox 5.2 + Vagrant"; // 52:54:00 (Exact MAC: 52:54:00:C9:C7:04)
  636.         if (mac_between($mac, '00:21:F6:00:00:00', '00:21:F6:FF:FF:FF')) $vm = "Oracle VirtualBox 3.3";
  637.         if (mac_between($mac, '00:14:4F:00:00:00', '00:14:4F:FF:FF:FF')) $vm = "Oracle VM Server for SPARC";
  638.         if (mac_between($mac, '00:0F:4B:00:00:00', '00:0F:4B:FF:FF:FF')) $vm = "Oracle Virtual Iron 4";
  639.  
  640.         if ($vm) {
  641.                 echo sprintf("%-32s %s\n", "Special use:", "Virtual machine $vm");
  642.         }
  643.  
  644.         $app = '';
  645.  
  646.         // === FAQ "Other MAC address applications"
  647.         // http://www.cavebear.com/archive/cavebear/Ethernet/Ethernet.txt
  648.         // https://tools.ietf.org/html/rfc1060
  649.         if (mac_between($mac, '03:00:00:01:00:00', '03:00:40:00:00:00')) $app = 'User-defined (per 802 spec), EtherType is 0x0802';
  650.         if (mac_equals($mac, '01:00:1D:00:00:00')) $app = 'Cabletron PC-OV PC discover (on demand), EtherType is 0x0802';
  651.         if (mac_equals($mac, '01:00:1D:42:00:00')) $app = 'Cabletron PC-OV Bridge discover (on demand), EtherType is 0x0802';
  652.         if (mac_equals($mac, '01:00:1D:52:00:00')) $app = 'Cabletron PC-OV MMAC discover (on demand), EtherType is 0x0802';
  653.         if (mac_between($mac, '01:00:3C:00:00:00' , '01:00:3C:FF:FF:FF')) $app = 'Auspex Systems (Serverguard)';
  654.         if (mac_equals($mac, '01:00:10:00:00:20')) $app = 'Hughes Lan Systems Terminal Server S/W download, EtherType is 0x0802';
  655.         if (mac_equals($mac, '01:00:10:FF:FF:20')) $app = 'Hughes Lan Systems Terminal Server S/W request, EtherType is 0x0802';
  656.         if (mac_equals($mac, '01:00:81:00:00:00')) $app = 'Synoptics Network Management';
  657.         if (mac_equals($mac, '01:00:81:00:00:02')) $app = 'Synoptics Network Management';
  658.         if (mac_equals($mac, '01:00:81:00:01:00')) $app = 'Bay Networks (Synoptics) autodiscovery, EtherType is 0x0802 SNAP type is 0x01A2';
  659.         if (mac_equals($mac, '01:00:81:00:01:01')) $app = 'Bay Networks (Synoptics) autodiscovery, EtherType is 0x0802 SNAP type is 0x01A1';
  660.         if (mac_between($mac, '01:20:25:00:00:00', '01:20:25:7F:FF:FF')) $app = 'Control Technology Inc\'s Industrial Ctrl Proto., EtherType is 0x873A';
  661.         if (mac_equals($mac, '01:80:24:00:00:00')) $app = 'Kalpana Etherswitch every 60 seconds, EtherType is 0x0802';
  662.         if (mac_equals($mac, '01:DD:00:FF:FF:FF')) $app = 'Ungermann-Bass boot-me requests, EtherType is 0x7002';
  663.         if (mac_equals($mac, '01:DD:01:00:00:00')) $app = 'Ungermann-Bass Spanning Tree, EtherType is 0x7005';
  664.         if (mac_equals($mac, '03:00:00:00:00:10')) $app = 'OS/2 1.3 EE + Communications Manager, EtherType is 0x80D5';
  665.         if (mac_equals($mac, '03:00:00:00:00:40')) $app = 'OS/2 1.3 EE + Communications Manager, EtherType is 0x80D5';
  666.         if (mac_equals($mac, '03:00:00:00:01:00')) $app = 'OSI All-IS Multicast, EtherType is 0x0802';
  667.         if (mac_equals($mac, '03:00:00:00:02:00')) $app = 'OSI All-ES Multicast, EtherType is 0x0802';
  668.         if (mac_equals($mac, '03:00:00:80:00:00')) $app = 'Discovery Client, EtherType is 0x0802';
  669.         if (mac_equals($mac, '03:00:FF:FF:FF:FF')) $app = 'All Stations address, EtherType is 0x0802';
  670.         if (mac_between($mac, '09:00:0D:00:00:00', '09:00:0D:FF:FF:FF')) $app = 'ICL Oslan Multicast, EtherType is 0x0802';
  671.         if (mac_equals($mac, '09:00:0D:02:00:00')) $app = 'ICL Oslan Service discover only on boot';
  672.         if (mac_equals($mac, '09:00:0D:02:0A:3C')) $app = 'ICL Oslan Service discover only on boot';
  673.         if (mac_equals($mac, '09:00:0D:02:0A:38')) $app = 'ICL Oslan Service discover only on boot';
  674.         if (mac_equals($mac, '09:00:0D:02:0A:39')) $app = 'ICL Oslan Service discover only on boot';
  675.         if (mac_equals($mac, '09:00:0D:02:FF:FF')) $app = 'ICL Oslan Service discover only on boot';
  676.         if (mac_equals($mac, '09:00:0D:09:00:00')) $app = 'ICL Oslan Service discover as required';
  677.         if (mac_equals($mac, '09:00:1E:00:00:00')) $app = 'Apollo DOMAIN, EtherType is 0x8019';
  678.         if (mac_equals($mac, '09:00:02:04:00:01')) $app = 'Vitalink printer messages, EtherType is 0x8080';
  679.         if (mac_equals($mac, '09:00:02:04:00:02')) $app = 'Vitalink bridge management, EtherType is 0x8080';
  680.         if (mac_equals($mac, '09:00:4C:00:00:0F')) $app = 'BICC Remote bridge adaptive routing (e.g. to Retix), EtherType is 0x0802';
  681.         if (mac_equals($mac, '09:00:4E:00:00:02')) $app = 'Novell IPX, EtherType is 0x8137';
  682.         if (mac_equals($mac, '09:00:6A:00:01:00')) $app = 'TOP NetBIOS';
  683.         if (mac_equals($mac, '09:00:7C:01:00:01')) $app = 'Vitalink DLS Multicast';
  684.         if (mac_equals($mac, '09:00:7C:01:00:03')) $app = 'Vitalink DLS Inlink';
  685.         if (mac_equals($mac, '09:00:7C:01:00:04')) $app = 'Vitalink DLS and non DLS Multicast';
  686.         if (mac_equals($mac, '09:00:7C:02:00:05')) $app = 'Vitalink diagnostics, EtherType is 0x8080';
  687.         if (mac_equals($mac, '09:00:7C:05:00:01')) $app = 'Vitalink gateway, EtherType is 0x8080';
  688.         if (mac_equals($mac, '09:00:7C:05:00:02')) $app = 'Vitalink Network Validation Message';
  689.         if (mac_equals($mac, '09:00:09:00:00:01')) $app = 'HP Probe, EtherType is 0x8005 or 0x0802';
  690.         if (mac_equals($mac, '09:00:09:00:00:04')) $app = 'HP DTC, EtherType is 0x8005';
  691.         if (mac_equals($mac, '09:00:26:01:00:01')) $app = 'Vitalink TransLAN bridge management, EtherType is 0x8038';
  692.         if (mac_equals($mac, '09:00:39:00:70:00')) $app = 'Spider Systems Bridge';
  693.         if (mac_between($mac, '09:00:56:00:00:00', '09:00:56:FE:FF:FF')) $app = 'Stanford reserved';
  694.         if (mac_between($mac, '09:00:56:FF:00:00', '09:00:56:FF:FF:FF')) $app = 'Stanford V Kernel, version 6.0, EtherType is 0x805C';
  695.         if (mac_equals($mac, '09:00:77:00:00:00')) $app = 'Retix Bridge Local Management System, EtherType is 0x0802';
  696.         if (mac_equals($mac, '09:00:77:00:00:01')) $app = 'Retix spanning tree bridges, EtherType is 0x0802';
  697.         if (mac_equals($mac, '09:00:77:00:00:02')) $app = 'Retix Bridge Adaptive routing, EtherType is 0x0802';
  698.         if (mac_equals($mac, '09:00:87:80:FF:FF')) $app = 'Xyplex Terminal Servers, EtherType is 0x0889';
  699.         if (mac_equals($mac, '09:00:87:90:FF:FF')) $app = 'Xyplex Terminal Servers, EtherType is 0x0889';
  700.         if (mac_between($mac, '44:38:39:FF:00:00', '44:38:39:FF:FF:FF')) $app = 'Multi-Chassis Link Aggregation (Cumulus Linux)';
  701.         if (mac_equals($mac, 'FF:FF:00:40:00:01')) $app = 'LANtastic, EtherType is 0x81D6';
  702.         if (mac_equals($mac, 'FF:FF:00:60:00:04')) $app = 'LANtastic, EtherType is 0x81D6';
  703.         if (mac_equals($mac, 'FF:FF:01:E0:00:04')) $app = 'LANtastic';
  704.  
  705.         // === FAQ "The "CF" series MAC addresses" ===
  706.         // https://www.iana.org/assignments/ppp-numbers/ppp-numbers.xhtml
  707.         // https://tools.ietf.org/html/rfc2153
  708.         // https://tools.ietf.org/html/rfc7042#section-2.3.2
  709.         if (mac_between($mac, 'CF:00:00:00:00:00', 'CF:00:00:FF:FF:FF')) $app = 'Reserved';
  710.         if (mac_equals($mac, 'CF:00:00:00:00:00')) $app = 'Used for Ethernet loopback tests';
  711.  
  712.         // === FAQ "How to recognise a Broadcast MAC address application?" ===
  713.         // According to https://standards.ieee.org/wp-content/uploads/import/documents/tutorials/eui.pdf FFFFFFFFFFFF can be used as NULL EUI
  714.         if (mac_equals($mac, 'FF:FF:FF:FF:FF:FF')) echo sprintf("%-32s %s\n", "Special use:", "Broadcast messaging or Null-EUI");
  715.  
  716.         // === FAQ "How to recognise a Virtual Router ID by MAC address?" ===
  717.         // https://tools.ietf.org/html/rfc7042#section-5.1
  718.         // https://tools.ietf.org/html/rfc5798
  719.         if (mac_between($mac, '00:00:5E:00:01:00', '00:00:5E:00:01:FF')) $app = 'IPv4 Virtual Router Redundancy Protocol  (VRRP)';
  720.         if (mac_between($mac, '00:00:5E:00:02:00', '00:00:5E:00:02:FF')) $app = 'IPv6 Virtual Router Redundancy Protocol';
  721.  
  722.         // === FAQ "How to recognise an IP frame by MAC address?" ===
  723.         // https://tools.ietf.org/html/rfc1060
  724.         // https://en.wikipedia.org/wiki/Multicast_address#cite_note-15
  725.         // https://tools.ietf.org/html/rfc2464
  726.         // https://www.iana.org/go/rfc1112
  727.         // http://www.cavebear.com/archive/cavebear/Ethernet/Ethernet.txt
  728.         if (mac_between($mac, '01:00:5E:00:00:00', '01:00:5E:7F:FF:FF')) $app = 'IPv4 Multicast (EtherType is 0x0800)';
  729.         if (mac_between($mac, '33:33:00:00:00:00', '33:33:FF:FF:FF:FF')) $app = 'IPv6 Multicast. IPv6 neighbor discovery (EtherType is 0x86DD)'; // TODO: Dabei werden die untersten 32 Bit der IPv6-Multicast-Adresse in die MAC-Adresse eingebettet.
  730.         if (mac_between($mac, '00:00:5E:00:52:13', '00:00:5E:00:52:13')) $app = 'Proxy Mobile IPv6';
  731.         if (mac_between($mac, '00:00:5E:FE:C0:00:02:00', '00:00:5E:FE:C0:00:02:FF')) $app = 'IPv4 derived documentation';
  732.         if (mac_between($mac, '00:00:5E:FE:C6:33:64:00', '00:00:5E:FE:C6:33:64:FF')) $app = 'IPv4 derived documentation';
  733.         if (mac_between($mac, '00:00:5E:FE:CB:00:71:00', '00:00:5E:FE:CB:00:71:FF')) $app = 'IPv4 derived documentation';
  734.         if (mac_equals($mac, '00:00:5E:FE:EA:C0:00:02')) $app = 'IPv4 multicast derived documentation';
  735.         if (mac_equals($mac, '00:00:5E:FE:EA:C6:33:64')) $app = 'IPv4 multicast derived documentation';
  736.         if (mac_equals($mac, '00:00:5E:FE:EA:CB:00:71')) $app = 'IPv4 multicast derived documentation';
  737.         if (mac_between($mac, '01:00:5E:FE:C0:00:02:00', '01:00:5E:FE:C0:00:02:FF')) $app = 'IPv4 derived documentation';
  738.         if (mac_between($mac, '01:00:5E:FE:C6:33:64:00', '01:00:5E:FE:C6:33:64:FF')) $app = 'IPv4 derived documentation';
  739.         if (mac_between($mac, '01:00:5E:FE:CB:00:71:00', '01:00:5E:FE:CB:00:71:FF')) $app = 'IPv4 derived documentation';
  740.         if (mac_equals($mac, '01:00:5E:FE:EA:C0:00:02')) $app = 'IPv4 multicast derived documentation';
  741.         if (mac_equals($mac, '01:00:5E:FE:EA:C6:33:64')) $app = 'IPv4 multicast derived documentation';
  742.         if (mac_equals($mac, '01:00:5E:FE:EA:CB:00:71')) $app = 'IPv4 multicast derived documentation';
  743.         if (mac_between($mac, '01:80:C2:00:00:20', '01:80:C2:00:00:2F')) $app = 'Reserved for use by Multiple Registration Protocol (MRP) applications';
  744.         if (mac_between($mac, '02:00:5E:FE:00:00:00:00', '02:00:5E:FE:FF:FF:FF:FF')) $app = 'IPv4 Addr Holders';
  745.         if (mac_equals($mac, '03:00:00:20:00:00')) $app = 'IP multicast address';
  746.         if (mac_equals($mac, 'C0:00:00:04:00:00')) $app = 'IP multicast address';
  747.         if (mac_between($mac, '03:00:5E:FE:00:00:00:00', '03:00:5E:FE:FF:FF:FF:FF')) $app = 'IPv4 Addr Holders';
  748.  
  749.         // === FAQ "How to recognise a MPLS multicast frame by MAC address?" ===
  750.         // http://www.iana.org/go/rfc5332
  751.         // http://www.iana.org/go/rfc7213
  752.         if (mac_between($mac, '01:00:5E:80:00:00', '01:00:5E:8F:FF:FF')) $app = 'MPLS multicast (EtherType is 0x8847 or 0x8848)';
  753.         if (mac_equals($mac, '01:00:5E:90:00:00')) $app = 'MPLS-TP p2p';
  754.  
  755.         // === FAQ "How to recognise a Bidirectional Forwarding Detection (BFD) on Link Aggregation Group (LAG) interfaces by MAC address?" ===
  756.         // http://www.iana.org/go/rfc7130
  757.         if (mac_equals($mac, '01:00:5E:90:00:01')) $app = 'Bidirectional Forwarding Detection (BFD) on Link Aggregation Group (LAG) interfaces';
  758.  
  759.         // === FAQ "How to recognise Token Ring specific functions by MAC address?" ===
  760.         // https://tools.ietf.org/html/rfc1060
  761.         // https://tools.ietf.org/html/rfc1469
  762.         // https://standards.ieee.org/products-services/regauth/grpmac/public.html
  763.         // https://tools.ietf.org/html/rfc2470
  764.         // http://www.cavebear.com/archive/cavebear/Ethernet/Ethernet.txt
  765.         if (mac_equals($mac, '03:00:00:00:00:01')) $app = 'NetBIOS (Token Ring)';
  766.         if (mac_equals($mac, '03:00:00:00:00:02')) $app = 'Locate - Directory Server (Token Ring)';
  767.         if (mac_equals($mac, '03:00:00:00:00:04')) $app = 'Synchronous Bandwidth Manager (Token Ring)';
  768.         if (mac_equals($mac, '03:00:00:00:00:08')) $app = 'Configuration Report Server (Token Ring)';
  769.         if (mac_equals($mac, '03:00:00:00:00:10')) $app = 'Ring Error Monitor (Token Ring)';
  770.         if (mac_equals($mac, '03:00:00:00:00:20')) $app = 'Network Server Heartbeat (Token Ring)';
  771.         if (mac_equals($mac, '03:00:00:00:00:40')) $app = 'Ring Parameter Monitor (Token Ring)';
  772.         if (mac_equals($mac, '03:00:00:00:00:80')) $app = 'Active Monitor (Token Ring)';
  773.         if (mac_equals($mac, '03:00:00:00:04:00')) $app = 'LAN Manager (Token Ring)';
  774.         if (mac_equals($mac, '03:00:00:00:08:00')) $app = 'Ring Wiring Concentrator (Token Ring)';
  775.         if (mac_equals($mac, '03:00:00:00:10:00')) $app = 'LAN Gateway (Token Ring)';
  776.         if (mac_equals($mac, '03:00:00:00:20:00')) $app = 'Ring Authorization Server (Token Ring)';
  777.         if (mac_equals($mac, '03:00:00:00:40:00')) $app = 'IMPL Server (Token Ring)';
  778.         if (mac_equals($mac, '03:00:00:00:80:00')) $app = 'Bridge (Token Ring)';
  779.         if (mac_equals($mac, '03:00:00:20:00:00')) $app = 'Single Token-Ring functional address';
  780.         if (mac_equals($mac, '03:00:00:00:00:08')) $app = 'Configuration Report Server (CRS) MAC Group address';
  781.         if (mac_equals($mac, '03:00:00:00:00:10')) $app = 'Ring Error Monitor (REM) MAC Group address';
  782.         if (mac_equals($mac, '03:00:00:00:00:40')) $app = 'Ring Parameter Server (RPS) MAC group address';
  783.         if (mac_equals($mac, '03:00:00:00:01:00')) $app = 'All Intermediate System Network Entities address';
  784.         if (mac_equals($mac, '03:00:00:00:02:00')) $app = 'All End System Network Entities address, and Lobe Media Test (LMT) MAC group address';
  785.         if (mac_equals($mac, '03:00:00:00:04:00')) $app = 'Generic address for all Manager Stations';
  786.         if (mac_equals($mac, '03:00:00:00:08:00')) $app = 'All CONs SNARES address';
  787.         if (mac_equals($mac, '03:00:00:00:10:00')) $app = 'All CONs End System address';
  788.         if (mac_equals($mac, '03:00:00:00:20:00')) $app = 'Loadable Device Generic address';
  789.         if (mac_equals($mac, '03:00:00:00:40:00')) $app = 'Load Server Generic address';
  790.         if (mac_equals($mac, '03:00:00:40:00:00')) $app = 'Generic address for all Agent Stations';
  791.         if (mac_equals($mac, 'C0:00:00:04:00:00')) $app = 'Single Token-Ring functional address';
  792.         if (mac_equals($mac, '03:00:80:00:00:00')) $app = 'IPv6 multicast over Token Ring: all-Nodes (FF01::1 and FF02::1) and solicited node (FF02:0:0:0:0:1:FFXX:XXXX) addresses';
  793.         if (mac_equals($mac, '03:00:40:00:00:00')) $app = 'IPv6 multicast over Token Ring: all-Routers addresses (FF0X::2)';
  794.         if (mac_equals($mac, '03:00:00:80:00:00')) $app = 'IPv6 multicast over Token Ring: any other multicast address with three least significant bits = 000';
  795.         if (mac_equals($mac, '03:00:00:40:00:00')) $app = 'IPv6 multicast over Token Ring: any other multicast address with three least significant bits = 001';
  796.         if (mac_equals($mac, '03:00:00:20:00:00')) $app = 'IPv6 multicast over Token Ring: any other multicast address with three least significant bits = 010';
  797.         if (mac_equals($mac, '03:00:00:10:00:00')) $app = 'IPv6 multicast over Token Ring: any other multicast address with three least significant bits = 011';
  798.         if (mac_equals($mac, '03:00:00:08:00:00')) $app = 'IPv6 multicast over Token Ring: any other multicast address with three least significant bits = 100';
  799.         if (mac_equals($mac, '03:00:00:04:00:00')) $app = 'IPv6 multicast over Token Ring: any other multicast address with three least significant bits = 101';
  800.         if (mac_equals($mac, '03:00:00:02:00:00')) $app = 'IPv6 multicast over Token Ring: any other multicast address with three least significant bits = 110';
  801.         if (mac_equals($mac, '03:00:00:01:00:00')) $app = 'IPv6 multicast over Token Ring: any other multicast address with three least significant bits = 111';
  802.  
  803.         // === FAQ "How to recognise an AppleTalk protocols by MAC address?" ===
  804.         // https://tools.ietf.org/html/rfc1060
  805.         // http://www.cavebear.com/archive/cavebear/Ethernet/Ethernet.txt
  806.         if (mac_between($mac, '09:00:07:00:00:00', '09:00:07:00:00:FC')) $app = 'AppleTalk zone multicast addresses (EtherType is 0x0802)';
  807.         if (mac_equals($mac, '09:00:07:FF:FF:FF')) $app = 'AppleTalk broadcast address (EtherType is 0x0802)';
  808.  
  809.         // === FAQ "How to recognise a TRILL protocols by MAC address?" ===
  810.         // http://www.iana.org/go/rfc7455
  811.         // https://tools.ietf.org/html/draft-ietf-trill-oam-framework-04
  812.         // https://standards.ieee.org/products-services/regauth/grpmac/public.html
  813.         // https://tools.ietf.org/html/rfc7455#appendix-C
  814.         if (mac_between($mac, '00:00:5E:90:01:00', '00:00:5E:90:01:00')) $app = 'TRILL OAM';
  815.         if (mac_equals($mac, '01:00:5E:90:01:00')) $app = 'TRILL OAM';
  816.         if (mac_between($mac, '01:80:C2:00:00:40', '01:80:C2:00:00:4F')) $app = 'Group MAC addresses used by the TRILL protocols';
  817.  
  818.         // === FAQ "How to recognise an IEEE 802.1X MAC address application?" ===
  819.         if (mac_between($mac, '01:0C:CD:01:00:00', '01:0C:CD:01:01:FF')) $app = 'IEC 61850-8-1 GOOSE Type 1/1A, EtherType is 0x88B8';
  820.         if (mac_between($mac, '01:0C:CD:02:00:00', '01:0C:CD:02:01:FF')) $app = 'GSSE (IEC 61850 8-1), EtherType is 0x88B9';
  821.         if (mac_between($mac, '01:0C:CD:04:00:00', '01:0C:CD:04:01:FF')) $app = 'Multicast sampled values (IEC 61850 8-1), EtherType is 0x88BA';
  822.         if (mac_equals($mac, '01:1B:19:00:00:00')) $app = 'General group address - An 802.1Q VLAN Bridge would forward the frame unchanged.';
  823.         if (mac_equals($mac, '01:1B:19:00:00:00')) $app = 'Precision Time Protocol (PTP) version 2 over Ethernet, EtherType is 0x88F7';
  824.         if (mac_equals($mac, '01:80:C2:00:00:00')) $app = 'Bridge Group address Nearest Customer Bridge group address';
  825.         if (mac_equals($mac, '01:80:C2:00:00:00')) $app = 'Spanning Tree Protocol (for bridges) IEEE 802.1D, EtherType is 0x0802';
  826.         if (mac_equals($mac, '01:80:C2:00:00:00')) $app = 'Link Layer Discovery Protocol, EtherType is 0x88CC';
  827.         if (mac_between($mac, '01:80:C2:00:00:00', '01:80:C2:00:00:0F')) $app = 'The initial bridging/link protocols block';
  828.         if (mac_between($mac, '01:80:C2:00:00:00', '01:80:C2:00:00:0F')) $app = 'IEEE 802.1D MAC Bridge Filtered MAC Group Addresses';
  829.         if (mac_between($mac, '01:80:C2:00:00:00', '01:80:C2:00:00:0F')) $app = 'IEEE Pause, 802.3x';
  830.         if (mac_equals($mac, '01:80:C2:00:00:0A')) $app = 'Reserved for future standardization';
  831.         if (mac_equals($mac, '01:80:C2:00:00:0B')) $app = 'EDE-SS PEP Address';
  832.         if (mac_equals($mac, '01:80:C2:00:00:0C')) $app = 'Reserved for future standardization';
  833.         if (mac_equals($mac, '01:80:C2:00:00:0D')) $app = 'Provider Bridge MVRP address';
  834.         if (mac_equals($mac, '01:80:C2:00:00:0E')) $app = 'Individual LAN Scope group address, It is intended that no IEEE 802.1 relay device will be defined that will forward frames that carry this destination address';
  835.         if (mac_equals($mac, '01:80:C2:00:00:0E')) $app = 'Nearest Bridge group address';
  836.         if (mac_equals($mac, '01:80:C2:00:00:0E')) $app = 'Link Layer Discovery Protocol, EtherType is 0x88CC';
  837.         if (mac_equals($mac, '01:80:C2:00:00:0E')) $app = 'Precision Time Protocol (PTP) version 2 over Ethernet, EtherType is 0x88F7';
  838.         if (mac_equals($mac, '01:80:C2:00:00:01')) $app = 'IEEE MAC-specific Control Protocols group address';
  839.         if (mac_equals($mac, '01:80:C2:00:00:01')) $app = 'Ethernet flow control (Pause frame) IEEE 802.3x, EtherType is 0x8808';
  840.         if (mac_equals($mac, '01:80:C2:00:00:1A')) $app = 'Generic Address for All Agent Stations';
  841.         if (mac_equals($mac, '01:80:C2:00:00:1B')) $app = 'All Multicast Capable End Systems address';
  842.         if (mac_equals($mac, '01:80:C2:00:00:1C')) $app = 'All Multicast Announcements address';
  843.         if (mac_equals($mac, '01:80:C2:00:00:1D')) $app = 'All Multicast Capable Intermediate Systems address';
  844.         if (mac_equals($mac, '01:80:C2:00:00:1E')) $app = 'All DTR Concentrators MAC group address';
  845.         if (mac_equals($mac, '01:80:C2:00:00:1F')) $app = 'EDE-CC PEP Address';
  846.         if (mac_between($mac, '01:80:C2:00:00:01', '01:80:C2:00:00:0F')) $app = '802.1 alternate Spanning multicast, EtherType is 0x0802';
  847.         if (mac_equals($mac, '01:80:C2:00:00:02')) $app = 'Ethernet OAM Protocol IEEE 802.3ah (also known as "slow protocols"), EtherType is 0x8809';
  848.         if (mac_equals($mac, '01:80:C2:00:00:03')) $app = 'Nearest non-TPMR Bridge group address IEEE Std 802.1X PAE address';
  849.         if (mac_equals($mac, '01:80:C2:00:00:03')) $app = 'Link Layer Discovery Protocol, EtherType is 0x88CC';
  850.         if (mac_equals($mac, '01:80:C2:00:00:04')) $app = 'IEEE MAC-specific Control Protocols group address';
  851.         if (mac_equals($mac, '01:80:C2:00:00:05')) $app = 'Reserved for future standardization';
  852.         if (mac_equals($mac, '01:80:C2:00:00:06')) $app = 'Reserved for future standardization';
  853.         if (mac_equals($mac, '01:80:C2:00:00:07')) $app = 'MEF Forum ELMI protocol group address';
  854.         if (mac_equals($mac, '01:80:C2:00:00:08')) $app = 'Provider Bridge group address';
  855.         if (mac_equals($mac, '01:80:C2:00:00:08')) $app = 'Spanning Tree Protocol (for provider bridges) IEEE 802.1ad, EtherType is 0x0802';
  856.         if (mac_equals($mac, '01:80:C2:00:00:09')) $app = 'Reserved for future standardization';
  857.         if (mac_equals($mac, '01:80:C2:00:00:10')) $app = 'All LANs Bridge Management group address (deprecated)';
  858.         if (mac_equals($mac, '01:80:C2:00:00:10')) $app = 'Bridge Management, EtherType is 0x0802';
  859.         if (mac_equals($mac, '01:80:C2:00:00:11')) $app = 'Load Server generic address';
  860.         if (mac_equals($mac, '01:80:C2:00:00:11')) $app = 'Load Server, EtherType is 0x0802';
  861.         if (mac_equals($mac, '01:80:C2:00:00:12')) $app = 'Loadable Device generic address';
  862.         if (mac_equals($mac, '01:80:C2:00:00:12')) $app = 'Loadable Device, EtherType is 0x0802';
  863.         if (mac_equals($mac, '01:80:C2:00:00:13')) $app = 'Transmission of IEEE 1905.1 control packets';
  864.         if (mac_equals($mac, '01:80:C2:00:00:14')) $app = 'All Level 1 Intermediate Systems address';
  865.         if (mac_equals($mac, '01:80:C2:00:00:14')) $app = 'OSI Route level 1 (within area), EtherType is 0x0802';
  866.         if (mac_equals($mac, '01:80:C2:00:00:15')) $app = 'All Level 2 Intermediate Systems address';
  867.         if (mac_equals($mac, '01:80:C2:00:00:15')) $app = 'OSI Route level 2 (between area), EtherType is 0x0802';
  868.         if (mac_equals($mac, '01:80:C2:00:00:16')) $app = 'All CONS End Systems address';
  869.         if (mac_equals($mac, '01:80:C2:00:00:17')) $app = 'All CONS SNARES address';
  870.         if (mac_equals($mac, '01:80:C2:00:00:18')) $app = 'Generic address for All Manager Stations';
  871.         if (mac_equals($mac, '01:80:C2:00:00:19')) $app = 'Groupcast with retries (GCR) MAC group address';
  872.         if (mac_between($mac, '01:80:C2:00:00:20', '01:80:C2:00:00:2F')) $app = 'Reserved for use by Multiple Registration Protocol (MRP) applications';
  873.         if (mac_equals($mac, '01:80:C2:00:00:21')) $app = 'GARP VLAN Registration Protocol (also known as IEEE 802.1q GVRP), EtherType is 0x88f5';
  874.         if (mac_between($mac, '01:80:C2:00:00:30', '01:80:C2:00:00:3F')) $app = 'Destination group MAC addresses for CCM and Linktrace messages';
  875.         if (mac_between($mac, '01:80:C2:00:00:30', '01:80:C2:00:00:3F')) $app = 'Ethernet CFM Protocol IEEE 802.1ag, EtherType is 0x8902';
  876.         if (mac_between($mac, '01:80:C2:00:00:50', '01:80:C2:00:00:FF')) $app = 'Unassigned standard group MAC address';
  877.         if (mac_equals($mac, '01:80:C2:00:01:00')) $app = 'Ring Management Directed Beacon multicast address';
  878.         if (mac_equals($mac, '01:80:C2:00:01:00')) $app = 'FDDI RMT Directed Beacon, EtherType is 0x0802';
  879.         if (mac_between($mac, '01:80:C2:00:01:01', '01:80:C2:00:01:0F')) $app = 'Assigned to ISO/IEC JTC1/SC25 for future use';
  880.         if (mac_equals($mac, '01:80:C2:00:01:10')) $app = 'Status Report Frame Status Report Protocol multicast address';
  881.         if (mac_equals($mac, '01:80:C2:00:01:10')) $app = 'FDDI status report frame, EtherType is 0x0802';
  882.         if (mac_between($mac, '01:80:C2:00:01:11', '01:80:C2:00:01:1F')) $app = 'Assigned to ISO/IEC JTC1/SC25 for future use';
  883.         if (mac_equals($mac, '01:80:C2:00:01:20')) $app = 'All FDDI Concentrator MACs';
  884.         if (mac_between($mac, '01:80:C2:00:01:21', '01:80:C2:00:01:2F')) $app = 'Assigned to ISO/IEC JTC1/SC25 for future use';
  885.         if (mac_equals($mac, '01:80:C2:00:01:30')) $app = 'Synchronous Bandwidth Allocation address';
  886.         if (mac_between($mac, '01:80:C2:00:01:31', '01:80:C2:00:01:FF')) $app = 'Assigned to ISO/IEC JTC1/SC25 for future use';
  887.         if (mac_between($mac, '01:80:C2:00:02:00', '01:80:C2:00:02:FF')) $app = 'Assigned to ETSI for future use';
  888.         if (mac_between($mac, '01:80:C2:00:03:00', '01:80:C2:FF-FF-FF')) $app = 'Unassigned standard group MAC address';
  889.         if (mac_equals($mac, '09:00:4C:00:00:00')) $app = 'BICC 802.1 management, EtherType is 0x0802';
  890.         if (mac_equals($mac, '09:00:4C:00:00:0C')) $app = 'BICC Remote bridge STA 802.1(D) Rev8, EtherType is 0x0802';
  891.         if (mac_equals($mac, '09:00:4C:00:00:02')) $app = 'BICC 802.1 management, EtherType is 0x0802';
  892.         if (mac_equals($mac, '09:00:4C:00:00:06')) $app = 'BICC Local bridge STA 802.1(D) Rev6, EtherType is 0x0802';
  893.         if (mac_between($mac, '33:33:00:00:00:00', '33:33:FF:FF:FF:FF')) $app = 'IPv6 multicast, EtherType is 0x86DD';
  894.  
  895.         // === FAQ "How to recognise an ISO 9542 ES-IS protocol's MAC address application?" ===
  896.         // https://standards.ieee.org/products-services/regauth/grpmac/public.html
  897.         if (mac_equals($mac, '09:00:2B:00:00:04')) $app = 'All End System Network Entities address';
  898.         if (mac_equals($mac, '09:00:2B:00:00:05')) $app = 'All Intermediate System Network Entities address';
  899.  
  900.         // === FAQ "How to recognise an IANA MAC address application?" ===
  901.         // https://www.iana.org/assignments/ethernet-numbers/ethernet-numbers.xhtml
  902.         // http://www.iana.org/go/rfc7042
  903.         // https://tools.ietf.org/html/rfc1060
  904.         if (mac_between($mac, '00:00:5E:00-52:14', '00:00:5E:00:52:FF')) $app = 'Unassigned (small allocations)';
  905.         if (mac_between($mac, '00:00:5E:00:00:00', '00:00:5E:00:00:FF')) $app = 'Reserved and require IESG Ratification for assignment';
  906.         if (mac_between($mac, '00:00:5E:00:03:00', '00:00:5E:00:51:FF')) $app = 'Unassigned';
  907.         if (mac_between($mac, '00:00:5E:00:52:00', '00:00:5E:00:52:FF')) $app = 'Is used for very small assignments. Currently, 3 out of these 256 values have been assigned.';
  908.         if (mac_between($mac, '00:00:5E:00:52:00', '00:00:5E:00:52:00')) $app = 'PacketPWEthA';
  909.         if (mac_between($mac, '00:00:5E:00:52:01', '00:00:5E:00:52:01')) $app = 'PacketPWEthB';
  910.         if (mac_between($mac, '00:00:5E:00:52:02', '00:00:5E:00:52:12')) $app = 'Unassigned (small allocations)';
  911.         if (mac_between($mac, '00:00:5E:00:53:00', '00:00:5E:00:53:FF')) $app = 'Assigned for use in documentation';
  912.         if (mac_between($mac, '00:00:5E:00:54:00', '00:00:5E:90:00:FF')) $app = 'Unassigned';
  913.         if (mac_between($mac, '00:00:5E:90:01:01', '00:00:5E:90:01:FF')) $app = 'Unassigned (small allocations requiring both unicast and multicast)';
  914.         if (mac_between($mac, '00:00:5E:EF:10:00:00:00', '00:00:5E:EF:10:00:00:FF')) $app = 'General documentation';
  915.         if (mac_between($mac, '00:00:5E:FF:FE:00:53:00', '00:00:5E:FF:FE:00:53:FF')) $app = 'EUI-48 derived documentation';
  916.         if (mac_between($mac, '01:00:5E:00:00:00', '01:00:5E:7F:FF:FF')) $app = 'DoD Internet Multicast (EtherType is 0x0800)'; // TODO: IPv4-Multicast  (Dabei werden dann die unteren 23 Bit der IP-Multicast-Adresse direkt auf die untersten 23 Bit der MAC-Adresse abgebildet. Der IP-Multicast-Adresse 224.0.0.1 ist somit die Multicast-MAC-Adresse 01-00-5e-00-00-01 fest zugeordnet.)
  917.         if (mac_between($mac, '01:00:5E:80:00:00', '01:00:5E:FF:FF:FF')) $app = 'DoD Internet';
  918.         if (mac_equals($mac, '01:00:5E:90:00:02')) $app = 'AllL1MI-ISs';
  919.         if (mac_equals($mac, '01:00:5E:90:00:03')) $app = 'AllL2MI-ISs';
  920.         if (mac_between($mac, '01:00:5E:90:00:04', '01:00:5E:90:00:FF')) $app = 'Unassigned (small allocations)';
  921.         if (mac_between($mac, '01:00:5E:90:01:01', '01:00:5E:90:01:FF')) $app = 'Unassigned (small allocations requiring both unicast and multicast)';
  922.         if (mac_between($mac, '01:00:5E:90:02:00', '01:00:5E:90:0F:FF')) $app = 'Unassigned';
  923.         if (mac_between($mac, '01:00:5E:90:02:00', '00:00:5E:FF:FF:FF')) $app = 'Unassigned';
  924.         if (mac_between($mac, '01:00:5E:90:10:00', '01:00:5E:90:10:FF')) $app = 'Documentation';
  925.         if (mac_between($mac, '01:00:5E:90:11:00', '01:00:5E:FF:FF:FF')) $app = 'Unassigned';
  926.         if (mac_between($mac, '01:00:5E:EF:10:00:00:00', '01:00:5E:EF:10:00:00:FF')) $app = 'General documentation';
  927.         if (mac_between($mac, '02:00:5E:00:00:00:00:00', '02:00:5E:0F:FF:FF:FF:FF')) $app = 'Reserved';
  928.         if (mac_between($mac, '02:00:5E:10:00:00:00:00', '02:00:5E:10:00:00:00:FF')) $app = 'Documentation';
  929.         if (mac_between($mac, '02:00:5E:10:00:00:01:00', '02:00:5E:EF:FF:FF:FF:FF')) $app = 'Unassigned';
  930.         if (mac_between($mac, '02:00:5E:F0:00:00:00:00', '02:00:5E:FD:FF:FF:FF:FF')) $app = 'Reserved';
  931.         if (mac_between($mac, '02:00:5E:FE:00:00:00:00', '02:00:5E:FE:FF:FF:FF:FF')) $app = 'IPv4 Addr Holders';
  932.         if (mac_between($mac, '02:00:5E:FF:00:00:00:00', '02:00:5E:FF:FD:FF:FF:FF')) $app = 'Reserved';
  933.         if (mac_between($mac, '02:00:5E:FF:FE:00:00:00', '02:00:5E:FF:FE:FF:FF:FF')) $app = 'IANA EUI-48 Holders';
  934.         if (mac_between($mac, '02:00:5E:FF:FF:00:00:00', '02:00:5E:FF:FF:FF:FF:FF')) $app = 'Reserved';
  935.         if (mac_between($mac, '03:00:5E:00:00:00:00:00', '03:00:5E:0F:FF:FF:FF:FF')) $app = 'Reserved';
  936.         if (mac_between($mac, '03:00:5E:10:00:00:00:00', '03:00:5E:10:00:00:00:FF')) $app = 'Documentation';
  937.         if (mac_between($mac, '03:00:5E:10:00:00:01:00', '03:00:5E:EF:FF:FF:FF:FF')) $app = 'Unassigned';
  938.         if (mac_between($mac, '03:00:5E:F0:00:00:00:00', '03:00:5E:FD:FF:FF:FF:FF')) $app = 'Reserved';
  939.         if (mac_between($mac, '03:00:5E:FF:00:00:00:00', '03:00:5E:FF:FD:FF:FF:FF')) $app = 'Reserved';
  940.         if (mac_between($mac, '03:00:5E:FF:FE:00:00:00', '03:00:5E:FF:FE:FF:FF:FF')) $app = 'IANA EUI-48 Holders';
  941.         if (mac_between($mac, '03:00:5E:FF:FF:00:00:00', '03:00:5E:FF:FF:FF:FF:FF')) $app = 'Reserved';
  942.  
  943.         // === FAQ "How to recognise a Cisco's MAC address application?" ===
  944.         // https://www.cisco.com/c/en/us/support/docs/switches/catalyst-4500-series-switches/13414-103.html
  945.         // https://tools.ietf.org/html/rfc1060
  946.         // https://en.wikipedia.org/wiki/Multicast_address#cite_note-15
  947.         // http://www.cavebear.com/archive/cavebear/Ethernet/Ethernet.txt
  948.         if (mac_equals($mac, '01:00:0C:00:00:00')) $app = 'Inter Switch Link (ISL)';
  949.         if (mac_equals($mac, '01:00:0C:CC:CC:CC')) $app = 'CDP (Cisco Discovery Protocol), VTP (VLAN Trunking Protocol), EtherType is 0x0802';
  950.         if (mac_equals($mac, '01:00:0C:CC:CC:CC')) $app = 'Port Aggregation Protocol (PAgP), SNAP HDLC Protocol Type is 0x0104';
  951.         if (mac_equals($mac, '01:00:0C:CC:CC:CC')) $app = 'Unidirectional Link Detection (UDLD), SNAP HDLC Protocol Type is 0x0111';
  952.         if (mac_equals($mac, '01:00:0C:CC:CC:CC')) $app = 'Dynamic Trunking (DTP), SNAP HDLC Protocol Type is 0x2004';
  953.         if (mac_equals($mac, '01:00:0C:CC:CC:CC')) $app = 'VLAN Trunking (VTP), SNAP HDLC Protocol Type is 0x2003';
  954.         if (mac_equals($mac, '01:00:0C:CC:CC:CD')) $app = 'Cisco Shared Spanning Tree Protocol address, EtherType is 0x0802';
  955.         if (mac_equals($mac, '01:00:0C:CC:CC:CD')) $app = 'Spanning Tree PVSTP+, SNAP HDLC Protocol Type is 0x010B';
  956.         if (mac_equals($mac, '01:00:0C:CD:CD:CD')) $app = 'STP Uplink Fast, SNAP HDLC Protocol Type is 0x200A';
  957.         if (mac_equals($mac, '01:00:0C:CD:CD:CE')) $app = 'VLAN Bridge, SNAP HDLC Protocol Type is 0x010C';
  958.         if (mac_equals($mac, '01:00:0C:DD:DD:DD')) $app = 'CGMP (Cisco Group Management Protocol)';
  959.  
  960.         // === FAQ "How to recognise an ITU-T's MAC address application?" ===
  961.         // https://www.itu.int/en/ITU-T/studygroups/2017-2020/15/Documents/IEEE-assigned_OUIs-30-06-2017.docx
  962.         if (mac_between($mac, '01:19:A7:00:00:00', '01:19:A7:00:00:FF')) $app = 'R-APS per G.8032';
  963.         if (mac_between($mac, '01:19:A7:52:76:90', '01:19:A7:52:76:9F')) $app = 'Multicast per G.9961';
  964.  
  965.         // === FAQ "How to recognise Digital Equipment Corporation's MAC address application?" ===
  966.         if (mac_equals($mac, '09:00:2B:00:00:00')) $app = 'DEC MUMPS, EtherType is 0x6009';
  967.         if (mac_equals($mac, '09:00:2B:00:00:0F')) $app = 'DEC Local Area Transport (LAT), EtherType is 0x6004';
  968.         if (mac_equals($mac, '09:00:2B:00:00:01')) $app = 'DEC DSM/DDP, EtherType is 0x8039';
  969.         if (mac_between($mac, '09:00:2B:00:00:10', '09:00:2B:00:00:1F')) $app = 'DEC Experimental';
  970.         if (mac_equals($mac, '09:00:2B:00:00:02')) $app = 'DEC VAXELN, EtherType is 0x803B';
  971.         if (mac_equals($mac, '09:00:2B:00:00:03')) $app = 'DEC Lanbridge Traffic Monitor (LTM), EtherType is 0x8038';
  972.         if (mac_equals($mac, '09:00:2B:00:00:04')) $app = 'DEC MAP End System';
  973.         if (mac_equals($mac, '09:00:2B:00:00:05')) $app = 'DEC MAP Intermediate System';
  974.         if (mac_equals($mac, '09:00:2B:00:00:06')) $app = 'DEC CSMA/CD Encryption, EtherType is 0x803D';
  975.         if (mac_equals($mac, '09:00:2B:00:00:07')) $app = 'DEC NetBios Emulator, EtherType is 0x8040';
  976.         if (mac_equals($mac, '09:00:2B:01:00:00')) $app = 'DEC LanBridge, EtherType is 0x8038';
  977.         if (mac_equals($mac, '09:00:2B:01:00:01')) $app = 'DEC LanBridge, EtherType is 0x8038';
  978.         if (mac_equals($mac, '09:00:2B:02:00:00')) $app = 'DEC DNA Level 2 Routing';
  979.         if (mac_equals($mac, '09:00:2B:02:01:00')) $app = 'DEC DNA Naming Service Advertisement, EtherType is 0x803C';
  980.         if (mac_equals($mac, '09:00:2B:02:01:01')) $app = 'DEC DNA Naming Service Solicitation, EtherType is 0x803C';
  981.         if (mac_equals($mac, '09:00:2B:02:01:02')) $app = 'DEC Distributed Time Service, EtherType is 0x803E';
  982.         if (mac_equals($mac, '09:00:2B:02:01:09')) $app = 'DEC Availability Manager for Distributed Systems DECamds, EtherType is 0x8048';
  983.         if (mac_between($mac, '09:00:2B:03:00:00', '09:00:2B:03:FF:FF')) $app = 'DEC default filtering by bridges';
  984.         if (mac_equals($mac, '09:00:2B:04:00:00')) $app = 'DEC Local Area System Transport (LAST), EtherType is 0x8041';
  985.         if (mac_equals($mac, '09:00:2B:23:00:00')) $app = 'DEC Argonaut Console, EtherType is 0x803A';
  986.         if (mac_equals($mac, 'AB:00:00:01:00:00')) $app = 'DEC Maintenance Operation Protocol (MOP) Dump/Load Assistance, EtherType is 0x6001';
  987.         if (mac_equals($mac, 'AB:00:00:02:00:00')) $app = 'DEC Maintenance Operation Protocol (MOP), EtherType is 0x6002';
  988.         if (mac_equals($mac, 'AB:00:00:03:00:00')) $app = 'DECNET Phase IV end node, EtherType is 0x6003';
  989.         if (mac_equals($mac, 'AB:00:00:04:00:00')) $app = 'DECNET Phase IV Router, EtherType is 0x6003';
  990.         if (mac_between($mac, 'AB:00:00:05:00:00', 'AB:00:03:FF:FF:FF')) $app = 'Reserved DEC';
  991.         if (mac_equals($mac, 'AB:00:03:00:00:00')) $app = 'DEC Local Area Transport (LAT) - old, EtherType is 0x6004';
  992.         if (mac_between($mac, 'AB:00:04:00:00:00', 'AB:00:04:00:FF:FF')) $app = 'Reserved DEC customer private use';
  993.         if (mac_between($mac, 'AB:00:04:01:00:00', 'AB:00:04:01:FF:FF')) $app = 'DEC Local Area VAX Cluster groups System Communication Architecture (SCA), EtherType is 0x6007';
  994.  
  995.         // https://standards.ieee.org/products-programs/regauth/grpmac/public/
  996.         // TODO: Check for duplicates between these and the ones at the top
  997.         // IEEE Std 802.1D and IEEE Std 802.1Q Reserved Addresses
  998.         if (mac_equals($mac, '01-80-C2-00-00-00')) $app = 'IEEE Std 802.1Q / Bridge Group address, Nearest Customer Bridge group address';
  999.         if (mac_equals($mac, '01-80-C2-00-00-01')) $app = 'IEEE Std 802.1Q / IEEE MAC-specific Control Protocols group address';
  1000.         if (mac_equals($mac, '01-80-C2-00-00-02')) $app = 'IEEE Std 802.1Q / IEEE 802.3 Slow_Protocols_Multicast address';
  1001.         if (mac_equals($mac, '01-80-C2-00-00-03')) $app = 'IEEE Std 802.1Q / Nearest non-TPMR Bridge group address, IEEE Std 802.1X PAE address';
  1002.         if (mac_equals($mac, '01-80-C2-00-00-04')) $app = 'IEEE Std 802.1Q / IEEE MAC-specific Control Protocols group address';
  1003.         if (mac_equals($mac, '01-80-C2-00-00-05')) $app = 'IEEE Std 802.1Q / Reserved for future standardization';
  1004.         if (mac_equals($mac, '01-80-C2-00-00-06')) $app = 'IEEE Std 802.1Q / Reserved for future standardization';
  1005.         if (mac_equals($mac, '01-80-C2-00-00-07')) $app = 'IEEE Std 802.1Q / MEF Forum ELMI protocol group address';
  1006.         if (mac_equals($mac, '01-80-C2-00-00-08')) $app = 'IEEE Std 802.1Q / Provider Bridge group address';
  1007.         if (mac_equals($mac, '01-80-C2-00-00-09')) $app = 'IEEE Std 802.1Q / Reserved for future standardization';
  1008.         if (mac_equals($mac, '01-80-C2-00-00-0A')) $app = 'IEEE Std 802.1Q / Reserved for future standardization';
  1009.         if (mac_equals($mac, '01-80-C2-00-00-0B')) $app = 'IEEE Std 802.1Q / EDE-SS PEP Address';
  1010.         if (mac_equals($mac, '01-80-C2-00-00-0C')) $app = 'IEEE Std 802.1Q / Reserved for future standardization';
  1011.         if (mac_equals($mac, '01-80-C2-00-00-0D')) $app = 'IEEE Std 802.1Q / Provider Bridge MVRP address';
  1012.         if (mac_equals($mac, '01-80-C2-00-00-0E')) $app = 'IEEE Std 802.1Q / Individual LAN Scope group address, Nearest Bridge group address';
  1013.         if (mac_equals($mac, '01-80-C2-00-00-0F')) $app = 'IEEE Std 802.1Q / Reserved for future standardization';
  1014.         // Standard Group MAC Addresses
  1015.         if (mac_equals($mac, '01-80-C2-00-00-10')) $app = 'All LANs Bridge Management Group Address (deprecated)';
  1016.         if (mac_equals($mac, '01-80-C2-00-00-11')) $app = 'Load Server Generic Address';
  1017.         if (mac_equals($mac, '01-80-C2-00-00-12')) $app = 'Loadable Device Generic Address';
  1018.         if (mac_equals($mac, '01-80-C2-00-00-13')) $app = 'Transmission of IEEE 1905.1 control packets';
  1019.         if (mac_equals($mac, '01-80-C2-00-00-14')) $app = 'All Level 1 Intermediate Systems Address';
  1020.         if (mac_equals($mac, '01-80-C2-00-00-15')) $app = 'All Level 2 Intermediate Systems Address';
  1021.         if (mac_equals($mac, '01-80-C2-00-00-16')) $app = 'All CONS End Systems Address';
  1022.         if (mac_equals($mac, '01-80-C2-00-00-17')) $app = 'All CONS SNARES Address';
  1023.         if (mac_equals($mac, '01-80-C2-00-00-18')) $app = 'Generic Address for All Manager Stations';
  1024.         if (mac_equals($mac, '01-80-C2-00-00-19')) $app = 'Groupcast with retries (GCR) MAC Group Address';
  1025.         if (mac_equals($mac, '01-80-C2-00-00-1A')) $app = 'Generic Address for All Agent Stations';
  1026.         if (mac_equals($mac, '01-80-C2-00-00-1B')) $app = 'All Multicast Capable End Systems Address';
  1027.         if (mac_equals($mac, '01-80-C2-00-00-1C')) $app = 'All Multicast Announcements Address';
  1028.         if (mac_equals($mac, '01-80-C2-00-00-1D')) $app = 'All Multicast Capable Intermediate Systems Address';
  1029.         if (mac_equals($mac, '01-80-C2-00-00-1E')) $app = 'All DTR Concentrators MAC Group Address';
  1030.         if (mac_equals($mac, '01-80-C2-00-00-1F')) $app = 'EDE-CC PEP Address';
  1031.         if (mac_between($mac, '01-80-C2-00-00-20','01-80-C2-00-00-2F')) $app = 'Reserved for use by Multiple Registration Protocol (MRP) applications';
  1032.         if (mac_between($mac, '01-80-C2-00-00-30','01-80-C2-00-00-3F')) $app = 'Destination group MAC addresses for CCM and Linktrace messages';
  1033.         if (mac_between($mac, '01-80-C2-00-00-40','01-80-C2-00-00-4F')) $app = 'Group MAC addresses used by the TRILL protocols';
  1034.         if (mac_between($mac, '01-80-C2-00-00-50','01-80-C2-00-00-FF')) $app = 'unassigned';
  1035.         if (mac_equals($mac, '01-80-C2-00-01-00')) $app = 'Ring Management Directed Beacon Multicast Address';
  1036.         if (mac_between($mac, '01-80-C2-00-01-01','01-80-C2-00-01-0F')) $app = 'Assigned to ISO/IEC JTC1/SC25 for future use';
  1037.         if (mac_equals($mac, '01-80-C2-00-01-10')) $app = 'Status Report Frame Status Report Protocol Multicast Address';
  1038.         if (mac_between($mac, '01-80-C2-00-01-11','01-80-C2-00-01-1F')) $app = 'Assigned to ISO/IEC JTC1/SC25 for future use';
  1039.         if (mac_equals($mac, '01-80-C2-00-01-20')) $app = 'ISO/IEC 9314-2 All FDDI Concentrator MACs';
  1040.         if (mac_between($mac, '01-80-C2-00-01-21','01-80-C2-00-01-2F')) $app = 'Assigned to ISO/IEC JTC1/SC25 for future use';
  1041.         if (mac_equals($mac, '01-80-C2-00-01-30')) $app = 'ISO/IEC 9314-6 Synchronous Bandwidth Allocation Address';
  1042.         if (mac_between($mac, '01-80-C2-00-01-31','01-80-C2-00-01-FF')) $app = 'Assigned to ISO/IEC JTC1/SC25 for future use';
  1043.         if (mac_between($mac, '01-80-C2-00-02-00','01-80-C2-00-02-FF')) $app = 'Assigned to ETSI for future use';
  1044.         if (mac_between($mac, '01-80-C2-00-03-00', '01-80-C2-FF-FF-FF')) $app = 'unassigned';
  1045.         if (mac_equals($mac, '09-00-2B-00-00-04')) $app = 'ISO 9542 All End System Network Entities Address';
  1046.         if (mac_equals($mac, '09-00-2B-00-00-05')) $app = 'ISO 9542 All Intermediate System Network Entities Address';
  1047.         // Group MAC Addresses Used in ISO 9542 ES-IS Protocol
  1048.         if (mac_equals($mac, '09-00-2B-00-00-04')) $app = 'ISO 9542 All End System Network Entities Address';
  1049.         if (mac_equals($mac, '09-00-2B-00-00-05')) $app = 'ISO 9542 All Intermediate System Network Entities Address';
  1050.         // Locally Administered Group MAC Addresses Used by IEEE Std 802.5 (IEEE Std 802.5 Functional Addresses)
  1051.         if (mac_equals($mac, '03-00-00-00-00-08')) $app = 'Configuration Report Server (CRS) MAC Group Address';
  1052.         if (mac_equals($mac, '03-00-00-00-00-10')) $app = 'Ring Error Monitor (REM) MAC Group Address';
  1053.         if (mac_equals($mac, '03-00-00-00-00-40')) $app = 'Ring Parameter Server (RPS) MAC Group Address';
  1054.         if (mac_equals($mac, '03-00-00-00-01-00')) $app = 'All Intermediate System Network Entities Address';
  1055.         if (mac_equals($mac, '03-00-00-00-02-00')) $app = 'All End System Network Entities Address, and Lobe Media Test (LMT) MAC Group Address';
  1056.         if (mac_equals($mac, '03-00-00-00-04-00')) $app = 'Generic Address for all Manager Stations';
  1057.         if (mac_equals($mac, '03-00-00-00-08-00')) $app = 'All CONs SNARES Address';
  1058.         if (mac_equals($mac, '03-00-00-00-10-00')) $app = 'All CONs End System Address';
  1059.         if (mac_equals($mac, '03-00-00-00-20-00')) $app = 'Loadable Device Generic Address';
  1060.         if (mac_equals($mac, '03-00-00-00-40-00')) $app = 'Load Server Generic Address';
  1061.         if (mac_equals($mac, '03-00-00-40-00-00')) $app = 'Generic Address for all Agent Stations';
  1062.  
  1063.         if ($app) {
  1064.                 echo sprintf("%-32s %s\n", "Special use:", $app);
  1065.         }
  1066.  
  1067. }
  1068.  
  1069. /**
  1070.  * @param string $mac1
  1071.  * @param string $mac2
  1072.  * @return bool
  1073.  */
  1074. function mac_equals(string $mac1, string $mac2): bool {
  1075.         $mac1test = eui64_to_eui48($mac1);
  1076.         if ($mac1test === false) return false;
  1077.         $mac2test = eui64_to_eui48($mac2);
  1078.         if ($mac2test === false) return false;
  1079.  
  1080.         if (eui_bits($mac1test) != eui_bits($mac2test)) {
  1081.                 $mac1test = eui48_to_eui64($mac1);
  1082.                 $mac2test = eui48_to_eui64($mac2);
  1083.         }
  1084.  
  1085.         return mac_canonize($mac1test) == mac_canonize($mac2test);
  1086. }
  1087.  
  1088. /**
  1089.  * @param string $mac
  1090.  * @param string $low
  1091.  * @param string $high
  1092.  * @return bool
  1093.  */
  1094. function mac_between(string $mac, string $low, string $high): bool {
  1095.         $mactest = eui64_to_eui48($mac);
  1096.         if ($mactest === false) return false;
  1097.         $lowtest = eui64_to_eui48($low);
  1098.         if ($lowtest === false) return false;
  1099.         $hightest = eui64_to_eui48($high);
  1100.         if ($hightest === false) return false;
  1101.  
  1102.         if ((eui_bits($mactest) != eui_bits($lowtest)) || (eui_bits($lowtest) != eui_bits($hightest))) {
  1103.                 $mactest = eui48_to_eui64($mac);
  1104.                 if ($mactest === false) return false; // e.g. trying ELI-48 to ELI-64
  1105.                 $lowtest = eui48_to_eui64($low);
  1106.                 if ($lowtest === false) return false; // e.g. trying ELI-48 to ELI-64
  1107.                 $hightest = eui48_to_eui64($high);
  1108.                 if ($hightest === false) return false; // e.g. trying ELI-48 to ELI-64
  1109.         }
  1110.  
  1111.         $mactest = strtoupper(preg_replace('@[^0-9A-F]@', '', $mactest));
  1112.         $lowtest = strtoupper(preg_replace('@[^0-9A-F]@', '', $lowtest));
  1113.         $hightest = strtoupper(preg_replace('@[^0-9A-F]@', '', $hightest));
  1114.  
  1115.         $mactest = gmp_init($mactest, 16);
  1116.         $lowtest = gmp_init($lowtest, 16);
  1117.         $hightest = gmp_init($hightest, 16);
  1118.  
  1119.         return (gmp_cmp($mactest, $lowtest) >= 0) && (gmp_cmp($mactest, $hightest) <= 0);
  1120. }
  1121.  
  1122. /**
  1123.  * Gets the current MAC address of the system
  1124.  * @return string|false MAC address of the local system
  1125.  */
  1126. function get_mac_address() {
  1127.         static $detected_mac = false;
  1128.  
  1129.         if ($detected_mac !== false) { // false NOT null!
  1130.                 return $detected_mac;
  1131.         }
  1132.  
  1133.         if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
  1134.                 // Windows
  1135.                 $cmds = array(
  1136.                         "ipconfig /all", // faster
  1137.                         "getmac"
  1138.                 );
  1139.                 foreach ($cmds as $cmd) {
  1140.                         $out = array();
  1141.                         $ec = -1;
  1142.                         exec($cmd, $out, $ec);
  1143.                         if ($ec == 0) {
  1144.                                 $out = implode("\n",$out);
  1145.                                 $m = array();
  1146.                                 if (preg_match("/([0-9a-f]{2}-[0-9a-f]{2}-[0-9a-f]{2}-[0-9a-f]{2}-[0-9a-f]{2}-[0-9a-f]{2})/ismU", $out, $m)) {
  1147.                                         $detected_mac = strtolower($m[1]);
  1148.                                         return $detected_mac;
  1149.                                 }
  1150.                         }
  1151.                 }
  1152.         } else if (strtoupper(PHP_OS) == 'DARWIN') {
  1153.                 // Mac OS X
  1154.                 $cmds = array(
  1155.                         "networksetup -listallhardwareports 2>/dev/null",
  1156.                         "netstat -i 2>/dev/null"
  1157.                 );
  1158.                 foreach ($cmds as $cmd) {
  1159.                         $out = array();
  1160.                         $ec = -1;
  1161.                         exec($cmd, $out, $ec);
  1162.                         if ($ec == 0) {
  1163.                                 $out = implode("\n",$out);
  1164.                                 $m = array();
  1165.                                 if (preg_match("/([0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2})/ismU", $out, $m)) {
  1166.                                         $detected_mac = $m[1];
  1167.                                         return $detected_mac;
  1168.                                 }
  1169.                         }
  1170.                 }
  1171.         } else {
  1172.                 // Linux
  1173.                 $addresses = @glob('/sys/class/net/'.'*'.'/address');
  1174.                 foreach ($addresses as $x) {
  1175.                         if (!strstr($x,'/lo/')) {
  1176.                                 $detected_mac = trim(file_get_contents($x));
  1177.                                 if (substr(mac_type($detected_mac),0,6) == 'EUI-48') {
  1178.                                         return $detected_mac;
  1179.                                 }
  1180.                         }
  1181.                 }
  1182.                 $cmds = array(
  1183.                         "netstat -ie 2>/dev/null",
  1184.                         "ifconfig 2>/dev/null" // only available for root (because it is in sbin)
  1185.                 );
  1186.                 foreach ($cmds as $cmd) {
  1187.                         $out = array();
  1188.                         $ec = -1;
  1189.                         exec($cmd, $out, $ec);
  1190.                         if ($ec == 0) {
  1191.                                 $out = implode("\n",$out);
  1192.                                 $m = array();
  1193.                                 if (preg_match("/([0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2})/ismU", $out, $m)) {
  1194.                                         $detected_mac = $m[1];
  1195.                                         return $detected_mac;
  1196.                                 }
  1197.                         }
  1198.                 }
  1199.         }
  1200.  
  1201.         return $detected_mac;
  1202. }
  1203.