diff options
author | Rohan Garg <rohan@garg.io> | 2015-04-08 23:10:14 +0200 |
---|---|---|
committer | Philip Withnall <philip.withnall@collabora.co.uk> | 2015-04-14 10:55:01 +0100 |
commit | 4e4af4be3890f12e300fe71104564171091b7a17 (patch) | |
tree | 09487846ce874a5c9047627f70233755e1bb4f70 | |
parent | dd1afec4b09223600f494331476133d3077b1336 (diff) | |
download | libnice-4e4af4be3890f12e300fe71104564171091b7a17.tar.gz |
agent: Add API to set local credentials
Adds new API: nice_agent_set_local_credentials().
-rw-r--r-- | agent/agent.c | 31 | ||||
-rw-r--r-- | agent/agent.h | 27 | ||||
-rw-r--r-- | docs/reference/libnice/libnice-sections.txt | 1 | ||||
-rw-r--r-- | nice/libnice.sym | 1 | ||||
-rw-r--r-- | tests/Makefile.am | 5 | ||||
-rw-r--r-- | tests/test-credentials.c | 204 | ||||
-rw-r--r-- | win32/vs9/libnice.def | 1 |
7 files changed, 269 insertions, 1 deletions
diff --git a/agent/agent.c b/agent/agent.c index 78e2919..cde2386 100644 --- a/agent/agent.c +++ b/agent/agent.c @@ -3160,6 +3160,37 @@ nice_agent_set_remote_credentials ( return ret; } +NICEAPI_EXPORT gboolean +nice_agent_set_local_credentials ( + NiceAgent *agent, + guint stream_id, + const gchar *ufrag, + const gchar *pwd) +{ + Stream *stream; + gboolean ret = FALSE; + + g_return_val_if_fail (NICE_IS_AGENT (agent), FALSE); + g_return_val_if_fail (stream_id >= 1, FALSE); + + agent_lock (); + + stream = agent_find_stream (agent, stream_id); + + /* note: oddly enough, ufrag and pwd can be empty strings */ + if (stream && ufrag && pwd) { + g_strlcpy (stream->local_ufrag, ufrag, NICE_STREAM_MAX_UFRAG); + g_strlcpy (stream->local_password, pwd, NICE_STREAM_MAX_PWD); + + ret = TRUE; + goto done; + } + + done: + agent_unlock_and_emit (agent); + return ret; +} + NICEAPI_EXPORT gboolean nice_agent_get_local_credentials ( diff --git a/agent/agent.h b/agent/agent.h index ddaa652..f7a2313 100644 --- a/agent/agent.h +++ b/agent/agent.h @@ -592,6 +592,33 @@ nice_agent_set_remote_credentials ( const gchar *ufrag, const gchar *pwd); +/** + * nice_agent_set_local_credentials: + * @agent: The #NiceAgent Object + * @stream_id: The ID of the stream + * @ufrag: nul-terminated string containing an ICE username fragment + * (length must be between 22 and 256 chars) + * @pwd: nul-terminated string containing an ICE password + * (length must be between 4 and 256 chars) + * + * Sets the local credentials for stream @stream_id. + * + <note> + <para> + This is only effective before ICE negotiation has started. + </para> + </note> + * + * Since 0.1.11 + * Returns: %TRUE on success, %FALSE on error. + */ +gboolean +nice_agent_set_local_credentials ( + NiceAgent *agent, + guint stream_id, + const gchar *ufrag, + const gchar *pwd); + /** * nice_agent_get_local_credentials: diff --git a/docs/reference/libnice/libnice-sections.txt b/docs/reference/libnice/libnice-sections.txt index 5c27fac..82f0197 100644 --- a/docs/reference/libnice/libnice-sections.txt +++ b/docs/reference/libnice/libnice-sections.txt @@ -21,6 +21,7 @@ nice_agent_forget_relays nice_agent_gather_candidates nice_agent_set_remote_credentials nice_agent_get_local_credentials +nice_agent_set_local_credentials nice_agent_set_remote_candidates nice_agent_get_remote_candidates nice_agent_get_local_candidates diff --git a/nice/libnice.sym b/nice/libnice.sym index 9a7cc79..efcfdc3 100644 --- a/nice/libnice.sym +++ b/nice/libnice.sym @@ -51,6 +51,7 @@ nice_agent_set_port_range nice_agent_set_relay_info nice_agent_set_remote_candidates nice_agent_set_remote_credentials +nice_agent_set_local_credentials nice_agent_set_selected_pair nice_agent_set_selected_remote_candidate nice_agent_set_software diff --git a/tests/Makefile.am b/tests/Makefile.am index c7b1fd9..3644091 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -48,7 +48,8 @@ check_PROGRAMS = \ test-dribble \ test-new-dribble \ test-tcp \ - test-icetcp + test-icetcp \ + test-credentials dist_check_SCRIPTS = \ check-test-fullmode-with-stun.sh \ @@ -116,6 +117,8 @@ test_tcp_LDADD = $(COMMON_LDADD) test_icetcp_LDADD = $(COMMON_LDADD) +test_credentials_LDADD = $(COMMON_LDADD) + test_gstreamer_CFLAGS = $(AM_CFLAGS) $(GST_CHECK_CFLAGS) test_gstreamer_LDADD = -lnice -L$(top_builddir)/nice/.libs $(GLIB_LIBS) $(GUPNP_LIBS) $(GST_CHECK_LIBS) $(GST_LIBS) diff --git a/tests/test-credentials.c b/tests/test-credentials.c new file mode 100644 index 0000000..f678afa --- /dev/null +++ b/tests/test-credentials.c @@ -0,0 +1,204 @@ +/* + * This file is part of the Nice GLib ICE library. + * + * (C) 2015 Rohan Garg <rohan@garg.io> + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is the Nice GLib ICE library. + * + * The Initial Developers of the Original Code are Collabora Ltd and Nokia + * Corporation. All Rights Reserved. + * + * Contributors: + * Dafydd Harries, Collabora Ltd. + * Kai Vehmanen, Nokia + * + * Alternatively, the contents of this file may be used under the terms of the + * the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which + * case the provisions of LGPL are applicable instead of those above. If you + * wish to allow use of your version of this file only under the terms of the + * LGPL and not to allow others to use your version of this file under the + * MPL, indicate your decision by deleting the provisions above and replace + * them with the notice and other provisions required by the LGPL. If you do + * not delete the provisions above, a recipient may use your version of this + * file under either the MPL or the LGPL. + */ +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif + +#include "agent.h" +#include "agent-priv.h" +#include <string.h> +#include <stdio.h> + +#define LEFT_AGENT GINT_TO_POINTER(1) +#define RIGHT_AGENT GINT_TO_POINTER(2) +#define USE_UPNP 0 + +static GMainLoop *loop = NULL; + +static void cb_nice_recv (NiceAgent *agent, guint stream_id, guint component_id, guint len, gchar *buf, gpointer user_data) +{ + g_debug ("test-credentials:%s: %p", G_STRFUNC, user_data); +} + +static void set_credentials(NiceAgent *lagent, NiceAgent *ragent) +{ + gchar *ufrag = NULL, *password = NULL; + + g_debug ("test-credentials:%s", G_STRFUNC); + + nice_agent_get_local_credentials (lagent, 1, &ufrag, &password); + nice_agent_set_remote_credentials (ragent, 1, ufrag, password); + + g_free (ufrag); + g_free (password); + + nice_agent_get_local_credentials (ragent, 1, &ufrag, &password); + nice_agent_set_remote_credentials (lagent, 1, ufrag, password); + + g_free (ufrag); + g_free (password); +} + +static void swap_candidates(NiceAgent *local, guint local_id, NiceAgent *remote, guint remote_id) +{ + GSList *cands = NULL; + + g_debug ("test-credentials:%s", G_STRFUNC); + cands = nice_agent_get_local_candidates(local, local_id, + NICE_COMPONENT_TYPE_RTP); + g_assert(nice_agent_set_remote_candidates(remote, remote_id, + NICE_COMPONENT_TYPE_RTP, cands)); + + g_slist_free_full (cands, (GDestroyNotify) nice_candidate_free); +} + + +static void cb_candidate_gathering_done(NiceAgent *agent, guint stream_id, gpointer data) +{ + static gboolean L_CAND_DONE = false, R_CAND_DONE = false; + static NiceAgent *lagent = NULL, *ragent = NULL; + + g_debug ("test-credentials:%s: %p", G_STRFUNC, data); + if (GPOINTER_TO_UINT(data) == 1) { + g_debug ("lagent finished gathering candidates"); + L_CAND_DONE = true; + lagent = agent; + } else if (GPOINTER_TO_UINT(data) == 2) { + g_debug ("ragent finished gathering candidates"); + R_CAND_DONE = true; + ragent = agent; + } + + if (L_CAND_DONE && R_CAND_DONE) { + set_credentials (lagent, ragent); + swap_candidates (lagent, 1, ragent, 1); + swap_candidates (ragent, 1, lagent, 1); + } +} + +static void cb_component_state_changed (NiceAgent *agent, guint stream_id, guint component_id, guint state, gpointer data) +{ + if (state == NICE_COMPONENT_STATE_READY) { + g_main_loop_quit(loop); + } +} + +static void setup(NiceAgent *lagent, NiceAgent *ragent) +{ + NiceAddress addr; + + g_assert (nice_agent_add_stream (lagent, 1) == 1); + g_assert (nice_agent_add_stream (ragent, 1) == 1); + g_assert (NULL != lagent->streams); + g_assert (NULL != ragent->streams); + + nice_address_init (&addr); + g_assert (nice_address_set_from_string (&addr, "127.0.0.1")); + nice_agent_add_local_address (lagent, &addr); + nice_agent_add_local_address (ragent, &addr); + + nice_agent_attach_recv (lagent, 1, NICE_COMPONENT_TYPE_RTP, + g_main_context_default (), + cb_nice_recv, LEFT_AGENT); + nice_agent_attach_recv (ragent, 1, NICE_COMPONENT_TYPE_RTP, + g_main_context_default (), + cb_nice_recv, RIGHT_AGENT); + + g_signal_connect(G_OBJECT(lagent), "candidate-gathering-done", + G_CALLBACK(cb_candidate_gathering_done), LEFT_AGENT); + g_signal_connect(G_OBJECT(ragent), "candidate-gathering-done", + G_CALLBACK(cb_candidate_gathering_done), RIGHT_AGENT); + + g_signal_connect(G_OBJECT(lagent), "component-state-changed", + G_CALLBACK(cb_component_state_changed), LEFT_AGENT); + + g_object_set (G_OBJECT (lagent), "ice-tcp", FALSE, NULL); + g_object_set (G_OBJECT (ragent), "ice-tcp", FALSE, NULL); + + g_object_set (G_OBJECT (lagent), "controlling-mode", TRUE, NULL); + g_object_set (G_OBJECT (ragent), "controlling-mode", FALSE, NULL); + + g_object_set (G_OBJECT (lagent), "upnp", USE_UPNP, NULL); + g_object_set (G_OBJECT (ragent), "upnp", USE_UPNP, NULL); + + g_object_set_data (G_OBJECT (lagent), "other-agent", ragent); + g_object_set_data (G_OBJECT (ragent), "other-agent", lagent); +} + +static void teardown(NiceAgent *lagent, NiceAgent *ragent) +{ + nice_agent_remove_stream (lagent, 1); + nice_agent_remove_stream (ragent, 1); +} + +int main (void) +{ + NiceAgent *lagent = NULL, *ragent = NULL; + gchar *ufrag = NULL, *password = NULL; + +#ifdef G_OS_WIN32 + WSADATA w; + WSAStartup(0x0202, &w); +#endif + g_type_init (); + g_thread_init (NULL); + + loop = g_main_loop_new (NULL, FALSE); + + lagent = nice_agent_new (NULL, NICE_COMPATIBILITY_RFC5245); + ragent = nice_agent_new (NULL, NICE_COMPATIBILITY_RFC5245); + + setup (lagent, ragent); + + nice_agent_set_local_credentials (lagent, 1, "unicorns", "awesome"); + nice_agent_get_local_credentials (lagent, 1, &ufrag, &password); + g_assert (g_strcmp0("unicorns", ufrag) == 0); + g_assert (g_strcmp0("awesome", password) == 0); + + nice_agent_gather_candidates (lagent, 1); + nice_agent_gather_candidates (ragent, 1); + + g_main_loop_run (loop); + + teardown (lagent, ragent); + + g_object_unref (lagent); + g_object_unref (ragent); + +#ifdef G_OS_WIN32 + WSACleanup(); +#endif + return 0; +} diff --git a/win32/vs9/libnice.def b/win32/vs9/libnice.def index a9cd217..7065330 100644 --- a/win32/vs9/libnice.def +++ b/win32/vs9/libnice.def @@ -47,6 +47,7 @@ nice_agent_set_port_range nice_agent_set_relay_info nice_agent_set_remote_candidates nice_agent_set_remote_credentials +nice_agent_set_local_credentials nice_agent_set_selected_pair nice_agent_set_selected_remote_candidate nice_agent_set_software |