############################################################################ # # # ip_convert() Version 1.0 # # Written by Craig Patchett craig@patchett.com # # Created 10/4/96 Last Modified 3/26/97 # # # # Copyright 1997 Craig Patchett & Matthew Wright. All Rights Reserved. # # This subroutine is part of The CGI/Perl Cookbook from John Wiley & Sons. # # License to use this program or install it on a server (in original or # # modified form) is granted only to those who have purchased a copy of The # # CGI/Perl Cookbook. (This notice must remain as part of the source code.) # # # # Function: Converts an IP address to an eight-character string or # # vice-versa. # # # # Usage: &ip_convert($ip); or &ip_convert($converted_string); # # # # Variables: $ip -- String containing IP address # # Example: '100.100.100.100' # # $converted_IP -- Converted IP address # # Example: 'ZGRkZAAw' # # # # Returns: String containing eight-character encoded IP address if # # called with an IP address # # String containing an IP address if called with a # # converted IP address # # Null string if not called with a valid parameter # # # # Uses Globals: None # # # # Files Created: None # # # ############################################################################ sub ip_convert { local($ip) = $_[0]; # Make sure that $ip contains an IP address if ($ip =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) { # Convert $ip into four-byte string $ip = pack("C4", split(/\./, $ip)); # Convert string into uuencoded then to base64 $ip = substr(pack("u", $ip), 1); chop($ip); $ip =~ tr| -_`|A-Za-z0-9*_A|; # $ip =~ tr| -_`|A-Za-z0-9+_A|; return($ip); } elsif (length($ip) == 8) { # Convert string into uuencoded format # $ip =~ tr|A-Za-z0-9+_| -_|; $ip =~ tr|A-Za-z0-9*_| -_|; $ip = '$' . $ip; $ip = unpack("u", $ip); $ip = join('.', unpack("C4", $ip)); return($ip); } else { return('') } } 1;