summaryrefslogtreecommitdiffstats
path: root/vespalib
diff options
context:
space:
mode:
authorHenning Baldersheim <balder@yahoo-inc.com>2018-06-04 13:52:30 +0200
committerGitHub <noreply@github.com>2018-06-04 13:52:30 +0200
commit2d90dc15072147114defb8a21c00c7352cdc0920 (patch)
tree39c405af1c473f249463dedc592a5f0968f5d9d1 /vespalib
parent3e29a18b9522c06794ca83fe817e02cb74129e7a (diff)
parent95d1a9480abec61246652c7d9e5e575946d059c3 (diff)
Merge pull request #6055 from vespa-engine/havardpe/simplify-detect-validate-hostname
Havardpe/simplify detect validate hostname
Diffstat (limited to 'vespalib')
-rw-r--r--vespalib/src/apps/vespa-detect-hostname/detect_hostname.cpp31
-rw-r--r--vespalib/src/apps/vespa-validate-hostname/validate_hostname.cpp15
2 files changed, 21 insertions, 25 deletions
diff --git a/vespalib/src/apps/vespa-detect-hostname/detect_hostname.cpp b/vespalib/src/apps/vespa-detect-hostname/detect_hostname.cpp
index 057d45e8ef5..5c84790a669 100644
--- a/vespalib/src/apps/vespa-detect-hostname/detect_hostname.cpp
+++ b/vespalib/src/apps/vespa-detect-hostname/detect_hostname.cpp
@@ -4,6 +4,7 @@
#include <stdlib.h>
#include <vespa/vespalib/net/socket_address.h>
#include <vespa/vespalib/stllike/string.h>
+#include <vespa/vespalib/util/stringfmt.h>
#include <set>
using vespalib::SocketAddress;
@@ -22,14 +23,17 @@ vespalib::string get_hostname() {
return SocketAddress::normalize(&result[0]);
}
-bool check(const vespalib::string &name, const std::set<vespalib::string> &ip_set) {
+bool check(const vespalib::string &name, const std::set<vespalib::string> &ip_set, vespalib::string &error_msg) {
auto addr_list = SocketAddress::resolve(80, name.c_str());
if (addr_list.empty()) {
+ error_msg = vespalib::make_string("hostname '%s' could not be resolved", name.c_str());
return false;
}
for (const SocketAddress &addr: addr_list) {
vespalib::string ip_addr = addr.ip_address();
if (ip_set.count(ip_addr) == 0) {
+ error_msg = vespalib::make_string("hostname '%s' resolves to ip address not owned by this host (%s)",
+ name.c_str(), ip_addr.c_str());
return false;
}
}
@@ -38,14 +42,21 @@ bool check(const vespalib::string &name, const std::set<vespalib::string> &ip_se
int main(int, char **) {
auto my_ip_set = make_ip_set();
- std::vector<vespalib::string> list({get_hostname(), "localhost", "127.0.0.1", "::1"});
- for (const vespalib::string &name: list) {
- if (check(name, my_ip_set)) {
- fprintf(stdout, "%s\n", name.c_str());
- return 0;
- }
+ vespalib::string my_hostname = get_hostname();
+ vespalib::string my_hostname_error;
+ vespalib::string localhost = "localhost";
+ vespalib::string localhost_error;
+ if (check(my_hostname, my_ip_set, my_hostname_error)) {
+ fprintf(stdout, "%s\n", my_hostname.c_str());
+ } else if (check(localhost, my_ip_set, localhost_error)) {
+ fprintf(stdout, "%s\n", localhost.c_str());
+ } else {
+ fprintf(stderr, "FATAL: hostname detection failed\n");
+ fprintf(stderr, " INFO: canonical hostname (from gethostname/getaddrinfo): %s\n", my_hostname.c_str());
+ fprintf(stderr, " ERROR: %s\n", my_hostname_error.c_str());
+ fprintf(stderr, " INFO: falling back to local hostname: %s\n", localhost.c_str());
+ fprintf(stderr, " ERROR: %s\n", localhost_error.c_str());
+ return 1;
}
- fprintf(stderr, "FATAL: hostname detection failed\n");
- // XXX we should explain why it failed
- return 1;
+ return 0;
}
diff --git a/vespalib/src/apps/vespa-validate-hostname/validate_hostname.cpp b/vespalib/src/apps/vespa-validate-hostname/validate_hostname.cpp
index c97927884f8..88c2d07dd29 100644
--- a/vespalib/src/apps/vespa-validate-hostname/validate_hostname.cpp
+++ b/vespalib/src/apps/vespa-validate-hostname/validate_hostname.cpp
@@ -25,19 +25,6 @@ vespalib::string normalize(const vespalib::string &hostname) {
return canon_name;
}
-void check_reverse(const vespalib::string &hostname, const SocketAddress &addr) {
- std::set<vespalib::string> seen({hostname});
- vespalib::string reverse = addr.reverse_lookup();
- for (size_t i = 0; !reverse.empty() && (i < 10); ++i) {
- if (seen.count(reverse) == 0) {
- seen.insert(reverse);
- fprintf(stderr, "warning: hostname validation: found conflicting reverse lookup: '%s' -> %s -> '%s'\n",
- hostname.c_str(), addr.ip_address().c_str(), reverse.c_str());
- }
- reverse = addr.reverse_lookup();
- }
-}
-
int usage(const char *self) {
fprintf(stderr, "usage: %s <hostname>\n", self);
return 1;
@@ -62,8 +49,6 @@ int main(int argc, char **argv) {
valid = false;
fprintf(stderr, "FATAL: hostname validation failed: '%s' resolves to ip address not owned by this host (%s)\n",
hostname.c_str(), ip_addr.c_str());
- } else {
- check_reverse(hostname, addr);
}
}
return valid ? 0 : 1;