Compare commits

...

2 Commits

Author SHA1 Message Date
9c74ca6396 Remove stale comment 2017-11-27 14:57:08 +03:30
e0b478612e add support for inet typer (only AF_INET4)
Also change -D output format to CSV (from COPY statement) and send it to
STDERR instead of STDOUT for separation
2017-11-27 14:53:51 +03:30

View File

@ -9,8 +9,11 @@
#include <ctype.h> #include <ctype.h>
#include <stdio.h> #include <stdio.h>
#include <assert.h> #include <assert.h>
#include <sys/socket.h>
#define ATTRTYPES_STR_MAX_LEN (1024-1) #define ATTRTYPES_STR_MAX_LEN (1024-1)
#define PGSQL_AF_INET (AF_INET + 0)
#define PGSQL_AF_INET6 (AF_INET + 1)
typedef int (*decode_callback_t)(const char* buffer, unsigned int buff_size, typedef int (*decode_callback_t)(const char* buffer, unsigned int buff_size,
unsigned int* out_size); unsigned int* out_size);
@ -60,6 +63,9 @@ decode_char(const char* buffer, unsigned int buff_size, unsigned int* out_size);
static int static int
decode_name(const char* buffer, unsigned int buff_size, unsigned int* out_size); decode_name(const char* buffer, unsigned int buff_size, unsigned int* out_size);
static int
decode_inet(const char* buffer, unsigned int buff_size, unsigned int* out_size);
static int static int
decode_ignore(const char* buffer, unsigned int buff_size, unsigned int* out_size); decode_ignore(const char* buffer, unsigned int buff_size, unsigned int* out_size);
@ -93,6 +99,7 @@ static ParseCallbackTableItem callback_table[] = {
{ "macaddr", &decode_macaddr }, { "macaddr", &decode_macaddr },
{ "name", &decode_name }, { "name", &decode_name },
{ "char", &decode_char }, { "char", &decode_char },
{ "inet", &decode_inet },
{ "~", &decode_ignore }, { "~", &decode_ignore },
/* internally all string types are stored the same way */ /* internally all string types are stored the same way */
@ -147,7 +154,7 @@ CopyAppend(const char* str)
return; return;
if(copyString.data[0] != '\0') if(copyString.data[0] != '\0')
appendStringInfoString(&copyString, "\t"); appendStringInfoString(&copyString, ",");
appendStringInfoString(&copyString, str); appendStringInfoString(&copyString, str);
} }
@ -257,7 +264,7 @@ CopyFlush(void)
/* Make sure init is done */ /* Make sure init is done */
CopyAppend(NULL); CopyAppend(NULL);
printf("COPY: %s\n", copyString.data); fprintf(stderr, "%s\n", copyString.data);
CopyClear(); CopyClear();
} }
@ -658,6 +665,38 @@ decode_macaddr(const char* buffer, unsigned int buff_size, unsigned int* out_siz
return 0; return 0;
} }
/* Decode a inet type */
static int
decode_inet(const char* buffer, unsigned int buff_size, unsigned int* out_size)
{
struct __attribute__((packed)) inet_t // FIXME: Only AF_INET is supported for now
{
unsigned char varlen;
unsigned char family;
unsigned char bits;
unsigned char ipaddr[4];
} inet;
if(buff_size < sizeof(inet))
return -2;
memcpy(&inet, buffer, sizeof(inet));
if (inet.family == PGSQL_AF_INET) {
CopyAppendFmt("%d.%d.%d.%d",
inet.ipaddr[0], inet.ipaddr[1], inet.ipaddr[2], inet.ipaddr[3]
);
*out_size = sizeof(inet);
return 0;
} else { // This basically dumps the stuff, now decodes them (perhaps AF_INET6)
CopyAppendFmt("[%d.%d.%d.%d.%d.%d.%d]",
inet.varlen, inet.family, inet.bits,
inet.ipaddr[0], inet.ipaddr[1], inet.ipaddr[2], inet.ipaddr[3]
);
*out_size = sizeof(inet);
}
return 0;
}
/* Decode a bool type */ /* Decode a bool type */
static int static int
decode_bool(const char* buffer, unsigned int buff_size, unsigned int* out_size) decode_bool(const char* buffer, unsigned int buff_size, unsigned int* out_size)