#define _POSIX_C_SOURCE 200112L
#endif
+#if HAVE_UNISTD_H
#include <unistd.h>
+#endif
+#ifdef HAVE_STRING_H
#include <string.h>
-#include <time.h>
-#include <sys/time.h>
+#endif
#include "ocstack.h"
#include "oic_malloc.h"
#include "oic_string.h"
+#include "oic_time.h"
#include "logger.h"
#include "cJSON.h"
#include "utlist.h"
#include "srmresourcestrings.h" //@note: SRM's internal header
#include "doxmresource.h" //@note: SRM's internal header
#include "pstatresource.h" //@note: SRM's internal header
+#include "verresource.h" //@note: SRM's internal header
#include "pmtypes.h"
#include "pmutility.h"
bool isOwnedDiscovery;
} DiscoveryInfo;
+/*
+ * Function to discover secre port information through unicast
+ *
+ * @param[in] discoveryInfo The pointer of discovery information to matain result of discovery
+ * @param[in] clientResponse Response information(It will contain payload)
+ *
+ * @return OC_STACK_OK on success otherwise error.
+ */
+static OCStackResult SecurePortDiscovery(DiscoveryInfo* discoveryInfo,
+ const OCClientResponse *clientResponse);
+
+/*
+ * Function to discover security version information through unicast
+ *
+ * @param[in] discoveryInfo The pointer of discovery information to matain result of discovery
+ * @param[in] clientResponse Response information(It will contain payload)
+ *
+ * @return OC_STACK_OK on success otherwise error.
+ */
+static OCStackResult SecurityVersionDiscovery(DiscoveryInfo* discoveryInfo,
+ const OCClientResponse *clientResponse);
+
+/**
+ * Callback handler for PMDeviceDiscovery API.
+ *
+ * @param[in] ctx User context
+ * @param[in] handle Handler for response
+ * @param[in] clientResponse Response information (It will contain payload)
+ * @return OC_STACK_KEEP_TRANSACTION to keep transaction and
+ * OC_STACK_DELETE_TRANSACTION to delete it.
+ */
+static OCStackApplicationResult DeviceDiscoveryHandler(void *ctx, OCDoHandle UNUSED,
+ OCClientResponse *clientResponse);
+
+/**
+ * Callback handler for getting secure port information using /oic/res discovery.
+ *
+ * @param[in] ctx user context
+ * @param[in] handle Handle for response
+ * @param[in] clientResponse Response information(It will contain payload)
+ *
+ * @return OC_STACK_KEEP_TRANSACTION to keep transaction and
+ * OC_STACK_DELETE_TRANSACTION to delete it.
+ */
+static OCStackApplicationResult SecurePortDiscoveryHandler(void *ctx, OCDoHandle UNUSED,
+ OCClientResponse *clientResponse);
+
+/**
+ * Callback handler for security version discovery.
+ *
+ * @param[in] ctx User context
+ * @param[in] handle Handler for response
+ * @param[in] clientResponse Response information (It will contain payload)
+ * @return OC_STACK_KEEP_TRANSACTION to keep transaction and
+ * OC_STACK_DELETE_TRANSACTION to delete it.
+ */
+static OCStackApplicationResult SecVersionDiscoveryHandler(void *ctx, OCDoHandle UNUSED,
+ OCClientResponse *clientResponse);
+
/**
* Function to search node in linked list that matches given IP and port.
*
ptr->next = NULL;
ptr->connType = connType;
ptr->devStatus = DEV_STATUS_ON; //AddDevice is called when discovery(=alive)
+ OICStrcpy(ptr->secVer, MAX_VERSION_LEN, DEFAULT_SEC_VERSION); // version initialization
LL_PREPEND(*ppDevicesList, ptr);
}
}
/**
+ * Function to set security version information from the given list of devices.
+ *
+ * @param[in] pList List of OCProvisionDev_t.
+ * @param[in] addr address of target device.
+ * @param[in] port port of remote server.
+ * @param[in] secVer security version information.
+ *
+ * @return OC_STACK_OK for success and errorcode otherwise.
+ */
+OCStackResult UpdateSecVersionOfDevice(OCProvisionDev_t **ppDevicesList, const char *addr,
+ uint16_t port, const char* secVer)
+{
+ if (NULL == secVer)
+ {
+ return OC_STACK_INVALID_PARAM;
+ }
+
+ OCProvisionDev_t *ptr = GetDevice(ppDevicesList, addr, port);
+
+ if(!ptr)
+ {
+ OIC_LOG(ERROR, TAG, "Can not find device information in the discovery device list");
+ return OC_STACK_ERROR;
+ }
+
+ OICStrcpy(ptr->secVer, MAX_VERSION_LEN, secVer);
+
+ return OC_STACK_OK;
+}
+
+/**
* This function deletes list of provision target devices
*
* @param[in] pDevicesList List of OCProvisionDev_t.
newDev->doxm->oxm = NULL;
}
+ if (0 == strlen(src->secVer))
+ {
+ OICStrcpy(newDev->secVer, MAX_VERSION_LEN, DEFAULT_SEC_VERSION);
+ }
+ else
+ {
+ OICStrcpy(newDev->secVer, MAX_VERSION_LEN, src->secVer);
+ }
+
newDev->securePort = src->securePort;
newDev->devStatus = src->devStatus;
newDev->connType = src->connType;
*/
OCStackResult PMTimeout(unsigned short waittime, bool waitForStackResponse)
{
- struct timespec startTime = {.tv_sec=0, .tv_nsec=0};
- struct timespec currTime = {.tv_sec=0, .tv_nsec=0};
-
OCStackResult res = OC_STACK_OK;
-#ifdef _POSIX_MONOTONIC_CLOCK
- int clock_res = clock_gettime(CLOCK_MONOTONIC, &startTime);
-#else
- int clock_res = clock_gettime(CLOCK_REALTIME, &startTime);
-#endif
- if (0 != clock_res)
- {
- return OC_STACK_ERROR;
- }
+
+ uint64_t startTime = OICGetCurrentTime(TIME_IN_MS);
while (OC_STACK_OK == res)
{
-#ifdef _POSIX_MONOTONIC_CLOCK
- clock_res = clock_gettime(CLOCK_MONOTONIC, &currTime);
-#else
- clock_res = clock_gettime(CLOCK_REALTIME, &currTime);
-#endif
- if (0 != clock_res)
- {
- return OC_STACK_TIMEOUT;
- }
- long elapsed = (currTime.tv_sec - startTime.tv_sec);
+ uint64_t currTime = OICGetCurrentTime(TIME_IN_MS);
+
+ long elapsed = (long)((currTime - startTime) / MS_PER_SEC);
if (elapsed > waittime)
{
return OC_STACK_OK;
}
bool PMGenerateQuery(bool isSecure,
- const char* address, const uint16_t port,
- const OCConnectivityType connType,
+ const char* address, uint16_t port,
+ OCConnectivityType connType,
char* buffer, size_t bufferSize, const char* uri)
{
if(!address || !buffer || !uri)
return true;
}
-/**
- * Callback handler for getting secure port information using /oic/res discovery.
- *
- * @param[in] ctx user context
- * @param[in] handle Handle for response
- * @param[in] clientResponse Response information(It will contain payload)
- *
- * @return OC_STACK_KEEP_TRANSACTION to keep transaction and
- * OC_STACK_DELETE_TRANSACTION to delete it.
- */
+static OCStackApplicationResult SecurityVersionDiscoveryHandler(void *ctx, OCDoHandle UNUSED,
+ OCClientResponse *clientResponse)
+{
+ if (ctx == NULL)
+ {
+ OIC_LOG(ERROR, TAG, "Lost List of device information");
+ return OC_STACK_KEEP_TRANSACTION;
+ }
+ (void)UNUSED;
+ if (clientResponse)
+ {
+ if (NULL == clientResponse->payload)
+ {
+ OIC_LOG(INFO, TAG, "Skiping Null payload");
+ return OC_STACK_KEEP_TRANSACTION;
+ }
+ if (OC_STACK_OK != clientResponse->result)
+ {
+ OIC_LOG(INFO, TAG, "Error in response");
+ return OC_STACK_KEEP_TRANSACTION;
+ }
+ else
+ {
+ if (PAYLOAD_TYPE_SECURITY != clientResponse->payload->type)
+ {
+ OIC_LOG(INFO, TAG, "Unknown payload type");
+ return OC_STACK_KEEP_TRANSACTION;
+ }
+
+ OicSecVer_t *ptrVer = NULL;
+ uint8_t *payload = ((OCSecurityPayload*)clientResponse->payload)->securityData;
+ size_t size = ((OCSecurityPayload*)clientResponse->payload)->payloadSize;
+
+ OCStackResult res = CBORPayloadToVer(payload, size, &ptrVer);
+ if ((NULL == ptrVer) && (OC_STACK_OK != res))
+ {
+ OIC_LOG(INFO, TAG, "Ignoring malformed CBOR");
+ return OC_STACK_KEEP_TRANSACTION;
+ }
+ else
+ {
+ OIC_LOG(DEBUG, TAG, "Successfully converted ver cbor to bin.");
+
+ //If this is owend device discovery we have to filter out the responses.
+ DiscoveryInfo* pDInfo = (DiscoveryInfo*)ctx;
+ res = UpdateSecVersionOfDevice(pDInfo->ppDevicesList, clientResponse->devAddr.addr,
+ clientResponse->devAddr.port, ptrVer->secv);
+ if (OC_STACK_OK != res)
+ {
+ OIC_LOG(ERROR, TAG, "Error while getting security version.");
+ DeleteVerBinData(ptrVer);
+ return OC_STACK_KEEP_TRANSACTION;
+ }
+
+ OIC_LOG(INFO, TAG, "= Discovered security version =");
+ OIC_LOG_V(DEBUG, TAG, "IP %s", clientResponse->devAddr.addr);
+ OIC_LOG_V(DEBUG, TAG, "PORT %d", clientResponse->devAddr.port);
+ OIC_LOG_V(DEBUG, TAG, "VERSION %s", ptrVer->secv);
+
+ OIC_LOG(INFO, TAG, "Exiting SecVersionDiscoveryHandler.");
+ DeleteVerBinData(ptrVer);
+ }
+ }
+ }
+ else
+ {
+ OIC_LOG(INFO, TAG, "Skiping Null response");
+ return OC_STACK_KEEP_TRANSACTION;
+ }
+
+ return OC_STACK_DELETE_TRANSACTION;
+}
+
static OCStackApplicationResult SecurePortDiscoveryHandler(void *ctx, OCDoHandle UNUSED,
OCClientResponse *clientResponse)
{
uint16_t securePort = 0;
OCResourcePayload* resPayload = ((OCDiscoveryPayload*)clientResponse->payload)->resources;
+ // Use seure port of doxm for OTM and Provision.
+ while (resPayload)
+ {
+ if (0 == strncmp(resPayload->uri, OIC_RSRC_DOXM_URI, strlen(OIC_RSRC_DOXM_URI)))
+ {
+ OIC_LOG_V(INFO,TAG,"resPaylod->uri:%s",resPayload->uri);
+ OIC_LOG(INFO, TAG, "Found doxm resource.");
+ break;
+ }
+ else
+ {
+ resPayload = resPayload->next;
+ }
+ }
+ if (NULL == resPayload)
+ {
+ OIC_LOG(ERROR, TAG, "Can not find doxm resource.");
+ return OC_STACK_DELETE_TRANSACTION;
+ }
if (resPayload && resPayload->secure)
{
securePort = resPayload->port;
}
DiscoveryInfo* pDInfo = (DiscoveryInfo*)ctx;
- OCProvisionDev_t **ppDevicesList = pDInfo->ppDevicesList;
-
- OCStackResult res = UpdateSecurePortOfDevice(ppDevicesList, clientResponse->devAddr.addr,
+ OCStackResult res = UpdateSecurePortOfDevice(pDInfo->ppDevicesList,
+ clientResponse->devAddr.addr,
clientResponse->devAddr.port, securePort);
if (OC_STACK_OK != res)
{
OIC_LOG(ERROR, TAG, "Error while getting secure port.");
return OC_STACK_DELETE_TRANSACTION;
}
+
+ res = SecurityVersionDiscovery(pDInfo, clientResponse);
+ if(OC_STACK_OK != res)
+ {
+ OIC_LOG(ERROR, TAG, "Failed to SecurityVersionDiscovery");
+ return OC_STACK_DELETE_TRANSACTION;
+ }
+
OIC_LOG(INFO, TAG, "Exiting SecurePortDiscoveryHandler.");
}
{
OIC_LOG(INFO, TAG, "Skiping Null response");
}
+
return OC_STACK_DELETE_TRANSACTION;
}
-/**
- * Callback handler for PMDeviceDiscovery API.
- *
- * @param[in] ctx User context
- * @param[in] handle Handler for response
- * @param[in] clientResponse Response information (It will contain payload)
- * @return OC_STACK_KEEP_TRANSACTION to keep transaction and
- * OC_STACK_DELETE_TRANSACTION to delete it.
- */
static OCStackApplicationResult DeviceDiscoveryHandler(void *ctx, OCDoHandle UNUSED,
OCClientResponse *clientResponse)
{
OIC_LOG(INFO, TAG, "Unknown payload type");
return OC_STACK_KEEP_TRANSACTION;
}
+
OicSecDoxm_t *ptrDoxm = NULL;
- uint8_t *payload = ((OCSecurityPayload*)clientResponse->payload)->securityData1;
- OCStackResult res = CBORPayloadToDoxm(payload, 0, &ptrDoxm);
- if ((NULL == ptrDoxm) && (OC_STACK_OK != res))
+ uint8_t *payload = ((OCSecurityPayload*)clientResponse->payload)->securityData;
+ size_t size = ((OCSecurityPayload*)clientResponse->payload)->payloadSize;
+
+ OCStackResult res = CBORPayloadToDoxm(payload, size, &ptrDoxm);
+ if ((NULL == ptrDoxm) || (OC_STACK_OK != res))
{
OIC_LOG(INFO, TAG, "Ignoring malformed CBOR");
return OC_STACK_KEEP_TRANSACTION;
return OC_STACK_KEEP_TRANSACTION;
}
+ res = GetDoxmDeviceID(&myId);
+ if(OC_STACK_OK != res)
+ {
+ OIC_LOG(ERROR, TAG, "Error while getting my UUID.");
+ DeleteDoxmBinData(ptrDoxm);
+ return OC_STACK_KEEP_TRANSACTION;
+ }
+ //if this is owned discovery and this is PT's reply, discard it
+ if((pDInfo->isOwnedDiscovery) &&
+ (0 == memcmp(&ptrDoxm->deviceID.id, &myId.id, sizeof(myId.id))) )
+ {
+ OIC_LOG(DEBUG, TAG, "discarding provision tool's reply");
+ DeleteDoxmBinData(ptrDoxm);
+ return OC_STACK_KEEP_TRANSACTION;
+ }
+
res = AddDevice(ppDevicesList, clientResponse->devAddr.addr,
clientResponse->devAddr.port,
clientResponse->devAddr.adapter,
DeleteDoxmBinData(ptrDoxm);
return OC_STACK_KEEP_TRANSACTION;
}
- char rsrc_uri[MAX_URI_LENGTH+1] = {0};
- int wr_len = snprintf(rsrc_uri, sizeof(rsrc_uri), "%s?%s=%s",
- OC_RSRVD_WELL_KNOWN_URI, OC_RSRVD_RESOURCE_TYPE, OIC_RSRC_TYPE_SEC_DOXM);
- if(wr_len <= 0 || (size_t)wr_len >= sizeof(rsrc_uri))
- {
- OIC_LOG(ERROR, TAG, "rsrc_uri_string_print failed");
- return OC_STACK_ERROR;
- }
- //Try to the unicast discovery to getting secure port
- char query[MAX_URI_LENGTH+MAX_QUERY_LENGTH+1] = {0};
- if(!PMGenerateQuery(false,
- clientResponse->devAddr.addr, clientResponse->devAddr.port,
- clientResponse->connType,
- query, sizeof(query), rsrc_uri))
- {
- OIC_LOG(ERROR, TAG, "DeviceDiscoveryHandler : Failed to generate query");
- return OC_STACK_KEEP_TRANSACTION;
- }
- OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
-
- OCCallbackData cbData;
- cbData.cb = &SecurePortDiscoveryHandler;
- cbData.context = ctx;
- cbData.cd = NULL;
- OCStackResult ret = OCDoResource(NULL, OC_REST_DISCOVER, query, 0, 0,
- clientResponse->connType, OC_LOW_QOS, &cbData, NULL, 0);
- // TODO: Should we use the default secure port in case of error?
- if(OC_STACK_OK != ret)
+
+ res = SecurePortDiscovery(pDInfo, clientResponse);
+ if(OC_STACK_OK != res)
{
- OIC_LOG(ERROR, TAG, "Failed to Secure Port Discovery");
+ OIC_LOG(ERROR, TAG, "Failed to SecurePortDiscovery");
+ DeleteDoxmBinData(ptrDoxm);
return OC_STACK_KEEP_TRANSACTION;
}
- else
- {
- OIC_LOG_V(INFO, TAG, "OCDoResource with [%s] Success", query);
- }
+
OIC_LOG(INFO, TAG, "Exiting ProvisionDiscoveryHandler.");
}
OCDoHandle handle = NULL;
res = OCDoResource(&handle, OC_REST_DISCOVER, query, 0, 0,
- CT_DEFAULT, OC_LOW_QOS, &cbData, NULL, 0);
+ CT_DEFAULT, OC_HIGH_QOS, &cbData, NULL, 0);
if (res != OC_STACK_OK)
{
OIC_LOG(ERROR, TAG, "OCStack resource error");
{
OIC_LOG(ERROR, TAG, "Failed to wait response for secure discovery.");
OICFree(pDInfo);
- OCStackResult resCancel = OCCancel(handle, OC_LOW_QOS, NULL, 0);
+ OCStackResult resCancel = OCCancel(handle, OC_HIGH_QOS, NULL, 0);
if(OC_STACK_OK != resCancel)
{
OIC_LOG(ERROR, TAG, "Failed to remove registered callback");
}
return res;
}
- res = OCCancel(handle,OC_LOW_QOS,NULL,0);
+ res = OCCancel(handle,OC_HIGH_QOS,NULL,0);
if (OC_STACK_OK != res)
{
OIC_LOG(ERROR, TAG, "Failed to remove registered callback");
return res;
}
+static OCStackResult SecurePortDiscovery(DiscoveryInfo* discoveryInfo,
+ const OCClientResponse *clientResponse)
+{
+ OIC_LOG(DEBUG, TAG, "IN SecurePortDiscovery");
+
+ if(NULL == discoveryInfo || NULL == clientResponse)
+ {
+ return OC_STACK_INVALID_PARAM;
+ }
+ //Try to the unicast discovery to getting secure port
+ char query[MAX_URI_LENGTH+MAX_QUERY_LENGTH+1] = {0};
+ if(!PMGenerateQuery(false,
+ clientResponse->devAddr.addr, clientResponse->devAddr.port,
+ clientResponse->connType,
+ query, sizeof(query), OC_RSRVD_WELL_KNOWN_URI))
+ {
+ OIC_LOG(ERROR, TAG, "SecurePortDiscovery : Failed to generate query");
+ return OC_STACK_ERROR;
+ }
+ OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
+
+ OCCallbackData cbData;
+ cbData.cb = &SecurePortDiscoveryHandler;
+ cbData.context = (void*)discoveryInfo;
+ cbData.cd = NULL;
+ OCStackResult ret = OCDoResource(NULL, OC_REST_DISCOVER, query, 0, 0,
+ clientResponse->connType, OC_HIGH_QOS, &cbData, NULL, 0);
+ if(OC_STACK_OK != ret)
+ {
+ OIC_LOG(ERROR, TAG, "Failed to Secure Port Discovery");
+ return ret;
+ }
+ else
+ {
+ OIC_LOG_V(INFO, TAG, "OCDoResource with [%s] Success", query);
+ }
+
+ OIC_LOG(DEBUG, TAG, "OUT SecurePortDiscovery");
+
+ return ret;
+}
+
+static OCStackResult SecurityVersionDiscovery(DiscoveryInfo* discoveryInfo,
+ const OCClientResponse *clientResponse)
+{
+ OIC_LOG(DEBUG, TAG, "IN SecurityVersionDiscovery");
+
+ if(NULL == discoveryInfo || NULL == clientResponse)
+ {
+ return OC_STACK_INVALID_PARAM;
+ }
+
+ //Try to the unicast discovery to getting security version
+ char query[MAX_URI_LENGTH+MAX_QUERY_LENGTH+1] = {0};
+ if(!PMGenerateQuery(false,
+ clientResponse->devAddr.addr, clientResponse->devAddr.port,
+ clientResponse->connType,
+ query, sizeof(query), OIC_RSRC_VER_URI))
+ {
+ OIC_LOG(ERROR, TAG, "SecurityVersionDiscovery : Failed to generate query");
+ return OC_STACK_ERROR;
+ }
+ OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
+
+ OCCallbackData cbData;
+ cbData.cb = &SecurityVersionDiscoveryHandler;
+ cbData.context = (void*)discoveryInfo;
+ cbData.cd = NULL;
+ OCStackResult ret = OCDoResource(NULL, OC_REST_DISCOVER, query, 0, 0,
+ clientResponse->connType, OC_HIGH_QOS, &cbData, NULL, 0);
+ if(OC_STACK_OK != ret)
+ {
+ OIC_LOG(ERROR, TAG, "Failed to Security Version Discovery");
+ return ret;
+ }
+ else
+ {
+ OIC_LOG_V(INFO, TAG, "OCDoResource with [%s] Success", query);
+ }
+
+ OIC_LOG(DEBUG, TAG, "OUT SecurityVersionDiscovery");
+
+ return ret;
+}
+
/**
* Function to print OCProvisionDev_t for debug purpose.
*