Repository: qpid-proton
Updated Branches:
  refs/heads/master 1cfeef1c0 -> 060b1aa8a


PROTON-904 Remove the dependency on uuid

Instead of relying on libuuid for uuid generation, let proton-c have a
built-in uuid4 generator to generate a somewhat random name.


Project: http://git-wip-us.apache.org/repos/asf/qpid-proton/repo
Commit: http://git-wip-us.apache.org/repos/asf/qpid-proton/commit/060b1aa8
Tree: http://git-wip-us.apache.org/repos/asf/qpid-proton/tree/060b1aa8
Diff: http://git-wip-us.apache.org/repos/asf/qpid-proton/diff/060b1aa8

Branch: refs/heads/master
Commit: 060b1aa8a0631ce3bdd05bc09374e2d684ccd201
Parents: 1cfeef1
Author: Flavio Percoco <[email protected]>
Authored: Sat Jun 6 15:56:50 2015 +0200
Committer: Ken Giusti <[email protected]>
Committed: Thu Jun 11 10:38:09 2015 -0400

----------------------------------------------------------------------
 proton-c/CMakeLists.txt            | 24 +----------------
 proton-c/bindings/python/setup.py  | 18 ++++++-------
 proton-c/src/messenger/messenger.c | 42 +++++++++++++++++++++++++++--
 proton-c/src/platform.c            | 48 +++++++++------------------------
 proton-c/src/platform.h            | 19 ++++++-------
 5 files changed, 70 insertions(+), 81 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/060b1aa8/proton-c/CMakeLists.txt
----------------------------------------------------------------------
diff --git a/proton-c/CMakeLists.txt b/proton-c/CMakeLists.txt
index b534e86..5b05e38 100644
--- a/proton-c/CMakeLists.txt
+++ b/proton-c/CMakeLists.txt
@@ -27,6 +27,7 @@ if(WIN32 AND NOT CYGWIN)
   # linking against Windows native libraries, including mingw
   set (PN_WINAPI TRUE)
   set (PLATFORM_LIBS ws2_32 Rpcrt4)
+  list(APPEND PLATFORM_DEFINITIONS "PN_WINAPI")
 endif(WIN32 AND NOT CYGWIN)
 
 # Can't use ${CMAKE_VERSION) as it is not available in all versions of cmake 
2.6
@@ -153,29 +154,6 @@ else (CLOCK_GETTIME_IN_LIBC)
   endif (CLOCK_GETTIME_IN_RT)
 endif (CLOCK_GETTIME_IN_LIBC)
 
-CHECK_SYMBOL_EXISTS(uuid_generate "uuid/uuid.h" UUID_GENERATE_IN_LIBC)
-if (UUID_GENERATE_IN_LIBC)
-  list(APPEND PLATFORM_DEFINITIONS "USE_UUID_GENERATE")
-else (UUID_GENERATE_IN_LIBC)
-  CHECK_LIBRARY_EXISTS (uuid uuid_generate "" UUID_GENERATE_IN_UUID)
-  if (UUID_GENERATE_IN_UUID)
-    set (UUID_LIB uuid)
-    list(APPEND PLATFORM_DEFINITIONS "USE_UUID_GENERATE")
-  else (UUID_GENERATE_IN_UUID)
-    CHECK_SYMBOL_EXISTS(uuid_create "uuid.h" UUID_CREATE_IN_LIBC)
-    if (UUID_CREATE_IN_LIBC)
-      list(APPEND PLATFORM_DEFINITIONS "USE_UUID_CREATE")
-    else (UUID_CREATE_IN_LIBC)
-      CHECK_SYMBOL_EXISTS(UuidToString "rpc.h" WIN_UUID)
-      if (WIN_UUID)
-        list(APPEND PLATFORM_DEFINITIONS "USE_WIN_UUID")
-      else (WIN_UUID)
-        message(FATAL_ERROR "No Uuid API found")
-      endif (WIN_UUID)
-    endif (UUID_CREATE_IN_LIBC)
-  endif (UUID_GENERATE_IN_UUID)
-endif (UUID_GENERATE_IN_LIBC)
-
 if (PN_WINAPI)
   CHECK_SYMBOL_EXISTS(strerror_s "string.h" STRERROR_S_IN_WINAPI)
   if (STRERROR_S_IN_WINAPI)

