Auto generate platform ID.
authorSoemin Tjong <stjong@microsoft.com>
Wed, 22 Mar 2017 22:57:10 +0000 (15:57 -0700)
committerDan Mihai <Daniel.Mihai@microsoft.com>
Tue, 28 Mar 2017 18:58:01 +0000 (18:58 +0000)
This allows all apps running on a platform (e.g. an installation of OS)
to register with the same platform ID in the platform info.

Applications can override this value by using the regular
OCSetPropertyValue(OC_RSRVD_PLATFORM_ID).

Change-Id: I638ab08a5e0a6319524052b95ac649f167e52a90
Signed-off-by: Soemin Tjong <stjong@microsoft.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/18103
Tested-by: jenkins-iotivity <jenkins@iotivity.org>
Reviewed-by: Dan Mihai <Daniel.Mihai@microsoft.com>
build_common/android/SConscript
resource/c_common/SConscript
resource/c_common/oic_platform/include/oic_platform.h [new file with mode: 0644]
resource/c_common/oic_platform/src/oic_platform.c [new file with mode: 0644]
resource/csdk/connectivity/src/SConscript
resource/csdk/stack/SConscript
resource/csdk/stack/include/ocstack.h
resource/csdk/stack/src/ocstack.c
resource/csdk/stack/test/SConscript
resource/unittests/SConscript

index 61c504d..a2e8a1f 100644 (file)
@@ -220,10 +220,14 @@ env.AppendUnique(LIBPATH = [env.get('BUILD_DIR')])
 env.AppendUnique(LIBPATH = [src_dir + '/resource/csdk/connectivity/lib/android'])
 env.AppendUnique(LIBS = ['log', 'coap'])
 
+# Build mbedtls as ocstack uses hash functions from mbedcrypto.lib.
+env.SConscript('#extlibs/mbedtls/SConscript')
+
 if env.get('SECURED') == '1':
-       env.SConscript('#extlibs/mbedtls/SConscript')
        env.AppendUnique(LIBS = ['mbedtls','mbedx509','mbedcrypto'])
-
+else:
+       env.AppendUnique(LIBS = ['mbedcrypto'])
+    
 # From android-5 (API > 20), all application must be built with flags '-fPIE' '-pie'.
 # Due to the limitation of Scons, it's required to added it into the command line
 # directly (otherwise, it will also be added when build share library)
index ac5e002..fa81420 100644 (file)
@@ -140,7 +140,9 @@ env.AppendUnique(CPPPATH = [
             os.path.join(Dir('.').abspath, 'oic_time', 'include'),
             os.path.join(Dir('.').abspath, 'ocatomic', 'include'),
             os.path.join(Dir('.').abspath, 'ocrandom', 'include'),
-            os.path.join(Dir('.').abspath, 'octhread', 'include')
+            os.path.join(Dir('.').abspath, 'octhread', 'include'),
+            os.path.join(Dir('.').abspath, 'oic_platform', 'include'),
+            '#/extlibs/mbedtls/mbedtls/include'
         ])
 
 if target_os not in ['tizen']:
@@ -172,7 +174,8 @@ common_src = [
     'oic_string/src/oic_string.c',
     'oic_malloc/src/oic_malloc.c',
     'oic_time/src/oic_time.c',
-    'ocrandom/src/ocrandom.c'
+    'ocrandom/src/ocrandom.c',
+    'oic_platform/src/oic_platform.c'
     ]
 
 if env['POSIX_SUPPORTED']:
