Subversion Repositories uuid_mac_utils

Rev

Rev 22 | Rev 25 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 22 Rev 23
Line 1... Line 1...
1
<?php
1
<?php
2
 
2
 
3
/*
3
/*
4
 * MAC (EUI-48 and EUI-64) utils for PHP
4
 * MAC (EUI-48 and EUI-64) utils for PHP
5
 * Copyright 2017 - 2023 Daniel Marschall, ViaThinkSoft
5
 * Copyright 2017 - 2023 Daniel Marschall, ViaThinkSoft
6
 * Version 2023-05-05
6
 * Version 2023-05-06
7
 *
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this file except in compliance with 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
10
 * You may obtain a copy of the License at
11
 *
11
 *
Line 214... Line 214...
214
 * @param string $ipv6 IPv6-Link-Local address
214
 * @param string $ipv6 IPv6-Link-Local address
215
 * @return false|string MAC-48 (or IPv6 if it was no LinkLocal address, or Modified EUI-64 if it decapsulation failed)
215
 * @return false|string MAC-48 (or IPv6 if it was no LinkLocal address, or Modified EUI-64 if it decapsulation failed)
216
 */
216
 */
217
function ipv6linklocal_to_mac48(string $ipv6) {
217
function ipv6linklocal_to_mac48(string $ipv6) {
218
        // https://stackoverflow.com/questions/12095835/quick-way-of-expanding-ipv6-addresses-with-php (modified)
218
        // https://stackoverflow.com/questions/12095835/quick-way-of-expanding-ipv6-addresses-with-php (modified)
219
        $tmp = inet_pton($ipv6);
219
        $tmp = @inet_pton($ipv6);
220
        if ($tmp === false) return false;
220
        if ($tmp === false) return false;
221
        $hex = unpack("H*hex", $tmp);
221
        $hex = unpack("H*hex", $tmp);
222
        $ipv6 = substr(preg_replace("/([A-f0-9]{4})/", "$1:", $hex['hex']), 0, -1);
222
        $ipv6 = substr(preg_replace("/([A-f0-9]{4})/", "$1:", $hex['hex']), 0, -1);
223
 
223
 
224
        // Remove "fe80::" to convert IPv6-Link-Local address back to EUI-64
224
        // Remove "fe80::" to convert IPv6-Link-Local address back to EUI-64