Browse Source

ETCP: добавлена проверка keepalive таймаута и статус линка

- Добавлен параметр keepalive_timeout в глобальный конфиг (default: 2000 ms)
- Добавлено поле keepalive_timeout в struct ETCP_LINK
- В keepalive_timer_cb() добавлена проверка таймаута по last_recv_local_time
- Выставление link_status: 1 (up) при получении пакетов, 0 (down) при таймауте
- Инициализация last_recv_local_time при создании линка
- Вывод keepalive_timeout в print_config()
nodeinfo-routing-update
Evgeny 2 months ago
parent
commit
bf33ae4e47
  1. 10
      src/config_parser.c
  2. 1
      src/config_parser.h
  3. 37
      src/etcp_connections.c
  4. 4
      src/etcp_connections.h

10
src/config_parser.c

@ -278,6 +278,10 @@ static int parse_global(const char *key, const char *value, struct global_config
global->mtu = atoi(value);
return 0;
}
if (strcmp(key, "keepalive_timeout") == 0) {
global->keepalive_timeout = atoi(value);
return 0;
}
if (strcmp(key, "control_ip") == 0) {
// Store for later processing with control_port
return 0; // We'll handle this when we see control_port
@ -437,7 +441,10 @@ static struct utun_config* parse_config_internal(FILE *fp, const char *filename)
DEBUG_ERROR(DEBUG_CATEGORY_MEMORY, "parse_config_internal: failed to allocate memory for config structure");
return NULL;
}
// Set default values
cfg->global.keepalive_timeout = 2000; // Default 2 seconds
section_type_t cur_section = SECTION_UNKNOWN;
struct CFG_SERVER *cur_server = NULL;
struct CFG_CLIENT *cur_client = NULL;
@ -614,6 +621,7 @@ void print_config(const struct utun_config *cfg) {
printf(" TUN interface: %s\n", g->tun_ifname[0] ? g->tun_ifname : "auto");
printf(" TUN IP: %s\n", ip_to_string(&g->tun_ip, ip_buffer, sizeof(ip_buffer)));
printf(" MTU: %d\n", g->mtu);
printf(" Keepalive timeout: %d ms\n", g->keepalive_timeout);
printf(" TUN test mode: %s\n", g->tun_test_mode ? "enabled" : "disabled");
printf(" Net debug: %d\n", g->net_debug);

1
src/config_parser.h

@ -77,6 +77,7 @@ struct global_config {
int enable_colors; // enable ANSI colors in logs
int tun_test_mode; // test mode: 1 = don't open real TUN, queues only
int keepalive_timeout; // keepalive timeout in ms (default: 2000)
};
struct utun_config {

37
src/etcp_connections.c

@ -157,6 +157,27 @@ static void keepalive_timer_cb(void* arg) {
goto restart_timer;
}
// Check keepalive timeout
uint64_t now = get_current_time_units();
uint64_t timeout_units = (uint64_t)link->keepalive_timeout * 10; // ms -> 0.1ms units
uint64_t elapsed = now - link->last_recv_local_time;
if (elapsed > timeout_units) {
if (link->link_status != 0) {
link->link_status = 0; // down
DEBUG_WARN(DEBUG_CATEGORY_CONNECTION, "[%s] Link %p (local_id=%d) status changed to DOWN - "
"no packets received for %llu ms (timeout=%u ms)",
link->etcp->log_name, link, link->local_link_id,
(unsigned long long)(elapsed / 10), link->keepalive_timeout);
}
} else {
if (link->link_status != 1) {
link->link_status = 1; // up
DEBUG_INFO(DEBUG_CATEGORY_CONNECTION, "[%s] Link %p (local_id=%d) status changed to UP",
link->etcp->log_name, link, link->local_link_id);
}
}
// Send keepalive only if no packets were sent since last tick
if (!link->pkt_sent_since_keepalive) {
etcp_link_send_keepalive(link);
@ -477,6 +498,14 @@ struct ETCP_LINK* etcp_link_new(struct ETCP_CONN* etcp, struct ETCP_SOCKET* conn
link->init_timer = NULL;
link->init_timeout = 0;
link->init_retry_count = 0;
link->link_status = 0; // down initially
// Initialize keepalive timeout from global config
if (etcp->instance && etcp->instance->config) {
link->keepalive_timeout = etcp->instance->config->global.keepalive_timeout;
} else {
link->keepalive_timeout = 2000; // Default 2 seconds
}
// Выделяем свободный local_link_id
int free_id = etcp_find_free_local_link_id(etcp);
@ -491,6 +520,7 @@ struct ETCP_LINK* etcp_link_new(struct ETCP_CONN* etcp, struct ETCP_SOCKET* conn
// link->last_activity = time(NULL);
link->ip_port_hash = sockaddr_hash(remote_addr);
link->last_recv_local_time = get_current_time_units(); // Initialize to prevent immediate timeout
insert_link(conn, link);
@ -803,6 +833,13 @@ process_decrypted:
link->last_recv_timestamp=pkt->timestamp;
link->last_recv_updated=1;
// Mark link as up when receiving packets
if (link->link_status != 1) {
link->link_status = 1;
DEBUG_INFO(DEBUG_CATEGORY_CONNECTION, "[%s] Link %p (local_id=%d) status changed to UP - packet received",
link->etcp->log_name, link, link->local_link_id);
}
size_t offset = 0;
uint8_t code = pkt->data[offset++];

4
src/etcp_connections.h

@ -60,6 +60,7 @@ struct ETCP_LINK {
uint8_t initialized; // Флаг инициализации (1=подтверждено или получен request)
uint8_t local_link_id; // id моего линка
uint8_t remote_link_id; // id этого линка на peer (устанавливается в момент initialized)
uint8_t link_status; // 1 - up, 0 - down
// Состояние установки соединения (только для клиентов)
void* init_timer; // Таймер для повторов INIT (NULL=не подключается)
@ -67,7 +68,7 @@ struct ETCP_LINK {
uint16_t init_retry_count; // Счетчик попыток
// uint64_t last_activity; // Время последней активности
uint64_t last_recv_local_time;
uint64_t last_recv_local_time; // x0.1 ms
uint16_t last_recv_timestamp;
uint8_t last_recv_updated; // =1 при обновлении timestamp, =0 при отправке (чтобы не дублировать отправки при отсутствии обновлений)
@ -93,6 +94,7 @@ struct ETCP_LINK {
// Keepalive state
void* keepalive_timer; // Таймер для отправки keepalive пакетов
uint32_t keepalive_timeout; // таймаут (ms)
uint8_t pkt_sent_since_keepalive; // Флаг: был ли отправлен пакет с последнего keepalive тика
};

Loading…
Cancel
Save