24 #define _DEFAULT_SOURCE
30 #include <sys/errno.h>
31 #include <sys/fcntl.h>
35 #define srand(x) srandom(x)
36 #define rand() random()
56 #define TIME_OFFSET_HIGH 0x01B21DD2
57 #define TIME_OFFSET_LOW 0x13814000
59 int FairRunIdGenerator::get_random_fd(
void)
67 fd =
open(
"/dev/urandom", O_RDONLY);
69 fd =
open(
"/dev/random", O_RDONLY | O_NONBLOCK);
71 srand((getpid() << 16) ^ getuid() ^ tv.tv_sec ^ tv.tv_usec);
75 for (i = (tv.tv_sec ^ tv.tv_usec) & 0x1F; i > 0; i--) {
85 void FairRunIdGenerator::get_random_bytes(
void* buf,
int nbytes)
87 int i, fd = get_random_fd();
89 char* cp =
static_cast<char*
>(buf);
93 i = read(fd, cp, nbytes);
94 if ((i < 0) && ((errno == EINTR) || (errno == EAGAIN))) {
98 if (lose_counter++ == 8) {
109 for (i = 0; i < nbytes; i++) {
110 *cp++ =
rand() & 0xFF;
118 int FairRunIdGenerator::get_node_id(
unsigned char* )
122 struct ifreq ifr, *ifrp;
136 #define max(a, b) ((a) > (b) ? (a) : (b))
138 #define ifreq_size(i) max(sizeof(struct ifreq), sizeof((i).ifr_name) + (i).ifr_addr.sa_len)
140 #define ifreq_size(i) sizeof(struct ifreq)
143 sd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
147 memset(buf, 0,
sizeof(buf));
148 ifc.ifc_len =
sizeof(buf);
150 if (ioctl(sd, SIOCGIFCONF, (
char*)&ifc) < 0) {
155 for (i = 0; i < n; i += ifreq_size(*ifr)) {
156 ifrp = (
struct ifreq*)((
char*)ifc.ifc_buf + i);
157 strncpy(ifr.ifr_name, ifrp->ifr_name, IFNAMSIZ);
159 if (ioctl(sd, SIOCGIFHWADDR, &ifr) < 0) {
162 a = (
unsigned char*)&ifr.ifr_hwaddr.sa_data;
165 if (ioctl(sd, SIOCGENADDR, &ifr) < 0) {
168 a = (
unsigned char*)ifr.ifr_enaddr;
178 if (!a[0] && !a[1] && !a[2] && !a[3] && !a[4] && !a[5]) {
182 memcpy(node_id, a, 6);
193 #define MAX_ADJUSTMENT 10
197 static int adjustment = 0;
198 static struct timeval last = {0, 0};
201 unsigned long long clock_reg;
204 gettimeofday(&tv, 0);
205 if ((last.tv_sec == 0) && (last.tv_usec == 0)) {
206 get_random_bytes(&clock_seq,
sizeof(clock_seq));
211 if ((tv.tv_sec < last.tv_sec) || ((tv.tv_sec == last.tv_sec) && (tv.tv_usec < last.tv_usec))) {
212 clock_seq = (clock_seq + 1) & 0x1FFF;
215 }
else if ((tv.tv_sec == last.tv_sec) && (tv.tv_usec == last.tv_usec)) {
225 clock_reg = tv.tv_usec * 10 + adjustment;
226 clock_reg += (
static_cast<unsigned long long>(tv.tv_sec)) * 10000000;
227 clock_reg += ((
static_cast<unsigned long long>(0x01B21DD2)) << 32) + 0x13814000;
229 *clock_high = clock_reg >> 32;
230 *clock_low = clock_reg;
231 *ret_clock_seq = clock_seq;
233 fTimeSpec.tv_sec = last.tv_sec;
235 fTimeSpec.tv_nsec = 0.;
240 void FairRunIdGenerator::uuid_generate_time(
uuid_t out)
242 static unsigned char node_id[6];
243 static int has_init = 0;
248 if (get_node_id(node_id) <= 0) {
249 get_random_bytes(node_id, 6);
259 get_clock(&clock_mid, &uu.time_low, &uu.clock_seq);
260 uu.clock_seq |= 0x8000;
261 uu.time_mid =
static_cast<uint16_t>(clock_mid);
262 uu.time_hi_and_version = (clock_mid >> 16) | 0x1000;
263 memcpy(uu.node, node_id, 6);
267 void FairRunIdGenerator::uuid_generate_random(
uuid_t out)
272 get_random_bytes(buf,
sizeof(buf));
273 uuid_unpack(buf, &uu);
275 uu.clock_seq = (uu.clock_seq & 0x3FFF) | 0x8000;
276 uu.time_hi_and_version = (uu.time_hi_and_version & 0x0FFF) | 0x4000;
286 void FairRunIdGenerator::uuid_generate(
uuid_t out)
295 uuid_generate_time(out);
309 void FairRunIdGenerator::uuid_pack(
const struct uuid* uu,
uuid_t ptr)
312 unsigned char* out = ptr;
315 out[3] =
static_cast<unsigned char>(tmp);
317 out[2] =
static_cast<unsigned char>(tmp);
319 out[1] =
static_cast<unsigned char>(tmp);
321 out[0] =
static_cast<unsigned char>(tmp);
324 out[5] =
static_cast<unsigned char>(tmp);
326 out[4] =
static_cast<unsigned char>(tmp);
328 tmp = uu->time_hi_and_version;
329 out[7] =
static_cast<unsigned char>(tmp);
331 out[6] =
static_cast<unsigned char>(tmp);
334 out[9] =
static_cast<unsigned char>(tmp);
336 out[8] =
static_cast<unsigned char>(tmp);
338 memcpy(out + 10, uu->node, 6);
352 void FairRunIdGenerator::uuid_unpack(
const uuid_t in,
struct uuid* uu)
358 tmp = (tmp << 8) | *ptr++;
359 tmp = (tmp << 8) | *ptr++;
360 tmp = (tmp << 8) | *ptr++;
364 tmp = (tmp << 8) | *ptr++;
368 tmp = (tmp << 8) | *ptr++;
369 uu->time_hi_and_version = tmp;
372 tmp = (tmp << 8) | *ptr++;
375 memcpy(uu->node, ptr, 6);
400 struct timespec ret_tv;
unsigned int generateId(void)
parOut open("mq_ex_params.root")
unsigned short int uint16_t