Skip to content

Commit

Permalink
srtla_send: handshake: fix race between REG3 and NGP
Browse files Browse the repository at this point in the history
This could have, in some cases, attempted double group registration
  • Loading branch information
rationalsa committed Oct 2, 2023
1 parent 5c1e730 commit f138fb4
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions srtla_send.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#define PKT_LOG_SZ 256
#define CONN_TIMEOUT 4
#define REG2_TIMEOUT 4
#define REG3_TIMEOUT 4
#define GLOBAL_TIMEOUT 10
#define IDLE_TIME 1

Expand Down Expand Up @@ -77,7 +78,7 @@ int active_connections = 0;
int has_connected = 0;

conn_t *pending_reg2_conn = NULL;
time_t pending_reg2_timeout = 0;
time_t pending_reg_timeout = 0;

char srtla_id[SRTLA_ID_LEN];

Expand Down Expand Up @@ -339,11 +340,15 @@ void handle_srtla_data(conn_t *c) {
/* Handling NGPs separately because we don't want them to update last_rcvd
Otherwise they could be keeping failed connections marked active */
if (packet_type == SRTLA_TYPE_REG_NGP) {
if (active_connections == 0 && pending_reg2_conn == NULL) {
/* Only process NGPs if:
* we don't have any established connections
* and we don't already have a pending REG1->REG2 exhange in flight
* and we don't have any pending REG2->REG3 exchanges in flight
*/
if (active_connections == 0 && pending_reg2_conn == NULL && ts > pending_reg_timeout) {
if (send_reg1(c) == 0) {

pending_reg2_conn = c;
pending_reg2_timeout = ts + REG2_TIMEOUT;
pending_reg_timeout = ts + REG2_TIMEOUT;
}
}
return;
Expand All @@ -366,6 +371,7 @@ void handle_srtla_data(conn_t *c) {
}

pending_reg2_conn = NULL;
pending_reg_timeout = ts + REG3_TIMEOUT;
}
return;
}
Expand Down Expand Up @@ -414,6 +420,7 @@ void handle_srtla_data(conn_t *c) {

case SRTLA_TYPE_REG3:
has_connected = 1;
active_connections++;
info("%s (%p): connection established\n", print_addr(&c->src), c);
return;
} // switch
Expand Down Expand Up @@ -605,7 +612,7 @@ void connection_housekeeping() {

active_connections = 0;

if (pending_reg2_conn && time > pending_reg2_timeout) {
if (pending_reg2_conn && time > pending_reg_timeout) {
pending_reg2_conn = NULL;
}

Expand Down

0 comments on commit f138fb4

Please sign in to comment.