CONPRO-1467: Improve Reset
[platform/upstream/iotivity.git] / resource / csdk / security / src / doxmresource.c
index 811fa89..d8b4506 100644 (file)
@@ -101,6 +101,7 @@ static oc_mutex            g_mutexWait;
 static oc_thread           g_waitConfirmThreadId;
 oc_cond                    g_condWait;
 static InformOxmSelectedCallback_t g_InformOxmSelectedCallback = NULL;
+static bool                g_isConfirmResult;
 
 static OicSecOxm_t gOicSecDoxmJustWorks = OIC_JUST_WORKS;
 static OicSecDoxm_t gDefaultDoxm =
@@ -1104,6 +1105,23 @@ void UnsetInformOxmSelCB()
 #if defined(__WITH_DTLS__) || defined (__WITH_TLS__)
 static OCEntityHandlerResult HandleDoxmPostRequest(OCEntityHandlerRequest * ehRequest);
 
+static void DestroyEntityHandlerRequest(OCEntityHandlerRequest * ehRequest)
+{
+    if (ehRequest == NULL) {
+        OIC_LOG(WARNING, TAG, "ehRequest is NULL");
+        return;
+    }
+
+    OICFree(ehRequest->query);
+
+    if (ehRequest->payload) {
+        OICFree(((OCSecurityPayload *)ehRequest->payload)->securityData);
+        OICFree(ehRequest->payload);
+    }
+
+    OICFree(ehRequest);
+}
+
 void * WaitConfirm(OCEntityHandlerRequest * ehRequest)
 {
     bool confirmResult = false, confirmState = false;
@@ -1111,21 +1129,30 @@ void * WaitConfirm(OCEntityHandlerRequest * ehRequest)
     oc_mutex_lock(g_mutexWait);
     oc_cond_wait(g_condWait, g_mutexWait);
     oc_cond_free(g_condWait);
+    g_condWait = NULL;
+
+    oc_mutex_unlock(g_mutexWait);
     oc_mutex_free(g_mutexWait);
+    g_mutexWait = NULL;
 
+    g_isConfirmResult = true;
     GetAsyncVerifyUserResult(&confirmResult, &confirmState);
     if (confirmResult == true)
     {
         gConfirmState = CONFIRM_STATE_ACCEPTED;
         HandleDoxmPostRequest(ehRequest);
-        return NULL;
+        g_isConfirmResult = false;
     }
     else
     {
         gConfirmState = CONFIRM_STATE_DENIED;
         HandleDoxmPostRequest(ehRequest);
-        return NULL;
+        g_isConfirmResult = false;
     }
+
+    DestroyEntityHandlerRequest(ehRequest);
+
+    return NULL;
 }
 
 static OCEntityHandlerRequest *CopyRequest(OCEntityHandlerRequest *entityHandlerRequest)
@@ -1139,7 +1166,7 @@ static OCEntityHandlerRequest *CopyRequest(OCEntityHandlerRequest *entityHandler
     }
 
     OCEntityHandlerRequest *copyOfRequest =
