From a1cbe3ca6505149b3167f2c2885fd6954947bf8f Mon Sep 17 00:00:00 2001 From: dave Date: Wed, 4 Jul 2018 17:44:29 -0700 Subject: [PATCH] use current year --- src/main.c | 22 ++++++++++++++++++++-- src/pfparser.h | 4 ++++ src/sysparser.c | 10 ++++++++++ src/sysparser.h | 22 ++++++++++++++++++++++ 4 files changed, 56 insertions(+), 2 deletions(-) diff --git a/src/main.c b/src/main.c index 53a49de..4efc659 100644 --- a/src/main.c +++ b/src/main.c @@ -9,6 +9,7 @@ #include #include "helpers.h" #include "sysparser.h" +#include #include @@ -66,6 +67,9 @@ int main(int argc, char** argv) { if (bind(sock_fd, (struct sockaddr*)&my_addr, sizeof(struct sockaddr_in)) < 0) panic("bind failed"); + time_t cur_t = {0}; + struct tm cur_time = {0}; + socklen_t addrlen = sizeof(struct sockaddr_in); char msg[4096]; while (running) { @@ -84,8 +88,8 @@ int main(int argc, char** argv) { dump it and wait until the next loop. if the next buffer is some portion of a too-long message, we can expect the various parsing below to fail.*/ - assert(addrlen == sizeof(struct sockaddr_in)); - // printf("\nGot message: %s\n", msg); + assert(addrlen == sizeof(struct sockaddr_in)); + /*printf("\nGot message: %s\n", msg);*/ /*TODO should we check that msg[size_recvd] == \0 ? printf("From host %s src port %d got message %.*s\n", @@ -117,7 +121,21 @@ int main(int argc, char** argv) { } else { // pfdata_print(&fwdata); + cur_t = time(NULL); + cur_time = *localtime(&cur_t); + + char date_formtted[32]; + sprintf(date_formtted, "%04d-%02d-%02dT%02d:%02d:%02dZ", + cur_time.tm_year + 1900, + month2num(result.date.month), + result.date.day, + result.date.hour, + result.date.minute, + result.date.second); + json_object* jobj = json_object_new_object(); + add_strfield(jobj, "date", date_formtted); + add_strfield(jobj, "app", result.application); pfdata_to_json(&fwdata, jobj); printf("%s\n",json_object_to_json_string(jobj)); json_object_put(jobj); diff --git a/src/pfparser.h b/src/pfparser.h index 6d32923..de5d42a 100644 --- a/src/pfparser.h +++ b/src/pfparser.h @@ -105,4 +105,8 @@ int pfdata_parse(char* message, pf_data* result); void pfdata_print(pf_data* data); +void add_intfield(json_object* obj, char* name, int value); + +void add_strfield(json_object* obj, char* name, char* value); + int pfdata_to_json(pf_data* data, json_object* obj); diff --git a/src/sysparser.c b/src/sysparser.c index 0bb1fa6..f7d4585 100644 --- a/src/sysparser.c +++ b/src/sysparser.c @@ -128,3 +128,13 @@ int sysmsg_parse(struct SysMessage* result, char* message) { return 0; } + + +int month2num(char* month) { + for(int i=1; i<=12; i++) { + if(strcmp(month, month2nummap[i]) == 0) { + return i; + } + } + return -1; +} diff --git a/src/sysparser.h b/src/sysparser.h index bac832a..a7fd9ba 100644 --- a/src/sysparser.h +++ b/src/sysparser.h @@ -1,7 +1,27 @@ +#include #include "pfparser.h" + #define DF_MONTH_LEN 9 + +const static char* month2nummap[] __attribute__ ((unused)) = + {[1] = "Jan", + [2] = "Feb", + [3] = "Mar", + [4] = "Apr", + [5] = "May", + [6] = "Jun", + [7] = "Jul", + [8] = "Aug", + [9] = "Sep", + [10] = "Oct", + [11] = "Nov", + [12] = "Dec"}; + + + + /*TODO numeric indicator for month?*/ struct Datefields { char month[DF_MONTH_LEN]; @@ -22,3 +42,5 @@ struct SysMessage { int sysmsg_parse(struct SysMessage* result, char* message); + +int month2num(char* month);