mirror of
https://github.com/wichtounet/thor-os.git
synced 2025-09-09 04:22:04 -04:00
Resolve domain names in ping
This commit is contained in:
parent
6bc6580bb3
commit
13ce3e4e33
@ -9,12 +9,39 @@
|
|||||||
#include <tlib/errors.hpp>
|
#include <tlib/errors.hpp>
|
||||||
#include <tlib/print.hpp>
|
#include <tlib/print.hpp>
|
||||||
#include <tlib/net.hpp>
|
#include <tlib/net.hpp>
|
||||||
|
#include <tlib/dns.hpp>
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
static constexpr const size_t N = 4;
|
static constexpr const size_t N = 4;
|
||||||
static constexpr const size_t timeout_ms = 2000;
|
static constexpr const size_t timeout_ms = 2000;
|
||||||
|
|
||||||
|
bool is_digit(char c){
|
||||||
|
return c >= '0' && c <= '9';
|
||||||
|
}
|
||||||
|
|
||||||
|
bool is_ip(const std::string& value){
|
||||||
|
auto ip_parts = std::split(value, '.');
|
||||||
|
|
||||||
|
if(ip_parts.size() != 4){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
for(auto& part : ip_parts){
|
||||||
|
if(part.empty() || part.size() > 3){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
for(size_t i = 0; i < part.size(); ++i){
|
||||||
|
if(!is_digit(part[i])){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
} // end of anonymous namespace
|
} // end of anonymous namespace
|
||||||
|
|
||||||
int main(int argc, char* argv[]) {
|
int main(int argc, char* argv[]) {
|
||||||
@ -23,7 +50,22 @@ int main(int argc, char* argv[]) {
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string ip(argv[1]);
|
std::string query(argv[1]);
|
||||||
|
|
||||||
|
std::string ip;
|
||||||
|
|
||||||
|
if (is_ip(query)) {
|
||||||
|
ip = query;
|
||||||
|
} else {
|
||||||
|
auto resolved = tlib::dns::resolve(query);
|
||||||
|
|
||||||
|
if(resolved){
|
||||||
|
ip = *resolved;
|
||||||
|
} else {
|
||||||
|
tlib::printf("ping: failed to resolve name: %s\n", std::error_message(resolved.error()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
auto ip_parts = std::split(ip, '.');
|
auto ip_parts = std::split(ip, '.');
|
||||||
|
|
||||||
if (ip_parts.size() != 4) {
|
if (ip_parts.size() != 4) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user