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.
5 *
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.
10 *
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.
15 *
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
19 */
20
21 #include "internal.h"
22 #include "debug.h"
23 #include "jingleinfo.h"
24
25 static void
jabber_google_stun_lookup_cb(GSList * hosts,gpointer data,const char * error_message)26 jabber_google_stun_lookup_cb(GSList *hosts, gpointer data,
27 const char *error_message)
28 {
29 JabberStream *js = (JabberStream *) data;
30
31 if (error_message) {
32 purple_debug_error("jabber", "Google STUN lookup failed: %s\n",
33 error_message);
34 g_slist_free(hosts);
35 js->stun_query = NULL;
36 return;
37 }
38
39 if (hosts && g_slist_next(hosts)) {
40 struct sockaddr *addr = g_slist_next(hosts)->data;
41 char dst[INET6_ADDRSTRLEN];
42 int port;
43
44 if (addr->sa_family == AF_INET6) {
45 inet_ntop(addr->sa_family, &((struct sockaddr_in6 *) addr)->sin6_addr,
46 dst, sizeof(dst));
47 port = ntohs(((struct sockaddr_in6 *) addr)->sin6_port);
48 } else {
49 inet_ntop(addr->sa_family, &((struct sockaddr_in *) addr)->sin_addr,
50 dst, sizeof(dst));
51 port = ntohs(((struct sockaddr_in *) addr)->sin_port);
52 }
53
54 if (js->stun_ip)
55 g_free(js->stun_ip);
56 js->stun_ip = g_strdup(dst);
57 js->stun_port = port;
58
59 purple_debug_info("jabber", "set Google STUN IP/port address: "
60 "%s:%d\n", dst, port);
61
62 /* unmark ongoing query */
63 js->stun_query = NULL;
64 }
65
66 while (hosts != NULL) {
67 hosts = g_slist_delete_link(hosts, hosts);
68 /* Free the address */
69 g_free(hosts->data);
70 hosts = g_slist_delete_link(hosts, hosts);
71 }
72 }
73
74 static void
jabber_google_jingle_info_common(JabberStream * js,const char * from,JabberIqType type,xmlnode * query)75 jabber_google_jingle_info_common(JabberStream *js, const char *from,
76 JabberIqType type, xmlnode *query)
77 {
78 const xmlnode *stun = xmlnode_get_child(query, "stun");
79 const xmlnode *relay = xmlnode_get_child(query, "relay");
80 gchar *my_bare_jid;
81
82 /*
83 * Make sure that random people aren't sending us STUN servers. Per
84 * http://code.google.com/apis/talk/jep_extensions/jingleinfo.html, these
85 * stanzas are stamped from our bare JID.
86 */
87 if (from) {
88 my_bare_jid = g_strdup_printf("%s@%s", js->user->node, js->user->domain);
89 if (!purple_strequal(from, my_bare_jid)) {
90 purple_debug_warning("jabber", "got google:jingleinfo with invalid from (%s)\n",
91 from);
92 g_free(my_bare_jid);
93 return;
94 }
95
96 g_free(my_bare_jid);
97 }
98
99 if (type == JABBER_IQ_ERROR || type == JABBER_IQ_GET)
100 return;
101
102 purple_debug_info("jabber", "got google:jingleinfo\n");
103
104 if (stun) {
105 xmlnode *server = xmlnode_get_child(stun, "server");
106
107 if (server) {
108 const gchar *host = xmlnode_get_attrib(server, "host");
109 const gchar *udp = xmlnode_get_attrib(server, "udp");
110
111 if (host && udp) {
112 PurpleAccount *account;
113 int port = atoi(udp);
114 /* if there, would already be an ongoing query,
115 cancel it */
116 if (js->stun_query)
117 purple_dnsquery_destroy(js->stun_query);
118
119 account = purple_connection_get_account(js->gc);
120 js->stun_query = purple_dnsquery_a_account(account, host, port,
121 jabber_google_stun_lookup_cb, js);
122 }
123 }
124 }
125
126 if (relay) {
127 xmlnode *token = xmlnode_get_child(relay, "token");
128 xmlnode *server = xmlnode_get_child(relay, "server");
129
130 if (token) {
131 gchar *relay_token = xmlnode_get_data(token);
132
133 /* we let js own the string returned from xmlnode_get_data */
134 js->google_relay_token = relay_token;
135 }
136
137 if (server) {
138 js->google_relay_host =
139 g_strdup(xmlnode_get_attrib(server, "host"));
140 }
141 }
142 }
143
144 static void
jabber_google_jingle_info_cb(JabberStream * js,const char * from,JabberIqType type,const char * id,xmlnode * packet,gpointer data)145 jabber_google_jingle_info_cb(JabberStream *js, const char *from,
146 JabberIqType type, const char *id,
147 xmlnode *packet, gpointer data)
148 {
149 xmlnode *query = xmlnode_get_child_with_namespace(packet, "query",
150 NS_GOOGLE_JINGLE_INFO);
151
152 if (query)
153 jabber_google_jingle_info_common(js, from, type, query);
154 else
155 purple_debug_warning("jabber", "Got invalid google:jingleinfo\n");
156 }
157
158 void
jabber_google_handle_jingle_info(JabberStream * js,const char * from,JabberIqType type,const char * id,xmlnode * child)159 jabber_google_handle_jingle_info(JabberStream *js, const char *from,
160 JabberIqType type, const char *id,
161 xmlnode *child)
162 {
163 jabber_google_jingle_info_common(js, from, type, child);
164 }
165
166 void
jabber_google_send_jingle_info(JabberStream * js)167 jabber_google_send_jingle_info(JabberStream *js)
168 {
169 JabberIq *jingle_info =
170 jabber_iq_new_query(js, JABBER_IQ_GET, NS_GOOGLE_JINGLE_INFO);
171
172 jabber_iq_set_callback(jingle_info, jabber_google_jingle_info_cb,
173 NULL);
174 purple_debug_info("jabber", "sending google:jingleinfo query\n");
175 jabber_iq_send(jingle_info);
176 }
177