Support acls for UDP ASSOC, fix udppm via SOCKSv5
C/C++ CI Linux / ${{ matrix.target }} (ubuntu-24.04-arm) (push) Waiting to run
C/C++ CI Linux / ${{ matrix.target }} (ubuntu-latest) (push) Waiting to run
C/C++ CI MacOS / ${{ matrix.target }} (macos-15) (push) Waiting to run
C/C++ CI Windows / ${{ matrix.target }} (windows-2022) (push) Waiting to run
C/C++ CI cmake / ${{ matrix.target }} (macos-15) (push) Waiting to run
C/C++ CI cmake / ${{ matrix.target }} (ubuntu-24.04-arm) (push) Waiting to run
C/C++ CI cmake / ${{ matrix.target }} (ubuntu-latest) (push) Waiting to run
C/C++ CI cmake / ${{ matrix.target }} (windows-2022) (push) Waiting to run
C/C++ CI cmake / ubuntu-latest (wolfSSL) (push) Waiting to run

This commit is contained in:
Vladimir Dubrovin committed 2026-08-11 19:02:54 +03:00
1 parent ddb74a111c
commit bebc3894e2
14 files changed
+699 -124

No files matched your search

+18 -2
View File
@@ -36,6 +36,8 @@ int ACLmatches(struct ace* acentry, struct clientparam * param){
struct hostname * hstentry=NULL;
int i;
int match = 0;
int dstdep = 0;
int preauth = (param->preauth == 1 && acentry->action <= REDIRECT);
username = param->username?param->username:(unsigned char *)"-";
if(acentry->src) {
@@ -45,7 +47,10 @@ int ACLmatches(struct ace* acentry, struct clientparam * param){
}
if(!ipentry) return 0;
}
if((acentry->dst && (!SAISNULL(&param->req) || param->operation==BIND)) || (acentry->dstnames && param->hostname)) {
if(preauth && (acentry->dst || acentry->dstnames)) {
dstdep = 1;
}
else if((acentry->dst && (!SAISNULL(&param->req) || param->operation==BIND)) || (acentry->dstnames && param->hostname)) {
for(ipentry = acentry->dst; ipentry; ipentry = ipentry->next)
if(IPInentry((struct sockaddr *)&param->req, ipentry)) {
break;
@@ -93,7 +98,10 @@ int ACLmatches(struct ace* acentry, struct clientparam * param){
}
if(!ipentry && !hstentry) return 0;
}
if(acentry->ports && (*SAPORT(&param->req) || param->operation == BIND)) {
if(preauth && acentry->ports) {
dstdep = 1;
}
else if(acentry->ports && (*SAPORT(&param->req) || param->operation == BIND)) {
for (portentry = acentry->ports; portentry; portentry = portentry->next)
if(ntohs(*SAPORT(&param->req)) >= portentry->startport &&
ntohs(*SAPORT(&param->req)) <= portentry->endport) {
@@ -125,6 +133,10 @@ int ACLmatches(struct ace* acentry, struct clientparam * param){
}
}
if(acentry->weight && (acentry->weight < param->weight)) return 0;
if(dstdep) {
param->dstindep = 0;
return acentry->action != DENY;
}
return 1;
}
@@ -132,6 +144,7 @@ int ACLmatches(struct ace* acentry, struct clientparam * param){
int checkACL(struct clientparam * param){
struct ace* acentry;
if(param->preauth == 1) param->dstindep = 1;
if(!param->srv->acl) {
return 0;
}
@@ -150,6 +163,8 @@ int checkACL(struct clientparam * param){
if(param->redirected && acentry->chains && SAISNULL(&acentry->chains->addr) && !*SAPORT(&acentry->chains->addr)) {
continue;
}
param->lastace = acentry;
if(param->preauth) return 2;
if((param->operation == UDPASSOC)? (param->ctrlsocksrv != INVALID_SOCKET) : (param->remsock != INVALID_SOCKET)) {
return 0;
}
@@ -166,6 +181,7 @@ int checkACL(struct clientparam * param){
}
return res;
}
param->lastace = acentry;
return acentry->action;
}
}
+3 -1
View File
@@ -102,6 +102,7 @@ int cacheauth(struct clientparam * param){
*(SAFAMILY(&param->sinsl)) = ac.sinsl_family;
memcpy(SAADDR(&param->sinsl), ac.sinsl_addr, SAADDRLEN(&param->sinsl));
}
if(param->preauth == 1 && param->srv->acl) param->dstindep = 0;
return 0;
}
@@ -110,6 +111,7 @@ int doauth(struct clientparam * param){
struct auth *authfuncs;
int ret = 0;
if(param->preauth == 1 && !param->srv->acl) param->dstindep = 1;
for(authfuncs=param->srv->authfuncs; authfuncs; authfuncs=authfuncs->next){
res = authfuncs->authenticate?(*authfuncs->authenticate)(param):0;
if(!res) {
@@ -148,7 +150,7 @@ int doauth(struct clientparam * param){
if(ret > 9) return ret;
}
if(!res){
ret = alwaysauth(param);
ret = (param->preauth == 2)? 0 : alwaysauth(param);
if (param->afterauthfilters){
FILTER_ACTION action;
+9
View File
@@ -9,6 +9,15 @@
#include "proxy.h"
#ifdef __linux__
#include <sched.h>
int switch_ns(struct srvparam *srv, int target_fd) {
if(target_fd < 0) return 0;
if(srv->saved_nsfd >= 0 && setns(srv->saved_nsfd, CLONE_NEWNET)) return -1;
return setns(target_fd, CLONE_NEWNET);
}
#endif
char * copyright = COPYRIGHT;
+4
View File
@@ -204,6 +204,10 @@ extern int timeouts[12];
int sockmap(struct clientparam * param, int timeo, int usesplice);
int udpsockmap(struct clientparam * param, int timeo);
int udpbind(struct clientparam * param);
#ifdef __linux__
int switch_ns(struct srvparam *srv, int target_fd);
#endif
int socksend(struct clientparam *param, SOCKET sock, unsigned char * buf, int bufsize, int to);
int socksendto(struct clientparam *param, SOCKET sock, struct sockaddr * sin, unsigned char * buf, int bufsize, int to);
int sockrecvfrom(struct clientparam *param, SOCKET sock, struct sockaddr * sin, unsigned char * buf, int bufsize, int to);
+7 -1
View File
@@ -331,6 +331,9 @@ int MODULEMAINFUNC (int argc, char** argv){
" -s Use splice() - no filtering for data, off by default\n"
" -C connection closed by any of the sides terminates the session, by default\n"
" the session is kept until both sides close it (TCP half-close)\n"
" -U(0-3) (for socks) what to do when the destination changes within an UDP\n"
" association: 1 - log, 2 - authorize, 3 - both (default), 0 (same as -U) -\n"
" neither. The first destination is always authorized and logged\n"
#endif
"-g(GRACE_TRAFF,GRACE_NUM,GRACE_DELAY) - delay GRACE_DELAY milliseconds before polling if average polling size below GRACE_TRAFF bytes and GRACE_NUM read operations in single directions are detected within 1 second to minimize polling\n"
" -fFORMAT logging format (see documentation)\n"
@@ -601,6 +604,9 @@ int MODULEMAINFUNC (int argc, char** argv){
case 'C':
srv.halfclose = *(argv[i]+2)? atoi(argv[i]+2) : 0;
break;
case 'U':
srv.udpauth = *(argv[i]+2)? atoi(argv[i]+2) : 0;
break;
case 's':
#ifdef WITHSPLICE
if(isudp || srv.service == S_ADMIN)
@@ -1253,6 +1259,7 @@ void srvinit(struct srvparam * srv, struct clientparam *param){
srv->usesplice = 0;
#endif
srv->halfclose = 1;
srv->udpauth = 3;
memset(param, 0, sizeof(struct clientparam));
param->srv = srv;
param->version = srv->version;
@@ -1355,7 +1362,6 @@ void srvfree(struct srvparam * srv){
void freeparam(struct clientparam * param) {
if(param->res == 2) return;
if(param->srv){
if(param->srv->so.freefunc) param->srv->so.freefunc(param->sostate);
_3proxy_mutex_lock(&param->srv->counter_mutex);
+9 -23
View File
@@ -7,15 +7,6 @@
*/
#include "proxy.h"
#ifdef __linux__
#include <sched.h>
static int switch_ns(struct srvparam *srv, int target_fd) {
if(target_fd < 0) return 0;
if(srv->saved_nsfd >= 0 && setns(srv->saved_nsfd, CLONE_NEWNET)) return -1;
return setns(target_fd, CLONE_NEWNET);
}
#endif
#define RETURN(xxx) { param->res = xxx; goto CLEANRET; }
@@ -219,7 +210,11 @@ void * sockschild(struct clientparam* param) {
RETURN(997);
}
if((res = (*param->srv->authfunc)(param))) {
if(command == 3) param->preauth = 1;
res = (*param->srv->authfunc)(param);
param->preauth = 0;
if(command == 3 && res == 2) res = 0;
if(res) {
RETURN(res);
}
@@ -238,18 +233,9 @@ void * sockschild(struct clientparam* param) {
#endif
if(command == 3) {
#ifdef __linux__
if(switch_ns(param->srv, param->srv->o_nsfd)) {RETURN(11);}
#endif
if ((param->remsock=param->srv->so._socket(param->sostate, SASOCK(&param->req), SOCK_DGRAM, IPPROTO_UDP)) == INVALID_SOCKET) {RETURN (11);}
#ifdef _WIN32
{ unsigned long ul = 1; ioctlsocket(param->remsock, FIONBIO, &ul); }
#else
fcntl(param->remsock, F_SETFL, O_NONBLOCK | fcntl(param->remsock, F_GETFL));
#endif
if((res = udpbind(param))) {RETURN(res);}
}
if(command > 1) {
else if(command == 2) {
if(param->srv->so._bind(param->sostate, param->remsock,(struct sockaddr *)&param->sinsl,SASIZE(&param->sinsl))) {
*SAPORT(&param->sinsl) = 0;
if(param->srv->so._bind(param->sostate, param->remsock,(struct sockaddr *)&param->sinsl,SASIZE(&param->sinsl)))RETURN (12);
@@ -260,7 +246,8 @@ fflush(stderr);
}
sasize = SASIZE(&param->sinsl);
param->srv->so._getsockname(param->sostate, param->remsock, (struct sockaddr *)&param->sinsl, &sasize);
if(command == 3) {
}
if(command == 3) {
param->ctrlsock = param->clisock;
#ifdef __linux__
if(switch_ns(param->srv, param->srv->i_nsfd)) {RETURN(11);}
@@ -283,7 +270,6 @@ fprintf(stderr, "%hu binded to communicate with client\n",
);
fflush(stderr);
#endif
}
}
param->res = 0;
+5 -1
View File
@@ -552,6 +552,7 @@ struct srvparam {
int usesplice;
#endif
int halfclose;
int udpauth;
unsigned bufsize;
unsigned authcachetype, authcachetime;
unsigned logdumpsrv, logdumpcli;
@@ -644,7 +645,9 @@ struct clientparam {
paused,
version,
connlim,
predatdone;
predatdone,
preauth,
dstindep;
unsigned char *hostname,
*username,
@@ -679,6 +682,7 @@ struct clientparam {
PROXYSOCKADDRTYPE udp_relay[3];
int udp_nhops;
struct ace *lastace;
time_t time_start;
};
+5 -12
View File
@@ -36,26 +36,19 @@ void * udppmchild(struct clientparam* param) {
int i;
int len = 0;
if(parsehostname((char *)param->srv->target, param, ntohs(param->srv->targetport))) { RETURN(201) }
if(parsehostname((char *)param->srv->target, param, ntohs(param->srv->targetport))) { RETURN(100) }
#ifndef NOIPV6
memcpy(&param->sinsl, *SAFAMILY(&param->req) == AF_INET6 ? (struct sockaddr *)&param->srv->extsa6 : (struct sockaddr *)&param->srv->extsa, SASIZE(&param->req));
param->sinsl = *SAFAMILY(&param->req) == AF_INET6? param->srv->extsa6 : param->srv->extsa;
#else
memcpy(&param->sinsl, (struct sockaddr *)&param->srv->extsa, SASIZE(&param->req));
param->sinsl = param->srv->extsa;
#endif
*SAPORT(&param->sinsl) = 0;
param->remsock = param->srv->so._socket(param->srv->so.state, SASOCK(&param->sinsl), SOCK_DGRAM, IPPROTO_UDP);
if(param->remsock == INVALID_SOCKET) { RETURN(202); }
if(param->srv->so._bind(param->srv->so.state, param->remsock, (struct sockaddr *)&param->sinsl, SASIZE(&param->sinsl))) { RETURN(203); }
#ifdef _WIN32
{ unsigned long ul2 = 1; ioctlsocket(param->remsock, FIONBIO, &ul2); }
#else
fcntl(param->remsock, F_SETFL, O_NONBLOCK | fcntl(param->remsock, F_GETFL));
#endif
memcpy(&param->sinsr, &param->req, sizeof(param->req));
param->sinsr = param->req;
param->operation = UDPASSOC;
authres = (*param->srv->authfunc)(param);
if(authres) { RETURN(authres); }
if((authres = udpbind(param))) { RETURN(authres); }
if(!param->srv->s_option)hashadd(&udp_table, param, &param, MAX_COUNTER_TIME);
if(!param->srvbuf){
if(!(param->srvbuf = malloc(UDPBUFSIZE)))RETURN(11);
+270 -84
View File
@@ -8,6 +8,10 @@
#include "proxy.h"
/* space reserved in front of the received datagram to prepend the headers
of the second and the third hop of a SOCKS5 UDP chain */
#define UDPHDROFF 48
int socks5_udp_build_hdr(unsigned char *buf, PROXYSOCKADDRTYPE *addr)
{
buf[0] = buf[1] = buf[2] = 0;
@@ -48,69 +52,192 @@ static int socks5_udp_skip_hdr(unsigned char *buf, int len)
* param->srv->s_option non-zero: return after first datagram sent to client
* param->ctrlsock TCP control socket from the client; INVALID_SOCKET if none.
*/
struct udppoll {
struct pollfd fds[4];
int nfds;
int cli, ctrl, ctrlsrv;
};
static void udpfds(struct clientparam *param, struct udppoll *p)
{
memset(p->fds, 0, sizeof(p->fds));
p->nfds = 0;
p->cli = p->ctrl = p->ctrlsrv = -1;
p->fds[p->nfds].fd = param->remsock; /* always index 0 */
p->fds[p->nfds].events = POLLIN;
p->nfds++;
if (!param->waitserver64) {
p->fds[p->nfds].fd = param->clisock;
p->fds[p->nfds].events = POLLIN;
p->cli = p->nfds++;
}
if (param->ctrlsock != INVALID_SOCKET) {
p->fds[p->nfds].fd = param->ctrlsock;
p->fds[p->nfds].events = POLLIN;
p->ctrl = p->nfds++;
}
if (param->ctrlsocksrv != INVALID_SOCKET) {
p->fds[p->nfds].fd = param->ctrlsocksrv;
p->fds[p->nfds].events = POLLIN;
p->ctrlsrv = p->nfds++;
}
}
static void udplog(struct clientparam *param)
{
unsigned char buf[400];
int len;
if(!param->srv->logfunc) return;
len = sprintf((char *)buf, "UDPMAP ");
if(param->hostname) len += sprintf((char *)buf + len, "%.256s", param->hostname);
else len += myinet_ntop(*SAFAMILY(&param->req), SAADDR(&param->req), (char *)buf + len, 64);
sprintf((char *)buf + len, ":%hu", ntohs(*SAPORT(&param->req)));
param->srv->logfunc(param, buf);
}
int udpbind(struct clientparam *param)
{
SOCKET s;
SASIZETYPE sasize;
#ifdef __linux__
if (switch_ns(param->srv, param->srv->o_nsfd)) return 11;
#endif
s = param->srv->so._socket(param->sostate, SASOCK(&param->sinsl), SOCK_DGRAM, IPPROTO_UDP);
#ifdef __linux__
if (switch_ns(param->srv, param->srv->i_nsfd)) {
if (s != INVALID_SOCKET) param->srv->so._closesocket(param->sostate, s);
return 11;
}
#endif
if (s == INVALID_SOCKET) return 11;
#ifdef _WIN32
{ unsigned long ul = 1; ioctlsocket(s, FIONBIO, &ul); }
#else
fcntl(s, F_SETFL, O_NONBLOCK | fcntl(s, F_GETFL));
#endif
param->remsock = s;
if (param->srv->so._bind(param->sostate, param->remsock,
(struct sockaddr *)&param->sinsl, SASIZE(&param->sinsl))) {
*SAPORT(&param->sinsl) = 0;
if (param->srv->so._bind(param->sostate, param->remsock,
(struct sockaddr *)&param->sinsl, SASIZE(&param->sinsl))) {
param->srv->so._closesocket(param->sostate, param->remsock);
param->remsock = INVALID_SOCKET;
return 12;
}
}
sasize = SASIZE(&param->sinsl);
param->srv->so._getsockname(param->sostate, param->remsock,
(struct sockaddr *)&param->sinsl, &sasize);
return 0;
}
static void udpreset(struct clientparam *param)
{
param->redirected = 0;
param->udp_nhops = 0;
memset(param->udp_relay, 0, sizeof(param->udp_relay));
#ifndef NOIPV6
param->sinsl = *SAFAMILY(&param->req) == AF_INET6? param->srv->extsa6 : param->srv->extsa;
#else
param->sinsl = param->srv->extsa;
#endif
param->sinsr = param->req;
}
/*
* Authorize param->req and (re)build the server side of the association:
* a parent proxy chain and/or an external address may be selected by the ACL.
* Returns the authorization result. param->remsock is INVALID_SOCKET if the
* socket can not be created, in this case the association can not continue.
*/
static int udpreconnect(struct clientparam *param)
{
int res;
if (param->ctrlsocksrv != INVALID_SOCKET) {
param->srv->so._closesocket(param->sostate, param->ctrlsocksrv);
param->ctrlsocksrv = INVALID_SOCKET;
}
if (param->remsock != INVALID_SOCKET) {
param->srv->so._closesocket(param->sostate, param->remsock);
param->remsock = INVALID_SOCKET;
}
udpreset(param);
res = (*param->srv->authfunc)(param);
if (res) {
if (param->ctrlsocksrv != INVALID_SOCKET) {
param->srv->so._closesocket(param->sostate, param->ctrlsocksrv);
param->ctrlsocksrv = INVALID_SOCKET;
}
if (param->remsock != INVALID_SOCKET) {
param->srv->so._closesocket(param->sostate, param->remsock);
param->remsock = INVALID_SOCKET;
}
udpreset(param);
}
if (udpbind(param)) return res? res : 11;
return res;
}
int udpsockmap(struct clientparam *param, int timeo)
{
PROXYSOCKADDRTYPE sin;
PROXYSOCKADDRTYPE cliaddr;
PROXYSOCKADDRTYPE from;
struct pollfd fds[4];
PROXYSOCKADDRTYPE lastdst;
struct udppoll p;
SASIZETYPE sasize;
int len, res, nfds;
int nhops = param->udp_nhops;
int clisock_idx = -1, ctrlsock_idx = -1, ctrlsocksrv_idx = -1;
int len, res, nhops;
int firstpacket = 1;
if(param->srv->service == S_UDPPM) nhops++;
int havedst = 0, lastres = 0;
char lastname[256] = "";
struct ace *lastace = NULL;
if (param->srvbufsize < UDPBUFSIZE) {
unsigned char *newbuf = realloc(param->srvbuf, UDPBUFSIZE);
if (!newbuf) return 21;
param->srvbuf = newbuf;
param->srvbufsize = UDPBUFSIZE;
}
sin = param->sincr;
/* Build poll array once — sockets don't change across iterations */
nfds = 0;
fds[nfds].fd = param->remsock; /* always index 0 */
fds[nfds].events = POLLIN;
nfds++;
if (!param->waitserver64) {
fds[nfds].fd = param->clisock;
fds[nfds].events = POLLIN;
clisock_idx = nfds++;
cliaddr = param->sincr;
if(param->ctrlsock != INVALID_SOCKET){
sasize = sizeof(cliaddr);
param->srv->so._getpeername(param->sostate, param->ctrlsock, (struct sockaddr *)&cliaddr, &sasize);
}
sin = cliaddr;
if (param->ctrlsock != INVALID_SOCKET) {
fds[nfds].fd = param->ctrlsock;
fds[nfds].events = POLLIN;
ctrlsock_idx = nfds++;
}
if (param->ctrlsocksrv != INVALID_SOCKET) {
fds[nfds].fd = param->ctrlsocksrv;
fds[nfds].events = POLLIN;
ctrlsocksrv_idx = nfds++;
}
nhops = param->udp_nhops;
if(param->srv->service == S_UDPPM) nhops++;
udpfds(param, &p);
for (;;) {
res = param->srv->so._poll(param->sostate, fds, nfds, timeo * 1000);
res = param->srv->so._poll(param->sostate, p.fds, p.nfds, timeo * 1000);
if (res < 0) return 481;
if (res == 0) return 92;
/* datagram from client */
if (clisock_idx >= 0 && fds[clisock_idx].revents) {
int recvoff = 0, k;
if (p.cli >= 0 && p.fds[p.cli].revents) {
unsigned char *base = param->srvbuf + UDPHDROFF;
PROXYSOCKADDRTYPE dst;
char dstnamebuf[256];
char *dstname = NULL;
int i, k, w, off;
sasize = sizeof(sin);
for (k = 1; k < nhops; k++)
recvoff += 4 + (int)SAADDRLEN(&param->udp_relay[k]) + 2;
len = param->srv->so._recvfrom(param->sostate, param->clisock,
(char *)param->srvbuf + recvoff, UDPBUFSIZE - recvoff,
(char *)base, UDPBUFSIZE - UDPHDROFF,
0, (struct sockaddr *)&sin, &sasize);
if (len < 0 && (errno == EAGAIN || errno == EINTR)) continue;
if (len <= 0) return 482;
if (SAADDRLEN(&sin) != SAADDRLEN(&param->sincr) ||
memcmp(SAADDR(&sin), SAADDR(&param->sincr), SAADDRLEN(&sin)))
if (SAADDRLEN(&sin) != SAADDRLEN(&cliaddr) ||
memcmp(SAADDR(&sin), SAADDR(&cliaddr), SAADDRLEN(&sin)))
continue;
if (firstpacket) {
if (!SAISNULL(&param->req) && *SAPORT(&param->req) &&
@@ -118,70 +245,129 @@ int udpsockmap(struct clientparam *param, int timeo)
!memcmp(SAADDR(&param->req), SAADDR(&sin), SAADDRLEN(&param->req)) &&
memcmp(SAPORT(&param->req), SAPORT(&sin), 2))
continue;
param->sincr = sin;
cliaddr = sin;
firstpacket = 0;
} else if (memcmp(SAPORT(&sin), SAPORT(&param->sincr), 2)) {
} else if (memcmp(SAPORT(&sin), SAPORT(&cliaddr), 2)) {
continue;
}
if(param->bandlimfunc && (*param->bandlimfunc)(param, 0, len)) continue;
if (nhops == 0) {
int i;
if (len < 10 || param->srvbuf[0] || param->srvbuf[1] || param->srvbuf[2])
return 483;
switch (param->srvbuf[3]) {
case 1:
*SAFAMILY(&param->sinsr) = AF_INET;
memcpy(SAADDR(&param->sinsr), param->srvbuf + 4, 4);
i = 8;
break;
case 4:
if (len < 22) return 484;
*SAFAMILY(&param->sinsr) = AF_INET6;
memcpy(SAADDR(&param->sinsr), param->srvbuf + 4, 16);
i = 20;
break;
case 3: {
int sz = param->srvbuf[4], j;
if (len < 7 + sz) return 485;
for (j = 4; j < 4 + sz; j++) param->srvbuf[j] = param->srvbuf[j + 1];
param->srvbuf[4 + sz] = 0;
i = 5 + sz;
if (!getip46(param->srv->family, param->srvbuf + 4,
(struct sockaddr *)&param->sinsr))
return 100;
break;
if (len < 10 || base[0] || base[1] || base[2]) return 483;
memset(&dst, 0, sizeof(dst));
switch (base[3]) {
case 1:
*SAFAMILY(&dst) = AF_INET;
memcpy(SAADDR(&dst), base + 4, 4);
i = 8;
break;
case 4:
if (len < 22) return 484;
*SAFAMILY(&dst) = AF_INET6;
memcpy(SAADDR(&dst), base + 4, 16);
i = 20;
break;
case 3: {
int sz = base[4];
if (len < 7 + sz) return 485;
memcpy(dstnamebuf, base + 5, sz);
dstnamebuf[sz] = 0;
dstname = dstnamebuf;
i = 5 + sz;
if (!getip46(param->srv->family, (unsigned char *)dstnamebuf,
(struct sockaddr *)&dst)) {
if (!nhops) return 100;
memset(&dst, 0, sizeof(dst));
*SAFAMILY(&dst) = AF_INET;
}
default: return 997;
break;
}
default: return 997;
}
memcpy(SAPORT(&dst), base + i, 2);
i += 2;
if (!havedst
|| SAADDRLEN(&lastdst) != SAADDRLEN(&dst)
|| memcmp(SAADDR(&lastdst), SAADDR(&dst), SAADDRLEN(&dst))
|| memcmp(SAPORT(&lastdst), SAPORT(&dst), 2)
|| strncmp(lastname, dstname? dstname : "", sizeof(lastname) - 1)) {
int ares = 0, reconnect = 0;
if ((param->srv->udpauth & 1) && havedst && !lastres) {
PROXYSOCKADDRTYPE newdst = dst;
param->req = lastdst;
if (param->hostname) free(param->hostname);
param->hostname = *lastname? (unsigned char *)strdup(lastname) : NULL;
udplog(param);
dst = newdst;
}
memcpy(SAPORT(&param->sinsr), param->srvbuf + i, 2);
i += 2;
if (param->hostname) free(param->hostname);
param->hostname = dstname? (unsigned char *)strdup(dstname) : NULL;
param->req = dst;
if (!nhops) param->sinsr = dst;
if (!havedst) reconnect = 1;
else if ((param->srv->udpauth & 2) && !param->dstindep) {
param->preauth = 2;
ares = (*param->srv->authfunc)(param);
param->preauth = 0;
if (ares == 2) {
if (param->lastace != lastace) reconnect = 1;
ares = 0;
}
else if (!ares && (param->redirected ||
(lastace && lastace->chains))) reconnect = 1;
}
if (reconnect) {
ares = udpreconnect(param);
if (param->remsock == INVALID_SOCKET) return ares;
nhops = param->udp_nhops;
if(param->srv->service == S_UDPPM) nhops++;
udpfds(param, &p);
}
lastres = ares;
lastdst = dst;
strncpy(lastname, dstname? dstname : "", sizeof(lastname) - 1);
lastname[sizeof(lastname) - 1] = 0;
havedst = 1;
if (!ares) lastace = param->lastace;
else if (param->srv->udpauth & 1) {
param->res = ares;
udplog(param);
param->res = 0;
}
}
if (lastres) continue;
if (!nhops) {
if (len > i) {
param->srv->so._sendto(param->sostate, param->remsock,
(char *)param->srvbuf + i, len - i, 0,
(struct sockaddr *)&param->sinsr, SASIZE(&param->sinsr));
(char *)base + i, len - i, 0,
(struct sockaddr *)&param->sinsr, SASIZE(&param->sinsr));
param->statscli64 += (len - i);
param->nwrites++;
}
} else {
int off = 0;
off = 0;
for (k = 1; k < nhops; k++)
off += socks5_udp_build_hdr(param->srvbuf + off, &param->udp_relay[k]);
off += 4 + (int)SAADDRLEN(&param->udp_relay[k]) + 2;
for (k = 1, w = UDPHDROFF - off; k < nhops; k++)
w += socks5_udp_build_hdr(param->srvbuf + w, &param->udp_relay[k]);
param->srv->so._sendto(param->sostate, param->remsock,
(char *)param->srvbuf, off + len, 0,
(struct sockaddr *)&param->udp_relay[0], SASIZE(&param->udp_relay[0]));
(char *)param->srvbuf + (UDPHDROFF - off), off + len, 0,
(struct sockaddr *)&param->udp_relay[0], SASIZE(&param->udp_relay[0]));
param->statscli64 += len;
param->nwrites++;
}
}
/* datagram from server / parent relay */
if (fds[0].revents) {
int hdrsize = (nhops == 0) ? 4 + (int)SAADDRLEN(&param->sinsr) + 2 : 0;
if (p.fds[0].revents) {
int hdrsize = (nhops == 0) ? 4 + (int)SAADDRLEN(&param->sinsl) + 2 : 0;
int sendoff = 0, sendlen;
sasize = sizeof(from);
if (hdrsize > UDPBUFSIZE) return 468;
len = param->srv->so._recvfrom(param->sostate, param->remsock,
(char *)param->srvbuf + hdrsize, UDPBUFSIZE - hdrsize, 0,
(struct sockaddr *)&from, &sasize);
@@ -201,9 +387,9 @@ int udpsockmap(struct clientparam *param, int timeo)
sendlen = len;
if (nhops == 0) {
param->srvbuf[0] = param->srvbuf[1] = param->srvbuf[2] = 0;
param->srvbuf[3] = (*SAFAMILY(&param->sinsr) == AF_INET) ? 1 : 4;
memcpy(param->srvbuf + 4, SAADDR(&param->sinsr), SAADDRLEN(&param->sinsr));
memcpy(param->srvbuf + 4 + SAADDRLEN(&param->sinsr), SAPORT(&param->sinsr), 2);
param->srvbuf[3] = (*SAFAMILY(&from) == AF_INET) ? 1 : 4;
memcpy(param->srvbuf + 4, SAADDR(&from), SAADDRLEN(&param->sinsl));
memcpy(param->srvbuf + 4 + SAADDRLEN(&param->sinsl), SAPORT(&from), 2);
sendlen = len + hdrsize;
} else if (nhops >= 2) {
int off = 0, k;
@@ -219,13 +405,13 @@ int udpsockmap(struct clientparam *param, int timeo)
}
if (sendlen > 0)
param->srv->so._sendto(param->sostate, param->clisock,
(char *)param->srvbuf + sendoff, sendlen, 0,
(struct sockaddr *)&sin, SASIZE(&sin));
(char *)param->srvbuf + sendoff, sendlen, 0,
(struct sockaddr *)&sin, SASIZE(&sin));
if (param->srv->s_option && param->srv->service == S_UDPPM) return 0;
}
if ((ctrlsock_idx >= 0 && fds[ctrlsock_idx].revents) ||
(ctrlsocksrv_idx >= 0 && fds[ctrlsocksrv_idx].revents)) return 0;
if ((p.ctrl >= 0 && p.fds[p.ctrl].revents) ||
(p.ctrlsrv >= 0 && p.fds[p.ctrlsrv].revents)) return 0;
}
return 0;
}