From ccdb5db67558205dd8d451e1878774cc93835cd1 Mon Sep 17 00:00:00 2001 From: MCApollo <34170230+MCApollo@users.noreply.github.com> Date: Mon, 3 Aug 2020 18:57:43 -0900 Subject: [PATCH] tcp-tunnel: botch Linux support --- tcp-tunnel/Makefile | 1 + tcp-tunnel/src/main.c | 21 ++++++++++++++++----- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/tcp-tunnel/Makefile b/tcp-tunnel/Makefile index a2ef74d..989ba71 100644 --- a/tcp-tunnel/Makefile +++ b/tcp-tunnel/Makefile @@ -10,6 +10,7 @@ src := $(wildcard src/*.c) $(wildcard src/**/*.c) CC = clang FLAGS = -DOUT_OF_TREE_BUILD -I$(QEMU_DIR)/include -fno-stack-check -fno-stack-protector -arch arm64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk +# FLAGS += -DLINUX TARGET = tunnel bin/$(TARGET): $(src) diff --git a/tcp-tunnel/src/main.c b/tcp-tunnel/src/main.c index 14aef64..6540662 100644 --- a/tcp-tunnel/src/main.c +++ b/tcp-tunnel/src/main.c @@ -14,6 +14,17 @@ #define DIR_IN 1 #define DIR_OUT 2 +#ifdef LINUX +/* FIXME: There's no way to detect this, good luck. + Apple changed EAGAIN to a different number, causing errors during runtime. +*/ +# define EEAGAIN 11 +# define EEWOULDBLOCK EEAGAIN +#else /* __APPLE__ */ +# define EEAGAIN EAGAIN +# define EEWOULDBLOCK EWOULDBLOCK +#endif + typedef struct { int fd; int *error; @@ -237,8 +248,8 @@ static int send_receive(socket_t *in, socket_t *out) out->close(out->fd); return 0; } else { - if ((*(in->error) != EAGAIN) && \ - (*(in->error) != EWOULDBLOCK)) + if ((*(in->error) != EEAGAIN) && \ + (*(in->error) != EEWOULDBLOCK)) { // Remote socket error! out->close(out->fd); @@ -260,7 +271,7 @@ static int send_receive(socket_t *in, socket_t *out) return 0; } else { if ((*(out->error) != EAGAIN) && \ - (*(out->error) != EWOULDBLOCK)) + (*(out->error) != EWOULDBLOCK)) // Don't use EEAGAIN on `out`. { // Target socket error! out->close(out->fd); @@ -371,8 +382,8 @@ static int tunnel(socket_t *s_listen, socket_t *in, socket_t *out, &remotelen); if (in->fd < 0) { - if ((*(in->error) != EAGAIN ) && \ - (*(in->error) != EWOULDBLOCK)) + if ((*(in->error) != EEAGAIN ) && \ + (*(in->error) != EEWOULDBLOCK)) { fprintf(stderr, "accept failed: %d\n", *(in->error)); s_listen->close(s_listen->fd);