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 juice_bind_stun to reflect stun requests from unbound client. #248

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
12 changes: 12 additions & 0 deletions include/juice/juice.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,17 @@ typedef void (*juice_cb_gathering_done_t)(juice_agent_t *agent, void *user_ptr);
typedef void (*juice_cb_recv_t)(juice_agent_t *agent, const char *data, size_t size,
void *user_ptr);

typedef struct juice_stun_binding {
const char *ufrag;
xicilion marked this conversation as resolved.
Show resolved Hide resolved
const char *pwd;
xicilion marked this conversation as resolved.
Show resolved Hide resolved

uint8_t family;
xicilion marked this conversation as resolved.
Show resolved Hide resolved
const char *address;
uint16_t port;
} juice_stun_binding_t;
paullouisageneau marked this conversation as resolved.
Show resolved Hide resolved

typedef void (*juice_cb_stun_binding_t)(const juice_stun_binding_t *info);
xicilion marked this conversation as resolved.
Show resolved Hide resolved

typedef struct juice_turn_server {
const char *host;
const char *username;
Expand Down Expand Up @@ -118,6 +129,7 @@ JUICE_EXPORT int juice_get_selected_addresses(juice_agent_t *agent, char *local,
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);
JUICE_EXPORT int juice_bind_stun(const char *bind_address, int local_port, juice_cb_stun_binding_t cb);

// ICE server

Expand Down
29 changes: 28 additions & 1 deletion src/conn.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ static void release_registry(conn_mode_entry_t *entry) {

// registry must be locked

if (registry->agents_count == 0) {
if (registry->agents_count == 0 && registry->cb_stun_binding == NULL) {
JLOG_DEBUG("No connection left, destroying connections registry");
mutex_unlock(&registry->mutex);

Expand Down Expand Up @@ -247,3 +247,30 @@ int conn_get_addrs(juice_agent_t *agent, addr_record_t *records, size_t size) {

return get_mode_entry(agent)->get_addrs_func(agent, records, size);
}

int juice_bind_stun(const char *bind_address, int local_port, juice_cb_stun_binding_t cb)
Copy link

@achingbrain achingbrain Jun 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to unbind from the address to allow closing all open sockets?

Would you just call this again with NULL as the cb argument?

{
conn_mode_entry_t *entry = &mode_entries[JUICE_CONCURRENCY_MODE_MUX];

udp_socket_config_t config;
config.bind_address = bind_address;
config.port_begin = config.port_end = local_port;

mutex_lock(&entry->mutex);

if (entry->registry) {
mutex_unlock(&entry->mutex);
return -1;
xicilion marked this conversation as resolved.
Show resolved Hide resolved
}

conn_registry_t *registry = acquire_registry(entry, &config);
mutex_unlock(&entry->mutex);

if (!registry)
return -2;

registry->cb_stun_binding = cb;
mutex_unlock(&registry->mutex);

return 0;
}
1 change: 1 addition & 0 deletions src/conn.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ typedef struct conn_registry {
juice_agent_t **agents;
int agents_size;
int agents_count;
juice_cb_stun_binding_t cb_stun_binding;
} conn_registry_t;

int conn_create(juice_agent_t *agent, udp_socket_config_t *config);
Expand Down
28 changes: 28 additions & 0 deletions src/conn_mux.c
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,8 @@ int conn_mux_prepare(conn_registry_t *registry, struct pollfd *pfd, timestamp_t
}

int count = registry->agents_count;
if (registry->cb_stun_binding)
++count;
mutex_unlock(&registry->mutex);
return count;
}
Expand Down Expand Up @@ -295,6 +297,32 @@ static juice_agent_t *lookup_agent(conn_registry_t *registry, char *buf, size_t
}
}

if (registry->cb_stun_binding) {
JLOG_DEBUG("Found STUN agent for unknown ICE ufrag");
paullouisageneau marked this conversation as resolved.
Show resolved Hide resolved
const struct sockaddr *sa = (const struct sockaddr *)&src->addr;

socklen_t salen = addr_get_len(sa);
paullouisageneau marked this conversation as resolved.
Show resolved Hide resolved
if (salen == 0)
return NULL;

char host[ADDR_MAX_NUMERICHOST_LEN];
if (getnameinfo(sa, salen, host, ADDR_MAX_NUMERICHOST_LEN, NULL, 0, NI_NUMERICHOST)) {
paullouisageneau marked this conversation as resolved.
Show resolved Hide resolved
JLOG_ERROR("getnameinfo failed, errno=%d", sockerrno);
return NULL;
}

juice_stun_binding_t binding_info;

binding_info.ufrag = username;
binding_info.pwd = separator + 1;
binding_info.family = (uint8_t)sa->sa_family;
binding_info.address = host;
binding_info.port = addr_get_port((struct sockaddr *)src);

registry->cb_stun_binding(&binding_info);

return NULL;
}
} else {
if (!STUN_IS_RESPONSE(msg.msg_class)) {
JLOG_INFO("Got unexpected STUN message from unknown source address");
Expand Down
Loading