Skip to content

Commit

Permalink
system: refactor logic and populate hosts file a bit more
Browse files Browse the repository at this point in the history
Already noticed that "ipsec" devices were throwing wrenches into
the engine here so try to match exactly on the pseudo-interface
names given by the GUI.
  • Loading branch information
fichtner committed Aug 3, 2022
1 parent 1e2f93d commit e76f15c
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 16 deletions.
22 changes: 12 additions & 10 deletions src/etc/inc/system.inc
Original file line number Diff line number Diff line change
Expand Up @@ -449,16 +449,18 @@ function system_hosts_generate($verbose = false)

$syscfg = config_read_array('system');

$hosts = "127.0.0.1\tlocalhost localhost.{$syscfg['domain']}\n";

/* we pull the second host entry from a "priority" list: lan, optX or wan */
$iflist = array_keys(get_configured_interface_with_descr());
sort($iflist);

if (!empty($iflist[0])) {
$cfgip = get_interface_ip($iflist[0]);
if (is_ipaddrv4($cfgip)) {
$hosts .= "{$cfgip}\t{$syscfg['hostname']}.{$syscfg['domain']} {$syscfg['hostname']}\n";
$hosts = "127.0.0.1\tlocalhost\tlocalhost.{$syscfg['domain']}\n";
$hosts .= "::1\t\tlocalhost\tlocalhost.{$syscfg['domain']}\n";

$if = get_primary_interface_from_list();
if (!empty($if)) {
$cfgip = get_interface_ip($if);
if (!empty($cfgip)) {
$hosts .= "{$cfgip}\t{$syscfg['hostname']}\t{$syscfg['hostname']}.{$syscfg['domain']}\n";
}
$cfgip = get_interface_ipv6($if);
if (!empty($cfgip)) {
$hosts .= "{$cfgip}\t{$syscfg['hostname']}\t{$syscfg['hostname']}.{$syscfg['domain']}\n";
}
}

Expand Down
30 changes: 25 additions & 5 deletions src/etc/inc/util.inc
Original file line number Diff line number Diff line change
Expand Up @@ -783,9 +783,9 @@ function get_configured_ip_aliases_list()

function get_configured_interface_with_descr()
{
$iflist = array();
$iflist = [];

foreach (legacy_config_get_interfaces(array('virtual' => false)) as $if => $ifdetail) {
foreach (legacy_config_get_interfaces(['virtual' => false]) as $if => $ifdetail) {
if (isset($ifdetail['enable'])) {
$iflist[$if] = $ifdetail['descr'];
}
Expand All @@ -794,17 +794,37 @@ function get_configured_interface_with_descr()
return $iflist;
}

function get_primary_interface_from_list($iflist = null)
{
$ret = null;

if ($iflist === null) {
$iflist = array_keys(get_configured_interface_with_descr());
}

/* we pull the primary entry from a "priority" list: lan, optX or wan */
sort($iflist);

foreach ($iflist as $if) {
if (preg_match('/^(lan|opt[0-9]+|wan)$/', $if)) {
$ret = $if;
break;
}
}

return $ret;
}

/*
* get_configured_ip_addresses() - Return a list of all configured
* interfaces IP Addresses (ipv4+ipv6)
*
*/
function get_configured_ip_addresses()
{
$ip_array = array();
$ip_array = [];

foreach (legacy_interfaces_details() as $ifname => $if) {
foreach (array('ipv4', 'ipv6') as $iptype) {
foreach (['ipv4', 'ipv6'] as $iptype) {
if (!empty($if[$iptype])) {
foreach ($if[$iptype] as $addr) {
if (!empty($addr['ipaddr'])) {
Expand Down
3 changes: 2 additions & 1 deletion src/etc/rc.resolv_conf_generate
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,5 @@ require_once("config.inc");
require_once("system.inc");
require_once("interfaces.inc");

system_resolvconf_generate();
system_resolvconf_generate(true);
system_hosts_generate(true);

0 comments on commit e76f15c

Please sign in to comment.