Skip to content

Commit

Permalink
Merge pull request #250 from paullouisageneau/refactoring
Browse files Browse the repository at this point in the history
Refactoring
  • Loading branch information
paullouisageneau authored Jun 13, 2024
2 parents 7378538 + 3c39ebd commit 29909c1
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 15 deletions.
21 changes: 19 additions & 2 deletions src/agent.c
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@ int agent_set_remote_description(juice_agent_t *agent, const char *sdp) {
return 0;
}

JLOG_WARN("ICE restart is unsupported");
JLOG_WARN("ICE restart is not supported");
conn_unlock(agent);
return -1;
}
Expand Down Expand Up @@ -560,9 +560,26 @@ int agent_add_remote_candidate(juice_agent_t *agent, const char *sdp) {
return ret;
}

int agent_set_local_ice_attributes(juice_agent_t *agent, const char *ufrag, const char *pwd)
{
if (agent->conn_impl) {
JLOG_WARN("Unable to set ICE attributes, candidates gathering already started");
return JUICE_ERR_FAILED;
}

if (strlen(ufrag) < 4 || strlen(pwd) < 22 || !ice_is_valid_string(ufrag) || !ice_is_valid_string(pwd)) {
JLOG_WARN("Invalid ICE attributes");
return JUICE_ERR_INVALID;
}

snprintf(agent->local.ice_ufrag, sizeof(agent->local.ice_ufrag), "%s", ufrag);
snprintf(agent->local.ice_pwd, sizeof(agent->local.ice_pwd), "%s", pwd);
return JUICE_ERR_SUCCESS;
}

int agent_add_turn_server(juice_agent_t *agent, const juice_turn_server_t *turn_server) {
if (agent->conn_impl) {
JLOG_WARN("Candidates gathering already started");
JLOG_WARN("Unable to add TURN server, candidates gathering already started");
return -1;
}

Expand Down
1 change: 1 addition & 0 deletions src/agent.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ int agent_resolve_servers(juice_agent_t *agent);
int agent_get_local_description(juice_agent_t *agent, char *buffer, size_t size);
int agent_set_remote_description(juice_agent_t *agent, const char *sdp);
int agent_add_remote_candidate(juice_agent_t *agent, const char *sdp);
int agent_set_local_ice_attributes(juice_agent_t *agent, const char *ufrag, const char *pwd);
int agent_add_turn_server(juice_agent_t *agent, const juice_turn_server_t *turn_server);
int agent_set_remote_gathering_done(juice_agent_t *agent);
int agent_send(juice_agent_t *agent, const char *data, size_t size, int ds);
Expand Down
13 changes: 12 additions & 1 deletion src/ice.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ int ice_parse_sdp(const char *sdp, ice_description_t *description) {
if (*sdp == '\n') {
if (size) {
buffer[size++] = '\0';
if(parse_sdp_line(buffer, description) == ICE_PARSE_ERROR)
if (parse_sdp_line(buffer, description) == ICE_PARSE_ERROR)
return ICE_PARSE_ERROR;

size = 0;
Expand Down Expand Up @@ -415,3 +415,14 @@ uint32_t ice_compute_priority(ice_candidate_type_t type, int family, int compone
p += 256 - CLAMP(component, 1, 256);
return p;
}

bool ice_is_valid_string(const char *str) {
if (!str)
return false;

for (size_t i = 0; i < strlen(str); ++i)
if (!isalpha(str[i]) && !isdigit(str[i]) && str[i] != '+' && str[i] != '/')
return false;

return true;
}
2 changes: 2 additions & 0 deletions src/ice.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,6 @@ int ice_candidates_count(const ice_description_t *description, ice_candidate_typ

uint32_t ice_compute_priority(ice_candidate_type_t type, int family, int component, int index);

bool ice_is_valid_string(const char *str);

#endif
14 changes: 2 additions & 12 deletions src/juice.c
Original file line number Diff line number Diff line change
Expand Up @@ -150,20 +150,10 @@ JUICE_EXPORT int juice_get_selected_addresses(juice_agent_t *agent, char *local,

int juice_set_local_ice_attributes(juice_agent_t *agent, const char *ufrag, const char *pwd)
{
if (agent->conn_impl) {
JLOG_WARN("Candidates gathering already started");
return JUICE_ERR_FAILED;
}

if (!ufrag || !pwd || strlen(ufrag) < 4 || strlen(pwd) < 22) {
JLOG_WARN("Invalid ICE credentials");
if (!ufrag || !pwd)
return JUICE_ERR_INVALID;
}

snprintf(agent->local.ice_ufrag, sizeof(agent->local.ice_ufrag), "%s", ufrag);
snprintf(agent->local.ice_pwd, sizeof(agent->local.ice_pwd), "%s", pwd);

return JUICE_ERR_SUCCESS;
return agent_set_local_ice_attributes(agent, ufrag, pwd);
}

JUICE_EXPORT const char *juice_state_to_string(juice_state_t state) {
Expand Down
5 changes: 5 additions & 0 deletions test/ufrag.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ int test_ufrag() {
if (juice_set_local_ice_attributes(agent, "usr", "pw01234567890123456789") != JUICE_ERR_INVALID)
success = false;

if (juice_set_local_ice_attributes(agent, "ufrag:", "pw01234567890123456789") != JUICE_ERR_INVALID)
success = false;

if (juice_set_local_ice_attributes(agent, "ufrag", "pw0123456789012345678?") != JUICE_ERR_INVALID)
success = false;

// Set local ICE attributes
juice_set_local_ice_attributes(agent, "ufrag", "pw01234567890123456789");
Expand Down

0 comments on commit 29909c1

Please sign in to comment.