You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
381 lines
20 KiB
381 lines
20 KiB
// remote_proxy.c — Удаленный TCP прокси (exit node) |
|
#include "remote_proxy.h" |
|
#include "tcp_proxy.h" |
|
#include "udp_proxy.h" |
|
#include "icmp_proxy.h" |
|
#include "etcp.h" |
|
#include "etcp_api.h" |
|
#include "etcp_router.h" |
|
#include "utun_instance.h" |
|
#include "config_parser.h" |
|
#include "../lib/u_async.h" |
|
#include "../lib/debug_config.h" |
|
#include "../lib/ll_queue.h" |
|
#include "../lib/mem.h" |
|
#include <stdlib.h> |
|
#include <string.h> |
|
#include <errno.h> |
|
#ifndef _WIN32 |
|
#include <unistd.h> |
|
#include <sys/socket.h> |
|
#include <netinet/in.h> |
|
#include <arpa/inet.h> |
|
#include <fcntl.h> |
|
#endif |
|
#ifndef MSG_NOSIGNAL |
|
#define MSG_NOSIGNAL 0 |
|
#endif |
|
|
|
static struct remote_proxy_ctx* g_rp_ctx = NULL; |
|
|
|
static void rp_sock_read_cb(socket_t sock, void* arg); |
|
static void rp_sock_write_cb(socket_t sock, void* arg); |
|
static void rp_sock_error_cb(socket_t sock, void* arg); |
|
static void rp_sock_retry_cb(void* arg); |
|
static void rp_sock_pause_cb(void* arg); |
|
static void rp_sock_send(struct remote_proxy_conn* rc, const uint8_t* data, size_t len); |
|
void rp_conn_free(struct remote_proxy_conn* rc); |
|
|
|
#define RP_NORMALIZER_Q_THRESHOLD 64 |
|
|
|
static int rp_send_msg(struct UTUN_INSTANCE* inst, uint64_t dst, uint8_t subcmd, |
|
uint32_t sid, const uint8_t* data, size_t len) { |
|
struct ll_entry* e = queue_entry_new(0); |
|
if (!e) { DEBUG_ERROR(DEBUG_CATEGORY_SOCKET, "rp_send_msg: queue_entry_new failed subcmd=%02x sid=%08x", subcmd, sid); return -1; } |
|
e->dgram = u_malloc(TCP_PROXY_HDR_SIZE + len); |
|
if (!e->dgram) { DEBUG_ERROR(DEBUG_CATEGORY_SOCKET, "rp_send_msg: malloc(%zu) failed subcmd=%02x sid=%08x", TCP_PROXY_HDR_SIZE + len, subcmd, sid); queue_entry_free(e); return -1; } |
|
e->dgram[0] = ETCP_ID_TCP_PROXY; |
|
e->dgram[1] = subcmd; |
|
memcpy(e->dgram + 2, &sid, 4); |
|
if (len > 0) memcpy(e->dgram + TCP_PROXY_HDR_SIZE, data, len); |
|
e->len = TCP_PROXY_HDR_SIZE + len; |
|
return etcp_route_send(inst, dst, e); |
|
} |
|
|
|
static int rp_send_connected(struct UTUN_INSTANCE* inst, uint64_t dst, uint32_t sid, |
|
uint16_t local_port, uint8_t status) { |
|
uint8_t buf[3]; |
|
memcpy(buf, &local_port, 2); buf[2] = status; |
|
return rp_send_msg(inst, dst, TCP_PROXY_SUBCMD_CONNECTED, sid, buf, 3); |
|
} |
|
|
|
static int rp_conn_total(struct remote_proxy_conn* rc) { |
|
if (!rc || !rc->ctx) return 0; |
|
int n = 0; struct remote_proxy_conn* c; |
|
for (c = rc->ctx->conns; c; c = c->next) n++; |
|
return n; |
|
} |
|
|
|
// ==================================================================== |
|
// Неблокирующий send с EAGAIN и таймером повтора |
|
// ==================================================================== |
|
static int rp_sock_try_send(struct remote_proxy_conn* rc) { |
|
if (!rc || rc->sock == SOCKET_INVALID || !rc->out_buf) return 0; |
|
while (rc->out_off < rc->out_len) { |
|
ssize_t n = send(rc->sock, rc->out_buf + rc->out_off, rc->out_len - rc->out_off, MSG_NOSIGNAL); |
|
if (n > 0) { |
|
rc->out_off += n; |
|
if (rc->out_off >= rc->out_len) { |
|
u_free(rc->out_buf); rc->out_buf = NULL; rc->out_len = rc->out_off = 0; |
|
return 0; |
|
} |
|
continue; |
|
} |
|
if (n < 0 && (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR)) return 1; |
|
DEBUG_ERROR(DEBUG_CATEGORY_SOCKET, "SOCK:SEND_ERR fd=%d sid=%08x err=%s", (int)rc->sock, rc->stream_id, strerror(errno)); |
|
return -1; |
|
} |
|
return 0; |
|
} |
|
|
|
static void rp_sock_retry_cb(void* arg) { |
|
struct remote_proxy_conn* rc = (struct remote_proxy_conn*)arg; |
|
if (!rc || !rc->out_buf) { rc->out_timer = NULL; return; } |
|
int ret = rp_sock_try_send(rc); |
|
if (ret == 0) { |
|
rc->out_timer = NULL; rc->out_backoff = 0; |
|
} else if (ret == 1) { |
|
rc->out_backoff = rc->out_backoff < 20 ? 20 : rc->out_backoff * 2; |
|
if (rc->out_backoff > 5000) rc->out_backoff = 5000; |
|
rc->out_timer = uasync_set_timeout(rc->ua, rc->out_backoff, rc, rp_sock_retry_cb, "rp_send"); |
|
} else { |
|
u_free(rc->out_buf); rc->out_buf = NULL; rc->out_len = rc->out_off = 0; |
|
rc->out_timer = NULL; rc->out_backoff = 0; |
|
} |
|
} |
|
|
|
static void rp_sock_send(struct remote_proxy_conn* rc, const uint8_t* data, size_t len) { |
|
if (!rc || !data || len == 0) return; |
|
if (rc->out_buf) { |
|
size_t new_len = rc->out_len + len; |
|
uint8_t* new_buf = u_realloc(rc->out_buf, new_len); |
|
if (!new_buf) { DEBUG_ERROR(DEBUG_CATEGORY_SOCKET, "SOCK:SEND realloc(%zu) failed sid=%08x", new_len, rc->stream_id); return; } |
|
memcpy(new_buf + rc->out_len, data, len); |
|
rc->out_buf = new_buf; rc->out_len = new_len; |
|
return; |
|
} |
|
rc->out_buf = u_malloc(len); |
|
if (!rc->out_buf) { DEBUG_ERROR(DEBUG_CATEGORY_SOCKET, "SOCK:SEND malloc(%zu) failed sid=%08x", len, rc->stream_id); return; } |
|
memcpy(rc->out_buf, data, len); rc->out_len = len; rc->out_off = 0; |
|
int ret = rp_sock_try_send(rc); |
|
if (ret == 0) return; |
|
if (ret == 1) { |
|
rc->out_backoff = 10; |
|
rc->out_timer = uasync_set_timeout(rc->ua, rc->out_backoff, rc, rp_sock_retry_cb, "rp_send"); |
|
} else { |
|
u_free(rc->out_buf); rc->out_buf = NULL; rc->out_len = rc->out_off = 0; |
|
} |
|
} |
|
|
|
static void rp_sock_pause_cb(void* arg) { |
|
struct remote_proxy_conn* rc = (struct remote_proxy_conn*)arg; |
|
if (!rc || rc->sock == SOCKET_INVALID) { rc->pause_timer = NULL; return; } |
|
struct UTUN_INSTANCE* inst = rc->ctx ? rc->ctx->inst : NULL; |
|
int qcnt = -1; |
|
if (inst) qcnt = etcp_router_input_q_count(inst, rc->peer_node_id); |
|
if (qcnt > RP_NORMALIZER_Q_THRESHOLD) { |
|
rc->pause_backoff = rc->pause_backoff * 2; |
|
if (rc->pause_backoff > 5000) rc->pause_backoff = 5000; |
|
rc->pause_timer = uasync_set_timeout(rc->ua, rc->pause_backoff, rc, rp_sock_pause_cb, "rp_pause"); |
|
return; |
|
} |
|
if (rc->pause_buf && rc->pause_len > 0) { |
|
DEBUG_INFO(DEBUG_CATEGORY_SOCKET, "SOCK:PAUSE_FLUSH fd=%d sid=%08x len=%zu qcnt=%d", (int)rc->sock, rc->stream_id, rc->pause_len, qcnt); |
|
rp_send_msg(inst, rc->peer_node_id, TCP_PROXY_SUBCMD_DATA, rc->stream_id, rc->pause_buf, rc->pause_len); |
|
u_free(rc->pause_buf); rc->pause_buf = NULL; rc->pause_len = 0; |
|
} |
|
rc->read_id = uasync_add_socket_t(rc->ua, rc->sock, rp_sock_read_cb, rp_sock_write_cb, rp_sock_error_cb, rc); |
|
rc->pause_timer = NULL; |
|
} |
|
|
|
// ==================================================================== |
|
// Socket callbacks |
|
// ==================================================================== |
|
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; |
|
uint8_t buf[8192]; ssize_t n = recv(rc->sock, buf, sizeof(buf), 0); |
|
if (n > 0) { |
|
if (rc->cli_closed || rc->error) return; |
|
struct UTUN_INSTANCE* inst = rc->ctx ? rc->ctx->inst : NULL; |
|
if (!inst) { DEBUG_ERROR(DEBUG_CATEGORY_SOCKET, "SOCK:RECV fd=%d sid=%08x — inst is NULL, drop", (int)rc->sock, rc->stream_id); return; } |
|
int qcnt = etcp_router_input_q_count(inst, rc->peer_node_id); |
|
if (qcnt > RP_NORMALIZER_Q_THRESHOLD) { |
|
DEBUG_INFO(DEBUG_CATEGORY_SOCKET, "SOCK:PAUSE fd=%d sid=%08x len=%zd qcnt=%d — normalizer переполнен, пауза", (int)rc->sock, rc->stream_id, n, qcnt); |
|
rc->pause_buf = u_malloc(n); |
|
if (rc->pause_buf) { memcpy(rc->pause_buf, buf, n); rc->pause_len = n; } |
|
else DEBUG_ERROR(DEBUG_CATEGORY_SOCKET, "SOCK:PAUSE malloc(%zd) failed sid=%08x", n, rc->stream_id); |
|
if (rc->read_id) { uasync_remove_socket_t(rc->ua, rc->sock); rc->read_id = NULL; } |
|
rc->pause_backoff = 50; |
|
rc->pause_timer = uasync_set_timeout(rc->ua, rc->pause_backoff, rc, rp_sock_pause_cb, "rp_pause"); |
|
return; |
|
} |
|
DEBUG_INFO(DEBUG_CATEGORY_SOCKET, "SOCK:RECV fd=%d sid=%08x len=%zd total=%d", (int)rc->sock, rc->stream_id, n, rp_conn_total(rc)); |
|
rp_send_msg(inst, rc->peer_node_id, TCP_PROXY_SUBCMD_DATA, rc->stream_id, buf, (size_t)n); |
|
} else if (n == 0) { |
|
DEBUG_INFO(DEBUG_CATEGORY_SOCKET, "SOCK:EOF fd=%d sid=%08x total=%d cli_closed=%d sock_closed=%d", |
|
(int)rc->sock, rc->stream_id, rp_conn_total(rc), rc->cli_closed, rc->sock_closed); |
|
rc->sock_closed = 1; |
|
struct UTUN_INSTANCE* inst = rc->ctx ? rc->ctx->inst : NULL; |
|
if (inst) rp_send_msg(inst, rc->peer_node_id, TCP_PROXY_SUBCMD_CLOSE, rc->stream_id, NULL, 0); |
|
else DEBUG_ERROR(DEBUG_CATEGORY_SOCKET, "SOCK:EOF fd=%d sid=%08x — inst is NULL, can't send CLOSE", (int)rc->sock, rc->stream_id); |
|
if (rc->read_id) { uasync_remove_socket_t(rc->ua, rc->sock); rc->read_id = NULL; } |
|
if (rc->cli_closed) rp_conn_free(rc); |
|
} else if (errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR) { |
|
DEBUG_ERROR(DEBUG_CATEGORY_SOCKET, "SOCK:ERROR fd=%d sid=%08x errno=%d %s total=%d", |
|
(int)rc->sock, rc->stream_id, errno, strerror(errno), rp_conn_total(rc)); |
|
rc->error = 1; |
|
struct UTUN_INSTANCE* inst = rc->ctx ? rc->ctx->inst : NULL; |
|
if (inst) rp_send_msg(inst, rc->peer_node_id, TCP_PROXY_SUBCMD_ERROR, rc->stream_id, NULL, 0); |
|
else DEBUG_ERROR(DEBUG_CATEGORY_SOCKET, "SOCK:ERROR fd=%d sid=%08x — inst is NULL, can't send ERROR", (int)rc->sock, rc->stream_id); |
|
rp_conn_free(rc); |
|
} |
|
} |
|
|
|
static void rp_sock_write_cb(socket_t sock, void* arg) { |
|
(void)sock; struct remote_proxy_conn* rc = (struct remote_proxy_conn*)arg; |
|
if (!rc || rc->sock == SOCKET_INVALID || rc->connected) return; |
|
int err = 0; socklen_t len = sizeof(err); |
|
if (getsockopt(rc->sock, SOL_SOCKET, SO_ERROR, &err, &len) == 0 && err == 0) { |
|
rc->connected = 1; |
|
struct sockaddr_in local; socklen_t llen = sizeof(local); |
|
uint16_t local_port = 0; |
|
if (getsockname(rc->sock, (struct sockaddr*)&local, &llen) == 0) local_port = local.sin_port; |
|
DEBUG_INFO(DEBUG_CATEGORY_SOCKET, "SOCK:CONN fd=%d sid=%08x local=%d dest=%d.%d.%d.%d:%d total=%d", |
|
(int)rc->sock, rc->stream_id, ntohs(local_port), |
|
rc->dest_ip[0], rc->dest_ip[1], rc->dest_ip[2], rc->dest_ip[3], ntohs(rc->dest_port), |
|
rp_conn_total(rc)); |
|
struct UTUN_INSTANCE* inst = rc->ctx ? rc->ctx->inst : NULL; |
|
if (inst) rp_send_connected(inst, rc->peer_node_id, rc->stream_id, local_port, TCP_PROXY_CONNECTED_OK); |
|
if (rc->pending_buf && rc->pending_len > 0) { |
|
DEBUG_INFO(DEBUG_CATEGORY_SOCKET, "SOCK:FLUSH fd=%d sid=%08x len=%zu", (int)rc->sock, rc->stream_id, rc->pending_len); |
|
rp_sock_send(rc, rc->pending_buf, rc->pending_len); |
|
u_free(rc->pending_buf); rc->pending_buf = NULL; rc->pending_len = 0; |
|
} |
|
} else { |
|
DEBUG_ERROR(DEBUG_CATEGORY_SOCKET, "SOCK:FAIL fd=%d sid=%08x err=%d %s", |
|
(int)rc->sock, rc->stream_id, err, err ? strerror(err) : "unknown"); |
|
struct UTUN_INSTANCE* inst = rc->ctx ? rc->ctx->inst : NULL; |
|
if (inst) rp_send_connected(inst, rc->peer_node_id, rc->stream_id, 0, TCP_PROXY_CONNECTED_REFUSED); |
|
rp_conn_free(rc); |
|
} |
|
} |
|
|
|
static void rp_sock_error_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; |
|
DEBUG_ERROR(DEBUG_CATEGORY_SOCKET, "SOCK:ERR fd=%d sid=%08x", (int)rc->sock, rc->stream_id); |
|
rc->error = 1; |
|
struct UTUN_INSTANCE* inst = rc->ctx ? rc->ctx->inst : NULL; |
|
if (inst) rp_send_msg(inst, rc->peer_node_id, TCP_PROXY_SUBCMD_ERROR, rc->stream_id, NULL, 0); |
|
else DEBUG_ERROR(DEBUG_CATEGORY_SOCKET, "SOCK:ERR fd=%d sid=%08x — inst is NULL", (int)rc->sock, rc->stream_id); |
|
rp_conn_free(rc); |
|
} |
|
|
|
void rp_conn_free(struct remote_proxy_conn* rc) { |
|
if (!rc) return; |
|
int total = rp_conn_total(rc); |
|
DEBUG_INFO(DEBUG_CATEGORY_SOCKET, "SOCK:FREE fd=%d sid=%08x total=%d cli_closed=%d sock_closed=%d error=%d", |
|
(int)rc->sock, rc->stream_id, total, rc->cli_closed, rc->sock_closed, rc->error); |
|
if (rc->ctx) { |
|
struct remote_proxy_conn** prev = &rc->ctx->conns; |
|
while (*prev) { if (*prev == rc) { *prev = rc->next; rc->ctx->conn_count--; break; } prev = &(*prev)->next; } |
|
} |
|
if (rc->sock != SOCKET_INVALID) { |
|
if (rc->read_id) { uasync_remove_socket_t(rc->ua, rc->sock); rc->read_id = NULL; } |
|
socket_close_wrapper(rc->sock); rc->sock = SOCKET_INVALID; |
|
} |
|
if (rc->pending_buf) { u_free(rc->pending_buf); rc->pending_buf = NULL; } |
|
if (rc->out_timer) { uasync_cancel_timeout(rc->ua, rc->out_timer); rc->out_timer = NULL; } |
|
if (rc->out_buf) { u_free(rc->out_buf); rc->out_buf = NULL; } |
|
if (rc->pause_timer) { uasync_cancel_timeout(rc->ua, rc->pause_timer); rc->pause_timer = NULL; } |
|
if (rc->pause_buf) { u_free(rc->pause_buf); rc->pause_buf = NULL; } |
|
u_free(rc); |
|
} |
|
|
|
// ==================================================================== |
|
// Public API |
|
// ==================================================================== |
|
struct remote_proxy_conn* remote_proxy_find_conn(struct remote_proxy_ctx* ctx, uint32_t stream_id) { |
|
struct remote_proxy_conn* c; |
|
for (c = ctx->conns; c; c = c->next) if (c->stream_id == stream_id) return c; |
|
return NULL; |
|
} |
|
|
|
int remote_proxy_handle_connect(struct UTUN_INSTANCE* inst, struct ll_entry* entry, |
|
uint32_t stream_id, uint64_t src_node_id) { |
|
if (!inst || !inst->remote_proxy.enabled) { if (entry) { queue_dgram_free(entry); queue_entry_free(entry); } return -1; } |
|
struct remote_proxy_ctx* ctx = &inst->remote_proxy; |
|
if (entry->len < TCP_PROXY_CONNECT_HDR_SIZE) { DEBUG_ERROR(DEBUG_CATEGORY_SOCKET, "remote_proxy: CONNECT too short len=%u", entry->len); queue_dgram_free(entry); queue_entry_free(entry); return -1; } |
|
uint8_t* dest_ip = entry->dgram + TCP_PROXY_HDR_SIZE; |
|
uint16_t dest_port = 0; memcpy(&dest_port, dest_ip + 4, 2); |
|
|
|
struct remote_proxy_conn* rc = u_calloc(1, sizeof(struct remote_proxy_conn)); |
|
if (!rc) { DEBUG_ERROR(DEBUG_CATEGORY_SOCKET, "remote_proxy: u_calloc failed for sid=%08x", stream_id); queue_dgram_free(entry); queue_entry_free(entry); return -1; } |
|
rc->ctx = ctx; rc->stream_id = stream_id; rc->peer_node_id = src_node_id; |
|
memcpy(rc->dest_ip, dest_ip, 4); rc->dest_port = dest_port; |
|
rc->ua = inst->ua; rc->sock = SOCKET_INVALID; |
|
|
|
rc->sock = socket(AF_INET, SOCK_STREAM, 0); |
|
if (rc->sock == SOCKET_INVALID) { DEBUG_ERROR(DEBUG_CATEGORY_SOCKET, "remote_proxy: socket() failed"); u_free(rc); queue_dgram_free(entry); queue_entry_free(entry); return -1; } |
|
ctx->conn_count++; |
|
DEBUG_INFO(DEBUG_CATEGORY_SOCKET, "SOCK:NEW fd=%d sid=%08x dest=%d.%d.%d.%d:%d total=%d", |
|
(int)rc->sock, stream_id, dest_ip[0],dest_ip[1],dest_ip[2],dest_ip[3],ntohs(dest_port), ctx->conn_count); |
|
socket_set_nonblocking(rc->sock); |
|
rc->read_id = uasync_add_socket_t(rc->ua, rc->sock, rp_sock_read_cb, rp_sock_write_cb, rp_sock_error_cb, rc); |
|
if (!rc->read_id) { DEBUG_ERROR(DEBUG_CATEGORY_SOCKET, "remote_proxy: uasync_add_socket_t failed"); u_free(rc); queue_dgram_free(entry); queue_entry_free(entry); return -1; } |
|
|
|
struct sockaddr_in addr; memset(&addr, 0, sizeof(addr)); |
|
addr.sin_family = AF_INET; memcpy(&addr.sin_addr.s_addr, dest_ip, 4); addr.sin_port = dest_port; |
|
int ret = connect(rc->sock, (struct sockaddr*)&addr, sizeof(addr)); |
|
if (ret < 0 && errno != EINPROGRESS) { |
|
DEBUG_ERROR(DEBUG_CATEGORY_SOCKET, "remote_proxy: connect() to %d.%d.%d.%d:%d failed: %s", |
|
dest_ip[0], dest_ip[1], dest_ip[2], dest_ip[3], ntohs(dest_port), strerror(errno)); |
|
rp_send_connected(inst, src_node_id, stream_id, 0, TCP_PROXY_CONNECTED_REFUSED); |
|
uasync_remove_socket_t(rc->ua, rc->sock); socket_close_wrapper(rc->sock); u_free(rc); |
|
queue_dgram_free(entry); queue_entry_free(entry); return -1; |
|
} |
|
|
|
rc->next = ctx->conns; ctx->conns = rc; |
|
queue_dgram_free(entry); queue_entry_free(entry); |
|
return 0; |
|
} |
|
|
|
int remote_proxy_handle_data(struct UTUN_INSTANCE* inst, struct ETCP_CONN* conn, struct ll_entry* entry, uint32_t stream_id) { |
|
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) { |
|
DEBUG_WARN(DEBUG_CATEGORY_SOCKET, "RP handle_data drop: нет соединения для sid=%08x, шлём CLOSE", stream_id); |
|
if (conn) rp_send_msg(inst, conn->peer_node_id, TCP_PROXY_SUBCMD_CLOSE, stream_id, NULL, 0); |
|
queue_dgram_free(entry); queue_entry_free(entry); return -1; |
|
} |
|
if (rc->error) { queue_dgram_free(entry); queue_entry_free(entry); return -1; } |
|
|
|
size_t data_len = entry->len - TCP_PROXY_HDR_SIZE; |
|
if (rc->connected) { |
|
DEBUG_INFO(DEBUG_CATEGORY_SOCKET, "SOCK:SEND fd=%d sid=%08x len=%zu total=%d", |
|
(int)rc->sock, stream_id, data_len, rp_conn_total(rc)); |
|
if (data_len > 0) rp_sock_send(rc, entry->dgram + TCP_PROXY_HDR_SIZE, data_len); |
|
} else { |
|
size_t new_len = rc->pending_len + data_len; |
|
uint8_t* new_buf = u_realloc(rc->pending_buf, new_len); |
|
if (new_buf) { |
|
if (data_len > 0) memcpy(new_buf + rc->pending_len, entry->dgram + TCP_PROXY_HDR_SIZE, data_len); |
|
rc->pending_buf = new_buf; rc->pending_len = new_len; |
|
DEBUG_INFO(DEBUG_CATEGORY_SOCKET, "RP buffer sid=%08x added=%zu total=%zu", stream_id, data_len, new_len); |
|
} else DEBUG_ERROR(DEBUG_CATEGORY_SOCKET, "RP buffer realloc(%zu) failed sid=%08x", new_len, stream_id); |
|
} |
|
queue_dgram_free(entry); queue_entry_free(entry); |
|
return 0; |
|
} |
|
|
|
void remote_proxy_handle_close(struct UTUN_INSTANCE* inst, uint32_t stream_id) { |
|
if (!inst) return; |
|
struct remote_proxy_ctx* ctx = &inst->remote_proxy; |
|
struct remote_proxy_conn** prev = &ctx->conns; |
|
while (*prev) { |
|
struct remote_proxy_conn* rc = *prev; |
|
if (rc->stream_id == stream_id) { |
|
DEBUG_INFO(DEBUG_CATEGORY_SOCKET, "SOCK:CLOSE_RECV fd=%d sid=%08x total=%d sock_closed=%d connected=%d", |
|
(int)rc->sock, stream_id, rp_conn_total(rc), rc->sock_closed, rc->connected); |
|
rc->cli_closed = 1; |
|
if (rc->sock != SOCKET_INVALID && rc->connected == 1) shutdown(rc->sock, SHUT_WR); |
|
if (rc->sock_closed) rp_conn_free(rc); |
|
return; |
|
} |
|
prev = &rc->next; |
|
} |
|
DEBUG_WARN(DEBUG_CATEGORY_SOCKET, "remote_proxy: CLOSE sid=%08x — no conn", stream_id); |
|
} |
|
|
|
int remote_proxy_init(struct UTUN_INSTANCE* inst) { |
|
if (!inst) return -1; |
|
struct remote_proxy_ctx* ctx = &inst->remote_proxy; |
|
memset(ctx, 0, sizeof(*ctx)); |
|
if (!inst->config) return 0; |
|
ctx->enabled = inst->config->global.remote_proxy_enabled; |
|
ctx->inst = inst; |
|
if (!ctx->enabled) return 0; |
|
g_rp_ctx = ctx; |
|
etcp_router_bind(inst, ETCP_ID_TCP_PROXY, tcp_proxy_etcp_recv_cb); |
|
if (!inst->config->global.tcp_proxy_enabled) { |
|
udp_proxy_init(inst, inst->ua); |
|
icmp_proxy_init(inst, inst->ua); |
|
} |
|
DEBUG_INFO(DEBUG_CATEGORY_SOCKET, "remote_proxy initialized node=%016llx", (unsigned long long)inst->node_id); |
|
return 0; |
|
} |
|
|
|
void remote_proxy_destroy(struct UTUN_INSTANCE* inst) { |
|
if (!inst) return; |
|
struct remote_proxy_ctx* ctx = &inst->remote_proxy; |
|
DEBUG_INFO(DEBUG_CATEGORY_SOCKET, "remote_proxy destroying: conn_count=%d", ctx->conn_count); |
|
struct remote_proxy_conn* rc = ctx->conns; |
|
while (rc) { struct remote_proxy_conn* next = rc->next; rp_conn_free(rc); rc = next; } |
|
ctx->conns = NULL; ctx->enabled = 0; |
|
if (g_rp_ctx == ctx) g_rp_ctx = NULL; |
|
udp_proxy_destroy(inst); |
|
icmp_proxy_destroy(inst); |
|
DEBUG_INFO(DEBUG_CATEGORY_SOCKET, "remote_proxy destroyed"); |
|
}
|
|
|