Приглашаем посетить
Сладков (sladkov.lit-info.ru)

host name for an IP address

############################################################################
#                                                                          #
# addr_to_host()                    Version 1.5                            #
# Written by Craig Patchett         craig@patchett.com                     #
# Created 8/1/96                    Last Modified 3/21/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:      Attempts to determine the host name for an IP address.    #
#                                                                          #
# Usage:         &addr_to_host($ip_address);                               #
#                                                                          #
# Variables:     $ip_address -- String containing IP address               #
#                               Example: '100.100.100.100'                 #
#                                                                          #
# Returns:       The host name as a string if successful                   #
#                The null string ('') if unsuccessful                      #
#                                                                          #
# Uses Globals:  None                                                      #
#                                                                          #
# Files Created: None                                                      #
#                                                                          #
############################################################################


sub addr_to_host {

    # Get the address to be converted
    
    local($ip_address) = $_[0];
    
    # Strip any leading or trailing spaces
    
    $ip_address =~ s/^\s+|\s+$//g;
    
    # Split the four bytes into an array
    
    local(@bytes) = split(/\./, $ip_address);
    
    # Pack the four bytes into a four-character string
    
    local($packaddr) = pack("C4",@bytes);
    
    # Use gethostbyaddr() to get the host info if available
    
    local($host_name) = (gethostbyaddr($packaddr, 2))[0];
    
    # Return the host name (null if not available)
    
    return($host_name);
}

1;