Skip to content

Commit 323f3f1

Browse files
committed
[3.0] Parse resolv.conf into Options class
Adding parsing logic to the `Config` class that respects values as specified on https://man7.org/linux/man-pages/man5/resolv.conf.5.html in such a way we can easily expand the list to add `search`, `ndots`, and more in follow up PRs. As per https://man7.org/linux/man-pages/man5/resolv.conf.5.html both values are silently capped and are not allowed to go below 1.
1 parent c78b412 commit 323f3f1

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

src/Config/Config.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,29 @@ public static function loadResolvConfBlocking($path = null)
9797
}
9898
}
9999

100+
$matches = [];
101+
preg_match_all('/^options.*\s*$/m', $contents, $matches);
102+
if (isset($matches[0][0])) {
103+
$options = preg_split('/\s+/', trim($matches[0][0]));
104+
array_shift($options);
105+
106+
foreach ($options as $option) {
107+
$value = null;
108+
if (strpos($option, ':') !== false) {
109+
[$option, $value] = explode(':', $option, 2);
110+
}
111+
112+
switch ($option) {
113+
case 'attempts':
114+
$config->options->attempts = ((int) $value) > 5 ? 5 : (int) $value;
115+
break;
116+
case 'timeout':
117+
$config->options->timeout = ((int) $value) > 30 ? 30 : (int) $value;
118+
break;
119+
}
120+
}
121+
}
122+
100123
return $config;
101124
}
102125

@@ -134,4 +157,13 @@ public static function loadWmicBlocking($command = null)
134157
}
135158

136159
public $nameservers = [];
160+
161+
/**
162+
* @var Options
163+
*/
164+
public $options;
165+
166+
public function __construct() {
167+
$this->options = new Options();
168+
}
137169
}

tests/Config/ConfigTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ public function testLoadsFromExplicitPath()
3030
$config = Config::loadResolvConfBlocking(__DIR__ . '/../Fixtures/etc/resolv.conf');
3131

3232
$this->assertEquals(['8.8.8.8'], $config->nameservers);
33+
$this->assertEquals(4, $config->options->attempts);
34+
$this->assertEquals(29, $config->options->timeout);
3335
}
3436

3537
public function testLoadThrowsWhenPathIsInvalid()

tests/Fixtures/etc/resolv.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
nameserver 8.8.8.8
2+
options timeout:29 trust-ad attempts:4

0 commit comments

Comments
 (0)