Exemple #1
0
function _fw_add_ips($opt, $addr, $fam, &$res)
{
    list($netid, $hostid) = parse_ipnet($addr);
    if ($netid) {
        global $cf;
        if (!isset($cf['nets'][$netid])) {
            trigger_error("{$opt}({$addr}) unknown network", E_USER_WARNING);
            return;
        }
        if (!$hostid) {
            // Assume we refer to the full subnet
            if ($fam == 'ipv6' || $fam == 'any') {
                if (isset($cf['nets'][$netid]['ip6'])) {
                    $res[] = '  option ' . $opt . ' ' . $cf['nets'][$netid]['ip6'] . '::/64' . NL;
                } elseif ($fam == 'ipv6') {
                    trigger_error("{$opt}({$addr}) Not an IPv6 network", E_USER_WARNING);
                }
            }
            if ($fam == 'ipv4' || $fam == 'any') {
                if (isset($cf['nets'][$netid]['ip4'])) {
                    $res[] = '  option ' . $opt . ' ' . $cf['nets'][$netid]['ip4'] . '.0/24' . NL;
                } elseif ($fam == 'ipv4') {
                    trigger_error("{$opt}({$addr}) Not an IPv4 network", E_USER_WARNING);
                }
            }
            return;
        }
        if ($fam == 'ipv6' || $fam == 'any') {
            if (isset($cf['nets'][$netid]['ip6'])) {
                $res[] = '  option ' . $opt . ' ' . $cf['nets'][$netid]['ip6'] . '::' . dechex($hostid) . NL;
            } elseif ($fam == 'ipv6') {
                trigger_error("{$opt}({$addr}) Not an IPv6 network", E_USER_WARNING);
            }
        }
        if ($fam == 'ipv4' || $fam == 'any') {
            if (isset($cf['nets'][$netid]['ip4'])) {
                $res[] = '  option ' . $opt . ' ' . $cf['nets'][$netid]['ip4'] . '.' . $hostid . NL;
            } elseif ($fam == 'ipv4') {
                trigger_error("{$opt}({$addr}) Not an IPv4 network", E_USER_WARNING);
            }
        }
        return;
    }
    trigger_error("Invalid netaddr definition {$addr}", E_USER_WARNING);
}
Exemple #2
0
/**
 * Parse and lookup an IP address reference
 *
 * Given an ip in the form `net:`_netid_._hostid_, it will generate
 * IPv4 and/or IPv6 addresses for it.
 *
 * @param $ip	string		IP address reference
 * @param &$cf	array_ref	Configuration array, must have a `nets` element
 * @return array		Array of strings containing IPv4/IPv6 addresses
 */
function lk_parse_ip($ip, &$cf)
{
    list($netid, $hostid) = parse_ipnet($ip);
    $res = array();
    if ($netid) {
        if (!isset($cf['nets'][$netid])) {
            trigger_error("Unknown network {$ip}", E_USER_WARNING);
            return $res;
        }
        $res['netid'] = $netid;
        $netdat =& $cf['nets'][$netid];
        if (isset($netdat['vlan'])) {
            $res['vlan'] = $netdat['vlan'];
        }
        $res['hostid'] = $hostid;
        if (!$hostid) {
            return $res;
        }
        // Configured without IP...
        if (isset($netdat['ip6'])) {
            $res['ip6'] = $netdat['ip6'] . '::' . dechex($hostid);
        }
        if (isset($netdat['ip4'])) {
            $res['ip4'] = $netdat['ip4'] . '.' . $hostid;
        }
    } else {
        if (is_ipv6($ip)) {
            $res['ip6'] = $ip;
        } elseif (is_ipv4($ip)) {
            $res['ip4'] = $ip;
        }
    }
    return $res;
}