DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
get_ips.hpp
Go to the documentation of this file.
1
11#ifndef UTILITIES_INCLUDE_UTILITIES_GET_IPS_HPP_
12#define UTILITIES_INCLUDE_UTILITIES_GET_IPS_HPP_
13
14#include "logging/Logging.hpp"
15#include "utilities/Issues.hpp"
16
17#include <arpa/nameser.h>
18#include <ifaddrs.h>
19#include <netdb.h>
20#include <netinet/in.h>
21#include <resolv.h>
22#include <sys/types.h>
23
24#include <cerrno> // NOLINT(runtime/output_format
25#include <string>
26#include <vector>
27
28namespace dunedaq::utilities {
29
30inline std::vector<std::string>
31get_hostname_ips(std::string hostname)
32{
33 std::vector<std::string> output;
34
35 TLOG_DEBUG(12) << "Name is " << hostname;
36
37 struct addrinfo* addrinfo = nullptr;
38 auto s = getaddrinfo(hostname.c_str(), nullptr, nullptr, &addrinfo);
39
40 if (s != 0) {
41 ers::error(NameNotFound(ERS_HERE, hostname, std::string(gai_strerror(s))));
42 return output;
43 }
44
45 for (auto rp = addrinfo; rp != nullptr; rp = rp->ai_next) {
46 char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV]; // NOLINT
47
48 // Let's skip all the IPv6 here
49 if (rp->ai_family == AF_INET6)
50 continue;
51
52 getnameinfo(rp->ai_addr, rp->ai_addrlen, hbuf, sizeof(hbuf), sbuf, sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV);
53 auto ipaddr = std::string(hbuf);
54 bool duplicate = false;
55 for (auto& ip : output) {
56 if (ip == ipaddr) {
57 duplicate = true;
58 break;
59 }
60 }
61 if (!duplicate) {
62 TLOG_DEBUG(13) << "Found address " << ipaddr << " for hostname " << hostname;
63 output.push_back(ipaddr);
64 }
65 }
66
67 freeaddrinfo(addrinfo);
68
69 return output;
70}
71
72inline std::string
73get_interface_ip(std::string eth_device_name, bool throw_if_missing = false)
74{
75 std::string ipaddr = "0.0.0.0";
76 char hostname[NI_MAXHOST]; // NOLINT This is a char array for interfacing with the C networking API
77 if (gethostname(&hostname[0], NI_MAXHOST) == 0) {
78 ipaddr = std::string(hostname);
79 }
80 bool eth_found = false;
81 if (eth_device_name != "") {
82 // Work out which ip address goes with this device
83 struct ifaddrs* ifaddr = nullptr;
84 getifaddrs(&ifaddr);
85
86 for (auto ifa = ifaddr; ifa != nullptr; ifa = ifa->ifa_next) {
87 if (ifa->ifa_addr == nullptr || std::string(ifa->ifa_name) != eth_device_name) {
88 continue;
89 }
90
91 char ip[NI_MAXHOST]; // NOLINT This is a char array for interfacing with the C networking API
92 int status = getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in), ip, NI_MAXHOST, nullptr, 0, NI_NUMERICHOST);
93 if (status != 0) {
94 continue;
95 }
96 eth_found = true;
97 ipaddr = std::string(ip);
98 break;
99 }
100 freeifaddrs(ifaddr);
101
102 if (!eth_found) {
103 auto err = InterfaceNotFound(ERS_HERE, eth_device_name);
104
105 if (throw_if_missing)
106 throw err;
107 else
108 ers::warning(err);
109 }
110 }
111
112 return ipaddr;
113}
114
115} // namespace dunedaq::utilities
116
117#endif // UTILITIES_INCLUDE_UTILITIES_GET_IPS_HPP_
#define ERS_HERE
#define TLOG_DEBUG(lvl,...)
Definition Logging.hpp:112
std::string get_interface_ip(std::string eth_device_name, bool throw_if_missing=false)
Definition get_ips.hpp:73
std::vector< std::string > get_hostname_ips(std::string hostname)
Definition get_ips.hpp:31
NameNotFound
Definition Issues.hpp:24
void warning(const Issue &issue)
Definition ers.hpp:115
void error(const Issue &issue)
Definition ers.hpp:81