Commit 8ccbd8f0 authored by Olivier Crête's avatar Olivier Crête
Browse files

agent: Make the TurnServer structure refcounted

Instead of just blindling assuming that all of the pointers are valid
until the end.
parent d510f45d
...@@ -1820,7 +1820,7 @@ priv_add_new_candidate_discovery_turn (NiceAgent *agent, ...@@ -1820,7 +1820,7 @@ priv_add_new_candidate_discovery_turn (NiceAgent *agent,
component_attach_socket (component, cdisco->nicesock); component_attach_socket (component, cdisco->nicesock);
} }
cdisco->turn = turn; cdisco->turn = turn_server_ref (turn);
cdisco->server = turn->server; cdisco->server = turn->server;
cdisco->stream = stream; cdisco->stream = stream;
......
...@@ -80,6 +80,9 @@ nice_candidate_free (NiceCandidate *candidate) ...@@ -80,6 +80,9 @@ nice_candidate_free (NiceCandidate *candidate)
if (candidate->password) if (candidate->password)
g_free (candidate->password); g_free (candidate->password);
if (candidate->turn)
turn_server_unref (candidate->turn);
g_slice_free (NiceCandidate, candidate); g_slice_free (NiceCandidate, candidate);
} }
...@@ -180,6 +183,7 @@ nice_candidate_copy (const NiceCandidate *candidate) ...@@ -180,6 +183,7 @@ nice_candidate_copy (const NiceCandidate *candidate)
memcpy (copy, candidate, sizeof(NiceCandidate)); memcpy (copy, candidate, sizeof(NiceCandidate));
copy->turn = NULL;
copy->username = g_strdup (copy->username); copy->username = g_strdup (copy->username);
copy->password = g_strdup (copy->password); copy->password = g_strdup (copy->password);
......
...@@ -129,6 +129,8 @@ typedef struct _TurnServer TurnServer; ...@@ -129,6 +129,8 @@ typedef struct _TurnServer TurnServer;
*/ */
struct _TurnServer struct _TurnServer
{ {
gint ref_count;
NiceAddress server; /**< TURN server address */ NiceAddress server; /**< TURN server address */
gchar *username; /**< TURN username */ gchar *username; /**< TURN username */
gchar *password; /**< TURN password */ gchar *password; /**< TURN password */
......
...@@ -148,15 +148,8 @@ component_new (guint id, NiceAgent *agent, Stream *stream) ...@@ -148,15 +148,8 @@ component_new (guint id, NiceAgent *agent, Stream *stream)
void void
component_clean_turn_servers (Component *cmp) component_clean_turn_servers (Component *cmp)
{ {
GList *item;
for (item = cmp->turn_servers; item; item = g_list_next (item)) { g_list_free_full (cmp->turn_servers, (GDestroyNotify) turn_server_unref);
TurnServer *turn = item->data;
g_free (turn->username);
g_free (turn->password);
g_slice_free (TurnServer, turn);
}
g_list_free (cmp->turn_servers);
cmp->turn_servers = NULL; cmp->turn_servers = NULL;
} }
...@@ -1080,6 +1073,7 @@ turn_server_new (const gchar *server_ip, guint server_port, ...@@ -1080,6 +1073,7 @@ turn_server_new (const gchar *server_ip, guint server_port,
nice_address_init (&turn->server); nice_address_init (&turn->server);
turn->ref_count = 1;
if (nice_address_set_from_string (&turn->server, server_ip)) { if (nice_address_set_from_string (&turn->server, server_ip)) {
nice_address_set_port (&turn->server, server_port); nice_address_set_port (&turn->server, server_port);
} else { } else {
...@@ -1092,3 +1086,23 @@ turn_server_new (const gchar *server_ip, guint server_port, ...@@ -1092,3 +1086,23 @@ turn_server_new (const gchar *server_ip, guint server_port,
return turn; return turn;
} }
TurnServer *
turn_server_ref (TurnServer *turn)
{
turn->ref_count++;
return turn;
}
void
turn_server_unref (TurnServer *turn)
{
turn->ref_count--;
if (turn->ref_count == 0) {
g_free (turn->username);
g_free (turn->password);
g_slice_free (TurnServer, turn);
}
}
...@@ -265,6 +265,13 @@ TurnServer * ...@@ -265,6 +265,13 @@ TurnServer *
turn_server_new (const gchar *server_ip, guint server_port, turn_server_new (const gchar *server_ip, guint server_port,
const gchar *username, const gchar *password, NiceRelayType type); const gchar *username, const gchar *password, NiceRelayType type);
TurnServer *
turn_server_ref (TurnServer *turn);
void
turn_server_unref (TurnServer *turn);
G_END_DECLS G_END_DECLS
#endif /* _NICE_COMPONENT_H */ #endif /* _NICE_COMPONENT_H */
......
...@@ -74,6 +74,9 @@ static inline int priv_timer_expired (GTimeVal *timer, GTimeVal *now) ...@@ -74,6 +74,9 @@ static inline int priv_timer_expired (GTimeVal *timer, GTimeVal *now)
*/ */
static void discovery_free_item (CandidateDiscovery *cand) static void discovery_free_item (CandidateDiscovery *cand)
{ {
if (cand->turn)
turn_server_unref (cand->turn);
g_slice_free (CandidateDiscovery, cand); g_slice_free (CandidateDiscovery, cand);
} }
...@@ -584,7 +587,7 @@ discovery_add_relay_candidate ( ...@@ -584,7 +587,7 @@ discovery_add_relay_candidate (
candidate->stream_id = stream_id; candidate->stream_id = stream_id;
candidate->component_id = component_id; candidate->component_id = component_id;
candidate->addr = *address; candidate->addr = *address;
candidate->turn = turn; candidate->turn = turn_server_ref (turn);
/* step: link to the base candidate+socket */ /* step: link to the base candidate+socket */
relay_socket = nice_turn_socket_new (agent->main_context, address, relay_socket = nice_turn_socket_new (agent->main_context, address,
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment