Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- function shorthand_to_mask($shorthand) {
- $mask = array();
- for ($x=0; $x<4; $x++) {
- $binaryMask = "";
- for ($y=0; $y<8; $y++) {
- if ($shorthand-->0) {
- $binaryMask .= "1";
- }
- else {
- $binaryMask .= "0";
- }
- }
- $mask[$x] = intval(bindec($binaryMask));
- }
- return $mask;
- }
- function find_net($ip, $mask) {
- $net = array();
- for ($x=0; $x<4; $x++) {
- $net[$x] = $ip[$x] & $mask[$x];
- }
- return $net;
- }
- function first_host($ip, $mask) {
- $ip = find_net($ip, $mask);
- $ip[3]++;
- return $ip;
- }
- function last_host($ip, $mask) {
- $last = broadcast($ip, $mask);
- $last[3]--;
- return $last;
- }
- function broadcast($ip, $mask) {
- $ip = find_net($ip, $mask);
- $broadcast = array();
- for ($x=0; $x<4; $x++) {
- $temp_mask = "";
- $temp_mask2 = str_split(strval(decbin($mask[$x])));
- if (count($temp_mask2)==1) {
- $temp_mask = "11111111";
- }
- else {
- for ($y=0; $y<8; $y++) {
- if ($temp_mask2[$y]=="0") {
- $temp_mask .= "1";
- }
- else {
- $temp_mask .= "0";
- }
- }
- }
- $temp_mask = bindec($temp_mask);
- $broadcast[$x] = $temp_mask | $ip[$x];
- }
- return $broadcast;
- }
- function elaborate_mask($maskRaw) {
- $mask = explode(".", $maskRaw);
- for ($x=0; $x<4; $x++) {
- $mask[$x] = intval($mask[$x]);
- }
- return $mask;
- }
- function elaborate_ip($ipRaw) {
- return elaborate_mask($ipRaw);
- }
- function mask_to_raw($mask) {
- $maskRaw = "";
- for ($x=0; $x<4; $x++) {
- $maskRaw .= $mask[$x] . ".";
- }
- return substr($maskRaw, 0, -1);
- }
- function net_to_raw($net) {
- return mask_to_raw($net);
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement