# HG changeset patch # User Josef 'Jeff' Sipek # Date 1435937826 14400 # Node ID 9626af32f256cdb211e80b6874f2e4bf7f4b260e # Parent f55b46222eed0f1b0888005590dc3b08d675a525 common: use arc4random() when available Resolves #8 Related to #3 Signed-off-by: Josef 'Jeff' Sipek diff -r f55b46222eed -r 9626af32f256 cmake/config.cmake --- 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) diff -r f55b46222eed -r 9626af32f256 src/common/CMakeLists.txt --- 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 diff -r f55b46222eed -r 9626af32f256 src/common/include/nomad/config.h.in --- 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 diff -r f55b46222eed -r 9626af32f256 src/common/include/nomad/rand.h --- /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 + * + * 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 diff -r f55b46222eed -r 9626af32f256 src/common/rand.c --- /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 + * + * 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 +#include +#include + +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; +}