use current year

This commit is contained in:
dave 2018-07-04 17:44:29 -07:00
parent bb3e5c75ed
commit a1cbe3ca65
4 changed files with 56 additions and 2 deletions

View File

@ -9,6 +9,7 @@
#include <signal.h>
#include "helpers.h"
#include "sysparser.h"
#include <time.h>
#include <json-c/json.h>
@ -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);

View File

@ -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);

View File

@ -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;
}

View File

@ -1,7 +1,27 @@
#include <stdio.h>
#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);