Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions include/logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -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) { \
Expand All @@ -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, ...);

Expand Down
11 changes: 6 additions & 5 deletions src/ipv4ipt.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,15 @@ 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;
}

cnt = sizeof(ipt_create_cmds) / sizeof(*ipt_create_cmds);
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;
}
}
Expand Down Expand Up @@ -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;
}
}
Expand Down
7 changes: 4 additions & 3 deletions src/ipv4nft.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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;
}

Expand Down
3 changes: 3 additions & 0 deletions src/ipv4pkt.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#define _GNU_SOURCE
#include "ipv4pkt.h"

#include <errno.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
Expand All @@ -29,6 +30,7 @@
#include <libnetfilter_queue/libnetfilter_queue_tcp.h>

#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,
Expand All @@ -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;
}

Expand Down
19 changes: 11 additions & 8 deletions src/logging.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
16 changes: 8 additions & 8 deletions src/mainfun.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -231,39 +231,39 @@ 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;
}

E("FakeHTTP version " VERSION);

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,
Expand All @@ -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;
}

Expand Down
26 changes: 13 additions & 13 deletions src/nfqueue.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -91,15 +91,15 @@ 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;
}

pkt_len = fh_pkt4_make(pkt_buff, sizeof(pkt_buff), saddr_be, daddr_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;
}

Expand Down Expand Up @@ -130,38 +130,38 @@ 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;
}

pkt_id = ntohl(ph->packet_id);
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;
}

iph = (struct iphdr *) pkt_data;
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;
}

Expand Down Expand Up @@ -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;
}
}
Expand All @@ -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;
}
}
Expand All @@ -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;
}
}
Expand Down Expand Up @@ -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;
}

Expand Down
8 changes: 4 additions & 4 deletions src/nfrules.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down