From ff65b9f78017ad2448084ff9047fdf8fe77cd249 Mon Sep 17 00:00:00 2001 From: Kirill <20815458+dartvader316@users.noreply.github.com> Date: Mon, 19 Aug 2024 12:35:13 +0000 Subject: [PATCH] NetBSD Support / Portability fixes (#75) * Use socket to detect family on NetBSD * Switch to _DEFAULT_SOURCE for CPPFLAGS * fix using signed char for detecting its type --- Makefile | 2 +- desync.c | 42 +++++++++++++++++++++++++++--------------- main.c | 3 +-- packets.c | 20 ++++++++++---------- proxy.c | 9 ++++++++- 5 files changed, 47 insertions(+), 29 deletions(-) diff --git a/Makefile b/Makefile index 4d96fe8..d324e1f 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ TARGET = ciadpi -#CPPFLAGS = -D_XOPEN_SOURCE=500 +CPPFLAGS = -D_DEFAULT_SOURCE CFLAGS += -I. -std=c99 -Wall -Wno-unused -O2 WIN_LDFLAGS = -lws2_32 -lmswsock diff --git a/desync.c b/desync.c index f4a3aba..b7c3bd0 100644 --- a/desync.c +++ b/desync.c @@ -1,5 +1,3 @@ -#define _GNU_SOURCE - #include "desync.h" #include @@ -10,19 +8,22 @@ #include #include #include - #include - #include - - #ifdef __linux__ #include - #include + #include #include + + #ifndef __linux__ + #include + #else + #include + #include + #include - #define memfd_create(name, flags) syscall(__NR_memfd_create, name, flags); + + #define memfd_create(name, flags) syscall(__NR_memfd_create, name, flags) #endif #else #include - #include #include #include #endif @@ -35,6 +36,7 @@ int get_family(struct sockaddr *dst) { +#ifndef __NetBSD__ if (dst->sa_family == AF_INET6) { struct sockaddr_in6 *d6 = (struct sockaddr_in6 *)dst; static char *pat = "\0\0\0\0\0\0\0\0\0\0\xff\xff"; @@ -43,6 +45,7 @@ int get_family(struct sockaddr *dst) return AF_INET; } } +#endif return dst->sa_family; } @@ -113,12 +116,14 @@ void wait_send(int sfd) #define wait_send_if_support(sfd) // :( #endif -#ifdef __linux__ +#ifdef FAKE_SUPPORT +#ifndef _WIN32 ssize_t send_fake(int sfd, char *buffer, int cnt, long pos, int fa, struct desync_params *opt) { struct sockaddr_in6 addr = {}; socklen_t addr_size = sizeof(addr); +#ifdef __linux__ if (opt->md5sig) { if (getpeername(sfd, (struct sockaddr *)&addr, &addr_size) < 0) { @@ -126,6 +131,7 @@ ssize_t send_fake(int sfd, char *buffer, return -1; } } +#endif struct packet pkt; if (opt->fake_data.data) { pkt = opt->fake_data; @@ -134,12 +140,13 @@ ssize_t send_fake(int sfd, char *buffer, pkt = cnt != IS_HTTP ? fake_tls : fake_http; } size_t psz = pkt.size; - +#ifdef __linux__ int ffd = memfd_create("name", 0); if (ffd < 0) { uniperror("memfd_create"); return -1; } +#endif char *p = 0; ssize_t len = -1; @@ -159,6 +166,8 @@ ssize_t send_fake(int sfd, char *buffer, if (setttl(sfd, opt->ttl ? opt->ttl : 8, fa) < 0) { break; } + +#ifdef __linux__ if (opt->md5sig) { struct tcp_md5sig md5 = { .tcpm_keylen = 5 @@ -171,18 +180,20 @@ ssize_t send_fake(int sfd, char *buffer, break; } } +#endif if (opt->ip_options && fa == AF_INET && setsockopt(sfd, IPPROTO_IP, IP_OPTIONS, opt->ip_options, opt->ip_options_len) < 0) { uniperror("setsockopt IP_OPTIONS"); break; } - +#ifdef __linux__ len = sendfile(sfd, ffd, 0, pos); if (len < 0) { uniperror("sendfile"); break; } +#endif wait_send(sfd); memcpy(p, buffer, pos); @@ -195,6 +206,7 @@ ssize_t send_fake(int sfd, char *buffer, uniperror("setsockopt IP_OPTIONS"); break; } +#ifdef __linux__ if (opt->md5sig) { struct tcp_md5sig md5 = { .tcpm_keylen = 0 @@ -207,15 +219,14 @@ ssize_t send_fake(int sfd, char *buffer, break; } } +#endif break; } if (p) munmap(p, pos); close(ffd); return len; } -#endif - -#ifdef _WIN32 +#else OVERLAPPED ov = {}; ssize_t send_fake(int sfd, char *buffer, @@ -310,6 +321,7 @@ ssize_t send_fake(int sfd, char *buffer, return len; } #endif +#endif ssize_t send_oob(int sfd, char *buffer, ssize_t n, long pos) diff --git a/main.c b/main.c index 28a17a3..f62e204 100644 --- a/main.c +++ b/main.c @@ -1,5 +1,3 @@ -#define _GNU_SOURCE - #include #include #include @@ -444,6 +442,7 @@ int main(int argc, char **argv) while (!invalid && (rez = getopt_long( argc, argv, opt, options, 0)) != -1) { + switch (rez) { case 'N': diff --git a/packets.c b/packets.c index ecc84ab..b15ff68 100644 --- a/packets.c +++ b/packets.c @@ -215,16 +215,16 @@ int parse_http(char *buffer, size_t bsize, char **hs, uint16_t *port) } host += 6; - while ((buff_end - host) > 0 && isblank(*host)) { + while ((buff_end - host) > 0 && isblank((unsigned char) *host)) { host++; } char *l_end = memchr(host, '\n', buff_end - host); if (!l_end) { return 0; } - for (; isspace(*(l_end - 1)); l_end--) {} + for (; isspace((unsigned char) *(l_end - 1)); l_end--) {} - if (!(isdigit(*(l_end - 1)))) + if (!(isdigit((unsigned char) *(l_end - 1)))) h_end = 0; else { char *h = host; @@ -265,7 +265,7 @@ int get_http_code(char *b, size_t n) } char *e; long num = strtol(b + 9, &e, 10); - if (num < 100 || num > 511 || !isspace(*e)) { + if (num < 100 || num > 511 || !isspace((unsigned char) *e)) { return 0; } return (int )num; @@ -297,7 +297,7 @@ bool is_http_redirect(char *req, size_t qn, char *resp, size_t sn) if (!l_end) { return 0; } - for (; isspace(*(l_end - 1)); l_end--) {} + for (; isspace((unsigned char) *(l_end - 1)); l_end--) {} if ((l_end - location) > 7) { if (!strncmp(location, "http://", 7)) { @@ -382,17 +382,17 @@ int mod_http(char *buffer, size_t bsize, int m) for (par = host - 1; *par != ':'; par--) {} par -= 4; if (m & MH_HMIX) { - par[0] = tolower(par[0]); - par[1] = toupper(par[1]); - par[3] = toupper(par[3]); + par[0] = tolower((unsigned char) par[0]); + par[1] = toupper((unsigned char) par[1]); + par[3] = toupper((unsigned char) par[3]); } if (m & MH_DMIX) { for (int i = 0; i < hlen; i += 2) { - host[i] = toupper(host[i]); + host[i] = toupper((unsigned char)host[i]); } } if (m & MH_SPACE) { - for (; !isspace(*(host + hlen)); hlen++) {} + for (; !isspace((unsigned char) *(host + hlen)); hlen++) {} int sc = host - (par + 5); memmove(par + 5, host, hlen); memset(par + 5 + hlen, '\t', sc); diff --git a/proxy.c b/proxy.c index b96301a..e45c07d 100644 --- a/proxy.c +++ b/proxy.c @@ -1,4 +1,3 @@ -#define _GNU_SOURCE #define EID_STR #include "proxy.h" @@ -29,6 +28,10 @@ #include #include #include + + #if defined(__linux__) && defined(__GLIBC__) + extern int accept4(int, struct sockaddr *__restrict, socklen_t *__restrict, int); + #endif #endif @@ -392,7 +395,11 @@ int create_conn(struct poolhd *pool, } val->pair = pair; pair->pair = val; +#ifdef __NetBSD__ + pair->in6 = addr.in6; +#else pair->in6 = dst->in6; +#endif pair->flag = FLAG_CONN; val->type = EV_IGNORE;