Skip to content

Commit

Permalink
dns resolver intial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
cmuhammedrafi committed Dec 16, 2024
1 parent 3ee8510 commit 8b6d055
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
67 changes: 67 additions & 0 deletions NetworkManagerConnectivity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
18 changes: 18 additions & 0 deletions NetworkManagerConnectivity.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

#pragma once

#include <netdb.h>
#include <arpa/inet.h>
#include <cstring>
#include <atomic>
#include <vector>
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 8b6d055

Please sign in to comment.