Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re^2: how to resolve IP's in an HTTPd that doesn't resolve them?

by VinsWorldcom (Prior)
on Jun 14, 2018 at 11:28 UTC ( #1216620=note: print w/replies, xml ) Need Help??


in reply to Re: how to resolve IP's in an HTTPd that doesn't resolve them?
in thread how to resolve IP's in an HTTPd that doesn't resolve them?

AGREE! I'm no expert at this, but when I did do it long ago with my own personal web server, Apache just logged IPs and the Perl script I wrote for post log processing (and long since lost) did name resolution, concatenation and statistics and emailed summary to me.

As long as we're on the topic of name resolution, you may not *yet* care about IPv6, but rather than rewriting your code later when you do, start with the address family independent resolution calls rather than the legacy ones:

  • gethostbyname => getnameinfo
  • gethostbyaddr => getaddrinfo
  • inet_ntoa => inet_ntop
  • inet_aton => inet_pton

Perl Socket module has had support since around version 1.94 (Perl 5.14 or there-abouts). A brief example that can be reduced but should be compatible with Socket modules of a certain earlier version that had the new routines, but no IPv6 support:

#!/usr/bin/perl use strict; use warnings; use Socket qw(inet_ntoa AF_INET IPPROTO_TCP); my $AF_INET6 = eval { Socket::AF_INET6() }; my $AF_UNSPEC = eval { Socket::AF_UNSPEC() }; my $AI_NUMERICHOST = eval { Socket::AI_NUMERICHOST() }; my $NI_NUMERICHOST = eval { Socket::NI_NUMERICHOST() }; # Required for reverse lookup my $NI_NAMEREQD = eval { Socket::NI_NAMEREQD() }; my %hints = ( family => $AF_UNSPEC, protocol => IPPROTO_TCP ); my ( $err, @getaddr ) = Socket::getaddrinfo( $ARGV[0], undef, \%hints +); if ( defined( $getaddr[0] ) ) { for my $addr (@getaddr) { my ( $err, $address ) = Socket::getnameinfo( $addr->{addr}, $NI_NUMERICHOST ); printf "getaddrinfo()/getnameinfo() Address = %s\n", ( defined($address) ) ? $address : $err; # Reverse Lookup my ( $host, $service ); ( $err, $host, $service ) = Socket::getnameinfo( $addr->{addr}, $NI_NAMEREQD ); printf " |_> getnameinfo() Name = %s\n", ( defined($host) ) ? $host : $err; } } else { print "$0: getaddrinfo() failed - error = $err\n"; }

and run ...

C:\> test.pl www.google.com getaddrinfo()/getnameinfo() Address = 172.217.15.100 |_> getnameinfo() Name = iad30s21-in-f4.1e100.net

Replies are listed 'Best First'.
Re^3: how to resolve IP's in an HTTPd that doesn't resolve them?
by taint (Chaplain) on Jun 14, 2018 at 14:55 UTC
    Wonderful example, VinsWorldcom . Thanks!

    Right you are; IPv6 should indeed be considered. I hadn't really blocked the whole process out yet. As I wanted to first look at just how it be best implemented up front -- what all the possibilities that might be available.

    I too used Socket in my OP. As well as inet_aton. But hadn't (yet) bothered with IPv6 resolution. But will be adding it. Thank you for such an elaborate example ++ !

    Thanks again, VinsWorldcom ! With yours, as well as the other excellent examples above. I feel pretty well armed for the task! :-)

    --Chris

    Evil is good, for without it, Good would have no value
    λɐp ʇɑəɹ⅁ ɐ əʌɐɥ puɐ ʻꜱdləɥ ꜱᴉɥʇ ədoH

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1216620]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2020-06-02 01:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you really want to know if there is extraterrestrial life?



    Results (12 votes). Check out past polls.

    Notices?