cpw.malu.xmpp.google_relay: 7e11dae4: Some unfinished code to take advantage o...

malu at pidgin.im malu at pidgin.im
Mon Aug 31 16:25:22 EDT 2009


-----------------------------------------------------------------
Revision: 7e11dae4b92b8ef950ff2a1ab3ed673b56d4f772
Ancestor: 45648a255619d2cd5f706725fa74543ded0409bc
Author: malu at pidgin.im
Date: 2009-08-31T20:23:51
Branch: im.pidgin.cpw.malu.xmpp.google_relay
URL: http://d.pidgin.im/viewmtn/revision/info/7e11dae4b92b8ef950ff2a1ab3ed673b56d4f772

Modified files:
        libpurple/media.c libpurple/protocols/jabber/google.c
        libpurple/protocols/jabber/jabber.c
        libpurple/protocols/jabber/jabber.h

ChangeLog: 

Some unfinished code to take advantage of Google's relays

-------------- next part --------------
============================================================
--- libpurple/media.c	2442fb00304d8b1f26cfe617755ab655ce16e5d6
+++ libpurple/media.c	f481c318149f00a036acbd39afa960a8e083f6c1
@@ -2413,7 +2413,7 @@ purple_media_candidate_pair_established_
 	FsParticipant *participant;
 	PurpleMediaStream *stream;
 	GList *iter;
-
+	
 	g_return_if_fail(FS_IS_STREAM(fsstream));
 	g_return_if_fail(session != NULL);
 
============================================================
--- libpurple/protocols/jabber/google.c	940c05f135865736e51fa0311197d6591352d1e6
+++ libpurple/protocols/jabber/google.c	6aee5960b789dc3e239d301d34cdd336f4293709
@@ -341,47 +341,27 @@ jabber_google_session_get_params(JabberS
 	return new_params;
 }
 
