Removed ocsocket
[platform/upstream/iotivity.git] / resource / csdk / stack / src / ocstack.c
index e2fca83..da84aa0 100644 (file)
@@ -52,6 +52,7 @@
 #include <arpa/inet.h>
 #endif
 
+
 //-----------------------------------------------------------------------------
 // Typedefs
 //-----------------------------------------------------------------------------
@@ -131,6 +132,26 @@ static OCStackResult FormOCResponse(OCResponse * * responseLoc,
     return OC_STACK_OK;
 }
 
+/// This method is used to create the IPv4 dev_addr structure.
+/// TODO: Remove in future. Temporary helper function.
+/// Builds a socket interface address using IP address and port number
+static int32_t OCBuildIPv4Address(uint8_t a, uint8_t b, uint8_t c, uint8_t d,
+        uint16_t port, OCDevAddr *ipAddr)
+{
+    if ( !ipAddr ) {
+        OC_LOG(FATAL, TAG, "Invalid argument");
+        return 1;
+    }
+
+    ipAddr->addr[0] = a;
+    ipAddr->addr[1] = b;
+    ipAddr->addr[2] = c;
+    ipAddr->addr[3] = d;
+    *((uint16_t*)&(ipAddr->addr[4])) = port;
+
+    return 0;
+}
+
 //-----------------------------------------------------------------------------
 // Internal API function
 //-----------------------------------------------------------------------------
@@ -712,9 +733,8 @@ void HandleCARequests(const CARemoteEndpoint_t* endPoint, const CARequestInfo_t*
         return;
     }
 
-    OCServerProtocolRequest serverRequest;
+    OCServerProtocolRequest serverRequest = {};
 
-    memset (&serverRequest, 0, sizeof(OCServerProtocolRequest));
     OC_LOG_V(INFO, TAG, PCF("***** Endpoint URI ***** : %s\n"), (char*)endPoint->resourceUri);
 
     char * newUri = (char *)endPoint->resourceUri;
