Delay closed connections free with root

This prevents creating a new connection on packets retransmissions
This commit is contained in:
emanuele-f 2021-07-06 18:27:26 +02:00
parent 4d5ad71429
commit fa4fdb1e1b
4 changed files with 21 additions and 16 deletions

View File

@ -240,6 +240,7 @@ static void destroy_connection(zdtun_t *tun, const zdtun_conn_t *conn_info) {
notify_connection(&proxy->conns_updates, tuple, data);
data->status = zdtun_conn_get_status(conn_info);
data->to_purge = true;
} else
conn_free_data(data);
}
@ -284,6 +285,9 @@ static void on_packet(zdtun_t *tun, const zdtun_pkt_t *pkt, uint8_t from_tun, co
}
account_packet(proxy, pkt, from_tun, tuple, data, pkt_ms);
if(data->status >= CONN_STATUS_CLOSED)
data->to_purge = true;
}
/* ******************************************************* */

View File

@ -27,6 +27,7 @@
// Keep in sync with zdtun.c
#define ICMP_TIMEOUT_SEC 5
#define UDP_TIMEOUT_SEC 30
#define TCP_CLOSED_TIMEOUT_SEC 15
#define TCP_TIMEOUT_SEC 60
/* ******************************************************* */
@ -333,12 +334,6 @@ static void handle_packet(vpnproxy_data_t *proxy, pcap_conn_t **connections, pca
account_packet(proxy, &pkt, from_tun, &conn->tuple, conn->data, pkt_ms);
update_connection_status(conn, &pkt, !from_tun);
if(conn->data->status >= CONN_STATUS_CLOSED) {
// Will free the data in sendConnectionsDump
HASH_DELETE(hh, *connections, conn);
free(conn);
}
}
/* ******************************************************* */
@ -352,7 +347,7 @@ static void purge_expired_connections(vpnproxy_data_t *proxy, pcap_conn_t **conn
switch(conn->tuple.ipproto) {
case IPPROTO_TCP:
timeout = TCP_TIMEOUT_SEC * 1000;
timeout = (conn->data->status >= CONN_STATUS_CLOSED) ? (TCP_CLOSED_TIMEOUT_SEC * 1000) : (TCP_TIMEOUT_SEC * 1000);
break;
case IPPROTO_UDP:
timeout = UDP_TIMEOUT_SEC * 1000;
@ -362,17 +357,22 @@ static void purge_expired_connections(vpnproxy_data_t *proxy, pcap_conn_t **conn
break;
}
if(purge_all
|| (conn->data->status >= CONN_STATUS_CLOSED)
|| (last_pkt_ms >= (conn->data->last_seen + timeout))) {
log_d("IDLE (type=%d)", conn->tuple.ipproto);
if(purge_all || (last_pkt_ms >= (conn->data->last_seen + timeout))) {
//log_d("IDLE (type=%d)", conn->tuple.ipproto);
// Will free the data in sendConnectionsDump
conn->data->update_type |= CONN_UPDATE_STATS;
notify_connection(&proxy->conns_updates, &conn->tuple, conn->data);
// The connection data will be purged
conn->data->to_purge = true;
if(conn->data->status < CONN_STATUS_CLOSED)
if(conn->data->status < CONN_STATUS_CLOSED) {
conn->data->status = CONN_STATUS_CLOSED;
conn->data->update_type |= CONN_UPDATE_STATS;
}
if(conn->data->update_type != 0) {
// Will free the data in sendConnectionsDump
notify_connection(&proxy->conns_updates, &conn->tuple, conn->data);
} else
conn_free_data(conn->data);
switch (conn->tuple.ipproto) {
case IPPROTO_TCP:

View File

@ -106,7 +106,7 @@ static void conns_clear(conn_array_t *arr, bool free_all) {
for(int i=0; i < arr->cur_items; i++) {
vpn_conn_t *slot = &arr->items[i];
if(slot->data && ((slot->data->status >= CONN_STATUS_CLOSED) || free_all))
if(slot->data && (slot->data->to_purge || free_all))
conn_free_data(slot->data);
}

View File

@ -79,6 +79,7 @@ typedef struct conn_data {
uint8_t pending_dns_queries;
};
bool pending_notification;
bool to_purge;
bool request_done;
char *request_data;
char *url;