Skip to content

Commit 0036079

Browse files
KDederichskesselb
authored andcommitted
Fix FreeBsd Interface parsing in cases that the interface has no speed or mac
Signed-off-by: Kai Dederichs <kai.dederichs@protonmail.com> Make test throw when requesting a non-valid interface Signed-off-by: Kai Dederichs <kai.dederichs@protonmail.com> Backport FreeBSD.php to stable22 Signed-off-by: Kai Dederichs <k.dederichs@spicymedia.io> Fix CS Signed-off-by: Kai Dederichs <k.dederichs@spicymedia.io>
1 parent 26dd4de commit 0036079

6 files changed

Lines changed: 98 additions & 6 deletions

File tree

lib/OperatingSystems/FreeBSD.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ public function getNetworkInterfaces(): array {
159159
preg_match_all("/^(?<=(?!\t)).*(?=:)/m", $ifconfig, $interfaces);
160160

161161
foreach ($interfaces[0] as $interface) {
162-
$iface = [];
162+
$iface = [];
163163
$iface['interface'] = $interface;
164164

165165
try {
@@ -170,17 +170,22 @@ public function getNetworkInterfaces(): array {
170170

171171
preg_match_all("/(?<=inet ).\S*/m", $intface, $ipv4);
172172
preg_match_all("/(?<=inet6 )((.*(?=%))|(.\S*))/m", $intface, $ipv6);
173-
$iface['ipv4'] = implode(' ', $ipv4[0]);
174-
$iface['ipv6'] = implode(' ', $ipv6[0]);
173+
$iface['ipv4'] = implode(' ', $ipv4[0]);
174+
$iface['ipv6'] = implode(' ', $ipv6[0]);
175175

176176
if ($iface['interface'] !== 'lo0') {
177177
preg_match_all("/(?<=ether ).*/m", $intface, $mac);
178178
preg_match("/(?<=status: ).*/m", $intface, $status);
179179
preg_match("/\b[0-9].*?(?=base)/m", $intface, $speed);
180180
preg_match("/(?<=\<).*(?=-)/m", $intface, $duplex);
181181

182-
$iface['mac'] = implode(' ', $mac[0]);
183-
$iface['speed'] = $speed[0];
182+
if (isset($mac[0])) {
183+
$iface['mac'] = implode(' ', $mac[0]);
184+
}
185+
186+
if (isset($speed[0])) {
187+
$iface['speed'] = $speed[0];
188+
}
184189

185190
if (isset($status[0])) {
186191
$iface['status'] = $status[0];
@@ -206,7 +211,7 @@ public function getNetworkInterfaces(): array {
206211
}
207212
} else {
208213
$iface['status'] = 'active';
209-
$iface['speed'] = 'unknown';
214+
$iface['speed'] = 'unknown';
210215
$iface['duplex'] = '';
211216
}
212217
$result[] = $iface;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
epair0b: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> metric 0 mtu 1500
2+
options=8<VLAN_MTU>
3+
ether 1a:c0:4d:ba:b5:82
4+
hwaddr 02:60:e8:04:f6:0b
5+
inet 192.168.178.150 netmask 0xffffff00 broadcast 192.168.178.255
6+
groups: epair
7+
media: Ethernet 10Gbase-T (10Gbase-T <full-duplex>)
8+
status: active
9+
nd6 options=1<PERFORMNUD>

tests/data/freebsd_interface_lo0

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> metric 0 mtu 16384
2+
options=680003<RXCSUM,TXCSUM,LINKSTATE,RXCSUM_IPV6,TXCSUM_IPV6>
3+
inet6 ::1 prefixlen 128
4+
inet6 fe80::1%lo0 prefixlen 64 scopeid 0x1
5+
inet 127.0.0.1 netmask 0xff000000
6+
groups: lo
7+
nd6 options=21<PERFORMNUD,AUTO_LINKLOCAL>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pflog0: flags=0<> metric 0 mtu 33160
2+
groups: pflog

tests/data/freebsd_interfaces

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> metric 0 mtu 16384
2+
options=680003<RXCSUM,TXCSUM,LINKSTATE,RXCSUM_IPV6,TXCSUM_IPV6>
3+
inet6 ::1 prefixlen 128
4+
inet6 fe80::1%lo0 prefixlen 64 scopeid 0x1
5+
inet 127.0.0.1 netmask 0xff000000
6+
groups: lo
7+
nd6 options=21<PERFORMNUD,AUTO_LINKLOCAL>
8+
pflog0: flags=0<> metric 0 mtu 33160
9+
groups: pflog
10+
epair0b: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> metric 0 mtu 1500
11+
options=8<VLAN_MTU>
12+
ether 1a:c0:4d:ba:b5:82
13+
hwaddr 02:60:e8:04:f6:0b
14+
inet 192.168.178.150 netmask 0xffffff00 broadcast 192.168.178.255
15+
groups: epair
16+
media: Ethernet 10Gbase-T (10Gbase-T <full-duplex>)
17+
status: active
18+
nd6 options=1<PERFORMNUD>

tests/lib/FreeBSDTest.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,57 @@ public function testGetMemoryNoData(): void {
9696
$this->assertEquals(new Memory(), $this->os->getMemory());
9797
}
9898

99+
public function testGetNetworkInterfaces(): void {
100+
$this->os->method('executeCommand')
101+
->willReturnCallback(static function ($command) {
102+
if ($command === '/sbin/ifconfig -a') {
103+
return file_get_contents(__DIR__ . '/../data/freebsd_interfaces');
104+
}
105+
if ($command === '/sbin/ifconfig lo0') {
106+
return file_get_contents(__DIR__ . '/../data/freebsd_interface_lo0');
107+
}
108+
if ($command === '/sbin/ifconfig pflog0') {
109+
return file_get_contents(__DIR__ . '/../data/freebsd_interface_pflog0');
110+
}
111+
if ($command === '/sbin/ifconfig epair0b') {
112+
return file_get_contents(__DIR__ . '/../data/freebsd_interface_epair0b');
113+
}
114+
115+
// Regex matches way more than the interface names, so if it doesn't match any of the defined ones, throw.
116+
throw new \RuntimeException();
117+
});
118+
119+
$interfaces = $this->os->getNetworkInterfaces();
120+
$this->assertEquals([
121+
[
122+
"interface" => "lo0",
123+
"ipv4" => "127.0.0.1",
124+
"ipv6" => "::1 fe80::1",
125+
"status" => "active",
126+
"speed" => "unknown",
127+
"duplex" => "",
128+
],
129+
[
130+
"interface" => "pflog0",
131+
"ipv4" => "",
132+
"ipv6" => "",
133+
"mac" => "",
134+
"status" => "active",
135+
"speed" => "unknown",
136+
"duplex" => "",
137+
],
138+
[
139+
"interface" => "epair0b",
140+
"ipv4" => "192.168.178.150",
141+
"ipv6" => "",
142+
"mac" => "1a:c0:4d:ba:b5:82",
143+
"speed" => "10 Gbps",
144+
"status" => "active",
145+
"duplex" => "Duplex: full",
146+
]
147+
], $interfaces);
148+
}
149+
99150
public function testSupported(): void {
100151
$this->assertFalse($this->os->supported());
101152
}

0 commit comments

Comments
 (0)