@@ -722,12 +742,28 @@ void HandleCARequests(const CARemoteEndpoint_t* endPoint, const CARequestInfo_t*
     getQueryFromUri(endPoint->resourceUri, &query, &newUri);
     OC_LOG_V(INFO, TAG, PCF("**********URI without query ****: %s\n"), newUri);
     OC_LOG_V(INFO, TAG, PCF("**********Query ****: %s\n"), query);
-    //copy URI
-    memcpy (&(serverRequest.resourceUrl), newUri, strlen(newUri));
+    if(strlen(newUri) < MAX_URI_LENGTH)
+    {
+        //copy URI
+        memcpy (&(serverRequest.resourceUrl), newUri, strlen(newUri));
+    }
+    else
+    {
+        OC_LOG(ERROR, TAG, PCF("URI length exceeds MAX_URI_LENGTH."));
+        return;
+    }
     //copy query
     if(query)
     {
-        memcpy (&(serverRequest.query), query, strlen((char*)query));
+        if(strlen((char*)query) < MAX_QUERY_LENGTH)
+        {
+            memcpy (&(serverRequest.query), query, strlen((char*)query));
+        }
+        else
+        {
+            OC_LOG(ERROR, TAG, PCF("Query length exceeds MAX_QUERY_LENGTH."));
+            return;
+        }
     }
     //copy request payload
     if (requestInfo->info.payload)
@@ -774,14 +810,13 @@ void HandleCARequests(const CARemoteEndpoint_t* endPoint, const CARequestInfo_t*
     OC_LOG_V(INFO, TAG, "HandleCARequests: CA token length = %d", CA_MAX_TOKEN_LEN);
     OC_LOG_BUFFER(INFO, TAG, (const uint8_t *)requestInfo->info.token, CA_MAX_TOKEN_LEN);
 
-    serverRequest.requestToken = (CAToken_t)OCMalloc(CA_MAX_TOKEN_LEN+1);
+    serverRequest.requestToken = (CAToken_t)OCCalloc(1, CA_MAX_TOKEN_LEN+1);
     // Module Name
     if (!serverRequest.requestToken)
     {
         OC_LOG(FATAL, TAG, "Server Request Token is NULL");
         return;
     }
-    memset(serverRequest.requestToken, 0, CA_MAX_TOKEN_LEN + 1);
     memcpy(serverRequest.requestToken, requestInfo->info.token, CA_MAX_TOKEN_LEN);
 
     if (requestInfo->info.type == CA_MSG_CONFIRM)
@@ -818,6 +853,7 @@ void HandleCARequests(const CARemoteEndpoint_t* endPoint, const CARequestInfo_t*
     {
         OC_LOG(ERROR, TAG,
                 PCF("The request info numOptions is greater than MAX_HEADER_OPTIONS"));
+        OCFree(serverRequest.requestToken);
         return;
     }
     serverRequest.numRcvdVendorSpecificHeaderOptions = tempNum;
@@ -858,8 +894,7 @@ OCStackResult HandleStackRequests(OCServerProtocolRequest * protocolRequest)
                 protocolRequest->observationOption, protocolRequest->qos,
                 protocolRequest->query, protocolRequest->rcvdVendorSpecificHeaderOptions,
                 protocolRequest->reqJSONPayload, &protocolRequest->requestToken,
-                &protocolRequest->requesterAddr, protocolRequest->resourceUrl,
-                protocolRequest->reqTotalSize,
+                protocolRequest->resourceUrl,protocolRequest->reqTotalSize,
                 &protocolRequest->addressInfo, protocolRequest->connectivityType);
         if (OC_STACK_OK != result)
         {
@@ -1538,7 +1573,7 @@ OCStackResult OCDoResource(OCDoHandle *handle, OCMethod method, const char *requ
     CAToken_t token = NULL;
     CAInfo_t requestData;
     CARequestInfo_t requestInfo;
-    CAGroupEndpoint_t grpEnd;
+    CAGroupEndpoint_t grpEnd = {0};
 
     // To track if memory is allocated for additional header options
     uint8_t hdrOptionMemAlloc = 0;
@@ -1633,7 +1668,6 @@ OCStackResult OCDoResource(OCDoHandle *handle, OCMethod method, const char *requ
 
     memset(&requestData, 0, sizeof(CAInfo_t));
     memset(&requestInfo, 0, sizeof(CARequestInfo_t));
-    memset(&grpEnd, 0, sizeof(CAGroupEndpoint_t));
     switch (method)
     {
         case OC_REST_GET:
@@ -2055,11 +2089,8 @@ OCStackResult OCStartPresence(const uint32_t ttl)
 
     if(OC_PRESENCE_UNINITIALIZED == presenceState)
     {
-        OCDevAddr multiCastAddr;
         presenceState = OC_PRESENCE_INITIALIZED;
 
-        OCBuildIPv4Address(224, 0, 1, 187, 5683, &multiCastAddr);
-
         CAAddress_t addressInfo;
         strncpy(addressInfo.IP.ipAddress, "224.0.1.187", CA_IPADDR_SIZE);
         addressInfo.IP.port = 5683;
@@ -2076,7 +2107,7 @@ OCStackResult OCStartPresence(const uint32_t ttl)
         }
 
         AddObserver(OC_PRESENCE_URI, NULL, 0, &caToken,
-                &multiCastAddr, (OCResource *)presenceResource.handle, OC_LOW_QOS,
+                (OCResource *)presenceResource.handle, OC_LOW_QOS,
                 &addressInfo, CA_WIFI);
     }
 
@@ -3667,3 +3698,35 @@ const char* OCGetServerInstanceIDString(void)
 
     return buffer;
 }
+
+/// Retrieve the IPv4 address embedded inside OCDev address data structure
+int32_t OCDevAddrToIPv4Addr(OCDevAddr *ipAddr, uint8_t *a, uint8_t *b,
+        uint8_t *c, uint8_t *d )
+{
+    if ( !ipAddr || !a || !b || !c || !d ) {
+        OC_LOG(FATAL, TAG, "Invalid argument");
+        return OC_STACK_INVALID_PARAM;
+    }
+
+    *a = ipAddr->addr[0];
+    *b = ipAddr->addr[1];
+    *c = ipAddr->addr[2];
+    *d = ipAddr->addr[3];
+
+    return OC_STACK_OK;
+}
+
+
+/// Retrieve the IPv4 address embedded inside OCDev address data structure
+int32_t OCDevAddrToPort(OCDevAddr *ipAddr, uint16_t *port)
+{
+    if ( !ipAddr || !port ) {
+        OC_LOG(FATAL, TAG, "Invalid argument");
+        return OC_STACK_INVALID_PARAM;
+    }
+
+    *port = *((uint16_t*)&ipAddr->addr[4]);
+
+    return OC_STACK_OK;
+}
+