mitmd

mitm TCP connections
Log | Files | Refs | README

commit 277a2610151905d713fa9196c62cd78c015ff97d
parent 497709360e7fa0cf43f8bf6b97822e7ad4e0e2f6
Author: hhvn <dev@hhvn.uk>
Date:   Fri,  6 May 2022 15:59:11 +0100

Report errors from parent/reader seperately

Diffstat:
Mmitmd.c | 22+++++++++++++---------
1 file changed, 13 insertions(+), 9 deletions(-)

diff --git a/mitmd.c b/mitmd.c @@ -23,18 +23,22 @@ #define CLIENT_STYLE "\033[96m>\033[0m" #define REMOTE_STYLE "\033[90m<\033[0m" +int parent = 1; char *argv0; char *myhost = "localhost", *myport = "9999", *host, *port; -void +void die(const int exitc, const char *format, ...) { va_list ap; va_start(ap, format); - fprintf(stderr, "Fatal: "); + if (parent) + fprintf(stderr, "(Parent) fatal: "); + else + fprintf(stderr, "(Child): "); vfprintf(stderr, format, ap); va_end(ap); @@ -105,21 +109,21 @@ mitm(int client, char *host, char *port) { hints.ai_socktype = SOCK_STREAM; if ((sret = getaddrinfo(host, port, &hints, &ai)) != 0) { - dprintf(client, "getaddrinfo(): %s\n", gai_strerror(sret)); + die(1, "getaddrinfo(): %s\n", gai_strerror(sret)); return; } if ((remote = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol)) == 1) { - dprintf(client, "socket(): %s\n", strerror(errno)); + die(1, "socket(): %s\n", strerror(errno)); goto cleanup; } if (connect(remote, ai->ai_addr, ai->ai_addrlen) == -1) { - dprintf(client, "connect(): %s\n", strerror(errno)); + die(1, "connect(): %s\n", strerror(errno)); goto cleanup; } switch (pid = fork()) { case -1: - dprintf(client, "fork(): %s\n", errno); + die(1, "fork(): %s\n", errno); break; case 0: while (read_line(client, bufc, sizeof(bufc)) != 0) { @@ -185,15 +189,15 @@ main(int argc, char *argv[]) { switch (pid = fork()) { case -1: - fprintf(stderr, "fork(): %s\n", strerror(errno)); - shutdown(handle, SHUT_RDWR); - close(handle); + die(1, "fork(): %s\n", strerror(errno)); break; case 0: + parent = 0; mitm(handle, host, port); shutdown(handle, SHUT_RDWR); close(handle); exit(EXIT_SUCCESS); + /* parent continue outside loop */ } }