From 50c26cda7bcde10f21d4d501ba4515cb73126b0b Mon Sep 17 00:00:00 2001 From: Mike Wang Date: Sun, 8 Jun 2025 15:28:47 +0800 Subject: [PATCH] feat: Enhance logging functionality --- include/logging.h | 6 ++++-- src/ipv4ipt.c | 11 ++++++----- src/ipv4nft.c | 7 ++++--- src/ipv4pkt.c | 3 +++ src/logging.c | 19 +++++++++++-------- src/mainfun.c | 16 ++++++++-------- src/nfqueue.c | 26 +++++++++++++------------- src/nfrules.c | 8 ++++---- 8 files changed, 53 insertions(+), 43 deletions(-) diff --git a/include/logging.h b/include/logging.h index f56d5c1..1b15002 100644 --- a/include/logging.h +++ b/include/logging.h @@ -20,7 +20,9 @@ #ifndef FH_LOGGING_H #define FH_LOGGING_H -#define E(...) fh_logger(__func__, __FILE__, __LINE__, __VA_ARGS__) +#define T(FUNC) (" at " #FUNC "()") +#define E(...) fh_logger(__func__, __FILE__, __LINE__, 0, __VA_ARGS__) +#define EE(...) fh_logger(__func__, __FILE__, __LINE__, 1, __VA_ARGS__) #define E_RAW(...) fh_logger_raw(__VA_ARGS__) #define E_INFO(...) \ if (!g_ctx.silent) { \ @@ -32,7 +34,7 @@ int fh_logger_setup(void); void fh_logger_cleanup(void); void fh_logger(const char *funcname, const char *filename, unsigned long line, - const char *fmt, ...); + int end, const char *fmt, ...); void fh_logger_raw(const char *fmt, ...); diff --git a/src/ipv4ipt.c b/src/ipv4ipt.c index e90eff3..d3e7e23 100644 --- a/src/ipv4ipt.c +++ b/src/ipv4ipt.c @@ -45,6 +45,7 @@ int fh_ipt4_flush(int auto_create) res = fh_execute_command(ipt_flush_cmd, 1, NULL); if (res < 0) { if (!auto_create) { + E(T(fh_execute_command)); return -1; } @@ -52,7 +53,7 @@ int fh_ipt4_flush(int auto_create) for (i = 0; i < cnt; i++) { res = fh_execute_command(ipt_create_cmds[i], 0, NULL); if (res < 0) { - E("ERROR: fh_execute_command()"); + E(T(fh_execute_command)); return -1; } } @@ -135,26 +136,26 @@ int fh_ipt4_add(void) res = snprintf(xmark_str, sizeof(xmark_str), "%" PRIu32 "/%" PRIu32, g_ctx.fwmark, g_ctx.fwmask); if (res < 0 || (size_t) res >= sizeof(xmark_str)) { - E("ERROR: snprintf()"); + E("ERROR: snprintf(): %s", "failure"); return -1; } res = snprintf(nfqnum_str, sizeof(nfqnum_str), "%" PRIu32, g_ctx.nfqnum); if (res < 0 || (size_t) res >= sizeof(nfqnum_str)) { - E("ERROR: snprintf()"); + E("ERROR: snprintf(): %s", "failure"); return -1; } res = snprintf(iface_str, sizeof(iface_str), "%s", g_ctx.iface); if (res < 0 || (size_t) res >= sizeof(iface_str)) { - E("ERROR: snprintf()"); + E("ERROR: snprintf(): %s", "failure"); return -1; } for (i = 0; i < ipt_cmds_cnt; i++) { res = fh_execute_command(ipt_cmds[i], 0, NULL); if (res < 0) { - E("ERROR: fh_execute_command()"); + E(T(fh_execute_command)); return -1; } } diff --git a/src/ipv4nft.c b/src/ipv4nft.c index 8f4f400..fd4d038 100644 --- a/src/ipv4nft.c +++ b/src/ipv4nft.c @@ -51,12 +51,13 @@ int fh_nft4_flush(int auto_create) res = fh_execute_command(nft_flush_cmd, 1, NULL); if (res < 0) { if (!auto_create) { + E(T(fh_execute_command)); return -1; } res = fh_execute_command(nft_cmd, 0, nft_create_conf); if (res < 0) { - E("ERROR: fh_execute_command()"); + E(T(fh_execute_command)); return -1; } } @@ -135,13 +136,13 @@ int fh_nft4_add(void) g_ctx.fwmask, g_ctx.fwmark, ~g_ctx.fwmask, g_ctx.fwmark, g_ctx.fwmask, g_ctx.fwmark, g_ctx.iface, g_ctx.nfqnum); if (res < 0 || (size_t) res >= sizeof(nft_conf_buff)) { - E("ERROR: snprintf()"); + E("ERROR: snprintf(): %s", "failure"); return -1; } res = fh_execute_command(nft_cmd, 1, nft_conf_buff); if (res < 0) { - E("ERROR: fh_execute_command()"); + E(T(fh_execute_command)); return -1; } diff --git a/src/ipv4pkt.c b/src/ipv4pkt.c index 5f965c4..8b603d4 100644 --- a/src/ipv4pkt.c +++ b/src/ipv4pkt.c @@ -20,6 +20,7 @@ #define _GNU_SOURCE #include "ipv4pkt.h" +#include #include #include #include @@ -29,6 +30,7 @@ #include #include "globvar.h" +#include "logging.h" int fh_pkt4_make(char *buffer, size_t buffer_size, uint32_t saddr_be, uint32_t daddr_be, uint16_t sport_be, uint16_t dport_be, @@ -42,6 +44,7 @@ int fh_pkt4_make(char *buffer, size_t buffer_size, uint32_t saddr_be, pkt_len = sizeof(*iph) + sizeof(*tcph) + tcp_payload_size; if (buffer_size < pkt_len + 1) { + E("ERROR: %s", strerror(ENOBUFS)); return -1; } diff --git a/src/logging.c b/src/logging.c index 06af7ee..bb52ba1 100644 --- a/src/logging.c +++ b/src/logging.c @@ -56,24 +56,27 @@ void fh_logger_cleanup(void) void fh_logger(const char *funcname, const char *filename, unsigned long line, - const char *fmt, ...) + int end, const char *fmt, ...) { va_list args; time_t t; - char *stime; + char time_buff[32]; + struct tm *tmi; t = time(NULL); - stime = ctime(&t); - if (stime) { - stime[strlen(stime) - 1] = '\0'; - fprintf(g_ctx.logfp, "%s ", stime); - } + tmi = localtime(&t); + strftime(time_buff, sizeof(time_buff), "%Y-%m-%d %H:%M:%S", tmi); - fprintf(g_ctx.logfp, "[%s() - %s:%lu] ", funcname, filename, line); + fprintf(g_ctx.logfp, "%19s [%13s:%03lu] ", time_buff, filename, line); va_start(args, fmt); vfprintf(g_ctx.logfp, fmt, args); va_end(args); fputc('\n', g_ctx.logfp); + + if (end) { + fprintf(g_ctx.logfp, "%19s [%13s:%03lu] at %s()\n", time_buff, + filename, line, funcname); + } fflush(g_ctx.logfp); } diff --git a/src/mainfun.c b/src/mainfun.c index e55acb2..01d2f57 100644 --- a/src/mainfun.c +++ b/src/mainfun.c @@ -185,7 +185,7 @@ int main(int argc, char *argv[]) if (g_ctx.killproc) { res = fh_logger_setup(); if (res < 0) { - E("ERROR: fh_logger_setup()"); + EE(T(fh_logger_setup)); return EXIT_FAILURE; } res = fh_kill_running(SIGTERM); @@ -231,7 +231,7 @@ int main(int argc, char *argv[]) res = fh_logger_setup(); if (res < 0) { - E("ERROR: fh_logger_setup()"); + EE(T(fh_logger_setup)); return EXIT_FAILURE; } @@ -239,31 +239,31 @@ int main(int argc, char *argv[]) res = fh_rawsock_setup(); if (res < 0) { - E("from fh_rawsock_setup()"); + EE(T(fh_rawsock_setup)); goto cleanup_logger; } res = fh_nfq_setup(); if (res < 0) { - E("ERROR: fh_nfq_setup()"); + EE(T(fh_nfq_setup)); goto cleanup_rawsock; } res = fh_nfrules_setup(); if (res < 0) { - E("ERROR: fh_nfrules_setup()"); + EE(T(fh_nfrules_setup)); goto cleanup_nfq; } res = fh_signal_setup(); if (res < 0) { - E("ERROR: fh_signal_setup()"); + EE(T(fh_signal_setup)); goto cleanup_nfrules; } res = setpriority(PRIO_PROCESS, getpid(), -20); if (res < 0) { - E("WARNING: setpriority(): %s", strerror(errno)); + EE("WARNING: setpriority(): %s", strerror(errno)); } E("listening on %s, netfilter queue number %" PRIu32 "...", g_ctx.iface, @@ -274,7 +274,7 @@ int main(int argc, char *argv[]) */ res = fh_nfq_loop(); if (res < 0) { - E("ERROR: fh_nfq_loop()"); + EE(T(fh_nfq_loop)); goto cleanup_nfrules; } diff --git a/src/nfqueue.c b/src/nfqueue.c index 5e97860..1716be3 100644 --- a/src/nfqueue.c +++ b/src/nfqueue.c @@ -56,7 +56,7 @@ static int send_ack(uint32_t saddr_be, uint32_t daddr_be, uint16_t sport_be, pkt_len = fh_pkt4_make(pkt_buff, sizeof(pkt_buff), saddr_be, daddr_be, sport_be, dport_be, seq_be, ackseq_be, 0, NULL, 0); if (pkt_len < 0) { - E("ERROR: fh_pkt4_make()"); + E(T(fh_pkt4_make)); return -1; } @@ -91,7 +91,7 @@ static int send_http(uint32_t saddr_be, uint32_t daddr_be, uint16_t sport_be, http_len = snprintf(http_buff, sizeof(http_buff), http_fmt, g_ctx.hostname); if (http_len < 0 || (size_t) http_len >= sizeof(http_buff)) { - E("ERROR: snprintf()"); + E("ERROR: snprintf(): %s", "failure"); return -1; } @@ -99,7 +99,7 @@ static int send_http(uint32_t saddr_be, uint32_t daddr_be, uint16_t sport_be, sport_be, dport_be, seq_be, ackseq_be, 1, http_buff, http_len); if (pkt_len < 0) { - E("ERROR: fh_pkt4_make()"); + E(T(fh_pkt4_make)); return -1; } @@ -130,7 +130,7 @@ static int callback(struct nfq_q_handle *qh, struct nfgenmsg *nfmsg, ph = nfq_get_msg_packet_hdr(nfa); if (!ph) { - E("ERROR: nfq_get_msg_packet_hdr()"); + EE("ERROR: nfq_get_msg_packet_hdr(): %s", "failure"); return -1; } @@ -138,12 +138,12 @@ static int callback(struct nfq_q_handle *qh, struct nfgenmsg *nfmsg, pkt_data = NULL; pkt_len = nfq_get_payload(nfa, &pkt_data); if (pkt_len < 0 || !pkt_data) { - E("ERROR: nfq_get_payload()"); + EE("ERROR: nfq_get_payload(): %s", "failure"); goto ret_accept; } if ((size_t) pkt_len < sizeof(*iph)) { - E("ERROR: invalid packet length: %d", pkt_len); + EE("ERROR: invalid packet length: %d", pkt_len); goto ret_accept; } @@ -151,17 +151,17 @@ static int callback(struct nfq_q_handle *qh, struct nfgenmsg *nfmsg, iph_len = iph->ihl * 4; if ((size_t) iph_len < sizeof(*iph)) { - E("ERROR: invalid IP header length: %d", iph_len); + EE("ERROR: invalid IP header length: %d", iph_len); goto ret_accept; } if (iph->protocol != IPPROTO_TCP) { - E("ERROR: not a TCP packet (protocol %d)", (int) iph->protocol); + EE("ERROR: not a TCP packet (protocol %d)", (int) iph->protocol); goto ret_accept; } if ((size_t) pkt_len < iph_len + sizeof(*tcph)) { - E("ERROR: invalid packet length: %d", pkt_len); + EE("ERROR: invalid packet length: %d", pkt_len); goto ret_accept; } @@ -196,7 +196,7 @@ static int callback(struct nfq_q_handle *qh, struct nfgenmsg *nfmsg, res = send_ack(iph->daddr, iph->saddr, tcph->dest, tcph->source, tcph->ack_seq, ack_new); if (res < 0) { - E("ERROR: send_ack()"); + EE(T(send_ack)); goto ret_accept; } } @@ -207,7 +207,7 @@ static int callback(struct nfq_q_handle *qh, struct nfgenmsg *nfmsg, res = send_http(iph->daddr, iph->saddr, tcph->dest, tcph->source, tcph->ack_seq, ack_new); if (res < 0) { - E("ERROR: send_http()"); + EE(T(send_http)); goto ret_accept; } } @@ -223,7 +223,7 @@ static int callback(struct nfq_q_handle *qh, struct nfgenmsg *nfmsg, res = send_http(iph->daddr, iph->saddr, tcph->dest, tcph->source, tcph->ack_seq, tcph->seq); if (res < 0) { - E("ERROR: send_http()"); + EE(T(send_http)); goto ret_accept; } } @@ -391,7 +391,7 @@ int fh_nfq_loop(void) res = nfq_handle_packet(h, buff, recv_len); if (res < 0) { err_cnt++; - E("ERROR: nfq_handle_packet()"); + E("ERROR: nfq_handle_packet(): %s", "failure"); continue; } diff --git a/src/nfrules.c b/src/nfrules.c index 14982dd..d4f5474 100644 --- a/src/nfrules.c +++ b/src/nfrules.c @@ -49,26 +49,26 @@ int fh_nfrules_setup(void) if (g_ctx.use_iptables) { res = fh_ipt4_flush(1); if (res < 0) { - E("ERROR: fh_ipt4_flush()"); + E(T(fh_ipt4_flush)); return -1; } res = fh_ipt4_add(); if (res < 0) { - E("ERROR: fh_ipt4_add()"); + E(T(fh_ipt4_add)); fh_ipt4_flush(0); return -1; } } else { res = fh_nft4_flush(1); if (res < 0) { - E("ERROR: fh_nft4_flush()"); + E(T(fh_nft4_flush)); return -1; } res = fh_nft4_add(); if (res < 0) { - E("ERROR: fh_nft4_add()"); + E(T(fh_nft4_add)); fh_nft4_flush(0); return -1; }