|
1 // |
|
2 // OSSP asgui - Accounting system graphical user interface |
|
3 // Copyright (c) 2002-2004 The OSSP Project (http://www.ossp.org/) |
|
4 // Copyright (c) 2002-2004 Ralf S. Engelschall <rse@engelschall.com> |
|
5 // Copyright (c) 2002-2004 Michael Schloh von Bennewitz <michael@schloh.com> |
|
6 // Copyright (c) 2002-2004 Cable & Wireless Telecommunications Services GmbH |
|
7 // |
|
8 // This file is part of OSSP asgui, an accounting system graphical user |
|
9 // interface which can be found at http://www.ossp.org/pkg/tool/asgui/. |
|
10 // |
|
11 // Permission to use, copy, modify, and distribute this software for |
|
12 // any purpose with or without fee is hereby granted, provided that |
|
13 // the above copyright notice and this permission notice appear in all |
|
14 // copies. |
|
15 // |
|
16 // THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED |
|
17 // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
|
18 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
|
19 // IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR |
|
20 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF |
|
23 // USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
|
24 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
|
25 // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
|
26 // OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
|
27 // SUCH DAMAGE. |
|
28 // |
|
29 // as_rand.cpp: ISO C++ implementation |
|
30 // |
|
31 |
|
32 #include <fcntl.h> |
|
33 #include <cerrno> |
|
34 |
|
35 #include "as_rand.h" |
|
36 |
|
37 #if HAVE_UNISTD_H |
|
38 #include <unistd.h> |
|
39 #endif // HAVE_UNISTD_H |
|
40 #if HAVE_STDLIB_H |
|
41 #include <stdlib.h> |
|
42 #endif // HAVE_STDLIB_H |
|
43 |
|
44 #if TIME_WITH_SYS_TIME |
|
45 #include <sys/time.h> |
|
46 #include <time.h> |
|
47 #else |
|
48 #if HAVE_SYS_TIME_H |
|
49 #include <sys/time.h> |
|
50 #else |
|
51 #include <time.h> |
|
52 #endif // HAVE_SYS_TIME_H |
|
53 #endif // TIME_WITH_SYS_TIME |
|
54 |
|
55 |
|
56 namespace AS { |
|
57 |
|
58 // Constructor |
|
59 Rand::Rand(void) |
|
60 { |
|
61 this->nFd = -4; // Object fresh status |
|
62 this->openDevice(); // Open the random device |
|
63 // if (this->nFd < 0) |
|
64 // throw Genexcept("Could not open /dev/urandom or /dev/random."); |
|
65 } |
|
66 |
|
67 // Destructor |
|
68 Rand::~Rand(void) |
|
69 { |
|
70 this->closeDevice(); // Close the random device |
|
71 this->nFd = -4; // Return this object to its fresh status |
|
72 } |
|
73 |
|
74 // |
|
75 // Rand::openDevice(void) |
|
76 // Open the random device associated with this object |
|
77 // |
|
78 void Rand::openDevice(void) |
|
79 { |
|
80 struct timeval Time; |
|
81 |
|
82 if (this->nFd == -4) { // -4 indicates a fresh object |
|
83 gettimeofday(&Time, 0); |
|
84 this->nFd = open("/dev/urandom", O_RDONLY); |
|
85 if (this->nFd == -1) |
|
86 this->nFd = open("/dev/random", O_RDONLY | O_NONBLOCK); |
|
87 srand((getpid() << 16) ^ getuid() ^ Time.tv_sec ^ Time.tv_usec); // Seed |
|
88 } |
|
89 |
|
90 // Improve randomness by stirring in entropy |
|
91 gettimeofday(&Time, 0); |
|
92 for (int nIter = (Time.tv_sec ^ Time.tv_usec) & 0x1F; nIter > 0; nIter--) |
|
93 rand(); |
|
94 } |
|
95 |
|
96 // |
|
97 // Rand::closeDevice(void) |
|
98 // Close the random device associated with this object |
|
99 // |
|
100 void Rand::closeDevice(void) |
|
101 { |
|
102 if (this->nFd >= 0) |
|
103 close(this->nFd); |
|
104 } |
|
105 |
|
106 // |
|
107 // Rand::genData(void *pvBuf, int nBytes) |
|
108 // Generate a series of random data from the system |
|
109 // standard random devices /dev/[u|s]random |
|
110 // |
|
111 void Rand::genData(void *pBuf, int nBytes) |
|
112 { |
|
113 int nRead = 0; |
|
114 int nLost = 0; |
|
115 char *szOut = (char *)pBuf; |
|
116 |
|
117 if (this->nFd >= 0) { |
|
118 while (nBytes > 0) { |
|
119 nRead = read(this->nFd, szOut, nBytes); |
|
120 if ((nRead < 0) && ((errno == EINTR) || (errno == EAGAIN))) |
|
121 continue; |
|
122 if (nRead <= 0) { |
|
123 if (nLost++ == 8) |
|
124 break; |
|
125 continue; |
|
126 } |
|
127 nBytes -= nRead; |
|
128 szOut += nRead; |
|
129 nLost = 0; |
|
130 } |
|
131 } |
|
132 |
|
133 // For systems with no /dev/random, we do the next best thing |
|
134 for (int nIter = 0; nIter< nBytes; nIter++) |
|
135 *szOut++ = rand() & 0xFF; |
|
136 } |
|
137 } // namespace AS |