function guifi_interfaces_add_cable_public_link_submit(&$form, &$form_state) { $values = $form_state['clicked_button']['#parents']; $iid = $values[1]; $ipv4_id = $values[3]; $to_did = $form_state['values']['interfaces'][$iid]['ipv4'][$ipv4_id]['to_did']; $rdevice = guifi_device_load($to_did); guifi_log(GUIFILOG_TRACE, sprintf('function guifi_interfaces_add_cable_public_link_submit(%d)', $iid), $form_state['clicked_button']['#parents']); $ips_allocated = guifi_ipcalc_get_ips('0.0.0.0', '0.0.0.0', $form_state['values'], 1); // get next available ip address $base_ip = $form_state['values']['interfaces'][$iid]['ipv4'][$ipv4_id]; $item = _ipcalc($base_ip['ipv4'], $base_ip['netmask']); $ip = guifi_ipcalc_find_ip($item['netid'], $base_ip['netmask'], $ips_allocated); // no IP was given, so raise a message and don't create the link' if (empty($ip)) { drupal_set_message(t('Unable to assign a free ip, link not created, ' . 'contact the administrator.')); return; } $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 ($rdevice['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']['ipv4']['new'] = TRUE; $newlk['interface']['ipv4']['ipv4_type'] = 1; $newlk['interface']['ipv4']['ipv4'] = $ip; $newlk['interface']['ipv4']['netmask'] = $base_ip['netmask']; $form_state['values']['interfaces'][$iid]['ipv4'][$ipv4_id]['links'][] = $newlk; $form_state['values']['interfaces'][$iid]['unfold'] = TRUE; // print_r($form_state['values']); $form_state['rebuild'] = TRUE; return TRUE; }
function guifi_radio_add_wds_submit(&$form, &$form_state) { $radio_id = $form_state['clicked_button']['#parents'][1]; $interface_id = $form_state['clicked_button']['#parents'][3]; guifi_log(GUIFILOG_TRACE, sprintf("function guifi_radio_add_wds(Radio: %d, Interface: %d)", $radio_id, $interface_id), $form_state['clicked_button']['#parents']); $form_state['rebuild'] = TRUE; $form_state['action'] = 'guifi_radio_add_wds_form'; // get list of the current used ips $ips_allocated = guifi_ipcalc_get_ips('0.0.0.0', '0.0.0.0', $form_state['values'], 2); $newif = _guifi_radio_add_wds_get_new_interface($form_state['values']['nid'], $ips_allocated); if ($newif == -1) { drupal_set_message(t('Unable to create link, no networks available'), 'warning'); return FALSE; } // Initialize filters $form_state['filters'] = array('dmin' => 0, 'dmax' => 15, 'search' => NULL, 'type' => 'wds', 'mode' => $form_state['values']['radios'][$radio_id]['mode'], 'from_node' => $form_state['values']['nid'], 'from_device' => $form_state['values']['id'], 'from_radio' => $radio_id, 'azimuth' => "0,360"); $form_state['newInterface'][$interface_id]['ipv4'][] = $newif; return TRUE; }
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); } }
function guifi_ipcalc_find_ip($base_ip = '0.0.0.0', $mask_range = '0.0.0.0', $ips_allocated = NULL, $verbose = true) { if ($ips_allocated == NULL) { $ips_allocated = guifi_ipcalc_get_ips($base_ip, $mask_range); } $ip_dec = ip2long($base_ip) + 1; $item = _ipcalc($base_ip, $mask_range); $end_dec = ip2long($item['broadcast']); $key = $ip_dec; while (isset($ips_allocated[$key]) and $key < $end_dec) { $key++; } if ($key < $end_dec) { return long2ip($key); } $ipc = _ipcalc($base_ip, $mask_range); if ($verbose) { drupal_set_message(t('Network %net/%mask is full', array('%net' => $base_ip, '%mask' => $ipc[maskbits])), 'warning'); } return FALSE; }
/** * 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>'); }
function guifi_api_cloudy_addlink($gapi, $parameters) { if (!guifi_api_check_fields($gapi, array('device_id', 'cloudy_id'), $parameters)) { return FALSE; } /* User allow changes ? */ $device = guifi_device_load($parameters['device_id']); if (!guifi_device_access('update', $device)) { $gapi->addError(501); return FALSE; } /* Exist ? */ if (!$device['id']) { $gapi->addError(500, "device not found: {$parameters['device_id']}"); return FALSE; } /* User allow changes ? */ $cloudy = guifi_device_load($parameters['cloudy_id']); if (!guifi_device_access('update', $cloudy)) { $gapi->addError(501); return FALSE; } /* Exist ? */ if (!$cloudy['id']) { $gapi->addError(500, "device not found: {$parameters['cloudy_id']}"); return FALSE; } $ipv4_id = 0; /* Si no hi ha interface sel·leccionada busquem una que tingui IPv4 */ foreach ($device['interfaces'] as $iid => $if) { if (is_array($if['ipv4'])) { break; } } if (!is_array($if['ipv4'])) { $gapi->addError(500, "Device did not assign IPv4."); } /* Calcular IP */ /* Coses que no m'agraden a. El 0 posat a ipv4, s'hauria de calcular. b. interface_type, tb sembla que es contabilitza */ $ips_allocated = guifi_ipcalc_get_ips('0.0.0.0', '0.0.0.0', $device, 1); $base_ip = $device['interfaces'][$iid]['ipv4'][$ipv4_id]; $item = _ipcalc($base_ip['ipv4'], $base_ip['netmask']); $ip = guifi_ipcalc_find_ip($item['netid'], $base_ip['netmask'], $ips_allocated); /* Creem l'interficie al Cloudy. És totalment necessari? */ $newLink = array("new" => TRUE, "interface" => array("new" => TRUE, "device_id" => $cloudy['id'], "interface_type" => "Lan", "ipv4" => array("new" => TRUE, "ipv4_type" => 1, "ipv4" => $ip, "netmask" => $base_ip['netmask'])), "id" => -1, "link_type" => "cable", "flag" => "Planned", "nid" => $cloudy['nid'], "device_id" => $cloudy['id'], "routing" => "Gateway"); $device['interfaces'][$iid]['ipv4'][$ipv4_id]['links'][] = $newLink; $device['interfaces'][$iid]['unfold'] = TRUE; guifi_device_save($device); $gapi->addResponseField('device', $device); return TRUE; }
/** * 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; }
/** * 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; }
/** guifi_zone_networks(): outputs the zone networks **/ function theme_guifi_zone_networks($zone) { drupal_set_breadcrumb(guifi_zone_ariadna($zone->id, 'node/%d/view/ipv4')); $zone = node_load(array('nid' => $zone->id)); $ips_allocated = guifi_ipcalc_get_ips(); if (user_access('administer guifi networks')) { $output = l(t('add network'), 'node/' . $zone->id . '/view/ipv4/add'); } // zone & parents $table = guifi_ipv4_print_data($zone, 'parents', $ips_allocated); $output .= theme('box', t('zone and zone parent(s) network allocation(s)'), $table); // zone childs $table = guifi_ipv4_print_data($zone, 'childs', $ips_allocated); $output .= theme('box', t('zone child(s) network allocation(s)'), $table); $output .= theme_links(module_invoke_all('link', 'node', $zone, FALSE)); print theme('page', $output, FALSE); return; }