Resolved bug IOT-685
authorShilpa Sodani <shilpa.a.sodani@intel.com>
Thu, 20 Aug 2015 09:45:54 +0000 (02:45 -0700)
committerJon A. Cruz <jonc@osg.samsung.com>
Mon, 24 Aug 2015 21:52:30 +0000 (21:52 +0000)
Resolved bug IOT-685. Due to unintialized value of field "interface" of
struct CAEndpoint_t and changed return value type of method  u_arraylist_add()
broke the security apps.

The unintailzed value of interface gave it random value
which inturn set the wrong value for ipv6 sockaddr_in6 struct field
"sin6_scope_id" causing "Network unreachable" error.

The return value of methode "u_arraylist_add()" was recently changed
from CAResult_t to bool. This caused seg fault.

Change-Id: I23fea80a0d6e12dd4122f2e032497a72496a4745
Signed-off-by: Shilpa Sodani <shilpa.a.sodani@intel.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/2249
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: John Light <john.j.light@intel.com>
Reviewed-by: Sachin Agrawal <sachin.agrawal@intel.com>
Reviewed-by: Jon A. Cruz <jonc@osg.samsung.com>
resource/csdk/connectivity/src/adapter_util/caadapternetdtls.c
resource/csdk/stack/src/ocstack.c

index 56a0f7d..780a6aa 100644 (file)
@@ -118,9 +118,10 @@ static CAResult_t CAAddIdToPeerInfoList(const char *peerAddr, uint32_t port,
     {
         OIC_LOG(ERROR, NET_DTLS_TAG, "u_arraylist_add failed!");
         OICFree(peer);
+        return CA_STATUS_FAILED;
     }
 
-    return result;
+    return CA_STATUS_OK;
 }
 
 static void CAFreePeerInfoList()
@@ -302,10 +303,11 @@ static CAResult_t CADtlsCacheMsg(stCACacheMessage_t *msg)
     if (!result)
     {
         OIC_LOG(ERROR, NET_DTLS_TAG, "u_arraylist_add failed!");
+        return CA_STATUS_FAILED;
     }
 
     OIC_LOG(DEBUG, NET_DTLS_TAG, "OUT");
-    return result;
+    return CA_STATUS_OK;
 }
 
 
@@ -437,11 +439,13 @@ static int32_t CASendSecureData(dtls_context_t *context,
 
     stCADtlsAddrInfo_t *addrInfo = (stCADtlsAddrInfo_t *)session;
 
-    CAEndpoint_t endpoint;
+    CAEndpoint_t endpoint = {.adapter = CA_DEFAULT_ADAPTER};
+
     CAConvertAddrToName(&(addrInfo->addr.st), endpoint.addr, &endpoint.port);
     endpoint.flags = addrInfo->addr.st.ss_family == AF_INET ? CA_IPV4 : CA_IPV6;
     endpoint.flags |= CA_SECURE;
     endpoint.adapter = CA_ADAPTER_IP;
+    endpoint.interface = session->ifindex;
     int type = 0;
 
     //Mutex is not required for g_caDtlsContext. It will be called in same thread.
index 3c5dd3c..654b785 100644 (file)
@@ -399,6 +399,7 @@ void CopyEndpointToDevAddr(const CAEndpoint_t *in, OCDevAddr *out)
     out->flags = CAToOCTransportFlags(in->flags);
     OICStrcpy(out->addr, sizeof(out->addr), in->addr);
     out->port = in->port;
+    out->interface = in->interface;
 }
 
 void CopyDevAddrToEndpoint(const OCDevAddr *in, CAEndpoint_t *out)
@@ -410,36 +411,7 @@ void CopyDevAddrToEndpoint(const OCDevAddr *in, CAEndpoint_t *out)
     out->flags = OCToCATransportFlags(in->flags);
     OICStrcpy(out->addr, sizeof(out->addr), in->addr);
     out->port = in->port;
-}
-
-static OCStackResult OCCreateEndpoint(OCDevAddr *devAddr, CAEndpoint_t **endpoint)
-{
-    VERIFY_NON_NULL(devAddr, FATAL, OC_STACK_INVALID_PARAM);
-    VERIFY_NON_NULL(endpoint, FATAL, OC_STACK_INVALID_PARAM);
-
-    CAEndpoint_t *ep = (CAEndpoint_t *)OICMalloc(sizeof (CAEndpoint_t));
-    if (!ep)
-    {
-        return OC_STACK_NO_MEMORY;
-    }
-
-    ep->adapter = (CATransportAdapter_t)devAddr->adapter;
-    if (!ep->adapter)
-    {
-        ep->adapter = CA_ADAPTER_IP;
-    }
-    ep->flags = OCToCATransportFlags(devAddr->flags);
-    OICStrcpy(ep->addr, sizeof(ep->addr), devAddr->addr);
-    ep->port = devAddr->port;
-
-    *endpoint = ep;
-
-    return OC_STACK_OK;
-}
-
-static void OCDestroyEndpoint(CAEndpoint_t *endpoint)
-{
-    free(endpoint);
+    out->interface = in->interface;
 }
 
 void FixUpClientResponse(OCClientResponse *cr)
