blob: 057d45e8ef50ecd7b8a19f48117e471a226ca0f6 (
plain) (
blame)
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
|
// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include <stdio.h>
#include <stdlib.h>
#include <vespa/vespalib/net/socket_address.h>
#include <vespa/vespalib/stllike/string.h>
#include <set>
using vespalib::SocketAddress;
std::set<vespalib::string> make_ip_set() {
std::set<vespalib::string> result;
for (const auto &addr: SocketAddress::get_interfaces()) {
result.insert(addr.ip_address());
}
return result;
}
vespalib::string get_hostname() {
std::vector<char> result(4096, '\0');
gethostname(&result[0], 4000);
return SocketAddress::normalize(&result[0]);
}
bool check(const vespalib::string &name, const std::set<vespalib::string> &ip_set) {
auto addr_list = SocketAddress::resolve(80, name.c_str());
if (addr_list.empty()) {
return false;
}
for (const SocketAddress &addr: addr_list) {
vespalib::string ip_addr = addr.ip_address();
if (ip_set.count(ip_addr) == 0) {
return false;
}
}
return true;
}
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;
}
}
fprintf(stderr, "FATAL: hostname detection failed\n");
// XXX we should explain why it failed
return 1;
}
|