diff --git a/resource/c_common/oic_platform/include/oic_platform.h b/resource/c_common/oic_platform/include/oic_platform.h
new file mode 100644 (file)
index 0000000..a7b102c
--- /dev/null
@@ -0,0 +1,42 @@
+/* *****************************************************************
+ *
+ * Copyright 2017 Microsoft
+ *
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ ******************************************************************/
+
+#ifndef OIC_OICPLATFORM_H_
+#define OIC_OICPLATFORM_H_
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif // __cplusplus
+
+#define OIC_UUID_LENGTH     16
+
+/**
+ * Function returns a UUID that can be used for platform ID.
+ *
+ * @param[in] platformUuid  The generated UUID.
+ *
+ * @return true if successful.
+ */
+bool OICGetPlatformUuid(uint8_t platformUuid[OIC_UUID_LENGTH]);
+
+#ifdef __cplusplus
+}
+#endif // __cplusplus
+#endif
diff --git a/resource/c_common/oic_platform/src/oic_platform.c b/resource/c_common/oic_platform/src/oic_platform.c
new file mode 100644 (file)
index 0000000..3573697
--- /dev/null
@@ -0,0 +1,165 @@
+/* *****************************************************************
+ *
+ * Copyright 2017 Microsoft
+ *
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ ******************************************************************/
+#include "iotivity_config.h"
+
+#ifdef HAVE_UNISTD_H
+#include <unistd.h>
+#endif
+
+#ifdef HAVE_SYS_SOCKET_H
+#include <sys/socket.h>
+#endif
+
+#ifdef HAVE_NETINET_IN_H
+#include <netinet/in.h>
+#endif
+
+#ifdef HAVE_WINSOCK2_H
+#include <winsock2.h>
+#endif
+
+#ifdef HAVE_WS2TCPIP_H
+#include <ws2tcpip.h>
+#endif
+
+#ifdef HAVE_NETDB_H
+#include <netdb.h>
+#endif
+
+#ifdef HAVE_SYS_SOCKET_H
+#include <sys/socket.h>
+#endif
+
+#ifdef HAVE_SYS_TYPES_H
+#include <sys/types.h>
+#endif
+
+#include <assert.h>
+#include <stdio.h>
+#include <errno.h>
+#include <mbedtls/md.h>
+#include "logger.h"
+#include "iotivity_debug.h"
+#include "oic_platform.h"
+#include "ocrandom.h"
+#include "oic_string.h"
+
+#define TAG "OIC_PLATFORM"
+
+#if defined(__unix__) || defined(_WIN32)
+// Create a UUID for using the "Name-Based UUID" mechanism specified in
+// RFC 4122 section 4.3 (see http://tools.ietf.org/html/rfc4122#section-4.3).
+bool HashStrToUuid(const char* str, size_t len, uint8_t platformUuid[OIC_UUID_LENGTH])
+{
+    if ((str == NULL) || (len == 0))
+    {
+        return false;
+    }
+
+    unsigned char hash[MBEDTLS_MD_MAX_SIZE];
+    int mbedret = mbedtls_md(mbedtls_md_info_from_type(MBEDTLS_MD_SHA256),
+                        (const unsigned char*)str,
+                        len,
+                        hash);
+    if (mbedret != 0)
+    {
+        OIC_LOG_V(WARNING, TAG, "Failed mbedtls_md(), error: %d.", mbedret);
+        return false;
+    }
+
+    // Set octets zero through 3 of the time_low field to octets zero through 3 of the hash.
+    // Set octets zero and one of the time_mid field to octets 4 and 5 of the hash.
+    // Set octets zero and one of the time_hi_and_version field to octets 6 and 7 of the hash.
+    // Set the clock_seq_hi_and_reserved field to octet 8 of the hash.
+    // Set the clock_seq_low field to octet 9 of the hash.
+    // Set octets zero through five of the node field to octets 10 through 15 of the hash.
+    memcpy(platformUuid, hash, OIC_UUID_LENGTH);
+
+    // Set the four most significant bits (bits 12 through 15) of the time_hi_and_version field
+    // to the appropriate 4-bit version number from Section 4.1.3.
+    platformUuid[6] &= 0x0F; // Set high bits to 0000.
+    platformUuid[6] |= 0x50; // Set high bits to 0101 to indicate a name-based GUID with SHA-1.
+
+    // Set the two most significant bits (bits 6 and 7) of the clock_seq_hi_and_reserved to zero
+    // and one, respectively.
+    platformUuid[8] &= 0x3F; // Set the two highest bits to 00.
+    platformUuid[8] |= 0x40; // Set the two highest bits to 01.
+
+    return true;
+}
+#endif
+
+// Buffer size for computer name.
+#define MAX_COMPUTER_NAME_LENGTH 256
+
+// This functions returns UUID generated from host name.
+bool OICGetPlatformUuid(uint8_t platformUuid[OIC_UUID_LENGTH])
+{
+#if defined(__unix__) || defined(_WIN32)
+    // Get host name.
+    char hostname[MAX_COMPUTER_NAME_LENGTH] = { 0 };
+    if (gethostname(hostname, MAX_COMPUTER_NAME_LENGTH) != 0)
+    {
+        OIC_LOG_V(WARNING, TAG, "Failed gethostname() errno: %s", strerror(errno));
+        return false;
+    }
+
+    // Get FDQN of the computer.
+    char fqdnComputerName[MAX_COMPUTER_NAME_LENGTH] = { 0 };
+    struct addrinfo* addrInfo;
+    struct addrinfo hints = { .ai_family = AF_UNSPEC,
+                              .ai_socktype = SOCK_STREAM,
+                              .ai_flags = AI_CANONNAME };
+    if (getaddrinfo(hostname, NULL, &hints, &addrInfo) == 0)
+    {
+        for(struct addrinfo* iter = addrInfo; iter != NULL; iter = iter->ai_next)
+        {
+            if (iter->ai_canonname != NULL)
+            {
+                OICStrcpy(fqdnComputerName, MAX_COMPUTER_NAME_LENGTH, iter->ai_canonname);
+                break;
+            }
+        }
+
+        if (NULL != addrInfo)
+        {
+            freeaddrinfo(addrInfo);
+            addrInfo = NULL;
+        }
+    }
+    else
+    {
+        OIC_LOG_V(WARNING, TAG, "Failed getaddrinfo() errno: %s, use hostname.", strerror(errno));
+
+        // Use hostname as getaddrinfo() failed.
+        OICStrcpy(fqdnComputerName, MAX_COMPUTER_NAME_LENGTH, hostname);
+    }
+
+    if (!HashStrToUuid(fqdnComputerName, strlen(fqdnComputerName), platformUuid))
+    {
+        OIC_LOG_V(WARNING, TAG, "Failed HashStrToUuid()");
+        return false;
+    }
+
+    return true;
+#else
+    OIC_LOG_V(WARNING, TAG, "Unsupported platform.");
+    return false;
+#endif
+}
index 52e8902..9cfaeb4 100644 (file)
@@ -55,19 +55,22 @@ if (('BLE' in ca_transport) or ('ALL' in ca_transport)):
        connectivity_env.AppendUnique(CA_SRC = [os.path.join(ca_path,
                                           'adapter_util/cafragmentation.c')])
 
