📄 setup_network.php
字号:
IP Address </th> <th> Netmask </th> </tr>INTERFACE_HEADER; $footerHtml = <<< INTERFACE_FOOTER <p> <input type="submit" value="Update"> </p> </form> </fieldset>INTERFACE_FOOTER; $interfacesHtml = ""; foreach ($kinterfaces as $interface) { $interface->isEditable = ($interface->name != "lo"); $nameHtml = "<td> $interface->name </td>"; $enabledHtml = gen_enabled_html($interface); $staticHtml = gen_static_html($interface); $addrHtml = gen_addr_html($interface, $textSize); $maskHtml = gen_mask_html($interface, $textSize); $hiddenGatewayHtml = gen_hidden_gateway_html($gateway); $interfacesHtml .= "<tr>" . $nameHtml . $enabledHtml . $staticHtml . $addrHtml . $maskHtml . "</tr>"; } $errorHtml = ""; if (count($errorMsgs) > 0) { $errorHtml .= "<p id=error>"; foreach ($errorMsgs as $msg) { $errorHtml .= htmlentities($msg) . "<br>"; } $errorHtml .= "</p>"; } return $headerHtml . $interfacesHtml . "</table>" . "\n" . $errorHtml . $hiddenGatewayHtml . "\n" . $footerHtml;}function gen_html_network_global($globalNetworkConfig, $errorMsgs=array()) { /* The maximum string length imposed by the daemon is 200, including the * terminating NULL char. */ $textSize = max(strlen($globalNetworkConfig->hostname), strlen($globalNetworkConfig->gateway), strlen($globalNetworkConfig->primaryDNS), strlen($globalNetworkConfig->secondaryDNS)); $textSize = min($textSize, 200); $headerHtml = "<fieldset> <legend>Hostname, Gateway and DNS</legend> <form action=setup_network.php method=post> <table>\n"; $hostNameHtml = "<tr> <th> Hostname </th> <td> <input type=text name=Hostname size=$textSize value=\"$globalNetworkConfig->hostname\" /> </td> </tr>\n"; $gatewayHtml = "<tr> <th> Default Gateway </th> <td> <input type=text name=Gateway size=$textSize value=\"$globalNetworkConfig->gateway\" /> </td> </tr>\n"; $primaryDnsHtml = "<tr> <th> Primary DNS </th> <td> <input type=text name=PrimaryDNS size=$textSize value=\"$globalNetworkConfig->primaryDNS\" /> </td>\n"; $secondaryDnsHtml = "</tr> <tr> <th> Secondary DNS </th> <td> <input type=text name=SecondaryDNS size=$textSize value=\"$globalNetworkConfig->secondaryDNS\" /> </td> </tr>\n"; $footerHtml = "<p> <input type=submit value=Update> </p> </form> </fieldset>\n"; $errorHtml = ""; if (count($errorMsgs) > 0) { $errorHtml .= "<p id=error>"; foreach ($errorMsgs as $msg) { $errorHtml .= htmlentities($msg) . "<br>"; } $errorHtml .= "</p>"; } return $headerHtml . $hostNameHtml . $gatewayHtml . $primaryDnsHtml . $secondaryDnsHtml . "</table>" . $errorHtml . $footerHtml; }/* Returns an array of NetInterfaces. * Don't modify the list of incoming interfaces. Use these just for the inteface names. * Return NULL if there was no user input or the user input was incomplete. */function read_ui_interfaces($infs) { $retInfs = array(); $foundUserInput = false; $params = read_params(); // XXX overuse of array_key_exists(). perhaps use empty()? foreach ($infs as $key => $inf) { $name = $inf->name; if (array_key_exists("Address_" . $name, $params) && array_key_exists("Netmask_" . $name, $params) && array_key_exists("Enabled_" . $name, $params) && array_key_exists("Dhcp_" . $name, $params)) { $foundUserInput = true; $retInfs[$key] = new NetInterface($name, $inf->isEnabled, $inf->isDhcp, $inf->ipAddress, $inf->netmask); // XXX refactor. we've already checked that the key exists. if (array_key_exists("Address_" . $name, $params)) $retInfs[$key]->ipAddress = $params["Address_" . $name]; if (array_key_exists("Netmask_" . $name, $params)) $retInfs[$key]->netmask = $params["Netmask_" . $name]; if (array_key_exists("Enabled_" . $name, $params)) $retInfs[$key]->isEnabled = (strcasecmp($params["Enabled_" . $name], 'Enabled') == 0)? 1 : 0; if (array_key_exists("Dhcp_" . $name, $params)) $retInfs[$key]->isDhcp = (strcasecmp($params["Dhcp_" . $name], 'DHCP') == 0)? true : false; } } if (!$foundUserInput) return NULL; return $retInfs;} /* Return newInfs except for any interfaces found in $currInfs * that are no different from $currInfs. */function calc_interface_diffs($newInfs, $currInfs) { $modInfs = array(); foreach ($newInfs as $inf) { $tmpInf = find_interface_by_name($currInfs, $inf->name); if (!$tmpInf) { $modInfs[] = $inf; } else { if (interface_cmp($inf, $tmpInf) != 0) { $modInfs[] = $inf; } } } return $modInfs;} /* Return true if $inf->isDhcp differs from x->isDhcp * where x is the interface in $currInfs such that x->name == $inf->name */function has_bootproto_changed($inf, $currInfs) { $tmpInf = find_interface_by_name($currInfs, $inf->name); if (!$tmpInf) return true; if ($inf->isDhcp != $tmpInf->isDhcp) return true; if ($inf->isDhcp && $inf->isEnabled && !$tmpInf->isEnabled) return true; return false;}function find_interface_by_name($infs, $name) { foreach ($infs as $inf) { if ($inf->name == $name) return $inf; } return NULL;} /* Compare two NetInterface objects. Return 0 if they are the same, -1 * otherwise. Undefined if either parameter is NULL. */function interface_cmp($infa, $infb) { if ($infa->name != $infb->name) return -1; if ($infa->isEnabled != $infb->isEnabled) return -1; if ($infa->isDhcp != $infb->isDhcp) return -1; if ($infa->ipAddress != $infb->ipAddress) return -1; if ($infa->netmask != $infb->netmask) return -1; if ($infa->isEditable != $infb->isEditable) return -1; return 0;}/* Returns a GlobalNetworkConfig object to be interpreted by * write_daemon_global_network(). In particular, in the returned * object, those fields that are not modified are set to NULL by * this function. Input parameters are GlobalNetworkConfig objects. * It is assumed that none of the fields in $newConf are empty. */function calc_global_diffs($newConf, $currConf) { $hostname = ($newConf->hostname != $currConf->hostname)? $newConf->hostname : NULL; $gateway = ($newConf->gateway != $currConf->gateway)? $newConf->gateway : NULL; $primaryDNS = ($newConf->primaryDNS != $currConf->primaryDNS)? $newConf->primaryDNS : NULL; $secondaryDNS = ($newConf->secondaryDNS != $currConf->secondaryDNS)? $newConf->primaryDNS : NULL; return new GlobalNetworkConfig($hostname, $gateway, $primaryDNS, $secondaryDNS); }/* Reads the hostname, gateway and DNS servers from web form. * Returns GlobalNetworkConfig object. */function read_ui_global_network() { $params = read_params(); $hostname = (array_key_exists('Hostname', $params))? $params['Hostname'] : NULL; $gateway = (array_key_exists('Gateway', $params))? $params['Gateway'] : NULL; $dns1 = (array_key_exists('PrimaryDNS', $params))? $params['PrimaryDNS'] : NULL; $dns2 = (array_key_exists('SecondaryDNS', $params))? $params['SecondaryDNS'] : NULL; if (is_null($hostname) || is_null($gateway) || is_null($dns1) || is_null($dns2)) return NULL; return new GlobalNetworkConfig($hostname, $gateway, $dns1, $dns2); }/* Read the possibly hidden Gateway field */function read_ui_gateway() { $params = read_params(); $gateway = (array_key_exists('Gateway', $params))? $params['Gateway'] : NULL; return $gateway;}/* Returns array of NetInterfaces */function read_daemon_interfaces($netConn, $confConn, &$error) { $command = "SELECT name, status, ipAddress, mask FROM Interfaces"; $result = exec_sql($netConn, $command, $error, DB_HOST, DB_NETWORK_PORT); if (!$result) return NULL; $infs = array(); for ($row = 0; $row < pg_NumRows($result); $row++) { $name = pg_result($result, $row, 0); $infs[$name] = new NetInterface( $name, pg_result($result, $row, 1)? true : false, /* status */ false, /* dhcp (unknown here) */ pg_result($result, $row, 2), /* ipAddress */ pg_result($result, $row, 3)); /* mask */ } pg_freeresult($result); /* Populate dhcp field in $infs array */ $command = "SELECT name, value FROM tbl2field WHERE field=\"bootproto\""; $result = exec_sql($confConn, $command, $error, DB_HOST, DB_CONFMGR_PORT); if (!$result) return NULL; for ($row = 0; $row < pg_NumRows($result); $row++) { $value = pg_result($result, $row, 1); if (strstr(strtolower($value),"dhcp")) { $ifName = pg_result($result, $row, 0); foreach ($infs as $key => $inf) { if (!strcasecmp($infs[$key]->name, $ifName)) { $infs[$key]->isDhcp = true; } } } } pg_freeresult($result); return $infs; }/* Return NULL if there is an error and update * $error[] accordingly. * Returns 1 if no error. */function write_daemon_interfaces($netConn, $confConn, $modInfs, $currInfs, &$error) { foreach ($modInfs as $inf) { /* Write to kernel */ $subCmd = ""; $tmpInf = find_interface_by_name($currInfs, $inf->name); if ($tmpInf) { if ($inf->isEnabled && !$tmpInf->isEnabled) $subCmd = "status=1,"; else if (!$inf->isEnabled && $tmpInf->isEnabled) $subCmd = "status=0,"; } $command = "UPDATE Interfaces SET $subCmd ipAddress=\"$inf->ipAddress\", mask=\"$inf->netmask\" where name=\"$inf->name\""; $result = exec_sql($netConn, $command, $error, DB_HOST, DB_NETWORK_PORT); if (!$result) return NULL; else pg_freeresult($result); /* Write to persistent files */ $command = "UPDATE tbl2field SET value=\"$inf->ipAddress\" where name=\"$inf->name\" and field=\"ipaddr\""; $result = exec_sql($confConn, $command, $error, DB_HOST, DB_CONFMGR_PORT); if (!$result) return NULL; else pg_freeresult($result); if (NETWORK_PERSISTENCE_STYLE == LINUXFROMSCRATCH_STYLE) { $prefix = calc_network_prefix($inf->netmask); if ($prefix < 0) { $error[] = "Unable to calculate network prefix"; return NULL; } $command = "UPDATE tbl2field SET value=\"$prefix\" where name=\"$inf->name\" and field=\"prefix\""; } else { $command = "UPDATE tbl2field SET value=\"$inf->netmask\" where name=\"$inf->name\" and field=\"netmask\""; } $result = exec_sql($confConn, $command, $error, DB_HOST, DB_CONFMGR_PORT); if (!$result) return NULL; else pg_freeresult($result); if (!calc_broadcast_address($inf->ipAddress, $inf->netmask, $baddr)) { $error[] = "Unable to calculate broadcast address"; return NULL; } $command = "UPDATE tbl2field SET value=\"$baddr\" where name=\"$inf->name\" and field=\"broadcast\""; $result = exec_sql($confConn, $command, $error, DB_HOST, DB_CONFMGR_PORT); if (!$result) return NULL; else pg_freeresult($result); if (!calc_network_address($inf->ipAddress, $inf->netmask, $naddr)) { $error[] = "Unable to calculate network address"; return NULL; } $command = "UPDATE tbl2field SET value=\"$naddr\" where name=\"$inf->name\" and field=\"network\""; $result = exec_sql($confConn, $command, $error, DB_HOST, DB_CONFMGR_PORT); if (!$result) return NULL; else pg_freeresult($result); $command = "UPDATE tbl2file SET do_commit=1 WHERE name=\"$inf->name\""; $result = exec_sql($confConn, $command, $error, DB_HOST, DB_CONFMGR_PORT); if (!$result) return NULL; else pg_freeresult($result); } return 1;}/* $ipaddr and $netmask are in dotted-decimal format. Returns * the $broadcast in dotted-decimal format. Returns false on error, * true otherwise. */function calc_broadcast_address($ipaddr, $netmask, &$broadcast) { $ipaddrInt = 0; $netmaskInt = 0; if (!inet_aton($ipaddr, $ipaddrInt)) return false; if (!inet_aton($netmask, $netmaskInt)) return false; $broadcastInt = $ipaddrInt | ~ $netmaskInt; inet_ntoa($broadcastInt, $broadcast); return true;} function calc_network_address($ipaddr, $netmask, &$network) { $ipaddrInt = 0; $netmaskInt = 0; if (!inet_aton($ipaddr, $ipaddrInt)) return false; if (!inet_aton($netmask, $netmaskInt)) return false; $networkInt = $ipaddrInt & $netmaskInt; inet_ntoa($networkInt, $network); return true;} /* * Similar to function validate_netmask(). * Returns an network prefix (0 to 32), or -1 on error. */function calc_network_prefix($netmask) { $tok = split("\.", $netmask); $a = ""; foreach ($tok as $n) { $n = intval($n); $a .= decbin($n); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -