Advertisement
Guest User

Untitled

a guest
Nov 12th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.58 KB | None | 0 0
  1. <?php
  2. function shorthand_to_mask($shorthand) {
  3.     $mask = array();
  4.     for ($x=0; $x<4; $x++) {
  5.         $binaryMask = "";
  6.         for ($y=0; $y<8; $y++) {
  7.             if ($shorthand-->0) {
  8.                 $binaryMask .= "1";
  9.             }
  10.             else {
  11.                 $binaryMask .= "0";
  12.             }
  13.         }
  14.         $mask[$x] = intval(bindec($binaryMask));
  15.     }
  16.     return $mask;
  17. }
  18.  
  19. function find_net($ip, $mask) {
  20.     $net = array();
  21.     for ($x=0; $x<4; $x++) {
  22.         $net[$x] = $ip[$x] & $mask[$x];
  23.     }
  24.     return $net;
  25. }
  26.  
  27. function first_host($ip, $mask) {
  28.     $ip = find_net($ip, $mask);
  29.     $ip[3]++;
  30.     return $ip;
  31. }
  32.  
  33. function last_host($ip, $mask) {
  34.     $last = broadcast($ip, $mask);
  35.     $last[3]--;
  36.     return $last;
  37. }
  38.  
  39. function broadcast($ip, $mask) {
  40.     $ip = find_net($ip, $mask);
  41.     $broadcast = array();
  42.     for ($x=0; $x<4; $x++) {
  43.         $temp_mask = "";
  44.         $temp_mask2 = str_split(strval(decbin($mask[$x])));
  45.         if (count($temp_mask2)==1) {
  46.             $temp_mask = "11111111";
  47.         }
  48.         else {
  49.             for ($y=0; $y<8; $y++) {
  50.                 if ($temp_mask2[$y]=="0") {
  51.                     $temp_mask .= "1";
  52.                 }
  53.                 else {
  54.                     $temp_mask .= "0";
  55.                 }
  56.             }
  57.         }
  58.         $temp_mask = bindec($temp_mask);
  59.         $broadcast[$x] = $temp_mask | $ip[$x];
  60.     }
  61.     return $broadcast;
  62. }
  63.  
  64. function elaborate_mask($maskRaw) {
  65.     $mask = explode(".", $maskRaw);
  66.     for ($x=0; $x<4; $x++) {
  67.         $mask[$x] = intval($mask[$x]);
  68.     }
  69.     return $mask;
  70. }
  71.  
  72. function elaborate_ip($ipRaw) {
  73.     return elaborate_mask($ipRaw);
  74. }
  75.  
  76. function mask_to_raw($mask) {
  77.     $maskRaw = "";
  78.     for ($x=0; $x<4; $x++) {
  79.         $maskRaw .= $mask[$x] . ".";
  80.     }
  81.     return substr($maskRaw, 0, -1);
  82. }
  83.  
  84. function net_to_raw($net) {
  85.     return mask_to_raw($net);
  86. }
  87.  
  88.  
  89. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement