From e0b478612e43d09d7f7af015ca1b56f400b7842c Mon Sep 17 00:00:00 2001 From: Babak Farrokhi Date: Mon, 27 Nov 2017 14:53:51 +0330 Subject: [PATCH] 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 --- decode.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 50 insertions(+), 2 deletions(-) diff --git a/decode.c b/decode.c index 7afa273..056cd4f 100644 --- a/decode.c +++ b/decode.c @@ -9,8 +9,11 @@ #include #include #include +#include #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, unsigned int* out_size); @@ -60,6 +63,9 @@ decode_char(const char* buffer, unsigned int buff_size, unsigned int* out_size); static int 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 decode_ignore(const char* buffer, unsigned int buff_size, unsigned int* out_size); @@ -93,6 +99,7 @@ static ParseCallbackTableItem callback_table[] = { { "macaddr", &decode_macaddr }, { "name", &decode_name }, { "char", &decode_char }, + { "inet", &decode_inet }, { "~", &decode_ignore }, /* internally all string types are stored the same way */ @@ -147,7 +154,7 @@ CopyAppend(const char* str) return; if(copyString.data[0] != '\0') - appendStringInfoString(©String, "\t"); + appendStringInfoString(©String, ","); appendStringInfoString(©String, str); } @@ -257,7 +264,7 @@ CopyFlush(void) /* Make sure init is done */ CopyAppend(NULL); - printf("COPY: %s\n", copyString.data); + fprintf(stderr, "%s\n", copyString.data); CopyClear(); } @@ -658,6 +665,47 @@ decode_macaddr(const char* buffer, unsigned int buff_size, unsigned int* out_siz 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; +/* + const char* new_buffer = (const char*)TYPEALIGN(sizeof(int32), (uintptr_t)buffer); + unsigned int delta = (unsigned int)( (uintptr_t)new_buffer - (uintptr_t)buffer ); + + if(buff_size < delta) + return -1; + + buff_size -= delta; + buffer = new_buffer; +*/ + 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 */ static int decode_bool(const char* buffer, unsigned int buff_size, unsigned int* out_size)