示例#1
0
function _guifi_radio_add_wds_get_new_interface($nid, $ips_allocated = array())
{
    //
    // initializing WDS/p2p link parameters
    //
    $newlk = array();
    $newlk['new'] = TRUE;
    $newlk['interface'] = array();
    $newlk['link_type'] = 'wds';
    $newlk['flag'] = 'Planned';
    $newlk['routing'] = 'BGP';
    if (!$ips_allocated) {
        // get list of the current used ips
        $ips_allocated = guifi_ipcalc_get_ips('0.0.0.0', '0.0.0.0', array(), 2);
    }
    // get an ip addres for local-remote interfaces
    $net = guifi_ipcalc_get_subnet_by_nid($nid, '255.255.255.252', 'backbone', $ips_allocated);
    if (!$net) {
        return -1;
    }
    $dnet = ip2long($net);
    $ip1 = long2ip($dnet + 1);
    $ip2 = long2ip($dnet + 2);
    $newlk['interface']['interface_type'] = 'wds/p2p';
    // remote ipv4
    $newlk['interface']['ipv4'] = array();
    $newlk['interface']['ipv4']['new'] = TRUE;
    $newlk['interface']['ipv4']['ipv4_type'] = 2;
    $newlk['interface']['ipv4']['ipv4'] = $ip2;
    $newlk['interface']['ipv4']['netmask'] = '255.255.255.252';
    // initializing local interface
    $newif = array();
    $newif['new'] = TRUE;
    $newif['ipv4_type'] = 2;
    $newif['ipv4'] = $ip1;
    $newif['netmask'] = '255.255.255.252';
    // agregating into the main array
    $newif['links'] = array();
    $newif['links'][0] = $newlk;
    return $newif;
}
function guifi_interfaces_add_cable_p2p_link_submit(&$form, &$form_state)
{
    $values = $form_state['clicked_button']['#parents'];
    $iid = $values[1];
    $to_did = $form_state['values']['interfaces'][$iid]['to_did'];
    $rdevice = guifi_device_load($to_did);
    guifi_log(GUIFILOG_TRACE, sprintf('function guifi_interfaces_add_cable_p2p_link_submit(%d)', $iid), $form_state['values']['interfaces'][$iid]);
    $dlinked = db_fetch_array(db_query("SELECT d.id, d.type " . "FROM {guifi_devices} d " . "WHERE d.id=%d AND type not IN ('switch','rack','torpedo','splitter') " . "ORDER BY ", $to_did));
    $ips_allocated = guifi_ipcalc_get_ips('0.0.0.0', '0.0.0.0', $form_state['values'], 2);
    // get backbone /30 subnet
    $mask = '255.255.255.252';
    $net = guifi_ipcalc_get_subnet_by_nid($form_state['values']['nid'], $mask, 'backbone', $ips_allocated);
    if (!$net) {
        drupal_set_message(t('Unable to create link, no networks available'), 'warning');
        return FALSE;
    }
    $dnet = ip2long($net);
    $ip1 = long2ip($dnet + 1);
    $ip2 = long2ip($dnet + 2);
    $newlk['new'] = TRUE;
    $newlk['interface'] = array();
    $newlk['link_type'] = 'cable';
    $newlk['flag'] = 'Planned';
    $newlk['nid'] = $form_state['values']['nid'];
    $newlk['device_id'] = $to_did;
    if ($dlinked['type'] == 'radio') {
        $newlk['routing'] = 'BGP';
    } else {
        $newlk['routing'] = 'Gateway';
    }
    $newlk['interface']['new'] = TRUE;
    $newlk['interface']['device_id'] = $to_did;
    $free = guifi_get_free_interfaces($to_did, $rdevice);
    $newlk['interface']['interface_type'] = array_shift($free);
    //  $newlk['interface']['interface_type']= 'ether3';
    $newlk['interface']['ipv4']['new'] = TRUE;
    $newlk['interface']['ipv4']['ipv4_type'] = 2;
    $newlk['interface']['ipv4']['ipv4'] = $ip2;
    $newlk['interface']['ipv4']['netmask'] = $mask;
    $ipv4['new'] = TRUE;
    $ipv4['ipv4'] = $ip1;
    $ipv4['ipv4_type'] = 2;
    $ipv4['netmask'] = $mask;
    $ipv4['interface_id'] = $iid;
    $ipv4['links'][] = $newlk;
    $form_state['values']['interfaces'][$iid]['ipv4'][] = $ipv4;
    $form_state['values']['interfaces'][$iid]['unfold'] = TRUE;
    $form_state['rebuild'] = TRUE;
    return TRUE;
}
示例#3
0
function guifi_links_check_overlap($overlap, &$form_state)
{
    if ($form_state['clicked_button']['#value'] == t('Reset')) {
        return;
    }
    $keys = count($overlap['#parents']);
    $radio_id = $overlap['#parents'][$keys - 6];
    $interface_id = $overlap['#parents'][$keys - 4];
    $ipv4_id = $overlap['#parents'][$keys - 2];
    if ($keys == 7) {
        $ipv4 =& $form_state['values']['radios'][$radio_id]['interfaces'][$interface_id]['ipv4'][$ipv4_id];
    } else {
        $ipv4 =& $form_state['values']['interfaces'][$interface_id]['ipv4'][$ipv4_id];
    }
    if ($ipv4['links'][$link_id]['deleted']) {
        return;
    }
    if (ip2long($ipv4['netmask']) >= ip2long($ipv4['overlap'])) {
        return;
    }
    $net = _ipcalc($ipv4['ipv4'], $ipv4['netmask']);
    $net_overlap = _ipcalc($ipv4['ipv4'], $ipv4['overlap']);
    $old_netid = $net_overlap['netid'];
    $new_netid = $net['netid'];
    $old_netmask = $ipv4['overlap'];
    $new_netmask = $ipv4['netmask'];
    $new_broadcast = $net['broadcast'];
    $old_broadcast = $net_overlap['broadcast'];
    //guifi_log(GUIFILOG_BASIC,'<br />Old Netid: '.$old_netid.'<br />New Netid: '.$new_netid.'<br />Old Netmask: '.$old_netmask.'<br />New NetMask: '.$new_netmask.'<br />Old broadcast: '.$old_broadcast.'<br />New Broadcast: '.$new_broadcast.'<br /><br /> ');
    $sql = db_query("SELECT INET_ATON(ipv4) as ip FROM guifi_ipv4 WHERE ipv4 BETWEEN '%s' AND '%s' ", $old_broadcast, $new_broadcast);
    while ($item = db_fetch_array($sql)) {
        $ip = long2ip($item['ip']);
        $s = ip2long($old_netid);
        $e = ip2long($new_broadcast);
        for ($i = $s; $i < $e + 1; $i++) {
            $ipnow = long2ip($i);
            if ($ip == $ipnow) {
                drupal_set_message(t('Ip address: %ip is already taken on another device!!', array('%ip' => $ip)), 'error');
                $error = TRUE;
                //guifi_log(GUIFILOG_BASIC,'Tipus de check: Broadcast<br />');
            }
        }
        $count = $e - $s + 1;
    }
    $sql = db_query("SELECT INET_ATON(ipv4) as ip FROM guifi_ipv4 WHERE ipv4 BETWEEN '%s' AND '%s' ", $new_netid, $old_netid);
    while ($item = db_fetch_array($sql)) {
        $ip = long2ip($item['ip']);
        $s = ip2long($new_netid);
        $e = ip2long($old_netid);
        for ($i = $s; $i < $e + 1; $i++) {
            $ipnow = long2ip($i);
            if ($ip == $ipnow) {
                drupal_set_message(t('Ip address: %ip is already taken on another device!!', array('%ip' => $ip)), 'error');
                $error = TRUE;
                //guifi_log(GUIFILOG_BASIC,'Tipus de check: Netid<br>');
            }
        }
        $count = $e - $s + 1;
    }
    if ($error == TRUE) {
        form_error($overlap, t('Error! Your new netmask: /%bit ( %mask ) is overlapping another existing subnet, you can\'t expand it!<br />' . 'Then, We will find a range of network in your area with the size needed, just for information. You can use it if it thinks fit.', array('%mask' => $new_netmask, '%bit' => $net['maskbits'])));
        $nid = $form_state['values']['nid'];
        $ips_allocated = guifi_ipcalc_get_ips('0.0.0.0', '0.0.0.0', array(), 2);
        guifi_ipcalc_get_subnet_by_nid($nid, $new_netmask, 'backbone', $ips_allocated, 'Yes', TRUE);
    }
}
示例#4
0
/**
 * Search for an unused subrange of network in the database.
 *
 * You can specify a network_zone where to search a subrange. In case such subrange is too big or doesn't exist,
 * it will try to find it in the parent network zone and so, recursively.
 *
 * @param $params
 *   String with values separated by commas: $mask, $network_type, $zone_id and $allocate.
 *
 * @return
 *   theme()
 */
