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