diff --git a/NetworkManagerConnectivity.cpp b/NetworkManagerConnectivity.cpp index 1a4667e..1e8b9e0 100644 --- a/NetworkManagerConnectivity.cpp +++ b/NetworkManagerConnectivity.cpp @@ -47,6 +47,73 @@ namespace WPEFramework } } + bool DnsResolver::resolveIP(std::string& uri) + { + struct addrinfo sockAddrProps, *resultAddr= NULL; + char ipStr[INET6_ADDRSTRLEN] = {0}; + + sockAddrProps.ai_family = AF_UNSPEC; + sockAddrProps.ai_socktype = SOCK_STREAM; + sockAddrProps.ai_flags = 0; + sockAddrProps.ai_protocol = 0; + + int ret = getaddrinfo(uri.c_str(), NULL, &sockAddrProps, &resultAddr); + if (ret != 0) { + NMLOG_WARNING("getaddrinfo: %s", gai_strerror(ret)); + return false; + } + + NMLOG_DEBUG("Resolved IP addresses for %s", uri.c_str()); + + for (struct addrinfo * resutIP = resultAddr; resutIP != NULL; resutIP = resutIP->ai_next) + { + void *addr= NULL; + if (resutIP->ai_family == AF_INET) { + struct sockaddr_in *ipv4 = (struct sockaddr_in *)resutIP->ai_addr; + addr = &(ipv4->sin_addr); + ipv4Resolved = true; + } else if (resutIP->ai_family == AF_INET6) { + struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)resutIP->ai_addr; + addr = &(ipv6->sin6_addr); + ipv6Resolved = true; + } else { + continue; + } + + inet_ntop(resutIP->ai_family, addr, ipStr, sizeof(ipStr)); + NMLOG_DEBUG(" --> %s", ipStr); + } + + freeaddrinfo(resultAddr); + return true; + } + + std::string DnsResolver::convertUrIToDomainName(std::string& url) + { + size_t domainStart = 0; + size_t protocolEnd = url.find("://"); + if (protocolEnd != std::string::npos) { + domainStart = protocolEnd + 3; + } + + size_t domainEnd = url.find('/', domainStart); + if (domainEnd != std::string::npos) + return url.substr(domainStart, domainEnd - domainStart); + else + return url.substr(domainStart); + } + + DnsResolver::DnsResolver(std::string url) + { + if(url.empty()) { + NMLOG_ERROR("URI/hostname missing"); + return; + } + + m_domain = convertUrIToDomainName(url); + resolveIP(m_domain); + } + bool EndpointCache::isEndpointCashFileExist() { std::ifstream fileStream(CachefilePath); diff --git a/NetworkManagerConnectivity.h b/NetworkManagerConnectivity.h index 3a5b215..cbf1959 100644 --- a/NetworkManagerConnectivity.h +++ b/NetworkManagerConnectivity.h @@ -19,6 +19,8 @@ #pragma once +#include +#include #include #include #include @@ -77,6 +79,22 @@ namespace WPEFramework { namespace Plugin { + + class DnsResolver + { + private: + std::string m_domain{}; + bool ipv4Resolved = false; + bool ipv6Resolved = false; + std::string convertUrIToDomainName(std::string& url); + virtual bool resolveIP(std::string& uri); + + public: + DnsResolver(std::string url); + ~DnsResolver(){}; + bool operator()() { return (ipv6Resolved || ipv4Resolved);} + }; + /* save user specific endponint in to a chache file and load form the file if monitorEndpoints are empty case wpeframework restared */ class EndpointCache { public: