MonkeyManChfKiller has asked for the wisdom of the Perl Monks concerning the following question:
I currently have this solution for requesting DNS querys in perl.
use Net::DNS::Resolver;
my $lh = "65.0.78.101";
my $packet = new Net::DNS::Packet($lh, "A", "IN");
Now im trying to re-create that, but without net::dns::resolver. just with packing or some other solution. so far im not getting the data i need, help is appreciated, and since i have virtual no network knowledge prior to this.. its hard.
Re: Net::Dns::packet creation.
by GrandFather (Saint) on May 23, 2014 at 20:04 UTC
|
| [reply] |
|
| [reply] [d/l] [select] |
Re: Net::Dns::packet creation.
by shadrack (Acolyte) on May 25, 2014 at 03:53 UTC
|
#!/usr/bin/perl
#Resolve IP address into hostname
sub ReverseLookup {
my(@addrs,$xname,$xaliases,$xtype,$xlen,$addr);
@addrs=split(/\./,$_[0]);
$addr=pack(' C4',@addrs[0..3]);
($xname,$xaliases,$xtype,$xlen,@addrs)=gethostbyaddr($addr,2);
return $xname;
}
$ip="65.0.78.101";
$name=&ReverseLookup($ip);
print "$ip = $name\n";
| [reply] [d/l] |
|
shadrack gave a good answer by using gethostbyaddr with the pack function to no use the Socket module which is tricky.
We normally would do :
#!/usr/bin/perl
use Socket;
#Resolve IP address into hostname
sub ReverseLookup {
my(@addrs,$xname,$xaliases,$xtype,$xlen,$addr);
# @addrs=split(/\./,$_[0]);
# $addr=pack('c4',@addrs[0..3]); # c or C - A signed/unsigned char
+(8-bit integer) value
$addr = inet_aton($_[0..3]);
($xname,$xaliases,$xtype,$xlen,@addrs)=gethostbyaddr($addr,2);
return $xname;
}
We don't know why MonkeyManChfKiller don't want to use modules, it's his choice but obviously this can be achieved in another way with perl.
Nice use of pack() which I don't see very often.
| [reply] [d/l] |
Re: Net::Dns::packet creation.
by no_slogan (Deacon) on May 23, 2014 at 19:57 UTC
|
If you're looking for technical information on DNS, try RFC 1035. | [reply] |
Re: Net::Dns::packet creation.
by Anonymous Monk on May 23, 2014 at 19:51 UTC
|
Now im trying to re-create that, but without net::dns::resolver. just with packing or some other solution. so far im not getting the data i need, help is appreciated, and since i have virtual no network knowledge prior to this.. its hard. :) You know thats not funny at all, right?
See Convert::Binary::C its what perlpacktut recommends
I hear you, that's just another module ... its got wisdom for you about portability and fun :)
| [reply] |
Re: Net::Dns::packet creation.
by andal (Hermit) on May 26, 2014 at 13:52 UTC
|
Try to read 'perldoc -f gethostbyname'. Basically, using that function you can get address suitable for use with 'connect' or 'bind' functions.
Module Socket also offers functions inet_ntoa and inet_aton that can be used to convert between packed and unpacked IP addresses.
Normally, module IO::Socket::INET takes care of this for you. So I don't remember when I've used these functions last time.
Just for general education. On the low level, networking requires "packed" addresses. The format of these addresses depends on the type of the network. Normally people work with IPv4 networks. The addresses on these networks are represented by 4 numbers from 0 to 255. Unpacked form of these addresses is written as 4 numbers joined by dot (10.30.96.10). One has to use some function to convert from "unpacked" form to "packed" one. To make things more complex (but easier to remember) people have introduced names that are associated with numbers. Those are referred as "domain-names". There's a service that maps names to IP addresses. There are special system functions for accessing that service. The service returns "packed" addresses.
Of course, if you have string '10.30.96.10', then you don't have to access DNS to obtain packed address. Function Socket::inet_aton can pack it for you. In this case, DNS can be contacted to obtain domains that are associated with this address (reverse lookup).
| [reply] |
Re: Net::Dns::packet creation.
by taint (Chaplain) on May 27, 2014 at 00:14 UTC
|
Something, with the possible exception(s) of shadrack, and cord-bin's examples (see; c4 in their examples), I can't help but notice the omission of IPv6.
This address type has quite a few differences, and as a result, is handled quite differently with regard to both requests, as well as answers. While I do manage quite a few DNS servers, and as a result, quite familiar with the protocol. I have not attempted to manufacture any low-level Perl scripts/Modules myself for this. I have found (so far) that the Modules already available on the CPAN have proved adequate for my needs. But I do appreciate the OP's curiosity, presumably leading them to attempt to "cobble" one up for themselves.
So I guess what I'm trying to say, is; while I don't know what your intent is for creating your own script/routine/procedure(s)/module. If it matters, don't overlook the IPv6 spec. This may also be a good reason to choose an already created Module. :)
Best wishes.
--Chris
¡λɐp ʇɑəɹ⅁ ɐ əʌɐɥ puɐ ʻꜱdləɥ ꜱᴉɥʇ ədoH
| [reply] |
|
|