2017-05-25 12:01:56 +00:00
|
|
|
|
|
|
|
/*
|
2017-06-07 11:50:58 +00:00
|
|
|
* ODISSEY.
|
2017-05-25 12:01:56 +00:00
|
|
|
*
|
|
|
|
* PostgreSQL connection pooler and request router.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2017-05-31 15:47:15 +00:00
|
|
|
#include <inttypes.h>
|
2017-05-25 12:01:56 +00:00
|
|
|
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
|
|
|
#include <machinarium.h>
|
2017-06-07 11:50:58 +00:00
|
|
|
#include <shapito.h>
|
2017-05-25 12:01:56 +00:00
|
|
|
|
|
|
|
#include "od_macro.h"
|
|
|
|
#include "od_pid.h"
|
2017-06-20 15:39:52 +00:00
|
|
|
#include "od_id.h"
|
2017-05-25 12:01:56 +00:00
|
|
|
#include "od_syslog.h"
|
|
|
|
#include "od_log.h"
|
|
|
|
#include "od_io.h"
|
|
|
|
|
2017-06-13 11:57:54 +00:00
|
|
|
int od_read(machine_io_t *io, so_stream_t *stream, int time_ms)
|
2017-05-25 12:01:56 +00:00
|
|
|
{
|
|
|
|
uint32_t request_start = so_stream_used(stream);
|
|
|
|
uint32_t request_size = 0;
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
uint8_t *request_data = stream->s + request_start;
|
|
|
|
uint32_t len;
|
|
|
|
int to_read;
|
|
|
|
to_read = so_read(&len, &request_data, &request_size);
|
|
|
|
if (to_read == 0)
|
|
|
|
break;
|
|
|
|
if (to_read == -1)
|
|
|
|
return -1;
|
|
|
|
int rc = so_stream_ensure(stream, to_read);
|
|
|
|
if (rc == -1)
|
|
|
|
return -1;
|
|
|
|
rc = machine_read(io, (char*)stream->p, to_read, time_ms);
|
2017-06-14 12:35:04 +00:00
|
|
|
if (rc == -1)
|
2017-05-25 12:01:56 +00:00
|
|
|
return -1;
|
|
|
|
so_stream_advance(stream, to_read);
|
|
|
|
request_size += to_read;
|
|
|
|
}
|
|
|
|
return request_start;
|
|
|
|
}
|
|
|
|
|
2017-06-13 11:57:54 +00:00
|
|
|
int od_write(machine_io_t *io, so_stream_t *stream)
|
2017-05-25 12:01:56 +00:00
|
|
|
{
|
|
|
|
int rc;
|
2017-06-14 12:35:04 +00:00
|
|
|
rc = machine_write(io, (char*)stream->s, so_stream_used(stream),
|
2017-05-25 12:01:56 +00:00
|
|
|
UINT32_MAX);
|
2017-06-14 12:35:04 +00:00
|
|
|
return rc;
|
2017-05-25 12:01:56 +00:00
|
|
|
}
|
|
|
|
|
2017-06-19 14:39:12 +00:00
|
|
|
int od_getsockaddrname(struct sockaddr *sa, char *buf, int size)
|
2017-05-25 12:01:56 +00:00
|
|
|
{
|
|
|
|
char addr[128];
|
2017-06-19 14:39:12 +00:00
|
|
|
if (sa->sa_family == AF_INET) {
|
|
|
|
struct sockaddr_in *sin = (struct sockaddr_in*)sa;
|
|
|
|
inet_ntop(sa->sa_family, &sin->sin_addr, addr, sizeof(addr));
|
2017-05-25 12:01:56 +00:00
|
|
|
snprintf(buf, size, "%s:%d", addr, ntohs(sin->sin_port));
|
|
|
|
return 0;
|
|
|
|
}
|
2017-06-19 14:39:12 +00:00
|
|
|
if (sa->sa_family == AF_INET6) {
|
|
|
|
struct sockaddr_in6 *sin = (struct sockaddr_in6*)sa;
|
|
|
|
inet_ntop(sa->sa_family, &sin->sin6_addr, addr, sizeof(addr));
|
2017-05-25 12:01:56 +00:00
|
|
|
snprintf(buf, size, "[%s]:%d", addr, ntohs(sin->sin6_port));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
snprintf(buf, size, "unknown");
|
|
|
|
return -1;
|
|
|
|
}
|
2017-06-19 14:39:12 +00:00
|
|
|
|
|
|
|
int od_getaddrname(struct addrinfo *ai, char *buf, int size)
|
|
|
|
{
|
|
|
|
return od_getsockaddrname(ai->ai_addr, buf, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
int od_getpeername(machine_io_t *io, char *buf, int size)
|
|
|
|
{
|
|
|
|
struct sockaddr_storage sa;
|
|
|
|
int salen = sizeof(sa);
|
|
|
|
int rc = machine_getpeername(io, (struct sockaddr*)&sa, &salen);
|
|
|
|
if (rc < 0) {
|
|
|
|
snprintf(buf, size, "unknown");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return od_getsockaddrname((struct sockaddr*)&sa, buf, size);
|
|
|
|
}
|