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.
76 lines
2.7 KiB
76 lines
2.7 KiB
// udp_proxy.h — UDP датаграммный прокси: клиент ↔ exit через etcp_router |
|
#ifndef UDP_PROXY_H |
|
#define UDP_PROXY_H |
|
|
|
#ifdef __cplusplus |
|
extern "C" { |
|
#endif |
|
|
|
|
|
#include <stdint.h> |
|
#include "../lib/socket_compat.h" |
|
|
|
struct UTUN_INSTANCE; |
|
struct UASYNC; |
|
struct ll_entry; |
|
struct ETCP_CONN; |
|
|
|
// Подкоманды |
|
#define UDP_PROXY_SUBCMD_DATA 0x01 // client→exit: пробрось датаграмму | exit→client: ответ |
|
|
|
// Заголовок сообщения (не включая байт svc_id) |
|
// svc_id(1) + subcmd(1) + sender_node_id(8) + src_ip(4) + src_port(2) + dst_ip(4) + dst_port(2) + payload |
|
#define UDP_PROXY_HDR_SIZE 22 |
|
|
|
// Один UDP поток (сторона exit узла: сопоставляет client_node_id + кортеж адресов с OS сокетом) |
|
struct udp_flow { |
|
struct udp_flow* next; |
|
uint64_t client_node_id; |
|
uint32_t src_ip; |
|
uint16_t src_port; |
|
uint32_t dst_ip; |
|
uint16_t dst_port; |
|
socket_t sock; |
|
void* read_id; |
|
struct UASYNC* ua; |
|
uint64_t created_tb; |
|
uint64_t last_activity_tb; |
|
}; |
|
|
|
struct udp_proxy_ctx { |
|
int initialized; |
|
int is_exit; // 1=exit узел (создаёт сокеты), 0=клиент (шлёт через etcp) |
|
struct UTUN_INSTANCE* inst; |
|
struct UASYNC* ua; |
|
struct udp_flow* flows; |
|
int flow_count; |
|
uint64_t flow_timeout_tb; |
|
void* expire_timer; // периодический таймер очистки истёкших потоков |
|
}; |
|
|
|
extern struct udp_proxy_ctx* g_udp_ctx; |
|
|
|
int udp_proxy_init(struct UTUN_INSTANCE* inst, struct UASYNC* ua); |
|
void udp_proxy_destroy(struct UTUN_INSTANCE* inst); |
|
|
|
// Exit узел: принимает REQUEST, создаёт сокет, шлёт данные |
|
// Сторона клиента: принимает REPLY, доставляет на TUN |
|
void udp_proxy_recv_cb(struct ETCP_CONN* conn, struct ll_entry* entry); |
|
|
|
// Сторона клиента: принять IP/UDP пакет с TUN, отправить через etcp_route_send |
|
int udp_proxy_send_to_exit(struct UTUN_INSTANCE* inst, uint64_t exit_node_id, |
|
uint32_t src_ip, uint16_t src_port, |
|
uint32_t dst_ip, uint16_t dst_port, |
|
const uint8_t* payload, size_t payload_len); |
|
|
|
// Сторона клиента: доставить ответ на TUN (вызывается из recv_cb) |
|
int udp_proxy_deliver_reply(struct UTUN_INSTANCE* inst, |
|
uint32_t src_ip, uint16_t src_port, |
|
uint32_t dst_ip, uint16_t dst_port, |
|
const uint8_t* payload, size_t payload_len); |
|
|
|
|
|
#ifdef __cplusplus |
|
} |
|
#endif |
|
#endif // UDP_PROXY_H
|
|
|