|
|
|
@ -34,7 +34,7 @@ static void pause_resume_cb(struct ll_queue* q, void* arg); |
|
|
|
static void close_retry_cb(void* arg); |
|
|
|
static void close_retry_cb(void* arg); |
|
|
|
static void diag_timer_cb(void* arg); |
|
|
|
static void diag_timer_cb(void* arg); |
|
|
|
static int send_msg(struct UTUN_INSTANCE* inst, uint64_t dst, uint8_t subcmd, |
|
|
|
static int send_msg(struct UTUN_INSTANCE* inst, uint64_t dst, uint8_t subcmd, |
|
|
|
uint32_t sid, const uint8_t* data, size_t len); |
|
|
|
uint32_t sid, const uint8_t* data, size_t len, int force); |
|
|
|
static void send_close(struct tcp_proxy_server_conn* rc); |
|
|
|
static void send_close(struct tcp_proxy_server_conn* rc); |
|
|
|
static void send_error(struct tcp_proxy_server_conn* rc); |
|
|
|
static void send_error(struct tcp_proxy_server_conn* rc); |
|
|
|
void tcp_proxy_server_conn_free(struct tcp_proxy_server_conn* rc); |
|
|
|
void tcp_proxy_server_conn_free(struct tcp_proxy_server_conn* rc); |
|
|
|
@ -48,7 +48,7 @@ static inline int write_pending(struct tcp_conn* tc) { |
|
|
|
// ====================================================================
|
|
|
|
// ====================================================================
|
|
|
|
|
|
|
|
|
|
|
|
static int send_msg(struct UTUN_INSTANCE* inst, uint64_t dst, uint8_t subcmd, |
|
|
|
static int send_msg(struct UTUN_INSTANCE* inst, uint64_t dst, uint8_t subcmd, |
|
|
|
uint32_t sid, const uint8_t* data, size_t len) { |
|
|
|
uint32_t sid, const uint8_t* data, size_t len, int force) { |
|
|
|
struct ll_entry* e = queue_entry_new(0); |
|
|
|
struct ll_entry* e = queue_entry_new(0); |
|
|
|
if (!e) { DEBUG_ERROR(DEBUG_CATEGORY_SOCKET, "tcp_proxy_server: queue_entry_new failed subcmd=%02x sid=%08x", subcmd, sid); return -1; } |
|
|
|
if (!e) { DEBUG_ERROR(DEBUG_CATEGORY_SOCKET, "tcp_proxy_server: queue_entry_new failed subcmd=%02x sid=%08x", subcmd, sid); return -1; } |
|
|
|
e->dgram = u_malloc(TCP_PROXY_HDR_SIZE + len); |
|
|
|
e->dgram = u_malloc(TCP_PROXY_HDR_SIZE + len); |
|
|
|
@ -58,7 +58,7 @@ static int send_msg(struct UTUN_INSTANCE* inst, uint64_t dst, uint8_t subcmd, |
|
|
|
memcpy(e->dgram + 2, &sid, 4); |
|
|
|
memcpy(e->dgram + 2, &sid, 4); |
|
|
|
if (len > 0) memcpy(e->dgram + TCP_PROXY_HDR_SIZE, data, len); |
|
|
|
if (len > 0) memcpy(e->dgram + TCP_PROXY_HDR_SIZE, data, len); |
|
|
|
e->len = TCP_PROXY_HDR_SIZE + len; |
|
|
|
e->len = TCP_PROXY_HDR_SIZE + len; |
|
|
|
return etcp_route_send(inst, dst, e); |
|
|
|
return etcp_route_send(inst, dst, e, force); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static void close_retry_cb(void* arg) { |
|
|
|
static void close_retry_cb(void* arg) { |
|
|
|
@ -69,7 +69,7 @@ static void close_retry_cb(void* arg) { |
|
|
|
struct UTUN_INSTANCE* inst = rc->ctx ? rc->ctx->inst : NULL; |
|
|
|
struct UTUN_INSTANCE* inst = rc->ctx ? rc->ctx->inst : NULL; |
|
|
|
if (!inst) return; |
|
|
|
if (!inst) return; |
|
|
|
uint8_t subcmd = (rc->tc && rc->tc->error) ? TCP_PROXY_SUBCMD_ERROR : TCP_PROXY_SUBCMD_CLOSE; |
|
|
|
uint8_t subcmd = (rc->tc && rc->tc->error) ? TCP_PROXY_SUBCMD_ERROR : TCP_PROXY_SUBCMD_CLOSE; |
|
|
|
if (send_msg(inst, rc->peer_node_id, subcmd, rc->stream_id, NULL, 0) < 0) { |
|
|
|
if (send_msg(inst, rc->peer_node_id, subcmd, rc->stream_id, NULL, 0, 1) < 0) { |
|
|
|
rc->close_backoff = rc->close_backoff < 5000 ? rc->close_backoff * 2 : 5000; |
|
|
|
rc->close_backoff = rc->close_backoff < 5000 ? rc->close_backoff * 2 : 5000; |
|
|
|
rc->close_timer = uasync_set_timeout(rc->ua, rc->close_backoff, rc, close_retry_cb, "tps_close_retry"); |
|
|
|
rc->close_timer = uasync_set_timeout(rc->ua, rc->close_backoff, rc, close_retry_cb, "tps_close_retry"); |
|
|
|
return; |
|
|
|
return; |
|
|
|
@ -80,7 +80,7 @@ static void close_retry_cb(void* arg) { |
|
|
|
static void send_close(struct tcp_proxy_server_conn* rc) { |
|
|
|
static void send_close(struct tcp_proxy_server_conn* rc) { |
|
|
|
struct UTUN_INSTANCE* inst = rc->ctx ? rc->ctx->inst : NULL; |
|
|
|
struct UTUN_INSTANCE* inst = rc->ctx ? rc->ctx->inst : NULL; |
|
|
|
if (!inst) return; |
|
|
|
if (!inst) return; |
|
|
|
if (send_msg(inst, rc->peer_node_id, TCP_PROXY_SUBCMD_CLOSE, rc->stream_id, NULL, 0) < 0) { |
|
|
|
if (send_msg(inst, rc->peer_node_id, TCP_PROXY_SUBCMD_CLOSE, rc->stream_id, NULL, 0, 1) < 0) { |
|
|
|
rc->close_pending = 1; |
|
|
|
rc->close_pending = 1; |
|
|
|
rc->close_backoff = 50; |
|
|
|
rc->close_backoff = 50; |
|
|
|
if (!rc->close_timer) |
|
|
|
if (!rc->close_timer) |
|
|
|
@ -91,7 +91,7 @@ static void send_close(struct tcp_proxy_server_conn* rc) { |
|
|
|
static void send_error(struct tcp_proxy_server_conn* rc) { |
|
|
|
static void send_error(struct tcp_proxy_server_conn* rc) { |
|
|
|
struct UTUN_INSTANCE* inst = rc->ctx ? rc->ctx->inst : NULL; |
|
|
|
struct UTUN_INSTANCE* inst = rc->ctx ? rc->ctx->inst : NULL; |
|
|
|
if (!inst) return; |
|
|
|
if (!inst) return; |
|
|
|
if (send_msg(inst, rc->peer_node_id, TCP_PROXY_SUBCMD_ERROR, rc->stream_id, NULL, 0) < 0) { |
|
|
|
if (send_msg(inst, rc->peer_node_id, TCP_PROXY_SUBCMD_ERROR, rc->stream_id, NULL, 0, 1) < 0) { |
|
|
|
rc->close_pending = 1; |
|
|
|
rc->close_pending = 1; |
|
|
|
rc->close_backoff = 50; |
|
|
|
rc->close_backoff = 50; |
|
|
|
if (!rc->close_timer) |
|
|
|
if (!rc->close_timer) |
|
|
|
@ -161,7 +161,7 @@ static void read_queue_drain_cb(struct ll_queue* q, void* arg) { |
|
|
|
struct UTUN_INSTANCE* inst = rc->ctx ? rc->ctx->inst : NULL; |
|
|
|
struct UTUN_INSTANCE* inst = rc->ctx ? rc->ctx->inst : NULL; |
|
|
|
struct ll_entry* e = queue_data_get(q); |
|
|
|
struct ll_entry* e = queue_data_get(q); |
|
|
|
if (!e) { queue_resume_callback(q); return; } |
|
|
|
if (!e) { queue_resume_callback(q); return; } |
|
|
|
int ret = send_msg(inst, rc->peer_node_id, TCP_PROXY_SUBCMD_DATA, rc->stream_id, e->dgram, e->len); |
|
|
|
int ret = send_msg(inst, rc->peer_node_id, TCP_PROXY_SUBCMD_DATA, rc->stream_id, e->dgram, e->len, 0); |
|
|
|
DEBUG_INFO(DEBUG_CATEGORY_SOCKET, "SOCK:RECV fd=%d sid=%08x len=%u total=%d", |
|
|
|
DEBUG_INFO(DEBUG_CATEGORY_SOCKET, "SOCK:RECV fd=%d sid=%08x len=%u total=%d", |
|
|
|
(int)rc->tc->sock, rc->stream_id, e->len, rc->ctx ? rc->ctx->conn_count : 0); |
|
|
|
(int)rc->tc->sock, rc->stream_id, e->len, rc->ctx ? rc->ctx->conn_count : 0); |
|
|
|
memory_pool_free(rc->tc->data_pool, e->dgram); |
|
|
|
memory_pool_free(rc->tc->data_pool, e->dgram); |
|
|
|
@ -258,7 +258,7 @@ int tcp_proxy_server_handle_connect(struct UTUN_INSTANCE* inst, struct ll_entry* |
|
|
|
if (ret < 0 && errno != EINPROGRESS) { |
|
|
|
if (ret < 0 && errno != EINPROGRESS) { |
|
|
|
DEBUG_ERROR(DEBUG_CATEGORY_SOCKET, "TCP proxy server: connect() to %d.%d.%d.%d:%d failed: %s", |
|
|
|
DEBUG_ERROR(DEBUG_CATEGORY_SOCKET, "TCP proxy server: 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)); |
|
|
|
dest_ip[0], dest_ip[1], dest_ip[2], dest_ip[3], ntohs(dest_port), strerror(errno)); |
|
|
|
send_msg(inst, src_node_id, TCP_PROXY_SUBCMD_ERROR, stream_id, NULL, 0); |
|
|
|
send_msg(inst, src_node_id, TCP_PROXY_SUBCMD_ERROR, stream_id, NULL, 0, 1); |
|
|
|
ctx->conn_count--; tcp_conn_destroy(rc->tc); u_free(rc); |
|
|
|
ctx->conn_count--; tcp_conn_destroy(rc->tc); u_free(rc); |
|
|
|
queue_dgram_free(entry); queue_entry_free(entry); return -1; |
|
|
|
queue_dgram_free(entry); queue_entry_free(entry); return -1; |
|
|
|
} |
|
|
|
} |
|
|
|
|