Skip to content

Instantly share code, notes, and snippets.

@hrbrmstr
Created August 4, 2014 03:00
Show Gist options
  • Save hrbrmstr/8c10e5ae313581dea023 to your computer and use it in GitHub Desktop.
Save hrbrmstr/8c10e5ae313581dea023 to your computer and use it in GitHub Desktop.
Rcpp + Boost version of gethostbyname and gethostbyaddr
#include <Rcpp.h>
#include <boost/asio.hpp>
using namespace Rcpp;
Function message("message"); // lets us use R's message() function
//[[Rcpp::export]]
std::vector< std::string > gethostbyname(std::string hostname) {
// setup storage for our return value
std::vector<std::string> addresses;
boost::asio::io_service io_service;
// we're dealing with network/connectivity 'stuff' + you never know
// when DNS queries will fail, so we need to handle exceptional cases
try {
// setup the resolver query
boost::asio::ip::tcp::resolver resolver(io_service);
boost::asio::ip::tcp::resolver::query query(hostname, "");
// prepare response iterator
boost::asio::ip::tcp::resolver::iterator destination = resolver.resolve(query);
boost::asio::ip::tcp::resolver::iterator end;
boost::asio::ip::tcp::endpoint endpoint;
// example of using a c-ish while loop to iterate through possible multiple resoponses
while (destination != end) {
endpoint = *destination++;
addresses.push_back(endpoint.address().to_string());
}
} catch(boost::system::system_error& error) {
message( "Hostname not found" );
}
return(addresses);
}
//[[Rcpp::export]]
std::vector< std::string > gethostbyaddr(std::string ipv4) {
// setup storage for our return value
std::vector<std::string> hostnames;
boost::asio::ip::tcp::endpoint endpoint;
boost::asio::io_service io_service;
// we're dealing with network/connectivity 'stuff' + you never know
// when DNS queries will fail, so we need to handle exceptional cases
try {
// setup the resolver query (for PTR record)
boost::asio::ip::address_v4 ip = boost::asio::ip::address_v4::from_string(ipv4);
endpoint.address(ip);
boost::asio::ip::tcp::resolver resolver(io_service);
// prepare response iterator
boost::asio::ip::tcp::resolver::iterator destination = resolver.resolve(endpoint);
boost::asio::ip::tcp::resolver::iterator end;
// example of using a for-loop to iterate through possible multiple resoponses
for (int i=1; destination != end; destination++, i++) {
hostnames.push_back(destination->host_name());
}
} catch(boost::system::system_error& error) {
message( "Address not found" );
}
return(hostnames);
}
@dothebart
Copy link

hm, when trying to look up my own fqdn the c way:

  hn = gethostbyname("bruecklinux");
  std::cout << "xx " << hn->h_name << "\n";

will give me:
bruecklinux.arangodb.biz
while your example gives me:
127.0.1.1
which is what my /etc/hosts says for localhost with name:

127.0.1.1       bruecklinux.arangodb.biz        bruecklinux

Is there a way to get the FQDN of the host I'm running on using boost?

@hrbrmstr
Copy link
Author

hrbrmstr commented Sep 5, 2019

I'd highly suggest just using {iptools} but you can use the far-more-recently-updated-than-this-gist code for said pkg and lookup functionality at https://github.com/hrbrmstr/iptools/blob/master/src/asio_bindings.cpp#L161-L177

adding your info to my /etc/hosts in the same fashion results in

iptools::hostname_to_ip("bruecklinux")
## [[1]]
## [1] "::1"       "127.0.0.1"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment