|
|
|
@ -31,13 +31,13 @@ static void etcp_connections_read_callback(int fd, void* arg); |
|
|
|
|
|
|
|
|
|
|
|
// Minimal packet dump - only shows first 16 bytes to avoid slowing down
|
|
|
|
// 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) { |
|
|
|
static void dump_packet_bytes(const char* prefix, const uint8_t* data, size_t len) { |
|
|
|
printf("[ETCP DUMP] %s: len=%zu; dump: ", prefix, len); |
|
|
|
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "[ETCP DUMP] %s: len=%zu; dump: ", prefix, len); |
|
|
|
size_t show = len < 160 ? len : 160; |
|
|
|
size_t show = len < 160 ? len : 160; |
|
|
|
for (size_t i = 0; i < show; i++) { |
|
|
|
for (size_t i = 0; i < show; i++) { |
|
|
|
printf("%02x ", data[i]); |
|
|
|
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "%02x ", data[i]); |
|
|
|
} |
|
|
|
} |
|
|
|
if (len > 160) printf("..."); |
|
|
|
if (len > 160) DEBUG_INFO(DEBUG_CATEGORY_ETCP, "..."); |
|
|
|
printf("\n"); |
|
|
|
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "\n"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -94,7 +94,7 @@ static void etcp_link_send_init(struct ETCP_LINK* link) { |
|
|
|
struct sockaddr_in* sin = (struct sockaddr_in*)&link->remote_addr; |
|
|
|
struct sockaddr_in* sin = (struct sockaddr_in*)&link->remote_addr; |
|
|
|
char addr_str[INET_ADDRSTRLEN]; |
|
|
|
char addr_str[INET_ADDRSTRLEN]; |
|
|
|
inet_ntop(AF_INET, &sin->sin_addr, addr_str, INET_ADDRSTRLEN); |
|
|
|
inet_ntop(AF_INET, &sin->sin_addr, addr_str, INET_ADDRSTRLEN); |
|
|
|
printf("[ETCP] INIT sending to %s:%d, link=%p, conn_fd=%d\n", addr_str, ntohs(sin->sin_port), link, link->conn->fd); |
|
|
|
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "[ETCP] INIT sending to %s:%d, link=%p, conn_fd=%d", addr_str, ntohs(sin->sin_port), link, link->conn->fd); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
etcp_encrypt_send(dgram); |
|
|
|
etcp_encrypt_send(dgram); |
|
|
|
@ -286,19 +286,19 @@ struct ETCP_SOCKET* etcp_socket_add(struct UTUN_INSTANCE* instance, struct socka |
|
|
|
socklen_t addr_len = (ip->ss_family == AF_INET) ? sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6); |
|
|
|
socklen_t addr_len = (ip->ss_family == AF_INET) ? sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6); |
|
|
|
if (bind(e_sock->fd, (struct sockaddr*)ip, addr_len) < 0) { |
|
|
|
if (bind(e_sock->fd, (struct sockaddr*)ip, addr_len) < 0) { |
|
|
|
perror("bind"); |
|
|
|
perror("bind"); |
|
|
|
printf("[ETCP] Failed to bind socket to address family %d\n", ip->ss_family); |
|
|
|
DEBUG_ERROR(DEBUG_CATEGORY_ETCP, "[ETCP] Failed to bind socket to address family %d", ip->ss_family); |
|
|
|
if (ip->ss_family == AF_INET) { |
|
|
|
if (ip->ss_family == AF_INET) { |
|
|
|
struct sockaddr_in* sin = (struct sockaddr_in*)ip; |
|
|
|
struct sockaddr_in* sin = (struct sockaddr_in*)ip; |
|
|
|
char addr_str[INET_ADDRSTRLEN]; |
|
|
|
char addr_str[INET_ADDRSTRLEN]; |
|
|
|
inet_ntop(AF_INET, &sin->sin_addr, addr_str, INET_ADDRSTRLEN); |
|
|
|
inet_ntop(AF_INET, &sin->sin_addr, addr_str, INET_ADDRSTRLEN); |
|
|
|
printf("[ETCP] Failed to bind to %s:%d\n", addr_str, ntohs(sin->sin_port)); |
|
|
|
DEBUG_ERROR(DEBUG_CATEGORY_ETCP, "[ETCP] Failed to bind to %s:%d", addr_str, ntohs(sin->sin_port)); |
|
|
|
} |
|
|
|
} |
|
|
|
close(e_sock->fd); |
|
|
|
close(e_sock->fd); |
|
|
|
free(e_sock); |
|
|
|
free(e_sock); |
|
|
|
return NULL; |
|
|
|
return NULL; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
printf("[ETCP] Successfully bound socket to local address, family=%d\n", ip->ss_family); |
|
|
|
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "[ETCP] Successfully bound socket to local address, family=%d", ip->ss_family); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
e_sock->instance = instance; |
|
|
|
e_sock->instance = instance; |
|
|
|
@ -315,7 +315,7 @@ struct ETCP_SOCKET* etcp_socket_add(struct UTUN_INSTANCE* instance, struct socka |
|
|
|
NULL, NULL, e_sock); |
|
|
|
NULL, NULL, e_sock); |
|
|
|
DEBUG_INFO(DEBUG_CATEGORY_CONNECTION, "Registered ETCP socket with uasync (fd=%d)", e_sock->fd); |
|
|
|
DEBUG_INFO(DEBUG_CATEGORY_CONNECTION, "Registered ETCP socket with uasync (fd=%d)", e_sock->fd); |
|
|
|
|
|
|
|
|
|
|
|
printf("[ETCP] Socket %p (fd=%d) registered and active\n", e_sock, e_sock->fd); |
|
|
|
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "[ETCP] Socket %p (fd=%d) registered and active", e_sock, e_sock->fd); |
|
|
|
|
|
|
|
|
|
|
|
return e_sock; |
|
|
|
return e_sock; |
|
|
|
} |
|
|
|
} |
|
|
|
@ -323,11 +323,11 @@ struct ETCP_SOCKET* etcp_socket_add(struct UTUN_INSTANCE* instance, struct socka |
|
|
|
void etcp_socket_remove(struct ETCP_SOCKET* conn) { |
|
|
|
void etcp_socket_remove(struct ETCP_SOCKET* conn) { |
|
|
|
if (!conn) return; |
|
|
|
if (!conn) return; |
|
|
|
|
|
|
|
|
|
|
|
printf("[ETCP] Removing socket %p, fd=%d\n", conn, conn->fd); |
|
|
|
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "[ETCP] Removing socket %p, fd=%d", conn, conn->fd); |
|
|
|
|
|
|
|
|
|
|
|
if (conn->fd >= 0) { |
|
|
|
if (conn->fd >= 0) { |
|
|
|
close(conn->fd); |
|
|
|
close(conn->fd); |
|
|
|
printf("[ETCP] Closed fd=%d\n", conn->fd); |
|
|
|
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "[ETCP] Closed fd=%d", conn->fd); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
for (size_t i = 0; i < conn->num_channels; i++) { |
|
|
|
for (size_t i = 0; i < conn->num_channels; i++) { |
|
|
|
@ -431,14 +431,14 @@ int etcp_encrypt_send(struct ETCP_DGRAM* dgram) { |
|
|
|
struct sockaddr_in* sin = (struct sockaddr_in*)addr; |
|
|
|
struct sockaddr_in* sin = (struct sockaddr_in*)addr; |
|
|
|
char addr_str[INET_ADDRSTRLEN]; |
|
|
|
char addr_str[INET_ADDRSTRLEN]; |
|
|
|
inet_ntop(AF_INET, &sin->sin_addr, addr_str, INET_ADDRSTRLEN); |
|
|
|
inet_ntop(AF_INET, &sin->sin_addr, addr_str, INET_ADDRSTRLEN); |
|
|
|
printf("[ETCP] Sending packet to %s:%d, size=%zd\n", addr_str, ntohs(sin->sin_port), enc_buf_len + dgram->noencrypt_len); |
|
|
|
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "[ETCP] Sending packet to %s:%d, size=%zd", addr_str, ntohs(sin->sin_port), enc_buf_len + dgram->noencrypt_len); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
ssize_t sent = sendto(dgram->link->conn->fd, enc_buf, enc_buf_len + dgram->noencrypt_len, 0, (struct sockaddr*)addr, addr_len); |
|
|
|
ssize_t sent = sendto(dgram->link->conn->fd, enc_buf, enc_buf_len + dgram->noencrypt_len, 0, (struct sockaddr*)addr, addr_len); |
|
|
|
if (sent < 0) { dgram->link->send_errors++; errcode=3; goto es_err;} else dgram->link->total_encrypted += sent; |
|
|
|
if (sent < 0) { dgram->link->send_errors++; errcode=3; goto es_err;} else dgram->link->total_encrypted += sent; |
|
|
|
return (int)sent; |
|
|
|
return (int)sent; |
|
|
|
es_err: |
|
|
|
es_err: |
|
|
|
printf("[ETCP] encrypt_send error %d\n", errcode); |
|
|
|
DEBUG_ERROR(DEBUG_CATEGORY_ETCP, "[ETCP] encrypt_send error %d", errcode); |
|
|
|
return -1; |
|
|
|
return -1; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@ -469,7 +469,7 @@ static void etcp_connections_read_callback(int fd, void* arg) { |
|
|
|
ssize_t recv_len = recvfrom(fd, data, PACKET_DATA_SIZE, 0, (struct sockaddr*)&addr, &addr_len); |
|
|
|
ssize_t recv_len = recvfrom(fd, data, PACKET_DATA_SIZE, 0, (struct sockaddr*)&addr, &addr_len); |
|
|
|
|
|
|
|
|
|
|
|
if (recv_len <= 0) { |
|
|
|
if (recv_len <= 0) { |
|
|
|
printf("[ETCP] recvfrom failed or no data, recv_len=%zd, errno=%d\n", recv_len, errno); |
|
|
|
DEBUG_ERROR(DEBUG_CATEGORY_ETCP, "[ETCP] recvfrom failed or no data, recv_len=%zd, errno=%d", recv_len, errno); |
|
|
|
DEBUG_ERROR(DEBUG_CATEGORY_ETCP, "etcp_connections_read_callback: recvfrom failed, error=%zd, errno=%d", recv_len, errno); |
|
|
|
DEBUG_ERROR(DEBUG_CATEGORY_ETCP, "etcp_connections_read_callback: recvfrom failed, error=%zd, errno=%d", recv_len, errno); |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
@ -493,7 +493,7 @@ static void etcp_connections_read_callback(int fd, void* arg) { |
|
|
|
sc_init_ctx(&sc, &e_sock->instance->my_keys); |
|
|
|
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] 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] Last 64 bytes of packet (PUBKEY): ");
|
|
|
|
for (int i=0; i<SC_PUBKEY_SIZE; i++) printf("%02x ", data[recv_len-SC_PUBKEY_SIZE+i]); |
|
|
|
for (int i=0; i<SC_PUBKEY_SIZE; i++) DEBUG_INFO(DEBUG_CATEGORY_CRYPTO, "%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) {
|
|
|
|
if (sc_set_peer_public_key(&sc, &data[recv_len-SC_PUBKEY_SIZE], SC_PEER_PUBKEY_BIN)!=SC_OK) {
|
|
|
|
DEBUG_ERROR(DEBUG_CATEGORY_CRYPTO, "etcp_connections_read_callback: failed to set peer public key during init"); |
|
|
|
DEBUG_ERROR(DEBUG_CATEGORY_CRYPTO, "etcp_connections_read_callback: failed to set peer public key during init"); |
|
|
|
@ -553,7 +553,7 @@ static void etcp_connections_read_callback(int fd, void* arg) { |
|
|
|
pkt->data_len=sizeof(*ack_repl_hdr); |
|
|
|
pkt->data_len=sizeof(*ack_repl_hdr); |
|
|
|
pkt->noencrypt_len=0; |
|
|
|
pkt->noencrypt_len=0; |
|
|
|
pkt->link=link; |
|
|
|
pkt->link=link; |
|
|
|
printf("[ETCP DEBUG] Send INIT RESPONSE\n"); |
|
|
|
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "[ETCP DEBUG] Send INIT RESPONSE"); |
|
|
|
etcp_encrypt_send(pkt); |
|
|
|
etcp_encrypt_send(pkt); |
|
|
|
// printf("[ETCP DEBUG] Send INIT RESPONSE ok\n");
|
|
|
|
// printf("[ETCP DEBUG] Send INIT RESPONSE ok\n");
|
|
|
|
|
|
|
|
|
|
|
|
@ -594,7 +594,7 @@ static void etcp_connections_read_callback(int fd, void* arg) { |
|
|
|
|
|
|
|
|
|
|
|
// Mark link as initialized
|
|
|
|
// Mark link as initialized
|
|
|
|
link->initialized = 1; |
|
|
|
link->initialized = 1; |
|
|
|
printf("[ETCP] Link initialized successfully! Server node_id=%llu, mtu=%d\n", |
|
|
|
DEBUG_INFO(DEBUG_CATEGORY_CONNECTION, "[ETCP] Link initialized successfully! Server node_id=%llu, mtu=%d", |
|
|
|
(unsigned long long)server_node_id, link->mtu); |
|
|
|
(unsigned long long)server_node_id, link->mtu); |
|
|
|
|
|
|
|
|
|
|
|
// Cancel init timer if exists
|
|
|
|
// Cancel init timer if exists
|
|
|
|
@ -613,7 +613,7 @@ static void etcp_connections_read_callback(int fd, void* arg) { |
|
|
|
return; |
|
|
|
return; |
|
|
|
|
|
|
|
|
|
|
|
ec_fr: |
|
|
|
ec_fr: |
|
|
|
printf("etcp_connections_read_callback: error %d\n", errorcode); |
|
|
|
DEBUG_ERROR(DEBUG_CATEGORY_ETCP, "etcp_connections_read_callback: error %d", errorcode); |
|
|
|
e_sock->pkt_format_errors++; |
|
|
|
e_sock->pkt_format_errors++; |
|
|
|
e_sock->errorcode=errorcode; |
|
|
|
e_sock->errorcode=errorcode; |
|
|
|
memory_pool_free(e_sock->instance->pkt_pool, pkt); |
|
|
|
memory_pool_free(e_sock->instance->pkt_pool, pkt); |
|
|
|
@ -648,7 +648,7 @@ int init_connections(struct UTUN_INSTANCE* instance) { |
|
|
|
sprintf(addr_str + strlen(addr_str), ":%d", ntohs(sin6->sin6_port)); |
|
|
|
sprintf(addr_str + strlen(addr_str), ":%d", ntohs(sin6->sin6_port)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
printf("Initialized server %s on %s (links: %zu)\n",
|
|
|
|
DEBUG_INFO(DEBUG_CATEGORY_CONNECTION, "Initialized server %s on %s (links: %zu)",
|
|
|
|
server->name, addr_str, e_sock->num_channels); |
|
|
|
server->name, addr_str, e_sock->num_channels); |
|
|
|
server = server->next; |
|
|
|
server = server->next; |
|
|
|
} |
|
|
|
} |
|
|
|
@ -733,7 +733,7 @@ int init_connections(struct UTUN_INSTANCE* instance) { |
|
|
|
client_link = client_link->next; |
|
|
|
client_link = client_link->next; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
printf("Added client %s with %d links\n", client->name, client->keepalive); |
|
|
|
DEBUG_INFO(DEBUG_CATEGORY_CONNECTION, "Added client %s with %d links", client->name, client->keepalive); |
|
|
|
client = client->next; |
|
|
|
client = client->next; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@ -744,7 +744,7 @@ int init_connections(struct UTUN_INSTANCE* instance) { |
|
|
|
return -1; |
|
|
|
return -1; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
printf("Initialized %d connections\n", instance->connections_count); |
|
|
|
DEBUG_INFO(DEBUG_CATEGORY_CONNECTION, "Initialized %d connections", instance->connections_count); |
|
|
|
return 0; |
|
|
|
return 0; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|