http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/060b1aa8/proton-c/bindings/python/setup.py
----------------------------------------------------------------------
diff --git a/proton-c/bindings/python/setup.py 
b/proton-c/bindings/python/setup.py
index 79168d2..84b5a66 100755
--- a/proton-c/bindings/python/setup.py
+++ b/proton-c/bindings/python/setup.py
@@ -157,7 +157,14 @@ class Configure(build_ext):
         # depending on the version. Specifically, lets avoid adding things
         # we don't need.
         sources = []
-        libraries = ['uuid']
+        libraries = []
+        extra_compile_args = [
+            '-std=gnu99',
+            '-Dqpid_proton_EXPORTS',
+            '-DUSE_ATOLL',
+            '-DUSE_CLOCK_GETTIME',
+            '-DUSE_STRERROR_R',
+        ]
 
         for subdir in ['object', 'framing', 'codec', 'dispatcher',
                        'engine', 'events', 'transport',
@@ -230,14 +237,7 @@ class Configure(build_ext):
             # the place. All these compile arguments will be appended to
             # the GCC command. This list of flags is not used during the
             # linking phase.
-            extra_compile_args = [
-                '-std=gnu99',
-                '-Dqpid_proton_EXPORTS',
-                '-DUSE_ATOLL',
-                '-DUSE_CLOCK_GETTIME',
-                '-DUSE_STRERROR_R',
-                '-DUSE_UUID_GENERATE',
-            ],
+            extra_compile_args = extra_compile_args,
 
             # If you need to add flags to the linking phase
             # this is the right place to do it. Just like the compile flags,

http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/060b1aa8/proton-c/src/messenger/messenger.c
----------------------------------------------------------------------
diff --git a/proton-c/src/messenger/messenger.c 
b/proton-c/src/messenger/messenger.c
index f226f7b..ec4fb2c 100644
--- a/proton-c/src/messenger/messenger.c
+++ b/proton-c/src/messenger/messenger.c
@@ -485,13 +485,51 @@ static void pn_connection_ctx_free(pn_connection_t *conn)
 #define pn_tracker_direction(tracker) ((tracker) & (0x1000000000000000))
 #define pn_tracker_sequence(tracker) ((pn_sequence_t) ((tracker) & 
(0x00000000FFFFFFFF)))
 
+
 static char *build_name(const char *name)
 {
+  static bool seeded = false;
+  // UUID standard format: 8-4-4-4-12 (36 chars, 32 alphanumeric and 4 hypens)
+  static const char *uuid_fmt = 
"%02X%02X%02X%02X-%02X%02X-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X";
+
+  int count = 0;
+  char *generated;
+  uint8_t bytes[16];
+  unsigned int r = 0;
+
   if (name) {
     return pn_strdup(name);
-  } else {
-    return pn_i_genuuid();
   }
+
+  if (!seeded) {
+    int pid = pn_i_getpid();
+    srand(pn_i_now() + pid);
+    seeded = true;
+  }
+
+  while (count < 16) {
+    if (!r) {
+      r =  (unsigned int) rand();
+    }
+
+    bytes[count] = r & 0xFF;
+    r >>= 8;
+    count++;
+  }
+
+  // From RFC4122, the version bits are set to 0100
+  bytes[6] = (bytes[6] & 0x0F) | 0x40;
+
+  // From RFC4122, the top two bits of byte 8 get set to 01
+  bytes[8] = (bytes[8] & 0x3F) | 0x80;
+
+  generated = (char *) malloc(37*sizeof(char));
+  sprintf(generated, uuid_fmt,
+         bytes[0], bytes[1], bytes[2], bytes[3],
+         bytes[4], bytes[5], bytes[6], bytes[7],
+         bytes[8], bytes[9], bytes[10], bytes[11],
+         bytes[12], bytes[13], bytes[14], bytes[15]);
+  return generated;
 }
 
 struct pn_link_ctx_t {

http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/060b1aa8/proton-c/src/platform.c
----------------------------------------------------------------------
diff --git a/proton-c/src/platform.c b/proton-c/src/platform.c
index 8f8ac5f..5470c5a 100644
--- a/proton-c/src/platform.c
+++ b/proton-c/src/platform.c
@@ -22,6 +22,18 @@
 #include "platform.h"
 #include "util.h"
 
+#ifdef PN_WINAPI
+#include <windows.h>
+int pn_i_getpid() {
+  return (int) GetCurrentProcessId();
+}
+#else
+#include <unistd.h>
+int pn_i_getpid() {
+  return (int) getpid();
+}
+#endif
+
 /* Allow for systems that do not implement clock_gettime()*/
 #ifdef USE_CLOCK_GETTIME
 #include <time.h>
@@ -53,42 +65,6 @@ pn_timestamp_t pn_i_now(void)
 }
 #endif
 
-#ifdef USE_UUID_GENERATE
-#include <uuid/uuid.h>
-#include <stdlib.h>
-char* pn_i_genuuid(void) {
-    char *generated = (char *) malloc(37*sizeof(char));
-    uuid_t uuid;
-    uuid_generate(uuid);
-    uuid_unparse(uuid, generated);
-    return generated;
-}
-#elif USE_UUID_CREATE
-#include <uuid.h>
-char* pn_i_genuuid(void) {
-    char *generated;
-    uuid_t uuid;
-    uint32_t rc;
-    uuid_create(&uuid, &rc);
-    // Under FreeBSD the returned string is newly allocated from the heap
-    uuid_to_string(&uuid, &generated, &rc);
-    return generated;
-}
-#elif USE_WIN_UUID
-#include <rpc.h>
-char* pn_i_genuuid(void) {
-    unsigned char *generated;
-    UUID uuid;
-    UuidCreate(&uuid);
-    UuidToString(&uuid, &generated);
-    char* r = pn_strdup((const char*)generated);
-    RpcStringFree(&generated);
-    return r;
-}
-#else
-#error "Don't know how to generate uuid strings on this platform"
-#endif
-
 #ifdef USE_STRERROR_R
 #include <string.h>
 static void pn_i_strerror(int errnum, char *buf, size_t buflen) {

http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/060b1aa8/proton-c/src/platform.h
----------------------------------------------------------------------
diff --git a/proton-c/src/platform.h b/proton-c/src/platform.h
index 6962493..6a0bbc1 100644
--- a/proton-c/src/platform.h
+++ b/proton-c/src/platform.h
@@ -29,6 +29,14 @@
 extern "C" {
 #endif
 
+/** Get the current PID
+ *
+ * @return process id
+ * @internal
+ */
+int pn_i_getpid(void);
+
+
 /** Get the current time in pn_timestamp_t format.
  *
  * Returns current time in milliseconds since Unix Epoch,
@@ -39,17 +47,6 @@ extern "C" {
  */
 pn_timestamp_t pn_i_now(void);
 
-/** Generate a UUID in string format.
- *
- * Returns a newly generated UUID in the standard 36 char format.
- * The returned char* array is zero terminated.
- * (eg. d797830d-0f5b-49d4-a83f-adaa78425125)
- *
- * @return newly generated stringised UUID
- * @internal
- */
-char* pn_i_genuuid(void);
-
 /** Generate system error message.
  *
  * Populate the proton error structure based on the last system error


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to