/
jingle.c
252 lines (213 loc) · 7.56 KB
/
jingle.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/*
* jingle.c
*
* Copyrigth (C) 2010 Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA
*/
#include "config.h"
#include <glib.h>
#include <loudmouth/loudmouth.h>
#include <mcabber/xmpp.h>
#include <mcabber/hooks.h>
#include <mcabber/modules.h>
#include <mcabber/logprint.h>
#include <mcabber/xmpp_helper.h>
#include <mcabber/xmpp_defines.h>
#include "jingle.h"
#include "check.h"
#include "action-handlers.h"
#include "register.h"
static void jingle_register_lm_handlers(void);
static void jingle_unregister_lm_handlers(void);
static guint jingle_connect_hh(const gchar *hname, hk_arg_t *args, gpointer ignore);
static guint jingle_disconn_hh(const gchar *hname, hk_arg_t *args, gpointer ignore);
static void jingle_init(void);
static void jingle_uninit(void);
static LmMessageHandler* jingle_iq_handler = NULL;
static guint connect_hid = 0;
static guint disconn_hid = 0;
/**
* Must be aligned with the values in JingleAction
* for easy acces.
*/
struct JingleActionList jingle_action_list[] = {
{ NULL, NULL }, // for JINGLE_UNKNOWN_ACTION
{ "content-accept", NULL },
{ "content-add", NULL },
{ "content-modify", NULL },
{ "content-reject", NULL },
{ "content-remove", NULL },
{ "description-info", NULL },
{ "security-info", NULL },
{ "session-accept", NULL },
{ "session-info", NULL },
{ "session-initiate", handle_session_initiate },
{ "session-terminate", NULL },
{ "transport-accept", NULL },
{ "transport-info", NULL },
{ "transport-reject", NULL },
{ "transport-replace", NULL },
};
module_info_t info_jingle = {
.branch = MCABBER_BRANCH,
.api = MCABBER_API_VERSION,
.version = PROJECT_VERSION,
.description = "Main Jingle module,"
" required for file transport, voip...\n",
.requires = NULL,
.init = jingle_init,
.uninit = jingle_uninit,
.next = NULL,
};
LmHandlerResult jingle_handle_iq(LmMessageHandler *handler,
LmConnection *connection,
LmMessage *message,
gpointer user_data)
{
LmMessageSubType iqtype = lm_message_get_sub_type(message);
if (iqtype != LM_MESSAGE_SUB_TYPE_SET)
return LM_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
JingleNode *jn = g_new0(JingleNode, 1);
GError *error = NULL;
LmMessageNode *root = lm_message_get_node(message);
LmMessageNode *jnode = lm_message_node_get_child(root, "jingle");
if (!jnode) // no <jingle> element found
return LM_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
if (g_strcmp0(lm_message_node_get_attribute(jnode, "xmlns"), NS_JINGLE)) {
scr_log_print(LPRINT_DEBUG, "jingle: Received a jingle IQ with an invalid namespace");
return LM_HANDLER_RESULT_REMOVE_MESSAGE;
}
check_jingle(jnode, jn, &error);
if (error != NULL) {
if (error->domain == JINGLE_CHECK_ERROR) {
// request malformed, we reply with a bad-request
jingle_send_iq_error(message, "cancel", "bad-request", NULL);
}
g_clear_error(&error);
return LM_HANDLER_RESULT_REMOVE_MESSAGE;
}
scr_log_print(LPRINT_DEBUG, "jingle: Received a valid jingle IQ");
if (jingle_action_list[jn->action].handler == NULL) {
jingle_send_iq_error(message, "cancel", "feature-not-implemented",
"unsupported-info");
return LM_HANDLER_RESULT_REMOVE_MESSAGE;
}
jingle_action_list[jn->action].handler(message, jn);
return LM_HANDLER_RESULT_REMOVE_MESSAGE;
}
/**
* According to the specifications:
* "An entity that receives an IQ request of type "get" or "set" MUST
* reply with an IQ response of type "result" or "error"."
* For Jingle's IQ, we have to reply with an empty "result" IQ to acknowledge
* receipt.
*/
void jingle_ack_iq(LmMessage *m)
{
LmMessage *r;
r = lm_message_new_iq_from_query(m, LM_MESSAGE_SUB_TYPE_RESULT);
lm_connection_send(lconnection, r, NULL);
lm_message_unref(r);
}
/**
* Reply to a Jingle IQ with an error.
*/
LmMessage *jingle_new_iq_error(LmMessage *m, const gchar *errtype,
const gchar *cond, const gchar *jinglecond)
{
LmMessage *r;
LmMessageNode *err, *tmpnode;
r = lm_message_new_iq_from_query(m, LM_MESSAGE_SUB_TYPE_ERROR);
err = lm_message_node_add_child(r->node, "error", NULL);
lm_message_node_set_attribute(err, "type", errtype);
// error condition as defined by RFC 3920bis section 8.3.3
if (cond != NULL) {
tmpnode = lm_message_node_add_child(err, cond, NULL);
lm_message_node_set_attribute(tmpnode, "xmlns", NS_XMPP_STANZAS);
}
// jingle error condition as defined by XEP-0166 section 10
if (jinglecond != NULL) {
tmpnode = lm_message_node_add_child(err, jinglecond, NULL);
lm_message_node_set_attribute(tmpnode, "xmlns", NS_JINGLE_ERRORS);
}
return r;
}
void jingle_send_iq_error(LmMessage *m, const gchar *errtype,
const gchar *cond, const gchar *jinglecond)
{
LmMessage *r = jingle_new_iq_error(m, errtype, cond, jinglecond);
if (r) {
lm_connection_send(lconnection, r, NULL);
lm_message_unref(r);
}
}
/**
* Find the jingle_action corresponding to a string
*/
JingleAction jingle_action_from_str(const gchar* string)
{
guint i, actstrlen = sizeof(jingle_action_list)/sizeof(jingle_action_list[0]);
for (i = 0; i < actstrlen; i++)
if (!g_strcmp0(jingle_action_list[i].name, string))
return (JingleAction) i;
return JINGLE_UNKNOWN_ACTION;
}
static void jingle_unregister_lm_handlers(void)
{
if (lconnection) {
lm_connection_unregister_message_handler(lconnection, jingle_iq_handler,
LM_MESSAGE_TYPE_IQ);
}
}
static void jingle_register_lm_handlers(void)
{
jingle_unregister_lm_handlers();
if (lconnection) {
lm_connection_register_message_handler(lconnection, jingle_iq_handler,
LM_MESSAGE_TYPE_IQ,
LM_HANDLER_PRIORITY_FIRST);
}
}
static guint jingle_connect_hh(const gchar *hname, hk_arg_t *args, gpointer ignore)
{
jingle_register_lm_handlers();
return HOOK_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
}
static guint jingle_disconn_hh(const gchar *hname, hk_arg_t *args, gpointer ignore)
{
jingle_unregister_lm_handlers();
return HOOK_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
}
static void jingle_init(void)
{
jingle_iq_handler = lm_message_handler_new(jingle_handle_iq, NULL, NULL);
xmpp_add_feature(NS_JINGLE);
connect_hid = hk_add_handler(jingle_connect_hh, HOOK_POST_CONNECT,
G_PRIORITY_DEFAULT_IDLE, NULL);
disconn_hid = hk_add_handler(jingle_disconn_hh, HOOK_PRE_DISCONNECT,
G_PRIORITY_DEFAULT_IDLE, NULL);
jingle_register_lm_handlers();
}
static void jingle_uninit(void)
{
xmpp_del_feature(NS_JINGLE);
hk_del_handler(HOOK_POST_CONNECT, connect_hid);
hk_del_handler(HOOK_PRE_DISCONNECT, disconn_hid);
jingle_unregister_lm_handlers();
lm_message_handler_invalidate(jingle_iq_handler);
lm_message_handler_unref(jingle_iq_handler);
}