-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
62 lines (52 loc) · 1.62 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <string>
#include <iostream>
#include <sstream>
#include <boost/program_options.hpp>
#include "resolver.hpp"
namespace po = boost::program_options;
std::string format_ip(const std::vector<uint8_t> & ip)
{
std::ostringstream stream;
for (size_t i = 0; i < ip.size(); ++i) {
if (i)
stream << '.';
stream << (int)ip[i];
}
return stream.str();
}
int main(int argc, char *argv[])
{
po::options_description desc("Allowed options");
desc.add_options()
("help,h", "produce help message")
("hostname", po::value<std::string>(), "host to resolve")
("nameserver", po::value<std::string>(), "user provided nameserver (if not provided /etc/resolv.conf will be used)")
;
po::positional_options_description p;
p.add("hostname", 1);
po::variables_map vm;
po::store(po::command_line_parser(argc, argv).options(desc).positional(p).run(), vm);
po::notify(vm);
if (vm.count("help")) {
std::cout << desc << std::endl;
return EXIT_SUCCESS;
}
dns::resolver resolver;
if (vm.count("nameserver"))
resolver.set_nameserver(vm["nameserver"].as<std::string>());
if (!vm.count("hostname")) {
std::cerr << "Hostname not provided.\n";
std::cerr << desc << std::endl;
return EXIT_FAILURE;
}
std::string hostname = vm["hostname"].as<std::string>();
dns::hostent h = resolver.gethostbyname(hostname);
std::cout << "Hostname " << h.name << " has:\n";
for (const auto & alias : h.aliases) {
std::cout << "alias:\t" << alias << std::endl;
}
for (const auto & ip : h.addresses) {
std::cout << "IPv4 address:\t" << format_ip(ip) << std::endl;
}
return 0;
}