-
-gboolean
-jabber_google_session_initiate(JabberStream *js, const gchar *who, PurpleMediaSessionType type)
+static void
+jabber_google_relay_response_cb(PurpleUtilFetchUrlData *url_data, 
+	gpointer user_data, const gchar *url_text, gsize len, 
+	const gchar *error_message)
 {
-	GoogleSession *session;
-	JabberBuddy *jb;
-	JabberBuddyResource *jbr;
-	gchar *jid;
+	GoogleSession *session = (GoogleSession *) user_data;
 	GParameter *params;
 	guint num_params;
+	JabberStream *js = session->js;
 
-	/* construct JID to send to */
-	jb = jabber_buddy_find(js, who, FALSE);
-	if (!jb) {
-		purple_debug_error("jingle-rtp",
-				"Could not find Jabber buddy\n");
-		return FALSE;
-	}
-	jbr = jabber_buddy_find_resource(jb, NULL);
-	if (!jbr) {
-		purple_debug_error("jingle-rtp",
-				"Could not find buddy's resource\n");
-	}
+	js->google_relay_request = NULL;
 
-	if ((strchr(who, '/') == NULL) && jbr && (jbr->name != NULL)) {
-		jid = g_strdup_printf("%s/%s", who, jbr->name);
+	purple_debug_info("jabber", "got response on HTTP request to relay server\n");
+
+	if (url_text && len > 0) {
+		purple_debug_info("jabber", "got Google relay request response:\n%s\n",
+			url_text);
 	} else {
-		jid = g_strdup(who);
+	
 	}
 
-	session = g_new0(GoogleSession, 1);
-	session->id.id = jabber_get_next_id(js);
-	session->id.initiator = g_strdup_printf("%s@%s/%s", js->user->node,
-			js->user->domain, js->user->resource);
-	session->state = SENT_INITIATE;
-	session->js = js;
-	session->remote_jid = jid;
-
-	if (type & PURPLE_MEDIA_VIDEO)
-		session->video = TRUE;
-
 	session->media = purple_media_manager_create_media(
 			purple_media_manager_get(),
 			purple_connection_get_account(js->gc),
@@ -414,9 +394,69 @@ jabber_google_session_initiate(JabberStr
 		return FALSE;
 	}
 
-	g_free(params);
+	g_free(params);	
+}
 
-	return (session->media != NULL) ? TRUE : FALSE;
+gboolean
+jabber_google_session_initiate(JabberStream *js, const gchar *who, PurpleMediaSessionType type)
+{
+	GoogleSession *session;
+	JabberBuddy *jb;
+	JabberBuddyResource *jbr;
+	gchar *jid;
+
+	/* construct JID to send to */
+	jb = jabber_buddy_find(js, who, FALSE);
+	if (!jb) {
+		purple_debug_error("jingle-rtp",
+				"Could not find Jabber buddy\n");
+		return FALSE;
+	}
+	jbr = jabber_buddy_find_resource(jb, NULL);
+	if (!jbr) {
+		purple_debug_error("jingle-rtp",
+				"Could not find buddy's resource\n");
+	}
+
+	if ((strchr(who, '/') == NULL) && jbr && (jbr->name != NULL)) {
+		jid = g_strdup_printf("%s/%s", who, jbr->name);
+	} else {
+		jid = g_strdup(who);
+	}
+
+	session = g_new0(GoogleSession, 1);
+	session->id.id = jabber_get_next_id(js);
+	session->id.initiator = g_strdup_printf("%s@%s/%s", js->user->node,
+			js->user->domain, js->user->resource);
+	session->state = SENT_INITIATE;
+	session->js = js;
+	session->remote_jid = jid;
+
+	if (type & PURPLE_MEDIA_VIDEO)
+		session->video = TRUE;
+
+	/* if we got a relay token and relay host in google:jingleinfo, issue an
+	 HTTP request to get that data */
+	if (js->google_relay_host && js->google_relay_token) {
+		const gchar *url = 
+			g_strdup_printf("http://%s/create_session", js->google_relay_host);
+		const gchar *request =
+			g_strdup_printf("GET /create_session HTTP 1.1\r\n"
+							"X-Talk-Google-Relay-Auth: %s\r\n"
+							"X-Google-Relay-Auth: %s\r\n", 
+				js->google_relay_token, js->google_relay_token);
+		js->google_relay_request =
+			purple_util_fetch_url_request(url, TRUE, NULL, TRUE, request, FALSE,
+				jabber_google_relay_response_cb, session);
+		g_free(url);
+		g_free(request);
+	} else {
+		jabber_google_relay_response_cb(NULL, session, NULL, 0, NULL);
+	}
+	
+	/* we don't actually know yet wether it succeeded... maybe this is very
+	 wrong... */
+	return TRUE;
 }
 
 static gboolean
@@ -1345,6 +1385,7 @@ jabber_google_jingle_info_common(JabberS
                                  JabberIqType type, xmlnode *query)
 {
 	const xmlnode *stun = xmlnode_get_child(query, "stun");
+	const xmlnode *relay = xmlnode_get_child(query, "relay");
 	gchar *my_bare_jid;
 
 	/*
@@ -1388,8 +1429,23 @@ jabber_google_jingle_info_common(JabberS
 			}
 		}
 	}
-	/* should perhaps handle relays later on, or maybe wait until
-	 Google supports a common standard... */
+
+	if (relay) {
+		const xmlnode *token = xmlnode_get_child(relay, "token");
+		const xmlnode *server = xmlnode_get_child(relay, "server");
+		
+		if (token) {
+			gchar *relay_token = xmlnode_get_data(token);
+
+			/* we let js own the string returned from xmlnode_get_data */
+			js->google_relay_token = relay_token;
+		}
+
+		if (server) {
+			js->google_relay_host = 
+				g_strdup(xmlnode_get_attrib(server, "host"));
+		}
+	}
 }
 
 static void
============================================================
--- libpurple/protocols/jabber/jabber.c	543a269241301747a30676bcaa76d8ecfdc4a8f3
+++ libpurple/protocols/jabber/jabber.c	100d27cd587fc6689bd67c828a9e8a2cf858d9a3
@@ -842,6 +842,9 @@ jabber_stream_new(PurpleAccount *account
 	js->stun_ip = NULL;
 	js->stun_port = 0;
 	js->stun_query = NULL;
+	js->google_relay_token = NULL;
+	js->google_relay_host = NULL;
+	js->google_relay_request = NULL;
 
 	/* if we are idle, set idle-ness on the stream (this could happen if we get
 		disconnected and the reconnects while being idle. I don't think it makes
@@ -1563,6 +1566,15 @@ void jabber_close(PurpleConnection *gc)
 		js->stun_query = NULL;
 	}
 
+	/* remove Google relay-related stuff */
+	g_free(js->google_relay_token);
+	g_free(js->google_relay_host);
+
+	if (js->google_relay_request != NULL) {
+		purple_util_fetch_url_cancel(js->google_relay_request);
+		js->google_relay_request = NULL;
+	}
+
 	g_free(js);
 
 	gc->proto_data = NULL;
@@ -3021,6 +3033,9 @@ jabber_initiate_media(PurpleAccount *acc
 		jbr = jabber_buddy_find_resource(jb, resource);
 		g_free(resource);
 
+		/*
+		return jabber_google_session_initiate(js, who, type);
+		*/
 		if (type & PURPLE_MEDIA_AUDIO &&
 				!jabber_resource_has_capability(jbr,
 				JINGLE_APP_RTP_SUPPORT_AUDIO) &&
============================================================
--- libpurple/protocols/jabber/jabber.h	2d8f26d7c8eafa62b4ca8ce877f356e82b29abfa
+++ libpurple/protocols/jabber/jabber.h	d63970faa17769504013dc1e2817cd5c7a2a15b3
@@ -272,7 +272,12 @@ struct _JabberStream
 	gchar *stun_ip;
 	int stun_port;
 	PurpleDnsQueryData *stun_query;
-	/* later add stuff to handle TURN relays... */
+
+	/* stuff for Google's relay handling */
+	gchar *google_relay_token;
+	gchar *google_relay_host;
+	PurpleUtilFetchUrlData *google_relay_request; /* the HTTP request to get */
+												/* relay info */
 };
 
 typedef gboolean (JabberFeatureEnabled)(JabberStream *js, const gchar *namespace);


More information about the Commits mailing list