penguin/tinyproxy

An L4 proxy designed to act as a tiny transparent shim

commit 7d1a7a487c8c5a99ad0f898240820ad2c2f14365

author斟酌 鵬兄 <tgckpg@gmail.com>
date2026-05-31T18:16:18Z
subjectFixed macos compat issue
commit 7d1a7a487c8c5a99ad0f898240820ad2c2f14365
Author: 斟酌 鵬兄 <tgckpg@gmail.com>
Date:   2026-05-31T18:16:18Z

    Fixed macos compat issue
---
 src/compat.h       | 39 +++++++++++++++++++++++++++++++++++++++
 src/datagram_raw.c | 21 ++++++++-------------
 src/tinyproxy.c    |  5 +++--
 3 files changed, 50 insertions(+), 15 deletions(-)

diff --git a/src/compat.h b/src/compat.h
index f69baee..23b34f3 100644
--- a/src/compat.h
+++ b/src/compat.h
@@ -23,8 +23,11 @@
 #include <sys/un.h>
 #include <arpa/inet.h>
 #include <netinet/in.h>
+#include <netinet/ip.h>
+#include <netinet/udp.h>
 #include <unistd.h>
 
+
 #endif
 
 #ifdef _WIN32
@@ -61,4 +64,40 @@ static inline int compat_mkdir(const char *path, int mode)
 #endif
 }
 
+static inline uint16_t compat_raw_ip_len(uint16_t len)
+{
+#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)
+	return len;
+#else
+	return htons(len);
+#endif
+}
+
+static inline uint16_t compat_raw_ip_off(uint16_t off)
+{
+#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)
+	return off;
+#else
+	return htons(off);
+#endif
+}
+
+#if defined(AF_INET) && \
+	defined(SOCK_RAW) && \
+	defined(IPPROTO_RAW) && \
+	defined(IPPROTO_IP) && \
+	defined(IP_HDRINCL)
+#define COMPAT_HAS_IPV4_RAW 1
+#else
+#define COMPAT_HAS_IPV4_RAW 0
+#endif
+
+#if !COMPAT_HAS_IPV4_RAW
+#if defined(_MSC_VER)
+#pragma message("info: IPv4 raw socket support not available; raw datagram features disabled")
+#elif defined(__clang__) || defined(__GNUC__)
+#pragma message "info: IPv4 raw socket support not available; raw datagram features disabled"
+#endif
+#endif
+
 #endif
diff --git a/src/datagram_raw.c b/src/datagram_raw.c
index f5ec6a0..41d19ba 100644
--- a/src/datagram_raw.c
+++ b/src/datagram_raw.c
@@ -5,16 +5,12 @@
 #include <stdlib.h>
 #include <string.h>
 
-#ifndef _WIN32
-#include <arpa/inet.h>
-#include <netinet/ip.h>
-#include <netinet/udp.h>
-#include <sys/socket.h>
-#include <unistd.h>
-#endif
-
+#include "klog.h"
+#include "compat.h"
 #include "endpoint.h"
 
+#if COMPAT_HAS_IPV4_RAW
+
 static uint16_t checksum16(const void *data, size_t len)
 {
 	const uint8_t *p = data;
@@ -37,8 +33,6 @@ static uint16_t checksum16(const void *data, size_t len)
 	return (uint16_t)~sum;
 }
 
-#ifndef _WIN32
-
 int datagram_raw_open_ipv4(evutil_socket_t *out_fd)
 {
 	evutil_socket_t fd;
@@ -142,9 +136,9 @@ int datagram_raw_send_udp_ipv4(
 	ip->ip_v = 4;
 	ip->ip_hl = 5;
 	ip->ip_tos = 0;
-	ip->ip_len = htons((uint16_t)packet_len);
+	ip->ip_len = compat_raw_ip_len((uint16_t)packet_len);
 	ip->ip_id = 0;
-	ip->ip_off = 0;
+	ip->ip_off = compat_raw_ip_off(0);
 	ip->ip_ttl = 64;
 	ip->ip_p = IPPROTO_UDP;
 	ip->ip_src = src_addr.sin_addr;
@@ -210,11 +204,12 @@ int datagram_raw_send_udp_ipv4(
 	size_t data_len)
 {
 	(void)raw_fd;
-	(void)src_ap;
+	(void)src_ep;
 	(void)dst_addr;
 	(void)dst_addr_len;
 	(void)data;
 	(void)data_len;
+	LOG_ERROR("IPv4 raw is not supported on this platform");
 
 	return -ENOTSUP;
 }
diff --git a/src/tinyproxy.c b/src/tinyproxy.c
index 3e130ca..a02a45d 100644
--- a/src/tinyproxy.c
+++ b/src/tinyproxy.c
@@ -289,8 +289,9 @@ int main(int argc, char **argv)
 			LOG_ERROR("failed to start route",
 					"line", _LOGV(r->line_no),
 					"listen", _LOGV_ENDPOINT(&r->listen),
-					"upstream", _LOGV_ENDPOINT(&r->upstream)
-					);
+					"upstream", _LOGV_ENDPOINT(&r->upstream),
+					"reason", _LOGV(strerror(-rc))
+			);
 			goto out;
 		}
 	}