Skip to content

Commit

Permalink
added support for FreeBSD/OpenBSD (#201)
Browse files Browse the repository at this point in the history
  • Loading branch information
ge9 authored Jun 29, 2024
1 parent c8e1e6c commit 82fa950
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 6 deletions.
11 changes: 10 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ endif
ifeq ($(OS), FreeBSD)
override CFLAGS +=-I/usr/local/include -L/usr/local//lib
endif
ifeq ($(OS), OpenBSD)
override CFLAGS +=-I/usr/local/include -L/usr/local//lib #same as FreeBSD
endif
ifeq ($(OS), Darwin)
override CFLAGS +=-I/usr/local/opt/openssl/include -L/usr/local/opt/openssl/lib
SHELL := /bin/bash
Expand Down Expand Up @@ -52,7 +55,10 @@ override CFLAGS += -DENABLE_HTTPS_PROXY
override FEATURES += ENABLE_HTTPS_PROXY
$(info Compile with HTTPS proxy enabled.)
endif
override LIBS += -lssl -lcrypto -ldl
override LIBS += -lssl -lcrypto
ifneq ($(OS), OpenBSD)
override LIBS += -ldl
endif
override CFLAGS += -DUSE_CRYPTO_OPENSSL
endif
ifdef ENABLE_STATIC
Expand All @@ -79,6 +85,9 @@ $(CONF):
OpenBSD) \
echo "#define USE_PF" >$(CONF) \
;; \
NetBSD) \
echo "#define USE_PF" >$(CONF) \
;; \
Darwin) \
echo -e "#define USE_PF\n#define _APPLE_" >$(CONF) \
;; \
Expand Down
2 changes: 1 addition & 1 deletion base.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ static base_instance instance = {
.log_info = false,
};

#if defined __FreeBSD__ || defined USE_PF
#if defined __FreeBSD__ || defined USE_PF || defined __OpenBSD__ || defined __NetBSD__
static int redir_open_private(const char *fname, int flags)
{
int fd = open(fname, flags);
Expand Down
2 changes: 1 addition & 1 deletion libc-compat.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
# define SOL_IP IPPROTO_IP
#endif

#ifdef __FreeBSD__
#if defined __FreeBSD__ || defined __OpenBSD__ || defined __NetBSD__
#ifndef INADDR_LOOPBACK
# warning Using hardcoded value for INADDR_LOOPBACK for FreeBSD.
# define INADDR_LOOPBACK 0x7F000001
Expand Down
2 changes: 1 addition & 1 deletion redsocks.c
Original file line number Diff line number Diff line change
Expand Up @@ -1022,7 +1022,7 @@ static int redsocks_init_instance(redsocks_instance *instance)
if (apply_reuseport(fd))
log_error(LOG_WARNING, "Continue without SO_REUSEPORT enabled");

#if defined(__APPLE__) || defined(__FreeBSD__)
#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)
bindaddr_len = instance->config.bindaddr.ss_len > 0 ? instance->config.bindaddr.ss_len : sizeof(instance->config.bindaddr);
#else
bindaddr_len = sizeof(instance->config.bindaddr);
Expand Down
32 changes: 31 additions & 1 deletion redudp.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

/* redsocks2 - transparent TCP/UDP-to-proxy redirector
* Copyright (C) 2013-2017 Zhuofei Wang <semigodking@gmail.com>
*
Expand Down Expand Up @@ -133,6 +134,22 @@ static int bound_udp_get(const struct sockaddr *addr)
node->key = key;
node->ref = 1;
node->fd = socket(addr->sa_family, SOCK_DGRAM, IPPROTO_UDP);
#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)
int on = 1;
#if defined(__FreeBSD__)
#ifdef SOL_IPV6
if (addr->sa_family == AF_INET) {
#endif
setsockopt(node->fd, IPPROTO_IP, IP_BINDANY, &on, sizeof(on));
#ifdef SOL_IPV6
} else {
setsockopt(node->fd, IPPROTO_IPV6, IPV6_BINDANY, &on, sizeof(on));
}
#endif
#else
setsockopt(node->fd, SOL_SOCKET, SO_BINDANY, &on, sizeof(on));
#endif
#endif
node->t_last_rx = redsocks_time(NULL);
if (node->fd == -1) {
log_errno(LOG_ERR, "socket");
Expand Down Expand Up @@ -300,8 +317,13 @@ void redudp_fwd_pkt_to_sender(redudp_client *client, void *buf, size_t len,
}
// TODO: record remote address in client


sent = sendto(fd, buf, len, 0,
#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)
(struct sockaddr*)&client->clientaddr, sizeof(struct sockaddr_in));
#else
(struct sockaddr*)&client->clientaddr, sizeof(client->clientaddr));
#endif
if (sent != len) {
redudp_log_error(
client,
Expand Down Expand Up @@ -638,6 +660,10 @@ static int redudp_init_instance(redudp_instance *instance)
log_errno(LOG_ERR, "setsockopt(listener, SOL_IP, IP_RECVORIGDSTADDR)");
goto fail;
}
#if defined(__OpenBSD__) || defined(__NetBSD__)
setsockopt(fd, IPPROTO_IP, IP_RECVDSTADDR, &on, sizeof(on));
setsockopt(fd, IPPROTO_IP, IP_RECVDSTPORT, &on, sizeof(on));
#endif
#ifdef SOL_IPV6
}
else {
Expand All @@ -646,6 +672,10 @@ static int redudp_init_instance(redudp_instance *instance)
log_errno(LOG_ERR, "setsockopt(listener, SOL_IPV6, IPV6_RECVORIGDSTADDR)");
goto fail;
}
#if defined(__OpenBSD__) || defined(__NetBSD__)
setsockopt(fd, IPPROTO_IP, IPV6_RECVPKTINFO, &on, sizeof(on));
setsockopt(fd, IPPROTO_IP, IPV6_RECVDSTPORT, &on, sizeof(on));
#endif
}
#endif
log_error(LOG_INFO, "redudp @ %s: TPROXY", red_inet_ntop(&instance->config.bindaddr, buf1, sizeof(buf1)));
Expand All @@ -660,7 +690,7 @@ static int redudp_init_instance(redudp_instance *instance)
if (apply_reuseport(fd))
log_error(LOG_WARNING, "Continue without SO_REUSEPORT enabled");

#if defined(__APPLE__) || defined(__FreeBSD__)
#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)
bindaddr_len = instance->config.bindaddr.ss_len > 0 ? instance->config.bindaddr.ss_len : sizeof(instance->config.bindaddr);
#else
bindaddr_len = sizeof(instance->config.bindaddr);
Expand Down
4 changes: 4 additions & 0 deletions socks5-udp.c
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,11 @@ static void socks5_read_assoc_reply(struct bufferevent *buffev, void *_arg)
goto fail;
}

#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)
error = connect(fd, (struct sockaddr*)&socks5client->udprelayaddr, sizeof(struct sockaddr_in));
#else
error = connect(fd, (struct sockaddr*)&socks5client->udprelayaddr, sizeof(socks5client->udprelayaddr));
#endif
if (error) {
redudp_log_errno(client, LOG_NOTICE, "connect");
goto fail;
Expand Down
2 changes: 1 addition & 1 deletion tcpdns.c
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ static int tcpdns_init_instance(tcpdns_instance *instance)
if (apply_reuseport(fd))
log_error(LOG_WARNING, "Continue without SO_REUSEPORT enabled");

#if defined(__APPLE__) || defined(__FreeBSD__)
#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)
bindaddr_len = instance->config.bindaddr.ss_len > 0 ? instance->config.bindaddr.ss_len : sizeof(instance->config.bindaddr);
#else
bindaddr_len = sizeof(instance->config.bindaddr);
Expand Down
15 changes: 15 additions & 0 deletions utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,29 @@ int red_recv_udp_pkt(
}
break;
}
#if defined(__OpenBSD__) || defined(__NetBSD__)
//TODO: support IPv6
else if (cmsg->cmsg_type == IP_RECVDSTADDR || cmsg->cmsg_type == IP_RECVDSTPORT){
struct sockaddr* cmsgaddr = (struct sockaddr*)CMSG_DATA(cmsg);
toaddr->ss_family=AF_INET;
struct sockaddr_in *toaddr_in = (struct sockaddr_in *)toaddr;
if (cmsg->cmsg_type == IP_RECVDSTADDR) {
memcpy(&toaddr_in->sin_addr, cmsgaddr, sizeof(struct in_addr));
} else if (cmsg->cmsg_type == IP_RECVDSTPORT) {
memcpy(&toaddr_in->sin_port, cmsgaddr, sizeof(in_port_t));
}
}
#endif
else {
log_error(LOG_WARNING, "unexepcted cmsg (level,type) = (%d,%d)",
cmsg->cmsg_level, cmsg->cmsg_type);
}
}
if (toaddr->ss_family == 0) {
#if !defined(__FreeBSD__) && !defined(__OpenBSD__) && !defined(__NetBSD__)
log_error(LOG_WARNING, "(SOL_IP, IP_ORIGDSTADDR) not found");
return -1;
#endif
}
}

Expand Down

0 comments on commit 82fa950

Please sign in to comment.