Migrate certificates, icons, logs to XDG dirs
[pidgin-git.git] / libpurple / protocols / jabber / google / jingleinfo.c
blob6ea8811722c3513bd486badd85ab07f372f08407
1 /**
2 * Purple is the legal property of its developers, whose names are too numerous
3 * to list here. Please refer to the COPYRIGHT file distributed with this
4 * source distribution.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
21 #include "internal.h"
22 #include "debug.h"
23 #include "jingleinfo.h"
25 #include <gio/gio.h>
27 static void
28 jabber_google_stun_lookup_cb(GObject *sender, GAsyncResult *result, gpointer data) {
29 GError *error = NULL;
30 GList *addresses = NULL;
31 JabberStream *js = (JabberStream *) data;
33 addresses = g_resolver_lookup_by_name_finish(G_RESOLVER(sender),
34 result, &error);
36 if(error) {
37 purple_debug_error("jabber", "Google STUN lookup failed: %s\n",
38 error->message);
40 g_error_free(error);
42 return;
45 if(g_list_length(addresses) > 0) {
46 GInetAddress *inet_address = G_INET_ADDRESS(addresses->data);
48 g_free(js->stun_ip);
49 js->stun_ip = g_inet_address_to_string(inet_address);
51 purple_debug_info("jabber", "set Google STUN IP/port address: "
52 "%s:%d\n", js->stun_ip, js->stun_port);
55 g_resolver_free_addresses(addresses);
58 static void
59 jabber_google_jingle_info_common(JabberStream *js, const char *from,
60 JabberIqType type, PurpleXmlNode *query)
62 const PurpleXmlNode *stun = purple_xmlnode_get_child(query, "stun");
63 const PurpleXmlNode *relay = purple_xmlnode_get_child(query, "relay");
64 gchar *my_bare_jid;
67 * Make sure that random people aren't sending us STUN servers. Per
68 * https://developers.google.com/talk/jep_extensions/jingleinfo, these
69 * stanzas are stamped from our bare JID.
71 if (from) {
72 my_bare_jid = g_strdup_printf("%s@%s", js->user->node, js->user->domain);
73 if (!purple_strequal(from, my_bare_jid)) {
74 purple_debug_warning("jabber", "got google:jingleinfo with invalid from (%s)\n",
75 from);
76 g_free(my_bare_jid);
77 return;
80 g_free(my_bare_jid);
83 if (type == JABBER_IQ_ERROR || type == JABBER_IQ_GET)
84 return;
86 purple_debug_info("jabber", "got google:jingleinfo\n");
88 if (stun) {
89 PurpleXmlNode *server = purple_xmlnode_get_child(stun, "server");
91 if (server) {
92 const gchar *host = purple_xmlnode_get_attrib(server, "host");
93 const gchar *udp = purple_xmlnode_get_attrib(server, "udp");
95 if (host && udp) {
96 GResolver *resolver = g_resolver_get_default();
98 js->stun_port = atoi(udp);
100 g_resolver_lookup_by_name_async(resolver,
101 host,
102 NULL,
103 jabber_google_stun_lookup_cb,
104 js);
105 g_object_unref(resolver);
110 if (relay) {
111 PurpleXmlNode *token = purple_xmlnode_get_child(relay, "token");
112 PurpleXmlNode *server = purple_xmlnode_get_child(relay, "server");
114 if (token) {
115 gchar *relay_token = purple_xmlnode_get_data(token);
117 /* we let js own the string returned from purple_xmlnode_get_data */
118 js->google_relay_token = relay_token;
121 if (server) {
122 js->google_relay_host =
123 g_strdup(purple_xmlnode_get_attrib(server, "host"));
128 static void
129 jabber_google_jingle_info_cb(JabberStream *js, const char *from,
130 JabberIqType type, const char *id,
131 PurpleXmlNode *packet, gpointer data)
133 PurpleXmlNode *query = purple_xmlnode_get_child_with_namespace(packet, "query",
134 NS_GOOGLE_JINGLE_INFO);
136 if (query)
137 jabber_google_jingle_info_common(js, from, type, query);
138 else
139 purple_debug_warning("jabber", "Got invalid google:jingleinfo\n");
142 void
143 jabber_google_handle_jingle_info(JabberStream *js, const char *from,
144 JabberIqType type, const char *id,
145 PurpleXmlNode *child)
147 jabber_google_jingle_info_common(js, from, type, child);
150 void
151 jabber_google_send_jingle_info(JabberStream *js)
153 JabberIq *jingle_info =
154 jabber_iq_new_query(js, JABBER_IQ_GET, NS_GOOGLE_JINGLE_INFO);
156 jabber_iq_set_callback(jingle_info, jabber_google_jingle_info_cb,
157 NULL);
158 purple_debug_info("jabber", "sending google:jingleinfo query\n");
159 jabber_iq_send(jingle_info);