Commit 8fe1acb0 authored by Stefan Spettel's avatar Stefan Spettel

refact(smf): Remove unsafe gethostbyaddr and use conversion functions

Signed-off-by: default avatarStefan Spettel <stefan.spettel@eurecom.fr>
parent a0655af0
......@@ -72,3 +72,39 @@ bool fqdn::resolve(
return false;
}
bool fqdn::reverse_resolve(const std::string& ip_addr, std::string& host_name) {
Logger::smf_app().debug("Resolving an IP address (name %s)", ip_addr.c_str());
int tries = 0;
while (tries < MAX_NB_RESOLVE_TRIES) {
try {
boost::asio::io_context io_context = {};
Logger::smf_app().debug("Reverse Resolving Try #%u", tries);
boost::asio::ip::address addr =
boost::asio::ip::address::from_string(ip_addr);
boost::asio::ip::tcp::endpoint ep;
ep.address(addr);
boost::asio::ip::tcp::resolver resolver(io_context);
boost::asio::ip::tcp::resolver::results_type endpoints =
resolver.resolve(ep);
for (const auto& endpoint : endpoints) {
// get the first endpoint
host_name = endpoint.host_name();
return true;
}
} catch (std::exception& e) {
tries++;
if (tries == MAX_NB_RESOLVE_TRIES) {
Logger::smf_app().warn(
"Cannot resolve IP address %s: %s -- After %d tries",
ip_addr.c_str(), e.what(), tries);
return false;
}
std::this_thread::sleep_for(std::chrono::seconds(TIME_BETWEEN_TRIES));
}
}
return false;
}
\ No newline at end of file
......@@ -40,6 +40,15 @@ class fqdn {
static bool resolve(
const std::string& host_name, std::string& address, uint32_t& port,
uint8_t& addr_type, const std::string& protocol = "http");
/**
* @brief Resolves an IP address to get the hostname
* @param ip_address to resolve
* @param host_name result, if return=true
* @return true if successful
*/
static bool reverse_resolve(
const std::string& ip_address, std::string& host_name);
};
#endif /* FILE_FQDN_HPP_SEEN */
......@@ -285,7 +285,7 @@ std::string pfcp_association::get_printable_name() {
if (!ipv4_addresses.empty()) {
std::string addresses;
for (auto add : ipv4_addresses) {
addresses.append(inet_ntoa(add));
addresses.append(conv::toString(add));
addresses.append(";");
}
return addresses;
......@@ -343,11 +343,9 @@ bool pfcp_associations::resolve_upf_hostname(pfcp::node_id_t& node_id) {
node_id.fqdn.c_str());
return false;
}
struct sockaddr_in sa {};
switch (addr_type) {
case 0:
inet_pton(AF_INET, ip_addr.c_str(), &(sa.sin_addr));
node_id.u1.ipv4_address = sa.sin_addr;
node_id.u1.ipv4_address = conv::fromString(ip_addr);
return true;
case 1:
// TODO
......@@ -498,15 +496,21 @@ bool pfcp_associations::get_association(
// We didn't find association, may be because hash map is made with
// node_id_type FQDN
if (node_id.node_id_type == pfcp::NODE_ID_TYPE_IPV4_ADDRESS) {
struct hostent* hostname = gethostbyaddr(
&node_id.u1.ipv4_address.s_addr,
sizeof(node_id.u1.ipv4_address.s_addr), AF_INET);
std::string hostname;
std::string ip_str = conv::toString(node_id.u1.ipv4_address);
if (!fqdn::reverse_resolve(ip_str, hostname)) {
Logger::smf_app().warn(
"Could not resolve hostname for IP address %s", ip_str.c_str());
return false;
}
pfcp::node_id_t node_id_tmp = {};
node_id_tmp.node_id_type = pfcp::NODE_ID_TYPE_FQDN;
node_id_tmp.fqdn = hostname->h_name;
node_id_tmp.fqdn = hostname;
Logger::smf_app().debug(
"Hash lookup for association retry: Associated Hostname -> %s",
hostname->h_name);
hostname.c_str());
if (get_association(node_id_tmp, sa)) return true;
}
// We didn't found association, may be because hash map is made with
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment