OSDN Git Service

osi: add osi_rand()
authorMarie Janssen <jamuraa@google.com>
Thu, 10 Mar 2016 19:27:53 +0000 (11:27 -0800)
committerAndre Eisenbach <eisenbach@google.com>
Fri, 25 Mar 2016 20:17:57 +0000 (13:17 -0700)
Provide a replacement for rand() which has a better random source.

Change-Id: I38a8c74d86b89ec160b43b0f648f53b463be89bb

btif/src/btif_core.c
osi/Android.mk
osi/BUILD.gn
osi/include/osi.h
osi/src/osi.c [new file with mode: 0644]
osi/test/rand_test.cpp [new file with mode: 0644]

index 5415f1a..c3a164d 100644 (file)
@@ -383,16 +383,14 @@ static void btif_fetch_local_bdaddr(bt_bdaddr_t *local_addr)
     if (!valid_bda)
     {
         bdstr_t bdstr;
-        /* Seed the random number generator */
-        srand((unsigned int) (time(0)));
 
         /* No autogen BDA. Generate one now. */
         local_addr->address[0] = 0x22;
         local_addr->address[1] = 0x22;
-        local_addr->address[2] = (uint8_t) ((rand() >> 8) & 0xFF);
-        local_addr->address[3] = (uint8_t) ((rand() >> 8) & 0xFF);
-        local_addr->address[4] = (uint8_t) ((rand() >> 8) & 0xFF);
-        local_addr->address[5] = (uint8_t) ((rand() >> 8) & 0xFF);
+        local_addr->address[2] = (uint8_t) osi_rand();
+        local_addr->address[3] = (uint8_t) osi_rand();
+        local_addr->address[4] = (uint8_t) osi_rand();
+        local_addr->address[5] = (uint8_t) osi_rand();
 
         /* Convert to ascii, and store as a persistent property */
         bdaddr_to_string(local_addr, bdstr, sizeof(bdstr));
index 23b01f1..4d372ec 100644 (file)
@@ -41,6 +41,7 @@ btosiCommonSrc := \
     ./src/list.c \
     ./src/metrics.cpp \
     ./src/mutex.c \
+    ./src/osi.c \
     ./src/properties.c \
     ./src/reactor.c \
     ./src/ringbuffer.c \
@@ -68,6 +69,7 @@ btosiCommonTestSrc := \
     ./test/hash_map_utils_test.cpp \
     ./test/list_test.cpp \
     ./test/properties_test.cpp \
+    ./test/rand_test.cpp \
     ./test/reactor_test.cpp \
     ./test/ringbuffer_test.cpp \
     ./test/semaphore_test.cpp \
index 88cdbdd..ed91fb1 100644 (file)
@@ -33,6 +33,7 @@ static_library("osi") {
     "src/list.c",
     "src/metrics_linux.cpp",
     "src/mutex.c",
+    "src/osi.c",
     "src/properties.c",
     "src/reactor.c",
     "src/ringbuffer.c",
@@ -72,6 +73,7 @@ executable("net_test_osi") {
     "test/hash_map_utils_test.cpp",
     "test/list_test.cpp",
     "test/properties_test.cpp",
+    "test/rand_test.cpp",
     "test/reactor_test.cpp",
     "test/ringbuffer_test.cpp",
     "test/thread_test.cpp",
index d2cc0a3..8cdf808 100644 (file)
@@ -1,3 +1,21 @@
+/******************************************************************************
+ *
+ *  Copyright (C) 2016 Google, Inc.
+ *
+ *  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.
+ *
+ ******************************************************************************/
+
 #pragma once
 
 #include <stdbool.h>
@@ -35,3 +53,9 @@
 
 #define PTR_TO_INT(p) ((int) ((intptr_t) (p)))
 #define INT_TO_PTR(i) ((void *) ((intptr_t) (i)))
+
+// Obtain a random number between 0 and INT_MAX inclusive.
+// Taken from a system random source such as /dev/random.
+// No guarantees of distribution are made.
+// Effort is made for this to come from a real random source.
+int osi_rand(void);
diff --git a/osi/src/osi.c b/osi/src/osi.c
new file mode 100644 (file)
index 0000000..2a6a779
--- /dev/null
@@ -0,0 +1,53 @@
+/******************************************************************************
+ *
+ *  Copyright (C) 2016 Google, Inc.
+ *
+ *  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.
+ *
+ ******************************************************************************/
+
+#define LOG_TAG "bt_osi_rand"
+
+#include <assert.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <string.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+#include "osi/include/log.h"
+#include "osi/include/osi.h"
+
+#define RANDOM_PATH "/dev/urandom"
+
+int osi_rand(void) {
+  int rand;
+  int rand_fd = open(RANDOM_PATH, O_RDONLY);
+
+  if (rand_fd == INVALID_FD) {
+    LOG_ERROR(LOG_TAG, "%s can't open rand fd %s: %s ", __func__, RANDOM_PATH,
+              strerror(errno));
+    assert(0);
+  }
+
+  ssize_t read_bytes = read(rand_fd, &rand, sizeof(rand));
+  close(rand_fd);
+
+  assert(read_bytes == sizeof(rand));
+
+  if (rand < 0)
+    rand = -rand;
+
+  return rand;
+}
diff --git a/osi/test/rand_test.cpp b/osi/test/rand_test.cpp
new file mode 100644 (file)
index 0000000..cf0d7f6
--- /dev/null
@@ -0,0 +1,19 @@
+#include <gtest/gtest.h>
+
+#include "AllocationTestHarness.h"
+
+extern "C" {
+#include "osi/include/osi.h"
+}
+
+class RandTest : public AllocationTestHarness {};
+
+TEST_F(RandTest, test_rand) {
+  // We can't guarantee any distribution
+  // We'd like it to not crash though.
+  for (int i = 0; i < 10; i++) {
+    int x;
+    x = osi_rand();
+    EXPECT_TRUE(x >= 0);
+  }
+}