cpw.malu.xmpp.google_relay: 67a91055: Now it compiles again...

malu at pidgin.im malu at pidgin.im
Wed Sep 1 17:25:53 EDT 2010


----------------------------------------------------------------------
Revision: 67a9105587ae5686beed39fca65c15a5fc8bcad1
Parent:   110ac05f535ab1600f0ebc4216164aa86a338cd7
Author:   malu at pidgin.im
Date:     09/01/10 17:23:55
Branch:   im.pidgin.cpw.malu.xmpp.google_relay
URL: http://d.pidgin.im/viewmtn/revision/info/67a9105587ae5686beed39fca65c15a5fc8bcad1

Changelog: 

Now it compiles again...

Changes against parent 110ac05f535ab1600f0ebc4216164aa86a338cd7

  patched  libpurple/protocols/jabber/google/google_session.c
  patched  libpurple/protocols/jabber/google/google_session.h
  patched  libpurple/protocols/jabber/google/jingleinfo.c

-------------- next part --------------
============================================================
--- libpurple/protocols/jabber/google/google_session.c	bd83f1588b57ef98f4f1e5f0a73b03b03b494609
+++ libpurple/protocols/jabber/google/google_session.c	b604b0975fdcd4d14b1d54748f03016d1324de9b
@@ -301,10 +301,14 @@ static GParameter *
 }
 
 static GParameter *
