Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DNS Reverse Lookup with Asio

I would like to do a DNS reverse lookup (return hostname for a given IP Address) with asio, but I am not able to figure out which components I need to achieve this. Asio documentiation refers to ip::basic_resolver::resolve, but an endpoint_type is needed and I don't know how to use it.
Could someone please post or refer to an example?


EDIT:
With Joachim Pileborg's help I was able to accomplish the task. Needed code (Minumin without error handling):

#include <asio.hpp>
#include <string>
#include <iostream>

int main()
{
    asio::ip::address_v4 ipa = asio::ip::address_v4::from_string("8.8.8.8");    
    asio::ip::tcp::endpoint ep;
    ep.address(ipa);

    asio::io_service io_service;
    asio::ip::tcp::resolver resolver(io_service);
    asio::ip::tcp::resolver::iterator destination = resolver.resolve(ep);

    std::cout << destination->host_name() << std::endl;

    return 0;
}
like image 528
mspoerr Avatar asked Jan 20 '12 10:01

mspoerr


People also ask

How do I do a reverse DNS lookup?

Type in an IP address (for example, 8.8. 8.8) and press enter. The tool will perform a reverse DNS lookup and return the name record for that IP address.

Which DNS record type performs reverse DNS lookup?

The PTR record is used for reverse DNS lookups.


1 Answers

I haven't used the resolver in Boost ASIO my self, but reading through the reference documentation it seems you shouldn't be using ip::basic_resolver directly. Instead you should use e.g. ip::tcp::resolver in which case the endpoint is an instance of ip::tcp::endpoint.

Edit

As each host can have multiple host names, the OPs solution could be extended like this:

asio::ip::tcp::resolver::iterator itr = resolver.resolve(ep);
asio::ip::tcp::resolver::iterator end;

for (int i = 1; itr != end; itr++, i++)
    std::cout << "hostname #" << i << ": " << itr->host_name() << '\n';
like image 162
Some programmer dude Avatar answered Sep 28 '22 06:09

Some programmer dude