~micahg/ubuntu/natty/pidgin/2.7.9-2

« back to all changes in this revision

Viewing changes to libpurple/protocols/jabber/google/jingleinfo.c

  • Committer: Bazaar Package Importer
  • Author(s): Sebastien Bacher
  • Date: 2010-11-05 19:44:21 UTC
  • mfrom: (62.1.1 maverick-security) (2.3.12 sid)
  • Revision ID: james.westby@ubuntu.com-20101105194421-8r8o4pzw2m5j4hiy
Tags: 1:2.7.5-1ubuntu1
Resync on Debian

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
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
 
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
                                int port = atoi(udp);
 
113
                                /* if there, would already be an ongoing query,
 
114
                                 cancel it */
 
115
                                if (js->stun_query)
 
116
                                        purple_dnsquery_destroy(js->stun_query);
 
117
 
 
118
                                js->stun_query = purple_dnsquery_a(host, port,
 
119
                                        jabber_google_stun_lookup_cb, js);
 
120
                        }
 
121
                }
 
122
        }
 
123
 
 
124
        if (relay) {
 
125
                xmlnode *token = xmlnode_get_child(relay, "token");
 
126
                xmlnode *server = xmlnode_get_child(relay, "server");
 
127
                
 
128
                if (token) {
 
129
                        gchar *relay_token = xmlnode_get_data(token);
 
130
 
 
131
                        /* we let js own the string returned from xmlnode_get_data */
 
132
                        js->google_relay_token = relay_token;
 
133
                }
 
134
 
 
135
                if (server) {
 
136
                        js->google_relay_host = 
 
137
                                g_strdup(xmlnode_get_attrib(server, "host"));
 
138
                }
 
139
        }
 
140
}
 
141
 
 
142
static void
 
143
jabber_google_jingle_info_cb(JabberStream *js, const char *from,
 
144
                             JabberIqType type, const char *id,
 
145
                             xmlnode *packet, gpointer data)
 
146
{
 
147
        xmlnode *query = xmlnode_get_child_with_namespace(packet, "query",
 
148
                        NS_GOOGLE_JINGLE_INFO);
 
149
 
 
150
        if (query)
 
151
                jabber_google_jingle_info_common(js, from, type, query);
 
152
        else
 
153
                purple_debug_warning("jabber", "Got invalid google:jingleinfo\n");
 
154
}
 
155
 
 
156
void
 
157
jabber_google_handle_jingle_info(JabberStream *js, const char *from,
 
158
                                 JabberIqType type, const char *id,
 
159
                                 xmlnode *child)
 
160
{
 
161
        jabber_google_jingle_info_common(js, from, type, child);
 
162
}
 
163
 
 
164
void
 
165
jabber_google_send_jingle_info(JabberStream *js)
 
166
{
 
167
        JabberIq *jingle_info =
 
168
                jabber_iq_new_query(js, JABBER_IQ_GET, NS_GOOGLE_JINGLE_INFO);
 
169
 
 
170
        jabber_iq_set_callback(jingle_info, jabber_google_jingle_info_cb,
 
171
                NULL);
 
172
        purple_debug_info("jabber", "sending google:jingleinfo query\n");
 
173
        jabber_iq_send(jingle_info);
 
174
}