Gethostbyaddr (Perl)
Gethostbyaddr ADDR, addrtype
This function converts an address to a name (or modifies an address ). ADDR should be the binary network address of a packet, while addrtype should actually be af_inet (from the socket module ). The returned values are:
($ Name, $ aliases, $ addrtype, $ length, @ addrs) =
Gethostbyaddr ($ packed_binary_address, $ addrtype );
Here @ addrs is the binary address of a packet. In the Internet domain, each address (due to the historical relationship) is four bytes long and can be unwrapped using the following:
($a, $b, $c, $d) = unpack('C4', $addrs[0]);
In addition, you can use the V modifier for sprintf to directly convert it into a dot vector representation:
$dots = sprintf "%vd", $addrs[0];
In addition, you can use the V modifier for sprintf to directly convert it into a dot vector representation:
$dots = sprintf "%vd", $addrs[0];
The inet_ntoa function of the socket module can be used to generate printable versions. This method will become very important when we are all preparing to switch to IPv6.
use Socket;
$printable_address = inet_ntoa($addrs[0]);
In a scalar environment, gethostbyaddr returns only the host name.
To generate an ADDR from a vertex vector, use:
use Socket;
$ipaddr = inet_aton("127.0.0.1"); # localhost
$claimed_hostname = gethostbyaddr($ipaddr, AF_INET);
Interestingly, in Perl 5.6, you can ignore inet_aton () and use the new v string representation for version number to operate the IP Address:
$ipaddr = v127.0.0.1;