Imported Upstream version 1.0.1
[platform/upstream/iotivity.git] / resource / csdk / connectivity / src / caprotocolmessage.c
index 227543d..0cb8a18 100644 (file)
@@ -24,7 +24,6 @@
 // For glibc information on feature test macros,
 // Refer http://www.gnu.org/software/libc/manual/html_node/Feature-Test-Macros.html
 //
-// This file requires #define use due to random() and srandom()
 // For details on compatibility and glibc support,
 // Refer http://www.gnu.org/software/libc/manual/html_node/BSD-Random.html
 #define _DEFAULT_SOURCE
 #include "logger.h"
 #include "oic_malloc.h"
 #include "oic_string.h"
-
-// ARM GCC compiler doesnt define srandom function.
-#if defined(ARDUINO) && !defined(ARDUINO_ARCH_SAM)
-#define HAVE_SRANDOM 1
-#endif
+#include "ocrandom.h"
 
 #define TAG "CA_PRTCL_MSG"
 
@@ -65,8 +60,6 @@
 
 static const char COAP_URI_HEADER[] = "coap://[::]/";
 
-static unsigned int SEED = 0;
-
 CAResult_t CAGetRequestInfoFromPDU(const coap_pdu_t *pdu, const CAEndpoint_t *endpoint,
                                    CARequestInfo_t *outReqInfo)
 {
@@ -127,6 +120,13 @@ coap_pdu_t *CAGeneratePDU(uint32_t code, const CAInfo_t *info, const CAEndpoint_
     // and ACKNOWLEDGE can use empty message when code is empty.
     if (CA_MSG_RESET == info->type || (CA_EMPTY == code && CA_MSG_ACKNOWLEDGE == info->type))
     {
+        if ((CA_EMPTY == code) && (info->payloadSize > 0 || info->payload
+            || info->token || info->tokenLength > 0))
+        {
+            OIC_LOG(ERROR, TAG, "Empty message has unnecessary data after messageID");
+            return NULL;
+        }
+
         OIC_LOG(DEBUG, TAG, "code is empty");
         if (!(pdu = CAGeneratePDUImpl((code_t) code, info, endpoint, NULL, transport)))
         {
@@ -136,7 +136,7 @@ coap_pdu_t *CAGeneratePDU(uint32_t code, const CAInfo_t *info, const CAEndpoint_
     }
     else
     {
-        if (CA_MSG_ACKNOWLEDGE != info->type && info->resourceUri)
+        if (info->resourceUri)
         {
             uint32_t length = strlen(info->resourceUri);
             if (CA_MAX_URI_LENGTH < length)
@@ -1035,26 +1035,6 @@ CAResult_t CAGenerateTokenInternal(CAToken_t *token, uint8_t tokenLength)
         return CA_STATUS_INVALID_PARAM;
     }
 
-    if (SEED == 0)
-    {
-#ifdef ARDUINO
-        SEED = now();
-#else
-        SEED = time(NULL);
-#endif
-        if (SEED == (unsigned int)((time_t)-1))
-        {
-            OIC_LOG(ERROR, TAG, "seed is not made");
-            SEED = 0;
-            return CA_STATUS_FAILED;
-        }
-#if HAVE_SRANDOM
-        srandom(SEED);
-#else
-        srand(SEED);
-#endif
-    }
-
     // memory allocation
     char *temp = (char *) OICCalloc(tokenLength, sizeof(char));
     if (NULL == temp)
@@ -1063,16 +1043,7 @@ CAResult_t CAGenerateTokenInternal(CAToken_t *token, uint8_t tokenLength)
         return CA_MEMORY_ALLOC_FAILED;
     }
 
-    // set random byte
-    for (uint8_t index = 0; index < tokenLength; index++)
-    {
-        // use valid characters
-#ifdef ARDUINO
-        temp[index] = rand() & 0x00FF;
-#else
-        temp[index] = random() & 0x00FF;
-#endif
-    }
+    OCFillRandomMem((uint8_t *)temp, tokenLength);
 
     // save token
     *token = temp;