2017-06-20 13:33:20 +00:00
|
|
|
|
|
|
|
/*
|
2017-07-05 12:42:49 +00:00
|
|
|
* Odissey.
|
2017-06-20 13:33:20 +00:00
|
|
|
*
|
2017-07-05 12:42:49 +00:00
|
|
|
* Advanced PostgreSQL connection pooler.
|
2017-06-20 13:33:20 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <inttypes.h>
|
|
|
|
#include <time.h>
|
2017-08-10 12:55:12 +00:00
|
|
|
#include <signal.h>
|
2017-06-20 13:33:20 +00:00
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
2017-08-10 12:55:12 +00:00
|
|
|
#include <sys/time.h>
|
2017-06-20 13:33:20 +00:00
|
|
|
#include <netinet/tcp.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
|
|
|
|
#include <machinarium.h>
|
|
|
|
#include <shapito.h>
|
|
|
|
|
2017-07-05 12:15:17 +00:00
|
|
|
#include "sources/macro.h"
|
2017-08-08 13:50:50 +00:00
|
|
|
#include "sources/atomic.h"
|
2017-11-27 12:54:16 +00:00
|
|
|
#include "sources/util.h"
|
2017-07-05 12:15:17 +00:00
|
|
|
#include "sources/pid.h"
|
|
|
|
#include "sources/id.h"
|
2017-07-26 14:05:29 +00:00
|
|
|
#include "sources/logger.h"
|
2017-06-20 13:33:20 +00:00
|
|
|
|
|
|
|
void od_idmgr_init(od_idmgr_t *mgr)
|
|
|
|
{
|
2017-11-21 14:30:32 +00:00
|
|
|
memset(&mgr->rand_state, 0, sizeof(mgr->rand_state));
|
2017-06-20 13:33:20 +00:00
|
|
|
}
|
|
|
|
|
2017-06-20 15:33:32 +00:00
|
|
|
int od_idmgr_seed(od_idmgr_t *mgr)
|
2017-06-20 13:33:20 +00:00
|
|
|
{
|
2017-11-21 14:30:32 +00:00
|
|
|
struct timeval tv;
|
|
|
|
gettimeofday(&tv, NULL);
|
|
|
|
|
|
|
|
long int rand_seed_2 = 0;
|
|
|
|
long int rand_seed;
|
|
|
|
rand_seed = getpid() ^ getuid() ^ tv.tv_sec ^ tv.tv_usec;
|
|
|
|
|
2017-06-20 13:33:20 +00:00
|
|
|
int fd;
|
2017-11-21 14:30:32 +00:00
|
|
|
fd = open("/dev/random", O_RDONLY);
|
2017-06-20 13:33:20 +00:00
|
|
|
if (fd == -1)
|
2017-11-21 14:30:32 +00:00
|
|
|
fd = open("/dev/urandom", O_RDONLY);
|
|
|
|
if (fd != -1) {
|
|
|
|
read(fd, &rand_seed_2, sizeof(rand_seed_2));
|
|
|
|
close(fd);
|
2017-06-20 13:33:20 +00:00
|
|
|
}
|
2017-11-21 14:30:32 +00:00
|
|
|
|
|
|
|
rand_seed ^= rand_seed_2;
|
|
|
|
srand48_r(rand_seed, &mgr->rand_state);
|
2017-06-20 13:33:20 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-07-26 14:05:29 +00:00
|
|
|
void od_idmgr_generate(od_idmgr_t *mgr, od_id_t *id, char *prefix)
|
2017-06-20 13:33:20 +00:00
|
|
|
{
|
2017-11-21 14:30:32 +00:00
|
|
|
long int a, b;
|
|
|
|
lrand48_r(&mgr->rand_state, &a);
|
|
|
|
lrand48_r(&mgr->rand_state, &b);
|
2017-06-20 13:33:20 +00:00
|
|
|
|
2017-11-21 14:30:32 +00:00
|
|
|
char seed[OD_ID_SEEDMAX];
|
|
|
|
memcpy(seed + 0, &a, 4);
|
|
|
|
memcpy(seed + 4, &b, 2);
|
2017-06-20 15:33:32 +00:00
|
|
|
|
2017-11-21 14:30:32 +00:00
|
|
|
id->id_prefix = prefix;
|
2017-11-24 12:19:14 +00:00
|
|
|
id->id_a = a;
|
|
|
|
id->id_b = b;
|
|
|
|
|
2017-06-20 15:33:32 +00:00
|
|
|
static const char *hex = "0123456789abcdef";
|
|
|
|
int q, w;
|
|
|
|
for (q = 0, w = 0; q < OD_ID_SEEDMAX; q++) {
|
2017-11-21 14:30:32 +00:00
|
|
|
id->id[w++] = hex[(seed[q] >> 4) & 0x0F];
|
|
|
|
id->id[w++] = hex[(seed[q] ) & 0x0F];
|
2017-06-20 15:33:32 +00:00
|
|
|
}
|
|
|
|
assert(w == (OD_ID_SEEDMAX * 2));
|
2017-06-20 13:33:20 +00:00
|
|
|
}
|