Update Snapshot(2018-02-28)
[platform/upstream/iotivity.git] / resource / csdk / security / src / doxmresource.c
index 43aeb54..3128723 100644 (file)
@@ -48,6 +48,7 @@
 #include "srmutility.h"
 #include "pinoxmcommon.h"
 #include "oxmverifycommon.h"
+#include "octhread.h"
 
 #if defined(__WITH_DTLS__) || defined (__WITH_TLS__)
 #include "pkix_interface.h"
@@ -91,6 +92,8 @@ typedef enum ConfirmState{
 }ConfirmState_t;
 
 static OicSecDoxm_t        *gDoxm = NULL;
+static oc_mutex            g_mutexDoxm = NULL;
+static bool                g_isDoxmNull = false;
 static OCResourceHandle    gDoxmHandle = NULL;
 
 static OicSecOxm_t gOicSecDoxmJustWorks = OIC_JUST_WORKS;
@@ -116,6 +119,7 @@ static OicSecDoxm_t gDefaultDoxm =
 static uint16_t gConfirmMsgId = 0;
 static ConfirmState_t gConfirmState = CONFIRM_STATE_READY;
 
+static uint8_t gEmptyUuid[UUID_LENGTH] = {0};
 
 /**
  * This method is internal method.
@@ -159,6 +163,12 @@ void DeleteDoxmBinData(OicSecDoxm_t* doxm)
         //Clean doxm itself
         OICFree(doxm);
     }
+
+    if(g_mutexDoxm)
+    {
+        oc_mutex_free(g_mutexDoxm);
+        g_mutexDoxm = NULL;
+    }
 }
 
 OCStackResult DoxmToCBORPayload(const OicSecDoxm_t *doxm, uint8_t **payload, size_t *size,
@@ -1237,6 +1247,16 @@ static OCEntityHandlerResult HandleDoxmPostRequest(OCEntityHandlerRequest * ehRe
                     InvokeOtmEventHandler(ehRequest->devAddr.addr, ehRequest->devAddr.port,
                                           NULL, OIC_OTM_STARTED);
                 }
+                else
+                {
+                    OIC_LOG_V(INFO, TAG, "%s: request owner not empty",__func__);
+                    char* strUuid = NULL;
+                    if (OC_STACK_OK == ConvertUuidToStr(&newDoxm->owner, &strUuid))
+                    {
+                        OIC_LOG_V(INFO, TAG, "%s: request owner: %s",__func__, strUuid);
+                        OICFree(strUuid);
+                    }
+                }
 #endif
 
                 if (OIC_JUST_WORKS == newDoxm->oxmSel || OIC_MV_JUST_WORKS == newDoxm->oxmSel)
@@ -1681,6 +1701,14 @@ OCEntityHandlerResult DoxmEntityHandler(OCEntityHandlerFlag flag,
         return ehRet;
     }
 
+    oc_mutex_lock(g_mutexDoxm);
+
+    if(g_isDoxmNull)
+    {
+        oc_mutex_unlock(g_mutexDoxm);
+        return OC_EH_SERVICE_UNAVAILABLE;
+    }
+
     if (flag & OC_REQUEST_FLAG)
     {
         OIC_LOG(DEBUG, TAG, "Flag includes OC_REQUEST_FLAG");
@@ -1706,6 +1734,8 @@ OCEntityHandlerResult DoxmEntityHandler(OCEntityHandlerFlag flag,
                                OC_EH_OK : OC_EH_ERROR;
                 break;
         }
+
+        oc_mutex_unlock(g_mutexDoxm);
     }
 
     return ehRet;
@@ -1902,6 +1932,15 @@ OCStackResult InitDoxmResource()
 {
     OCStackResult ret = OC_STACK_ERROR;
 
+    if (!g_mutexDoxm)
+    {
+        g_mutexDoxm = oc_mutex_new();
+            if(!g_mutexDoxm)
+            {
+                return OC_STACK_ERROR;
+            }
+    }
+
     gConfirmState = CONFIRM_STATE_READY;
     gConfirmMsgId = 0;
 
@@ -1929,6 +1968,10 @@ OCStackResult InitDoxmResource()
         gDoxm = GetDoxmDefault();
     }
 
+     oc_mutex_lock(g_mutexDoxm);
+     g_isDoxmNull = false;
+     oc_mutex_unlock(g_mutexDoxm);
+
     //In case of the server is shut down unintentionally, we should initialize the owner
     if(gDoxm && (false == gDoxm->owned))
     {
@@ -1965,19 +2008,23 @@ OCStackResult InitDoxmResource()
 
 OCStackResult DeInitDoxmResource()
 {
+    oc_mutex_lock(g_mutexDoxm);
     OCStackResult ret = OCDeleteResource(gDoxmHandle);
     if (gDoxm  != &gDefaultDoxm)
     {
         DeleteDoxmBinData(gDoxm);
     }
     gDoxm = NULL;
+    g_isDoxmNull = true;
 
     if (OC_STACK_OK == ret)
     {
+        oc_mutex_unlock(g_mutexDoxm);
         return OC_STACK_OK;
     }
     else
     {
+        oc_mutex_unlock(g_mutexDoxm);
         return OC_STACK_ERROR;
     }
 }
@@ -2049,8 +2096,9 @@ OCStackResult SetDoxmDeviceID(const OicUuid_t *deviceID)
 
 #ifdef __WITH_DTLS__
     //for normal device.
-    if (true == gDoxm->owned &&
-        memcmp(gDoxm->deviceID.id, gDoxm->owner.id, sizeof(gDoxm->owner.id)) != 0)
+    if (true == gDoxm->owned
+            && memcmp(gEmptyUuid, gDoxm->owner.id, sizeof(gDoxm->owner.id)) != 0
+            && memcmp(gDoxm->deviceID.id, gDoxm->owner.id, sizeof(gDoxm->owner.id)) != 0)
     {
         OIC_LOG(ERROR, TAG, "This device owned by owner's device.");
         OIC_LOG(ERROR, TAG, "Device UUID cannot be changed to guarantee the reliability of the connection.");
@@ -2177,7 +2225,7 @@ OCStackResult SetMOTStatus(bool enable)
     bool isDeallocateRequired = false;
 
     VERIFY_NON_NULL(TAG, gDoxm, ERROR);
-    
+
     if (NULL == gDoxm->mom && !enable)
     {
         OIC_LOG_V(DEBUG, TAG, "Out %s", __func__);