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
This commit is contained in:
Kirill 2024-08-19 12:35:13 +00:00 committed by GitHub
parent 9a9fc8a9aa
commit ff65b9f780
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 47 additions and 29 deletions

View File

@ -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

View File

@ -1,5 +1,3 @@
#define _GNU_SOURCE
#include "desync.h"
#include <stdio.h>
@ -10,19 +8,22 @@
#include <time.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/tcp.h>
#ifdef __linux__
#include <sys/mman.h>
#include <sys/sendfile.h>
#include <arpa/inet.h>
#include <fcntl.h>
#ifndef __linux__
#include <netinet/tcp.h>
#else
#include <sys/sendfile.h>
#include <linux/tcp.h>
#include <sys/syscall.h>
#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 <winsock2.h>
#include <windows.h>
#include <ws2tcpip.h>
#include <mswsock.h>
#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)

3
main.c
View File

@ -1,5 +1,3 @@
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@ -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':

View File

@ -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);

View File

@ -1,4 +1,3 @@
#define _GNU_SOURCE
#define EID_STR
#include "proxy.h"
@ -29,6 +28,10 @@
#include <arpa/inet.h>
#include <netinet/tcp.h>
#include <netdb.h>
#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;