From: Habib Virji Date: Wed, 25 Nov 2015 16:44:43 +0000 (+0000) Subject: RD handling of publishing device address X-Git-Tag: 1.2.0+RC1~798 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=2b53f2d597d376993d813cb958f488bbfbd1cdcc;p=platform%2Fupstream%2Fiotivity.git RD handling of publishing device address Correct address in the baseURI field for resource directory. This patch fixes issues where host() in OCResource.cpp returned a wrong IP address. It uses baseURI field in the /oic/res to send publishing device address. JIRA ISSUE:855 Signed-off-by: Habib Virji Change-Id: Idf86fe801e25e32c84268f421feff479270faa1b Reviewed-on: https://gerrit.iotivity.org/gerrit/4337 Tested-by: jenkins-iotivity Reviewed-by: Madan Lanka Reviewed-by: Uze Choi --- diff --git a/resource/csdk/stack/src/ocresource.c b/resource/csdk/stack/src/ocresource.c index 2c67858..6c44c52 100644 --- a/resource/csdk/stack/src/ocresource.c +++ b/resource/csdk/stack/src/ocresource.c @@ -305,6 +305,10 @@ OCStackResult BuildVirtualCollectionResourceResponse(const OCResourceCollectionP if (resourcePtr->tags && !resourcePtr->tags->baseURI) { resourcePtr->tags->baseURI = OICStrdup(devAddr->addr); + if (resourcePtr->tags->port == 0 && devAddr->port != 0) + { + resourcePtr->tags->port = devAddr->port; + } } OCDiscoveryCollectionPayloadAddResource(payload, resourcePtr->tags, resourcePtr->setLinks); return OC_STACK_OK; @@ -569,9 +573,9 @@ OCStackResult SendNonPersistantDiscoveryResponse(OCServerRequest *request, OCRes #ifdef WITH_RD static OCStackResult checkResourceExistsAtRD(const char *interfaceType, const char *resourceType, - OCResourceCollectionPayload **repPayload) + OCResourceCollectionPayload **repPayload, OCDevAddr *devAddr) { - if (OCRDCheckPublishedResource(interfaceType, resourceType, repPayload) == OC_STACK_OK) + if (OCRDCheckPublishedResource(interfaceType, resourceType, repPayload, devAddr) == OC_STACK_OK) { return OC_STACK_OK; } @@ -622,14 +626,15 @@ static OCStackResult HandleVirtualResource (OCServerRequest *request, OCResource if (strcmp(resource->uri, OC_RSRVD_RD_URI) == 0) { OCResourceCollectionPayload *repPayload; - discoveryResult = checkResourceExistsAtRD(filterOne, filterTwo, &repPayload); + OCDevAddr devAddr; + discoveryResult = checkResourceExistsAtRD(filterOne, filterTwo, &repPayload, &devAddr); if (discoveryResult != OC_STACK_OK) { break; } discoveryResult = BuildVirtualCollectionResourceResponse(repPayload, (OCDiscoveryPayload*)payload, - &request->devAddr); + &devAddr); foundResourceAtRD = true; } #endif diff --git a/resource/include/OCSerialization.h b/resource/include/OCSerialization.h index b112af1..4c21ea3 100644 --- a/resource/include/OCSerialization.h +++ b/resource/include/OCSerialization.h @@ -21,6 +21,7 @@ #include #include "ocpayload.h" #include "ocrandom.h" +#include "oic_string.h" namespace OC { @@ -81,19 +82,24 @@ namespace OC { while(colRes) { + OCDevAddr colAddr; + OICStrcpy(colAddr.addr, sizeof(colAddr.addr), colRes->tags->baseURI); if (colRes->tags->bitmap & OC_SECURE) { - m_devAddr.flags = - (OCTransportFlags)(OC_FLAG_SECURE | m_devAddr.flags); + colAddr.flags = + (OCTransportFlags)(OC_FLAG_SECURE | colRes->tags->bitmap); + if (colRes->tags->port != 0) + { + colAddr.port = colRes->tags->port; + } } - - if (colRes->tags->port != 0) + else { - m_devAddr.port = colRes->tags->port; + colAddr.port = colRes->tags->port; } m_resources.push_back(std::shared_ptr( - new OC::OCResource(m_clientWrapper, m_devAddr, + new OC::OCResource(m_clientWrapper, colAddr, std::string(colRes->setLinks->href), std::string((char*)colRes->tags->di.id), (colRes->tags->bitmap & OC_OBSERVABLE) == OC_OBSERVABLE, diff --git a/service/resource-directory/include/rd_server.h b/service/resource-directory/include/rd_server.h index 69877d8..4e3ec51 100644 --- a/service/resource-directory/include/rd_server.h +++ b/service/resource-directory/include/rd_server.h @@ -50,13 +50,14 @@ OCStackResult OCRDStop(); * * @param interfaceType a interface type that is being queried. * @param resourceType a resource type that is being queried. - * @param payload A payload of the maching resource type or interface type or NULL. + * @param payload A payload of the maching resource type or interface type or NULL. + * @param addr A device address. * * @return ::OC_STACK_OK upon success, ::OC_STACK_ERROR is returned except * the case that OC_STACK_SUCCESS is returned. */ OCStackResult OCRDCheckPublishedResource(const char *interfaceType, const char *resourceType, - OCResourceCollectionPayload **payload); + OCResourceCollectionPayload **payload, OCDevAddr *addr); #ifdef __cplusplus } diff --git a/service/resource-directory/src/internal/rd_storage.c b/service/resource-directory/src/internal/rd_storage.c index 5f80354..5311e78 100644 --- a/service/resource-directory/src/internal/rd_storage.c +++ b/service/resource-directory/src/internal/rd_storage.c @@ -24,6 +24,7 @@ #include "payload_logging.h" #include "oic_malloc.h" +#include "octypes.h" #include "rdpayload.h" @@ -46,7 +47,7 @@ static void printStoragedResources(OCRDStorePublishResources *payload) } } -OCStackResult OCRDStorePublishedResources(const OCResourceCollectionPayload *payload) +OCStackResult OCRDStorePublishedResources(const OCResourceCollectionPayload *payload, const OCDevAddr *address) { OCResourceCollectionPayload *storeResource = (OCResourceCollectionPayload *)OICCalloc(1, sizeof(OCResourceCollectionPayload)); if (!storeResource) @@ -55,11 +56,11 @@ OCStackResult OCRDStorePublishedResources(const OCResourceCollectionPayload *pay return OC_STACK_NO_MEMORY; } - OC_LOG(DEBUG, TAG, "Storing Resources ... "); + OC_LOG_V(DEBUG, TAG, "Storing Resources for %s:%u", address->addr, address->port); OCTagsPayload *tags = payload->tags; storeResource->tags = OCCopyTagsResources(tags->n.deviceName, tags->di.id, tags->baseURI, - tags->bitmap, tags->port, tags->ins, tags->rts, tags->drel, tags->ttl); + tags->bitmap, address->port, tags->ins, tags->rts, tags->drel, tags->ttl); if (!storeResource->tags) { OC_LOG(ERROR, TAG, "Failed allocating memory for tags."); @@ -106,6 +107,7 @@ OCStackResult OCRDStorePublishedResources(const OCResourceCollectionPayload *pay return OC_STACK_NO_MEMORY; } resources->publishedResource = storeResource; + resources->devAddr = *address; pthread_mutex_lock(&storageMutex); if (g_rdStorage) @@ -128,7 +130,7 @@ OCStackResult OCRDStorePublishedResources(const OCResourceCollectionPayload *pay } OCStackResult OCRDCheckPublishedResource(const char *interfaceType, const char *resourceType, - OCResourceCollectionPayload **payload) + OCResourceCollectionPayload **payload, OCDevAddr *devAddr) { // ResourceType and InterfaceType if both are NULL it will return. If either is // not null it will continue execution. @@ -183,6 +185,7 @@ OCStackResult OCRDCheckPublishedResource(const char *interfaceType, const char * OCFreeLinksResource(links); return OC_STACK_NO_MEMORY; } + memcpy(devAddr, &pResource->devAddr, sizeof(*devAddr)); return OC_STACK_OK; } temp = temp->next; @@ -217,6 +220,7 @@ OCStackResult OCRDCheckPublishedResource(const char *interfaceType, const char * OCFreeLinksResource(links); return OC_STACK_NO_MEMORY; } + devAddr = &pResource->devAddr; return OC_STACK_OK; } temp = temp->next; diff --git a/service/resource-directory/src/internal/rd_storage.h b/service/resource-directory/src/internal/rd_storage.h index 36803d5..e91cee9 100644 --- a/service/resource-directory/src/internal/rd_storage.h +++ b/service/resource-directory/src/internal/rd_storage.h @@ -29,6 +29,7 @@ extern "C" { /** Stucture holding Published Resources on the Resource Directory. */ typedef struct OCRDStorePublishResources { + OCDevAddr devAddr; /** Publish resource. */ OCResourceCollectionPayload *publishedResource; /** Linked list pointing to next published resource. */ @@ -39,10 +40,11 @@ typedef struct OCRDStorePublishResources * Stores the publish resources. * * @param payload RDPublish payload sent from the remote device. + * @param address The address of the device publishing resources. * * @return ::OC_STACK_OK upon success, ::OC_STACK_ERROR in case of error. */ -OCStackResult OCRDStorePublishedResources(const OCResourceCollectionPayload *payload); +OCStackResult OCRDStorePublishedResources(const OCResourceCollectionPayload *payload, const OCDevAddr *address); #ifdef __cplusplus } diff --git a/service/resource-directory/src/rd_client.c b/service/resource-directory/src/rd_client.c index f5ffe01..e351b6c 100644 --- a/service/resource-directory/src/rd_client.c +++ b/service/resource-directory/src/rd_client.c @@ -331,6 +331,9 @@ OCStackResult OCRDPublish(char *addr, uint16_t port, int numArg, ... ) goto no_memory; } + OCTagsLog(DEBUG, rdPayload->rdPublish->tags); + OCLinksLog(DEBUG, rdPayload->rdPublish->setLinks); + OCDevAddr rdAddr = { 0 }; OICStrcpy(rdAddr.addr, MAX_ADDR_STR_SIZE, addr); rdAddr.port = port; diff --git a/service/resource-directory/src/rd_server.c b/service/resource-directory/src/rd_server.c index 4b67b97..7713679 100644 --- a/service/resource-directory/src/rd_server.c +++ b/service/resource-directory/src/rd_server.c @@ -97,7 +97,7 @@ static OCEntityHandlerResult handlePublishRequest(const OCEntityHandlerRequest * OCRDPayload *payload = (OCRDPayload *)ehRequest->payload; if (payload && payload->rdPublish) { - OCRDStorePublishedResources(payload->rdPublish); + OCRDStorePublishedResources(payload->rdPublish, &ehRequest->devAddr); } OCRDPayload *rdPayload = OCRDPayloadCreate();