diff --git a/src/agent.c b/src/agent.c index 09af91c7..fa52b647 100644 --- a/src/agent.c +++ b/src/agent.c @@ -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; } @@ -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; } diff --git a/src/agent.h b/src/agent.h index 0eb5b922..179e9983 100644 --- a/src/agent.h +++ b/src/agent.h @@ -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); diff --git a/src/ice.c b/src/ice.c index 393caeff..8961c1f1 100644 --- a/src/ice.c +++ b/src/ice.c @@ -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; @@ -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; +} diff --git a/src/ice.h b/src/ice.h index 51078bd4..86f1de3a 100644 --- a/src/ice.h +++ b/src/ice.h @@ -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 diff --git a/src/juice.c b/src/juice.c index 62647813..d7c79139 100644 --- a/src/juice.c +++ b/src/juice.c @@ -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) { diff --git a/test/ufrag.c b/test/ufrag.c index c9d5de81..36ad9f72 100644 --- a/test/ufrag.c +++ b/test/ufrag.c @@ -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");