/
xmpp.c
479 lines (411 loc) · 13 KB
/
xmpp.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
/*
* XMPP Module
* This file is part of opensips, a free SIP server.
*
* Copyright (C) 2006 Voice Sistem S.R.L.
*
* opensips 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
*
* opensips 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Author: Andreea Spirea
*
*/
/*
* 2-way dialback sequence with xmppd2:
*
* Originating server (us) Receiving server (them) Authoritative server (us)
* ----------------------- ----------------------- -------------------------
* | | |
* | establish connection | |
* |------------------------------>| |
* | send stream header | |
* |------------------------------>| |
* | send stream header | |
* |<------------------------------| |
* | send db:result request | |
* |------------------------------>| |
* | establish connection |
* |------------------------------>|
* | send stream header |
* |------------------------------>|
* | send stream header |
* |<------------------------------|
* | send db:result request |
* |------------------------------>|
* | send db:verify request |
* |------------------------------>|
* | send db:verify response |
* |<------------------------------|
* | send db:result response |
* |------------------------------>|
* | send db:verify request |
* |<------------------------------|
* | send db:verify response |
* |------------------------------>|
* | send db:result response |
* |<------------------------------|
* : : :
* : : :
* | outgoing <message/> | :
* |------------------------------>| :
* | incoming <message/> |
* |------------------------------>|
*/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include "../../sr_module.h"
#include "../../mem/mem.h"
#include "../../data_lump_rpl.h"
#include "../../parser/msg_parser.h"
#include "../../parser/parse_content.h"
#include "../../parser/parse_from.h"
#include "../tm/tm_load.h"
#include "../../pt.h"
#include "xode.h"
#include "xmpp.h"
#include "network.h"
#include "xmpp_api.h"
#include <arpa/inet.h>
/* XXX hack */
#define DB_KEY "this-be-a-random-key"
struct tm_binds tmb;
static int mod_init(void);
static void xmpp_process(int rank);
static int cmd_send_message(struct sip_msg* msg, char* _foo, char* _bar);
void destroy(void);
static int child_init(int);
static int pipe_fds[2] = {-1,-1};
#define XMPP_COMP 1
#define XMPP_SERV 2
int backend_mode = XMPP_COMP;
/*
* Configuration
*/
char *backend = "component";
char *domain_sep_str = NULL;
char *xmpp_domain = "127.0.0.1";
char *xmpp_host = "127.0.0.1";
str sip_domain= {0, 0};
int xmpp_port = 0;
char *xmpp_password = "secret";
str outbound_proxy= {0, 0};
int pid = 0;
int* xmpp_pid;
#define DEFAULT_COMPONENT_PORT 5347
#define DEFAULT_SERVER_PORT 5269
static proc_export_t procs[] = {
{"XMPP receiver", 0, 0, xmpp_process, 1, 0},
{0,0,0,0,0,0}
};
/*
* Exported functions
*/
static cmd_export_t cmds[] = {
{"xmpp_send_message", (cmd_function)cmd_send_message, {{0,0,0}},
REQUEST_ROUTE},
{"bind_xmpp", (cmd_function)bind_xmpp,
{{0,0,0}}, ALL_ROUTES},
{0,0,{{0,0,0}},0}
};
/*
* Exported parameters
*/
static param_export_t params[] = {
{ "backend", STR_PARAM, &backend },
{ "xmpp_domain", STR_PARAM, &xmpp_domain },
{ "xmpp_host", STR_PARAM, &xmpp_host },
{ "xmpp_port", INT_PARAM, &xmpp_port },
{ "xmpp_password", STR_PARAM, &xmpp_password },
{ "outbound_proxy", STR_PARAM, &outbound_proxy.s},
{ "sip_domain", STR_PARAM, &sip_domain.s },
{0, 0, 0}
};
static dep_export_t deps = {
{ /* OpenSIPS module dependencies */
{ MOD_TYPE_DEFAULT, "tm", DEP_ABORT },
{ MOD_TYPE_NULL, NULL, 0 },
},
{ /* modparam dependencies */
{ NULL, NULL },
},
};
/*
* Module description
*/
struct module_exports exports = {
"xmpp", /* Module name */
MOD_TYPE_DEFAULT,/* class of this module */
MODULE_VERSION,
DEFAULT_DLFLAGS, /* dlopen flags */
&deps, /* OpenSIPS module dependencies */
cmds, /* Exported functions */
0, /* Exported async functions */
params, /* Exported parameters */
0, /* exported statistics */
0, /* exported MI functions */
0, /* exported pseudo-variables */
0, /* exported transformations */
procs, /* extra processes */
mod_init, /* Initialization function */
0, /* Response function */
destroy, /* Destroy function */
child_init, /* Child init function */
};
/*
* initialize module
*/
static int mod_init(void) {
LM_DBG("initializing\n");
if (load_tm_api(&tmb)) {
LM_ERR("failed to load tm API\n");
return -1;
}
if (strcmp(backend, "component") && strcmp(backend, "server")) {
LM_ERR("invalid backend '%s'\n", backend);
return -1;
}
if (!xmpp_port) {
if (!strcmp(backend, "component"))
xmpp_port = DEFAULT_COMPONENT_PORT;
else if (!strcmp(backend, "server"))
xmpp_port = DEFAULT_SERVER_PORT;
}
if(outbound_proxy.s)
outbound_proxy.len= strlen(outbound_proxy.s);
if(sip_domain.s)
sip_domain.len = strlen(sip_domain.s);
if(init_xmpp_cb_list()<0){
LM_ERR("failed to init callback list\n");
return -1;
}
if (pipe(pipe_fds) < 0) {
LM_ERR("pipe() failed\n");
return -1;
}
xmpp_pid = (int*)shm_malloc(sizeof(int));
if(xmpp_pid == NULL) {
LM_ERR("No more shared memory\n");
return -1;
}
return 0;
}
static int child_init(int rank)
{
LM_NOTICE("init_child [%d] pid [%d]\n", rank, getpid());
pid = my_pid();
return 0;
}
static void xmpp_process(int rank)
{
/* if this blasted server had a decent I/O loop, we'd
* just add our socket to it and connect().
*/
close(pipe_fds[1]);
pid = my_pid();
*xmpp_pid = pid;
LM_DBG("started child connection process\n");
if (!strcmp(backend, "component")) {
backend_mode = XMPP_COMP;
xmpp_component_child_process(pipe_fds[0]);
}
else if (!strcmp(backend, "server")) {
backend_mode = XMPP_SERV;
xmpp_server_child_process(pipe_fds[0]);
}
}
/* TODO */
void destroy(void)
{
LM_DBG("cleaning up...\n");
shm_free(xmpp_pid);
}
/*********************************************************************************/
/* Relay a MESSAGE to a SIP client */
int xmpp_send_sip_msg(char *from, char *to, char *msg)
{
str msg_type = { "MESSAGE", 7 };
str hdr, fromstr, tostr, msgstr;
char buf[512];
char buf_from[256];
ENC_SIP_URI(fromstr, buf_from, from);
hdr.s = buf;
hdr.len = snprintf(buf, sizeof(buf),
"Content-type: text/plain" CRLF "Contact: %s" CRLF, from);
tostr.s = uri_xmpp2sip(to, &tostr.len);
if(tostr.s == NULL) {
LM_ERR("Failed to translate xmpp uri to sip uri\n");
return -1;
}
msgstr.s = msg;
msgstr.len = strlen(msg);
return tmb.t_request(
&msg_type, /* Type of the message */
0, /* Request-URI */
&tostr, /* To */
&fromstr, /* From */
&hdr, /* Optional headers */
&msgstr, /* Message body */
(outbound_proxy.s)?&outbound_proxy:NULL,/* Outbound proxy*/
0, /* Callback function */
0,
0 /* Callback parameter */
);
}
/*********************************************************************************/
void xmpp_free_pipe_cmd(struct xmpp_pipe_cmd *cmd)
{
if (cmd->from)
shm_free(cmd->from);
if (cmd->to)
shm_free(cmd->to);
if (cmd->body)
shm_free(cmd->body);
if (cmd->id)
shm_free(cmd->id);
shm_free(cmd);
}
static int xmpp_send_pipe_cmd(enum xmpp_pipe_cmd_type type, str *from, str *to,
str *body, str *id)
{
struct xmpp_pipe_cmd *cmd;
str ret = {0,0};
/* todo: make shm allocation for one big chunk to include all fields */
cmd = (struct xmpp_pipe_cmd *) shm_malloc(sizeof(struct xmpp_pipe_cmd));
memset(cmd, 0, sizeof(struct xmpp_pipe_cmd));
cmd->type = type;
shm_nt_str_dup(&ret, from);
cmd->from = ret.s;
shm_nt_str_dup(&ret, to);
cmd->to = ret.s;
shm_nt_str_dup(&ret, body);
cmd->body = ret.s;
shm_nt_str_dup(&ret, id);
cmd->id = ret.s;
if(pid == *xmpp_pid) /* if attempting to send from the xmpp extra process */
{
LM_DBG("I am the XMPP extra process\n");
if(backend_mode == XMPP_COMP) {
struct xmpp_private_data priv;
priv.fd = curr_fd;
priv.running = 1;
xmpp_component_net_send(cmd, &priv);
}
else {
xmpp_server_net_send(cmd);
}
}
else
{
if (write(pipe_fds[1], &cmd, sizeof(cmd)) != sizeof(cmd)) {
LM_ERR("failed to write to command pipe: %s\n", strerror(errno));
xmpp_free_pipe_cmd(cmd);
return -1;
}
}
return 0;
}
static int cmd_send_message(struct sip_msg* msg, char* _foo, char* _bar)
{
str body, from_uri, dst, tagid;
int mime;
LM_DBG("cmd_send_message\n");
/* extract body */
if (get_body(msg,&body)!=0 || body.len==0) {
LM_ERR("failed to extract body\n");
return -1;
}
if ((mime = parse_content_type_hdr(msg)) < 1) {
LM_ERR("failed parse content-type\n");
return -1;
}
if (mime != (TYPE_TEXT << 16) + SUBTYPE_PLAIN
&& mime != (TYPE_MESSAGE << 16) + SUBTYPE_CPIM) {
LM_ERR("invalid content-type 0x%x\n", mime);
return -1;
}
/* extract sender */
if (parse_headers(msg, HDR_TO_F | HDR_FROM_F, 0) == -1 || !msg->to
|| !msg->from) {
LM_ERR("no To/From headers\n");
return -1;
}
if (parse_from_header(msg) < 0 || !msg->from->parsed) {
LM_ERR("failed to parse From header\n");
return -1;
}
from_uri.s = uri_sip2xmpp(&((struct to_body *) msg->from->parsed)->uri);
from_uri.len = strlen(from_uri.s);
tagid = ((struct to_body *) msg->from->parsed)->tag_value;
LM_DBG("message from <%.*s>\n", from_uri.len, from_uri.s);
/* extract recipient */
dst.len = 0;
if (msg->new_uri.len > 0) {
LM_DBG("using new URI as destination\n");
dst = msg->new_uri;
} else if (msg->first_line.u.request.uri.s
&& msg->first_line.u.request.uri.len > 0) {
LM_DBG("using R-URI as destination\n");
dst = msg->first_line.u.request.uri;
} else if (msg->to->parsed) {
LM_DBG("using TO-URI as destination\n");
dst = ((struct to_body *) msg->to->parsed)->uri;
} else {
LM_ERR("failed to find a valid destination\n");
return -1;
}
dst.s = dst.s + 4;
dst.len = dst.len - 4;
if (!xmpp_send_pipe_cmd(XMPP_PIPE_SEND_MESSAGE, &from_uri, &dst, &body,
&tagid))
return 1;
return -1;
}
/**
*
*/
int xmpp_send_xpacket(str *from, str *to, str *msg, str *id)
{
if(from==NULL || to==NULL || msg==NULL || id==NULL)
return -1;
return xmpp_send_pipe_cmd(XMPP_PIPE_SEND_PACKET, from, to, msg, id);
}
/**
*
*/
int xmpp_send_xmessage(str *from, str *to, str *msg, str *id)
{
if(from==NULL || to==NULL || msg==NULL || id==NULL)
return -1;
return xmpp_send_pipe_cmd(XMPP_PIPE_SEND_MESSAGE, from, to, msg, id);
}
/**
*
*/
int xmpp_send_xsubscribe(str *from, str *to, str *msg, str *id)
{
if(from==NULL || to==NULL || msg==NULL || id==NULL)
return -1;
return xmpp_send_pipe_cmd(XMPP_PIPE_SEND_PSUBSCRIBE, from, to, msg, id);
}
/**
*
*/
int xmpp_send_xnotify(str *from, str *to, str *msg, str *id)
{
if(from==NULL || to==NULL || msg==NULL || id==NULL)
return -1;
return xmpp_send_pipe_cmd(XMPP_PIPE_SEND_PNOTIFY, from, to, msg, id);
}