@@ -2001,7 +1973,7 @@ OCStackResult OCDoResource(OCDoHandle *handle,
     uint8_t tokenLength = CA_MAX_TOKEN_LEN;
     ClientCB *clientCB = NULL;
     OCDoHandle resHandle = NULL;
-    CAEndpoint_t *endpoint = NULL;
+    CAEndpoint_t endpoint = {.adapter = CA_DEFAULT_ADAPTER};
     OCDevAddr tmpDevAddr = { OC_DEFAULT_ADAPTER };
     uint32_t ttl = 0;
     OCTransportAdapter adapter;
@@ -2139,8 +2111,8 @@ OCStackResult OCDoResource(OCDoHandle *handle,
         requestInfo.info.numOptions = numOptions;
     }
 
-    // create remote endpoint
-    result = OCCreateEndpoint(devAddr, &endpoint);
+    CopyDevAddrToEndpoint(devAddr, &endpoint);
+
     if(payload)
     {
         if((result =
@@ -2157,8 +2129,6 @@ OCStackResult OCDoResource(OCDoHandle *handle,
         requestInfo.info.payloadSize = 0;
     }
 
-
-
     if (result != OC_STACK_OK)
     {
         OC_LOG(ERROR, TAG, PCF("CACreateEndpoint error"));
@@ -2170,7 +2140,7 @@ OCStackResult OCDoResource(OCDoHandle *handle,
     if (method == OC_REST_PRESENCE)
     {
         char *presenceUri = NULL;
-        result = OCPreparePresence(endpoint, resourceUri, &presenceUri);
+        result = OCPreparePresence(&endpoint, resourceUri, &presenceUri);
         if (OC_STACK_OK != result)
         {
             goto exit;
@@ -2196,7 +2166,7 @@ OCStackResult OCDoResource(OCDoHandle *handle,
     resourceType = NULL;  // Client CB list entry now owns it
 
     // send request
-    caResult = CASendRequest(endpoint, &requestInfo);
+    caResult = CASendRequest(&endpoint, &requestInfo);
     if (caResult != CA_STATUS_OK)
     {
         OC_LOG(ERROR, TAG, PCF("CASendRequest"));
@@ -2228,7 +2198,6 @@ exit:
     OICFree(devAddr);
     OICFree(resourceUri);
     OICFree(resourceType);
-    OICFree(endpoint);
     if (hdrOptionMemAlloc)
     {
         OICFree(requestInfo.info.options);
@@ -2257,7 +2226,7 @@ OCStackResult OCCancel(OCDoHandle handle, OCQualityOfService qos, OCHeaderOption
      *      Remove the callback associated on client side.
      */
     OCStackResult ret = OC_STACK_OK;
-    CAEndpoint_t* endpoint = NULL;
+    CAEndpoint_t endpoint = {.adapter = CA_DEFAULT_ADAPTER};
     CAResult_t caResult;
     CAInfo_t requestData = {.type = CA_MSG_CONFIRM};
     CARequestInfo_t requestInfo = {.method = CA_GET};
@@ -2304,15 +2273,10 @@ OCStackResult OCCancel(OCDoHandle handle, OCQualityOfService qos, OCHeaderOption
             requestInfo.method = CA_GET;
             requestInfo.info = requestData;
 
-            ret = OCCreateEndpoint(clientCB->devAddr, &endpoint);
-            if (ret != OC_STACK_OK)
-            {
-                OC_LOG(ERROR, TAG, PCF("CACreateEndpoint error"));
-                goto Error;
-            }
+            CopyDevAddrToEndpoint(clientCB->devAddr, &endpoint);
 
             // send request
-            caResult = CASendRequest(endpoint, &requestInfo);
+            caResult = CASendRequest(&endpoint, &requestInfo);
             if (caResult != CA_STATUS_OK)
             {
                 OC_LOG(ERROR, TAG, PCF("CASendRequest error"));
@@ -2333,7 +2297,6 @@ OCStackResult OCCancel(OCDoHandle handle, OCQualityOfService qos, OCHeaderOption
     }
 
 Error:
-    OCDestroyEndpoint(endpoint);
     if (requestData.numOptions > 0)
     {
         OICFree(requestData.options);
@@ -2342,7 +2305,6 @@ Error:
     {
         OICFree (requestData.resourceUri);
     }
-
     return ret;
 }
 
@@ -2440,18 +2402,13 @@ OCStackResult OCProcessPresence()
         }
 
         CAResult_t caResult = CA_STATUS_OK;
-        CAEndpoint_t* endpoint = NULL;
+        CAEndpoint_t endpoint = {.adapter = CA_DEFAULT_ADAPTER};
         CAInfo_t requestData = {.type = CA_MSG_CONFIRM};
         CARequestInfo_t requestInfo = {.method = CA_GET};
 
         OC_LOG(DEBUG, TAG, PCF("time to test server presence"));
 
-        result = OCCreateEndpoint(cbNode->devAddr, &endpoint);
-        if (result != OC_STACK_OK)
-        {
-            OC_LOG(ERROR, TAG, PCF("CACreateEndpoint error"));
-            goto exit;
-        }
+        CopyDevAddrToEndpoint(cbNode->devAddr, &endpoint);
 
         requestData.type = CA_MSG_NONCONFIRM;
         requestData.token = cbNode->token;
@@ -2460,8 +2417,7 @@ OCStackResult OCProcessPresence()
         requestInfo.method = CA_GET;
         requestInfo.info = requestData;
 
-        caResult = CASendRequest(endpoint, &requestInfo);
-        OCDestroyEndpoint(endpoint);
+        caResult = CASendRequest(&endpoint, &requestInfo);
 
         if (caResult != CA_STATUS_OK)
         {
@@ -2477,6 +2433,7 @@ exit:
     {
         OC_LOG(ERROR, TAG, PCF("OCProcessPresence error"));
     }
+
     return result;
 }
 #endif // WITH_PRESENCE