diff options
author | David Sommerseth <dazo@users.sourceforge.net> | 2009-01-10 00:49:59 +0100 |
---|---|---|
committer | David Sommerseth <dazo@users.sourceforge.net> | 2009-01-10 00:49:59 +0100 |
commit | d5c7a4c72731cc36e0e8cf4fd3a6a0811344a4d8 (patch) | |
tree | 8a9c24f5e99acf06bab383f1c798bb0a174317a5 /common | |
parent | be9c9cab62dbfe03ff6b2a44b9eefc27e8afd0b4 (diff) | |
download | eurephia-d5c7a4c72731cc36e0e8cf4fd3a6a0811344a4d8.tar.gz eurephia-d5c7a4c72731cc36e0e8cf4fd3a6a0811344a4d8.tar.xz eurephia-d5c7a4c72731cc36e0e8cf4fd3a6a0811344a4d8.zip |
Moved eDBsessionGetRandString(...) to a more global and independent eurephia_randstring(...) function
Diffstat (limited to 'common')
-rw-r--r-- | common/eurephiadb_session_common.c | 27 | ||||
-rw-r--r-- | common/eurephiadb_session_common.h | 5 | ||||
-rw-r--r-- | common/randstr.c | 51 | ||||
-rw-r--r-- | common/randstr.h | 26 |
4 files changed, 79 insertions, 30 deletions
diff --git a/common/eurephiadb_session_common.c b/common/eurephiadb_session_common.c index 9e51702..76cd1c0 100644 --- a/common/eurephiadb_session_common.c +++ b/common/eurephiadb_session_common.c @@ -1,6 +1,6 @@ /* eurephiadb_session_common.c -- Common function for handling sessions * - * GPLv2 - Copyright (C) 2008 David Sommerseth <dazo@users.sourceforge.net> + * GPLv2 - Copyright (C) 2008, 2009 David Sommerseth <dazo@users.sourceforge.net> * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -22,7 +22,6 @@ #include <stdlib.h> #include <string.h> #include <unistd.h> -#include <openssl/rand.h> #include <eurephia_nullsafe.h> #include <eurephia_context.h> @@ -95,30 +94,6 @@ int eDBset_session_value(eurephiaCTX *ctx, eurephiaSESSION *session, const char } -// Generate some random data and return a string. -static int rand_init = 0; -int eDBsessionGetRandString(eurephiaCTX *ctx, char *rndstr, int len) { - int attempts = 0; - do { - if( !rand_init ) { - if( !RAND_load_file("/dev/urandom", 64) ) { - eurephia_log(ctx, LOG_FATAL, 0, "Could not load random data from /dev/urandom"); - return 0; - } - rand_init = 1; - } - - if( RAND_pseudo_bytes((unsigned char *) rndstr, len) ) { - return 1; - } - sleep(1); - rand_init = 0; - } while( attempts++ < 11 ); - eurephia_log(ctx, LOG_FATAL, 0, "RAND_pseudo_bytes() could not generate enough random data"); - return 0; -} - - // Free up the memory used by a session structure void eDBfree_session_func(eurephiaCTX *ctx, eurephiaSESSION *session) { if( session == NULL ) { diff --git a/common/eurephiadb_session_common.h b/common/eurephiadb_session_common.h index 34b8a9b..53f7c26 100644 --- a/common/eurephiadb_session_common.h +++ b/common/eurephiadb_session_common.h @@ -1,6 +1,6 @@ /* eurephiadb_session_common.h -- Common function for handling sessions * - * GPLv2 - Copyright (C) 2008 David Sommerseth <dazo@users.sourceforge.net> + * GPLv2 - Copyright (C) 2008, 2009 David Sommerseth <dazo@users.sourceforge.net> * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -26,10 +26,7 @@ int eDBset_session_value(eurephiaCTX *ctx, eurephiaSESSION *session, const char *key, const char *val); #define eDBget_session_value(s, k) eGet_value(s->sessvals, k); -int eDBsessionGetRandString(eurephiaCTX *ctx, char *rndstr, int len); - #define eDBfree_session(c, s) { eDBfree_session_func(c, s); s = NULL; } void eDBfree_session_func(eurephiaCTX *ctx, eurephiaSESSION *sk); - #endif /* !EUREPHIADB_SESSION_COMMON_H_ */ diff --git a/common/randstr.c b/common/randstr.c new file mode 100644 index 0000000..d27ec9f --- /dev/null +++ b/common/randstr.c @@ -0,0 +1,51 @@ +/* randstr.c -- Functions for getting random data + * + * GPLv2 - Copyright (C) 2009 David Sommerseth <dazo@users.sourceforge.net> + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; version 2 + * of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +#include <stdio.h> +#include <unistd.h> +#include <openssl/rand.h> + +#include <eurephia_nullsafe.h> +#include <eurephia_context.h> +#include <eurephia_log.h> + +static int rand_init = 0; + +// Generate some random data and return a string. +int eurephia_randstring(eurephiaCTX *ctx, char *rndstr, size_t len) { + int attempts = 0; + do { + if( !rand_init ) { + if( !RAND_load_file("/dev/urandom", 64) ) { + eurephia_log(ctx, LOG_FATAL, 0, "Could not load random data from /dev/urandom"); + return 0; + } + rand_init = 1; + } + + if( RAND_pseudo_bytes((unsigned char *) rndstr, len) ) { + return 1; + } + sleep(1); + rand_init = 0; + } while( attempts++ < 11 ); + eurephia_log(ctx, LOG_FATAL, 0, "RAND_pseudo_bytes() could not generate enough random data"); + return 0; +} diff --git a/common/randstr.h b/common/randstr.h new file mode 100644 index 0000000..c4739a6 --- /dev/null +++ b/common/randstr.h @@ -0,0 +1,26 @@ +/* randstr.h -- Functions for getting random data + * + * GPLv2 - Copyright (C) 2009 David Sommerseth <dazo@users.sourceforge.net> + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; version 2 + * of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +#ifndef RANDSTR_H_ +#define RANDSTR_H_ + +int eurephia_randstring(eurephiaCTX *ctx, char *rndstr, size_t len); + +#endif /* !RANDSTR_H_ */ |