Skip to content

Commit

Permalink
Small remote connection via PSN changes
Browse files Browse the repository at this point in the history
Notable Changes:
- Correct STUN length check
- Move getting STUN servers to before starting session
  • Loading branch information
streetpea committed Oct 15, 2024
1 parent bf30ca3 commit 840f642
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
10 changes: 5 additions & 5 deletions lib/src/remote/holepunch.c
Original file line number Diff line number Diff line change
Expand Up @@ -944,6 +944,11 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_holepunch_session_start(
return CHIAKI_ERR_UNKNOWN;
}
chiaki_mutex_unlock(&session->state_mutex);
ChiakiErrorCode err_stun = get_stun_servers(session);
if(err_stun != CHIAKI_ERR_SUCCESS)
{
CHIAKI_LOGW(session->log, "Getting stun servers returned error %s", chiaki_error_string(err_stun));
}
ChiakiErrorCode err;
session->console_type = console_type;
if(console_type == CHIAKI_HOLEPUNCH_CONSOLE_TYPE_PS4)
Expand Down Expand Up @@ -3378,11 +3383,6 @@ static bool get_client_addr_remote_stun(Session *session, char *address, uint16_
// run STUN test if it hasn't been run yet
if(session->stun_allocation_increment == -1)
{
ChiakiErrorCode err = get_stun_servers(session);
if(err != CHIAKI_ERR_SUCCESS)
{
CHIAKI_LOGW(session->log, "Getting stun servers returned error %s", chiaki_error_string(err));
}
if (!stun_port_allocation_test(session->log, address, port, &session->stun_allocation_increment, &session->stun_random_allocation, session->stun_server_list, session->num_stun_servers, sock))
{
CHIAKI_LOGE(session->log, "get_client_addr_remote_stun: Failed to get external address");
Expand Down
6 changes: 3 additions & 3 deletions lib/src/remote/stun.h
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@ static bool stun_get_external_address_from_server(ChiakiLog *log, StunServer *se

// Verify length stored in binding_resp[2] is correct
size_t expected_size = ntohs(*(uint16_t*)(&binding_resp[2])) + STUN_HEADER_SIZE;
if (received != ntohs(*(uint16_t*)(&binding_resp[2])) + STUN_HEADER_SIZE) {
if (received != expected_size) {
CHIAKI_LOGE(log, "remote/stun.h: Received STUN response with invalid length: %zd received, %zu expected", received, expected_size);
return false;
}
Expand All @@ -566,12 +566,12 @@ static bool stun_get_external_address_from_server(ChiakiLog *log, StunServer *se
//uint16_t response_attrs_length = ntohs(*(uint16_t*)(&binding_resp[2]));
uint16_t response_pos = STUN_HEADER_SIZE;
// Check we can read 4 bytes of attribute data
while (response_pos < (received - 4))
while (response_pos < (received - 3))
{
uint16_t attr_type = ntohs(*(uint16_t*)(&binding_resp[response_pos]));
uint16_t attr_length = ntohs(*(uint16_t*)(&binding_resp[response_pos + 2]));
// check that the whole advertised message has been received
if(response_pos >= (received - (sizeof(attr_type) + sizeof(attr_length) + attr_length)))
if(response_pos > (received - (sizeof(attr_type) + sizeof(attr_length) + attr_length)))
{
CHIAKI_LOGE(log, "remote/stun.h: Received STUN response with invalid data");
return false;
Expand Down

0 comments on commit 840f642

Please sign in to comment.