Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support custom ICE ufrag and pwd fields via config #249

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions include/juice/juice.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ typedef struct juice_config {

void *user_ptr;

const char *ice_ufrag;
const char *ice_pwd;

} juice_config_t;

JUICE_EXPORT juice_agent_t *juice_create(const juice_config_t *config);
Expand Down
11 changes: 10 additions & 1 deletion src/agent.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,15 @@ juice_agent_t *agent_create(const juice_config_t *config) {
agent->config.cb_gathering_done = config->cb_gathering_done;
agent->config.cb_recv = config->cb_recv;
agent->config.user_ptr = config->user_ptr;

if (config->ice_ufrag != NULL) {
agent->config.ice_ufrag = alloc_string_copy(config->ice_ufrag, &alloc_failed);
}

if (config->ice_pwd != NULL) {
agent->config.ice_pwd = alloc_string_copy(config->ice_pwd, &alloc_failed);
}

if (alloc_failed) {
JLOG_FATAL("Memory allocation for configuration copy failed");
goto error;
Expand Down Expand Up @@ -133,7 +142,7 @@ juice_agent_t *agent_create(const juice_config_t *config) {
agent->conn_index = -1;
agent->conn_impl = NULL;

ice_create_local_description(&agent->local);
ice_create_local_description(&agent->local, (char*)agent->config.ice_ufrag, (char*)agent->config.ice_pwd);

// RFC 8445: 16.1. Attributes
// The content of the [ICE-CONTROLLED/ICE-CONTROLLING] attribute is a 64-bit
Expand Down
17 changes: 14 additions & 3 deletions src/ice.c
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,21 @@ int ice_parse_candidate_sdp(const char *line, ice_candidate_t *candidate) {
return ICE_PARSE_ERROR;
}

int ice_create_local_description(ice_description_t *description) {
int ice_create_local_description(ice_description_t *description, char *ice_ufrag, char *ice_pwd) {
memset(description, 0, sizeof(*description));
juice_random_str64(description->ice_ufrag, 4 + 1);
juice_random_str64(description->ice_pwd, 22 + 1);

if (ice_ufrag != NULL) {
strcpy(description->ice_ufrag, ice_ufrag);
} else {
juice_random_str64(description->ice_ufrag, 4 + 1);
}

if (ice_pwd != NULL) {
strcpy(description->ice_pwd, ice_pwd);
} else {
juice_random_str64(description->ice_pwd, 22 + 1);
}

description->ice_lite = false;
description->candidates_count = 0;
description->finished = false;
Expand Down
2 changes: 1 addition & 1 deletion src/ice.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ typedef enum ice_resolve_mode {

int ice_parse_sdp(const char *sdp, ice_description_t *description);
int ice_parse_candidate_sdp(const char *line, ice_candidate_t *candidate);
int ice_create_local_description(ice_description_t *description);
int ice_create_local_description(ice_description_t *description, char *ice_ufrag, char *ice_pwd);
int ice_create_local_candidate(ice_candidate_type_t type, int component, int index,
const addr_record_t *record, ice_candidate_t *candidate);
int ice_resolve_candidate(ice_candidate_t *candidate, ice_resolve_mode_t mode);
Expand Down
7 changes: 7 additions & 0 deletions test/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ int test_turn(void);
int test_conflict(void);
int test_bind(void);
int test_ufrag(void);
int test_custom_ufrag(void);

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

printf("\nRunning custom ufrag test...\n");
if (test_custom_ufrag()) {
fprintf(stderr, "Custom ufrag test failed\n");
return -2;
}

#ifndef NO_SERVER
printf("\nRunning server test...\n");
if (test_server()) {
Expand Down
38 changes: 38 additions & 0 deletions test/ufrag.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,41 @@ int test_ufrag() {
return -1;
}
}

int test_custom_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));
config.ice_ufrag = "custom-ice-ufrag";
config.ice_pwd = "custom-ice-pwd";

agent = juice_create(&config);

// 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:custom-ice-ufrag\r\n") == NULL)
success = false;

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

// Destroy
juice_destroy(agent);

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