diff --git a/.gitignore b/.gitignore index 3a3cff5b..88181568 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ .vscode -ciadpi.exe \ No newline at end of file +ciadpi* +*.o diff --git a/Makefile b/Makefile index 40f67083..4d96fe86 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ TARGET = ciadpi -CPPFLAGS = -D_XOPEN_SOURCE=500 +#CPPFLAGS = -D_XOPEN_SOURCE=500 CFLAGS += -I. -std=c99 -Wall -Wno-unused -O2 WIN_LDFLAGS = -lws2_32 -lmswsock diff --git a/desync.c b/desync.c index 1b0fbb99..8cbeabc0 100644 --- a/desync.c +++ b/desync.c @@ -10,14 +10,16 @@ #include #include #include - - #ifdef __linux__ + #include - #include + #ifdef __linux__ + #include + #endif #include - #include - + #include "desync.h" + + #ifdef __linux__ #ifdef MFD_CLOEXEC #include #define memfd_create(name, flags) syscall(__NR_memfd_create, name, flags); @@ -82,7 +84,7 @@ static inline void delay(long ms) #define delay(ms) Sleep(ms) #endif -#ifdef __linux__ +#if defined(__linux__) || defined(__FreeBSD__) void wait_send(int sfd) { for (int i = 0; params.wait_send && i < 500; i++) { @@ -118,7 +120,7 @@ void wait_send(int sfd) #define wait_send_if_support(sfd) // :( #endif -#ifdef __linux__ +#if defined(__linux__) || defined(__FreeBSD__) ssize_t send_fake(int sfd, char *buffer, int cnt, long pos, int fa, struct desync_params *opt) { @@ -165,11 +167,18 @@ ssize_t send_fake(int sfd, char *buffer, break; } if (opt->md5sig) { +#ifdef __linux__ struct tcp_md5sig md5 = { .tcpm_keylen = 5 }; memcpy(&md5.tcpm_addr, &addr, addr_size); - +#elif defined(__FreeBSD__) + // FIXME: Should be struct tcpmd5_support + // but netipsec/ipsec_support.h hides this under ifdef KERNEL + int md5 = 1; +#else +#error +#endif if (setsockopt(sfd, IPPROTO_TCP, TCP_MD5SIG, (char *)&md5, sizeof(md5)) < 0) { uniperror("setsockopt TCP_MD5SIG"); @@ -183,7 +192,11 @@ ssize_t send_fake(int sfd, char *buffer, break; } +#ifdef __linux__ len = sendfile(sfd, ffd, 0, pos); +#else + len = sendfile(sfd, ffd, 0, pos, NULL, NULL, 0); +#endif if (len < 0) { uniperror("sendfile"); break; @@ -201,10 +214,16 @@ ssize_t send_fake(int sfd, char *buffer, break; } if (opt->md5sig) { +#ifdef __linux__ struct tcp_md5sig md5 = { .tcpm_keylen = 0 }; memcpy(&md5.tcpm_addr, &addr, addr_size); +#elif defined(__FreeBSD__) + int md5 = 0; +#else +#error +#endif if (setsockopt(sfd, IPPROTO_TCP, TCP_MD5SIG, (char *)&md5, sizeof(md5)) < 0) { diff --git a/extend.c b/extend.c index b91edf12..247010d4 100644 --- a/extend.c +++ b/extend.c @@ -30,15 +30,16 @@ int set_timeout(int fd, unsigned int s) uniperror("setsockopt TCP_USER_TIMEOUT"); return -1; } - #else - #ifdef _WIN32 + #elif defined(__FreeBSD__) + // https://wiki.freebsd.org/CatalinNicutar/TCPUTO + // sadly not yet available + #elif defined(_WIN32) if (setsockopt(fd, IPPROTO_TCP, TCP_MAXRT, (char *)&s, sizeof(s))) { uniperror("setsockopt TCP_MAXRT"); return -1; } #endif - #endif return 0; } diff --git a/main.c b/main.c index 19e0c2b3..be92616b 100644 --- a/main.c +++ b/main.c @@ -24,6 +24,10 @@ #define close(fd) closesocket(fd) #endif +#ifdef __FreeBSD__ + #include +#endif + #define VERSION "12" char oob_char[1] = "a"; @@ -57,7 +61,7 @@ struct params params = { .sin6_family = AF_INET6 }, .laddr = { - .sin6_family = AF_INET + .sin6_family = AF_INET6 }, .debug = 0 }; @@ -94,7 +98,7 @@ const char help_text[] = { #ifdef FAKE_SUPPORT " -f, --fake Split and send fake packet\n" " -t, --ttl TTL of fake packets, default 8\n" - #ifdef __linux__ + #if defined(__linux__) || defined(__FreeBSD__) " -k, --ip-opt[=f|:str] IP options of fake packets\n" " -S, --md5sig Add MD5 Signature option for fake packets\n" #endif @@ -138,7 +142,7 @@ const struct option options[] = { #ifdef FAKE_SUPPORT {"fake", 1, 0, 'f'}, {"ttl", 1, 0, 't'}, - #ifdef __linux__ + #if defined(__linux__) || defined(__FreeBSD__) {"ip-opt", 2, 0, 'k'}, {"md5sig", 0, 0, 'S'}, #endif diff --git a/packets.c b/packets.c index 540ba86b..100f3c69 100644 --- a/packets.c +++ b/packets.c @@ -1,6 +1,10 @@ -#define _GNU_SOURCE +#ifdef __FreeBSD__ + #include +#else + #define _GNU_SOURCE +#endif -#include +#include "packets.h" #include #include #include diff --git a/params.h b/params.h index 825950c3..79047d62 100644 --- a/params.h +++ b/params.h @@ -1,5 +1,6 @@ #include #include +#include #include "mpool.h" @@ -9,7 +10,11 @@ #include #endif -#if defined(__linux__) || defined(_WIN32) +#ifdef __FreeBSD__ + #include +#endif + +#if defined(__linux__) || defined(_WIN32) || defined(__FreeBSD__) #define FAKE_SUPPORT 1 #define TIMEOUT_SUPPORT 1 #endif @@ -107,4 +112,4 @@ extern struct packet fake_http; extern struct packet oob_data; extern struct packet fake_udp; -extern char ip_option[1]; \ No newline at end of file +extern char ip_option[1]; diff --git a/proxy.c b/proxy.c index de5ad7ad..e3151d60 100644 --- a/proxy.c +++ b/proxy.c @@ -80,7 +80,7 @@ static inline char addr_equ( static inline int nb_socket(int domain, int type) { - #ifdef __linux__ + #if defined(__linux__) || defined(__FreeBSD__) int fd = socket(domain, type | SOCK_NONBLOCK, 0); #else int fd = socket(domain, type, 0); @@ -96,15 +96,13 @@ static inline int nb_socket(int domain, int type) close(fd); return -1; } - #else - #ifndef __linux__ + #elif !defined(__linux__) if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0) { uniperror("fcntl"); close(fd); return -1; } #endif - #endif return fd; } @@ -515,7 +513,7 @@ static inline int on_accept(struct poolhd *pool, struct eval *val) while (1) { socklen_t len = sizeof(client); - #ifdef __linux__ + #if defined(__linux__) || defined(__FreeBSD__) int c = accept4(val->fd, &client.sa, &len, SOCK_NONBLOCK); #else int c = accept(val->fd, &client.sa, &len); diff --git a/proxy.h b/proxy.h index b9db0a86..8da072b4 100644 --- a/proxy.h +++ b/proxy.h @@ -4,6 +4,7 @@ #include #else #include + #include #endif #include "conev.h"