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-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)
|
|
|
|
{
|
|
|
|
memset(mgr->seed, 0, sizeof(mgr->seed));
|
2017-11-20 13:05:47 +00:00
|
|
|
mgr->rand_state = 0;
|
2017-06-20 13:33:20 +00:00
|
|
|
mgr->seq = 0;
|
2017-06-20 13:35:59 +00:00
|
|
|
mgr->uid = getuid();
|
|
|
|
mgr->pid = getpid();
|
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
|
|
|
{
|
|
|
|
struct timeval tv;
|
|
|
|
gettimeofday(&tv, 0);
|
2017-11-20 13:05:47 +00:00
|
|
|
mgr->rand_state = (mgr->pid << 16) ^ mgr->uid ^ tv.tv_sec ^ tv.tv_usec;
|
2017-06-20 13:33:20 +00:00
|
|
|
int i = 0;
|
2017-06-20 15:33:32 +00:00
|
|
|
for (; i < OD_ID_SEEDMAX; i++)
|
2017-11-20 13:05:47 +00:00
|
|
|
mgr->seed[i] = (rand_r(&mgr->rand_state) >> 7) & 0xFF;
|
2017-06-20 13:33:20 +00:00
|
|
|
int fd;
|
|
|
|
fd = open("/dev/urandom", O_RDONLY);
|
|
|
|
if (fd == -1)
|
|
|
|
fd = open("/dev/random", O_RDONLY);
|
2017-06-20 15:33:32 +00:00
|
|
|
if (fd == -1)
|
2017-06-20 13:33:20 +00:00
|
|
|
return -1;
|
2017-06-20 15:33:32 +00:00
|
|
|
char seed[OD_ID_SEEDMAX];
|
2017-06-20 13:33:20 +00:00
|
|
|
int seed_read = 0;
|
2017-06-20 15:33:32 +00:00
|
|
|
while (seed_read <= OD_ID_SEEDMAX) {
|
2017-06-20 13:33:20 +00:00
|
|
|
int rc;
|
|
|
|
rc = read(fd, seed, sizeof(seed));
|
|
|
|
if (rc == -1) {
|
|
|
|
if (errno == EAGAIN || errno == EINTR)
|
|
|
|
continue;
|
|
|
|
close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
seed_read += rc;
|
|
|
|
}
|
2017-06-20 15:33:32 +00:00
|
|
|
for (i = 0; i < OD_ID_SEEDMAX; i++)
|
2017-06-20 13:33:20 +00:00
|
|
|
mgr->seed[i] ^= seed[i];
|
|
|
|
close(fd);
|
|
|
|
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-07-26 14:05:29 +00:00
|
|
|
id->id_prefix = prefix;
|
|
|
|
|
2017-06-20 13:33:20 +00:00
|
|
|
struct timespec t;
|
|
|
|
clock_gettime(CLOCK_MONOTONIC, &t);
|
|
|
|
|
|
|
|
time_t current_time = t.tv_sec;
|
|
|
|
|
2017-06-20 13:35:59 +00:00
|
|
|
uint8_t second;
|
2017-06-20 13:33:20 +00:00
|
|
|
second = current_time % 60;
|
|
|
|
current_time /= 60;
|
|
|
|
|
2017-06-20 13:35:59 +00:00
|
|
|
uint8_t minute;
|
2017-06-20 13:33:20 +00:00
|
|
|
minute = current_time % 60;
|
|
|
|
current_time /= 60;
|
|
|
|
|
2017-06-20 13:35:59 +00:00
|
|
|
uint8_t hour;
|
2017-06-20 13:33:20 +00:00
|
|
|
hour = current_time % 24;
|
|
|
|
current_time /= 60;
|
|
|
|
|
2017-06-20 13:35:59 +00:00
|
|
|
uint8_t day;
|
2017-06-20 13:33:20 +00:00
|
|
|
day = current_time % 30;
|
|
|
|
current_time /= 60;
|
|
|
|
|
2017-06-20 13:35:59 +00:00
|
|
|
uint8_t month;
|
2017-06-20 13:33:20 +00:00
|
|
|
month = current_time % 12;
|
|
|
|
current_time /= 60;
|
|
|
|
|
2017-06-20 15:33:32 +00:00
|
|
|
uint32_t seq;
|
2017-06-20 13:33:20 +00:00
|
|
|
seq = ++mgr->seq;
|
|
|
|
seq ^= t.tv_nsec;
|
2017-06-20 15:33:32 +00:00
|
|
|
seq ^= machine_self();
|
2017-11-20 13:05:47 +00:00
|
|
|
seq ^= rand_r(&mgr->rand_state);
|
2017-06-20 15:33:32 +00:00
|
|
|
|
|
|
|
mgr->seed[0] ^= seq ^ second;
|
|
|
|
mgr->seed[1] ^= seq ^ minute;
|
|
|
|
mgr->seed[2] ^= seq ^ (hour + day + month);
|
2017-08-17 14:13:31 +00:00
|
|
|
mgr->seed[3] ^= ((uint8_t*)&seq)[0] ^ ((uint8_t*)&seq)[1] ^ mgr->pid;
|
|
|
|
mgr->seed[4] ^= ((uint8_t*)&seq)[2] ^ ((uint8_t*)&seq)[3] ^ mgr->uid;
|
2017-06-20 15:33:32 +00:00
|
|
|
mgr->seed[5] ^= seq ^ (uintptr_t)id;
|
|
|
|
|
2017-07-26 14:05:29 +00:00
|
|
|
char *dest = id->id;
|
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++) {
|
|
|
|
dest[w++] = hex[(mgr->seed[q] >> 4) & 0x0F];
|
|
|
|
dest[w++] = hex[(mgr->seed[q] ) & 0x0F];
|
|
|
|
}
|
|
|
|
assert(w == (OD_ID_SEEDMAX * 2));
|
2017-06-20 13:33:20 +00:00
|
|
|
}
|