+# Build mbedtls as ocstack uses hash functions from mbedcrypto.lib.
+tls_path = 'extlibs/mbedtls';
+tls_headers_path = 'mbedtls/include';
+if ca_os == 'tizen' and os.path.exists(root_dir + '/' + tls_path):
+    connectivity_env.SConscript(os.path.join(root_dir, tls_path + '/SConscript'))
+    connectivity_env.AppendUnique(CPPPATH = [os.path.join(root_dir, tls_path + '/' + tls_headers_path)])
+else:
+    if ca_os != 'android':
+        connectivity_env.SConscript('#' + tls_path + '/SConscript')
+    connectivity_env.AppendUnique(CPPPATH = ['#' + tls_path + '/' + tls_headers_path])
+
 if connectivity_env.get('SECURED') == '1':
        connectivity_env.AppendUnique(CPPPATH = ['#extlibs/timer'])
        connectivity_env.AppendUnique(CPPPATH = [src_dir + '/resource/csdk/security/include'])
        connectivity_env.AppendUnique(CPPPATH = [os.path.join(root_dir, 'external/inc')])
-       tls_path = 'extlibs/mbedtls';
-       tls_headers_path = 'mbedtls/include';
-       if ca_os == 'tizen' and os.path.exists(root_dir + '/' + tls_path):
-               connectivity_env.SConscript(os.path.join(root_dir, tls_path + '/SConscript'))
-               connectivity_env.AppendUnique(CPPPATH = [os.path.join(root_dir, tls_path + '/' + tls_headers_path)])
-       else:
-               if ca_os != 'android':
-                       connectivity_env.SConscript('#' + tls_path + '/SConscript')
-               connectivity_env.AppendUnique(CPPPATH = ['#' + tls_path + '/' + tls_headers_path])
+       if ca_os != 'tizen' or os.path.exists(root_dir + '/' + tls_path) == False:
                connectivity_env.AppendUnique(CA_SRC = [os.path.join(ca_path, 'adapter_util/ca_adapter_net_ssl.c')])
 
 ca_common_src = None
index ece16ab..7d19138 100644 (file)
@@ -98,7 +98,7 @@ if target_os in ['linux'] and liboctbstack_env.get('SIMULATOR', False):
     liboctbstack_env.Append( RPATH = liboctbstack_env.Literal('\\$$ORIGIN'))
 
 if env.get('SECURED') == '1':
