Force Proxy to any Windows/MacOS/Linux Application TCP and UDP traffic
C ccybersecurityhttp3httpproxylinuxlinux-proxymacosmacos-appmacos-proxyproxifierproxifier-alternativeproxyclienttcpthick-clientudpwindivertwindows
fix(linux/relay): source-address check, leak audit, cheaper UDP path, leaner code
anof-cybercaace65e8423
diff --git a/Linux/src/pb_relay.c b/Linux/src/pb_relay.cindex e1de2bb..2fc833e 100644--- a/Linux/src/pb_relay.c+++ b/Linux/src/pb_relay.c@@ -1,8 +1,5 @@-// pb_relay.c - userspace relay for the eBPF backend.-//-// TCP: acceptor thread -> bounded handshake worker pool (blocking steps with deadlines) -> epoll data threads.-// UDP: one epoll thread owning every session; SOCKS5 UDP ASSOCIATE setup runs in the worker pool.-// Ownership: a conn/session is only touched by the thread that owns its epoll; workers hand objects over via queues.+// Userspace relay for the eBPF backend: TCP (SOCKS5 / HTTP CONNECT / DIRECT) and UDP (SOCKS5 UDP ASSOCIATE / DIRECT).+// Every conn/session is owned by exactly one epoll thread; handshake workers hand finished objects over through queues.#ifndef _GNU_SOURCE#define _GNU_SOURCE#endif@@ -28,7 +25,7 @@#include "pb_internal.h"-#define SCRATCH_SZ 65536 // per data thread read buffer; idle connections hold no buffer+#define SCRATCH_SZ 65536#define HDR_MAX 8192#define NWORKERS 16#define MAX_DATA 8@@ -39,15 +36,14 @@#define UDP_QMAX 32#define UDP_QBYTES (256 * 1024)#define UDP_MAXDG 65536+#define UDP_BATCH 16+#define FLOW_TTL_MS 1000#define DC_SIZE 1024#define SESS_BUCKETS 256#define DIRECT_ID 0xFFFFFFFFustatic char TAG_STOP, TAG_WAKE, TAG_UDP, TAG_TIMER;-// ----------------------------------------------------------------------------------------------// Utilities-// ---------------------------------------------------------------------------------------------static int64_t now_ms(void){struct timespec ts;@@ -105,7 +101,6 @@ static int wait_fd(int fd, short ev, int64_t deadline, int stopfd)}}-// Blocking-with-deadline I/O context for handshakes (fd is non-blocking).struct io {int fd;int64_t dl;@@ -145,7 +140,6 @@ static int io_send(struct io *x, const void *buf, size_t n)return 0;}-// Receives 1..n bytes.static ssize_t io_recv(struct io *x, void *buf, size_t n){for (;;) {@@ -175,9 +169,6 @@ static int io_recv_exact(struct io *x, void *buf, size_t n)return 0;}-// ----------------------------------------------------------------------------------------------// Connecting-// ---------------------------------------------------------------------------------------------static void tune_sock(int fd){int one = 1, idle = 60, intvl = 15, cnt = 4;@@ -205,10 +196,8 @@ static int connect_addr(const struct sockaddr *sa, socklen_t len, int64_t dl, inreturn fd;}-// ---------------------------------------------------------------------------------------------// Hostname resolution (IPv4): at most RES_MAX_THREADS short-lived detached lookups, deadline-bounded for the waiter,// results cached (fresh for 60 s; last good answer reused for up to 10 min when re-resolution fails).-// ---------------------------------------------------------------------------------------------#define RES_MAX_THREADS 4#define RES_DEADLINE_MS 5000#define RES_TTL_MS 60000@@ -254,7 +243,7 @@ static void *res_thread(void *arg)if (gr == 0 && res && res->ai_family == AF_INET) {r->addr = ((struct sockaddr_in *)res->ai_addr)->sin_addr;struct rescache *c = res_cache_find(r->name);- if (!c) { // reuse a free slot, else evict the oldest+ if (!c) {c = &res_cache[0];for (int i = 0; i < RES_CACHE_N; i++) {if (!res_cache[i].used) { c = &res_cache[i]; break; }@@ -304,7 +293,7 @@ static int resolve_v4(const char *name, int64_t dl, int stopfd, struct in_addr *pthread_attr_setstacksize(&at, 256 * 1024);if (r) {snprintf(r->name, sizeof r->name, "%s", name);- r->refs = 2; // thread + this waiter+ r->refs = 2;r->next = res_inflight; res_inflight = r;res_threads++;if (pthread_create(&th, &at, res_thread, r) != 0) {@@ -336,7 +325,7 @@ static int resolve_v4(const char *name, int64_t dl, int stopfd, struct in_addr *res_unref(r);}if (rc != 0) {- c = res_cache_find(name); // the lookup may have refreshed it; else try a stale answer+ c = res_cache_find(name);if (c && now_ms() - c->when < RES_STALE_MS) { *out = c->addr; rc = 0; }}pthread_mutex_unlock(&res_mu);@@ -362,7 +351,7 @@ static int connect_host(const char *host, uint16_t port, int64_t dl, int stopfd,s->sin6_family = AF_INET6; s->sin6_port = htons(port); s->sin6_addr = a6;sl[0] = sizeof *s; n = 1;} else {- return -1; // resolve_v4 filled err+ return -1;}int eno = ETIMEDOUT;for (int i = 0; i < n; i++) {@@ -376,9 +365,6 @@ static int connect_host(const char *host, uint16_t port, int64_t dl, int stopfd,return -1;}-// ----------------------------------------------------------------------------------------------// SOCKS5 / HTTP CONNECT handshakes-// ---------------------------------------------------------------------------------------------struct s5dst { uint8_t atyp; uint8_t addr[16]; char host[256]; uint16_t port; };struct s5bnd { uint8_t atyp; uint8_t addr[16]; uint16_t port; };@@ -405,7 +391,6 @@ static void s5dst_from_orig(struct s5dst *d, const struct pb_orig *o)else { d->atyp = 4; memcpy(d->addr, o->addr, 16); }}-// cmd: 1 = CONNECT, 3 = UDP ASSOCIATEstatic int socks5_request(struct io *x, const struct pb_proxy *p, uint8_t cmd, const struct s5dst *d, struct s5bnd *b){bool auth = p->user[0] != 0;@@ -484,7 +469,6 @@ static void b64(const char *in, size_t n, char *out)out[o] = 0;}-// host: IP literal (v6 without brackets) or hostname. Bytes received after the response headers go to left/leftn.static int http_connect(struct io *x, const struct pb_proxy *p, const char *host, uint16_t port, uint8_t *left, size_t *leftn){char hp[300];@@ -530,17 +514,14 @@ static int http_connect(struct io *x, const struct pb_proxy *p, const char *hostreturn 0;}-// ----------------------------------------------------------------------------------------------// Relay state-// ---------------------------------------------------------------------------------------------struct conn {struct conn *prev, *next;- int fd[2]; // 0 = client, 1 = upstream+ int fd[2];size_t off[2], len[2]; // pend[d]: unsent tail of bytes read from fd[d] (heap, freed once drained)uint8_t *pend[2];bool dns; // original destination port 53: upstream->client is parsed as TCP-DNSbool doff; // stopped observing (malformed stream)- uint8_t *dbuf; // bytes of the current, not yet complete, DNS message(s)+ uint8_t *dbuf;size_t dhave, dcap;uint32_t mask[2];bool eof[2], shut[2], dead;@@ -559,7 +540,7 @@ struct dthread {int ep, wake;pthread_mutex_t mu;struct conn *inq_head, *inq_tail;- struct conn *conns; // owned by thread+ struct conn *conns;struct conn *grave;uint8_t *scratch;int pfd[2]; // reusable splice pipe; always empty between connections@@ -588,13 +569,13 @@ struct sess {struct sess *hnext, *prev, *next, *dnext;struct sockaddr_in cli;uint32_t id, flow_be; // id = proxy id or DIRECT_ID; flow_be only for DIRECT sessions+ uint64_t sid;int kind, state;int up_fd, ctl_fd;- int family; // DIRECT: family of original destinationbool connected; // DIRECT: v6 sockets are connected; v4 are not (replies from any port of the host are accepted)- struct sockaddr_storage dst; // DIRECT destination+ struct sockaddr_storage dst;socklen_t dstlen;- uint16_t dport; // DIRECT: original destination port (53 => DNS tap)+ uint16_t dport;bool dead, ok, warned;int64_t last, fail_until;struct pb_proxy proxy;@@ -607,14 +588,14 @@ struct sess {size_t qbytes;};-struct dce { // per-(client, flow ip) decision cache+struct dce {bool used;struct sockaddr_in cli;- uint32_t flow_be;- uint8_t addr[16];- uint16_t port;- int action;- int64_t last;+ uint32_t flow_be, pid;+ struct pb_orig o;+ int action, fidx;+ int64_t looked, last;+ uint64_t sid;};static struct {@@ -638,17 +619,21 @@ static struct {struct job *jhead, *jtail;int jcount;- pthread_mutex_t dmu; // UDP setup completion queue+ pthread_mutex_t dmu;struct sess *done;- // UDP thread-confined statestruct sess *sess_head;struct sess *buckets[SESS_BUCKETS];int nsess;struct sess *sgrave;struct dce *dc;- uint8_t *rxbuf, *txbuf;+ uint8_t *rxbuf;int64_t drop_count, drop_logged;+ uint64_t next_sid;+ struct mmsghdr *mm;+ struct iovec *mmiov;+ struct sockaddr_in *mmname;+ char (*mmctl)[CMSG_SPACE(sizeof(struct in_pktinfo))];} R = { .life = PTHREAD_MUTEX_INITIALIZER, .jmu = PTHREAD_MUTEX_INITIALIZER, .jcv = PTHREAD_COND_INITIALIZER,.dmu = PTHREAD_MUTEX_INITIALIZER, .lfd = -1, .ufd = -1, .stopfd = -1, .tmfd = -1, .uep = -1, .uwake = -1 };@@ -657,9 +642,6 @@ static void proxy_desc(const struct pb_proxy *p, char *out, size_t n)snprintf(out, n, "%s %s:%u", p->type == PROXY_TYPE_SOCKS5 ? "SOCKS5" : "HTTP", p->host, p->port);}-// ----------------------------------------------------------------------------------------------// Job queue / worker pool-// ---------------------------------------------------------------------------------------------static int job_push(int kind, int fd, const struct pb_orig *o, struct sess *s){struct job *j = calloc(1, sizeof *j);@@ -695,7 +677,7 @@ static void conn_handoff(int cfd, int ufd, const uint8_t *left, size_t leftn, bo}unsigned start = atomic_fetch_add(&R.rr, 1);struct dthread *t = &R.data[start % R.ndata];- for (int i = 1; i < R.ndata; i++) { // least-loaded, ties go to the round-robin choice+ for (int i = 1; i < R.ndata; i++) {struct dthread *u = &R.data[(start + i) % R.ndata];if (atomic_load(&u->load) < atomic_load(&t->load)) t = u;}@@ -714,13 +696,6 @@ static void conn_handoff(int cfd, int ufd, const uint8_t *left, size_t leftn, boeventfd_write(t->wake, 1);}-static void abort_close(int fd)-{- struct linger l = { 1, 0 };- setsockopt(fd, SOL_SOCKET, SO_LINGER, &l, sizeof l);- close(fd);-}-static void tcp_job_run(struct job *j){struct pb_orig *o = &j->o;@@ -738,7 +713,7 @@ static void tcp_job_run(struct job *j)action = d.action; pid = d.proxy_id;}if (action == PB_ACT_DIRECT) {- pb_core_flow_event(o, PB_ACT_DIRECT, "DIRECT");+ pb_core_flow_event(o, PB_ACT_DIRECT);struct sockaddr_storage ss;socklen_t sl = orig_to_sa(o, &ss);int eno = 0;@@ -747,11 +722,11 @@ static void tcp_job_run(struct job *j)} else if (action == PB_ACT_PROXY) {struct pb_proxy px;if (pb_core_get_proxy(pid, &px) != 0) {- pb_core_flow_event(o, PB_ACT_PROXY, "unknown proxy");+ pb_core_flow_event(o, PB_ACT_PROXY);snprintf(err, sizeof err, "no proxy configured (id %u) for %s", pid, dst);} else {proxy_desc(&px, desc, sizeof desc);- pb_core_flow_event(o, PB_ACT_PROXY, desc);+ pb_core_flow_event(o, PB_ACT_PROXY);char cerr[200];up = connect_host(px.host, px.port, dl, R.stopfd, cerr, sizeof cerr);if (up < 0) {@@ -775,8 +750,9 @@ static void tcp_job_run(struct job *j)}}} else {- pb_core_flow_event(o, PB_ACT_BLOCK, "BLOCK");- abort_close(cfd);+ pb_core_flow_event(o, PB_ACT_BLOCK);+ setsockopt(cfd, SOL_SOCKET, SO_LINGER, &(struct linger){ 1, 0 }, sizeof(struct linger));+ close(cfd);atomic_fetch_sub(&R.nconn, 1);return;}@@ -858,9 +834,6 @@ static void *worker_main(void *arg)}}-// ----------------------------------------------------------------------------------------------// Acceptor-// ---------------------------------------------------------------------------------------------static void *acceptor_main(void *arg){(void)arg;@@ -879,15 +852,14 @@ static void *acceptor_main(void *arg)struct pollfd s = { .fd = R.stopfd, .events = POLLIN };poll(&s, 1, 200);}- break; // EAGAIN or error: back to poll+ break;}struct pb_orig o;- if (pb_core_take_tcp_orig(ntohs(pa.sin_port), &o) != 0) {- pb_core_log("relay: no original destination for client port %u, closing", ntohs(pa.sin_port));+ if (pb_core_take_tcp_orig(pa.sin_addr.s_addr, ntohs(pa.sin_port), &o) != 0) {+ pb_core_log("relay: no original destination for client %s:%u, closing", inet_ntoa(pa.sin_addr), ntohs(pa.sin_port));close(fd);continue;}- tune_sock(fd);if (atomic_fetch_add(&R.nconn, 1) >= R.cfg.max_tcp_conns) {atomic_fetch_sub(&R.nconn, 1);int64_t t = now_ms();@@ -906,16 +878,12 @@ static void *acceptor_main(void *arg)return NULL;}-// ----------------------------------------------------------------------------------------------// TCP data threads-// ---------------------------------------------------------------------------------------------static void pipe_discard(int fd){char b[4096];while (read(fd, b, sizeof b) > 0) {}}-// Reads `n` bytes the pipe holds into a fresh heap tail for direction d.static int spill_from_pipe(struct conn *c, int d, int pipe_rd, size_t n){c->pend[d] = malloc(n);@@ -955,7 +923,6 @@ static int pump_splice(struct dthread *t, struct conn *c, int d)}// Upstream->client for TCP-DNS: only whole messages (after pb_core_dns_observe returned) are forwarded.-// Returns 1 progress, 0 none, -1 fatal.static int dns_pump_up(struct conn *c, uint8_t *scratch){int progress = 0;@@ -1091,7 +1058,6 @@ static void conn_close(struct dthread *t, struct conn *c)close(c->fd[1]);c->dead = true;conn_bufs_free(c);- c->pend[0] = c->pend[1] = NULL;if (c->prev) c->prev->next = c->next; else t->conns = c->next;if (c->next) c->next->prev = c->prev;c->next = t->grave;@@ -1101,7 +1067,6 @@ static void conn_close(struct dthread *t, struct conn *c)atomic_fetch_sub(&t->load, 1);}-// Returns after pump + epoll mask refresh; closes the conn when done/failed.static void conn_service(struct dthread *t, struct conn *c){int r = conn_pump(t, c);@@ -1121,7 +1086,8 @@ static void dthread_adopt(struct dthread *t)if (t->conns) t->conns->prev = c;t->conns = c;c->last = now_ms();- conn_service(t, c);+ if (c->len[1] || c->dhave) conn_service(t, c);+ else if (conn_update(t, c) != 0) conn_close(t, c);c = n;}}@@ -1169,9 +1135,6 @@ done:return NULL;}-// ----------------------------------------------------------------------------------------------// UDP-// ---------------------------------------------------------------------------------------------static void udp_droplog(const char *why){R.drop_count++;@@ -1235,6 +1198,7 @@ static struct sess *sess_new(const struct sockaddr_in *cli, uint32_t id, uint32_s->ref[0].s = s->ref[1].s = s;s->ref[0].which = 0; s->ref[1].which = 1;s->last = now_ms();+ s->sid = ++R.next_sid;uint32_t h = sess_hash(cli, id, flow);s->hnext = R.buckets[h]; R.buckets[h] = s;s->next = R.sess_head;@@ -1267,85 +1231,91 @@ static int sess_arm(struct sess *s, int which, uint32_t events)return epoll_ctl(R.uep, EPOLL_CTL_ADD, fd, &ev);}-static void flow_upsert(struct sess *s, const struct pb_orig *o, uint32_t flow_be)+static int flow_upsert(struct sess *s, const struct pb_orig *o, uint32_t flow_be, int64_t now){int victim = 0;for (int i = 0; i < s->nflows; i++) {- if (s->flows[i].port == o->port && !memcmp(s->flows[i].addr, o->addr, 16)) { victim = -1; s->flows[i].ip_be = flow_be; s->flows[i].last = s->last; break; }+ if (s->flows[i].port == o->port && !memcmp(s->flows[i].addr, o->addr, 16)) {+ s->flows[i].ip_be = flow_be;+ s->flows[i].last = now;+ return i;+ }if (s->flows[i].last < s->flows[victim].last) victim = i;}- if (victim < 0) return;if (s->nflows < MAX_FLOWS) victim = s->nflows++;memcpy(s->flows[victim].addr, o->addr, 16);s->flows[victim].port = o->port;s->flows[victim].ip_be = flow_be;- s->flows[victim].last = s->last;+ s->flows[victim].last = now;+ return victim;}-// Wraps the datagram with a SOCKS5 UDP header into R.txbuf; returns total length or 0.-static size_t socks5_wrap(const struct pb_orig *o, const uint8_t *pl, size_t n)+static size_t socks5_header(uint8_t *h, const struct pb_orig *o){- size_t h = o->family == AF_INET ? 10 : 22;- if (n + h > UDP_MAXDG) return 0;- uint8_t *b = R.txbuf;- b[0] = b[1] = b[2] = 0;- if (o->family == AF_INET) { b[3] = 1; memcpy(b + 4, o->addr + 12, 4); }- else { b[3] = 4; memcpy(b + 4, o->addr, 16); }- b[h - 2] = (uint8_t)(o->port >> 8); b[h - 1] = (uint8_t)o->port;- memcpy(b + h, pl, n);- return n + h;+ h[0] = h[1] = h[2] = 0;+ size_t n;+ if (o->family == AF_INET) { h[3] = 1; memcpy(h + 4, o->addr + 12, 4); n = 10; }+ else { h[3] = 4; memcpy(h + 4, o->addr, 16); n = 22; }+ h[n - 2] = (uint8_t)(o->port >> 8);+ h[n - 1] = (uint8_t)o->port;+ return n;}-static void udp_dgram(const struct sockaddr_in *cli, uint32_t flow_be, const uint8_t *pl, size_t n)+static void udp_dgram(const struct sockaddr_in *cli, uint32_t flow_be, const uint8_t *pl, size_t n, int64_t now){- struct pb_orig o;- if (pb_core_get_udp_flow(flow_be, &o) != 0) { udp_droplog("unknown flow address"); return; }- int64_t now = now_ms();uint32_t hv = (cli->sin_addr.s_addr * 2654435761u) ^ (cli->sin_port * 40503u) ^ (flow_be * 2246822519u);struct dce *e = &R.dc[hv % DC_SIZE];- uint32_t pid = o.proxy_id;- if (!(e->used && e->cli.sin_addr.s_addr == cli->sin_addr.s_addr && e->cli.sin_port == cli->sin_port && e->flow_be == flow_be &&- e->port == o.port && !memcmp(e->addr, o.addr, 16) && now - e->last <= (int64_t)R.cfg.udp_idle_timeout_s * 1000)) {- int act = o.action;- if (act == PB_ACT_DECIDE) {- struct pb_decision d;- pb_core_decide(&o, &d);- act = d.action; pid = d.proxy_id;- }- char desc[300] = "BLOCK";- if (act == PB_ACT_PROXY) {- struct pb_proxy px;- if (pb_core_get_proxy(pid, &px) != 0) {- pb_core_log("relay: UDP: no proxy configured (id %u), dropping", pid);+ bool same = e->used && e->cli.sin_addr.s_addr == cli->sin_addr.s_addr && e->cli.sin_port == cli->sin_port && e->flow_be == flow_be;+ if (!same || now - e->looked >= FLOW_TTL_MS) {+ struct pb_orig o;+ if (pb_core_get_udp_flow(flow_be, &o) != 0) { e->used = false; udp_droplog("unknown flow address"); return; }+ bool keep = same && e->o.port == o.port && !memcmp(e->o.addr, o.addr, 16) && now - e->last <= (int64_t)R.cfg.udp_idle_timeout_s * 1000;+ if (!keep) {+ int act = o.action;+ uint32_t pid = o.proxy_id;+ if (act == PB_ACT_DECIDE) {+ struct pb_decision d;+ pb_core_decide(&o, &d);+ act = d.action;+ pid = d.proxy_id;+ }+ if (act == PB_ACT_PROXY) {+ struct pb_proxy px;+ if (pb_core_get_proxy(pid, &px) != 0) {+ pb_core_log("relay: UDP: no proxy configured (id %u), dropping", pid);+ act = PB_ACT_BLOCK;+ } else {+ pid = px.id;+ }+ } else if (act != PB_ACT_DIRECT) {act = PB_ACT_BLOCK;- } else {- proxy_desc(&px, desc, sizeof desc);- if (px.type != PROXY_TYPE_SOCKS5) snprintf(desc + strlen(desc), sizeof desc - strlen(desc), " (UDP unsupported)");}- } else if (act == PB_ACT_DIRECT) {- strcpy(desc, "DIRECT");- } else {- act = PB_ACT_BLOCK;+ pb_core_flow_event(&o, act);+ e->action = act;+ e->pid = pid;+ e->sid = 0;}- pb_core_flow_event(&o, act, desc);- e->used = true; e->cli = *cli; e->flow_be = flow_be; memcpy(e->addr, o.addr, 16); e->port = o.port; e->action = act;+ e->used = true;+ e->cli = *cli;+ e->flow_be = flow_be;+ e->o = o;+ e->looked = now;}e->last = now;if (e->action == PB_ACT_BLOCK) return;+ const struct pb_orig *o = &e->o;struct sess *s;if (e->action == PB_ACT_DIRECT) {s = sess_find(cli, DIRECT_ID, flow_be);if (!s) {- struct sockaddr_storage ss;- socklen_t sl = orig_to_sa(&o, &ss);s = sess_new(cli, DIRECT_ID, flow_be, SK_DIRECT);if (!s) { udp_droplog("UDP session limit reached"); return; }- s->family = o.family;- s->dst = ss; s->dstlen = sl; s->dport = o.port;- s->connected = o.family != AF_INET;- s->up_fd = socket(ss.ss_family, SOCK_DGRAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0);- if (s->up_fd < 0 || (s->connected && connect(s->up_fd, (struct sockaddr *)&ss, sl) < 0) || sess_arm(s, 0, EPOLLIN) < 0) {+ s->dstlen = orig_to_sa(o, &s->dst);+ s->dport = o->port;+ s->connected = o->family != AF_INET;+ s->up_fd = socket(s->dst.ss_family, SOCK_DGRAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0);+ if (s->up_fd < 0 || (s->connected && connect(s->up_fd, (struct sockaddr *)&s->dst, s->dstlen) < 0) || sess_arm(s, 0, EPOLLIN) < 0) {pb_core_log("relay: UDP direct socket failed: %s", strerror(errno));sess_destroy(s);return;@@ -1358,11 +1328,10 @@ static void udp_dgram(const struct sockaddr_in *cli, uint32_t flow_be, const uinreturn;}- // PROXY: re-resolve to get the canonical id and type- struct pb_proxy px;- if (pb_core_get_proxy(pid, &px) != 0) { udp_droplog("proxy vanished"); return; }- s = sess_find(cli, px.id, 0);+ s = sess_find(cli, e->pid, 0);if (!s) {+ struct pb_proxy px;+ if (pb_core_get_proxy(e->pid, &px) != 0) { udp_droplog("proxy vanished"); return; }s = sess_new(cli, px.id, 0, px.type == PROXY_TYPE_SOCKS5 ? SK_SOCKS5 : SK_DROP);if (!s) { udp_droplog("UDP session limit reached"); return; }s->proxy = px;@@ -1370,50 +1339,72 @@ static void udp_dgram(const struct sockaddr_in *cli, uint32_t flow_be, const uins->state = ST_READY;} else {s->state = ST_SETUP;- if (job_push(J_UDP, -1, NULL, s) != 0) { s->state = ST_FAILED; s->fail_until = now + 5000; udp_droplog("setup queue full"); }+ if (job_push(J_UDP, -1, NULL, s) != 0) {+ s->state = ST_FAILED;+ s->fail_until = now + 5000;+ udp_droplog("setup queue full");+ }}}s->last = now;if (s->kind == SK_DROP) {if (!s->warned) {s->warned = true;- pb_core_log("relay: UDP cannot be proxied through HTTP proxy '%s'; dropping UDP from %s:%u", px.name,+ pb_core_log("relay: UDP cannot be proxied through HTTP proxy '%s'; dropping UDP from %s:%u", s->proxy.name,inet_ntoa(cli->sin_addr), ntohs(cli->sin_port));}return;}if (s->state == ST_FAILED) { udp_droplog("proxy UDP setup failed"); return; }- flow_upsert(s, &o, flow_be);- size_t wl = socks5_wrap(&o, pl, n);- if (!wl) { udp_droplog("datagram too large"); return; }++ if (e->sid == s->sid && e->fidx < s->nflows && s->flows[e->fidx].port == o->port && !memcmp(s->flows[e->fidx].addr, o->addr, 16)) {+ s->flows[e->fidx].last = now;+ } else {+ e->fidx = flow_upsert(s, o, flow_be, now);+ e->sid = s->sid;+ }++ uint8_t hdr[22];+ size_t hl = socks5_header(hdr, o);+ if (hl + n > UDP_MAXDG) { udp_droplog("datagram too large"); return; }if (s->state == ST_SETUP) {- if (s->nq >= UDP_QMAX || s->qbytes + wl > UDP_QBYTES) { udp_droplog("setup queue full"); return; }- struct pkt *p = malloc(sizeof *p + wl);+ if (s->nq >= UDP_QMAX || s->qbytes + hl + n > UDP_QBYTES) { udp_droplog("setup queue full"); return; }+ struct pkt *p = malloc(sizeof *p + hl + n);if (!p) return;- p->len = wl; memcpy(p->d, R.txbuf, wl);- s->q[s->nq++] = p; s->qbytes += wl;+ p->len = hl + n;+ memcpy(p->d, hdr, hl);+ memcpy(p->d + hl, pl, n);+ s->q[s->nq++] = p;+ s->qbytes += p->len;return;}- send(s->up_fd, R.txbuf, wl, MSG_DONTWAIT | MSG_NOSIGNAL);+ struct iovec iov[2] = { { hdr, hl }, { (void *)pl, n } };+ struct msghdr mh = { .msg_iov = iov, .msg_iovlen = 2 };+ sendmsg(s->up_fd, &mh, MSG_DONTWAIT | MSG_NOSIGNAL);}static void udp_recv_client(void){- for (int i = 0; i < 128; i++) {- struct sockaddr_in cli;- union { char b[CMSG_SPACE(sizeof(struct in_pktinfo))]; struct cmsghdr a; } cb;- struct iovec iov = { R.rxbuf, UDP_MAXDG };- struct msghdr mh = { .msg_name = &cli, .msg_namelen = sizeof cli, .msg_iov = &iov, .msg_iovlen = 1,- .msg_control = cb.b, .msg_controllen = sizeof cb.b };- ssize_t n = recvmsg(R.ufd, &mh, MSG_DONTWAIT);- if (n < 0) return;- if (mh.msg_flags & MSG_TRUNC) continue;- bool have = false;- struct in_pktinfo pi;- for (struct cmsghdr *c = CMSG_FIRSTHDR(&mh); c; c = CMSG_NXTHDR(&mh, c))- if (c->cmsg_level == IPPROTO_IP && c->cmsg_type == IP_PKTINFO) { memcpy(&pi, CMSG_DATA(c), sizeof pi); have = true; }- if (!have || (ntohl(pi.ipi_addr.s_addr) >> 24) != 127) { udp_droplog("destination not in 127.0.0.0/8"); continue; }- udp_dgram(&cli, pi.ipi_addr.s_addr, R.rxbuf, (size_t)n);+ for (int round = 0; round < 8; round++) {+ for (int i = 0; i < UDP_BATCH; i++) {+ R.mmiov[i] = (struct iovec){ R.rxbuf + (size_t)i * UDP_MAXDG, UDP_MAXDG };+ R.mm[i].msg_hdr = (struct msghdr){ .msg_name = &R.mmname[i], .msg_namelen = sizeof(struct sockaddr_in), .msg_iov = &R.mmiov[i],+ .msg_iovlen = 1, .msg_control = R.mmctl[i], .msg_controllen = sizeof R.mmctl[i] };+ }+ int cnt = recvmmsg(R.ufd, R.mm, UDP_BATCH, MSG_DONTWAIT, NULL);+ if (cnt <= 0) return;+ int64_t now = now_ms();+ for (int i = 0; i < cnt; i++) {+ struct msghdr *mh = &R.mm[i].msg_hdr;+ if (mh->msg_flags & MSG_TRUNC) continue;+ struct in_pktinfo pi;+ bool have = false;+ for (struct cmsghdr *c = CMSG_FIRSTHDR(mh); c; c = CMSG_NXTHDR(mh, c))+ if (c->cmsg_level == IPPROTO_IP && c->cmsg_type == IP_PKTINFO) { memcpy(&pi, CMSG_DATA(c), sizeof pi); have = true; }+ if (!have || (ntohl(pi.ipi_addr.s_addr) >> 24) != 127) { udp_droplog("destination not in 127.0.0.0/8"); continue; }+ udp_dgram(&R.mmname[i], pi.ipi_addr.s_addr, R.rxbuf + (size_t)i * UDP_MAXDG, R.mm[i].msg_len, now);+ }+ if (cnt < UDP_BATCH) return;}}@@ -1544,9 +1535,6 @@ static void *udp_main(void *arg)return NULL;}-// ----------------------------------------------------------------------------------------------// Start / stop-// ---------------------------------------------------------------------------------------------static void teardown(void){atomic_store(&R.stopping, true);@@ -1561,7 +1549,6 @@ static void teardown(void)R.acc_started = R.udp_started = false;R.nworkers = 0;- // everything is single-threaded from here onwhile (R.jhead) {struct job *j = R.jhead;R.jhead = j->next;@@ -1589,7 +1576,8 @@ static void teardown(void)R.nsess = 0;free(R.dc); R.dc = NULL;free(R.rxbuf); R.rxbuf = NULL;- free(R.txbuf); R.txbuf = NULL;+ free(R.mm); free(R.mmiov); free(R.mmname); free(R.mmctl);+ R.mm = NULL; R.mmiov = NULL; R.mmname = NULL; R.mmctl = NULL;close_fd(&R.lfd);close_fd(&R.ufd);close_fd(&R.tmfd);@@ -1624,9 +1612,6 @@ int pb_relay_start(const struct pb_relay_cfg *cfg)int one = 1;long ncpu = sysconf(_SC_NPROCESSORS_ONLN);R.ndata = ncpu < 2 ? 2 : (ncpu > MAX_DATA ? MAX_DATA : (int)ncpu);-#ifdef PB_RELAY_NDATA // benchmarking override- R.ndata = PB_RELAY_NDATA;-#endiffor (int i = 0; i < R.ndata; i++) {memset(&R.data[i], 0, sizeof R.data[i]);R.data[i].ep = R.data[i].wake = -1;@@ -1641,6 +1626,7 @@ int pb_relay_start(const struct pb_relay_cfg *cfg)R.lfd = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0);if (R.lfd < 0) { why = "tcp socket"; goto fail; }setsockopt(R.lfd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof one);+ tune_sock(R.lfd);struct sockaddr_in a = { .sin_family = AF_INET, .sin_port = htons(c.tcp_port), .sin_addr.s_addr = htonl(INADDR_LOOPBACK) };if (bind(R.lfd, (struct sockaddr *)&a, sizeof a) < 0 || listen(R.lfd, 4096) < 0) {pb_core_log("relay: cannot listen on 127.0.0.1:%u: %s", c.tcp_port, strerror(errno));@@ -1652,7 +1638,7 @@ int pb_relay_start(const struct pb_relay_cfg *cfg)if (setsockopt(R.ufd, SOL_SOCKET, SO_BINDTODEVICE, "lo", 3) < 0)pb_core_log("relay: SO_BINDTODEVICE lo failed (%s); relying on destination address filtering", strerror(errno));int rb = 4 << 20;- setsockopt(R.ufd, SOL_SOCKET, SO_RCVBUF, &rb, sizeof rb);+ if (setsockopt(R.ufd, SOL_SOCKET, SO_RCVBUFFORCE, &rb, sizeof rb) < 0) setsockopt(R.ufd, SOL_SOCKET, SO_RCVBUF, &rb, sizeof rb);if (setsockopt(R.ufd, IPPROTO_IP, IP_PKTINFO, &one, sizeof one) < 0) { why = "IP_PKTINFO"; goto fail; }a.sin_port = htons(c.udp_port);a.sin_addr.s_addr = htonl(INADDR_ANY);@@ -1665,9 +1651,12 @@ int pb_relay_start(const struct pb_relay_cfg *cfg)R.uep = epoll_create1(EPOLL_CLOEXEC);R.tmfd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK | TFD_CLOEXEC);R.dc = calloc(DC_SIZE, sizeof *R.dc);- R.rxbuf = malloc(UDP_MAXDG);- R.txbuf = malloc(UDP_MAXDG + 32);- if (R.uep < 0 || R.tmfd < 0 || !R.dc || !R.rxbuf || !R.txbuf) { why = "udp resources"; goto fail; }+ R.rxbuf = malloc((size_t)UDP_MAXDG * UDP_BATCH);+ R.mm = calloc(UDP_BATCH, sizeof *R.mm);+ R.mmiov = calloc(UDP_BATCH, sizeof *R.mmiov);+ R.mmname = calloc(UDP_BATCH, sizeof *R.mmname);+ R.mmctl = calloc(UDP_BATCH, sizeof *R.mmctl);+ if (R.uep < 0 || R.tmfd < 0 || !R.dc || !R.rxbuf || !R.mm || !R.mmiov || !R.mmname || !R.mmctl) { why = "udp resources"; goto fail; }struct itimerspec its = { .it_interval = { 1, 0 }, .it_value = { 1, 0 } };if (timerfd_settime(R.tmfd, 0, &its, NULL) < 0 ||add_ep(R.uep, R.ufd, &TAG_UDP, EPOLLIN) || add_ep(R.uep, R.stopfd, &TAG_STOP, EPOLLIN) ||@@ -1678,9 +1667,6 @@ int pb_relay_start(const struct pb_relay_cfg *cfg)t->scratch = malloc(SCRATCH_SZ);if (pipe2(t->pfd, O_CLOEXEC | O_NONBLOCK) == 0) fcntl(t->pfd[0], F_SETPIPE_SZ, PIPE_SZ);else t->pfd[0] = t->pfd[1] = -1, t->nosplice = true;-#ifdef PB_RELAY_COPY_ONLY // benchmarking override- t->nosplice = true;-#endift->ep = epoll_create1(EPOLL_CLOEXEC);t->wake = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK);if (!t->scratch || t->ep < 0 || t->wake < 0 || add_ep(t->ep, t->wake, &TAG_WAKE, EPOLLIN) || add_ep(t->ep, R.stopfd, &TAG_STOP, EPOLLIN)) {@@ -1728,9 +1714,6 @@ void pb_relay_stop(void)uint16_t pb_relay_tcp_port(void) { return R.tcp_port; }uint16_t pb_relay_udp_port(void) { return R.udp_port; }-// ----------------------------------------------------------------------------------------------// Synchronous proxy test-// ---------------------------------------------------------------------------------------------int pb_relay_test_proxy(const struct pb_proxy *proxy, const char *host, uint16_t port, char *buf, size_t buflen){if (!buf || !buflen) return -1;diff --git a/Linux/src/tests/relay_test.c b/Linux/src/tests/relay_test.cindex c385a3e..452d52f 100644--- a/Linux/src/tests/relay_test.c+++ b/Linux/src/tests/relay_test.c@@ -1,7 +1,5 @@-// Root-free loopback test harness for pb_relay.c. Fakes every pb_core_* function and runs in-process fake-// SOCKS5 / HTTP CONNECT proxies and echo servers. The kernel redirect is simulated (see README comments below):-// TCP: client bind()s a known source port, the fake pb_core_take_tcp_orig() maps that port to the destination.-// UDP: client sends to 127.x.y.z:udp_port, the fake pb_core_get_udp_flow() maps 127.x.y.z to the destination.+// Root-free loopback test harness for pb_relay.c: fake pb_core_* services, in-process SOCKS5 / HTTP proxies and echo servers.+// The kernel redirect is simulated by registering original destinations keyed by the client's (ip,port) or by flow address.#ifndef _GNU_SOURCE#define _GNU_SOURCE#endif@@ -33,12 +31,11 @@#include "../pb_internal.h"-// ----------------------------------------------------------------------------------------------// Fake core-// ---------------------------------------------------------------------------------------------static pthread_mutex_t g_mu = PTHREAD_MUTEX_INITIALIZER;-static struct pb_orig g_tcp[65536];-static bool g_tcp_valid[65536];+#define TCP_SLOTS 65536+static struct tcp_slot { uint64_t key; struct pb_orig o; uint8_t state; } g_tcp[TCP_SLOTS]; // 0 free, 1 used, 2 tombstone+static inline uint64_t tcp_key(uint32_t ip_be, uint16_t port) { return (uint64_t)ip_be << 16 | port; }+static inline uint32_t tcp_hash(uint64_t k) { return (uint32_t)((k * 0x9E3779B97F4A7C15ull) >> 48); }static struct { uint32_t ip_be; struct pb_orig o; bool used; } g_flows[64];static struct pb_proxy g_px[16];static bool g_px_used[16];@@ -48,11 +45,19 @@ static atomic_int g_ev, g_ev_proxy, g_ev_direct, g_ev_block, g_decide_calls;static char g_log[64][512];static int g_log_n;-int pb_core_take_tcp_orig(uint16_t p, struct pb_orig *out)+int pb_core_take_tcp_orig(uint32_t ip_be, uint16_t port, struct pb_orig *out){+ uint64_t k = tcp_key(ip_be, port);+ int ok = 0;pthread_mutex_lock(&g_mu);- int ok = g_tcp_valid[p];- if (ok) { *out = g_tcp[p]; g_tcp_valid[p] = false; }+ for (uint32_t i = tcp_hash(k), n = 0; n < TCP_SLOTS && g_tcp[i].state; i = (i + 1) % TCP_SLOTS, n++) {+ if (g_tcp[i].state == 1 && g_tcp[i].key == k) {+ *out = g_tcp[i].o;+ g_tcp[i].state = g_tcp[(i + 1) % TCP_SLOTS].state ? 2 : 0;+ ok = 1;+ break;+ }+ }pthread_mutex_unlock(&g_mu);return ok ? 0 : -1;}@@ -92,15 +97,14 @@ void pb_core_log(const char *fmt, ...)pthread_mutex_unlock(&g_mu);if (getenv("VERBOSE")) fprintf(stderr, " [log] %s\n", b);}-void pb_core_flow_event(const struct pb_orig *o, int action, const char *d)+void pb_core_flow_event(const struct pb_orig *o, int action){- (void)o; (void)d;+ (void)o;atomic_fetch_add(&g_ev, 1);if (action == PB_ACT_PROXY) atomic_fetch_add(&g_ev_proxy, 1);else if (action == PB_ACT_DIRECT) atomic_fetch_add(&g_ev_direct, 1);else atomic_fetch_add(&g_ev_block, 1);}-bool pb_core_dns_via_proxy(void) { return false; }struct obs { int64_t t0, t1; size_t len; uint32_t h; };static struct obs g_obs[64];@@ -143,9 +147,6 @@ static void set_decision(struct pb_decision d)static void log_clear(void) { pthread_mutex_lock(&g_mu); g_log_n = 0; memset(g_log, 0, sizeof g_log); pthread_mutex_unlock(&g_mu); }static void ev_clear(void) { g_ev = g_ev_proxy = g_ev_direct = g_ev_block = 0; }-// ----------------------------------------------------------------------------------------------// Test plumbing-// ---------------------------------------------------------------------------------------------static int g_pass, g_fail;static const char *volatile g_cur = "startup";static void watchdog(int sig)@@ -165,7 +166,7 @@ static void arm_watchdog(void)signal(SIGALRM, watchdog);alarm((unsigned)(secs > 0 ? secs : 300));}-#define RUN(fn) do { g_cur = #fn; printf("---- %s\n", #fn); fn(); } while (0)+#define RUN(fn) do { const char *only_ = getenv("PB_TEST_ONLY"); if (only_ && !strstr(only_, #fn)) break; g_cur = #fn; printf("---- %s\n", #fn); fn(); } while (0)#define CHECK(cond, ...) do { if (cond) { g_pass++; printf("PASS "); } else { g_fail++; printf("FAIL "); } \printf(__VA_ARGS__); printf("\n"); fflush(stdout); } while (0)@@ -181,13 +182,21 @@ static void fill_orig(struct pb_orig *o, const char *ip, uint16_t port, int protmemcpy(o->addr + 12, &a, 4);o->port = port; o->family = AF_INET; o->proto = (uint8_t)proto; o->action = (uint8_t)action; o->proxy_id = (uint16_t)px;}-static void set_tcp(uint16_t src, const char *ip, uint16_t port, int action, uint32_t px)+static void set_tcp_from(uint32_t src_ip_be, uint16_t src, const char *ip, uint16_t port, int action, uint32_t px){+ uint64_t k = tcp_key(src_ip_be, src);pthread_mutex_lock(&g_mu);- fill_orig(&g_tcp[src], ip, port, IPPROTO_TCP, action, px);- g_tcp_valid[src] = true;+ uint32_t i = tcp_hash(k);+ while (g_tcp[i].state == 1 && g_tcp[i].key != k) i = (i + 1) % TCP_SLOTS;+ g_tcp[i].key = k;+ g_tcp[i].state = 1;+ fill_orig(&g_tcp[i].o, ip, port, IPPROTO_TCP, action, px);pthread_mutex_unlock(&g_mu);}+static void set_tcp(uint16_t src, const char *ip, uint16_t port, int action, uint32_t px)+{+ set_tcp_from(htonl(INADDR_LOOPBACK), src, ip, port, action, px);+}static int g_nflows;static void set_flow(const char *flow_ip, const char *ip, uint16_t port, int action, uint32_t px){@@ -222,9 +231,8 @@ static int count_fds(void)DIR *d = opendir("/proc/self/fd");while (readdir(d)) n++;closedir(d);- return n - 3; // ., .., and the dir fd itself+ return n - 3;}-// Waits (up to ms) for the fd count to drop to <= target; returns the final count.static int settle_fds(int target, int ms);static int64_t now_ms(void){@@ -241,9 +249,6 @@ static int settle_fds(int target, int ms)return n;}-// ----------------------------------------------------------------------------------------------// Fake servers-// ---------------------------------------------------------------------------------------------struct srvcfg { bool auth; char user[64], pass[64]; bool leftover; bool silent; };struct srv { int lfd; uint16_t port; void (*h)(int, const struct srvcfg *); struct srvcfg cfg; };struct hctx { int fd; struct srv *s; };@@ -399,7 +404,6 @@ static void http_handler(int fd, const struct srvcfg *c)}b[n] = 0;if (c->auth) {- // Basic base64("user:pass") computed by the test config: pass holds the expected token in user field? keep simple:char want[200];snprintf(want, sizeof want, "Proxy-Authorization: Basic %s", c->pass);if (!strstr(b, want)) { const char *r = "HTTP/1.1 407 Proxy Authentication Required\r\n\r\n"; wr_full(fd, r, strlen(r)); return; }@@ -476,6 +480,8 @@ static uint16_t udpecho_start(void){struct udpecho *e = calloc(1, sizeof *e);e->fd = socket(AF_INET, SOCK_DGRAM, 0);+ int rb = 4 << 20;+ setsockopt(e->fd, SOL_SOCKET, SO_RCVBUF, &rb, sizeof rb);struct sockaddr_in a = { .sin_family = AF_INET, .sin_addr.s_addr = htonl(INADDR_LOOPBACK) };bind(e->fd, (struct sockaddr *)&a, sizeof a);socklen_t al = sizeof a;@@ -487,24 +493,22 @@ static uint16_t udpecho_start(void)return e->port;}-// ----------------------------------------------------------------------------------------------// Client helpers-// ----------------------------------------------------------------------------------------------static int g_last_connect_errno; // errno of the last failed relay_connect()-static int relay_connect(uint16_t src)+static int g_last_connect_errno;+static int relay_connect_from(uint32_t src_ip_be, uint16_t src){int fd = socket(AF_INET, SOCK_STREAM, 0);int one = 1;setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof one);- struct sockaddr_in a = { .sin_family = AF_INET, .sin_addr.s_addr = htonl(INADDR_LOOPBACK), .sin_port = htons(src) };+ struct sockaddr_in a = { .sin_family = AF_INET, .sin_addr.s_addr = src_ip_be, .sin_port = htons(src) };if (bind(fd, (struct sockaddr *)&a, sizeof a) < 0) {g_last_connect_errno = errno;printf(" relay_connect: bind(127.0.0.1:%u) failed: %s\n", src, strerror(errno));close(fd); return -1;}a.sin_port = htons(pb_relay_tcp_port());+ a.sin_addr.s_addr = htonl(INADDR_LOOPBACK);struct timeval tv = { 10, 0 };- setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof tv); // bounds connect()+ setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof tv);if (connect(fd, (struct sockaddr *)&a, sizeof a) < 0) {g_last_connect_errno = errno;// an ECONNRESET here is legitimate for flows the relay aborts (BLOCK, cap): on a fast loopback the RST can arrive@@ -518,6 +522,7 @@ static int relay_connect(uint16_t src)setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof tv);return fd;}+static int relay_connect(uint16_t src) { return relay_connect_from(htonl(INADDR_LOOPBACK), src); }static uint16_t new_src(void) { return (uint16_t)atomic_fetch_add(&g_next_port, 1); }static inline uint8_t pat(uint64_t i) { return (uint8_t)((i * 0x9E3779B1u) >> 11); }@@ -537,7 +542,6 @@ static void *xf_send(void *p)if (x->half) shutdown(x->fd, SHUT_WR);return NULL;}-// Sends `total` patterned bytes and verifies the echo. Returns bytes verified, -1 on mismatch.static int64_t xfer(int fd, uint64_t total, bool half){struct xf x = { fd, total, half };@@ -559,10 +563,9 @@ out:pthread_join(t, NULL);return res;}-// True when the peer closes (EOF or reset) within timeout_ms with no data.static bool expect_closed(int fd, int timeout_ms){- if (timeout_ms < 10000) timeout_ms = 10000; // generous: the watchdog bounds real hangs+ if (timeout_ms < 10000) timeout_ms = 10000;struct pollfd p = { fd, POLLIN, 0 };int pr = poll(&p, 1, timeout_ms);if (pr <= 0) { printf(" expect_closed(fd=%d): poll=%d errno=%d revents=0x%x\n", fd, pr, errno, p.revents); return false; }@@ -599,7 +602,6 @@ static uint16_t free_port(void)return ntohs(a.sin_port);}-// One echo round trip of `n` bytes through the relay for the given action.static int64_t tcp_roundtrip(const char *dst_ip, uint16_t dst_port, int action, uint32_t px, uint64_t n, bool half){uint16_t src = new_src();@@ -611,9 +613,6 @@ static int64_t tcp_roundtrip(const char *dst_ip, uint16_t dst_port, int action,return r;}-// ----------------------------------------------------------------------------------------------// UDP client helpers-// ---------------------------------------------------------------------------------------------static int udp_client(void){int fd = socket(AF_INET, SOCK_DGRAM, 0);@@ -627,7 +626,6 @@ static void udp_send_flow(int fd, const char *flow_ip, const char *msg)inet_pton(AF_INET, flow_ip, &a.sin_addr);sendto(fd, msg, strlen(msg), 0, (struct sockaddr *)&a, sizeof a);}-// Receives one datagram; returns length or -1; src_ip filled with the sender address.static int udp_recv_to(int fd, char *buf, size_t n, char *src_ip, uint16_t *src_port, int ms){struct timeval tv = { ms / 1000, (ms % 1000) * 1000 };@@ -652,9 +650,6 @@ static bool udp_echo_ok(int fd, const char *flow_ip, const char *msg)return r == (int)strlen(msg) && !strcmp(b, msg) && !strcmp(ip, flow_ip) && sp == pb_relay_udp_port();}-// ----------------------------------------------------------------------------------------------// Tests-// ---------------------------------------------------------------------------------------------static struct srv *g_echo, *g_s5, *g_s5auth, *g_http, *g_httpauth, *g_httpleft, *g_silent;static uint16_t g_uecho1, g_uecho2;@@ -668,6 +663,207 @@ static void *conc_thread(void *p)return (void *)(intptr_t)(r == (int64_t)(mb << 20));}+static long vmrss_kb(void)+{+ FILE *f = fopen("/proc/self/status", "r");+ char line[256];+ long v = -1;+ while (f && fgets(line, sizeof line, f))+ if (!strncmp(line, "VmRSS:", 6)) { v = atol(line + 6); break; }+ if (f) fclose(f);+ return v;+}++static atomic_uint g_churn_ctr;+static atomic_int g_churn_bad, g_churn_udp_lost;+static uint16_t g_dead_port;++static void churn_one(unsigned n)+{+ uint32_t ip = htonl(0x7f000002 + n % 60);+ uint16_t src = (uint16_t)(20000 + (n / 60) % 12000);+ int mode = (int)(n % 9);+ uint32_t px = 0;+ int action = PB_ACT_PROXY;+ uint16_t dport = g_echo->port;+ switch (mode) {+ case 0: px = 1; break;+ case 1: px = 3; break;+ case 2: action = PB_ACT_DIRECT; break;+ case 3: action = PB_ACT_BLOCK; break;+ case 4: px = 6; break;+ case 5: px = 7; break;+ case 6: px = 1; break;+ case 7: action = PB_ACT_DIRECT; dport = g_dead_port; break;+ default: action = PB_ACT_DIRECT; break;+ }+ set_tcp_from(ip, src, "127.0.0.1", dport, action, px);+ int fd = relay_connect_from(ip, src);+ if (fd < 0) {+ if (mode != 3 && mode != 4 && mode != 5 && mode != 7) atomic_fetch_add(&g_churn_bad, 1);+ return;+ }+ char b[128];+ switch (mode) {+ case 0: case 1: case 2:+ if (send(fd, "z", 1, MSG_NOSIGNAL) != 1 || rd_full(fd, b, 1) || b[0] != 'z') atomic_fetch_add(&g_churn_bad, 1);+ break;+ case 6: {+ struct linger l = { 1, 0 };+ setsockopt(fd, SOL_SOCKET, SO_LINGER, &l, sizeof l);+ break;+ }+ case 8: {+ char big[100] = { 0 };+ send(fd, big, sizeof big, MSG_NOSIGNAL);+ struct linger l = { 1, 0 };+ setsockopt(fd, SOL_SOCKET, SO_LINGER, &l, sizeof l);+ break;+ }+ default:+ (void)expect_closed(fd, 10000);+ }+ close(fd);+}++struct churn_arg { unsigned count; };+static void *churn_tcp_thread(void *p)+{+ struct churn_arg *a = p;+ for (unsigned i = 0; i < a->count; i++) churn_one(atomic_fetch_add(&g_churn_ctr, 1));+ return NULL;+}++struct churn_udp { unsigned total; int bad; };+static void *churn_udp_thread(void *p)+{+ struct churn_udp *a = p;+ enum { B = 1000 };+ int *fds = malloc(B * sizeof *fds);+ for (unsigned done = 0; done < a->total;) {+ int n = a->total - done < B ? (int)(a->total - done) : B;+ for (int lo = 0; lo < n; lo += 50) { // paced so a starved echo server cannot overflow its socket buffer+ int hi = lo + 50 < n ? lo + 50 : n;+ for (int i = lo; i < hi; i++) {+ fds[i] = udp_client();+ udp_send_flow(fds[i], (done + (unsigned)i) % 10 == 0 ? "127.1.9.2" : "127.1.9.1", "churn");+ }+ for (int i = lo; i < hi; i++) {+ char r[32], ip[32];+ uint16_t sp;+ if (udp_recv_to(fds[i], r, sizeof r, ip, &sp, 4000) != 5) a->bad++;+ close(fds[i]);+ }+ }+ done += (unsigned)n;+ if (done < a->total) msleep(2200);+ }+ free(fds);+ return NULL;+}++static void churn_phase(unsigned tcp_total, unsigned udp_total)+{+ enum { T = 8 };+ pthread_t th[T], ut;+ struct churn_arg ca = { tcp_total / T };+ struct churn_udp cu = { udp_total, 0 };+ pthread_create(&ut, NULL, churn_udp_thread, &cu);+ for (int i = 0; i < T; i++) pthread_create(&th[i], NULL, churn_tcp_thread, &ca);+ for (int i = 0; i < T; i++) pthread_join(th[i], NULL);+ pthread_join(ut, NULL);+ atomic_fetch_add(&g_churn_udp_lost, cu.bad);+}++static void test_churn(void)+{+ unsigned tcp_total = getenv("PB_CHURN_TCP") ? (unsigned)atoi(getenv("PB_CHURN_TCP")) : 100000;+ unsigned udp_total = getenv("PB_CHURN_UDP") ? (unsigned)atoi(getenv("PB_CHURN_UDP")) : 20000;+ long limit_kb = (getenv("PB_CHURN_RSS_MB") ? atol(getenv("PB_CHURN_RSS_MB")) : 6) * 1024;+ pb_relay_stop();+ CHECK(relay_up(300, 1, 0, 5) == 0, "restart relay for churn (udp_idle=1s)");+ g_dead_port = free_port();+ set_proxy(6, PROXY_TYPE_SOCKS5, g_dead_port, NULL, NULL);+ set_proxy(7, PROXY_TYPE_SOCKS5, g_s5auth->port, "alice", "WRONG");+ set_flow("127.1.9.1", "127.0.0.1", g_uecho1, PB_ACT_DIRECT, 0);+ set_flow("127.1.9.2", "127.0.0.1", g_uecho2, PB_ACT_PROXY, 1);+ msleep(300);+ int base_fds = count_fds();++ unsigned chunk_tcp = tcp_total / 10 / 8 * 8, chunk_udp = udp_total / 10;+ long budget_ms = (getenv("PB_CHURN_BUDGET_S") ? atol(getenv("PB_CHURN_BUDGET_S")) : 60) * 1000;+ int64_t t0 = now_ms();+ churn_phase(chunk_tcp, chunk_udp);+ msleep(3000);+ long rss0 = vmrss_kb();+ int chunks = 1;+ while (chunks < 10 && now_ms() - t0 < budget_ms) { churn_phase(chunk_tcp, chunk_udp); chunks++; }+ msleep(3500);+ int fds_end = settle_fds(base_fds, 10000);+ long rss1 = vmrss_kb();+ unsigned tcp_done = chunk_tcp * (unsigned)chunks, udp_done = chunk_udp * (unsigned)chunks;+ printf(" churn: %u TCP + %u UDP sessions in %lld ms; RSS after warm-up %ld KB, final %ld KB (+%ld KB); fds base=%d end=%d\n",+ tcp_done, udp_done, (long long)(now_ms() - t0), rss0, rss1, rss1 - rss0, base_fds, fds_end);+ CHECK(chunks >= 4, "churn: completed %d of 10 chunks within the time budget", chunks);+ CHECK(g_churn_bad == 0, "churn: every TCP flow that must succeed did (failures=%d)", (int)g_churn_bad);+ CHECK(g_churn_udp_lost * 50 <= (int)udp_done, "churn: UDP datagrams lost <= 2%% (%d of %u; session-limit drop logged: %s)", (int)g_churn_udp_lost, udp_done,+ log_has("UDP session limit") ? "yes" : "no");+ CHECK(fds_end <= base_fds, "churn: fd count back to baseline (base=%d end=%d)", base_fds, fds_end);+ CHECK(rss1 - rss0 < limit_kb, "churn: RSS growth after warm-up < %ld MB (+%ld KB)", limit_kb / 1024, rss1 - rss0);+ pb_relay_stop();+ CHECK(relay_up(300, 60, 0, 5) == 0, "restart relay");+}++static void *perf_conn_thread(void *p)+{+ int n = (int)(intptr_t)p, ok = 0;+ for (int i = 0; i < n; i++) {+ uint16_t src = new_src();+ set_tcp(src, "127.0.0.1", g_echo->port, PB_ACT_PROXY, 1);+ int fd = relay_connect(src);+ char b = 'a';+ if (fd >= 0 && send(fd, &b, 1, MSG_NOSIGNAL) == 1 && recv(fd, &b, 1, 0) == 1) ok++;+ if (fd >= 0) close(fd);+ }+ return (void *)(intptr_t)ok;+}++static void test_perf(void)+{+ unsigned dg = getenv("PB_PERF_UDP") ? (unsigned)atoi(getenv("PB_PERF_UDP")) : 60000;+ int conns = getenv("PB_PERF_CONNS") ? atoi(getenv("PB_PERF_CONNS")) : 1500;+ set_flow("127.1.10.1", "127.0.0.1", g_uecho1, PB_ACT_DIRECT, 0);+ set_flow("127.1.10.2", "127.0.0.1", g_uecho2, PB_ACT_PROXY, 1);+ const char *fl[2] = { "127.1.10.1", "127.1.10.2" };+ const char *nm[2] = { "DIRECT", "SOCKS5" };+ for (int k = 0; k < 2; k++) {+ int c = udp_client();+ struct sockaddr_in a = { .sin_family = AF_INET, .sin_port = htons(pb_relay_udp_port()) };+ inet_pton(AF_INET, fl[k], &a.sin_addr);+ char msg[64] = "perf-datagram", rx[128], ip[32];+ uint16_t sp;+ udp_echo_ok(c, fl[k], "warm");+ unsigned got = 0;+ int64_t t0 = now_ms();+ for (unsigned sent = 0; sent < dg; sent += 16) {+ for (int i = 0; i < 16; i++) sendto(c, msg, sizeof msg, 0, (struct sockaddr *)&a, sizeof a);+ for (int i = 0; i < 16; i++) if (udp_recv_to(c, rx, sizeof rx, ip, &sp, 2000) > 0) got++;+ }+ int64_t ms = now_ms() - t0;+ CHECK(got >= dg * 99 / 100, "perf UDP %s: %u datagrams round trip, %.0f pps (%lld ms, %u received)", nm[k], dg,+ (double)got * 1000.0 / (double)(ms + 1), (long long)ms, got);+ close(c);+ }+ enum { T = 8 };+ pthread_t th[T];+ int64_t t0 = now_ms();+ for (int i = 0; i < T; i++) pthread_create(&th[i], NULL, perf_conn_thread, (void *)(intptr_t)(conns / T));+ int ok = 0;+ for (int i = 0; i < T; i++) { void *r; pthread_join(th[i], &r); ok += (int)(intptr_t)r; }+ int64_t ms = now_ms() - t0;+ CHECK(ok == conns / T * T, "perf TCP via SOCKS5: %d short connections, %.0f conn/s (%lld ms)", ok, (double)ok * 1000.0 / (double)(ms + 1), (long long)ms);+}+static void test_tcp_basic(void){ev_clear();@@ -681,7 +877,6 @@ static void test_tcp_basic(void)CHECK(tcp_roundtrip("127.0.0.1", g_echo->port, PB_ACT_DIRECT, 0, 100000, false) == 100000, "TCP DIRECT");CHECK(g_ev == 1 && g_ev_direct == 1, "flow_event for direct");- // HTTP leftover bytes after the 200 response must reach the client firstuint16_t src = new_src();set_tcp(src, "127.0.0.1", g_echo->port, PB_ACT_PROXY, 5);int fd = relay_connect(src);@@ -690,7 +885,6 @@ static void test_tcp_basic(void)CHECK(ok, "HTTP CONNECT leftover bytes forwarded to client");if (fd >= 0) close(fd);- // BLOCKev_clear();src = new_src();set_tcp(src, "127.0.0.1", g_echo->port, PB_ACT_BLOCK, 0);@@ -700,7 +894,6 @@ static void test_tcp_basic(void)CHECK(g_ev_block == 1, "flow_event BLOCK");close(fd);- // DECIDEset_decision((struct pb_decision){ PB_ACT_DIRECT, 0 });CHECK(tcp_roundtrip("127.0.0.1", g_echo->port, PB_ACT_DECIDE, 0, 5000, false) == 5000, "TCP DECIDE -> DIRECT");set_decision((struct pb_decision){ PB_ACT_PROXY, 1 });@@ -712,12 +905,28 @@ static void test_tcp_basic(void)CHECK(closed_by_relay(fd, 3000), "TCP DECIDE -> BLOCK");close(fd);- // no orig entryfd = relay_connect(new_src());CHECK(closed_by_relay(fd, 3000), "TCP without original destination is closed");close(fd);- // domain-name target through test helper is covered separately; IPv6 original destination (no v6 proxy server: direct to ::1)+}++static void test_tcp_identity(void)+{+ uint16_t src = new_src();+ set_tcp(src, "127.0.0.1", g_echo->port, PB_ACT_DIRECT, 0);+ int fd = relay_connect_from(htonl(0x7f000002), src);+ if (fd >= 0) send(fd, "x", 1, MSG_NOSIGNAL);+ CHECK(closed_by_relay(fd, 3000), "connection from 127.0.0.2 with a registered port is closed");+ if (fd >= 0) close(fd);+ fd = relay_connect(src);+ char b = 0;+ bool ok = fd >= 0 && send(fd, "y", 1, MSG_NOSIGNAL) == 1 && rd_full(fd, &b, 1) == 0 && b == 'y';+ CHECK(ok, "registered flow from the right source address still works (entry not consumed by the impostor)");+ if (fd >= 0) close(fd);+ fd = relay_connect_from(htonl(0x7f000003), new_src());+ CHECK(closed_by_relay(fd, 3000), "connection from an unregistered address/port is closed");+ if (fd >= 0) close(fd);}static void test_tcp_bad_proxy(void)@@ -749,12 +958,11 @@ static void test_tcp_bad_proxy(void)log_clear();src = new_src();- set_tcp(src, "127.0.0.1", g_echo->port, PB_ACT_PROXY, 12); // unknown proxy id+ set_tcp(src, "127.0.0.1", g_echo->port, PB_ACT_PROXY, 12);fd = relay_connect(src);CHECK(closed_by_relay(fd, 5000) && log_has("no proxy configured"), "unknown proxy id: client closed + logged");close(fd);- // dead target through a working proxy -> SOCKS5 REP 5log_clear();src = new_src();set_tcp(src, "127.0.0.1", dead, PB_ACT_PROXY, 1);@@ -972,7 +1180,7 @@ static void test_tcp_idle(void)set_tcp(src, "127.0.0.1", g_echo->port, PB_ACT_DIRECT, 0);fd = relay_connect(src);bool alive = true;- for (int i = 0; i < 10 && alive; i++) { // ~5 s of traffic every 0.5 s, longer than the 3 s idle timeout+ for (int i = 0; i < 10 && alive; i++) {send(fd, "p", 1, 0);alive = rd_full(fd, b, 1) == 0;msleep(500);@@ -1004,7 +1212,7 @@ static void test_tcp_cap(void)close(extra);close(fds[0]);bool freed = false;- for (int i = 0; i < 100 && !freed; i++) { // up to ~10 s for the relay to notice the close+ for (int i = 0; i < 100 && !freed; i++) {msleep(100);freed = tcp_roundtrip("127.0.0.1", g_echo->port, PB_ACT_DIRECT, 0, 1000, false) == 1000;}@@ -1041,7 +1249,6 @@ static void test_udp(void)CHECK(udp_echo_ok(c, "127.1.2.3", "again"), "first destination again");CHECK(g_assoc_count - a0 == 1, "exactly one UDP ASSOCIATE for the session (got %d)", (int)g_assoc_count - a0);- // burst while association is being set up (new client => new session): queued datagrams all deliveredint c2 = udp_client();for (int i = 0; i < 10; i++) { char m[32]; snprintf(m, sizeof m, "burst%d", i); udp_send_flow(c2, "127.1.2.3", m); }int got = 0;@@ -1049,39 +1256,33 @@ static void test_udp(void)CHECK(got == 10, "10 datagrams sent during association setup delivered (got %d)", got);close(c2);- // SOCKS5 with auth for UDPset_flow("127.1.2.5", "127.0.0.1", g_uecho1, PB_ACT_PROXY, 2);c2 = udp_client();CHECK(udp_echo_ok(c2, "127.1.2.5", "auth-udp"), "UDP via authenticated SOCKS5");close(c2);- // directset_flow("127.1.3.1", "127.0.0.1", g_uecho1, PB_ACT_DIRECT, 0);set_flow("127.1.3.2", "127.0.0.1", g_uecho2, PB_ACT_DIRECT, 0);CHECK(udp_echo_ok(c, "127.1.3.1", "direct-1") && udp_echo_ok(c, "127.1.3.2", "direct-2"), "UDP DIRECT, two destinations");- // block / unknown flow / non-loopbackset_flow("127.1.4.1", "127.0.0.1", g_uecho1, PB_ACT_BLOCK, 0);udp_send_flow(c, "127.1.4.1", "blocked");CHECK(udp_recv_neg(c, b, sizeof b, ip, &sp) < 0, "UDP BLOCK dropped");udp_send_flow(c, "127.9.9.9", "unknown");CHECK(udp_recv_neg(c, b, sizeof b, ip, &sp) < 0, "UDP unknown flow dropped");- // HTTP proxy cannot do UDPlog_clear();set_flow("127.1.4.2", "127.0.0.1", g_uecho1, PB_ACT_PROXY, 3);udp_send_flow(c, "127.1.4.2", "via-http");udp_send_flow(c, "127.1.4.2", "via-http2");CHECK(udp_recv_neg(c, b, sizeof b, ip, &sp) < 0 && log_has("HTTP proxy"), "UDP via HTTP proxy dropped + logged");- // DECIDE cached per (client, flow)set_decision((struct pb_decision){ PB_ACT_DIRECT, 0 });set_flow("127.1.4.3", "127.0.0.1", g_uecho1, PB_ACT_DECIDE, 0);g_decide_calls = 0;bool ok = udp_echo_ok(c, "127.1.4.3", "d1") && udp_echo_ok(c, "127.1.4.3", "d2") && udp_echo_ok(c, "127.1.4.3", "d3");CHECK(ok && g_decide_calls == 1, "UDP DECIDE -> DIRECT, decided once (calls=%d)", (int)g_decide_calls);- // bad proxy for UDP: nothing comes back, no crashset_flow("127.1.4.4", "127.0.0.1", g_uecho1, PB_ACT_PROXY, 6);udp_send_flow(c, "127.1.4.4", "dead-proxy");CHECK(udp_recv_neg(c, b, sizeof b, ip, &sp) < 0, "UDP via dead SOCKS5 proxy: no reply, relay survives");@@ -1098,7 +1299,7 @@ static void test_udp_idle(void)set_flow("127.1.5.2", "127.0.0.1", g_uecho2, PB_ACT_PROXY, 1);int base = count_fds();bool ok = udp_echo_ok(c, "127.1.5.1", "x") && udp_echo_ok(c, "127.1.5.2", "y");- int mid = count_fds(); // direct: +1 fd; socks5: +ctl +udp in relay and +3 in the fake server+ int mid = count_fds();int end = settle_fds(base, 15000);CHECK(ok && mid > base && end <= base, "UDP sessions expire after idle and free their fds (base=%d mid=%d end=%d)", base, mid, end);CHECK(udp_echo_ok(c, "127.1.5.1", "again"), "new session after expiry works");@@ -1128,7 +1329,6 @@ static void test_start_stop(void)int after = settle_fds(before, 5000);CHECK(bad == 0 && after <= before, "start/stop x20 (with traffic): fds before=%d after=%d, failures=%d", before, after, bad);- // start while ports are taken: must fail and leak nothingint l = socket(AF_INET, SOCK_STREAM, 0);int one = 1;setsockopt(l, SOL_SOCKET, SO_REUSEADDR, &one, sizeof one);@@ -1157,9 +1357,6 @@ static void test_start_stop(void)static void test_proxy_helper(void){char b[256];- // HTTP target on port 80 is not available on loopback in tests: use a tiny HTTP server through the proxies.- struct srvcfg hc = { 0 };- (void)hc;int r = pb_relay_test_proxy(&g_px[1], "echo.test", g_echo->port, b, sizeof b);CHECK(r == 0 && !strcmp(b, "Tunnel established"), "test_proxy SOCKS5 domain ATYP, non-80 port: '%s'", b);r = pb_relay_test_proxy(&g_px[2], "127.0.0.1", g_echo->port, b, sizeof b);@@ -1174,12 +1371,6 @@ static void test_proxy_helper(void)CHECK(r == -1 && strstr(b, "refused"), "test_proxy unreachable target -> error: '%s'", b);}-// Tiny HTTP origin to exercise the port-80 HEAD path of pb_relay_test_proxy (it listens on a high port, so we-// verify the HEAD logic via a SOCKS5 server mapping port 80 is not possible; instead call with the origin port and-// confirm the tunnel path only. HEAD logic is covered by the echo server answering the request verbatim.)-// ----------------------------------------------------------------------------------------------// Network namespace so that we can bind port 53 without real root-// ---------------------------------------------------------------------------------------------static bool loopback_ok(void){int l = socket(AF_INET, SOCK_STREAM, 0), c = socket(AF_INET, SOCK_STREAM, 0);@@ -1211,7 +1402,7 @@ static bool can_bind53(void)static bool isolate_for_port53(void){bool force_no_lo = getenv("PB_TEST_FORCE_NO_LO") != NULL;- if (!force_no_lo && can_bind53() && loopback_ok()) return true; // already privileged enough+ if (!force_no_lo && can_bind53() && loopback_ok()) return true;uid_t uid = getuid();gid_t gid = getgid();if (unshare(CLONE_NEWUSER | CLONE_NEWNET) != 0) { printf("NOTE DNS child: unshare failed: %s\n", strerror(errno)); return false; }@@ -1235,13 +1426,9 @@ static bool isolate_for_port53(void)return true;}-// Shared with the forked DNS child.struct shared { int pass, fail, status; }; // status: 0 = not run, 1 = ran, 3 = cannot isolatestatic struct shared *g_shared;-// ----------------------------------------------------------------------------------------------// DNS tap tests-// ---------------------------------------------------------------------------------------------static void mk_msg(int mode, int idx, uint8_t *m, size_t len){for (size_t i = 0; i < len; i++) m[i] = (uint8_t)(mode * 31 + idx * 17 + (int)i * 7);@@ -1257,14 +1444,14 @@ static void dns_tcp_handler(int fd, const struct srvcfg *c)if (n > sizeof q || rd_full(fd, q, n)) return;int mode = q[0];uint8_t out[300];- if (mode == 'a') { // one 100-byte message, written one byte at a time+ if (mode == 'a') {out[0] = 0; out[1] = 100; mk_msg(mode, 0, out + 2, 100);for (int i = 0; i < 102; i++) { wr_full(fd, out + i, 1); usleep(1500); }- } else if (mode == 'b') { // two messages in a single write+ } else if (mode == 'b') {out[0] = 0; out[1] = 40; mk_msg(mode, 0, out + 2, 40);out[42] = 0; out[43] = 60; mk_msg(mode, 1, out + 44, 60);wr_full(fd, out, 104);- } else { // 'c': plain single write+ } else {out[0] = 0; out[1] = 100; mk_msg(mode, 0, out + 2, 100);wr_full(fd, out, 102);}@@ -1344,11 +1531,9 @@ static void test_dns(void)dns_tcp_case("via HTTP CONNECT 1-byte writes", PB_ACT_PROXY, 3, 'a', 1);dns_tcp_case("via HTTP CONNECT two messages", PB_ACT_PROXY, 3, 'b', 2);- // non-53 TCP must not be observedatomic_store(&g_obs_n, 0);CHECK(tcp_roundtrip("127.0.0.1", g_echo->port, PB_ACT_DIRECT, 0, 1000, false) == 1000 && g_obs_n == 0, "TCP non-53 not observed");- // UDPstruct { const char *name; int action; uint32_t px; const char *flow; } u[] = {{ "UDP DIRECT", PB_ACT_DIRECT, 0, "127.1.7.1" }, { "UDP via SOCKS5", PB_ACT_PROXY, 1, "127.1.7.2" } };for (int k = 0; k < 2; k++) {@@ -1366,7 +1551,6 @@ static void test_dns(void)CHECK(ok, "%s DNS tap: reply observed (calls=%d) before delivery, from flow address", u[k].name, (int)g_obs_n);close(c);}- // non-53 UDP not observedatomic_store(&g_obs_n, 0);int c = udp_client();set_flow("127.1.7.3", "127.0.0.1", g_uecho1, PB_ACT_DIRECT, 0);@@ -1375,9 +1559,6 @@ static void test_dns(void)atomic_store(&g_obs_sleep, 0);}-// ----------------------------------------------------------------------------------------------// Latency, hostname proxies, UDP sizes / odd servers-// ---------------------------------------------------------------------------------------------static double pingpong_us(int fd, int n){char b = 'x';@@ -1530,7 +1711,6 @@ static void init_world(void)struct srvcfg ca = { .auth = true }; strcpy(ca.user, "alice"); strcpy(ca.pass, "secret");g_s5auth = srv_start(socks_handler, &ca);g_http = srv_start(http_handler, &c);- // HTTP auth: server expects base64("alice:secret") == "YWxpY2U6c2VjcmV0"struct srvcfg ch = { .auth = true }; strcpy(ch.pass, "YWxpY2U6c2VjcmV0");g_httpauth = srv_start(http_handler, &ch);struct srvcfg cl = { .leftover = true };@@ -1596,6 +1776,7 @@ int main(void)g_cur = "pb_relay_start";CHECK(relay_up(300, 60, 0, 5) == 0, "pb_relay_start");RUN(test_tcp_basic);+ RUN(test_tcp_identity);RUN(test_tcp_bad_proxy);RUN(test_tcp_halfclose);RUN(test_udp);@@ -1605,7 +1786,9 @@ int main(void)RUN(test_latency);RUN(test_tcp_large);RUN(test_throughput);+ RUN(test_perf);RUN(test_tcp_10k);+ RUN(test_churn);RUN(test_tcp_idle);RUN(test_tcp_cap);RUN(test_handshake_timeout);diff --git a/Linux/src/tests/run_relay_test.sh b/Linux/src/tests/run_relay_test.shindex cab1f8f..c9cf79d 100755--- a/Linux/src/tests/run_relay_test.sh+++ b/Linux/src/tests/run_relay_test.sh@@ -11,5 +11,5 @@ if [ "$mode" = plain ] || [ "$mode" = both ]; thenfiif [ "$mode" = asan ] || [ "$mode" = both ]; thengcc $CF -fsanitize=address,undefined -fno-omit-frame-pointer relay_test.c ../pb_relay.c -o "$out/relay_test_asan" -lpthread- PB_TEST_WATCHDOG="${PB_TEST_WATCHDOG:-900}" LARGE_MB="${LARGE_MB:-4}" THRU_MB="${THRU_MB:-32}" ASAN_OPTIONS=detect_leaks=1 UBSAN_OPTIONS=halt_on_error=1:print_stacktrace=1 "$out/relay_test_asan"+ PB_TEST_WATCHDOG="${PB_TEST_WATCHDOG:-900}" LARGE_MB="${LARGE_MB:-4}" THRU_MB="${THRU_MB:-32}" PB_CHURN_TCP="${PB_CHURN_TCP:-10000}" PB_CHURN_UDP="${PB_CHURN_UDP:-3000}" PB_CHURN_RSS_MB="${PB_CHURN_RSS_MB:-48}" PB_PERF_UDP=10000 PB_PERF_CONNS=400 ASAN_OPTIONS=detect_leaks=1:quarantine_size_mb=1 UBSAN_OPTIONS=halt_on_error=1:print_stacktrace=1 "$out/relay_test_asan"fi