function guifi_tools_ip_rangesearch($params = NULL)
{
    $output .= drupal_get_form('guifi_tools_ip_rangesearch_form', $params);
    if (empty($params)) {
        return $output;
    }
    // for testing, load a device with quite a few ip's'
    // $device = guifi_device_load(115);
    $tgetipsbegin = microtime(TRUE);
    $ips_allocated = guifi_ipcalc_get_ips('0.0.0.0', '0.0.0.0');
    $tgetipsend = microtime(TRUE);
    $toutput = t('Got & sorted %num ips in %secs seconds', array('%num' => number_format(count($ips_allocated)), '%secs' => number_format($tgetipsend - $tgetipsbegin, 4))) . '<br />';
    list($mask, $network_type, $zone_id, $allocate) = explode(',', $params);
    if (!user_access('administer guifi networks')) {
        $allocate = 'No';
    }
    $net = guifi_ipcalc_get_subnet_by_nid($zone_id, $mask, $network_type, $ips_allocated, $allocate, TRUE);
    // verbose output
    $tgetsubnetbynid = microtime(TRUE);
    $toutput .= t('Got %base/%net in %secs seconds', array('%base' => $net, '%net' => $mask, '%secs' => number_format($tgetsubnetbynid - $tgetipsend, 4))) . '<br />';
    $toutput .= t('Total elapsed was %secs seconds', array('%secs' => number_format($tgetsubnetbynid - $tgetipsbegin, 4))) . '<br />';
    $item = _ipcalc($net, $mask);
    if ($net) {
        foreach ($item as $k => $value) {
            $header[] = t($k);
            $row[] = $value;
        }
        $qoutput .= theme('box', t('Space found at %net', array('%net' => $net)), theme('table', $header, array($row)));
    } else {
        drupal_set_message(t('Was not possible to find %type space for %mask', array('%type' => $network_type, '%mask' => $mask)), 'error');
    }
    return $qoutput . theme('box', t('Find available space for a subnetwork'), $output) . theme('box', t('Performance'), '<small>' . $toutput . '</small>');
}
示例#5
0
/**
 * Add ipv4s dialog
 * Used at device/radio edit form, at IPv4 Networking section for adding
 *  Public, Private subnetworks, or get an IP from an already defined subnetwork
 *  at another device 
 *
 * URL: http://guifi.net/guifi/js/add-ipv4s
 */
