[r10338]: trunk / modules / event_rabbitmq / event_rabbitmq.c Maximize Restore History

Download this file

event_rabbitmq.c    587 lines (509 with data), 14.0 kB

  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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
/*
* Copyright (C) 2011 OpenSIPS Solutions
*
* This file is part of opensips, a free SIP server.
*
* 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*
* history:
* ---------
* 2011-05-xx created (razvancrainea)
*/
#include "../../sr_module.h"
#include "../../evi/evi_transport.h"
#include "../../ut.h"
#include "event_rabbitmq.h"
#include "rabbitmq_send.h"
#include <string.h>
/* send buffer */
static char rmq_buffer[RMQ_BUFFER_SIZE];
static int rmq_buffer_len;
/**
* module functions
*/
static int mod_init(void);
static int child_init(int);
static void destroy(void);
/**
* module parameters
*/
static unsigned int heartbeat = 0;
/**
* exported functions
*/
static evi_reply_sock* rmq_parse(str socket);
static int rmq_raise(struct sip_msg *msg, str* ev_name,
evi_reply_sock *sock, evi_params_t * params);
static int rmq_match(evi_reply_sock *sock1, evi_reply_sock *sock2);
static void rmq_free(evi_reply_sock *sock);
static str rmq_print(evi_reply_sock *sock);
/* sending process */
static proc_export_t procs[] = {
{"RabbitMQ sender", 0, 0, rmq_process, 1, 0},
{0,0,0,0,0,0}
};
/* module parameters */
static param_export_t mod_params[] = {
{"heartbeat", INT_PARAM, &heartbeat},
{0,0,0}
};
/**
* module exports
*/
struct module_exports exports= {
"event_rabbitmq", /* module name */
MODULE_VERSION,
DEFAULT_DLFLAGS, /* dlopen flags */
0, /* exported functions */
mod_params, /* exported parameters */
0, /* exported statistics */
0, /* exported MI functions */
0, /* exported pseudo-variables */
procs, /* extra processes */
mod_init, /* module initialization function */
0, /* response handling function */
destroy, /* destroy function */
child_init /* per-child init function */
};
/**
* exported functions for core event interface
*/
static evi_export_t trans_export_rmq = {
RMQ_STR, /* transport module name */
rmq_raise, /* raise function */
rmq_parse, /* parse function */
rmq_match, /* sockets match function */
rmq_free, /* free function */
rmq_print, /* print socket */
RMQ_FLAG /* flags */
};
/**
* init module function
*/
static int mod_init(void)
{
LM_NOTICE("initializing module ......\n");
if (register_event_mod(&trans_export_rmq)) {
LM_ERR("cannot register transport functions for RabbitMQ\n");
return -1;
}
if (rmq_create_pipe() < 0) {
LM_ERR("cannot create communication pipe\n");
return -1;
}
if ( heartbeat <= 0 || heartbeat > 65535) {
LM_WARN("heartbeat is disabled according to the modparam configuration\n");
heartbeat = 0;
} else {
LM_WARN("heartbeat is enabled for [%d] seconds\n", heartbeat);
}
return 0;
}
static int child_init(int rank)
{
if (rmq_init_writer() < 0) {
LM_ERR("cannot init writing pipe\n");
return -1;
}
return 0;
}
/* returns 0 if sockets don't match */
static int rmq_match(evi_reply_sock *sock1, evi_reply_sock *sock2)
{
rmq_params_t *p1, *p2;
/* sock flags */
if (!sock1 || !sock2 ||
!(sock1->flags & RMQ_FLAG) || !(sock2->flags & RMQ_FLAG) ||
!(sock1->flags & EVI_PARAMS) || !(sock2->flags & EVI_PARAMS) ||
!(sock1->flags & EVI_PORT) || !(sock2->flags & EVI_PORT) ||
!(sock1->flags & EVI_ADDRESS) || !(sock2->flags & EVI_ADDRESS))
return 0;
p1 = (rmq_params_t *)sock1->params;
p2 = (rmq_params_t *)sock2->params;
if (!p1 || !p2 ||
!(p1->flags & RMQ_PARAM_EXCH) || !(p2->flags & RMQ_PARAM_EXCH) ||
!(p1->flags & RMQ_PARAM_USER) || !(p2->flags & RMQ_PARAM_USER))
return 0;
if (sock1->port == sock2->port &&
sock1->address.len == sock2->address.len &&
p1->exchange.len == p2->exchange.len &&
p1->user.len == p2->user.len &&
(p1->user.s == p2->user.s || /* trying the static values */
!memcmp(p1->user.s, p2->user.s, p1->user.len)) &&
!memcmp(sock1->address.s, sock2->address.s, sock1->address.len) &&
!memcmp(p1->exchange.s, p2->exchange.s, p1->exchange.len)) {
LM_DBG("socket matched: %s@%s:%hu/%s\n",
p1->user.s, sock1->address.s, sock2->port, p1->exchange.s);
return 1;
}
return 0;
}
static inline int dupl_string(str* dst, const char* begin, const char* end)
{
if (dst->s)
shm_free(dst->s);
dst->s = shm_malloc(end - begin + 1);
if (!dst->s) {
LM_ERR("no more shm memory\n");
return -1;
}
memcpy(dst->s, begin, end - begin);
dst->s[end - begin] = 0;
dst->len = end - begin + 1;
return 0;
}
/*
* This is the parsing function
* The socket grammar should be:
* [user [':' password] '@'] ip [':' port] '/' exchange
*/
static evi_reply_sock* rmq_parse(str socket)
{
evi_reply_sock *sock = NULL;
rmq_params_t *param;
unsigned int len, i;
const char* begin;
str prev_token;
enum state {
ST_USER_HOST, /* Username or hostname */
ST_PASS_PORT, /* Password or port part */
ST_HOST, /* Hostname part */
ST_PORT, /* Port part */
} st;
if (!socket.len || !socket.s) {
LM_ERR("no socket specified\n");
return NULL;
}
sock = shm_malloc(sizeof(evi_reply_sock) + sizeof(rmq_params_t));
if (!sock) {
LM_ERR("no more memory for socket\n");
return NULL;
}
memset(sock, 0, sizeof(evi_reply_sock) + sizeof(rmq_params_t));
param = (rmq_params_t*)(sock + 1);
prev_token.s = 0;
prev_token.len = 0;
/* Initialize all attributes to 0 */
st = ST_USER_HOST;
begin = socket.s;
len = socket.len;
for(i = 0; i < len; i++) {
switch(st) {
case ST_USER_HOST:
switch(socket.s[i]) {
case '@':
st = ST_HOST;
if (dupl_string(&param->user, begin, socket.s + i)) goto err;
begin = socket.s + i + 1;
param->flags |= RMQ_PARAM_USER;
break;
case ':':
st = ST_PASS_PORT;
if (dupl_string(&prev_token, begin, socket.s + i) < 0) goto err;
begin = socket.s + i + 1;
break;
case '/':
if (dupl_string(&sock->address, begin, socket.s + i) < 0)
goto err;
sock->flags |= EVI_ADDRESS;
if (dupl_string(&param->exchange, socket.s + i + 1,
socket.s + len) < 0)
goto err;
param->flags |= RMQ_PARAM_EXCH;
goto success;
}
break;
case ST_PASS_PORT:
switch(socket.s[i]) {
case '@':
st = ST_HOST;
param->user.len = prev_token.len;
param->user.s = prev_token.s;
param->flags |= RMQ_PARAM_USER;
prev_token.s = 0;
if (dupl_string(&param->pass, begin, socket.s + i) < 0)
goto err;
param->flags |= RMQ_PARAM_PASS;
begin = socket.s + i + 1;
break;
case '/':
sock->address.len = prev_token.len;
sock->address.s = prev_token.s;
prev_token.s = 0;
sock->flags |= EVI_ADDRESS;
sock->port = str2s(begin, socket.s + i - begin, 0);
if (!sock->port) {
LM_DBG("malformed port: %.*s\n",
(int)(socket.s + i - begin), begin);
goto err;
}
sock->flags |= EVI_PORT;
if (dupl_string(&param->exchange, socket.s + i + 1,
socket.s + len) < 0)
goto err;
param->flags |= RMQ_PARAM_EXCH;
goto success;
}
break;
case ST_HOST:
switch(socket.s[i]) {
case ':':
st = ST_PORT;
if (dupl_string(&sock->address, begin, socket.s + i) < 0)
goto err;
sock->flags |= EVI_ADDRESS;
begin = socket.s + i + 1;
break;
case '/':
if (dupl_string(&sock->address, begin, socket.s + i) < 0)
goto err;
sock->flags |= EVI_ADDRESS;
if (dupl_string(&param->exchange, socket.s + i + 1,
socket.s + len) < 0)
goto err;
param->flags |= RMQ_PARAM_EXCH;
goto success;
}
break;
case ST_PORT:
switch(socket.s[i]) {
case '/':
sock->port = str2s(begin, socket.s + i - begin, 0);
if (!sock->port) {
LM_DBG("malformed port: %.*s\n",
(int)(socket.s + i - begin), begin);
goto err;
}
sock->flags |= EVI_PORT;
if (dupl_string(&param->exchange, socket.s + i + 1,
socket.s + len) < 0)
goto err;
param->flags |= RMQ_PARAM_EXCH;
goto success;
}
break;
}
}
LM_WARN("not implemented %.*s\n", socket.len, socket.s);
goto err;
success:
if (!(sock->flags & EVI_PORT) || !sock->port) {
sock->port = RMQ_DEFAULT_PORT;
sock->flags |= EVI_PORT;
}
if (!(param->flags & RMQ_PARAM_USER) || !param->user.s) {
param->user.s = param->pass.s = RMQ_DEFAULT_UP;
param->user.len = param->pass.len = RMQ_DEFAULT_UP_LEN;
param->flags |= RMQ_PARAM_USER|RMQ_PARAM_PASS;
param->heartbeat = heartbeat;
}
sock->params = param;
sock->flags |= EVI_PARAMS | RMQ_FLAG;
return sock;
err:
LM_ERR("error while parsing socket %.*s\n", socket.len, socket.s);
if (prev_token.s)
shm_free(prev_token.s);
rmq_free_param(param);
if (sock->address.s)
shm_free(sock->address.s);
shm_free(sock);
return NULL;
}
#define DO_PRINT(_s, _l) \
do { \
if (rmq_print_s.len + (_l) > rmq_print_len) { \
int new_len = (rmq_print_s.len + (_l)) * 2; \
char *new_s = pkg_realloc(rmq_print_s.s, new_len); \
if (!new_s) { \
LM_ERR("no more pkg mem to realloc\n"); \
goto end; \
} \
rmq_print_s.s = new_s; \
rmq_print_len = new_len; \
} \
memcpy(rmq_print_s.s + rmq_print_s.len, (_s), (_l)); \
rmq_print_s.len += (_l); \
} while (0)
static int rmq_print_len = 0;
static str rmq_print_s = { 0, 0 };
static str rmq_print(evi_reply_sock *sock)
{
rmq_params_t * param;
rmq_print_s.len = 0;
if (!sock) {
LM_DBG("Nothing to print");
goto end;
}
if (!(sock->flags & EVI_PARAMS))
goto end;
param = sock->params;
if (param->flags & RMQ_PARAM_USER) {
DO_PRINT(param->user.s, param->user.len - 1 /* skip 0 */);
DO_PRINT("@", 1);
}
if (sock->flags & EVI_ADDRESS)
DO_PRINT(sock->address.s, sock->address.len - 1);
if (param->flags & RMQ_PARAM_EXCH) {
DO_PRINT("/", 1);
DO_PRINT(param->exchange.s, param->exchange.len - 1);
}
end:
return rmq_print_s;
}
#undef DO_PRINT
#define DO_COPY(buff, str, len) \
do { \
if ((buff) - rmq_buffer + (len) > RMQ_BUFFER_SIZE) { \
LM_ERR("buffer too small\n"); \
goto end; \
} \
memcpy((buff), (str), (len)); \
buff += (len); \
} while (0)
/* builds parameters list */
static int rmq_build_params(str* ev_name, evi_params_p ev_params)
{
evi_param_p node;
int len;
char *buff, *int_s, *p, *end, *old;
char quote = QUOTE_C, esc = ESC_C;
if (ev_params && ev_params->flags & RMQ_FLAG) {
LM_DBG("buffer already built\n");
return rmq_buffer_len;
}
rmq_buffer_len = 0;
/* first is event name - cannot be larger than the buffer size */
memcpy(rmq_buffer, ev_name->s, ev_name->len);
rmq_buffer_len = ev_name->len;
buff = rmq_buffer + ev_name->len;
if (!ev_params)
goto end;
for (node = ev_params->first; node; node = node->next) {
*buff = PARAM_SEP;
buff++;
/* parameter name */
if (node->name.len && node->name.s) {
DO_COPY(buff, node->name.s, node->name.len);
DO_COPY(buff, ATTR_SEP_S, ATTR_SEP_LEN);
}
if (node->flags & EVI_STR_VAL) {
/* it is a string value */
if (node->val.s.len && node->val.s.s) {
len++;
/* check to see if enclose is needed */
end = node->val.s.s + node->val.s.len;
for (p = node->val.s.s; p < end; p++)
if (*p == PARAM_SEP)
break;
if (p == end) {
/* copy the whole buffer */
DO_COPY(buff, node->val.s.s, node->val.s.len);
} else {
DO_COPY(buff, &quote, 1);
old = node->val.s.s;
/* search for '"' to escape */
for (p = node->val.s.s; p < end; p++)
if (*p == QUOTE_C) {
DO_COPY(buff, old, p - old);
DO_COPY(buff, &esc, 1);
old = p;
}
/* copy the rest of the string */
DO_COPY(buff, old, p - old);
DO_COPY(buff, &quote, 1);
}
}
} else if (node->flags & EVI_INT_VAL) {
int_s = int2str(node->val.n, &len);
DO_COPY(buff, int_s, len);
} else {
LM_DBG("unknown parameter type [%x]\n", node->flags);
}
}
end:
/* set buffer end */
*buff = 0;
rmq_buffer_len = buff - rmq_buffer + 1;
if (ev_params)
ev_params->flags |= RMQ_FLAG;
return rmq_buffer_len;
}
#undef DO_COPY
static int rmq_raise(struct sip_msg *msg, str* ev_name,
evi_reply_sock *sock, evi_params_t * params)
{
rmq_send_t *rmqs;
int len;
if (!sock || !(sock->flags & RMQ_FLAG)) {
LM_ERR("invalid socket type\n");
return -1;
}
/* sanity checks */
if ((sock->flags & (EVI_ADDRESS|EVI_PORT|EVI_PARAMS)) !=
(EVI_ADDRESS|EVI_PORT|EVI_PARAMS) ||
!sock->port || !sock->address.len || !sock->address.s) {
LM_ERR("socket doesn't have enough details\n");
return -1;
}
/* check connection */
/* build the params list */
if ((len = rmq_build_params(ev_name, params)) < 0) {
LM_ERR("error while building parameters list\n");
return -1;
}
rmqs = shm_malloc(sizeof(rmq_send_t) + len);
if (!rmqs) {
LM_ERR("no more shm memory\n");
return -1;
}
memcpy(rmqs->msg, rmq_buffer, len);
rmqs->sock = sock;
if (rmq_send(rmqs) < 0) {
LM_ERR("cannot send message\n");
shm_free(rmqs);
return -1;
}
return 0;
}
/*
* destroy function
*/
static void destroy(void)
{
LM_NOTICE("destroy module ...\n");
/* closing sockets */
rmq_destroy_pipe();
}
static void rmq_free(evi_reply_sock *sock)
{
rmq_send_t *rmqs = shm_malloc(sizeof(rmq_send_t) + 1);
if (!rmqs) {
LM_ERR("no more shm memory\n");
goto destroy;
}
rmqs->sock = sock;
rmqs->msg[0] = 0;
if (rmq_send(rmqs) < 0) {
LM_ERR("cannot send message\n");
goto destroy;
}
return;
destroy:
if (rmqs)
shm_free(rmqs);
rmq_destroy(sock);
}