odyssey/sources/id.c

127 lines
2.5 KiB
C
Raw Normal View History

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 <signal.h>
#include <time.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <netinet/tcp.h>
#include <netdb.h>
#include <machinarium.h>
#include <shapito.h>
#include "sources/macro.h"
#include "sources/pid.h"
#include "sources/id.h"
#include "sources/syslog.h"
#include "sources/log.h"
2017-06-20 13:33:20 +00:00
void od_idmgr_init(od_idmgr_t *mgr)
{
memset(mgr->seed, 0, sizeof(mgr->seed));
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
}
int od_idmgr_seed(od_idmgr_t *mgr)
2017-06-20 13:33:20 +00:00
{
struct timeval tv;
gettimeofday(&tv, 0);
srand((mgr->pid << 16) ^ mgr->uid ^ tv.tv_sec ^ tv.tv_usec);
int i = 0;
for (; i < OD_ID_SEEDMAX; i++)
2017-06-20 13:33:20 +00:00
mgr->seed[i] = (rand() >> 7) & 0xFF;
int fd;
fd = open("/dev/urandom", O_RDONLY);
if (fd == -1)
fd = open("/dev/random", O_RDONLY);
if (fd == -1)
2017-06-20 13:33:20 +00:00
return -1;
char seed[OD_ID_SEEDMAX];
2017-06-20 13:33:20 +00:00
int seed_read = 0;
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;
}
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;
}
void od_idmgr_generate(od_idmgr_t *mgr, od_id_t *id)
{
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;
uint32_t seq;
2017-06-20 13:33:20 +00:00
seq = ++mgr->seq;
seq ^= t.tv_nsec;
seq ^= machine_self();
seq ^= rand();
mgr->seed[0] ^= seq ^ second;
mgr->seed[1] ^= seq ^ minute;
mgr->seed[2] ^= seq ^ (hour + day + month);
mgr->seed[3] ^= ((uint8_t*)&seq)[0] ^ ((uint8_t*)&seq)[1];
mgr->seed[4] ^= ((uint8_t*)&seq)[2] ^ ((uint8_t*)&seq)[3];
mgr->seed[5] ^= seq ^ (uintptr_t)id;
mgr->seed[6] ^= seq ^ (mgr->pid + mgr->uid);
mgr->seed[7] ^= seq;
uint8_t *dest = &id->id[0];
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
}