예제 #1
0
function _fw_expand_ips($opt, $ipspec, $fam)
{
    $res = array();
    if (substr($ipspec, 0, 4) == 'net:') {
        _fw_add_ips($opt, $ipspec, $fam, $res);
        return $res;
    }
    if (($fam == 'ipv6' || $fam == 'any') && is_ipv6($ipspec)) {
        $res[] = '  option ' . $opt . ' ' . $addr . NL;
        return $res;
    }
    if (($fam == 'ipv4' || $fam == 'any') && is_ipv4($ipspec)) {
        $res[] = '  option ' . $opt . ' ' . $addr . NL;
        return $res;
    }
    $nic = NULL;
    $hn = $ipspec;
    if ($p = strrpos($ipspec, '-')) {
        if ($p) {
            $nic = substr($ipspec, $p + 1);
            $hn = substr($ipspec, 0, $p);
        }
    }
    foreach (lk_addr($hn, $nic) as $addr) {
        if (($fam == 'ipv4' || $fam == 'any') && isset($addr['ip4'])) {
            $res[] = '  option ' . $opt . ' ' . $addr['ip4'] . NL;
        }
        if (($fam == 'ipv6' || $fam == 'any') && isset($addr['ip6'])) {
            $res[] = '  option ' . $opt . ' ' . $addr['ip6'] . NL;
        }
    }
    if (!count($res)) {
        // trigger_error("Unable to lookup ipspec: $ipspec",E_USER_WARNING);
        $res[] = '  option ' . $opt . ' ' . $ipspec . NL;
    }
    return $res;
}
예제 #2
0
function dump_hosts()
{
    global $cf;
    $txt = '';
    foreach ($cf['hosts'] as $hn => $hdat) {
        if (isset($hdat['alias'])) {
            $aliases = $hdat['alias'];
        } else {
            $aliases = array();
        }
        foreach (lk_addr($hn) as $addr) {
            if (isset($addr['ip4'])) {
                $txt .= dump_host($addr['ip4'], $addr, $hn, $aliases);
            }
            if (isset($addr['ip6'])) {
                $txt .= dump_host($addr['ip6'], $addr, $hn, $aliases);
            }
        }
    }
    return $txt;
}
예제 #3
0
function config_network_interface($host, $ifname)
{
    $addr = lk_addr($host, $ifname);
    if (!count($addr)) {
        return '';
    }
    $proto = 'dhcp';
    if (isset($addr[0]['hostid'])) {
        if ($addr[0]['hostid']) {
            $proto = 'static';
        } elseif ($addr[0]['hostid'] !== "") {
            $proto = 'non-addr';
        }
    }
    global $cf;
    $txt = 'config interface ' . $ifname . NL;
    $vlan = res('vlan', $ifname);
    if (!$vlan) {
        trigger_error('Interface ' . $ifname . ' missing VLAN configuration', E_USER_WARNING);
    } else {
        $txt .= '    option ifname eth0.' . $vlan . NL;
        switch ($proto) {
            case 'static':
                $txt .= '    option force_link 1' . NL;
                $txt .= '    option proto static' . NL;
                $ip = res('ip4', $ifname);
                if ($ip) {
                    $txt .= '    option ipaddr ' . $ip . NL;
                    $txt .= '    option netmask 255.255.255.0' . NL;
                    $ip = lk_gw('ip4', $ifname);
                    if ($ip) {
                        $txt .= '    option gateway ' . $ip . NL;
                    }
                }
                $ip = res('ip6', $ifname);
                if ($ip) {
                    $txt .= '    option ip6addr ' . $ip . '/64' . NL;
                    $ip = lk_gw('ip6', $ifname);
                    if ($ip) {
                        $txt .= '    option ip6gw ' . $ip . NL;
                    }
                }
                break;
            case 'non-addr':
                break;
            case 'dhcp':
            default:
                $txt .= '    option proto dhcp' . NL;
                break;
        }
        if (isset($cf['hosts'][SYSNAME]['nics'][$ifname]['wifi'])) {
            if ($cf['hosts'][SYSNAME]['nics'][$ifname]['wifi']) {
                $txt .= '    option type bridge' . NL;
            }
        }
        if (isset($cf['hosts'][SYSNAME]['nics'][$ifname]['opts'])) {
            $xopts = $cf['hosts'][SYSNAME]['nics'][$ifname]['opts'];
            if (!is_array($xopts)) {
                $xopts = array($xopts);
            }
            foreach ($xopts as $ln) {
                $txt .= '    option ' . $ln . NL;
            }
        }
    }
    $txt .= NL;
    return $txt;
}
예제 #4
0
function lk_gw($type, $a = NULL, $b = NULL)
{
    list($host, $nic) = _lk_defaults($a, $b);
    foreach (lk_addr($host, $nic) as $addr) {
        if (!isset($addr['netid'])) {
            continue;
        }
        if (!isset($addr['hostid'])) {
            continue;
        }
        if (!isset($addr[$type])) {
            continue;
        }
        if ($addr['hostid'] != 1) {
            global $cf;
            $netdat =& $cf['nets'][$addr['netid']];
            if ($type == 'ip4') {
                if (isset($netdat['gw'])) {
                    if (preg_match('/^\\d+$/', $netdat['gw'])) {
                        return $netdat['ip4'] . '.' . $netdat['gw'];
                    }
                    return $netdat['gw'];
                }
                return $netdat['ip4'] . '.1';
            } elseif ($type == 'ip6') {
                if (isset($netdat['ip6gw'])) {
                    if (preg_match('/^\\d+$/', $netdat['ip6gw'])) {
                        return $netdat['ip6'] . '::' . dechex($netdat['ip6gw']);
                    }
                    return $netdat['ip6gw'];
                }
                if (isset($netdat['gw']) && preg_match('/^\\d+$/', $netdat['gw'])) {
                    return $netdat['ip6'] . '::' . dechex($netdat['gw']);
                }
                return $netdat['ip6'] . '::1';
            } else {
                trigger_error('Invalid address type ' . $type, E_USER_WARNING);
            }
        }
    }
    return NULL;
}