-            (OCEntityHandlerRequest *)OICMalloc(sizeof(OCEntityHandlerRequest));
+            (OCEntityHandlerRequest *)OICCalloc(1, sizeof(OCEntityHandlerRequest));
     if(!copyOfRequest)
     {
         OIC_LOG(ERROR, TAG, "Copy failed due to allocation failure");
@@ -1162,7 +1189,7 @@ static OCEntityHandlerRequest *CopyRequest(OCEntityHandlerRequest *entityHandler
     if (entityHandlerRequest->payload)
     {
         copyOfRequest->payload =
-                (OCSecurityPayload *)OICMalloc(sizeof(OCSecurityPayload));
+                (OCSecurityPayload *)OICCalloc(1, sizeof(OCSecurityPayload));
         if(!copyOfRequest->payload)
         {
             OIC_LOG(ERROR, TAG, "Copy failed due to allocation failure");
@@ -1171,11 +1198,10 @@ static OCEntityHandlerRequest *CopyRequest(OCEntityHandlerRequest *entityHandler
             return NULL;
         }
 
-        if (((OCSecurityPayload *)copyOfRequest->payload)->securityData &&
-            ((OCSecurityPayload *)entityHandlerRequest->payload)->payloadSize)
+        if (((OCSecurityPayload *)entityHandlerRequest->payload)->payloadSize)
         {
             ((OCSecurityPayload *)copyOfRequest->payload)->securityData =
-                    (uint8_t *)OICMalloc(((OCSecurityPayload *)entityHandlerRequest->payload)->payloadSize);
+                    (uint8_t *)OICCalloc(1, ((OCSecurityPayload *)entityHandlerRequest->payload)->payloadSize);
             if(!((OCSecurityPayload *)copyOfRequest->payload)->securityData)
             {
                 OIC_LOG(ERROR, TAG, "Copy failed due to allocation failure");
@@ -1191,10 +1217,6 @@ static OCEntityHandlerRequest *CopyRequest(OCEntityHandlerRequest *entityHandler
 
             ((OCSecurityPayload *)(copyOfRequest->payload))->payloadSize =
                     ((OCSecurityPayload *)(entityHandlerRequest->payload))->payloadSize;
-
-            memcpy(((OCSecurityPayload *)copyOfRequest->payload)->securityData,
-                   ((OCSecurityPayload *)entityHandlerRequest->payload)->securityData,
-                   ((OCSecurityPayload *)entityHandlerRequest->payload)->payloadSize);
         }
 
         copyOfRequest->payload->type = entityHandlerRequest->payload->type;
@@ -1205,14 +1227,8 @@ static OCEntityHandlerRequest *CopyRequest(OCEntityHandlerRequest *entityHandler
     copyOfRequest->numRcvdVendorSpecificHeaderOptions = 0;
     copyOfRequest->rcvdVendorSpecificHeaderOptions = NULL;
 
-    if (copyOfRequest)
-    {
-        OIC_LOG(INFO, TAG, "Copied client request");
-    }
-    else
-    {
-        OIC_LOG(ERROR, TAG, "Error copying client request");
-    }
+    OIC_LOG(INFO, TAG, "Copied client request");
+
     return copyOfRequest;
 }
 #endif // defined(__WITH_DTLS__) || defined (__WITH_TLS__)
@@ -1245,7 +1261,7 @@ static OCEntityHandlerResult HandleDoxmPostRequest(OCEntityHandlerRequest * ehRe
              * In case of other transport adapter, duplicate message check is not required.
              */
             if (OC_ADAPTER_IP == ehRequest->devAddr.adapter &&
-                 previousMsgId == ehRequest->messageID)
+                 previousMsgId == ehRequest->messageID && g_isConfirmResult == false)
             {
                 isDuplicatedMsg = true;
             }
@@ -1286,6 +1302,13 @@ static OCEntityHandlerResult HandleDoxmPostRequest(OCEntityHandlerRequest * ehRe
                     ehRet = OC_EH_NOT_ACCEPTABLE;
                     goto exit;
                 }
+
+                if(0 != memcmp(&gDoxm->owner.id, &newDoxm->owner.id, sizeof(gDoxm->owner.id)))
+                {
+                    OIC_LOG(ERROR, TAG, "Not acceptable request for owned property");
+                    ehRet = OC_EH_NOT_ACCEPTABLE;
+                }
+
                 //Update gDoxm based on newDoxm
                 updateWriteableProperty(newDoxm, gDoxm);
 
@@ -1624,6 +1647,9 @@ static OCEntityHandlerResult HandleDoxmPostRequest(OCEntityHandlerRequest * ehRe
                                 {
                                     oc_thread_detach(g_waitConfirmThreadId);
                                 }
+
+                                previousMsgId = ehRequest->messageID;
+
                                 return OC_EH_SLOW;
                             }
                         }
@@ -1752,8 +1778,7 @@ exit:
                     InvokeOtmEventHandler(ehRequest->devAddr.addr, ehRequest->devAddr.port,
                                           NULL, OIC_OTM_ERROR);
 #endif
-                    RestoreDoxmToInitState();
-                    RestorePstatToInitState();
+                    ResetSecureResourceInPS();
                     OIC_LOG(WARNING, TAG, "DOXM will be reverted.");
                 }
             }