uasync: replace raw socket_node* handle with packed index — fixes UAF after socket_array realloc tcp_io: defer u_free in tcp_conn_destroy via uasync_call_soon — prevents callback-chain UAF tcp_io: guard read_cb/write_cb with NULL queue checks after deferred destroy tcp_io: save read_queue to local before queue_data_put + NULL guard after route_connectivity: linked-list probe_ctx — cancel all parallel probes before nq free
66 lines
2.5 KiB
C
66 lines
2.5 KiB
C
/**
|
|
* @file etcp_api.c
|
|
* @brief ETCP API — send/bind/recv dispatch
|
|
*/
|
|
#include "etcp_api.h"
|
|
#include "etcp.h"
|
|
#include "pkt_normalizer.h"
|
|
#include "utun_instance.h"
|
|
#include "stcp_link.h"
|
|
#include "../lib/debug_config.h"
|
|
#include <string.h>
|
|
|
|
#define DEBUG_CATEGORY_ETCP_API DEBUG_CATEGORY_ETCP
|
|
|
|
void etcp_conn_set_ready_cbk(struct ETCP_CONN* e, etcp_cbk_fn fn, void* arg) {
|
|
if (e) { e->ready_cbk = fn; e->ready_arg = arg; }
|
|
}
|
|
void etcp_conn_set_up_cbk(struct ETCP_CONN* e, etcp_cbk_fn fn, void* arg) {
|
|
if (e) { e->up_cbk = fn; e->up_arg = arg; }
|
|
}
|
|
void etcp_conn_set_down_cbk(struct ETCP_CONN* e, etcp_cbk_fn fn, void* arg) {
|
|
if (e) { e->down_cbk = fn; e->down_arg = arg; }
|
|
}
|
|
void etcp_set_new_conn_cbk(struct UTUN_INSTANCE* inst, etcp_cbk_fn fn, void* arg) {
|
|
if (inst) { inst->etcp_new_conn_cbk = fn; inst->etcp_new_conn_arg = arg; }
|
|
}
|
|
|
|
int etcp_bind(struct UTUN_INSTANCE* inst, uint8_t id, etcp_recv_fn callback) {
|
|
if (!inst || !callback || (unsigned)id >= ETCP_MAX_BINDINGS) return -1;
|
|
inst->api_bindings.callbacks[id] = callback;
|
|
return 0;
|
|
}
|
|
|
|
int etcp_unbind(struct UTUN_INSTANCE* inst, uint8_t id) {
|
|
if (!inst || (unsigned)id >= ETCP_MAX_BINDINGS) return -1;
|
|
inst->api_bindings.callbacks[id] = NULL;
|
|
return 0;
|
|
}
|
|
|
|
int etcp_send(struct ETCP_CONN* conn, struct ll_entry* entry) {
|
|
if (!conn || !entry) return -1;
|
|
// TCP (stcp_link) — send directly
|
|
if (conn->transport_link)
|
|
return stcp_link_send((struct stcp_link *)conn->transport_link, entry->dgram, entry->len);
|
|
// UDP — через normalizer
|
|
if (!conn->normalizer || !conn->normalizer->input) return -1;
|
|
return queue_data_put(conn->normalizer->input, entry);
|
|
}
|
|
|
|
void etcp_int_recv(struct ll_queue* queue, void* arg) {
|
|
struct ETCP_CONN* conn = (struct ETCP_CONN*)arg;
|
|
if (!queue || !conn) return;
|
|
struct ll_entry* e = queue_data_get(queue);
|
|
if (!e) { queue_resume_callback(queue); return; }
|
|
if (!e->dgram || e->len == 0) { queue_dgram_free(e); queue_entry_free(e); queue_resume_callback(queue); return; }
|
|
uint8_t id = e->dgram[0];
|
|
struct UTUN_INSTANCE* inst = conn->instance;
|
|
if (!inst) { queue_dgram_free(e); queue_entry_free(e); queue_resume_callback(queue); return; }
|
|
if (inst->api_bindings.callbacks[id])
|
|
inst->api_bindings.callbacks[id](conn, e);
|
|
else if (inst->api_bindings.callbacks[0])
|
|
inst->api_bindings.callbacks[0](conn, e);
|
|
else { queue_dgram_free(e); queue_entry_free(e); }
|
|
queue_resume_callback(queue);
|
|
}
|