function guifi_ahah_add_ipv4s()
{
    $cid = 'form_' . $_POST['form_build_id'];
    $cache = cache_get($cid, 'cache_form');
    $iClass = arg(3);
    guifi_log(GUIFILOG_TRACE, 'guifi_ahah_add_ipv4s(' . $iClass . ')', $_POST[ipv4][ipv4sdialog][netmask]);
    //if (!$net) {
    //  drupal_set_message(t(
    //    'Unable to allocate new range, no networks available'),'warning');
    //  return FALSE;
    //}
    if ($cache) {
        $form = $cache->data;
        $form_dialog = $form['ipv4']['ipv4sdialog'];
        guifi_log(GUIFILOG_TRACE, 'guifi_ahah_add_ipv4s(dialog)', $form_dialog);
        // Dialog fields
        $form['ipv4']['ipv4sdialog']['#type'] = 'fieldset';
        $form['ipv4']['ipv4sdialog']['#collapsed'] = false;
        $form['ipv4']['ipv4sdialog']['#collapsible'] = true;
        switch ($iClass) {
            case 'private':
                $form['ipv4']['ipv4sdialog']['adddid']['#type'] = 'hidden';
                $form['ipv4']['ipv4sdialog']['adddid']['#value'] = false;
                $form['ipv4']['ipv4sdialog']['snet']['#type'] = 'hidden';
                $form['ipv4']['ipv4sdialog']['snet']['#value'] = false;
                $form['ipv4']['ipv4sdialog']['netmask']['#type'] = 'select';
                $form['ipv4']['ipv4sdialog']['netmask']['#value'] = '255.255.255.252';
                $form['ipv4']['ipv4sdialog']['ipv4_type']['#value'] = 2;
                $form['ipv4']['ipv4sdialog']['ipv4']['#type'] = 'textfield';
                $form['ipv4']['ipv4sdialog']['netmask']['#options'] = guifi_types('netmask', 30, 26);
                $form['ipv4']['ipv4sdialog']['netmask']['#description'] = t('%class subnetwork size (mask)', array('%class' => $iClass));
                $form['ipv4']['ipv4sdialog']['#description'] = t('Obtain a new %class subnetwork range and get an IPv4 address from it', array('%class' => $iClass));
                break;
            case 'public':
                $form['ipv4']['ipv4sdialog']['adddid']['#type'] = 'hidden';
                $form['ipv4']['ipv4sdialog']['adddid']['#value'] = false;
                $form['ipv4']['ipv4sdialog']['snet']['#type'] = 'hidden';
                $form['ipv4']['ipv4sdialog']['snet']['#value'] = false;
                $form['ipv4']['ipv4sdialog']['netmask']['#type'] = 'select';
                $form['ipv4']['ipv4sdialog']['netmask']['#value'] = '255.255.255.224';
                $form['ipv4']['ipv4sdialog']['ipv4_type']['#value'] = 1;
                $form['ipv4']['ipv4sdialog']['ipv4']['#type'] = 'textfield';
                $form['ipv4']['ipv4sdialog']['netmask']['#options'] = guifi_types('netmask', 30, 22);
                $form['ipv4']['ipv4sdialog']['netmask']['#description'] = t('%class subnetwork size (mask)', array('%class' => $iClass));
                $form['ipv4']['ipv4sdialog']['#description'] = t('Obtain a new %class subnetwork range and get an IPv4 address from it', array('%class' => $iClass));
                break;
            case 'netmask':
                $form['ipv4']['ipv4sdialog']['netmask']['#value'] = $_POST[ipv4][ipv4sdialog][netmask];
                break;
            case 'defined':
                $form['ipv4']['ipv4sdialog']['netmask']['#type'] = 'hidden';
                $form['ipv4']['ipv4sdialog']['adddid']['#type'] = 'textfield';
                $form['ipv4']['ipv4sdialog']['snet']['#type'] = 'select';
                $form['ipv4']['ipv4sdialog']['ipv4']['#type'] = 'hidden';
                $form['ipv4']['ipv4sdialog']['ipv4']['#value'] = 'false';
                $form['ipv4']['ipv4sdialog']['#description'] = t('Get a new ip from an already defined subnetwork range at any existing device');
        }
        // Obtaining ipv4
        if ($iClass != 'defined') {
            $ntype = $form['ipv4']['ipv4sdialog']['ipv4_type']['#value'];
            drupal_set_message(t('Obtaining a new %type range', array('%type' => $ntype == 1 ? t('public') : t('private'))), 'warning');
            $ips_allocated = guifi_ipcalc_get_ips('0.0.0.0', '0.0.0.0', $_POST, $ntype);
            $net = guifi_ipcalc_get_subnet_by_nid($_POST[nid], $form['ipv4']['ipv4sdialog']['netmask']['#value'], $ntype == 1 ? 'public' : 'backbone', $ips_allocated, 'Yes', TRUE);
            $form['ipv4']['ipv4sdialog']['ipv4']['#value'] = long2ip(ip2long($net) + 1);
        }
        unset($form['ipv4']['ipv4sdialog']['#prefix']);
        unset($form['ipv4']['ipv4sdialog']['#suffix']);
        guifi_log(GUIFILOG_TRACE, 'guifi_ahah_add_ipv4s(dialog)', $form['ipv4']['ipv4sdialog']);
        cache_set($cid, $form, 'cache_form', $cache->expire);
        // Build and render the new select element, then return it in JSON format.
        $form_state = array();
        $form['#post'] = $_POST;
        $form = form_builder($form['form_id']['#value'], $form, $form_state);
        $output = theme('status_messages') . drupal_render($form['ipv4']['ipv4sdialog']);
        drupal_json(array('status' => TRUE, 'data' => $output));
    } else {
        drupal_json(array('status' => FALSE, 'data' => ''));
    }
    exit;
    // Build the new form.
    $form_state = array('submitted' => FALSE);
    $form_build_id = $_POST['form_build_id'];
    // Add the new element to the stored form. Without adding the element to the
    // form, Drupal is not aware of this new elements existence and will not
    // process it. We retreive the cached form, add the element, and resave.
    $form = form_get_cache($form_build_id, $form_state);
    $choice_form = $form[ipv4s][ipv4sdialog];
    $form[ipv4s][ipv4sdialog] = $form_dialog;
    form_set_cache($form_build_id, $form, $form_state);
    $form += array('#post' => $_POST, '#programmed' => FALSE);
    // Rebuild the old form.
    $form = form_builder('guifi_device_form', $form, $form_state);
    // Render the new output.
    $choice_form = $form[ipv4s][ipv4sdialog];
    unset($choice_form['#prefix'], $choice_form['#suffix']);
    // Prevent duplicate wrappers.
    unset($choice_form[$delta]);
    // build new form
    $fs = array();
    $form_element['#post'] = array();
    $form_element = form_builder($form_element['form_id']['#value'], $form_element, $fs);
    $newfield = drupal_render($form_element);
    //  guifi_log(GUIFILOG_BASIC,sprintf('choice_form %d',$delta),htmlspecialchars($newfield));
    $output = theme('status_messages') . drupal_render($choice_form) . $newfield;
    drupal_json(array('status' => TRUE, 'data' => $output));
    exit;
}
示例#6
0
/**
 * Present the guifi zone editing form.
 */
