From fde7f41c89391a0d3ddebcbc7b5326c2a486a6ca Mon Sep 17 00:00:00 2001 From: Mosder Date: Tue, 9 Jun 2026 01:32:28 +0200 Subject: feat: added basic main.c and Makefile --- main.c | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 main.c (limited to 'main.c') diff --git a/main.c b/main.c new file mode 100644 index 0000000..90f7944 --- /dev/null +++ b/main.c @@ -0,0 +1,64 @@ +#include +#include + +// prints help +void print_help() { + printf("A bare-bones analyzer for the game Broken Ranks\n"); + printf("\nUsage:\n"); + printf(" branal - start live analysis\n"); + printf(" branal (l | live) - start live analysis (same as above)\n"); + printf(" branal (c | capture) [file] - start capturing packets to [file].\n"); + printf(" If [file] not given - captures to $HOME/.local/share/branal/captures\n"); + printf(" branal (f | file) - analyze the specified \n"); + printf(" branal (h | help) - display this help\n"); +} + +// starts live analyzer mode +void start_live_analyzer() { + // TODO: implement + printf("Not implemented\n"); +} + +// starts the only capturing packets mode +void start_capturing_packets() { + // TODO: implement + printf("Not implemented\n"); +} + +// analyzes the specified file +// params: +// - file_path - path to the .pcap file to be analyzed +void analyze_file(char *file_path) { + // TODO: implement + printf("Not implemented\n"); +} + +int main(int argc, char *argv[]) { + // TODO: create necessary directories + + // start live analysis if no arguments provided + if (argc < 2) { + start_live_analyzer(); + return 0; + } + + if (strcmp("l", argv[1]) == 0 || strcmp("live", argv[1]) == 0) { + start_live_analyzer(); + } + else if (strcmp("c", argv[1]) == 0 || strcmp("capture", argv[1]) == 0) { + start_capturing_packets(); + } + else if (strcmp("h", argv[1]) == 0 || strcmp("help", argv[1]) == 0) { + print_help(); + } + else if (argc >= 3 && (strcmp("f", argv[1]) == 0 || strcmp("file", argv[1]) == 0)) { + analyze_file(argv[2]); + } + else { + printf("Command not recognized\n"); + printf("Use \"branal (h | help)\" for help\n"); + return 1; + } + + return 0; +} -- cgit v1.2.3 From 070f39fc2b033bcf53797480c4b1ce27a0eb0f91 Mon Sep 17 00:00:00 2001 From: Mosder Date: Thu, 11 Jun 2026 17:26:52 +0200 Subject: feat: added utils/fs.h for filesystem operations and made a function to create necessary directories --- Makefile | 8 ++++-- analyzer/utils/fs.c | 43 +++++++++++++++++++++++++++++ analyzer/utils/fs.h | 31 +++++++++++++++++++++ main.c | 78 +++++++++++++++++++++++++++-------------------------- 4 files changed, 120 insertions(+), 40 deletions(-) create mode 100644 analyzer/utils/fs.c create mode 100644 analyzer/utils/fs.h (limited to 'main.c') diff --git a/Makefile b/Makefile index 78e0b31..00fcb55 100644 --- a/Makefile +++ b/Makefile @@ -4,5 +4,9 @@ global: compile local: compile mv branal ${HOME}/.local/bin -compile: - gcc main.c -o branal +compile: utils + gcc *.o main.c -o branal + rm *.o + +utils: + gcc -c analyzer/utils/fs.c diff --git a/analyzer/utils/fs.c b/analyzer/utils/fs.c new file mode 100644 index 0000000..76fb581 --- /dev/null +++ b/analyzer/utils/fs.c @@ -0,0 +1,43 @@ +#include "utils.h" + +#include +#include +#include + +void get_home_dir(char *home_dir) { + strcpy(home_dir, getenv("HOME")); +} + +void get_main_dir(char *main_dir) { + char home_dir[256]; + get_home_dir(home_dir); + sprintf(main_dir, "%s/.local/share/branal", home_dir); +} + +void get_capture_dir(char *capture_dir) { + char main_dir[256]; + get_main_dir(main_dir); + sprintf(capture_dir, "%s/captures", main_dir); +} + +void get_saves_dir(char *saves_dir) { + char main_dir[256]; + get_main_dir(main_dir); + sprintf(saves_dir, "%s/saves", main_dir); +} + +void create_necessary_directories() { + char main_dir[256], capture_dir[256], saves_dir[256]; + get_main_dir(main_dir); + get_capture_dir(capture_dir); + get_saves_dir(saves_dir); + mkdir(main_dir, 0755); + mkdir(capture_dir, 0755); + mkdir(saves_dir, 0755); +} + +void write_file(char *path, void *contents, int contents_len, int flags, mode_t permissions) { + int fd = open(path, flags, permissions); + write(fd, contents, contents_len); + close(fd); +} diff --git a/analyzer/utils/fs.h b/analyzer/utils/fs.h new file mode 100644 index 0000000..6335799 --- /dev/null +++ b/analyzer/utils/fs.h @@ -0,0 +1,31 @@ +// utils/fs.h +// functions for operating on the file system, i.e. creating directories, writing files, etc. + +#include + +// get main directory used by the program +// params: +// - main_dir - buffer to save the directory to +extern void get_main_dir(char *main_dir); + +// get directory to save the .pcap files to +// params: +// - capture_dir - buffer to save the directory to +extern void get_capture_dir(char *capture_dir); + +// get directory of save data of the program +// params: +// - saves_dir - buffer to save the directory to +extern void get_saves_dir(char *saves_dir); + +// create directories that will be used by the program +extern void create_necessary_directories(); + +// writes file to the specified path +// params: +// - path - path to write the file to +// - contents - contents of the file +// - contents_len - amount of bytes of contents to write +// - flags - flags to open the file with +// - permissions - permissions of the written file +extern void write_file(char *path, void *contents, int contents_len, int flags, mode_t permissions); diff --git a/main.c b/main.c index 90f7944..aa43e57 100644 --- a/main.c +++ b/main.c @@ -1,64 +1,66 @@ #include #include +#include "analyzer/utils/fs.h" + // prints help void print_help() { - printf("A bare-bones analyzer for the game Broken Ranks\n"); - printf("\nUsage:\n"); - printf(" branal - start live analysis\n"); - printf(" branal (l | live) - start live analysis (same as above)\n"); - printf(" branal (c | capture) [file] - start capturing packets to [file].\n"); - printf(" If [file] not given - captures to $HOME/.local/share/branal/captures\n"); - printf(" branal (f | file) - analyze the specified \n"); - printf(" branal (h | help) - display this help\n"); + printf("A bare-bones analyzer for the game Broken Ranks\n"); + printf("\nUsage:\n"); + printf(" branal - start live analysis\n"); + printf(" branal (l | live) - start live analysis (same as above)\n"); + printf(" branal (c | capture) [file] - start capturing packets to [file].\n"); + printf(" If [file] not given - captures to $HOME/.local/share/branal/captures\n"); + printf(" branal (f | file) - analyze the specified \n"); + printf(" branal (h | help) - display this help\n"); } // starts live analyzer mode void start_live_analyzer() { - // TODO: implement - printf("Not implemented\n"); + // TODO: implement + printf("Not implemented\n"); } // starts the only capturing packets mode void start_capturing_packets() { - // TODO: implement - printf("Not implemented\n"); + // TODO: implement + printf("Not implemented\n"); } // analyzes the specified file // params: // - file_path - path to the .pcap file to be analyzed void analyze_file(char *file_path) { - // TODO: implement - printf("Not implemented\n"); + // TODO: implement + printf("Not implemented\n"); } int main(int argc, char *argv[]) { - // TODO: create necessary directories + create_necessary_directories(); - // start live analysis if no arguments provided - if (argc < 2) { - start_live_analyzer(); - return 0; - } + // start live analysis if no arguments provided + if (argc < 2) { + start_live_analyzer(); + return 0; + } - if (strcmp("l", argv[1]) == 0 || strcmp("live", argv[1]) == 0) { - start_live_analyzer(); - } - else if (strcmp("c", argv[1]) == 0 || strcmp("capture", argv[1]) == 0) { - start_capturing_packets(); - } - else if (strcmp("h", argv[1]) == 0 || strcmp("help", argv[1]) == 0) { - print_help(); - } - else if (argc >= 3 && (strcmp("f", argv[1]) == 0 || strcmp("file", argv[1]) == 0)) { - analyze_file(argv[2]); - } - else { - printf("Command not recognized\n"); - printf("Use \"branal (h | help)\" for help\n"); - return 1; - } + if (strcmp("l", argv[1]) == 0 || strcmp("live", argv[1]) == 0) { + start_live_analyzer(); + } + else if (strcmp("c", argv[1]) == 0 || strcmp("capture", argv[1]) == 0) { + start_capturing_packets(); + } + else if (strcmp("h", argv[1]) == 0 || strcmp("help", argv[1]) == 0) { + print_help(); + } + else if (argc >= 3 && (strcmp("f", argv[1]) == 0 || strcmp("file", argv[1]) == 0)) { + analyze_file(argv[2]); + } + else { + printf("Command not recognized\n"); + printf("Use \"branal (h | help)\" for help\n"); + return 1; + } - return 0; + return 0; } -- cgit v1.2.3 From f0d9f0c2dcc2029a907deee7981e7e5230b6d77c Mon Sep 17 00:00:00 2001 From: Mosder Date: Sat, 13 Jun 2026 20:14:51 +0200 Subject: feat: added the simple capturing packets mode --- Makefile | 6 +++++- analyzer/capture.c | 28 ++++++++++++++++++++++++++++ analyzer/capture.h | 8 ++++++++ main.c | 9 ++------- 4 files changed, 43 insertions(+), 8 deletions(-) create mode 100644 analyzer/capture.c create mode 100644 analyzer/capture.h (limited to 'main.c') diff --git a/Makefile b/Makefile index 00fcb55..a85df64 100644 --- a/Makefile +++ b/Makefile @@ -4,9 +4,13 @@ global: compile local: compile mv branal ${HOME}/.local/bin -compile: utils +compile: utils capture gcc *.o main.c -o branal rm *.o utils: gcc -c analyzer/utils/fs.c + gcc -c analyzer/utils/time.c + +capture: + gcc -c analyzer/capture.c diff --git a/analyzer/capture.c b/analyzer/capture.c new file mode 100644 index 0000000..0a75e09 --- /dev/null +++ b/analyzer/capture.c @@ -0,0 +1,28 @@ +#include "capture.h" + +#include +#include + +#include "headers/interface.h" +#include "headers/server.h" +#include "utils/fs.h" +#include "utils/time.h" + +void capture_to_file(char *path) { + char command[128]; + sprintf(command, "tshark -i %s -f \"src host %s\" -w %s", INTERFACE, SERVER_IP, path); + system(command); +} + +void capturing_mode(char *path) { + if (path != NULL) { + capture_to_file(path); + return; + } + + char capture_dir[256], new_path[256], date_time[128]; + get_capture_dir(capture_dir); + get_date_time_string(date_time); + sprintf(new_path, "%s/capture-%s.pcap", capture_dir, date_time); + capture_to_file(new_path); +} diff --git a/analyzer/capture.h b/analyzer/capture.h new file mode 100644 index 0000000..648e5ad --- /dev/null +++ b/analyzer/capture.h @@ -0,0 +1,8 @@ +// capture.h +// functions for capturing packets using tshark + +// starts capturing mode +// params: +// - path - file path to capture the .pcap file to, +// if NULL, saves it to default capture directory as capture-_