⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 setup_network.php

📁 Linux嵌入式设计配套光盘,学习嵌入式设计可参考
💻 PHP
📖 第 1 页 / 共 3 页
字号:
  /* $a should be 1's followed by 0's */  if (strlen($a) > 32)    return -1;  if (!ereg("^1*0*$", $a))    return -1;   $prefix = 0;  for ($i=0; $i<strlen($a); $i++) {     if ($a[$i] == "1")         $prefix++;    else         break;  }  return $prefix; }/* Returns NULL on error. * Returns GlobalNetworkConfig object if no error. */function read_daemon_global_network($confConn, $netConn, &$error) {  $row = 0;  $command = "SELECT hostname FROM Host";  $result = exec_sql($netConn, $command, $error, DB_HOST, DB_NETWORK_PORT);  if (!$result)    return NULL;    $hostname = pg_result($result, $row, 0);   pg_freeresult($result);  $gateway = read_daemon_gateway($confConn, $netConn, $error);   if (is_null($gateway))    return NULL;  $command = "SELECT value FROM tbl2field where name = \"resolv\" and field = \"nameserver_1\"";  $result = exec_sql($confConn, $command, $error, DB_HOST, DB_CONFMGR_PORT);  if (!$result)    return NULL;    $primaryDNS = pg_result($result, $row, 0);   pg_freeresult($result);  $command = "SELECT value FROM tbl2field where name = \"resolv\" and field = \"nameserver_2\"";  $result = exec_sql($confConn, $command, $error, DB_HOST, DB_CONFMGR_PORT);  if (!$result)    return NULL;    $secondaryDNS = pg_result($result, $row, 0);   pg_freeresult($result);      return new GlobalNetworkConfig($hostname, $gateway, $primaryDNS, $secondaryDNS); }/* Return NULL for error. Return "" when there is no default  * gateway. Return IP address when a default gateway is found. */function read_daemon_gateway($confConn, $netConn, &$error) {  $command = "SELECT gateway FROM Host";  $result = exec_sql($netConn, $command, $error, DB_HOST, DB_NETWORK_PORT);  if (!$result)    return NULL;    $gateway = pg_result($result, 0, 0);   pg_freeresult($result);  if (empty($gateway)) {    $command = "SELECT value FROM tbl2field where name = \"network\" and field = \"gateway\"";    $result = exec_sql($confConn, $command, $error, DB_HOST, DB_CONFMGR_PORT);    if ($result)       $gateway = pg_result($result, 0, 0);   }  return $gateway;}/* $modConfig is treated as follows.  If any field is NULL it means there is  * no change  * Return NULL if there is an error and update $error[]. */function write_daemon_global_network($confConn, $netConn, $modConfig, &$error) {   $some_network_change = false;   $some_resolv_change = false;   if ($modConfig->hostname) {     $some_network_change = true;     $command = "UPDATE Host SET hostname=\"$modConfig->hostname\"";    $result = exec_sql($netConn, $command, $error, DB_HOST, DB_NETWORK_PORT);    if (!$result)      return NULL;     else      pg_freeresult($result);    $command = "UPDATE tbl2field SET value=\"$modConfig->hostname\" where name=\"network\" and field=\"hostname\"";    $result = exec_sql($confConn, $command, $error, DB_HOST, DB_CONFMGR_PORT);    if (!$result)      return NULL;     else      pg_freeresult($result);  }  if ($modConfig->gateway) {     $some_network_change = true;     $command = "UPDATE Host SET gateway=\"$modConfig->gateway\"";    $result = exec_sql($netConn, $command, $error, DB_HOST, DB_NETWORK_PORT);    if (!$result)      return NULL;     else      pg_freeresult($result);    /* Read the gateway to see if the gateway is the same as the one     * we wrote above (it should be), and if so, persist the new gateway.      * We do this so that if the user tries to configure a gateway,     * we only persist this gateway if the kernel accepts it.     */    $kernel_accepted_gateway = 0;    $command = "SELECT gateway FROM Host";    $result = exec_sql($netConn, $command, $error, DB_HOST, DB_NETWORK_PORT);    if ($result) {      $newGateway = pg_result($result, 0, 0);       pg_freeresult($result);      if ($newGateway == $modConfig->gateway)         $kernel_accepted_gateway = 1;    }    if ($kernel_accepted_gateway) {      if (NETWORK_PERSISTENCE_STYLE == LINUXFROMSCRATCH_STYLE) {        /* Hack ahead. In Linux From Scratch, the gateway is a          * per-interface entity rather than a per host entity.  So          * modify the gateway for eth0.          */        $command = "UPDATE tbl2field SET value=\"$modConfig->gateway\" where name=\"eth0\" and field=\"gateway\"";      } else {        $command = "UPDATE tbl2field SET value=\"$modConfig->gateway\" where name=\"network\" and field=\"gateway\"";      }      $result = exec_sql($confConn, $command, $error, DB_HOST, DB_CONFMGR_PORT);      if (!$result)        return NULL;       else        pg_freeresult($result);    }  }  if ($modConfig->primaryDNS) {     $some_resolv_change = true;     $command = "UPDATE tbl2field SET value=\"$modConfig->primaryDNS\" where name=\"resolv\" and field=\"nameserver_1\"";    $result = exec_sql($confConn, $command, $error, DB_HOST, DB_CONFMGR_PORT);    if (!$result)      return NULL;     else      pg_freeresult($result);  }  if ($modConfig->secondaryDNS) {     $some_resolv_change = true;     $command = "UPDATE tbl2field SET value=\"$modConfig->secondaryDNS\" where name=\"resolv\" and field=\"nameserver_2\"";    $result = exec_sql($confConn, $command, $error, DB_HOST, DB_CONFMGR_PORT);    if (!$result)      return NULL;     else      pg_freeresult($result);  }  if ($some_network_change) {    $command = "UPDATE tbl2file SET do_commit=1 WHERE name=\"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_script=1 WHERE name=\"network\"";    $result = exec_sql($confConn, $command, $error, DB_HOST, DB_CONFMGR_PORT);    if (!$result)      return NULL;     else      pg_freeresult($result);  }  if ($some_resolv_change) {    $command = "UPDATE tbl2file SET do_commit=1 WHERE name=\"resolv\"";    $result = exec_sql($confConn, $command, $error, DB_HOST, DB_CONFMGR_PORT);    if (!$result)      return NULL;     else      pg_freeresult($result);  }  return 0; }     function gen_enabled_html($inf) {  /* Mozilla doesn't seem to support 'selected'.   * IE does.   */  if ($inf->isEditable) {    if ($inf->isEnabled)       return "<td> <select name=\"Enabled_$inf->name\"> <option selected>Enabled</option> <option>Disabled</option> </select> </td>";    else      return "<td> <select name=\"Enabled_$inf->name\"> <option selected>Disabled</option> <option>Enabled</option> </select> </td>";  } else {    if ($inf->isEnabled)       return "<td>Enabled</td>";    else      return "<td>Disabled</td>";  }} function gen_static_html($inf) {  if ($inf->isEditable) {    if ($inf->isDhcp)       return "<td> <select name=\"Dhcp_$inf->name\"> <option selected>DHCP</option> <option>Static</option> </select> </td>";    else      return "<td> <select name=\"Dhcp_$inf->name\"> <option selected>Static</option> <option>DHCP</option> </select> </td>";  } else {    if ($inf->isDhcp)       return "<td>DHCP</td>";    else      return "<td>Static</td>";  }}function gen_addr_html($inf, $textSize) {  if ($inf->isEditable)     return "<td><input type=text size=$textSize name=\"Address_$inf->name\" value=\"$inf->ipAddress\" /></td>";  else    return "<td>" . $inf->ipAddress . "</td>";}function gen_mask_html($inf, $textSize) {  if ($inf->isEditable)     return "<td><input type=text size=$textSize name=\"Netmask_$inf->name\" value=\"$inf->netmask\" /></td>";  else    return "<td> $inf->netmask </td>";} function gen_hidden_gateway_html($gateway) {  if (empty($gateway))     return "";  return "<p><input type=hidden name=Gateway value=\"$gateway\" /></p>"; }/* inet_ntoa returns 0 if the addr_str is valid, non-zero if not. */function inet_ntoa($addr_int, &$addr_str) {  $d = $addr_int & 0xFF;  $addr_int = $addr_int >> 8;   $c = $addr_int & 0xFF;  $addr_int = $addr_int >> 8;   $b = $addr_int & 0xFF;  $addr_int = $addr_int >> 8;   $a = $addr_int & 0xFF;  $addr_str = "" . $a . "." . $b . "." . $c . "." . $d;   return 1;}/* inet_aton returns 0 if the addr_str is valid, non-zero if not. */function inet_aton($addr_str, &$addr_int) {  if (!$addr_str)    return 0;  $addr_str = trim($addr_str);  $tok = split("\.", $addr_str);  if (count($tok) != 4)    return 0;  $a = array();  $count = 0;  foreach ($tok as $n) {    if (!is_numeric($n))      return 0;    $n = 0+$n;     if (!is_int($n))      return 0;    if (intval($n) < 0)      return 0;    if (intval($n) > 255)      return 0;    $a[$count] = $n;    $count++;  }  $addr_int = $a[0];  $addr_int = ($addr_int << 8) + $a[1];  $addr_int = ($addr_int << 8) + $a[2];  $addr_int = ($addr_int << 8) + $a[3];  return 1; }/* Returns a trimmed dotted-decimal IP address given a dotted-decimal IP  * Removed leading zeros, eg "255.255.01.1" will be converted to "255.255.1.1". * Returns false if the given IP address is badly formed.  */ function validate_ip_address(&$address) {  if (!$address)    return false;  $address = trim($address);  $tok = split("\.", $address);  if (count($tok) != 4)    return false;  $a = array();  $count = 0;  foreach ($tok as $n) {    if (!is_numeric($n))      return false;    $n = 0+$n;     if (!is_int($n))      return false;    if (intval($n) < 0)      return false;    if (intval($n) > 255)      return false;    $a[$count] = $n;    $count++;  }  $address = "" . $a[0] . "." . $a[1] . "." . $a[2] . "." . $a[3];   return true; }function validate_netmask(&$address) {  if (!validate_ip_address($address))    return false;   $tok = split("\.", $address);  $a = "";  foreach ($tok as $n) {    $n = intval($n);    $a .= decbin($n);   }  /* $a should be 1's followed by 0's with at least one 0. */  /* allow 0.0.0.0 as well */  if (!ereg("^1*0+$", $a))    return false;  return true;}/* Return true if the hostname is valid, false otherwise.  */function validate_hostname(&$hostname) {  if (!$hostname)    return false;  $hostname = trim($hostname);  if (strlen($hostname) <= 0)    return false;  if (strlen($hostname) > HOST_NAME_MAX_INPUT)    return false;  if (!ereg("^[a-zA-Z]",$hostname)) {    return false;  }  return true;}/* Provides a wrapper to pg_exec that includes error message generation. Returns the same result as pg_exec; false on error or a result index if the sql command could be executed. * $error is an array that is appended to if there is an error  * $host and $port just used for error reporting * The caller is expected to free the returned object with pg_freeresult.  */function exec_sql($conn, $cmd, &$error, $host, $port) {  $result = pg_exec($conn, $cmd);  if (!$result) {     $detail = pg_ErrorMessage($conn);     $error[] = "SQL command failed. Connected to \"" . $host . "\" on port " . $port . "." . " " . $detail;      return false;   }  return $result;}?>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -