Skip to content

Commit

Permalink
First take at refactoring Janus to use reference counters
Browse files Browse the repository at this point in the history
  • Loading branch information
lminiero committed Dec 18, 2015
1 parent bc17aee commit 6d1edc9
Show file tree
Hide file tree
Showing 25 changed files with 2,036 additions and 2,653 deletions.
15 changes: 5 additions & 10 deletions config.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,16 @@ static char *trim(char *s) {
static void janus_config_free_item(gpointer data) {
janus_config_item *i = (janus_config_item *)data;
if(i) {
if(i->name)
g_free((gpointer)i->name);
if(i->value)
g_free((gpointer)i->value);
g_free((gpointer)i->name);
g_free((gpointer)i->value);
g_free(i);
}
}

static void janus_config_free_category(gpointer data) {
janus_config_category *c = (janus_config_category *)data;
if(c) {
if(c->name)
g_free((gpointer)c->name);
g_free((gpointer)c->name);
if(c->items)
g_list_free_full(c->items, janus_config_free_item);
g_free(c);
Expand Down Expand Up @@ -312,8 +309,7 @@ janus_config_item *janus_config_add_item(janus_config *config, const char *categ
} else {
/* Update it */
char *item_value = g_strdup(value);
if(item->value)
g_free((gpointer)item->value);
g_free((gpointer)item->value);
item->value = item_value;
}
return item;
Expand Down Expand Up @@ -456,8 +452,7 @@ void janus_config_destroy(janus_config *config) {
g_list_free_full(config->categories, janus_config_free_category);
config->items = NULL;
}
if(config->name)
g_free((gpointer)config->name);
g_free((gpointer)config->name);
g_free((gpointer)config);
config = NULL;
}
72 changes: 43 additions & 29 deletions dtls.c
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,39 @@ gint janus_dtls_srtp_init(gchar *server_pem, gchar *server_key) {
return 0;
}

/* DTLS-SRTP de-initialization */
void janus_dtls_srtp_deinit(void) {
SSL_CTX_free(ssl_ctx);
g_free(janus_dtls_locks);
}

static void janus_dtls_srtp_free(const janus_refcount *dtls_ref) {
janus_dtls_srtp *dtls = janus_refcount_containerof(dtls_ref, janus_dtls_srtp, ref);
JANUS_LOG(LOG_WARN, "Freeing DTLS stack: %p\n", dtls);
/* This stack can be destroyed, free all the resources */
dtls->component = NULL;
if(dtls->ssl != NULL) {
SSL_free(dtls->ssl);
dtls->ssl = NULL;
}
/* BIOs are destroyed by SSL_free */
dtls->read_bio = NULL;
dtls->write_bio = NULL;
dtls->filter_bio = NULL;
if(dtls->srtp_valid) {
if(dtls->srtp_in) {
srtp_dealloc(dtls->srtp_in);
dtls->srtp_in = NULL;
}
if(dtls->srtp_out) {
srtp_dealloc(dtls->srtp_out);
dtls->srtp_out = NULL;
}
/* FIXME What about dtls->remote_policy and dtls->local_policy? */
}
g_free(dtls);
dtls = NULL;
}

janus_dtls_srtp *janus_dtls_srtp_create(void *ice_component, janus_dtls_role role) {
janus_ice_component *component = (janus_ice_component *)ice_component;
Expand All @@ -256,13 +289,14 @@ janus_dtls_srtp *janus_dtls_srtp_create(void *ice_component, janus_dtls_role rol
JANUS_LOG(LOG_FATAL, "Memory error!\n");
return NULL;
}
janus_refcount_init(&dtls->ref, janus_dtls_srtp_free);
/* Create SSL context, at last */
dtls->srtp_valid = 0;
dtls->ssl = SSL_new(janus_dtls_get_ssl_ctx());
if(!dtls->ssl) {
JANUS_LOG(LOG_ERR, "[%"SCNu64"] Error creating DTLS session! (%s)\n",
handle->handle_id, ERR_reason_error_string(ERR_get_error()));
janus_dtls_srtp_destroy(dtls);
janus_refcount_decrease(&dtls->ref);
return NULL;
}
SSL_set_ex_data(dtls->ssl, 0, dtls);
Expand All @@ -271,15 +305,15 @@ janus_dtls_srtp *janus_dtls_srtp_create(void *ice_component, janus_dtls_role rol
if(!dtls->read_bio) {
JANUS_LOG(LOG_ERR, "[%"SCNu64"] Error creating read BIO! (%s)\n",
handle->handle_id, ERR_reason_error_string(ERR_get_error()));
janus_dtls_srtp_destroy(dtls);
janus_refcount_decrease(&dtls->ref);
return NULL;
}
BIO_set_mem_eof_return(dtls->read_bio, -1);
dtls->write_bio = BIO_new(BIO_s_mem());
if(!dtls->write_bio) {
JANUS_LOG(LOG_ERR, "[%"SCNu64"] Error creating write BIO! (%s)\n",
handle->handle_id, ERR_reason_error_string(ERR_get_error()));
janus_dtls_srtp_destroy(dtls);
janus_refcount_decrease(&dtls->ref);
return NULL;
}
BIO_set_mem_eof_return(dtls->write_bio, -1);
Expand All @@ -288,7 +322,7 @@ janus_dtls_srtp *janus_dtls_srtp_create(void *ice_component, janus_dtls_role rol
if(!dtls->filter_bio) {
JANUS_LOG(LOG_ERR, "[%"SCNu64"] Error creating filter BIO! (%s)\n",
handle->handle_id, ERR_reason_error_string(ERR_get_error()));
janus_dtls_srtp_destroy(dtls);
janus_refcount_decrease(&dtls->ref);
return NULL;
}
/* Chain filter and write BIOs */
Expand All @@ -312,7 +346,7 @@ janus_dtls_srtp *janus_dtls_srtp_create(void *ice_component, janus_dtls_role rol
if(ecdh == NULL) {
JANUS_LOG(LOG_ERR, "[%"SCNu64"] Error creating ECDH group! (%s)\n",
handle->handle_id, ERR_reason_error_string(ERR_get_error()));
janus_dtls_srtp_destroy(dtls);
janus_refcount_decrease(&dtls->ref);
return NULL;
}
SSL_set_options(dtls->ssl, SSL_OP_SINGLE_ECDH_USE);
Expand Down Expand Up @@ -530,6 +564,7 @@ void janus_dtls_srtp_incoming_msg(janus_dtls_srtp *dtls, char *buf, uint16_t len
dtls->sctp = janus_sctp_association_create(dtls, handle->handle_id, 5000);
if(dtls->sctp != NULL) {
/* FIXME We need to start it in a thread, though, since it has blocking accept/connect stuff */
janus_refcount_increase(&dtls->sctp->ref);
GError *error = NULL;
g_thread_try_new("DTLS-SCTP", janus_dtls_sctp_setup_thread, dtls, &error);
if(error != NULL) {
Expand Down Expand Up @@ -571,32 +606,11 @@ void janus_dtls_srtp_destroy(janus_dtls_srtp *dtls) {
/* Destroy the SCTP association if this is a DataChannel */
if(dtls->sctp != NULL) {
janus_sctp_association_destroy(dtls->sctp);
janus_refcount_decrease(&dtls->sctp->ref);
dtls->sctp = NULL;
}
#endif
/* Destroy DTLS stack and free resources */
dtls->component = NULL;
if(dtls->ssl != NULL) {
SSL_free(dtls->ssl);
dtls->ssl = NULL;
}
/* BIOs are destroyed by SSL_free */
dtls->read_bio = NULL;
dtls->write_bio = NULL;
dtls->filter_bio = NULL;
if(dtls->srtp_valid) {
if(dtls->srtp_in) {
srtp_dealloc(dtls->srtp_in);
dtls->srtp_in = NULL;
}
if(dtls->srtp_out) {
srtp_dealloc(dtls->srtp_out);
dtls->srtp_out = NULL;
}
/* FIXME What about dtls->remote_policy and dtls->local_policy? */
}
g_free(dtls);
dtls = NULL;
janus_refcount_decrease(&dtls->ref);
}

/* DTLS alert callback */
Expand Down Expand Up @@ -634,7 +648,7 @@ void janus_dtls_callback(const SSL *ssl, int where, int ret) {
janus_flags_set(&handle->webrtc_flags, JANUS_ICE_HANDLE_WEBRTC_CLEANING);
if(!janus_flags_is_set(&handle->webrtc_flags, JANUS_ICE_HANDLE_WEBRTC_ALERT)) {
janus_flags_set(&handle->webrtc_flags, JANUS_ICE_HANDLE_WEBRTC_ALERT);
if(handle->iceloop)
if(handle->icethread == NULL && handle->iceloop != NULL)
g_main_loop_quit(handle->iceloop);
janus_plugin *plugin = (janus_plugin *)handle->app;
if(plugin != NULL) {
Expand Down
4 changes: 4 additions & 0 deletions dtls.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
* @param[in] server_key Path to the key to use
* @returns 0 in case of success, a negative integer on errors */
gint janus_dtls_srtp_init(gchar *server_pem, gchar *server_key);
/*! \brief DTLS stuff de-initialization */
void janus_dtls_srtp_deinit(void);
/*! \brief Method to return the shared SSL_CTX instance */
SSL_CTX *janus_dtls_get_ssl_ctx(void);
/*! \brief Method to return a string representation (SHA-256) of the certificate fingerprint */
Expand Down Expand Up @@ -84,6 +86,8 @@ typedef struct janus_dtls_srtp {
/*! \brief SCTP association, if DataChannels are involved */
janus_sctp_association *sctp;
#endif
/*! \brief Reference counter for this instance */
janus_refcount ref;
} janus_dtls_srtp;


Expand Down
Loading

0 comments on commit 6d1edc9

Please sign in to comment.