changeset 55:9626af32f256

common: use arc4random() when available Resolves #8 Related to #3 Signed-off-by: Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
author Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
date Fri, 03 Jul 2015 11:37:06 -0400
parents f55b46222eed
children c3da169e5719
files cmake/config.cmake src/common/CMakeLists.txt src/common/include/nomad/config.h.in src/common/include/nomad/rand.h src/common/rand.c
diffstat 5 files changed, 82 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/cmake/config.cmake	Fri Jul 03 11:34:19 2015 -0400
+++ b/cmake/config.cmake	Fri Jul 03 11:37:06 2015 -0400
@@ -20,6 +20,10 @@
 # SOFTWARE.
 #
 
+include(CheckFunctionExists)
+
+check_function_exists(arc4random HAVE_ARC4RANDOM)
+
 set(CMAKE_MODULE_PATH "${CMAKE_DIR}/Modules")
 find_package(umem)
 find_package(avl)
--- a/src/common/CMakeLists.txt	Fri Jul 03 11:34:19 2015 -0400
+++ b/src/common/CMakeLists.txt	Fri Jul 03 11:37:06 2015 -0400
@@ -23,6 +23,7 @@
 add_library(nomad_common SHARED
 	node.c
 	oid.c
+	rand.c
 	uuid.c
 	vclock.c
 )
@@ -38,6 +39,7 @@
 		include/nomad/attr.h
 		include/nomad/config.h
 		include/nomad/error.h
+		include/nomad/rand.h
 		include/nomad/time.h
 		include/nomad/types.h
 	DESTINATION include/nomad
--- a/src/common/include/nomad/config.h.in	Fri Jul 03 11:34:19 2015 -0400
+++ b/src/common/include/nomad/config.h.in	Fri Jul 03 11:37:06 2015 -0400
@@ -23,4 +23,6 @@
 #ifndef __NOMAD_CONFIG_H
 #define __NOMAD_CONFIG_H
 
+#cmakedefine HAVE_ARC4RANDOM 1
+
 #endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/common/include/nomad/rand.h	Fri Jul 03 11:37:06 2015 -0400
@@ -0,0 +1,29 @@
+/*
+ * Copyright (c) 2015 Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#ifndef __NOMAD_RAND_H
+#define __NOMAD_RAND_H
+
+extern uint32_t rand32(void);
+extern uint64_t rand64(void);
+
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/common/rand.c	Fri Jul 03 11:37:06 2015 -0400
@@ -0,0 +1,45 @@
+/*
+ * Copyright (c) 2015 Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include <nomad/types.h>
+#include <nomad/rand.h>
+#include <nomad/config.h>
+
+uint32_t rand32(void)
+{
+#ifdef HAVE_ARC4RANDOM
+	return arc4random();
+#else
+#error "Need a way to generate random uint32_t"
+#endif
+}
+
+uint64_t rand64(void)
+{
+	uint64_t tmp;
+
+	tmp   = rand32();
+	tmp <<= 32;
+	tmp  |= rand32();
+
+	return tmp;
+}