From e45340973a67d1c4dac91b5653400a49bb406c90 Mon Sep 17 00:00:00 2001 From: Ikey Doherty Date: Sun, 22 Oct 2017 02:39:47 +0100 Subject: [PATCH] Fix logging, start fleshing out redirect profile API Signed-off-by: Ikey Doherty --- src/common/log.c | 15 +++++++++++---- src/common/log.h | 5 +++++ src/intercept/main.c | 1 + src/redirect/main.c | 1 + src/redirect/profile.c | 22 ++++++++++++++++++++++ src/redirect/redirect.h | 8 ++++++++ 6 files changed, 48 insertions(+), 4 deletions(-) diff --git a/src/common/log.c b/src/common/log.c index cb84b69..2f69af7 100644 --- a/src/common/log.c +++ b/src/common/log.c @@ -19,6 +19,13 @@ #include "log.h" #include "nica/util.h" +static const char *_log_id = "__init__"; + +void lsi_log_set_id(const char *id) +{ + _log_id = id; +} + void lsi_log_debug(const char *format, ...) { if (!getenv("LSI_DEBUG")) { @@ -35,7 +42,7 @@ void lsi_log_debug(const char *format, ...) goto end; } - fprintf(stderr, "\033[32;1m[lsi:debug]\033[0m %s\n", p); + fprintf(stderr, "\033[32;1m[lsi:%s]\033[0m %s\n", _log_id, p); end: va_end(va); @@ -53,7 +60,7 @@ void lsi_log_info(const char *format, ...) goto end; } - fprintf(stderr, "\033[34;1m[lsi:info]\033[0m %s\n", p); + fprintf(stderr, "\033[34;1m[lsi:%s]\033[0m %s\n", _log_id, p); end: va_end(va); @@ -71,7 +78,7 @@ void lsi_log_warn(const char *format, ...) goto end; } - fprintf(stderr, "\033[33;1m[lsi:warn]\033[0m %s\n", p); + fprintf(stderr, "\033[33;1m[lsi:%s]\033[0m %s\n", _log_id, p); end: va_end(va); @@ -89,7 +96,7 @@ void lsi_log_error(const char *format, ...) goto end; } - fprintf(stderr, "\033[31;1m[lsi:error]\033[0m %s\n", p); + fprintf(stderr, "\033[31;1m[lsi:%s]\033[0m %s\n", _log_id, p); end: va_end(va); diff --git a/src/common/log.h b/src/common/log.h index 18fbcc1..fa71cbe 100644 --- a/src/common/log.h +++ b/src/common/log.h @@ -15,6 +15,11 @@ #include +/** + * Set the ID used in all log output + */ +void lsi_log_set_id(const char *id); + /** * Emit some debug information if LSI_DEBUG is set */ diff --git a/src/intercept/main.c b/src/intercept/main.c index 64cc792..c5b4a51 100644 --- a/src/intercept/main.c +++ b/src/intercept/main.c @@ -161,6 +161,7 @@ static void check_is_intercept_candidate(void) work_mode = INTERCEPT_MODE_VENDOR_OFFENDER; matched_process = "vendor_offender"; } + lsi_log_set_id(matched_process); } /** diff --git a/src/redirect/main.c b/src/redirect/main.c index 4e304f6..fb10bcd 100644 --- a/src/redirect/main.c +++ b/src/redirect/main.c @@ -153,6 +153,7 @@ __attribute__((constructor)) static void lsi_redirect_init(void) /* Temporary hack, we'll make this more generic later */ if (strcmp(process_name, "ShooterGame") == 0) { + lsi_log_set_id("ShooterGame"); lsi_profile = lsi_redirect_profile_new("ARK Survival Evolved"); } diff --git a/src/redirect/profile.c b/src/redirect/profile.c index ca38413..dd79c47 100644 --- a/src/redirect/profile.c +++ b/src/redirect/profile.c @@ -14,6 +14,7 @@ #include #include +#include "../common/log.h" #include "redirect.h" LsiRedirectProfile *lsi_redirect_profile_new(const char *name) @@ -41,6 +42,27 @@ void lsi_redirect_profile_free(LsiRedirectProfile *self) free(self); } +void lsi_redirect_profile_insert_rule(LsiRedirectProfile *self, LsiRedirect *redirect) +{ + LsiRedirectOperation op; + + switch (redirect->type) { + case LSI_REDIRECT_PATH: + op = LSI_OPERATION_OPEN; + break; + default: + lsi_log_error("Attempted insert of unknown rule into '%s'", self->name); + break; + } + + /* Set head or prepend the rule */ + if (self->op_table[op]) { + self->op_table[op] = redirect->next = self->op_table[op]; + } else { + self->op_table[op] = redirect; + } +} + /* * Editor modelines - https://www.wireshark.org/tools/modelines.html * diff --git a/src/redirect/redirect.h b/src/redirect/redirect.h index e61e286..aa4a62d 100644 --- a/src/redirect/redirect.h +++ b/src/redirect/redirect.h @@ -71,6 +71,14 @@ LsiRedirectProfile *lsi_redirect_profile_new(const char *name); */ void lsi_redirect_profile_free(LsiRedirectProfile *profile); +/** + * Attempt insert of a redirect rule into this profile + * + * @param profile Pointer to a valid LsiRedirectProfile + * @param redirect Pointer to an allocated LsiRedirect + */ +void lsi_redirect_profile_insert_rule(LsiRedirectProfile *self, LsiRedirect *redirect); + /* * Editor modelines - https://www.wireshark.org/tools/modelines.html *