Gethostbyaddr (Perl)

Source: Internet
Author: User
Tags sprintf

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;

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.