From 77bbc184e2cb41da4889379d93fa20ef6293d4c7 Mon Sep 17 00:00:00 2001 From: Annie Rashmitha Date: Wed, 7 Jan 2026 11:16:56 +0000 Subject: [PATCH 1/2] Memory leak fix --- source/InterDeviceManager/Idm_TCP_apis.c | 23 +++++- source/InterDeviceManager/Idm_TCP_apis.h | 3 + .../InterDeviceManager/Idm_call_back_apis.c | 8 +- source/InterDeviceManager/Idm_msg_process.c | 75 +++++++++++++++---- 4 files changed, 90 insertions(+), 19 deletions(-) diff --git a/source/InterDeviceManager/Idm_TCP_apis.c b/source/InterDeviceManager/Idm_TCP_apis.c index b5af977..541dff5 100644 --- a/source/InterDeviceManager/Idm_TCP_apis.c +++ b/source/InterDeviceManager/Idm_TCP_apis.c @@ -41,7 +41,8 @@ extern char g_sslSeCA[128]; #endif bool ssl_lib_init = false; bool TCP_server_started = false; - +bool connect_reset = false; +pthread_mutex_t connect_reset_mutex = PTHREAD_MUTEX_INITIALIZER; typedef int (*callback_recv)( connection_info_t* conn_info, void *payload); typedef struct tcp_server_threadargs @@ -526,8 +527,20 @@ int open_remote_connection(connection_config_t* connectionConf, int (*connection servaddr.sin_port = htons(connectionConf->port); CcspTraceInfo(("waiting to connect to the IDM server..\n")); + connect_reset = false; + while (1) { + pthread_mutex_lock(&connect_reset_mutex); + if (connect_reset == true) + { + CcspTraceInfo(("Connect stopped since discovery is restarted")); + pthread_mutex_unlock(&connect_reset_mutex); + close(client_sockfd); + return -1; //This discovery is omitted due to discovery restart + } + pthread_mutex_unlock(&connect_reset_mutex); + // Wait indefinitely untill other end idm server accepts the connection if (connect(client_sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) != 0) { @@ -538,6 +551,7 @@ int open_remote_connection(connection_config_t* connectionConf, int (*connection break; } } + //TODO: check for dynamic allocation connection_info_t conn_info; conn_info.conn = client_sockfd; @@ -846,12 +860,15 @@ int send_remote_message(connection_info_t* conn_info,void *payload) #ifndef IDM_DEBUG int val; if (conn_info->enc.ctx != NULL && conn_info->enc.ssl != NULL) { - if ((val = SSL_write(conn_info->enc.ssl, payload, sizeof(payload_t))) > 0) { + val = SSL_write(conn_info->enc.ssl, payload, sizeof(payload_t)); + if (val > 0) { + CcspTraceInfo(("(%s:%d) SSL_write successful connection id %d \n", __FUNCTION__, __LINE__,conn_info->conn)); return 0; } else { - CcspTraceError(("(%s:%d) Data encryption failed (Err: %d)", __FUNCTION__, __LINE__, val)); + int ssl_err = SSL_get_error(conn_info->enc.ssl, val); + CcspTraceError(("(%s:%d) SSL_write failed (Ret: %d, SSL Error: %d)\n", __FUNCTION__, __LINE__, val, ssl_err)); } } else diff --git a/source/InterDeviceManager/Idm_TCP_apis.h b/source/InterDeviceManager/Idm_TCP_apis.h index 1d92c4b..2f33111 100644 --- a/source/InterDeviceManager/Idm_TCP_apis.h +++ b/source/InterDeviceManager/Idm_TCP_apis.h @@ -23,6 +23,9 @@ #include "Idm_rbus.h" #include "Idm_msg_process.h" +extern bool connect_reset; +extern pthread_mutex_t connect_reset_mutex; + int open_remote_connection(connection_config_t* connectionConf, int (*connection_cb)(device_info_t* Device, connection_info_t* conn_info, uint encryption_status), int (*rcv_message_cb)( connection_info_t* conn_info, void *payload)) ; int send_remote_message(connection_info_t* conn_info, void *payload); diff --git a/source/InterDeviceManager/Idm_call_back_apis.c b/source/InterDeviceManager/Idm_call_back_apis.c index 1b4dc44..fe7d661 100644 --- a/source/InterDeviceManager/Idm_call_back_apis.c +++ b/source/InterDeviceManager/Idm_call_back_apis.c @@ -77,9 +77,10 @@ int stop_discovery(); int rcv_message_cb( connection_info_t* conn_info, void *payload) { - CcspTraceInfo(("%s %d - \n", __FUNCTION__, __LINE__)); payload_t *recvData = (payload_t*)payload; + CcspTraceInfo(("%s %d - msgType-%d \n", __FUNCTION__, __LINE__,recvData->msgType)); + if(recvData->msgType == REQ) { IDM_Incoming_Request_handler(recvData); @@ -823,6 +824,11 @@ ANSC_STATUS IDM_Start_Device_Discovery() ANSC_STATUS IDM_Stop_Device_Discovery() { CcspTraceInfo(("%s %d - called\n", __FUNCTION__, __LINE__ )); + + pthread_mutex_lock(&connect_reset_mutex); + connect_reset = true; //To exit any previous waiting connect + pthread_mutex_unlock(&connect_reset_mutex); + if(stop_discovery() !=0) { CcspTraceError(("%s %d - stop_discovery failed\n", __FUNCTION__, __LINE__)); diff --git a/source/InterDeviceManager/Idm_msg_process.c b/source/InterDeviceManager/Idm_msg_process.c index b0a7bba..59c9ec4 100644 --- a/source/InterDeviceManager/Idm_msg_process.c +++ b/source/InterDeviceManager/Idm_msg_process.c @@ -109,6 +109,25 @@ sendReqList* IDM_getFromSendRequestList(uint reqID) } } +sendReqList* IDM_searchFromSendRequestList(const char *param_mac) +{ + + if(param_mac == NULL) + { + return NULL; + } + + sendReqList *cur = headsendReqList; + while (cur != NULL) + { + if (strncmp(cur->Mac_dest, param_mac, sizeof(cur->Mac_dest) - 1) == 0) + { + return cur; + } + cur = cur->next; + } + return NULL; +} void IDM_addToSendSubscriptionuestList( sendSubscriptionList *newSubscription) { @@ -318,31 +337,40 @@ ANSC_STATUS IDM_sendMsg_to_Remote_device(idm_send_msg_Params_t *param) memset(&payload, 0, sizeof(payload_t)); if(param->operation == GET || param->operation == SET || param->operation == IDM_REQUEST) { - /* Create request entry */ - sendReqList *newReq = malloc(sizeof(sendReqList)); - if (newReq != NULL) { - memset(newReq, 0, sizeof(sendReqList)); - newReq->reqId = gReqIdCounter++; - strncpy(newReq->Mac_dest,param->Mac_dest, sizeof(newReq->Mac_dest)-1); - newReq->resCb = param->resCb; - newReq->timeout = param->timeout; - newReq->next = NULL; + sendReqList *SendReq = IDM_searchFromSendRequestList(param->Mac_dest); + if(SendReq != NULL) + { + CcspTraceInfo(("%s:%d Resending the same request with request id %d \n",__FUNCTION__, __LINE__,SendReq->reqId)); + payload.reqID = SendReq->reqId; + } + else + { + /* Create request entry */ + sendReqList *newReq = malloc(sizeof(sendReqList)); + if (newReq != NULL) { + memset(newReq, 0, sizeof(sendReqList)); + newReq->reqId = gReqIdCounter++; + strncpy(newReq->Mac_dest,param->Mac_dest, sizeof(newReq->Mac_dest)-1); + newReq->resCb = param->resCb; + newReq->timeout = param->timeout; + newReq->next = NULL; - IDM_addToSendRequestList(newReq); - payload.reqID = newReq->reqId; - } + IDM_addToSendRequestList(newReq); + payload.reqID = newReq->reqId; + } + } }else if(param->operation == IDM_SUBS) { /* Create request entry */ sendSubscriptionList *newReq = malloc(sizeof(sendSubscriptionList)); - if (newReq != NULL) { + if (newReq != NULL) { memset(newReq, 0, sizeof(sendSubscriptionList)); newReq->reqId = gReqIdCounter++; newReq->resCb = param->resCb; newReq->next = NULL; IDM_addToSendSubscriptionuestList(newReq); payload.reqID = newReq->reqId; - } + } } payload.operation = param->operation; @@ -353,8 +381,24 @@ ANSC_STATUS IDM_sendMsg_to_Remote_device(idm_send_msg_Params_t *param) payload.type = param->type; /* send message */ - send_remote_message(&remoteDevice->stRemoteDeviceInfo.conn_info, &payload); + int ret = send_remote_message(&remoteDevice->stRemoteDeviceInfo.conn_info, &payload); usleep(250000); //Sleep for 250ms + if(ret != 0) + { + CcspTraceError(("%s:%d send_remote_message failed for request id %d\n",__FUNCTION__, __LINE__,payload.reqID)); + if(param->operation == GET || param->operation == SET || param->operation == IDM_REQUEST) + { + sendReqList *req; + req = IDM_getFromSendRequestList(payload.reqID); + if(req == NULL) + { + CcspTraceError(("%s:%d Request not found in SendRequestList \n",__FUNCTION__, __LINE__)); + }else{ + CcspTraceInfo(("%s:%d Removing request from SendRequestList \n",__FUNCTION__, __LINE__)); + free(req); + } + } + } }else { IdmMgrDml_GetConfigData_release(pidmDmlInfo); @@ -490,6 +534,7 @@ int IDM_Incoming_Response_handler(payload_t * payload) { rbusMethodAsyncHandle_t async_callBack_handler; rbusError_t ret = RBUS_ERROR_SUCCESS; + CcspTraceInfo(("%s:%d operation - %d req id %d \n",__FUNCTION__, __LINE__,payload->operation, payload->reqID)); /* find req entry in LL */ if(payload->operation == IDM_SUBS) { From c7030e03625a06a2860ef19311fddf8576f618e7 Mon Sep 17 00:00:00 2001 From: Annie Rashmitha Date: Wed, 7 Jan 2026 11:20:06 +0000 Subject: [PATCH 2/2] Adding param name validation to sen request list --- source/InterDeviceManager/Idm_msg_process.c | 16 ++++++++++------ source/InterDeviceManager/Idm_msg_process.h | 1 + 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/source/InterDeviceManager/Idm_msg_process.c b/source/InterDeviceManager/Idm_msg_process.c index 59c9ec4..ce4f0f3 100644 --- a/source/InterDeviceManager/Idm_msg_process.c +++ b/source/InterDeviceManager/Idm_msg_process.c @@ -109,7 +109,7 @@ sendReqList* IDM_getFromSendRequestList(uint reqID) } } -sendReqList* IDM_searchFromSendRequestList(const char *param_mac) +sendReqList* IDM_searchFromSendRequestList(const char *param_mac, const char *param_name) { if(param_mac == NULL) @@ -122,9 +122,12 @@ sendReqList* IDM_searchFromSendRequestList(const char *param_mac) { if (strncmp(cur->Mac_dest, param_mac, sizeof(cur->Mac_dest) - 1) == 0) { - return cur; - } - cur = cur->next; + if (strncmp(cur->param_name, param_name, sizeof(cur->param_name) -1) == 0) + { + return cur; + } + } + cur = cur->next; } return NULL; } @@ -275,6 +278,7 @@ ANSC_STATUS IDM_getFile_from_Remote_device(char* Mac_dest,char* filename,char* o strncpy(payload.param_name,filename,sizeof(payload.param_name)-1); strncpy(newReq->output_location,output_location,sizeof(newReq->output_location)-1); payload.reqID = newReq->reqId; + strncpy(newReq->param_name, payload.param_name, sizeof(newReq->param_name)-1); IDM_addToSendRequestList(newReq); CcspTraceDebug(("Inside %s:%d peer MAC=%s\n",__FUNCTION__,__LINE__,Mac_dest)); send_remote_message(&remoteDevice->stRemoteDeviceInfo.conn_info, &payload); @@ -337,7 +341,7 @@ ANSC_STATUS IDM_sendMsg_to_Remote_device(idm_send_msg_Params_t *param) memset(&payload, 0, sizeof(payload_t)); if(param->operation == GET || param->operation == SET || param->operation == IDM_REQUEST) { - sendReqList *SendReq = IDM_searchFromSendRequestList(param->Mac_dest); + sendReqList *SendReq = IDM_searchFromSendRequestList(param->Mac_dest, param->param_name); if(SendReq != NULL) { CcspTraceInfo(("%s:%d Resending the same request with request id %d \n",__FUNCTION__, __LINE__,SendReq->reqId)); @@ -354,7 +358,7 @@ ANSC_STATUS IDM_sendMsg_to_Remote_device(idm_send_msg_Params_t *param) newReq->resCb = param->resCb; newReq->timeout = param->timeout; newReq->next = NULL; - + strncpy(newReq->param_name, param->param_name, sizeof(newReq->param_name)-1); IDM_addToSendRequestList(newReq); payload.reqID = newReq->reqId; } diff --git a/source/InterDeviceManager/Idm_msg_process.h b/source/InterDeviceManager/Idm_msg_process.h index bc99503..9da96bc 100644 --- a/source/InterDeviceManager/Idm_msg_process.h +++ b/source/InterDeviceManager/Idm_msg_process.h @@ -28,6 +28,7 @@ typedef struct _sendReqList uint reqId; char Mac_dest[MAC_ADDR_SIZE]; rbusMethodAsyncHandle_t resCb; + char param_name[128]; uint timeout; char output_location[LOC_SIZE]; struct _sendReqList *next;