From e02f15a212689957917150150b4bcba9c09a2235 Mon Sep 17 00:00:00 2001 From: dave Date: Thu, 19 Jul 2018 21:09:51 -0700 Subject: [PATCH] json config file --- sim/sim.py | 14 +++++++-- src/Makefile | 2 +- src/config.c | 77 +++++++++++++++++++++++++++++++++++++++++++++++++ src/config.h | 9 ++++++ src/config.json | 4 +++ src/gdb.sh | 2 +- src/geo.c | 2 +- src/helpers.c | 5 ++++ src/helpers.h | 2 ++ src/main.c | 18 +++++++++--- src/server.c | 10 +++++-- src/server.h | 2 +- src/valgrind.sh | 2 +- 13 files changed, 135 insertions(+), 14 deletions(-) create mode 100644 src/config.c create mode 100644 src/config.h create mode 100644 src/config.json diff --git a/sim/sim.py b/sim/sim.py index e8248ac..2f88dfd 100755 --- a/sim/sim.py +++ b/sim/sim.py @@ -5,6 +5,7 @@ import os from time import sleep import argparse +from random import random DEST = "127.0.0.1" @@ -13,16 +14,25 @@ def main(): parser = argparse.ArgumentParser() parser.add_argument("file") parser.add_argument("port", type=int, default=4200) - parser.add_argument("rate", type=float, default=1) + parser.add_argument("rate", default=1) args = parser.parse_args() + sleep_times = [float(i) for i in args.rate.split("-")] + + def sleep_lenght(): + if len(sleep_times) == 2: + return (max(sleep_times) - min(sleep_times)) * random() + min(sleep_times) + else: + return sleep_times[0] + with open(os.path.join(os.path.dirname(args.file), args.file), "r") as f: lines = [line.rstrip().encode("UTF-8") for line in f] sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) while True: for line in lines: sock.sendto(line, (DEST, args.port)) - sleep(args.rate) + print(sleep_lenght()) + sleep(sleep_lenght()) if __name__ == '__main__': diff --git a/src/Makefile b/src/Makefile index d3c9dd6..794e6bd 100644 --- a/src/Makefile +++ b/src/Makefile @@ -5,7 +5,7 @@ LDFLAGS = -L $(LIBPATH) LDLIBS = -ljson-c -lcurl -lpthread -lGeoIP LDFLAGS += $(LDLIBS) CFLAGS_STATIC = $(CFLAGS) --static -OBJ=helpers.o pfparser.o sysparser.o msgbuffer.o geo.o elasticsearch.o server.o +OBJ=helpers.o pfparser.o sysparser.o msgbuffer.o geo.o elasticsearch.o server.o config.o TESTS=$(patsubst %.c,%.test,$(wildcard tests/*.c)) diff --git a/src/config.c b/src/config.c new file mode 100644 index 0000000..bedcb69 --- /dev/null +++ b/src/config.c @@ -0,0 +1,77 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "helpers.h" +#include "config.h" +#include "elasticsearch.h" + + +char* get_strfield(json_object* obj, char* key) { + json_object *o_value = json_object_object_get(obj, key); + if(o_value == NULL) + return NULL; + const char* value = json_object_get_string(o_value); + if(value == NULL) + return NULL; + char* dest = malloc(strlen(value) + 1); + strcpy(dest, value); + return dest; +} + + +int get_intfield(json_object* obj, char* key) { + json_object *o_value = json_object_object_get(obj, key); + if(o_value == NULL) + return 0; + return json_object_get_int(o_value); +} + + +struct config* config_load(char* conf_path) { + int fd; + if((fd = open(conf_path, O_RDONLY)) == -1) + panic("Config load failed"); + + struct stat fsize = {0}; + if(fstat(fd, &fsize) != 0) + die("Conf stat failed"); + + if(fsize.st_size == 0) + die("Empty config"); + + char* confdata = mmap(NULL, fsize.st_size, PROT_READ, MAP_SHARED, fd, 0); + if(confdata == MAP_FAILED) + panic("Failed to mmap conf"); + + json_object *j_conf; + if((j_conf = json_tokener_parse(confdata)) == NULL) + die("Json parse failed"); + + struct config* conf = calloc(1, sizeof(struct config)); + + if((conf->url = get_strfield(j_conf, "elasticsearch")) == NULL) + die("Config missing or invalid elasticsearch url"); + + if((conf->port = get_intfield(j_conf, "serverport")) == 0) + die("Config missing or invalid serverport number"); + + json_object_put(j_conf); + + if(munmap(confdata, fsize.st_size) != 0) + panic("munmap failed"); + + close(fd); + return conf; +} + + +void config_free(struct config* conf) { + free(conf->url); + free(conf); +} diff --git a/src/config.h b/src/config.h new file mode 100644 index 0000000..f222cd1 --- /dev/null +++ b/src/config.h @@ -0,0 +1,9 @@ +struct config { + char* url; + int port; +}; + + +struct config* config_load(char* conf_path); + +void config_free(struct config* conf); diff --git a/src/config.json b/src/config.json new file mode 100644 index 0000000..301f313 --- /dev/null +++ b/src/config.json @@ -0,0 +1,4 @@ +{ + "elasticsearch": "http://127.0.0.1:8299", + "serverport": 4200 +} diff --git a/src/gdb.sh b/src/gdb.sh index 178b266..16e25a8 100755 --- a/src/gdb.sh +++ b/src/gdb.sh @@ -1 +1 @@ -gdb -ex=r --args ./csyslog 4200 +gdb -ex=r --args ./csyslog config.json diff --git a/src/geo.c b/src/geo.c index e25519d..f62807e 100644 --- a/src/geo.c +++ b/src/geo.c @@ -11,7 +11,7 @@ void geo_init() { gi = GeoIP_open("GeoLiteCity.dat", GEOIP_INDEX_CACHE); gi6 = GeoIP_open("GeoLiteCityv6.dat", GEOIP_INDEX_CACHE); if (gi == NULL || gi6 == NULL) { - fprintf(stderr, "Error opening geoip databases\n"); + fprintf(stderr, "Fatal: could not open geoip databases\n"); exit(1); } } diff --git a/src/helpers.c b/src/helpers.c index 8f0c0ee..7cc8490 100644 --- a/src/helpers.c +++ b/src/helpers.c @@ -6,3 +6,8 @@ void panic(const char* s) { perror(s); exit(1); } + +void die(const char* s) { + fprintf(stderr, "%s\n", s); + exit(EXIT_FAILURE); +} diff --git a/src/helpers.h b/src/helpers.h index e056fa8..c4b87d8 100644 --- a/src/helpers.h +++ b/src/helpers.h @@ -7,3 +7,5 @@ http://www.decompile.com/cpp/faq/file_and_line_error_string.htm*/ #define STR(x) _STR(x) void panic(const char* s); + +void die(const char* s); diff --git a/src/main.c b/src/main.c index e514b92..ddcb585 100644 --- a/src/main.c +++ b/src/main.c @@ -2,17 +2,19 @@ #include #include #include +#include "config.h" #include "server.h" +#include "helpers.h" int main(int argc, char** argv) { if (argc != 2) { - fprintf(stderr, "usage: %s \n", argv[0]); + fprintf(stderr, "usage: %s \n", argv[0]); exit(1); } /*Parse port number to integer*/ - char* portend; + /*char* portend; unsigned int portl; portl = strtol(argv[1], &portend, 10); if (portend == NULL || portend == argv[1]) { @@ -20,8 +22,16 @@ int main(int argc, char** argv) { exit(1); } assert(portl < USHRT_MAX); - unsigned short port = (unsigned short)portl; + unsigned short port = (unsigned short)portl;*/ + + struct config* conf = config_load(argv[1]); + + printf("url: %s\n", conf->url); + printf("port: %d\n", conf->port); + + run_server(conf->port, conf->url); + + config_free(conf); - run_server(port); exit(EXIT_SUCCESS); } diff --git a/src/server.c b/src/server.c index d30276f..eefce11 100644 --- a/src/server.c +++ b/src/server.c @@ -29,6 +29,8 @@ pthread_mutex_t buflock; time_t cur_t = {0}; struct tm cur_time = {0}; +char* es_url = NULL; + void sig_handler(int signum) { printf("\nExiting on signal %s\n", strsignal(signum)); @@ -39,7 +41,7 @@ void sig_handler(int signum) { int submit_events(char* message) { - if(put_events(message, "http://192.168.1.120:8298") == 0) { + if(put_events(message, es_url) == 0) { return 0; } else { printf("Failed to post messages!\n"); @@ -214,11 +216,13 @@ int handle_message(char* msg) { /*UDP server bits mostly lifted from https://cs.nyu.edu/~mwalfish/classes/16sp/classnotes/handout01.pdf*/ -int run_server(int port) { - geo_init(); +int run_server(int port, char* url) { signal(SIGTERM, sig_handler); signal(SIGINT, sig_handler); + geo_init(); + es_url = url; + /*Create socket*/ if ((sock_fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) panic("socket"); diff --git a/src/server.h b/src/server.h index f1fa1bd..6d12ab0 100644 --- a/src/server.h +++ b/src/server.h @@ -1 +1 @@ -int run_server(int port); +int run_server(int port, char* url); diff --git a/src/valgrind.sh b/src/valgrind.sh index 7b6a317..27dcebb 100755 --- a/src/valgrind.sh +++ b/src/valgrind.sh @@ -1 +1 @@ -valgrind --leak-check=full --track-origins=yes -v ./csyslog 4200 +valgrind --leak-check=full --track-origins=yes -v ./csyslog config.json