Skip to content

Commit

Permalink
connect only logic added
Browse files Browse the repository at this point in the history
  • Loading branch information
cmuhammedrafi committed Mar 28, 2024
1 parent 259e4a3 commit 595ecc2
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
34 changes: 26 additions & 8 deletions Network/NetworkConnectivity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,9 @@ namespace WPEFramework {
else
{
if(isConnectivityMonitorEndpointSet())
internetState = testConnectivity(getConnectivityMonitorEndpoints(), 10000, ipversion);
internetState = testConnectivity(getConnectivityMonitorEndpoints(), TEST_CONNECTIVITY_DEFAULT_TIMEOUT_MS, ipversion, false);
else
internetState = testConnectivity(getConnectivityDefaultEndpoints(), 10000, ipversion);
internetState = testConnectivity(getConnectivityDefaultEndpoints(), TEST_CONNECTIVITY_DEFAULT_TIMEOUT_MS, ipversion, false);
}

return internetState;
Expand Down Expand Up @@ -237,7 +237,8 @@ namespace WPEFramework {

static bool curlVerboseEnabled() {
std::ifstream fileStream("/tmp/network.plugin.debug");
return fileStream.is_open();
//return fileStream.is_open();
return true;
}

static long current_time ()
Expand All @@ -253,7 +254,7 @@ namespace WPEFramework {
return size * nmemb;
}

nsm_internetState Connectivity::testConnectivity(const std::vector<std::string>& endpoints, long timeout_ms, nsm_ipversion ipversion)
nsm_internetState Connectivity::testConnectivity(const std::vector<std::string>& endpoints, long timeout_ms, nsm_ipversion ipversion, bool connectOnly)
{
long deadline = current_time() + timeout_ms, time_now = 0, time_earlier = 0;
if(endpoints.size() < 1) {
Expand Down Expand Up @@ -288,7 +289,10 @@ namespace WPEFramework {
curl_easy_setopt(curl_easy_handle, CURLOPT_HTTPHEADER, chunk);
curl_easy_setopt(curl_easy_handle, CURLOPT_USERAGENT, "RDKCaptiveCheck/1.0");
/* set CURLOPT_HTTPGET option insted of CURLOPT_CONNECT_ONLY bcause we need to get the captiveportal URI not just connection only */
curl_easy_setopt(curl_easy_handle, CURLOPT_HTTPGET, 1L);
if(connectOnly)
curl_easy_setopt(curl_easy_handle, CURLOPT_CONNECT_ONLY, 1L);
else
curl_easy_setopt(curl_easy_handle, CURLOPT_HTTPGET, 1L);
curl_easy_setopt(curl_easy_handle, CURLOPT_WRITEFUNCTION, writeFunction);
curl_easy_setopt(curl_easy_handle, CURLOPT_TIMEOUT_MS, deadline - current_time());
if ((ipversion == CURL_IPRESOLVE_V4) || (ipversion == CURL_IPRESOLVE_V6))
Expand Down Expand Up @@ -323,7 +327,11 @@ namespace WPEFramework {
continue;
if (CURLE_OK == msg->data.result) {
curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE, &endpoint);
if (curl_easy_getinfo(msg->easy_handle, CURLINFO_RESPONSE_CODE, &response_code) == CURLE_OK) {
if(connectOnly) {
LOGINFO("curl check type CURLOPT_CONNECT_ONLY");
response_code = HttpStatus_204_No_Content;
}
else if (curl_easy_getinfo(msg->easy_handle, CURLINFO_RESPONSE_CODE, &response_code) == CURLE_OK) {
if(curlVerboseEnabled())
LOGINFO("endpoint = <%s> response code <%d>",endpoint, static_cast<int>(response_code));
if (HttpStatus_302_Found == response_code) {
Expand All @@ -334,8 +342,13 @@ namespace WPEFramework {
}
}
}
else
else {
LOGERR("endpoint = <%s> error = %d (%s)", endpoint, msg->data.result, curl_easy_strerror(msg->data.result));
// if (msg->data.result == CURLE_OPERATION_TIMEDOUT)
// {
// response_code = HttpStatus_204_No_Content;
// }
}
http_responses.push_back(response_code);
}
time_earlier = time_now;
Expand Down Expand Up @@ -503,7 +516,12 @@ namespace WPEFramework {
nsm_internetState InternetConnectionState = nsm_internetState::UNKNOWN;
while (!stopFlag)
{
InternetConnectionState = testConnectivity(getConnectivityMonitorEndpoints(), 10000, NSM_IPRESOLVE_WHATEVER);
if(g_internetState.load() == nsm_internetState::FULLY_CONNECTED)
/*if previous check was fully connected then do connect only curl check*/
InternetConnectionState = testConnectivity(getConnectivityMonitorEndpoints(), TEST_CONNECTIVITY_DEFAULT_TIMEOUT_MS, NSM_IPRESOLVE_WHATEVER, true);
else
InternetConnectionState = testConnectivity(getConnectivityMonitorEndpoints(), TEST_CONNECTIVITY_DEFAULT_TIMEOUT_MS, NSM_IPRESOLVE_WHATEVER, false);

if(g_internetState.load() != InternetConnectionState)
{
g_internetState.store(InternetConnectionState);
Expand Down
3 changes: 2 additions & 1 deletion Network/NetworkConnectivity.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#define CAPTIVEPORTAL_MAX_LEN 512
#define DEFAULT_MONITOR_TIMEOUT 60 // in seconds
#define MONITOR_TIMEOUT_INTERVAL_MIN 5
#define TEST_CONNECTIVITY_DEFAULT_TIMEOUT_MS 5000

enum nsm_ipversion {
NSM_IPRESOLVE_WHATEVER = 0, /* default, resolves addresses to all IP*/
Expand Down Expand Up @@ -81,7 +82,7 @@ namespace WPEFramework {
}
~Connectivity(){}

nsm_internetState testConnectivity(const std::vector<std::string>& endpoints, long timeout_ms, nsm_ipversion ipversion);
nsm_internetState testConnectivity(const std::vector<std::string>& endpoints, long timeout_ms, nsm_ipversion ipversion, bool connectOnly);
std::vector<std::string> getConnectivityDefaultEndpoints() { return m_defaultEndpoints; };
std::string getCaptivePortal() { const std::lock_guard<std::mutex> lock(capitiveMutex); return g_captivePortal; }
void setCaptivePortal(const char* captivePortal) {const std::lock_guard<std::mutex> lock(capitiveMutex); g_captivePortal = captivePortal; }
Expand Down

0 comments on commit 595ecc2

Please sign in to comment.