1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
|
<?php
/**
* This file is part of the ramsey/uuid library
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @copyright Copyright (c) Ben Ramsey <ben@benramsey.com>
* @license http://opensource.org/licenses/MIT MIT
*/
declare(strict_types=1);
namespace Ramsey\Uuid\Provider\Node;
use Ramsey\Uuid\Exception\NodeException;
use Ramsey\Uuid\Provider\NodeProviderInterface;
use Ramsey\Uuid\Type\Hexadecimal;
use function array_filter;
use function array_map;
use function array_walk;
use function count;
use function ob_get_clean;
use function ob_start;
use function preg_match;
use function preg_match_all;
use function reset;
use function str_replace;
use function strpos;
use function strtolower;
use function strtoupper;
use function substr;
use const GLOB_NOSORT;
use const PREG_PATTERN_ORDER;
/**
* SystemNodeProvider retrieves the system node ID, if possible
*
* The system node ID, or host ID, is often the same as the MAC address for a
* network interface on the host.
*/
class SystemNodeProvider implements NodeProviderInterface
{
/**
* Pattern to match nodes in ifconfig and ipconfig output.
*/
private const IFCONFIG_PATTERN = '/[^:]([0-9a-f]{2}([:-])[0-9a-f]{2}(\2[0-9a-f]{2}){4})[^:]/i';
/**
* Pattern to match nodes in sysfs stream output.
*/
private const SYSFS_PATTERN = '/^([0-9a-f]{2}:){5}[0-9a-f]{2}$/i';
public function getNode(): Hexadecimal
{
$node = $this->getNodeFromSystem();
if ($node === '') {
throw new NodeException(
'Unable to fetch a node for this system'
);
}
return new Hexadecimal($node);
}
/**
* Returns the system node, if it can find it
*/
protected function getNodeFromSystem(): string
{
static $node = null;
if ($node !== null) {
return (string) $node;
}
// First, try a Linux-specific approach.
$node = $this->getSysfs();
if ($node === '') {
// Search ifconfig output for MAC addresses & return the first one.
$node = $this->getIfconfig();
}
$node = str_replace([':', '-'], '', $node);
return $node;
}
/**
* Returns the network interface configuration for the system
*
* @codeCoverageIgnore
*/
protected function getIfconfig(): string
{
$disabledFunctions = strtolower((string) ini_get('disable_functions'));
if (strpos($disabledFunctions, 'passthru') !== false) {
return '';
}
ob_start();
switch (strtoupper(substr(constant('PHP_OS'), 0, 3))) {
case 'WIN':
passthru('ipconfig /all 2>&1');
break;
case 'DAR':
passthru('ifconfig 2>&1');
break;
case 'FRE':
passthru('netstat -i -f link 2>&1');
break;
case 'LIN':
default:
passthru('netstat -ie 2>&1');
break;
}
$ifconfig = (string) ob_get_clean();
$node = '';
if (preg_match_all(self::IFCONFIG_PATTERN, $ifconfig, $matches, PREG_PATTERN_ORDER)) {
$node = $matches[1][0] ?? '';
}
return $node;
}
/**
* Returns MAC address from the first system interface via the sysfs interface
*/
protected function getSysfs(): string
{
$mac = '';
if (strtoupper(constant('PHP_OS')) === 'LINUX') {
$addressPaths = glob('/sys/class/net/*/address', GLOB_NOSORT);
if ($addressPaths === false || count($addressPaths) === 0) {
return '';
}
$macs = [];
array_walk($addressPaths, function (string $addressPath) use (&$macs): void {
if (is_readable($addressPath)) {
$macs[] = file_get_contents($addressPath);
}
});
$macs = array_map('trim', $macs);
// Remove invalid entries.
$macs = array_filter($macs, function (string $address) {
return $address !== '00:00:00:00:00:00'
&& preg_match(self::SYSFS_PATTERN, $address);
});
$mac = reset($macs);
}
return (string) $mac;
}
}
|