From a9bc0348aea49a9c76738e68c2a46e2014119e9b Mon Sep 17 00:00:00 2001 From: yuvaramachandran_gurusamy Date: Wed, 10 Apr 2024 13:12:03 +0000 Subject: [PATCH] RDKTV-30016 - [miracast] Connection fails with "ENT-32203/ENT-32102/ENT-32103" after Thunder restart Signed-off-by: yuvaramachandran_gurusamy --- .../MiracastService/MiracastController.cpp | 52 ++++++++++++++++++- Miracast/RTSP/MiracastRtspMsg.cpp | 7 +++ Miracast/include/MiracastController.h | 4 ++ 3 files changed, 62 insertions(+), 1 deletion(-) diff --git a/Miracast/MiracastService/MiracastController.cpp b/Miracast/MiracastService/MiracastController.cpp index 3cc84e146d..5a798d0ba9 100644 --- a/Miracast/MiracastService/MiracastController.cpp +++ b/Miracast/MiracastService/MiracastController.cpp @@ -206,6 +206,53 @@ std::string MiracastController::parse_p2p_event_data(const char *tmpBuff, const return std::string(" "); } +std::string MiracastController::getifNameByIPv4(std::string ip_address) +{ + struct ifaddrs *ifaddrList = nullptr, *currentifa = nullptr; + int family, s; + char host[MAX_IFACE_NAME_LEN]; + std::string ifaceName = ""; + + // Get list of all network interfaces + if ( -1 == getifaddrs(&ifaddrList)) + { + MIRACASTLOG_ERROR("getifaddrs failed[%s]",strerror(errno)); + } + else + { + // Iterate through the list of network interfaces + for ( currentifa = ifaddrList; nullptr != currentifa; currentifa = currentifa->ifa_next) + { + if ( nullptr == currentifa->ifa_addr ) + { + continue; + } + family = currentifa->ifa_addr->sa_family; + // Check for IPv4 address + if ( AF_INET == family ) + { + s = getnameinfo(currentifa->ifa_addr, sizeof(struct sockaddr_in), host, MAX_IFACE_NAME_LEN, NULL, 0, NI_NUMERICHOST); + if (s != 0) + { + MIRACASTLOG_ERROR("getnameinfo failed[%s]",strerror(errno)); + break; + } + // Compare IP address with the given IP + if ( 0 == strcmp(host, ip_address.c_str())) + { + ifaceName = currentifa->ifa_name; + } + } + } + if ( nullptr != ifaddrList ) + { + freeifaddrs(ifaddrList); + ifaddrList = nullptr; + } + } + return ifaceName; +} + std::string MiracastController::start_DHCPClient(std::string interface, std::string &default_gw_ip_addr) { MIRACASTLOG_TRACE("Entering..."); @@ -484,7 +531,10 @@ MiracastError MiracastController::set_WFDParameters(void) { MIRACASTLOG_TRACE("Entering..."); MiracastError ret = MIRACAST_FAIL; - if (nullptr != m_p2p_ctrl_obj){ + if (nullptr != m_p2p_ctrl_obj) + { + std::string ifName = getifNameByIPv4("192.168.59.1"); + m_p2p_ctrl_obj->remove_GroupInterface(ifName); ret = m_p2p_ctrl_obj->set_WFDParameters(); } MIRACASTLOG_TRACE("Exiting..."); diff --git a/Miracast/RTSP/MiracastRtspMsg.cpp b/Miracast/RTSP/MiracastRtspMsg.cpp index 74d335d700..d68744b97d 100644 --- a/Miracast/RTSP/MiracastRtspMsg.cpp +++ b/Miracast/RTSP/MiracastRtspMsg.cpp @@ -1024,6 +1024,13 @@ MiracastError MiracastRTSPMsg::initiate_TCP(std::string goIP) MIRACASTLOG_ERROR("TCP Socket creation error %s", strerror(errno)); continue; } + // Set SO_REUSEADDR option + int optval = 1; + if (setsockopt(m_tcpSockfd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)) == -1) + { + MIRACASTLOG_ERROR("Failed to set SO_REUSEADDR: %s", strerror(errno)); + continue; + } #if 0 /* Bind socket */ if (bind(m_tcpSockfd, (struct sockaddr *)&addr, sizeof(addr)) < 0) diff --git a/Miracast/include/MiracastController.h b/Miracast/include/MiracastController.h index e94e865c51..1f6d34b5c3 100644 --- a/Miracast/include/MiracastController.h +++ b/Miracast/include/MiracastController.h @@ -32,6 +32,8 @@ #include #include #include +#include +#include #include #include "MiracastP2P.h" #include "MiracastLogger.h" @@ -41,6 +43,7 @@ using namespace std; using namespace MIRACAST; #define THUNDER_REQ_THREAD_CLIENT_CONNECTION_WAITTIME (30) +#define MAX_IFACE_NAME_LEN 16 class MiracastController { @@ -124,6 +127,7 @@ class MiracastController MiracastError create_ControllerFramework(std::string p2p_ctrl_iface); MiracastError destroy_ControllerFramework(void); void checkAndInitiateP2PBackendDiscovery(void); + std::string getifNameByIPv4(std::string ip_address); void set_localIp(std::string ipAddr);