Fix mesh topology handling with fallback to normal decryption
When receiving packets on client links, try INIT decryption first.
If it fails (e.g., for INIT_RESPONSE packets), fallback to normal
decryption using the existing link's crypto context.
This fixes mesh topology where both nodes send INIT simultaneously,
while maintaining compatibility with standard client-server mode where
client links receive INIT_RESPONSE packets.
Changes:
- If link==NULL OR link is client (is_server==0), try INIT decryption
- On any INIT decryption error, if we have an existing link, goto normal_decrypt
- Added normal_decrypt label before standard packet processing
- This allows handling both incoming INIT (create server link) and
incoming responses (use existing client link) correctly
if(link==NULL||link->is_server==0){// пробуем расшифровать, возможно это init
// printf("[ETCP DEBUG] No existing link found, trying to decrypt as INIT packet\n");
structsecure_channelsc;
if(recv_len<=SC_PUBKEY_SIZE){errorcode=1;DEBUG_ERROR(DEBUG_CATEGORY_ETCP,"etcp_connections_read_callback: packet too small for init, size=%zd",recv_len);gotoec_fr;}
if(recv_len<=SC_PUBKEY_SIZE){
DEBUG_ERROR(DEBUG_CATEGORY_ETCP,"etcp_connections_read_callback: packet too small for init, size=%zd",recv_len);
// If we have an existing link (client link), try normal decryption instead
if(link!=NULL){
DEBUG_INFO(DEBUG_CATEGORY_ETCP,"INIT packet too small, trying normal decryption with existing link");
gotonormal_decrypt;
}
errorcode=1;
gotoec_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): ");
if(ack_hdr->code!=ETCP_INIT_REQUEST&&ack_hdr->code!=ETCP_CHANNEL_INIT){errorcode=4;DEBUG_ERROR(DEBUG_CATEGORY_ETCP,"etcp_connections_read_callback: not an init packet, code=%02x",ack_hdr->code);gotoec_fr;}// не init
DEBUG_ERROR(DEBUG_CATEGORY_CRYPTO,"etcp_connections_read_callback: failed to decrypt packet from node %llu len=%d",(unsignedlonglong)link->etcp->instance->node_id,recv_len);