Skip to content

Commit

Permalink
Merge pull request #243 from xicilion/master
Browse files Browse the repository at this point in the history
Add support for setting ICE ufrag and pwd
  • Loading branch information
paullouisageneau authored May 14, 2024
2 parents 9d35168 + 237697f commit 3a121cb
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 0 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ set(TESTS_SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/test/server.c
${CMAKE_CURRENT_SOURCE_DIR}/test/conflict.c
${CMAKE_CURRENT_SOURCE_DIR}/test/bind.c
${CMAKE_CURRENT_SOURCE_DIR}/test/ufrag.c
)
source_group("Test Files" FILES "${TESTS_SOURCES}")

Expand Down
1 change: 1 addition & 0 deletions include/juice/juice.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ JUICE_EXPORT int juice_get_selected_candidates(juice_agent_t *agent, char *local
char *remote, size_t remote_size);
JUICE_EXPORT int juice_get_selected_addresses(juice_agent_t *agent, char *local, size_t local_size,
char *remote, size_t remote_size);
JUICE_EXPORT int juice_set_local_ice_attributes(juice_agent_t *agent, const char *ufrag, const char *pwd);
JUICE_EXPORT const char *juice_state_to_string(juice_state_t state);

// ICE server
Expand Down
19 changes: 19 additions & 0 deletions src/juice.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#endif

#include <stdio.h>
#include <string.h>

JUICE_EXPORT juice_agent_t *juice_create(const juice_config_t *config) {
if (!config)
Expand Down Expand Up @@ -147,6 +148,24 @@ JUICE_EXPORT int juice_get_selected_addresses(juice_agent_t *agent, char *local,
return JUICE_ERR_SUCCESS;
}

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");
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;
}

JUICE_EXPORT const char *juice_state_to_string(juice_state_t state) {
switch (state) {
case JUICE_STATE_DISCONNECTED:
Expand Down
7 changes: 7 additions & 0 deletions test/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ int test_gathering(void);
int test_turn(void);
int test_conflict(void);
int test_bind(void);
int test_ufrag(void);

#ifndef NO_SERVER
int test_server(void);
Expand Down Expand Up @@ -97,6 +98,12 @@ int main(int argc, char **argv) {
return -1;
}

printf("\nRunning ufrag test...\n");
if (test_ufrag()) {
fprintf(stderr, "Ufrag test failed\n");
return -1;
}

#ifndef NO_SERVER
printf("\nRunning server test...\n");
if (test_server()) {
Expand Down
69 changes: 69 additions & 0 deletions test/ufrag.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/**
* Copyright (c) 2024 Paul-Louis Ageneau
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/

#include "juice/juice.h"

#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>

int test_ufrag() {
juice_agent_t *agent;
bool success = true;
int ret;

juice_set_log_level(JUICE_LOG_LEVEL_DEBUG);

// Create agent
juice_config_t config;
memset(&config, 0, sizeof(config));

agent = juice_create(&config);

if (juice_set_local_ice_attributes(agent, NULL, NULL) != JUICE_ERR_INVALID)
success = false;

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

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

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

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


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

// Generate local description
char sdp[JUICE_MAX_SDP_STRING_LEN];
juice_get_local_description(agent, sdp, JUICE_MAX_SDP_STRING_LEN);
printf("Local description:\n%s\n", sdp);

if (strstr(sdp, "a=ice-ufrag:ufrag\r\n") == NULL)
success = false;

if (strstr(sdp, "a=ice-pwd:pw01234567890123456789\r\n") == NULL)
success = false;

// Destroy
juice_destroy(agent);

if (success) {
printf("Success\n");
return 0;
} else {
printf("Failure\n");
return -1;
}
}

0 comments on commit 3a121cb

Please sign in to comment.