function guifi_ipv4_form($form_state, $params = array())
{
    guifi_log(GUIFILOG_TRACE, 'guifi_ipv4_form()', $params);
    $network_types = array('public' => t('public - for any device available to everyone'), 'backbone' => t("backbone - used for internal management, links..."), 'mesh' => t('mesh - for any device in Mesh'), 'reserved' => t('reserved - used for reserved addressing'));
    // $network_types = array_merge($network_types,guifi_types('adhoc'));
    if (empty($form_state['values'])) {
        // first execution, initializing the form
        // if new network, initialize the zone
        if ($params['add']) {
            $zone_id = $params['add'];
            $zone = guifi_zone_load($zone_id);
            // if is root zone, don't find next value'
            if ($zone_id != guifi_zone_root()) {
                // not root zone, fill default values looking to next available range
                $zone = guifi_zone_load($zone_id);
                $ndefined = db_fetch_object(db_query('SELECT count(*) c FROM guifi_networks WHERE zone=%d', $zone_id));
                switch ($ndefined->c) {
                    case 0:
                        $mask = '255.255.255.0';
                        break;
                    case 1:
                        $mask = '255.255.254.0';
                        break;
                    case 2:
                        $mask = '255.255.252.0';
                        break;
                    case 3:
                        $mask = '255.255.248.0';
                        break;
                    default:
                        $mask = '255.255.240.0';
                }
                $form_state['values']['zone'] = $zone_id;
                $ips_allocated = guifi_ipcalc_get_ips('0.0.0.0', '0.0.0.0', NULL, 1);
                $network_type = 'public';
                $allocate = 'No';
                $net = guifi_ipcalc_get_subnet_by_nid($zone->master, $mask, $network_type, $ips_allocated, $allocate, TRUE);
                // verbose output
                if ($net) {
                    $item = _ipcalc($net, $mask);
                    $form_state['values']['base'] = $net;
                    $form_state['values']['mask'] = $mask;
                } else {
                    drupal_set_message(t('It was not possible to find %type space for %mask', array('%type' => $network_type, '%mask' => $mask)), 'error');
                }
            }
            // if is not the root zone
        }
        // if existent network, get the network and edit
        if ($params['edit']) {
            $form_state['values'] = db_fetch_array(db_query('SELECT *
                                                       FROM {guifi_networks}
                                                       WHERE id = %d', $params['edit']));
        }
    }
    $form['base'] = array('#type' => 'textfield', '#title' => t('Network base IPv4 address'), '#required' => TRUE, '#default_value' => $form_state['values']['base'], '#size' => 16, '#maxlength' => 16, '#description' => t('A valid base ipv4 network address.'), '#weight' => 0);
    $form['mask'] = array('#type' => 'select', '#title' => t("Mask"), '#required' => TRUE, '#default_value' => $form_state['values']['mask'], '#options' => guifi_types('netmask', 24, 0), '#description' => t('The mask of the network. The number of valid hosts of each masks is displayed in the list box.'), '#weight' => 1);
    $form['zone'] = guifi_zone_select_field($form_state['values']['zone'], 'zone');
    $form['zone']['#weight'] = 2;
    $form['network_type'] = array('#type' => 'select', '#title' => t("Network type"), '#required' => TRUE, '#default_value' => $form_state['values']['network_type'], '#options' => $network_types, '#description' => t('The type of usage that this network will be used for.'), '#weight' => 3);
    $form['submit'] = array('#type' => 'submit', '#value' => t('Submit'), '#weight' => 4);
    $form['id'] = array('#type' => 'hidden', '#value' => $form_state['values']['id'], '#weight' => 5);
    $form['valid'] = array('#type' => 'hidden', '#value' => $form_state['values']['valid'], '#weight' => 6);
    return $form;
}