-jabber_google_session_get_params(JabberStream *js, guint *num)
+jabber_google_session_get_params(JabberStream *js, const gchar *relay_ip,
+	guint16 relay_udp, guint16 relay_tcp, guint16 relay_ssltcp,
+    const gchar *relay_username, const gchar *relay_password, guint *num)
 {
 	guint num_params;
-	GParameter *params = jingle_get_params(js, &num_params);
+	GParameter *params =
+		jingle_get_params(js, relay_ip, relay_udp, relay_tcp, relay_ssltcp,
+	    	relay_username, relay_password, &num_params);
 	GParameter *new_params = g_new0(GParameter, num_params + 1);
 
 	memcpy(new_params, params, sizeof(GParameter) * num_params);
@@ -319,49 +323,83 @@ jabber_google_session_get_params(JabberS
 	return new_params;
 }
 
+static void
+jabber_google_relay_parse_response(const gchar *response, gchar **ip,
+	guint *udp, guint *tcp, guint *ssltcp, gchar **username, gchar **password)
+{
+	gchar **lines = g_strsplit(response, "\n", -1);
+	int i = 0;
 
-gboolean
-jabber_google_session_initiate(JabberStream *js, const gchar *who, PurpleMediaSessionType type)
+	for (; lines[i] ; i++) {
+		gchar *line = lines[i];
+		gchar **parts = g_strsplit(line, "=", 2);
+		
+		if (parts[0] && parts[1]) {
+			if (purple_strequal(parts[0], "relay.ip")) {
+				*ip = g_strdup(parts[1]);
+			} else if (purple_strequal(parts[0], "relay.udp_port")) {
+				*udp = atoi(parts[1]);
+			} else if (purple_strequal(parts[0], "relay.tcp_port")) {
+				*tcp = atoi(parts[1]);
+			} else if (purple_strequal(parts[0], "relay.ssltcp_port")) {
+				*ssltcp = atoi(parts[1]);
+			} else if (purple_strequal(parts[0], "username")) {
+				*username = g_strdup(parts[1]);
+			} else if (purple_strequal(parts[0], "password")) {
+				*password = g_strdup(parts[1]);
+			}
+		}
+		g_strfreev(parts);
+	}
+
+	g_strfreev(lines);
+}
+
+static void
+jabber_google_relay_remove_url_data(JabberStream *js, 
+	PurpleUtilFetchUrlData *url_data)
 {
-	GoogleSession *session;
-	JabberBuddy *jb;
-	JabberBuddyResource *jbr;
-	gchar *jid;
+	GList *iter = js->google_relay_requests;
+
+	while (iter) {
+		if (iter->data == url_data) {
+			js->google_relay_requests =
+				g_list_delete_link(js->google_relay_requests, iter);
+			break;
+		}
+	}
+}
+
+static void
+jabber_google_relay_response_session_initiate_cb(PurpleUtilFetchUrlData *url_data, 
+	gpointer user_data, const gchar *url_text, gsize len, 
+	const gchar *error_message)
+{
+	GoogleSession *session = (GoogleSession *) user_data;
 	GParameter *params;
 	guint num_params;
-	GoogleAVSessionData *session_data;
+	JabberStream *js = session->js;
+	gchar *relay_ip = NULL;
+	guint relay_udp = 0;
+	guint relay_tcp = 0;
+	guint relay_ssltcp = 0;
+	gchar *relay_username = NULL;
+	gchar *relay_password = NULL;
+	GoogleAVSessionData *session_data =
+		(GoogleAVSessionData *) session->session_data;
 	
-	/* 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;
+	if (url_data) {
+		jabber_google_relay_remove_url_data(js, url_data);
 	}
-	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);
-	}
+	purple_debug_info("jabber", "got response on HTTP request to relay server\n");
 
-	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;
-	session_data = g_new0(GoogleAVSessionData, 1);
-	session->session_data = session_data;
-	
-	if (type & PURPLE_MEDIA_VIDEO)
-		session_data->video = TRUE;
+	if (url_text && len > 0) {
+		purple_debug_info("jabber", "got Google relay request response:\n%s\n",
+			url_text);
+		jabber_google_relay_parse_response(url_text, &relay_ip, &relay_udp,
+			&relay_tcp, &relay_ssltcp, &relay_username, &relay_password);
+	}
 
 	session_data->media = purple_media_manager_create_media(
 			purple_media_manager_get(),
@@ -380,8 +418,14 @@ jabber_google_session_initiate(JabberStr
 	g_signal_connect(G_OBJECT(session_data->media), "stream-info",
 			G_CALLBACK(google_session_stream_info_cb), session);
 
-	params = jabber_google_session_get_params(js, &num_params);
+	params =
+		jabber_google_session_get_params(js, relay_ip, relay_udp, relay_tcp,
+			relay_ssltcp, relay_username, relay_password, &num_params);
 
+	g_free(relay_ip);
+	g_free(relay_username);
+	g_free(relay_password);
+	
 	if (purple_media_add_stream(session_data->media, "google-voice",
 			session->remote_jid, PURPLE_MEDIA_AUDIO,
 			TRUE, "nice", num_params, params) == FALSE ||
@@ -392,63 +436,135 @@ jabber_google_session_initiate(JabberStr
 		purple_media_error(session_data->media, "Error adding stream.");
 		purple_media_end(session_data->media, NULL, NULL);
 		g_free(params);
-		return FALSE;
 	}
 
-	g_free(params);
+	g_free(params);	
+}
 
-	return (session_data->media != NULL) ? TRUE : FALSE;
+static void
+jabber_google_do_relay_request(JabberStream *js, GoogleSession *session,
+	PurpleUtilFetchUrlCallback cb)
+{
+	PurpleUtilFetchUrlData *url_data = NULL;
+	gchar *url = g_strdup_printf("http://%s", js->google_relay_host);
+	gchar *request =
+		g_strdup_printf("GET /create_session HTTP/1.0\r\n"
+			            "Host: %s\r\n"
+						"X-Talk-Google-Relay-Auth: %s\r\n"
+						"X-Google-Relay-Auth: %s\r\n\r\n", 
+			js->google_relay_host, js->google_relay_token, js->google_relay_token);
+	purple_debug_info("jabber", 
+		"sending Google relay request %s to %s\n", request, url); 
+	url_data = 
+		purple_util_fetch_url_request(url, FALSE, NULL, FALSE, request, FALSE,
+			cb, session);
+	if (url_data) {
+		js->google_relay_requests =
+			g_list_prepend(js->google_relay_requests, url_data);
+	} else {
+		purple_debug_error("jabber", "unable to create Google relay request\n");
+		cb(NULL, session, NULL, 0, NULL);
+	}
+	g_free(url);
+	g_free(request);
 }
 
-static gboolean
-google_session_handle_initiate(JabberStream *js, GoogleSession *session, xmlnode *sess, const char *iq_id)
+gboolean
+jabber_google_session_initiate(JabberStream *js, const gchar *who, PurpleMediaSessionType type)
 {
-	JabberIq *result;
-	GList *codecs = NULL, *video_codecs = NULL;
-	xmlnode *desc_element, *codec_element;
-	PurpleMediaCodec *codec;
-	const char *xmlns;
-	GParameter *params;
-	guint num_params;
-	GoogleAVSessionData *session_data =
-		(GoogleAVSessionData *) session->session_data;
-	
-	if (session->state != UNINIT) {
-		purple_debug_error("jabber", "Received initiate for active session.\n");
+	GoogleSession *session;
+	JabberBuddy *jb;
+	JabberBuddyResource *jbr;
+	gchar *jid;
+	GoogleAVSessionData *session_data = NULL;
+
+	/* 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");
+	}
 
-	desc_element = xmlnode_get_child(sess, "description");
-	xmlns = xmlnode_get_namespace(desc_element);
+	if ((strchr(who, '/') == NULL) && jbr && (jbr->name != NULL)) {
+		jid = g_strdup_printf("%s/%s", who, jbr->name);
+	} else {
+		jid = g_strdup(who);
+	}
 
-	if (purple_strequal(xmlns, NS_GOOGLE_SESSION_PHONE))
-		session_data->video = FALSE;
-	else if (purple_strequal(xmlns, NS_GOOGLE_SESSION_VIDEO))
+	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;
+	session_data = g_new0(GoogleAVSessionData, 1);
+	session->session_data = session_data;
+	
+	if (type & PURPLE_MEDIA_VIDEO)
 		session_data->video = TRUE;
-	else {
-		purple_debug_error("jabber", "Received initiate with "
-				"invalid namespace %s.\n", xmlns);
-		return FALSE;
+
+	/* 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) {
+		jabber_google_do_relay_request(js, session,
+			jabber_google_relay_response_session_initiate_cb);
+	} else {
+		jabber_google_relay_response_session_initiate_cb(NULL, session, NULL, 0,
+			NULL);
 	}
+	
+	/* we don't actually know yet wether it succeeded... maybe this is very
+	 wrong... */
+	return TRUE;
+}
 
-	session_data->media = purple_media_manager_create_media(
-			purple_media_manager_get(),
-			purple_connection_get_account(js->gc),
-			"fsrtpconference", session->remote_jid, FALSE);
+static void
+jabber_google_relay_response_session_handle_initiate_cb(
+    PurpleUtilFetchUrlData *url_data, 
+	gpointer user_data, const gchar *url_text, gsize len, 
+	const gchar *error_message)
+{
+	GoogleSession *session = (GoogleSession *) user_data;
+	GParameter *params;
+	guint num_params;
+	JabberStream *js = session->js;
+	gchar *relay_ip = NULL;
+	guint relay_udp = 0;
+	guint relay_tcp = 0;
+	guint relay_ssltcp = 0;
+	gchar *relay_username = NULL;
+	gchar *relay_password = NULL;
+	xmlnode *codec_element;
+	xmlnode *desc_element;
+	const gchar *xmlns;
+	PurpleMediaCodec *codec;
+	GList *video_codecs = NULL;
+	GList *codecs = NULL;
+	JabberIq *result;
+	GoogleAVSessionData *session_data =
+		(GoogleAVSessionData *) session->session_data;
 
-	purple_media_set_prpl_data(session_data->media, session);
+	if (url_data) {
+		jabber_google_relay_remove_url_data(js, url_data);
+	}
 
-	g_signal_connect_swapped(G_OBJECT(session_data->media),
-			"candidates-prepared",
-			G_CALLBACK(google_session_ready), session);
-	g_signal_connect_swapped(G_OBJECT(session_data->media), "codecs-changed",
-			G_CALLBACK(google_session_ready), session);
-	g_signal_connect(G_OBJECT(session_data->media), "state-changed",
-			G_CALLBACK(google_session_state_changed_cb), session);
-	g_signal_connect(G_OBJECT(session_data->media), "stream-info",
-			G_CALLBACK(google_session_stream_info_cb), session);
+	if (url_text && len > 0) {
+		purple_debug_info("jabber", "got Google relay request response:\n%s\n",
+			url_text);
+		jabber_google_relay_parse_response(url_text, &relay_ip, &relay_udp,
+			&relay_tcp, &relay_ssltcp, &relay_username, &relay_password);
+	}
 
-	params = jabber_google_session_get_params(js, &num_params);
+	params = 
+		jabber_google_session_get_params(js, relay_ip, relay_udp, relay_tcp, 
+	    	relay_ssltcp, relay_username, relay_password, &num_params);
 
 	if (purple_media_add_stream(session_data->media, "google-voice",
 			session->remote_jid, PURPLE_MEDIA_AUDIO, FALSE,
@@ -461,7 +577,6 @@ google_session_handle_initiate(JabberStr
 		purple_media_stream_info(session_data->media,
 				PURPLE_MEDIA_INFO_REJECT, NULL, NULL, TRUE);
 		g_free(params);
-		return FALSE;
 	}
 
 	g_free(params);
@@ -517,13 +632,68 @@ google_session_handle_initiate(JabberStr
 	purple_media_codec_list_free(video_codecs);
 
 	result = jabber_iq_new(js, JABBER_IQ_RESULT);
-	jabber_iq_set_id(result, iq_id);
+	jabber_iq_set_id(result, session->iq_id);
 	xmlnode_set_attrib(result->node, "to", session->remote_jid);
 	jabber_iq_send(result);
+}
 
+static gboolean
+google_session_handle_initiate(JabberStream *js, GoogleSession *session, xmlnode *sess, const char *iq_id)
+{
+	xmlnode *desc_element;
+	const gchar *xmlns;
+	GoogleAVSessionData *session_data =
+		(GoogleAVSessionData *) session->session_data;
+	
+	if (session->state != UNINIT) {
+		purple_debug_error("jabber", "Received initiate for active session.\n");
+		return FALSE;
+	}
+
+	desc_element = xmlnode_get_child(sess, "description");
+	xmlns = xmlnode_get_namespace(desc_element);
+
+	if (purple_strequal(xmlns, NS_GOOGLE_SESSION_PHONE))
+		session_data->video = FALSE;
+	else if (purple_strequal(xmlns, NS_GOOGLE_SESSION_VIDEO))
+		session_data->video = TRUE;
+	else {
+		purple_debug_error("jabber", "Received initiate with "
+				"invalid namespace %s.\n", xmlns);
+		return FALSE;
+	}
+
+	session_data->media = purple_media_manager_create_media(
+			purple_media_manager_get(),
+			purple_connection_get_account(js->gc),
+			"fsrtpconference", session->remote_jid, FALSE);
+
+	purple_media_set_prpl_data(session_data->media, session);
+
+	g_signal_connect_swapped(G_OBJECT(session_data->media),
+			"candidates-prepared",
+			G_CALLBACK(google_session_ready), session);
+	g_signal_connect_swapped(G_OBJECT(session_data->media), "codecs-changed",
+			G_CALLBACK(google_session_ready), session);
+	g_signal_connect(G_OBJECT(session_data->media), "state-changed",
+			G_CALLBACK(google_session_state_changed_cb), session);
+	g_signal_connect(G_OBJECT(session_data->media), "stream-info",
+			G_CALLBACK(google_session_stream_info_cb), session);
+
+	session->iq_id = g_strdup(iq_id);
+	
+	if (js->google_relay_host && js->google_relay_token) {
+		jabber_google_do_relay_request(js, session, 
+			jabber_google_relay_response_session_handle_initiate_cb);
+	} else {
+		jabber_google_relay_response_session_handle_initiate_cb(NULL, session,
+			NULL, 0, NULL);
+	}
+
 	return TRUE;
 }
 
+
 static void
 google_session_handle_candidates(JabberStream  *js, GoogleSession *session, xmlnode *sess, const char *iq_id)
 {
============================================================
--- libpurple/protocols/jabber/google/google_session.h	276c8948293c255688af95adee7cd0b13853acca
+++ libpurple/protocols/jabber/google/google_session.h	442150502421974e97741778f24e2a856d48a676
@@ -41,6 +41,7 @@ typedef struct {
 	GoogleSessionState state;
 	JabberStream *js;
 	char *remote_jid;
+	char *iq_id;
 	gpointer session_data;
 } GoogleSession;
 
============================================================
--- libpurple/protocols/jabber/google/jingleinfo.c	6c0672dd63fd4485e6d835241794d56b0546a541
+++ libpurple/protocols/jabber/google/jingleinfo.c	8579fd72a6ab4f47db130543e26240673017ea4b
@@ -76,6 +76,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;
 
 	/*
@@ -119,8 +120,23 @@ jabber_google_jingle_info_common(JabberS
 			}
 		}
 	}
-	/* should perhaps handle relays later on, or maybe wait until
-	 Google supports a common standard... */
+
+	if (relay) {
+		xmlnode *token = xmlnode_get_child(relay, "token");
+		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


More information about the Commits mailing list