From: pk910 Date: Thu, 28 Jul 2011 07:57:48 +0000 (+0200) Subject: added own IPAddr Class and rewrote the IP Address parser & builder X-Git-Url: http://git.pk910.de/?p=PHP-P10.git;a=commitdiff_plain;h=f1a1066826a5cc04606cc12f0bd0b8e0383ca190 added own IPAddr Class and rewrote the IP Address parser & builder --- diff --git a/Bots/IPv6.class.php b/Bots/IPv6.class.php index 9dc8f84..44c79d7 100644 --- a/Bots/IPv6.class.php +++ b/Bots/IPv6.class.php @@ -81,9 +81,7 @@ class {$_NAME} extends Bot { public function recive_join($user, $channel, $isBurst) { if(!$this->botOppedOnChannel($channel)) return false; - $ipv6 = '/^((([0-9A-Fa-f]{1,4}:){7}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){6}:[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){5}:([0-9A-Fa-f]{1,4}:)?[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){4}:([0-9A-Fa-f]{1,4}:){0,2}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){3}:([0-9A-Fa-f]{1,4}:){0,3}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){2}:([0-9A-Fa-f]{1,4}:){0,4}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){6}((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|(([0-9A-Fa-f]{1,4}:){0,5}:((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|(::([0-9A-Fa-f]{1,4}:){0,5}((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|([0-9A-Fa-f]{1,4}::([0-9A-Fa-f]{1,4}:){0,5}[0-9A-Fa-f]{1,4})|(::([0-9A-Fa-f]{1,4}:){0,6}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){1,7}:))(|\/[0-9]{1,3})$/'; - echo $user->getIP()."\n"; - if(preg_match($ipv6, $user->getIP())) { + if($user->getIP()->isIPv6()) { $this->uplink->mode($this->ipv6, $channel, "+ov ".$user->getNumeric()." ".$user->getNumeric()); } else { $this->uplink->mode($this->ipv6, $channel, "+v ".$user->getNumeric()); diff --git a/Uplink/IPAddr.class.php b/Uplink/IPAddr.class.php new file mode 100644 index 0000000..217fbe7 --- /dev/null +++ b/Uplink/IPAddr.class.php @@ -0,0 +1,176 @@ +parseIPv6($initial_value); + } elseif(preg_match(self::$pattern_IPv4, $initial_value)) { + $this->parseIPv4($initial_value); + } else { + $this->parseNumeric($initial_value); + } + } + + public function parseNumeric($numeric) { + if(strlen($numeric) == 6) { //IPv4 + $value = self::numToInt($numeric); + $this->ip6[6] = ($value & 0xffff0000) >> 16; + $this->ip6[7] = ($value & 0x0000ffff); + $this->addr_is_ipv6 = false; + } else { //IPv6 + $j = 0; + for($i = 0; $i < strlen($numeric);) { + if($numeric[$i] == "_") { + $rightBlocks = (strlen($numeric) - $i - 1) / 3; + $skipBlocks = 8 - $j - $rightBlocks; + $j += $skipBlocks; + } else { + $value = self::numToInt($numeric[$i].$numeric[$i+1].$numeric[$i+2]); + $this->ip6[$j++] = dechex($value); + $i += 3; + } + } + } + } + + public function parseIPv6($ipv6) { + if(substr($ipv6,0,2) == "::") $ipv6 = substr($ipv6, 1); + if(substr($ipv6,-2) == "::") $ipv6 = substr($ipv6, 0, -1); + $ipv6blocks = explode(":", $ipv6); + $j = 0; + foreach($ipv6blocks as $i => $block) { + if($block == "") { + $skipBlocks = 8 - count($ipv6blocks); + $j += $skipBlocks + 1; + } else { + $this->ip6[$j++] = hexdec($block); + } + } + } + + public function parseIPv4($ipv4) { + $ipv4blocks = explode(".",$ipv4); + $this->ip6[6] = intval($ipv4blocks[0]) << 8; + $this->ip6[6] |= intval($ipv4blocks[1]); + $this->ip6[7] = intval($ipv4blocks[2]) << 8; + $this->ip6[7] |= intval($ipv4blocks[3]); + } + + public function isIPv6() { + return $this->addr_is_ipv6; + } + + public function getAddress() { + if($this->isIPv6()) { + $max_start = 0; + $max_zeros = 0; + $curr_zeros = 0; + for ($i = 0; $i < 8; $i++) { + if ($this->ip6[$i] == 0) + $curr_zeros++; + else if ($curr_zeros > $max_zeros) { + $max_start = $i - $curr_zeros; + $max_zeros = $curr_zeros; + $curr_zeros = 0; + } + } + if ($curr_zeros > $max_zeros) { + $max_start = $i - $curr_zeros; + $max_zeros = $curr_zeros; + } + $ipv6 = ""; + for($i = 0; $i < 8; $i++) { + if($max_zeros > 1 && $i == $max_start) { + $ipv6 .= "::"; + $i += $max_zeros - 1; + } else { + if($ipv6 != "") $ipv6 .= ":"; + $ipv6 .= dechex($this->ip6[$i]); + } + } + return $ipv6; + } else { + $ipv4 = array(); + $ipv4[0] = ($this->ip6[6] >> 8) & 0xff; + $ipv4[1] = ($this->ip6[6]) & 0xff; + $ipv4[2] = ($this->ip6[7] >> 8) & 0xff; + $ipv4[3] = ($this->ip6[7]) & 0xff; + return implode(".", $ipv4); + } + } + + public function getNumeric() { + if($this->isIPv6()) { + $max_start = 0; + $max_zeros = 0; + $curr_zeros = 0; + for ($i = 0; $i < 8; $i++) { + if ($this->ip6[$i] == 0) + $curr_zeros++; + else if ($curr_zeros > $max_zeros) { + $max_start = $i - $curr_zeros; + $max_zeros = $curr_zeros; + $curr_zeros = 0; + } + } + if ($curr_zeros > $max_zeros) { + $max_start = $i - $curr_zeros; + $max_zeros = $curr_zeros; + } + $ipv6 = ""; + for($i = 0; $i < 8; $i++) { + if($max_zeros > 0 && $i == $max_start) { + $ipv6 .= "_"; + $i += $max_zeros - 1; + } else { + $ipv6 .= Numerics::intToNum($this->ip6[$i],3); + } + } + return $ipv6; + } else { + $ipv4 = array(); + $ipv4[0] = Numerics::intToNum($this->ip6[6], 3); + $ipv4[1] = Numerics::intToNum($this->ip6[7], 3); + return $ipv4[0].$ipv4[1]; + } + } + +} + +?> \ No newline at end of file diff --git a/Uplink/Numerics.class.php b/Uplink/Numerics.class.php index 7b6bee1..01944dc 100644 --- a/Uplink/Numerics.class.php +++ b/Uplink/Numerics.class.php @@ -84,117 +84,6 @@ class Numerics { return $int; } - public static function parseIP($numeric, $shortForm = true) { - if(strlen($numeric) == 6) { //IPv4 - $value = self::numToInt($numeric); - $ip = array(); - $ip[0] = ($value & 0xff000000) >> 24; - $ip[1] = ($value & 0x00ff0000) >> 16; - $ip[2] = ($value & 0x0000ff00) >> 8; - $ip[3] = ($value & 0x000000ff); - return implode(".", $ip); - } else { //IPv6 - $ip = array(); - for($i = 0; $i < strlen($numeric);) { - if($numeric[$i] == "_") { - if($shortForm) { - $ip[] = ""; //:: - } else { - $rightBlocks = (strlen($numeric) - ($i + 1)) / 3; - $skipCount = 8 - count($ip) - $rightBlocks; - for($j = 0; $j < $skipBlocks; $j++) { - $ip[] = "0"; - } - } - $i++; - } else { - $value = self::numToInt($numeric[$i].$numeric[$i+1].$numeric[$i+2]); - $ip[] = dechex($value); - $i += 3; - } - } - return implode(":", $ip); - } - } - - public static function numericFromIP($ip) { - $pattern = array(); - $pattern['ipv6'] = '/^((([0-9A-Fa-f]{1,4}:){7}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){6}:[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){5}:([0-9A-Fa-f]{1,4}:)?[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){4}:([0-9A-Fa-f]{1,4}:){0,2}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){3}:([0-9A-Fa-f]{1,4}:){0,3}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){2}:([0-9A-Fa-f]{1,4}:){0,4}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){6}((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|(([0-9A-Fa-f]{1,4}:){0,5}:((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|(::([0-9A-Fa-f]{1,4}:){0,5}((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|([0-9A-Fa-f]{1,4}::([0-9A-Fa-f]{1,4}:){0,5}[0-9A-Fa-f]{1,4})|(::([0-9A-Fa-f]{1,4}:){0,6}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){1,7}:))(|\/[0-9]{1,3})$/'; - $pattern['ipv4'] = '/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}(|\/[0-9]{1,2})$/'; - if(preg_match($pattern['ipv4'], $ip)) { - //thats quite simple here - $ip = explode(".", $ip); - $ipValue = intval($ip[3]); - $ipValue <<= 8; - $ipValue |= intval($ip[2]); - $ipValue <<= 8; - $ipValue |= intval($ip[1]); - $ipValue <<= 8; - $ipValue |= intval($ip[0]); - $ip = self::intToNum($ipValue,6); - } else if(preg_match($pattern['ipv6'], $ip)) { - //thats a little bit complicated :D (we may only have one _) - $ipv6 = array(); - $ip = explode(":",$ip); - $last_zero = false; $zero_sequence = 0; $biggest_zero_sequence = 0; $max_start = -1; - $i = 0; - foreach($ip as $v) { - if($v == "") { - $skipBlocks = (8 - count($ip)); - for($j = 0; $j < $skipBlocks; $j++) { - $ipv6[$i+$j] = "_"; - } - $max_start = $i; - if($last_zero) { - $zero_sequence += $skipBlocks; - } else { - $last_zero = true; - $zero_sequence = $skipBlocks; - } - $i+=$skipBlocks; - if($zero_sequence > $biggest_zero_sequence) { - $biggest_zero_sequence = $zero_sequence; - $max_start = $i-($zero_sequence-1); - } - } else { - $value = hexdec($v); - if($value == 0) { - $ipv6[$i] = "_"; - if($last_zero) { - $zero_sequence++; - } else { - $last_zero = true; - $zero_sequence = 1; - } - if($zero_sequence > $biggest_zero_sequence) { - $biggest_zero_sequence = $zero_sequence; - $max_start = $i-($zero_sequence-1); - } - } else { - $ipv6[$i] = self::intToNum($value,3); - $last_zero = false; - } - $i++; - } - } - $ip = ""; - for($i = 0; $i < 8; $i++) { - if($i == $max_start) { //merge the biggest sequence of _'s - $ip .= "_"; - $i += ($biggest_zero_sequence-1); - } elseif($ipv6[$i] == "_") { - $ip .= "AAA"; - } else { - $ip .= $ipv6[$i]; - } - } - return $ip; - } else { - $ip = "AAAAAA"; - } - return $ip; - } - } ?> \ No newline at end of file diff --git a/Uplink/Uplink.class.php b/Uplink/Uplink.class.php index f83c7e3..a23f4ce 100644 --- a/Uplink/Uplink.class.php +++ b/Uplink/Uplink.class.php @@ -61,6 +61,7 @@ require_once("P10_User.class.php"); require_once("P10_Channel.class.php"); require_once("P10_ModeSets.class.php"); require_once("EventHandler.interface.php"); +require_once("IPAddr.class.php"); $e=1; define("ERR_NICK_IN_USE", $e++); @@ -394,7 +395,7 @@ class Uplink { $host = $args[4]; $modes = implode(" ",array_slice($args, 5, count($args)-8)); $modes = new P10_UserModeSet($modes); - $ip = Numerics::parseIP($args[count($args)-3]); + $ip = new IPAddr($args[count($args)-3]); $realname = $args[count($args)-1]; $user = new P10_User($nick, $numeric, $server, $connect_time, $ident, $host, $ip, $realname, $modes); if($this->eventHandler) @@ -785,9 +786,10 @@ class Uplink { } $this->last_local_numeric++; $modes = new P10_UserModeSet($modes); + $ip = new IPAddr($ip); $user = new P10_User($nick, $numeric, $this->server, time(), $ident, $host, $ip, $realname, $modes); if(($this->flags & self::FLAG_CONNECTED)) { - $ip = Numerics::numericFromIP($user->getIP()); + $ip = $user->getIP()->getNumeric(); $this->send("N", $nick, $user->getConnectTime(), $ident, $host, $user->getModes()->getModeString(), $ip, $numeric, $realname); } return $user;