function getNetUsageAJAX() { assertStringArg('net_id'); list($ip, $mask) = explode('/', $_REQUEST['net_id']); $ip_bin = ip_parse($ip); $net = spotNetworkByIP($ip_bin, $mask + 1); if (!isset($net) or $net['mask'] != $mask) { $net = constructIPRange($ip_bin, $mask); } loadIPAddrList($net); echo getRenderedIPNetCapacity($net); }
function getifTable() { $this->ifTable['ifIndex'] = $this->snmpdevice->walk('ifIndex', TRUE); $this->indexcount = count($this->ifTable['ifIndex']); $this->ifTable['ifDescr'] = $this->snmpdevice->walk('ifDescr', TRUE); $this->ifTable['ifAlias'] = $this->snmpdevice->walk('ifAlias', TRUE); $this->ifTable['ifName'] = $this->snmpdevice->walk('ifName', TRUE); $this->ifTable['ifType'] = $this->snmpdevice->walk('ifType', TRUE); $this->ifTable['ifSpeed'] = $this->snmpdevice->walk('ifSpeed', TRUE); /* notation changes when SNMP_VALUE_PLAIN is set string -> hex!! */ $this->ifTable['ifPhysAddress'] = $this->snmpdevice->walk('ifPhysAddress', TRUE); $this->ifTable['ifOperStatus'] = $this->snmpdevice->walk('ifOperStatus', TRUE); $this->ifTable['ifInOctets'] = $this->snmpdevice->walk('ifInOctets', TRUE); $this->ifTable['ifOutOctets'] = $this->snmpdevice->walk('ifOutOctets', TRUE); $this->ifTable['ifConnectorPresent'] = $this->snmpdevice->walk('ifConnectorPresent', TRUE); $this->ifTable['ipaddress'] = array(); /* ip address v4 only ipaddrtable */ $ipAdEntIfIndex = $this->snmpdevice->walk('ipAdEntIfIndex'); if (!empty($ipAdEntIfIndex)) { $ipAdEntNetMask = $this->snmpdevice->walk('ipAdEntNetMask'); /* all addresses per interface */ reset($ipAdEntNetMask); foreach ($ipAdEntIfIndex as $oid => $value) { $netmask = current($ipAdEntNetMask); next($ipAdEntNetMask); $ipaddr = preg_replace('/.*\\.([0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+)$/', '$1', $oid); $ifindex = array_search($value, $this->ifTable['ifIndex']); if ($netmask != '0.0.0.0') { $maskbits = 32 - log((ip2long($netmask) ^ 0xffffffff) + 1, 2); $net = ip2long($ipaddr) & ip2long($netmask); $bcast = $net | ip2long($netmask) ^ 0xffffffff; $this->ifTable['ipaddress'][$ifindex][$ipaddr] = array('addrtype' => 'ipv4', 'maskbits' => $maskbits, 'net' => long2ip($net), 'bcast' => long2ip($bcast)); } } unset($oid); unset($value); // sg_var_dump_html($this->ipaddress); // sg_var_dump_html($ipAdEntIfIndex); // sg_var_dump_html($ipAdEntNetMask); } /* ipadentifindex */ /* ipv4 ipv6 ipaddresstable */ /* also overwrites ipv4 from ipaddrtable */ $ipAddressIfIndex = $this->snmpdevice->walk('ipAddressIfIndex'); if (!empty($ipAddressIfIndex)) { $ipAddressPrefix = $this->snmpdevice->walk('ipAddressPrefix'); $ipAddressType = $this->snmpdevice->walk('ipAddressType'); /* 1 unicast, 2 anycast, 3 braodcast */ reset($ipAddressPrefix); reset($ipAddressType); foreach ($ipAddressIfIndex as $oid => $value) { $prefix = current($ipAddressPrefix); next($ipAddressPrefix); $type = current($ipAddressType); next($ipAddressType); if (!preg_match('/.*\\.(ipv[46]z?)\\.\\"(.*)"$/', $oid, $matches)) { continue; } /* ipv4 or ipv6 address */ $ifindex = array_search($value, $this->ifTable['ifIndex']); $net = NULL; $maskbits = NULL; $bcast = NULL; if (preg_match('/\\."([\\.0-9a-fA-F:%]+)"\\.([0-9]+)$/', $prefix, $prefixmatches)) { $net = $prefixmatches[1]; $maskbits = $prefixmatches[2]; /* ipv4 */ if ($matches[1] == 'ipv4z' || $matches[1] == 'ipv4') { $intnetmask = (int) (0xffffffff << 32 - $maskbits); $intnet = ip2long($net) & $intnetmask; $intbcast = $intnet | $intnetmask ^ 0xffffffff; $net = long2ip($intnet); $bcast = long2ip($intbcast); } } $this->ifTable['ipaddress'][$ifindex][$matches[2]] = array('addrtype' => $matches[1], 'maskbits' => $maskbits, 'net' => $net, 'bcast' => $bcast); } unset($oid); unset($value); // sg_var_dump_html($ipAddressIfIndex); // sg_var_dump_html($ipAddressPrefix); } /* ipaddressifindex */ /* ipv6 MIB */ /* overwrites ipv6 from ipaddresstable */ $ipv6interfaces = $this->snmpdevice->get('ipv6Interfaces.0'); if ($ipv6interfaces) { echo "Found {$ipv6interfaces} ipv6 interfaces<br>"; $ipv6addraddress = $this->snmpdevice->walk('ipv6AddrAddress'); if (!empty($ipv6addraddress)) { $ipv6addrpfxlength = $this->snmpdevice->walk('ipv6AddrPfxLength'); // $ipv6addrtype = $this->snmpdevice->walk('ipv6AddrType'); /* 1 stateless, 2 stateful, 3 unknown */ reset($ipv6addrpfxlength); //reset($ipv6addrtype); foreach ($ipv6addraddress as $oid => $addr_bin) { $addr = ip_format($addr_bin); //$type = current($ipv6addrtype); //next($ipv6addrtype); if (!preg_match('/.*(ipv6).*\\.([0-9]+)\\..*$/', $oid, $matches)) { continue; } $ifindex = array_search($matches[2], $this->ifTable['ifIndex']); if ($ifindex === false) { continue; } $maskbits = current($ipv6addrpfxlength); next($ipv6addrpfxlength); $range = constructIPRange($addr_bin, $maskbits); $net = ip_format($range['ip_bin']); $bcast = NULL; $this->ifTable['ipaddress'][$ifindex][$addr] = array('addrtype' => $matches[1], 'maskbits' => $maskbits, 'net' => $net, 'bcast' => $bcast); } unset($oid); unset($value); } /* ipv6addraddress */ } /* ipv6interfaces */ }
function snmpgeneric_list($object_id) { global $sg_create_noconnector_ports, $sg_known_sysObjectIDs, $sg_portoifoptions, $sg_ifType_ignore; if (isset($_POST['snmpconfig'])) { $snmpconfig = $_POST; } else { showError("Missing SNMP Config"); return; } // sg_var_dump_html($snmpconfig); echo '<body onload="document.getElementById(\'createbutton\').focus();">'; addJS('function setchecked(classname) { var boxes = document.getElementsByClassName(classname); var value = document.getElementById(classname).checked; for(i=0;i<boxes.length;i++) { if(boxes[i].disabled == false) boxes[i].checked=value; } };', TRUE); $object = spotEntity('object', $object_id); $object['attr'] = getAttrValues($object_id); $snmpdev = new mySNMP($snmpconfig['version'], $snmpconfig['host'], $snmpconfig['community']); if ($snmpconfig['version'] == "v3") { $snmpdev->setSecurity($snmpconfig['sec_level'], $snmpconfig['auth_protocol'], $snmpconfig['auth_passphrase'], $snmpconfig['priv_protocol'], $snmpconfig['priv_passphrase']); } $snmpdev->init(); if ($snmpdev->getErrno()) { showError($snmpdev->getError()); return; } /* SNMP connect successfull */ showSuccess("SNMP " . $snmpconfig['version'] . " connect to {$snmpconfig['host']} successfull"); echo '<form name=CreatePorts method=post action=' . $_SERVER['REQUEST_URI'] . '&module=redirect&op=create>'; echo "<strong>System Informations</strong>"; echo "<table>"; // echo "<tr><th>OID</th><th>Value</th></tr>"; $systemoids = array('sysDescr', 'sysObjectID', 'sysUpTime', 'sysContact', 'sysName', 'sysLocation'); foreach ($systemoids as $shortoid) { $value = $snmpdev->{$shortoid}; if ($shortoid == 'sysUpTime') { /* in hundredths of a second */ $secs = (int) ($value / 100); $days = (int) ($secs / (60 * 60 * 24)); $secs -= $days * 60 * 60 * 24; $hours = (int) ($secs / (60 * 60)); $secs -= $hours * 60 * 60; $mins = (int) ($secs / 60); $secs -= $mins * 60; $value = "{$value} ({$days} {$hours}:{$mins}:{$secs})"; } echo "<tr><td title=\"" . $snmpdev->lastgetoid . "\" align=\"right\">{$shortoid}: </td><td>{$value}</td></tr>"; } unset($shortoid); echo "</table>"; /* sysObjectID Attributes and Ports */ $sysObjectID['object'] =& $object; /* get sysObjectID */ $sysObjectID['raw_value'] = $snmpdev->sysObjectID; //$sysObjectID['raw_value'] = 'NET-SNMP-MIB::netSnmpAgentOIDs.10'; $sysObjectID['value'] = preg_replace('/^.*enterprises\\.([\\.[:digit:]]+)$/', '\\1', $sysObjectID['raw_value']); /* try snmptranslate to numeric */ if (preg_match('/[^\\.0-9]+/', $sysObjectID['value'])) { $numeric_value = $snmpdev->translatetonumeric($sysObjectID['value']); if (!empty($numeric_value)) { showSuccess("sysObjectID: " . $sysObjectID['value'] . " translated to {$numeric_value}"); $sysObjectID['value'] = preg_replace('/^.1.3.6.1.4.1.([\\.[:digit:]]+)$/', '\\1', $numeric_value); } } /* array_merge doesn't work with numeric keys !! */ $sysObjectID['attr'] = array(); $sysObjectID['port'] = array(); $sysobjid = $sysObjectID['value']; $count = 1; while ($count) { if (isset($sg_known_sysObjectIDs[$sysobjid])) { $sysObjectID = $sysObjectID + $sg_known_sysObjectIDs[$sysobjid]; if (isset($sg_known_sysObjectIDs[$sysobjid]['attr'])) { $sysObjectID['attr'] = $sysObjectID['attr'] + $sg_known_sysObjectIDs[$sysobjid]['attr']; } if (isset($sg_known_sysObjectIDs[$sysobjid]['port'])) { $sysObjectID['port'] = $sysObjectID['port'] + $sg_known_sysObjectIDs[$sysobjid]['port']; } if (isset($sg_known_sysObjectIDs[$sysobjid]['text'])) { showSuccess("found sysObjectID ({$sysobjid}) " . $sg_known_sysObjectIDs[$sysobjid]['text']); } } $sysobjid = preg_replace('/\\.[[:digit:]]+$/', '', $sysobjid, 1, $count); /* add default sysobjectid */ if ($count == 0 && $sysobjid != 'default') { $sysobjid = 'default'; $count = 1; } } $sysObjectID['vendor_number'] = $sysobjid; /* device pf */ if (isset($sysObjectID['pf'])) { foreach ($sysObjectID['pf'] as $function) { if (function_exists($function)) { /* call device pf */ $function($snmpdev, $sysObjectID, NULL); } else { showWarning("Missing processor function " . $function . " for device {$sysobjid}"); } } } /* sort attributes maintain numeric keys */ ksort($sysObjectID['attr']); /* DEBUG */ //sg_var_dump_html($sysObjectID['attr'], "Before processing"); /* needs PHP >= 5 foreach call by reference */ /* php 5.1.6 doesn't seem to work */ //foreach($sysObjectID['attr'] as $attr_id => &$attr) foreach ($sysObjectID['attr'] as $attr_id => $value) { $attr =& $sysObjectID['attr'][$attr_id]; if (isset($object['attr'][$attr_id])) { if (array_key_exists('key', $object['attr'][$attr_id])) { $attr['key'] = $object['attr'][$attr_id]['key']; } switch (TRUE) { case isset($attr['pf']): if (function_exists($attr['pf'])) { $attr['pf']($snmpdev, $sysObjectID, $attr_id); } else { showWarning("Missing processor function " . $attr['pf'] . " for attribute {$attr_id}"); } break; case isset($attr['oid']): $attrvalue = $snmpdev->get($attr['oid']); if (isset($attr['regex'])) { $regex = $attr['regex']; if (isset($attr['replacement'])) { $replacement = $attr['replacement']; $attrvalue = preg_replace($regex, $replacement, $attrvalue); } else { if (!preg_match($regex, $attrvalue)) { if (!isset($attr['uncheck'])) { $attr['uncheck'] = "regex doesn't match"; } } else { unset($attr['uncheck']); } } } $attr['value'] = $attrvalue; break; case isset($attr['value']): break; default: showError("Error handling attribute id: {$attr_id}"); } } else { showWarning("Object has no attribute id: {$attr_id}"); unset($sysObjectID['attr'][$attr_id]); } } unset($attr_id); /* sort again in case there where attribs added ,maintain numeric keys */ ksort($sysObjectID['attr']); /* print attributes */ echo '<br>Attributes<br><table>'; echo '<tr><th><input type="checkbox" id="attribute" checked="checked" onclick="setchecked(this.id)"></td>'; echo '<th>Name</th><th>Current Value</th><th>new value</th></tr>'; /* DEBUG */ //sg_var_dump_html($sysObjectID['attr'], "After processing"); foreach ($sysObjectID['attr'] as $attr_id => &$attr) { $attr['id'] = $attr_id; if (isset($object['attr'][$attr_id]) && isset($attr['value'])) { if ($attr['value'] == $object['attr'][$attr_id]['value']) { $attr['uncheck'] = 'Current = new value'; } if (isset($attr['key']) && isset($object['attr'][$attr_id]['key'])) { if ($attr['key'] == $object['attr'][$attr_id]['key']) { $attr['uncheck'] = 'Current = new key'; } } $value = $attr['value']; $val_key = isset($object['attr'][$attr_id]['key']) ? ' (' . $object['attr'][$attr_id]['key'] . ')' : ''; $comment = ''; if (isset($attr['comment'])) { if (!empty($attr['comment'])) { $comment = $attr['comment']; } } if (isset($attr['uncheck'])) { $checked = ''; $comment .= ', ' . $attr['uncheck']; } else { $checked = ' checked="checked"'; } $updateattrcheckbox = '<b style="background-color:#00ff00">' . '<input style="background-color:#00ff00" class="attribute" type="checkbox" name="updateattr[' . $attr_id . ']" value="' . $value . '"' . $checked . '></b>'; $comment = trim($comment, ', '); echo "<tr><td>{$updateattrcheckbox}</td><td title=\"id: {$attr_id}\">" . $object['attr'][$attr_id]['name'] . '</td><td style="background-color:#d8d8d8">' . $object['attr'][$attr_id]['value'] . $val_key . '</td><td>' . $value . '</td>' . '<td style="color:#888888">' . $comment . '</td></tr>'; } } unset($attr_id); echo '</table>'; $object['breed'] = sg_detectDeviceBreedByObject($sysObjectID); if (!empty($object['breed'])) { echo "Found Breed: " . $object['breed'] . "<br>"; } /* ports */ /* get ports */ amplifyCell($object); /* set array key to lowercase port name */ foreach ($object['ports'] as $key => $values) { $object['ports'][strtolower(shortenIfName($values['name'], $object['breed']))] = $values; unset($object['ports'][$key]); } $newporttypeoptions = getNewPortTypeOptions(); // sg_var_dump_html($sysObjectID['port']); if (!empty($sysObjectID['port'])) { echo '<br>Vendor / Device specific ports<br>'; echo '<table><tr><th><input type="checkbox" id="moreport" checked="checked" onclick="setchecked(this.id)"></th><th>ifName</th><th>porttypeid</th></tr>'; foreach ($sysObjectID['port'] as $name => $port) { if (array_key_exists(strtolower($name), $object['ports'])) { $disableport = TRUE; } else { $disableport = FALSE; } $comment = ''; if (isset($port['comment'])) { if (!empty($port['comment'])) { $comment = $port['comment']; } } if (isset($port['uncheck'])) { $checked = ''; $comment .= ', ' . $port['uncheck']; } else { $checked = ' checked="checked"'; } $portcreatecheckbox = '<b style="background-color:' . ($disableport ? '#ff0000' : '#00ff00') . '"><input style="background-color:' . ($disableport ? '#ff0000' : '#00ff00') . '" class="moreport" type="checkbox" name="portcreate[' . $name . ']" value="' . $name . '"' . ($disableport ? ' disabled="disbaled"' : $checked) . '></b>'; $formfield = '<input type="hidden" name="ifName[' . $name . ']" value="' . $name . '">'; echo "<tr>{$formfield}<td>{$portcreatecheckbox}</td><td>{$name}</td>"; if (isset($port['disabled'])) { $disabledselect = array('disabled' => "disabled"); } else { $disabledselect = array(); } foreach ($port as $key => $value) { if ($key == 'uncheck' || $key == 'comment') { continue; } /* TODO iif_name */ if ($key == 'porttypeid') { $displayvalue = getNiftySelect($newporttypeoptions, array('name' => "porttypeid[{$name}]") + $disabledselect, $value); } else { $displayvalue = $value; } $formfield = '<input type="hidden" name="' . $key . '[' . $name . ']" value="' . $value . '">'; echo "{$formfield}<td>{$displayvalue}</td>"; } $comment = trim($comment, ', '); echo "<td style=\"color:#888888\">{$comment}</td></tr>"; } unset($name); unset($port); echo '</table>'; } /* snmp ports */ $ifsnmp = new ifSNMP($snmpdev); // needed for shortenIfName() $ifsnmp->object_breed = $object['breed']; /* ip spaces */ $ipspace = NULL; foreach ($ifsnmp->ipaddress as $ifindex => $ipaddresses) { foreach ($ipaddresses as $ipaddr => $value) { $addrtype = $value['addrtype']; $netaddr = $value['net']; $maskbits = $value['maskbits']; $netid = NULL; $linklocal = FALSE; //echo "<br> - DEBUG: ipspace $ipaddr - $netaddr - $addrtype - $maskbits<br>"; /* check for ip space */ switch ($addrtype) { case 'ipv4': case 'ipv4z': if ($maskbits == 32) { $netid = 'host'; } else { $netid = getIPv4AddressNetworkId(ip_parse($ipaddr)); } break; case 'ipv6': /* format ipaddr for ip6_parse */ $ipaddr = preg_replace('/((..):(..))/', '\\2\\3', $ipaddr); $ipaddr = preg_replace('/%.*$/', '', $ipaddr); if (ip_checkparse($ipaddr) === false) { continue 2; } // 2 because of switch $ip6_bin = ip6_parse($ipaddr); $ip6_addr = ip_format($ip6_bin); $netid = getIPv6AddressNetworkId($ip6_bin); $node = constructIPRange($ip6_bin, $maskbits); $netaddr = $node['ip']; $linklocal = substr($ip6_addr, 0, 5) == "fe80:"; //echo "<br> - DEBUG: ipspace $ipaddr - $addrtype - $maskbits - $netaddr - >$linklocal<<br>"; break; case 'ipv6z': /* link local */ $netid = 'ignore'; break; default: } if (empty($netid) && $netaddr != '::1' && $netaddr != '127.0.0.1' && $netaddr != '127.0.0.0' && $netaddr != '0.0.0.0' && !$linklocal) { $netaddr .= "/{$maskbits}"; $ipspace[$netaddr] = array('addrtype' => $addrtype, 'checked' => $maskbits > 0 ? true : false); } } unset($ipaddr); unset($value); unset($addrtype); } unset($ifindex); unset($ipaddresses); /* print ip spaces table */ if (!empty($ipspace)) { echo '<br><br>Create IP Spaces'; echo '<table><tr><th><input type="checkbox" id="ipspace" onclick="setchecked(this.id)" checked=\\"checked\\"></th>'; echo '<th>Type</th><th>prefix</th><th>name</th><th width=150 title="reserve network and router addresses">reserve network / router addresses</th></tr>'; $i = 1; foreach ($ipspace as $prefix => $ipspace) { $netcreatecheckbox = '<b style="background-color:#00ff00">' . '<input class="ipspace" style="background-color:#00ff00" type="checkbox" name="netcreate[' . $i . ']" value="' . $ipspace['addrtype'] . '"' . ($ipspace['checked'] ? ' checked=\\"checked\\"' : '') . '></b>'; $netprefixfield = '<input type="text" size=50 name="netprefix[' . $i . ']" value="' . $prefix . '">'; $netnamefield = '<input type="text" name="netname[' . $i . ']">'; $netreservecheckbox = '<input type="checkbox" name="netreserve[' . $i . ']" checked="checked">'; echo "<tr><td>{$netcreatecheckbox}</td><td style=\"color:#888888\">{$ipspace['addrtype']}</td><td>{$netprefixfield}</td><td>{$netnamefield}</td><td>{$netreservecheckbox}</td></tr>"; $i++; } unset($prefix); unset($addrtype); unset($i); echo '</table>'; } echo "<br><br>ifNumber: " . $ifsnmp->ifNumber . "<br>indexcount: " . $ifsnmp->indexcount . "<br><table><tbody valign=\"top\">"; $portcompat = getPortInterfaceCompat(); $ipnets = array(); $ifsnmp->printifInfoTableHeader("<th>add ip</th><th>add port</th><th>upd label</th><th title=\"update mac\">upd mac</th><td>upd port type</th><th>porttypeid</th><th>comment</th></tr>"); echo '<tr><td colspan="11"></td> <td><input type="checkbox" id="ipaddr" onclick="setchecked(this.id)">IPv4<br> <input type="checkbox" id="ipv6addr" onclick="setchecked(this.id)">IPv6</td> <td><input type="checkbox" id="ports" onclick="setchecked(this.id)"></td> <td><input type="checkbox" id="label" onclick="setchecked(this.id)" checked="checked"></td> <td><input type="checkbox" id="mac" onclick="setchecked(this.id)" checked="checked"></td> <td><input type="checkbox" id="porttype" onclick="setchecked(this.id);"></td></tr>'; foreach ($ifsnmp as $if) { $createport = TRUE; $disableport = FALSE; $ignoreport = FALSE; $port_info = NULL; $updatelabel = false; $updateporttype = false; $updatemaccheckbox = ''; $hrefs = array(); $comment = ""; if (trim($ifsnmp->ifName($if)) == '') { $comment .= "no ifName"; $createport = FALSE; } else { if (array_key_exists($ifsnmp->ifName($if), $object['ports'])) { $port_info =& $object['ports'][$ifsnmp->ifName($if)]; $comment .= "Name exists"; /* ifalias change */ if ($port_info['label'] != $ifsnmp->ifAlias($if)) { $updatelabel = true; } $createport = FALSE; $disableport = TRUE; } } if ($ifsnmp->ifPhysAddress($if) != '') { $ifPhysAddress = $ifsnmp->ifPhysAddress($if); $l2port = sg_checkL2Address($ifPhysAddress); if (!empty($l2port)) { $l2object_id = key($l2port); $porthref = makeHref(array('page' => 'object', 'tab' => 'ports', 'object_id' => $l2object_id, 'hl_port_id' => $l2port[$l2object_id])); $comment .= ", L2Address exists"; $hrefs['ifPhysAddress'] = $porthref; $createport = FALSE; // $disableport = TRUE; $updatemaccheckbox = ''; } $disablemac = true; if ($disableport) { if ($port_info !== NULL) { if (str_replace(':', '', $port_info['l2address']) != $ifPhysAddress) { $disablemac = false; } else { $disablemac = true; } } } else { /* port create always updates mac */ $updatemaccheckbox = '<b style="background-color:#00ff00">' . '<input style="background-color:' . '#00ff00" type="checkbox"' . ' checked="checked"' . ' disabled=\\"disabled\\"></b>'; } if (!$disablemac) { $updatemaccheckbox = '<b style="background-color:' . ($disablemac ? '#ff0000' : '#00ff00') . '">' . '<input class="mac" style="background-color:' . ($disablemac ? '#ff0000' : '#00ff00') . '" type="checkbox" name="updatemac[' . $if . ']" value="' . $object['ports'][$ifsnmp->ifName($if)]['id'] . '" checked="checked"' . ($disablemac ? ' disabled=\\"disabled\\"' : '') . '></b>'; } } $porttypeid = guessRToif_id($ifsnmp->ifType($if), $ifsnmp->ifDescr($if)); if (in_array($ifsnmp->ifType($if), $sg_ifType_ignore)) { $comment .= ", ignore if type"; $createport = FALSE; $ignoreport = TRUE; } else { if ($port_info) { $ptid = $port_info['iif_id'] . "-" . $port_info['oif_id']; if ($porttypeid != $ptid) { $comment .= ", Update Type {$ptid} -> {$porttypeid}"; $updateporttype = true; } } } /* ignore ports without an Connector */ if (!$sg_create_noconnector_ports && $ifsnmp->ifConnectorPresent($if) == 2) { $comment .= ", no Connector"; $createport = FALSE; } /* Allocate IPs ipv4 and ipv6 */ $ipaddresses = $ifsnmp->ipaddress($if); if (!empty($ipaddresses)) { $ipaddrcell = '<table>'; foreach ($ipaddresses as $ipaddr => $value) { $createipaddr = FALSE; $disableipaddr = FALSE; $ipaddrhref = ''; $linklocal = FALSE; $addrtype = $value['addrtype']; $maskbits = $value['maskbits']; $bcast = $value['bcast']; //echo "<br> - DEBUG: ip $ipaddr - $addrtype - $maskbits - $bcast<br>"; switch ($addrtype) { case 'ipv4z': case 'ipv4': if ($maskbits == 32) { $bcast = "host"; } $inputname = 'ip'; break; case 'ipv6z': $disableipaddr = TRUE; case 'ipv6': $inputname = 'ipv6'; /* format ipaddr for ip6_parse */ $ipaddr = preg_replace('/((..):(..))/', '\\2\\3', $ipaddr); $ipaddr = preg_replace('/%.*$/', '', $ipaddr); if (ip_checkparse($ipaddr) === false) { continue 2; } // 2 because of switch /* ip_parse throws exception on parse errors */ $ip6_bin = ip_parse($ipaddr); $ipaddr = ip_format($ip6_bin); $node = constructIPRange($ip6_bin, $maskbits); $linklocal = $node['ip'] == 'fe80::'; $createipaddr = FALSE; break; } //switch $address = getIPAddress(ip_parse($ipaddr)); /* only if ip not already allocated */ if (empty($address['allocs'])) { if (!$ignoreport) { $createipaddr = TRUE; } } else { $disableipaddr = TRUE; $ipobject_id = $address['allocs'][0]['object_id']; $ipaddrhref = makeHref(array('page' => 'object', 'object_id' => $ipobject_id, 'hl_ipv4_addr' => $ipaddr)); } /* reserved addresses */ if ($address['reserved'] == 'yes') { $comment .= ', ' . $address['ip'] . ' reserved ' . $address['name']; $createipaddr = FALSE; // $disableipaddr = TRUE; } if ($ipaddr == '127.0.0.1' || $ipaddr == '0.0.0.0' || $ipaddr == '::1' || $ipaddr == '::' || $linklocal) { $createipaddr = FALSE; $disableipaddr = TRUE; } if ($ipaddr === $bcast) { $comment .= ", {$ipaddr} broadcast"; $createipaddr = FALSE; $disableipaddr = TRUE; } if (!$disableipaddr) { $ipaddrcheckbox = '<b style="background-color:' . ($disableipaddr ? '#ff0000' : '#00ff00') . '"><input class="' . $inputname . 'addr" style="background-color:' . ($disableipaddr ? '#ff0000' : '#00ff00') . '" type="checkbox" name="' . $inputname . 'addrcreate[' . $ipaddr . ']" value="' . $if . '"' . ($disableipaddr ? ' disabled="disabled"' : '') . ($createipaddr ? ' checked="checked"' : '') . '></b>'; } else { $ipaddrcheckbox = ''; } $ipaddrcell .= "<tr><td>{$ipaddrcheckbox}</td>"; if (!empty($ipaddrhref)) { $ipaddrcell .= "<td><a href={$ipaddrhref}>{$ipaddr}/{$maskbits}</a></td></tr>"; } else { $ipaddrcell .= "<td>{$ipaddr}/{$maskbits}</td></tr>"; } } // foreach unset($ipaddr); unset($value); $ipaddrcell .= '</table>'; // if(!empty($ipaddresses)) } else { $ipaddrcreatecheckbox = ''; $ipaddrcell = ''; } /* checkboxes for add port and add ip */ /* FireFox needs <b style=..>, IE and Opera work with <td style=..> */ if (!$disableport) { $portcreatecheckbox = '<b style="background-color:' . ($disableport ? '#ff0000' : '#00ff00') . '"><input class="ports" style="background-color:' . ($disableport ? '#ff0000' : '#00ff00') . '" type="checkbox" name="portcreate[' . $if . ']" value="' . $if . '"' . ($disableport ? ' disabled="disbaled"' : '') . ($createport ? ' checked="checked"' : '') . '></b>'; } else { $portcreatecheckbox = ''; } /* port type id */ /* add port type to newporttypeoptions if missing */ if (strpos(serialize($newporttypeoptions), $porttypeid) === FALSE) { $portids = explode('-', $porttypeid); $oif_name = $sg_portoifoptions[$portids[1]]; $newporttypeoptions['auto'] = array($porttypeid => "*{$oif_name}"); } $selectoptions = array('name' => "porttypeid[{$if}]"); if ($disableport && !$updateporttype) { $selectoptions['disabled'] = "disabled"; } $updateporttypecheckbox = ""; if ($updateporttype) { $updateporttypecheckbox = '<b style="background-color:#00ff00;">' . '<input class="porttype" style="background-color:#00ff00;" type="checkbox" name="updateporttype[' . $if . ']" value="' . $port_info['id'] . '"></b>'; } $porttypeidselect = getNiftySelect($newporttypeoptions, $selectoptions, $porttypeid); $updatelabelcheckbox = ""; if ($updatelabel) { $updatelabelcheckbox = '<b style="background-color:#00ff00;">' . '<input class="label" style="background-color:#00ff00;" type="checkbox" name="updatelabel[' . $if . ']" value="' . $port_info['id'] . ($updatelabel ? '" checked="checked"' : '') . '></b>'; } $comment = trim($comment, ', '); $ifsnmp->printifInfoTableRow($if, "<td>{$ipaddrcell}</td><td>{$portcreatecheckbox}</td><td>{$updatelabelcheckbox}</td><td>{$updatemaccheckbox}</td><td>{$updateporttypecheckbox}</td><td>{$porttypeidselect}</td><td nowrap=\"nowrap\">{$comment}</td>", $hrefs); } unset($if); /* preserve snmpconfig */ foreach ($_POST as $key => $value) { echo '<input type=hidden name=' . $key . ' value=' . $value . ' />'; } unset($key); unset($value); echo '<tr><td colspan=15 align="right"><p><input id="createbutton" type=submit value="Create Ports and IPs" onclick="return confirm(\'Create selected items?\')"></p></td></tr></tbody></table></form>'; }
function createIPv6Prefix($range = '', $name = '', $is_connected = FALSE, $taglist = array(), $vlan_ck = NULL) { // $range is in aaa0:b::c:d/x format, split into ip/mask vars $rangeArray = explode('/', $range); if (count($rangeArray) != 2) { throw new InvalidRequestArgException('range', $range, 'Invalid IPv6 prefix'); } $ip = $rangeArray[0]; $mask = $rangeArray[1]; $net = constructIPRange(ip6_parse($ip), $mask); usePreparedInsertBlade('IPv6Network', array('ip' => $net['ip_bin'], 'last_ip' => ip_last($net), 'mask' => $mask, 'name' => $name)); $network_id = lastInsertID(); # RFC3513 2.6.1 - Subnet-Router anycast if ($is_connected) { updateV6Address($net['ip_bin'], 'Subnet-Router anycast', 'yes'); } produceTagsForNewRecord('ipv6net', $taglist, $network_id); if ($vlan_ck != NULL) { $ctx = getContext(); fixContext(spotEntity('ipv6net', $network_id)); if (permitted('ipv6net', '8021q', 'bind')) { commitSupplementVLANIPv6($vlan_ck, $network_id); } restoreContext($ctx); } return $network_id; }
function loadIPAddrList(&$node) { $node['addrlist'] = scanIPSpace(array(array('first' => $node['ip_bin'], 'last' => ip_last($node)))); if (!isset($node['id'])) { $node['own_addrlist'] = $node['addrlist']; } else { if ($node['kidc'] == 0) { $node['own_addrlist'] = $node['addrlist']; } else { $node['own_addrlist'] = array(); // node has childs foreach ($node['spare_ranges'] as $mask => $spare_list) { foreach ($spare_list as $spare_ip) { $spare_range = constructIPRange($spare_ip, $mask); foreach ($node['addrlist'] as $bin_ip => $addr) { if (($bin_ip & $spare_range['mask_bin']) == $spare_range['ip_bin']) { $node['own_addrlist'][$bin_ip] = $addr; } } } } } } $node['addrc'] = count($node['addrlist']); $node['own_addrc'] = count($node['own_addrlist']); }