Skip to content

Commit baffa1e

Browse files
feat: Add silent mode
1 parent 81a3ff3 commit baffa1e

2 files changed

Lines changed: 35 additions & 24 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Options:
2323
-m <mark> fwmark for bypassing the queue
2424
-n <number> netfilter queue number
2525
-r <repeat> duplicate generated packets for <repeat> times
26+
-s enable silent mode
2627
-t <ttl> TTL for generated packets
2728
-w <file> write log to <file> instead of stderr
2829
-x <mask> set the mask for fwmark

src/fakehttp.c

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,15 @@
4444

4545
#define E(...) logger(__func__, __FILE__, __LINE__, __VA_ARGS__)
4646
#define E_RAW(...) logger_raw(__VA_ARGS__)
47+
#define E_INFO(...) \
48+
if (!g_silent) { \
49+
E(__VA_ARGS__); \
50+
}
4751

4852
static FILE *g_logfp = NULL;
4953
static int g_sockfd = 0;
5054
static int g_exit = 0;
55+
static int g_silent = 0;
5156
static int g_repeat = 3;
5257
static uint32_t g_fwmark = 0x8000;
5358
static uint32_t g_fwmask = 0;
@@ -68,6 +73,7 @@ static void print_usage(const char *name)
6873
" -n <number> netfilter queue number\n"
6974
" -r <repeat> duplicate generated packets for <repeat> "
7075
"times\n"
76+
" -s enable silent mode\n"
7177
" -t <ttl> TTL for generated packets\n"
7278
" -w <file> write log to <file> instead of stderr\n"
7379
" -x <mask> set the mask for fwmark\n"
@@ -610,25 +616,26 @@ static int callback(struct nfq_q_handle *qh, struct nfgenmsg *nfmsg,
610616

611617
tcph = (struct tcphdr *) (pkt_data + iph_len);
612618
tcph_len = tcph->doff * 4;
619+
tcp_payload_len = pkt_len - iph_len - tcph_len;
613620

614-
if (!inet_ntop(AF_INET, &iph->saddr, src_ip, sizeof(src_ip))) {
615-
strncpy(src_ip, "INVALID", sizeof(src_ip) - 1);
616-
src_ip[sizeof(src_ip) - 1] = '\0';
617-
}
618-
if (!inet_ntop(AF_INET, &iph->daddr, dst_ip, sizeof(dst_ip))) {
619-
strncpy(dst_ip, "INVALID", sizeof(dst_ip) - 1);
620-
src_ip[sizeof(src_ip) - 1] = '\0';
621+
if (!g_silent) {
622+
if (!inet_ntop(AF_INET, &iph->saddr, src_ip, sizeof(src_ip))) {
623+
strncpy(src_ip, "INVALID", sizeof(src_ip) - 1);
624+
src_ip[sizeof(src_ip) - 1] = '\0';
625+
}
626+
if (!inet_ntop(AF_INET, &iph->daddr, dst_ip, sizeof(dst_ip))) {
627+
strncpy(dst_ip, "INVALID", sizeof(dst_ip) - 1);
628+
src_ip[sizeof(src_ip) - 1] = '\0';
629+
}
621630
}
622631

623-
tcp_payload_len = pkt_len - iph_len - tcph_len;
624-
625632
if (tcp_payload_len > 0) {
626-
E("%s:%u ===PAYLOAD(?)===> %s:%u", src_ip, ntohs(tcph->source), dst_ip,
627-
ntohs(tcph->dest));
633+
E_INFO("%s:%u ===PAYLOAD(?)===> %s:%u", src_ip, ntohs(tcph->source),
634+
dst_ip, ntohs(tcph->dest));
628635
goto ret_mark_repeat;
629636
} else if (tcph->syn && tcph->ack) {
630-
E("%s:%u ===SYN-ACK===> %s:%u", src_ip, ntohs(tcph->source), dst_ip,
631-
ntohs(tcph->dest));
637+
E_INFO("%s:%u ===SYN-ACK===> %s:%u", src_ip, ntohs(tcph->source),
638+
dst_ip, ntohs(tcph->dest));
632639

633640
ack_new = ntohl(tcph->seq);
634641
ack_new++;
@@ -642,8 +649,8 @@ static int callback(struct nfq_q_handle *qh, struct nfgenmsg *nfmsg,
642649
goto ret_accept;
643650
}
644651
}
645-
E("%s:%u <===ACK(*)=== %s:%u", src_ip, ntohs(tcph->source), dst_ip,
646-
ntohs(tcph->dest));
652+
E_INFO("%s:%u <===ACK(*)=== %s:%u", src_ip, ntohs(tcph->source),
653+
dst_ip, ntohs(tcph->dest));
647654

648655
for (i = 0; i < g_repeat; i++) {
649656
res = send_http(iph->daddr, iph->saddr, tcph->dest, tcph->source,
@@ -653,13 +660,13 @@ static int callback(struct nfq_q_handle *qh, struct nfgenmsg *nfmsg,
653660
goto ret_accept;
654661
}
655662
}
656-
E("%s:%u <===HTTP(*)=== %s:%u", src_ip, ntohs(tcph->source), dst_ip,
657-
ntohs(tcph->dest));
663+
E_INFO("%s:%u <===HTTP(*)=== %s:%u", src_ip, ntohs(tcph->source),
664+
dst_ip, ntohs(tcph->dest));
658665

659666
goto ret_mark_repeat;
660667
} else if (tcph->ack) {
661-
E("%s:%u ===ACK===> %s:%u", src_ip, ntohs(tcph->source), dst_ip,
662-
ntohs(tcph->dest));
668+
E_INFO("%s:%u ===ACK===> %s:%u", src_ip, ntohs(tcph->source), dst_ip,
669+
ntohs(tcph->dest));
663670

664671
for (i = 0; i < g_repeat; i++) {
665672
res = send_http(iph->daddr, iph->saddr, tcph->dest, tcph->source,
@@ -669,13 +676,13 @@ static int callback(struct nfq_q_handle *qh, struct nfgenmsg *nfmsg,
669676
goto ret_accept;
670677
}
671678
}
672-
E("%s:%u <===HTTP(*)=== %s:%u", src_ip, ntohs(tcph->source), dst_ip,
673-
ntohs(tcph->dest));
679+
E_INFO("%s:%u <===HTTP(*)=== %s:%u", src_ip, ntohs(tcph->source),
680+
dst_ip, ntohs(tcph->dest));
674681

675682
goto ret_mark_repeat;
676683
} else {
677-
E("%s:%u ===(?)===> %s:%u", src_ip, ntohs(tcph->source), dst_ip,
678-
ntohs(tcph->dest));
684+
E_INFO("%s:%u ===(?)===> %s:%u", src_ip, ntohs(tcph->source), dst_ip,
685+
ntohs(tcph->dest));
679686
goto ret_accept;
680687
}
681688

@@ -705,7 +712,7 @@ int main(int argc, char *argv[])
705712
return EXIT_FAILURE;
706713
}
707714

708-
while ((opt = getopt(argc, argv, "h:i:m:n:r:t:w:x:")) != -1) {
715+
while ((opt = getopt(argc, argv, "h:i:m:n:r:st:w:x:")) != -1) {
709716
switch (opt) {
710717
case 'h':
711718
if (strlen(optarg) > _POSIX_HOST_NAME_MAX) {
@@ -751,6 +758,9 @@ int main(int argc, char *argv[])
751758
}
752759
g_repeat = tmp;
753760
break;
761+
case 's':
762+
g_silent = 1;
763+
break;
754764
case 't':
755765
if (sscanf(optarg, "%llu", &tmp) != 1 || !tmp ||
756766
tmp > UINT8_MAX) {

0 commit comments

Comments
 (0)