OW_RandomNumber.cpp

Go to the documentation of this file.
00001 /*******************************************************************************
00002 * Copyright (C) 2001-2004 Vintela, Inc. All rights reserved.
00003 *
00004 * Redistribution and use in source and binary forms, with or without
00005 * modification, are permitted provided that the following conditions are met:
00006 *
00007 *  - Redistributions of source code must retain the above copyright notice,
00008 *    this list of conditions and the following disclaimer.
00009 *
00010 *  - Redistributions in binary form must reproduce the above copyright notice,
00011 *    this list of conditions and the following disclaimer in the documentation
00012 *    and/or other materials provided with the distribution.
00013 *
00014 *  - Neither the name of Vintela, Inc. nor the names of its
00015 *    contributors may be used to endorse or promote products derived from this
00016 *    software without specific prior written permission.
00017 *
00018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
00019 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00020 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00021 * ARE DISCLAIMED. IN NO EVENT SHALL Vintela, Inc. OR THE CONTRIBUTORS
00022 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00023 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00024 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00025 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00026 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00027 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00028 * POSSIBILITY OF SUCH DAMAGE.
00029 *******************************************************************************/
00030 
00036 #include "OW_config.h"
00037 #include "OW_RandomNumber.hpp"
00038 #include "OW_Assertion.hpp"
00039 #include "OW_ThreadOnce.hpp"
00040 #include "OW_Mutex.hpp"
00041 #include "OW_MutexLock.hpp"
00042 #include <fstream>
00043 #include <sys/types.h>
00044 
00045 #ifdef OW_HAVE_UNISTD_H
00046 #include <unistd.h>
00047 #endif
00048 
00049 #ifdef OW_HAVE_SYS_TIME_H
00050 #include <sys/time.h>
00051 #endif
00052 
00053 #include <stdlib.h>
00054 #include <time.h>
00055 
00056 namespace OW_NAMESPACE
00057 {
00058 
00060 namespace
00061 {
00062 OnceFlag guard;
00063 unsigned int seed = 0;
00064 }
00065 
00067 RandomNumber::RandomNumber(Int32 lowVal, Int32 highVal)
00068 : m_lowVal(lowVal), m_highVal(highVal)
00069 {
00070    if (lowVal > highVal)
00071    {
00072       m_lowVal = highVal;
00073       m_highVal = lowVal;
00074    }
00075    callOnce(guard, &initRandomness);
00076 }
00077    
00079 void
00080 RandomNumber::initRandomness()
00081 {
00082 #ifdef OW_WIN32
00083    time_t timeval = ::time(NULL);
00084    seed = timeval;
00085 #else
00086    // use the time as part of the seed
00087    struct timeval tv;
00088    gettimeofday(&tv, 0);
00089    // try to get something from the kernel
00090    std::ifstream infile("/dev/urandom", std::ios::in);
00091    if (!infile)
00092    {
00093       infile.open("/dev/random", std::ios::in);
00094    }
00095    // don't initialize this, we may get random stack
00096    // junk in case infile isn't usable.
00097    unsigned int dev_rand_input;
00098    if (infile)
00099    {
00100       infile.read(reinterpret_cast<char*>(&dev_rand_input), sizeof(dev_rand_input));
00101       infile.close();
00102    }
00103    // Build the seed. Take into account our pid and uid.
00104    seed = dev_rand_input ^ (getpid() << 16) ^ getuid() ^ tv.tv_sec ^ tv.tv_usec;
00105 #endif
00106 #ifdef OW_HAVE_SRANDOM
00107    srandom(seed);
00108 #else
00109    srand(seed);
00110 #endif
00111 }
00112 
00114 void
00115 RandomNumber::saveRandomState()
00116 {
00117    // Do nothing. This function is so that RandomNumber has the same interface as CryptographicRandomNumber
00118 }
00119 
00120 namespace
00121 {
00122 Mutex g_guard;
00123 }
00125 Int32
00126 RandomNumber::getNextNumber()
00127 {
00128    MutexLock lock(g_guard);
00129 #ifdef OW_HAVE_RANDOM
00130    return m_lowVal + (random() % (m_highVal - m_lowVal + 1));
00131 #else
00132    return m_lowVal + (rand() % (m_highVal - m_lowVal + 1));
00133 #endif
00134 }
00135 
00136 } // end namespace OW_NAMESPACE
00137 

Generated on Thu Feb 9 08:48:10 2006 for openwbem by  doxygen 1.4.6