DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
Resolver.cpp
Go to the documentation of this file.
1
11
12std::vector<std::string>
14{
15 std::vector<std::string> output;
16
17 TLOG_DEBUG(12) << "Name is " << hostname;
18
19 struct addrinfo* result;
20 auto s = getaddrinfo(hostname.c_str(), nullptr, nullptr, &result);
21
22 if (s != 0) {
23 ers::error(NameNotFound(ERS_HERE, hostname, std::string(gai_strerror(s))));
24 return output;
25 }
26
27 for (auto rp = result; rp != nullptr; rp = rp->ai_next) {
28 char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
29
30 // Let's skip all the IPv6 here
31 if (rp->ai_family == AF_INET6)
32 continue;
33
34 getnameinfo(rp->ai_addr, rp->ai_addrlen, hbuf, sizeof(hbuf), sbuf, sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV);
35 auto result = std::string(hbuf);
36 bool duplicate = false;
37 for (auto& res : output) {
38 if (res == result) {
39 duplicate = true;
40 break;
41 }
42 }
43 if (!duplicate) {
44 TLOG_DEBUG(13) << "Found address " << result << " for hostname " << hostname;
45 output.push_back(result);
46 }
47 }
48
49 freeaddrinfo(result);
50
51 return output;
52}
53
54std::vector<std::string>
55dunedaq::utilities::resolve_uri_hostname(std::string connection_string)
56{
57 auto uri = parse_connection_string(connection_string);
58
59 if(uri.scheme == "tcp") {
60 auto output = get_ips_from_hostname(uri.host);
61
62 for (size_t ii = 0; ii < output.size(); ++ii) {
63 output[ii] = "tcp://" + output[ii] + ":" + uri.port;
64 }
65 return output;
66 } else {
67 return { connection_string };
68 }
69}
70
72dunedaq::utilities::parse_connection_string(std::string connection_string)
73{
74 // ZMQ URIs are formatted as follows: tcp://{host}:{port}
75 ZmqUri output;
76
77 if (connection_string.find("://") == std::string::npos) {
78 throw InvalidUri(ERS_HERE, connection_string);
79 }
80
81 output.scheme = connection_string.substr(0, connection_string.find("://"));
82 connection_string = connection_string.substr(connection_string.find("://") + 3);
83
84 if (connection_string.find(":") != std::string::npos) {
85 output.port = connection_string.substr(connection_string.find(":") + 1);
86 connection_string = connection_string.substr(0, connection_string.find(":"));
87 }
88 output.host = connection_string;
89
90 return output;
91}
#define ERS_HERE
#define TLOG_DEBUG(lvl,...)
Definition Logging.hpp:112
ZmqUri parse_connection_string(std::string connection_string)
Definition Resolver.cpp:72
std::vector< std::string > get_ips_from_hostname(std::string hostname)
Definition Resolver.cpp:13
std::vector< std::string > resolve_uri_hostname(std::string connection_string)
Definition Resolver.cpp:55
NameNotFound
Definition Issues.hpp:29
Unsupported std::string uri Execution of command std::string error Failed to create CommandFacility uri
Definition Issues.hpp:77
void error(const Issue &issue)
Definition ers.hpp:81