Skip to content

Commit 0da2079

Browse files
feat: Add options for inbound/outbound connections
1 parent 9efa690 commit 0da2079

File tree

5 files changed

+45
-5
lines changed

5 files changed

+45
-5
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ Basic Options:
2222
-i <interface> work on specified network interface
2323
2424
General Options:
25+
-0 process inbound connections
26+
-1 process outbound connections
2527
-4 process IPv4 connections
2628
-6 process IPv6 connections
2729
-d run as a daemon

include/globvar.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
struct fh_context {
2727
int exit;
2828
FILE *logfp;
29-
29+
/* -0 */ int inbound;
30+
/* -1 */ int outbound;
3031
/* -4 */ int use_ipv4;
3132
/* -6 */ int use_ipv6;
3233
/* -b */ const char *payloadpath;

src/globvar.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
struct fh_context g_ctx = {.exit = 0,
2727
.logfp = NULL,
2828

29+
/* -0 */ .inbound = 0,
30+
/* -1 */ .outbound = 0,
2931
/* -4 */ .use_ipv4 = 0,
3032
/* -6 */ .use_ipv6 = 0,
3133
/* -b */ .payloadpath = NULL,

src/mainfun.c

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ static void print_usage(const char *name)
5858
" -i <interface> work on specified network interface\n"
5959
"\n"
6060
"General Options:\n"
61+
" -0 process inbound connections\n"
62+
" -1 process outbound connections\n"
6163
" -4 process IPv4 connections\n"
6264
" -6 process IPv6 connections\n"
6365
" -d run as a daemon\n"
@@ -86,7 +88,7 @@ int main(int argc, char *argv[])
8688
{
8789
unsigned long long tmp;
8890
int res, opt, exitcode;
89-
char *ipproto_info;
91+
char *direction_info, *ipproto_info;
9092

9193
if (!argc || !argv[0]) {
9294
print_usage(PROGNAME);
@@ -98,8 +100,16 @@ int main(int argc, char *argv[])
98100

99101
exitcode = EXIT_FAILURE;
100102

101-
while ((opt = getopt(argc, argv, "46b:dfh:i:km:n:r:st:w:x:z")) != -1) {
103+
while ((opt = getopt(argc, argv, "0146b:dfh:i:km:n:r:st:w:x:z")) != -1) {
102104
switch (opt) {
105+
case '0':
106+
g_ctx.inbound = 1;
107+
break;
108+
109+
case '1':
110+
g_ctx.outbound = 1;
111+
break;
112+
103113
case '4':
104114
g_ctx.use_ipv4 = 1;
105115
break;
@@ -235,6 +245,10 @@ int main(int argc, char *argv[])
235245
return res < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
236246
}
237247

248+
if (!g_ctx.inbound && !g_ctx.outbound) {
249+
g_ctx.inbound = g_ctx.outbound = 1;
250+
}
251+
238252
if (!g_ctx.use_ipv4 && !g_ctx.use_ipv6) {
239253
g_ctx.use_ipv4 = g_ctx.use_ipv6 = 1;
240254
}
@@ -324,8 +338,17 @@ int main(int argc, char *argv[])
324338
} else {
325339
ipproto_info = "";
326340
}
327-
E("listening on %s%s, netfilter queue number %" PRIu32 "...", g_ctx.iface,
328-
ipproto_info, g_ctx.nfqnum);
341+
342+
if (g_ctx.inbound && !g_ctx.outbound) {
343+
direction_info = " (inbound only)";
344+
} else if (!g_ctx.inbound && g_ctx.outbound) {
345+
direction_info = " (outbound only)";
346+
} else {
347+
direction_info = "";
348+
}
349+
350+
E("listening on %s%s%s, netfilter queue number %" PRIu32 "...",
351+
g_ctx.iface, ipproto_info, direction_info, g_ctx.nfqnum);
329352

330353
/*
331354
Main Loop

src/rawsend.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,12 @@ int fh_rawsend_handle(struct sockaddr_ll *sll, uint8_t *pkt_data, int pkt_len)
344344
dst_ip, ntohs(tcph->dest));
345345
return 0;
346346
} else if (tcph->syn && tcph->ack) {
347+
if (!g_ctx.outbound) {
348+
E_INFO("%s:%u ===SYN-ACK(?)===> %s:%u", src_ip,
349+
ntohs(tcph->source), dst_ip, ntohs(tcph->dest));
350+
return 0;
351+
}
352+
347353
E_INFO("%s:%u ===SYN-ACK===> %s:%u", src_ip, ntohs(tcph->source),
348354
dst_ip, ntohs(tcph->dest));
349355

@@ -375,6 +381,12 @@ int fh_rawsend_handle(struct sockaddr_ll *sll, uint8_t *pkt_data, int pkt_len)
375381

376382
return 0;
377383
} else if (tcph->ack) {
384+
if (!g_ctx.inbound) {
385+
E_INFO("%s:%u ===ACK(?)===> %s:%u", src_ip, ntohs(tcph->source),
386+
dst_ip, ntohs(tcph->dest));
387+
return 0;
388+
}
389+
378390
E_INFO("%s:%u ===ACK===> %s:%u", src_ip, ntohs(tcph->source), dst_ip,
379391
ntohs(tcph->dest));
380392

0 commit comments

Comments
 (0)