From 616986457f8ccebbd742299b107a21e557089045 Mon Sep 17 00:00:00 2001 From: Evgeny Date: Sun, 31 May 2026 01:58:44 +0300 Subject: [PATCH] add DEBUG_ERROR in all silent proxy error paths: NULL checks, alloc fails, transport send fail, data drop --- src/remote_proxy.c | 8 ++++++-- src/tcp_proxy.c | 16 +++++++++------- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/remote_proxy.c b/src/remote_proxy.c index b4551eaa..8a4a3d18 100644 --- a/src/remote_proxy.c +++ b/src/remote_proxy.c @@ -69,7 +69,7 @@ static int rp_conn_total(struct remote_proxy_conn* rc) { // ==================================================================== static void rp_sock_read_cb(socket_t sock, void* arg) { (void)sock; struct remote_proxy_conn* rc = (struct remote_proxy_conn*)arg; - if (!rc || rc->sock == SOCKET_INVALID) return; + if (!rc || rc->sock == SOCKET_INVALID) { DEBUG_ERROR(DEBUG_CATEGORY_SOCKET, "RP rp_sock_read_cb: rc=%p sock=%d", (void*)rc, rc ? (int)rc->sock : -1); return; } uint8_t buf[8192]; ssize_t n = recv(rc->sock, buf, sizeof(buf), 0); if (n > 0) { DEBUG_INFO(DEBUG_CATEGORY_SOCKET, "SOCK:RECV fd=%d stream=%016llx len=%zd total=%d", (int)rc->sock, (unsigned long long)rc->stream_id, n, rp_conn_total(rc)); @@ -208,7 +208,11 @@ int remote_proxy_handle_data(struct UTUN_INSTANCE* inst, struct ll_entry* entry, if (!inst) { queue_dgram_free(entry); queue_entry_free(entry); return -1; } struct remote_proxy_ctx* ctx = &inst->remote_proxy; struct remote_proxy_conn* rc = remote_proxy_find_conn(ctx, stream_id); - if (!rc || rc->sock == SOCKET_INVALID || rc->connected != 1) { queue_dgram_free(entry); queue_entry_free(entry); return -1; } + if (!rc || rc->sock == SOCKET_INVALID || rc->connected != 1) { + DEBUG_ERROR(DEBUG_CATEGORY_SOCKET, "RP handle_data drop: rc=%p stream=%016llx sock=%d connected=%d", + (void*)rc, (unsigned long long)stream_id, rc ? (int)rc->sock : -1, rc ? rc->connected : -1); + queue_dgram_free(entry); queue_entry_free(entry); return -1; + } uint16_t seq; memcpy(&seq, entry->dgram + 10, 2); if (!rc->recv_seq_init) { rc->recv_last_seq = seq; rc->recv_seq_init = 1; diff --git a/src/tcp_proxy.c b/src/tcp_proxy.c index 1d35b0ef..91c0aecd 100644 --- a/src/tcp_proxy.c +++ b/src/tcp_proxy.c @@ -216,6 +216,7 @@ static void proxy_feed_from_transport(struct proxy_conn *pc) { if (len >= e->len) { queue_dgram_free(e); queue_entry_free(e); } else { memmove(e->dgram, e->dgram + len, e->len - len); e->len -= len; queue_data_put_first(pc->transport_to_uip, e); break; } } else { + DEBUG_ERROR(DEBUG_CATEGORY_SOCKET, "PROXY FEED tcp_write failed stream=%016llx len=%u ret=%d", (unsigned long long)pc->remote_stream_id, len, ret); queue_data_put_first(pc->transport_to_uip, e); break; } @@ -270,7 +271,7 @@ static err_t proxy_accept_cb(void *arg, struct tcp_pcb *newpcb, err_t err) { static err_t proxy_recv_cb(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err) { struct proxy_conn *pc = (struct proxy_conn *)arg; - if (!pc) { if (p) pbuf_free(p); return LERR_OK; } + if (!pc) { if (p) pbuf_free(p); DEBUG_ERROR(DEBUG_CATEGORY_SOCKET, "PROXY proxy_recv_cb: pc=NULL"); return LERR_OK; } if (p == NULL || err != LERR_OK) { DEBUG_INFO(DEBUG_CATEGORY_SOCKET, "PROXY FIN stream=%016llx tun=1 active=%d", (unsigned long long)(pc->remote_stream_id ? pc->remote_stream_id : 0), pc->active); @@ -296,6 +297,7 @@ static err_t proxy_recv_cb(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t pbuf_copy_partial(p, data, len, 0); int ret = pc->transport->ops->send(pc->transport, data, len); if (ret < 0) { + DEBUG_ERROR(DEBUG_CATEGORY_SOCKET, "PROXY send failed stream=%016llx ret=%d len=%u", (unsigned long long)pc->remote_stream_id, ret, len); struct ll_entry *e = entry_from_data(pc->proxy->entry_pool, data, len); if (e) queue_data_put(pc->uip_to_transport, e); } @@ -317,7 +319,7 @@ static err_t proxy_sent_cb(void *arg, struct tcp_pcb *pcb, uint16_t len) { static void proxy_err_cb(void *arg, err_t err) { struct proxy_conn *pc = (struct proxy_conn *)arg; - if (!pc) return; + if (!pc) { DEBUG_ERROR(DEBUG_CATEGORY_SOCKET, "PROXY proxy_err_cb: pc=NULL err=%d", err); return; } DEBUG_ERROR(DEBUG_CATEGORY_SOCKET, "TCP proxy: tcp error %d", err); pc->closing_tun = 1; if (pc->transport && !pc->closing_sent) pc->transport->ops->close(pc->transport); @@ -340,7 +342,7 @@ static void proxy_try_close(struct proxy_conn *pc) { static err_t proxy_poll_cb(void *arg, struct tcp_pcb *pcb) { struct proxy_conn *pc = (struct proxy_conn *)arg; - if (!pc) return LERR_OK; + if (!pc) { DEBUG_ERROR(DEBUG_CATEGORY_SOCKET, "PROXY proxy_poll_cb: pc=NULL"); return LERR_OK; } proxy_try_close(pc); if (pc->closing_tun && (pc->closing_rem || pc->closing_sent) && pc->pcb) { DEBUG_INFO(DEBUG_CATEGORY_SOCKET, "PROXY CLEANUP stream=%016llx tun=1 rem=%d sent=%d cwnd=%u sndbuf=%u unsent=%p unacked=%p", @@ -611,11 +613,11 @@ static void sock_transport_error_callback(socket_t sock, void* arg) { // ==================================================================== static int etcp_transport_send(struct tcp_proxy_transport* t, const uint8_t* data, size_t len) { struct etcp_transport* et = (struct etcp_transport*)t; - if (!et->connected) return -1; + if (!et->connected) { DEBUG_INFO(DEBUG_CATEGORY_SOCKET, "PROXY send not connected stream=%016llx len=%zu", (unsigned long long)et->stream_id, len); return -1; } struct ll_entry* e = queue_entry_new(0); - if (!e) return -1; + if (!e) { DEBUG_ERROR(DEBUG_CATEGORY_SOCKET, "PROXY send alloc entry fail stream=%016llx", (unsigned long long)et->stream_id); return -1; } e->dgram = u_malloc(TCP_PROXY_HDR_SIZE + len); - if (!e->dgram) { queue_entry_free(e); return -1; } + if (!e->dgram) { DEBUG_ERROR(DEBUG_CATEGORY_SOCKET, "PROXY send alloc dgram fail stream=%016llx len=%zu", (unsigned long long)et->stream_id, len); queue_entry_free(e); return -1; } e->dgram[0] = ETCP_ID_TCP_PROXY; e->dgram[1] = TCP_PROXY_SUBCMD_DATA; memcpy(e->dgram + 2, &et->stream_id, 8); memcpy(e->dgram + 10, &et->send_seq, 2); @@ -691,7 +693,7 @@ static void handle_connected(struct tcp_proxy* p, uint64_t stream_id, struct ll_ return; } struct etcp_transport* et = (struct etcp_transport*)pc->transport; - if (et->base.ops != &etcp_transport_ops) { queue_dgram_free(entry); queue_entry_free(entry); return; } + if (et->base.ops != &etcp_transport_ops) { DEBUG_ERROR(DEBUG_CATEGORY_SOCKET, "PROXY CONNECTED wrong ops stream=%016llx", (unsigned long long)stream_id); queue_dgram_free(entry); queue_entry_free(entry); return; } if (entry->len >= TCP_PROXY_CONNECTED_HDR_SIZE) { uint8_t status = entry->dgram[TCP_PROXY_HDR_SIZE + 2]; if (status == TCP_PROXY_CONNECTED_OK) { et->connected = 1;