-    liboctbstack_env.AppendUnique(LIBS = ['mbedtls','mbedx509','mbedcrypto'])
+    liboctbstack_env.AppendUnique(LIBS = ['mbedtls','mbedx509'])
 
 if target_os in ['android', 'linux', 'tizen', 'msys_nt', 'windows']:
     liboctbstack_env.PrependUnique(LIBS = ['connectivity_abstraction'])
@@ -151,7 +151,7 @@ if target_os == 'android':
 if env.get('LOGGING'):
     liboctbstack_env.AppendUnique(CPPDEFINES = ['TB_LOG'])
 
-liboctbstack_env.Append(LIBS = ['c_common'])
+liboctbstack_env.Append(LIBS = ['c_common', 'mbedcrypto'])
 
 if liboctbstack_env.get('ROUTING') in ['GW', 'EP']:
     liboctbstack_env.Prepend(LIBS = ['routingmanager'])
index dede976..231c251 100644 (file)
@@ -862,7 +862,7 @@ OCStackResult OCSetPropertyValue(OCPayloadType type, const char *propName, const
 OCStackResult OCGetPropertyValue(OCPayloadType type, const char *propName, void **value);
 
 /**
-* Get the registered persistent storage handler. All modules must use this to obtain access to 
+* Get the registered persistent storage handler. All modules must use this to obtain access to
 * persistent storage.
 *
 * @return pointer to OCPersistentStorage structure on success and NULL otherwise.
index 988d7b2..eb5a077 100755 (executable)
@@ -70,6 +70,7 @@
 #include "ocendpoint.h"
 #include "ocatomic.h"
 #include "platform_features.h"
+#include "oic_platform.h"
 
 #if defined(TCP_ADAPTER) && defined(WITH_CLOUD)
 #include "occonnectionmanager.h"
@@ -4896,6 +4897,34 @@ OCStackResult initResources()
         result = InitializeDeviceProperties();
     }
 
+    // Initialize platform ID of OC_RSRVD_RESOURCE_TYPE_PLATFORM.
+    // Multiple devices or applications running on the same IoTivity platform should have the same
+    // platform ID.
+    if (OC_STACK_OK == result)
+    {
+        uint8_t platformID[OIC_UUID_LENGTH];
+        char uuidString[UUID_STRING_SIZE];
+
+        if (!OICGetPlatformUuid(platformID))
+        {
+            OIC_LOG(WARNING, TAG, "Failed OICGetPlatformUuid(), generate random uuid.");
+            OCGenerateUuid(platformID);
+        }
+
+        if (OCConvertUuidToString(platformID, uuidString))
+        {
+            // Set the platform ID.
+            // Application can overwrite the value set here by calling similar
+            // OCSetPropertyValue(OC_RSRVD_PLATFORM_ID, ...).
+            result = OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_PLATFORM_ID, uuidString);
+        }
+        else
+        {
+            result = OC_STACK_ERROR;
+            OIC_LOG(ERROR, TAG, "Failed OCConvertUuidToString() for platform ID.");
+        }
+    }
+
     return result;
 }
 
index f294f41..e0b80f3 100644 (file)
@@ -49,12 +49,13 @@ stacktest_env.PrependUnique(CPPPATH = [
 stacktest_env.PrependUnique(LIBS = ['octbstack_test',
                                     'ocsrm',
                                     'connectivity_abstraction',
-                                    'coap'])
+                                    'coap',
+                                    'mbedcrypto'])
 if target_os != 'darwin':
     stacktest_env.PrependUnique(LIBS = ['oc_logger'])
 
 if stacktest_env.get('SECURED') == '1':
-    stacktest_env.AppendUnique(LIBS = ['mbedtls', 'mbedx509','mbedcrypto'])
+    stacktest_env.AppendUnique(LIBS = ['mbedtls', 'mbedx509'])
 
 if stacktest_env.get('LOGGING'):
     stacktest_env.AppendUnique(CPPDEFINES = ['TB_LOG'])
index 02f880d..06bef6f 100644 (file)
@@ -62,10 +62,11 @@ unittests_env.PrependUnique(LIBS = [
                'oc_logger',
                'connectivity_abstraction',
                'coap',
-               'timer'])
+               'timer',
+               'mbedcrypto'])
 
 if unittests_env.get('SECURED') == '1':
-       unittests_env.AppendUnique(LIBS = ['mbedtls', 'mbedx509','mbedcrypto'])
+       unittests_env.AppendUnique(LIBS = ['mbedtls', 'mbedx509'])
 
 if unittests_env.get('LOGGING'):
        unittests_env.AppendUnique(CPPDEFINES = ['TB_LOG'])