odyssey/sources/io.c

126 lines
3.1 KiB
C
Raw Normal View History

/*
2017-07-05 12:42:49 +00:00
* Odissey.
*
2017-07-05 12:42:49 +00:00
* Advanced PostgreSQL connection pooler.
*/
#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>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <machinarium.h>
2017-06-07 11:50:58 +00:00
#include <shapito.h>
#include "sources/macro.h"
#include "sources/util.h"
#include "sources/pid.h"
#include "sources/id.h"
2017-07-26 14:05:29 +00:00
#include "sources/logger.h"
#include "sources/io.h"
2018-01-26 13:50:17 +00:00
int od_read(machine_io_t *io, shapito_stream_t *stream, uint32_t time_ms)
{
2017-07-06 13:36:14 +00:00
uint32_t request_start = shapito_stream_used(stream);
uint32_t request_size = 0;
for (;;)
{
char *request_data = stream->start + request_start;
uint32_t len;
int to_read;
2017-07-06 13:36:14 +00:00
to_read = shapito_read(&len, &request_data, &request_size);
if (to_read == 0)
break;
if (to_read == -1)
return -1;
2017-07-06 13:36:14 +00:00
int rc = shapito_stream_ensure(stream, to_read);
if (rc == -1)
return -1;
rc = machine_read(io, stream->pos, to_read, time_ms);
2017-06-14 12:35:04 +00:00
if (rc == -1)
return -1;
2017-07-06 13:36:14 +00:00
shapito_stream_advance(stream, to_read);
request_size += to_read;
}
return request_start;
}
2017-07-06 13:36:14 +00:00
int od_write(machine_io_t *io, shapito_stream_t *stream)
{
int rc;
2017-07-06 13:36:14 +00:00
rc = machine_write(io, stream->start, shapito_stream_used(stream),
UINT32_MAX);
2017-06-14 12:35:04 +00:00
return rc;
}
static int
od_getsockaddrname(struct sockaddr *sa, char *buf, int size,
int add_addr,
int add_port)
{
char addr[128];
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));
if (add_addr && add_port)
od_snprintf(buf, size, "%s:%d", addr, ntohs(sin->sin_port));
else
if (add_addr)
od_snprintf(buf, size, "%s", addr);
else
if (add_port)
od_snprintf(buf, size, "%d", ntohs(sin->sin_port));
return 0;
}
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));
if (add_addr && add_port)
od_snprintf(buf, size, "[%s]:%d", addr, ntohs(sin->sin6_port));
else
if (add_addr)
od_snprintf(buf, size, "%s", addr);
else
if (add_port)
od_snprintf(buf, size, "%d", ntohs(sin->sin6_port));
return 0;
}
od_snprintf(buf, size, "%s", "");
return -1;
}
int od_getaddrname(struct addrinfo *ai, char *buf, int size, int add_addr, int add_port)
{
return od_getsockaddrname(ai->ai_addr, buf, size, add_addr, add_port);
}
int od_getpeername(machine_io_t *io, char *buf, int size, int add_addr, int add_port)
{
struct sockaddr_storage sa;
int salen = sizeof(sa);
int rc = machine_getpeername(io, (struct sockaddr*)&sa, &salen);
if (rc < 0) {
od_snprintf(buf, size, "%s", "");
return -1;
}
return od_getsockaddrname((struct sockaddr*)&sa, buf, size, add_addr, add_port);
}
int od_getsockname(machine_io_t *io, char *buf, int size, int add_addr, int add_port)
{
struct sockaddr_storage sa;
int salen = sizeof(sa);
int rc = machine_getsockname(io, (struct sockaddr*)&sa, &salen);
if (rc < 0) {
od_snprintf(buf, size, "%s", "");
return -1;
}
return od_getsockaddrname((struct sockaddr*)&sa, buf, size, add_addr, add_port);
}