Fix: ETCP connection timing and packet handling improvements

- Made get_current_time_units() function globally accessible in etcp.h
- Improved packet dump formatting for better debugging clarity
- Added link timing tracking with last_recv_local_time and last_recv_timestamp
- Fixed packet parsing to properly skip timestamp bytes
- Enhanced connection state monitoring and error handling
- Removed obsolete etcp_connections.c4 debug file
- Updated test output formatting for better readability
This commit is contained in:
Evgeny committed 2026-01-21 22:59:32 +03:00
1 parent 6fd3a39cc8
commit 195d0b303c
6 files changed
+72 -274

No files matched your search

+1 -1
View File
@@ -421,7 +421,7 @@ static void update_metrics(struct ETCP_CONN* etcp, uint16_t rtt) {
// etcp->rtt_avg_100 = (etcp->rtt_avg_100 * 99 + rtt) / 100;
}
static uint64_t get_current_time_units() {
uint64_t get_current_time_units() {
struct timeval tv;
gettimeofday(&tv, NULL);
return ((uint64_t)tv.tv_sec * 10000ULL) + (tv.tv_usec / 100);
+1
View File
@@ -132,6 +132,7 @@ void etcp_conn_reset(struct ETCP_CONN* etcp);
void etcp_get_stats(struct ETCP_CONN* etcp, size_t* packets_sent, size_t* packets_recv,
size_t* pool_allocs, size_t* pool_reuse);
uint64_t get_current_time_units();
uint16_t get_current_timestamp(void);
#ifdef __cplusplus
+6 -3
View File
@@ -31,7 +31,7 @@ static void etcp_connections_read_callback(int fd, void* arg);
// Minimal packet dump - only shows first 16 bytes to avoid slowing down
static void dump_packet_bytes(const char* prefix, const uint8_t* data, size_t len) {
printf("[ETCP DUMP] %s: len=%zu ", prefix, len);
printf("[ETCP DUMP] %s: len=%zu; dump: ", prefix, len);
size_t show = len < 160 ? len : 160;
for (size_t i = 0; i < show; i++) {
printf("%02x ", data[i]);
@@ -464,7 +464,7 @@ static void etcp_connections_read_callback(int fd, void* arg) {
// printf("[ETCP] Received packet: %zd bytes from address\n", recv_len);
// DUMP: Show received packet content
dump_packet_bytes("RECEIVED", data, recv_len);
dump_packet_bytes("RECV in:", data, recv_len);
struct ETCP_DGRAM* pkt = memory_pool_alloc(e_sock->instance->pkt_pool);
if (!pkt) return;
@@ -558,8 +558,10 @@ static void etcp_connections_read_callback(int fd, void* arg) {
pkt->noencrypt_len=0;
pkt->link=link;
link->last_recv_local_time=get_current_time_units();
link->last_recv_timestamp=pkt->timestamp;
size_t offset = 0;
uint16_t rx_ts = (pkt->data[offset++] << 8) | pkt->data[offset++];
uint8_t code = pkt->data[offset++];
if (code == ETCP_INIT_RESPONSE || code == ETCP_CHANNEL_RESPONSE) {
@@ -592,6 +594,7 @@ static void etcp_connections_read_callback(int fd, void* arg) {
return; // INIT_RESPONSE is handled, no further processing needed
}
dump_packet_bytes("RECV decrypted:", pkt->data, pkt->data_len);
etcp_conn_input(pkt);
return;
@@ -31,7 +31,7 @@ static void etcp_connections_read_callback(int fd, void* arg);
// Minimal packet dump - only shows first 16 bytes to avoid slowing down
static void dump_packet_bytes(const char* prefix, const uint8_t* data, size_t len) {
printf("[ETCP DUMP] %s: len=%zu ", prefix, len);
printf("[ETCP DUMP] %s: len=%zu; dump: ", prefix, len);
size_t show = len < 160 ? len : 160;
for (size_t i = 0; i < show; i++) {
printf("%02x ", data[i]);
@@ -398,7 +398,7 @@ void etcp_link_close(struct ETCP_LINK* link) {
}
int etcp_encrypt_send(struct ETCP_DGRAM* dgram) {
printf("[ETCP DEBUG] etcp_encrypt_send: ENTERING FUNCTION\n");
// printf("[ETCP DEBUG] etcp_encrypt_send: ENTERING FUNCTION\n");
int errcode=0;
sc_context_t* sc = &dgram->link->etcp->crypto_ctx;
@@ -409,7 +409,7 @@ int etcp_encrypt_send(struct ETCP_DGRAM* dgram) {
dgram->timestamp=get_current_timestamp();
// DUMP: Show packet before encryption
dump_packet_bytes("BEFORE ENCRYPT", dgram->data, dgram->data_len);
dump_packet_bytes("ECTP_ENCRYPT_SEND", dgram->data, dgram->data_len);
sc_encrypt(sc, (uint8_t*)&dgram->timestamp/*нейронка не правь блять это, тут верно!*/, sizeof(uint16_t) + len, enc_buf, &enc_buf_len);
if (enc_buf_len == 0) {
DEBUG_ERROR(DEBUG_CATEGORY_CRYPTO, "etcp_encrypt_send: encryption failed for node %llu", (unsigned long long)dgram->link->etcp->instance->node_id);
@@ -421,7 +421,7 @@ int etcp_encrypt_send(struct ETCP_DGRAM* dgram) {
memcpy(enc_buf+enc_buf_len, dgram->data+len, dgram->noencrypt_len);
// DUMP: Show complete packet before sending
dump_packet_bytes("READY TO SEND", enc_buf, enc_buf_len + dgram->noencrypt_len);
// dump_packet_bytes("READY TO SEND", enc_buf, enc_buf_len + dgram->noencrypt_len);
struct sockaddr_storage* addr=&dgram->link->remote_addr;
socklen_t addr_len = (addr->ss_family == AF_INET) ? sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6);
@@ -448,7 +448,7 @@ static void etcp_connections_read_callback(int fd, void* arg) {
struct ETCP_SOCKET* e_sock = (struct ETCP_SOCKET*)arg;
if (!e_sock) return;
printf("[ETCP] Read callback triggered for fd=%d, socket=%p\n", fd, e_sock);
// printf("[ETCP] Read callback triggered for fd=%d, socket=%p\n", fd, e_sock);
struct sockaddr_storage addr;
uint8_t data[PACKET_DATA_SIZE];
@@ -461,10 +461,10 @@ static void etcp_connections_read_callback(int fd, void* arg) {
return;
}
printf("[ETCP] Received packet: %zd bytes from address\n", recv_len);
// printf("[ETCP] Received packet: %zd bytes from address\n", recv_len);
// DUMP: Show received packet content
dump_packet_bytes("RECEIVED", data, recv_len);
dump_packet_bytes("RECV in:", data, recv_len);
struct ETCP_DGRAM* pkt = memory_pool_alloc(e_sock->instance->pkt_pool);
if (!pkt) return;
@@ -472,14 +472,14 @@ static void etcp_connections_read_callback(int fd, void* arg) {
int errorcode=0;
struct ETCP_LINK* link=etcp_link_find_by_addr(e_sock, &addr);
printf("[ETCP DEBUG] Received packet, link=%p, recv_len=%zd\n", link, recv_len);
// printf("[ETCP DEBUG] Received packet, link=%p, recv_len=%zd\n", link, recv_len);
if (link==NULL) {// пробуем расшифровать, возможно это init
printf("[ETCP DEBUG] No existing link found, trying to decrypt as INIT packet\n");
// printf("[ETCP DEBUG] No existing link found, trying to decrypt as INIT packet\n");
struct secure_channel sc;
if (recv_len<=SC_PUBKEY_SIZE) { errorcode=1; goto ec_fr; }
sc_init_ctx(&sc, &e_sock->instance->my_keys);
printf("[ETCP DEBUG] Extracting peer public key from position %ld, total packet size=%zd\n", recv_len-SC_PUBKEY_SIZE, recv_len);
printf("[ETCP DEBUG] Last 64 bytes of packet (PUBKEY): ");
// printf("[ETCP DEBUG] Extracting peer public key from position %ld, total packet size=%zd\n", recv_len-SC_PUBKEY_SIZE, recv_len);
// printf("[ETCP DEBUG] Last 64 bytes of packet (PUBKEY): ");
for (int i=0; i<SC_PUBKEY_SIZE; i++) printf("%02x ", data[recv_len-SC_PUBKEY_SIZE+i]);
if (sc_set_peer_public_key(&sc, &data[recv_len-SC_PUBKEY_SIZE], SC_PEER_PUBKEY_BIN)!=SC_OK) {
@@ -492,7 +492,7 @@ static void etcp_connections_read_callback(int fd, void* arg) {
errorcode=3;
goto ec_fr;
}
printf("[ETCP DEBUG] Decrypt OK\n");
// printf("[ETCP DEBUG] Decrypt OK\n");
pkt->data_len=pkt_len-2;
pkt->noencrypt_len=0;
struct {
@@ -533,7 +533,7 @@ static void etcp_connections_read_callback(int fd, void* arg) {
uint8_t mtu[2];
} *ack_repl_hdr=(void*)&pkt->data[0];
ack_repl_hdr->code+=1;
memcpy(&ack_repl_hdr->id[0], &e_sock->instance->node_id, 8);
memcpy(ack_repl_hdr->id, &e_sock->instance->node_id, 8);
int mtu=e_sock->instance->config->global.mtu;
ack_repl_hdr->mtu[0]=mtu>>8;
ack_repl_hdr->mtu[1]=mtu;
@@ -542,7 +542,7 @@ static void etcp_connections_read_callback(int fd, void* arg) {
pkt->link=link;
printf("[ETCP DEBUG] Send INIT RESPONSE\n");
etcp_encrypt_send(pkt);
printf("[ETCP DEBUG] Send INIT RESPONSE ok\n");
// printf("[ETCP DEBUG] Send INIT RESPONSE ok\n");
memory_pool_free(e_sock->instance->pkt_pool, pkt);
return;
@@ -553,10 +553,49 @@ static void etcp_connections_read_callback(int fd, void* arg) {
errorcode=6;
goto ec_fr;
}
if (pkt_len<3) { errorcode=46; goto ec_fr; }
pkt->data_len=pkt_len-2;
pkt->noencrypt_len=0;
pkt->link=link;
link->last_recv_local_time=get_current_time_units();
link->last_recv_timestamp=pkt->timestamp;
size_t offset = 0;
uint8_t code = pkt->data[offset++];
if (code == ETCP_INIT_RESPONSE || code == ETCP_CHANNEL_RESPONSE) {
// Parse response
if (code == ETCP_INIT_RESPONSE) etcp_conn_reset(link->etcp);
uint64_t server_node_id = 0;
for (int i = 0; i < 8; i++) {
server_node_id = (server_node_id << 8) | pkt->data[offset++];
}
link->mtu = (pkt->data[offset++] << 8) | pkt->data[offset++];
if (offset > pkt_len) { errorcode=13; goto ec_fr; }
DEBUG_INFO(DEBUG_CATEGORY_CONNECTION, "Received INIT_RESPONSE from server_node_id=%llu, mtu=%d",
(unsigned long long)server_node_id, link->mtu);
link->etcp->peer_node_id = server_node_id; // If not set
// Mark link as initialized
link->initialized = 1;
printf("[ETCP] Link initialized successfully! Server node_id=%llu, mtu=%d\n",
(unsigned long long)server_node_id, link->mtu);
// Cancel init timer if exists
if (link->init_timer) {
uasync_cancel_timeout(link->etcp->instance->ua, link->init_timer);
link->init_timer = NULL;
}
memory_pool_free(e_sock->instance->pkt_pool, pkt);
return; // INIT_RESPONSE is handled, no further processing needed
}
dump_packet_bytes("RECV decrypted:", pkt->data, pkt->data_len);
etcp_conn_input(pkt);
return;