Skip to content

Commit

Permalink
juice, refactor: update juice to use new stun binding callback signat…
Browse files Browse the repository at this point in the history
…ure.
  • Loading branch information
xicilion committed Jul 5, 2024
1 parent 8218be9 commit a5eaf56
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 39 deletions.
16 changes: 7 additions & 9 deletions juice/include/juice/juice.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,15 @@ 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;
const char *pwd;
typedef struct juice_mux_incoming {
const char *local_ufrag;
const char *remote_ufrag;

uint8_t family;
const char *address;
uint16_t port;
} juice_stun_binding_t;
} juice_mux_incoming_t;

typedef void (*juice_cb_stun_binding_t)(const juice_stun_binding_t *info, void *user_ptr);
typedef void (*juice_cb_mux_incoming_t)(const juice_mux_incoming_t *info, void *user_ptr);

typedef struct juice_turn_server {
const char *host;
Expand Down Expand Up @@ -129,9 +128,8 @@ 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, void *user_ptr);
JUICE_EXPORT int juice_unbind_stun();
JUICE_EXPORT int juice_mux_listen(const char *bind_address, int local_port, juice_cb_mux_incoming_t cb, void *user_ptr);
JUICE_EXPORT int juice_mux_stop_listen(const char *bind_address, int local_port);

// ICE server

Expand Down
21 changes: 11 additions & 10 deletions juice/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 && registry->cb_stun_binding == NULL) {
if (registry->agents_count == 0 && registry->cb_mux_incoming == NULL) {
JLOG_DEBUG("No connection left, destroying connections registry");
mutex_unlock(&registry->mutex);

Expand Down Expand Up @@ -248,7 +248,10 @@ 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_unbind_stun() {
int juice_mux_stop_listen(const char *bind_address, int local_port) {
(void)bind_address;
(void)local_port;

conn_mode_entry_t *entry = &mode_entries[JUICE_CONCURRENCY_MODE_MUX];

mutex_lock(&entry->mutex);
Expand All @@ -261,8 +264,8 @@ int juice_unbind_stun() {

mutex_lock(&registry->mutex);

registry->cb_stun_binding = NULL;
registry->stun_binding_user_ptr = NULL;
registry->cb_mux_incoming = NULL;
registry->mux_incoming_user_ptr = NULL;
conn_mux_interrupt_registry(registry);

release_registry(entry);
Expand All @@ -272,11 +275,8 @@ int juice_unbind_stun() {
return 0;
}

int juice_bind_stun(const char *bind_address, int local_port, juice_cb_stun_binding_t cb, void *user_ptr)
int juice_mux_listen(const char *bind_address, int local_port, juice_cb_mux_incoming_t cb, void *user_ptr)
{
if (!cb)
return juice_unbind_stun();

conn_mode_entry_t *entry = &mode_entries[JUICE_CONCURRENCY_MODE_MUX];

udp_socket_config_t config;
Expand All @@ -287,6 +287,7 @@ int juice_bind_stun(const char *bind_address, int local_port, juice_cb_stun_bind

if (entry->registry) {
mutex_unlock(&entry->mutex);
JLOG_DEBUG("juice_mux_listen needs to be called before establishing any mux connection.");
return -1;
}

Expand All @@ -296,8 +297,8 @@ int juice_bind_stun(const char *bind_address, int local_port, juice_cb_stun_bind
if (!registry)
return -2;

registry->cb_stun_binding = cb;
registry->stun_binding_user_ptr = user_ptr;
registry->cb_mux_incoming = cb;
registry->mux_incoming_user_ptr = user_ptr;
mutex_unlock(&registry->mutex);

return 0;
Expand Down
4 changes: 2 additions & 2 deletions juice/src/conn.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ typedef struct conn_registry {
juice_agent_t **agents;
int agents_size;
int agents_count;
juice_cb_stun_binding_t cb_stun_binding;
void *stun_binding_user_ptr;
juice_cb_mux_incoming_t cb_mux_incoming;
void *mux_incoming_user_ptr;
} conn_registry_t;

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

int count = registry->agents_count;
if (registry->cb_stun_binding)
if (registry->cb_mux_incoming)
++count;
mutex_unlock(&registry->mutex);
return count;
Expand Down Expand Up @@ -297,30 +297,22 @@ 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");

const struct sockaddr *sa = (const struct sockaddr *)&src->addr;

socklen_t salen = addr_get_len(sa);
if (salen == 0)
return NULL;

if (registry->cb_mux_incoming) {
JLOG_DEBUG("Found STUN request with unknown ICE ufrag");
char host[ADDR_MAX_NUMERICHOST_LEN];
if (getnameinfo(sa, salen, host, ADDR_MAX_NUMERICHOST_LEN, NULL, 0, NI_NUMERICHOST)) {
if (getnameinfo((const struct sockaddr *)&src->addr, src->len, host, ADDR_MAX_NUMERICHOST_LEN, NULL, 0, NI_NUMERICHOST)) {
JLOG_ERROR("getnameinfo failed, errno=%d", sockerrno);
return NULL;
}

juice_stun_binding_t binding_info;
juice_mux_incoming_t incoming_info;

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

registry->cb_stun_binding(&binding_info, registry->stun_binding_user_ptr);
registry->cb_mux_incoming(&incoming_info, registry->mux_incoming_user_ptr);

return NULL;
}
Expand Down

0 comments on commit a5eaf56

Please sign in to comment.