-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmsilo.c
2261 lines (1956 loc) · 58.1 KB
/
msilo.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
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
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* $Id$
*
* MSILO module
*
* Copyright (C) 2001-2003 FhG Fokus
*
* 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
* -------
*
* 2003-01-23: switched from t_uac to t_uac_dlg (dcm)
* 2003-02-28: protocolization of t_uac_dlg completed (jiri)
* 2003-03-11: updated to the new module interface (andrei)
* removed non-constant initializers to some strs (andrei)
* 2003-03-16: flags parameter added (janakj)
* 2003-04-05: default_uri #define used (jiri)
* 2003-04-06: db_init removed from mod_init, will be called from child_init
* now (janakj)
* 2003-04-07: m_dump takes a parameter which sets the way the outgoing URI
* is computed (dcm)
* 2003-08-05 adapted to the new parse_content_type_hdr function (bogdan)
* 2004-06-07 updated to the new DB api (andrei)
* 2006-09-10 m_dump now checks if registering UA supports MESSAGE method (jh)
* 2006-10-05 added max_messages module variable (jh)
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <unistd.h>
#include <fcntl.h>
#include <time.h>
#include <pthread.h>
#include "../../sr_module.h"
#include "../../dprint.h"
#include "../../ut.h"
#include "../../timer.h"
#include "../../mem/shm_mem.h"
#include "../../db/db.h"
#include "../../parser/parse_from.h"
#include "../../parser/parse_content.h"
#include "../../parser/contact/parse_contact.h"
#include "../../parser/parse_allow.h"
#include "../../parser/parse_methods.h"
#include "../../resolve.h"
#include "../../usr_avp.h"
#include "../../mod_fix.h"
#include "../tm/tm_load.h"
#include "../../pt.h"
#include "ms_msg_list.h"
#include "msg_retry.h"
#include "msfuncs.h"
#include "msilo.h"
#include "ms_amqp.h"
#define MAX_DEL_KEYS 1
#define MAX_PEEK_NUM 10
#define NR_KEYS 11
#define PH_SQL_BUF_LEN 2048
#define MSG_BODY_BUFF_LEN 2048
#define MSG_HDR_BUFF_LEN 1024
static str sc_mid = str_init("id"); /* 0 */
static str sc_from = str_init("src_addr"); /* 1 */
static str sc_to = str_init("dst_addr"); /* 2 */
static str sc_uri_user = str_init("username"); /* 3 */
static str sc_uri_host = str_init("domain"); /* 4 */
static str sc_body = str_init("body"); /* 5 */
static str sc_ctype = str_init("ctype"); /* 6 */
static str sc_exp_time = str_init("exp_time"); /* 7 */
static str sc_inc_time = str_init("inc_time"); /* 8 */
static str sc_snd_time = str_init("snd_time"); /* 9 */
static str sc_msg_type = str_init("msg_type"); /* 10 */
#define SET_STR_VAL(_str, _res, _r, _c) \
if (RES_ROWS(_res)[_r].values[_c].nul == 0) \
{ \
switch(RES_ROWS(_res)[_r].values[_c].type) \
{ \
case DB_STRING: \
(_str).s=(char*)RES_ROWS(_res)[_r].values[_c].val.string_val; \
(_str).len=strlen((_str).s); \
break; \
case DB_STR: \
(_str).len=RES_ROWS(_res)[_r].values[_c].val.str_val.len; \
(_str).s=(char*)RES_ROWS(_res)[_r].values[_c].val.str_val.s; \
break; \
case DB_BLOB: \
(_str).len=RES_ROWS(_res)[_r].values[_c].val.blob_val.len; \
(_str).s=(char*)RES_ROWS(_res)[_r].values[_c].val.blob_val.s; \
break; \
default: \
(_str).len=0; \
(_str).s=NULL; \
} \
}
#define S_TABLE_VERSION 6
/** database connection */
static db_con_t *db_con = NULL;
static db_func_t msilo_dbf;
/** precessed msg list - used for dumping the messages */
msg_list ml = NULL;
retry_list rl = NULL;
/** TM bind */
struct tm_binds tmb;
/** parameters */
static str ms_db_url = {NULL, 0};
static str ms_db_table = str_init("silo");
str ms_reminder = {NULL, 0};
str ms_outbound_proxy = {NULL, 0};
char* ms_from = NULL; /*"sip:[email protected]";*/
char* ms_contact = NULL; /*"Contact: <sip:[email protected]>\r\n";*/
char* ms_content_type = NULL; /*"Content-Type: text/plain\r\n";*/
char* ms_offline_message = NULL; /*"<em>I'm offline.</em>"*/
void** ms_from_sp = NULL;
void** ms_contact_sp = NULL;
void** ms_content_type_sp = NULL;
void** ms_offline_message_sp = NULL;
long ms_expire_time = 259200;
long ms_check_time = 60;
int ms_retry_count = 2;
int ms_delay_sec = 5;
long ms_send_time = 0;
int ms_clean_period = 10;
int ms_use_contact = 1;
int ms_add_date = 1;
int ms_max_messages = 0;
// AMQP related
char* ms_amqp_host = "localhost";
char* ms_amqp_vhost = "/";
char* ms_amqp_user = "guest";
char* ms_amqp_pass = "guest";
char* ms_amqp_queue = NULL;
int ms_amqp_port = 5672;
int ms_amqp_enabled = 0;
static str ms_snd_time_avp_param = {NULL, 0};
int ms_snd_time_avp_name = -1;
unsigned short ms_snd_time_avp_type;
str msg_type = str_init("MESSAGE");
/** module functions */
static int mod_init(void);
static int child_init(int);
static int m_store(struct sip_msg*, char*, char*);
static int m_dump(struct sip_msg*, char*, char*);
void destroy(void);
void m_clean_silo(unsigned int ticks, void *);
void m_send_ontimer(unsigned int ticks, void *);
int ms_reset_stime(t_msg_mid mid);
int check_message_support(struct sip_msg* msg);
/** TM callback function */
static void m_tm_callback( struct cell *t, int type, struct tmcb_params *ps);
// --------------------------------------------------------
/** Sender thread */
#define SENDER_THREAD_NUM 1
#define SENDER_THREAD_WAIT_MS 100
str msg_hdr_type = str_init("X-MsgType");
static volatile int sender_threads_running;
static int sender_thread_waiters;
pthread_mutex_t * p_sender_thread_queue_cond_mutex = NULL;
pthread_mutexattr_t * p_sender_thread_queue_cond_mutex_attr = NULL;
pthread_cond_t * p_sender_thread_queue_cond = NULL;
pthread_condattr_t * p_sender_thread_queue_cond_attr = NULL;
typedef struct t_senderThreadArg_ {
int thread_id;
int rank;
} t_senderThreadArg, *t_sender_thread_arg;
t_senderThreadArg sender_threads_args[SENDER_THREAD_NUM];
pthread_t sender_threads[SENDER_THREAD_NUM];
static int init_sender_worker_env(void);
static int destroy_sender_worker_env(void);
static int init_child_sender_threads(void);
static int spawn_sender_threads(void);
static int terminate_sender_threads(void);
static void *sender_thread_main(void *varg);
static void signal_new_task(void);
// https://voipmagazine.wordpress.com/tag/extra-process/
int* sender_pid;
int pid = 0;
static int msg_process_prefork(void);
static int msg_process_postfork(void);
static void msg_process(int rank);
static int build_sql_query(char *sql_query, str *sql_str, t_msg_mid *mids_to_load, size_t mids_to_load_size);
static unsigned long wait_not_before(time_t not_before);
static int send_messages(retry_list_el list);
static int msg_set_flags_all_list_prev(retry_list_el list, int flag);
static int msg_set_flags_all(t_msg_mid *mids, size_t mids_size, int flag);
static void timespec_add_milli(struct timespec * time_to_change, struct timeval * now, long long milli_seconds);
#ifdef MS_AMQP
#define AMQP_BUFF 2048
/** RabbitMQ */
t_msilo_amqp ms_amqp;
int amqp_cfg_ok = 0;
#endif
static proc_export_t procs[] = {
// name, pre-fork, post-fork, function, number, flags
{"MSG sender", msg_process_prefork, msg_process_postfork, msg_process, 1, PROC_FLAG_INITCHILD},
{0,0,0,0,0,0}
};
// --------------------------------------------------------
static cmd_export_t cmds[]={
{"m_store", (cmd_function)m_store, 0, 0, 0,
REQUEST_ROUTE | FAILURE_ROUTE},
{"m_store", (cmd_function)m_store, 1, fixup_spve_null, 0,
REQUEST_ROUTE | FAILURE_ROUTE},
{"m_dump", (cmd_function)m_dump, 0, 0, 0,
REQUEST_ROUTE},
{"m_dump", (cmd_function)m_dump, 1, fixup_spve_null, 0,
REQUEST_ROUTE},
{0,0,0,0,0,0}
};
static param_export_t params[]={
{ "db_url", STR_PARAM, &ms_db_url.s },
{ "db_table", STR_PARAM, &ms_db_table.s },
{ "from_address", STR_PARAM, &ms_from },
{ "contact_hdr", STR_PARAM, &ms_contact },
{ "content_type_hdr", STR_PARAM, &ms_content_type },
{ "offline_message", STR_PARAM, &ms_offline_message },
{ "reminder", STR_PARAM, &ms_reminder.s },
{ "outbound_proxy", STR_PARAM, &ms_outbound_proxy.s },
{ "expire_time", INT_PARAM, &ms_expire_time },
{ "check_time", INT_PARAM, &ms_check_time },
{ "retry_count", INT_PARAM, &ms_retry_count },
{ "delay_sec", INT_PARAM, &ms_delay_sec },
{ "send_time", INT_PARAM, &ms_send_time },
{ "clean_period", INT_PARAM, &ms_clean_period },
{ "use_contact", INT_PARAM, &ms_use_contact },
{ "sc_mid", STR_PARAM, &sc_mid.s },
{ "sc_from", STR_PARAM, &sc_from.s },
{ "sc_to", STR_PARAM, &sc_to.s },
{ "sc_uri_user", STR_PARAM, &sc_uri_user.s },
{ "sc_uri_host", STR_PARAM, &sc_uri_host.s },
{ "sc_body", STR_PARAM, &sc_body.s },
{ "sc_ctype", STR_PARAM, &sc_ctype.s },
{ "sc_exp_time", STR_PARAM, &sc_exp_time.s },
{ "sc_inc_time", STR_PARAM, &sc_inc_time.s },
{ "sc_snd_time", STR_PARAM, &sc_snd_time.s },
{ "sc_msg_type", STR_PARAM, &sc_msg_type.s },
{ "snd_time_avp", STR_PARAM, &ms_snd_time_avp_param.s },
{ "add_date", INT_PARAM, &ms_add_date },
{ "max_messages", INT_PARAM, &ms_max_messages },
{ "amqp_host", STR_PARAM, &ms_amqp_host },
{ "amqp_vhost", STR_PARAM, &ms_amqp_vhost },
{ "amqp_user", STR_PARAM, &ms_amqp_user },
{ "amqp_pass", STR_PARAM, &ms_amqp_pass },
{ "amqp_queue", STR_PARAM, &ms_amqp_queue },
{ "amqp_port", INT_PARAM, &ms_amqp_port },
{ "amqp_enabled", INT_PARAM, &ms_amqp_enabled },
{ 0,0,0 }
};
#ifdef STATISTICS
#include "../../statistics.h"
#include "../../db/db_val.h"
stat_var* ms_stored_msgs;
stat_var* ms_dumped_msgs;
stat_var* ms_failed_msgs;
stat_var* ms_dumped_rmds;
stat_var* ms_failed_rmds;
static stat_export_t msilo_stats[] = {
{"stored_messages" , 0, &ms_stored_msgs },
{"dumped_messages" , 0, &ms_dumped_msgs },
{"failed_messages" , 0, &ms_failed_msgs },
{"dumped_reminders" , 0, &ms_dumped_rmds },
{"failed_reminders" , 0, &ms_failed_rmds },
{0,0,0}
};
#endif
/** module exports */
struct module_exports exports= {
"msilo", /* module id */
MODULE_VERSION,
DEFAULT_DLFLAGS, /* dlopen flags */
cmds, /* module's exported functions */
params, /* module's exported parameters */
#ifdef STATISTICS
msilo_stats,
#else
0, /* exported statistics */
#endif
0, /* exported MI functions */
0, /* exported pseudo-variables */
procs, /* extra processes */
mod_init, /* module initialization function */
(response_function) 0, /* response handler */
(destroy_function) destroy, /* module destroy function */
child_init /* per-child init function */
};
/**
* init module function
*/
static int mod_init(void)
{
pv_spec_t avp_spec;
init_db_url( ms_db_url , 0 /*cannot be null*/);
ms_db_table.len = strlen (ms_db_table.s);
sc_mid.len = strlen(sc_mid.s);
sc_from.len = strlen(sc_from.s);
sc_to.len = strlen(sc_to.s);
sc_uri_user.len = strlen(sc_uri_user.s);
sc_uri_host.len = strlen(sc_uri_host.s);
sc_body.len = strlen(sc_body.s);
sc_ctype.len = strlen(sc_ctype.s);
sc_exp_time.len = strlen(sc_exp_time.s);
sc_inc_time.len = strlen(sc_inc_time.s);
sc_snd_time.len = strlen(sc_snd_time.s);
sc_msg_type.len = strlen(sc_msg_type.s);
if (ms_snd_time_avp_param.s)
ms_snd_time_avp_param.len = strlen(ms_snd_time_avp_param.s);
LM_DBG("initializing ...\n");
/* binding to mysql module */
if (db_bind_mod(&ms_db_url, &msilo_dbf))
{
LM_INFO("database module not found\n");
return -1;
}
if (!DB_CAPABILITY(msilo_dbf, DB_CAP_ALL)) {
LM_ERR("database module does not implement "
"all functions needed by the module\n");
return -1;
}
if (ms_snd_time_avp_param.s && ms_snd_time_avp_param.len > 0) {
if (pv_parse_spec(&ms_snd_time_avp_param, &avp_spec)==0
|| avp_spec.type!=PVT_AVP) {
LM_ERR("malformed or non AVP %.*s AVP definition\n",
ms_snd_time_avp_param.len, ms_snd_time_avp_param.s);
return -1;
}
if(pv_get_avp_name(0, &(avp_spec.pvp), &ms_snd_time_avp_name,
&ms_snd_time_avp_type)!=0)
{
LM_ERR("[%.*s]- invalid AVP definition\n",
ms_snd_time_avp_param.len, ms_snd_time_avp_param.s);
return -1;
}
}
db_con = msilo_dbf.init(&ms_db_url);
if (!db_con)
{
LM_ERR("failed to connect to the database\n");
return -1;
}
if(db_check_table_version(&msilo_dbf, db_con, &ms_db_table, S_TABLE_VERSION) < 0) {
LM_ERR("error during table version check.\n");
return -1;
}
if(db_con)
msilo_dbf.close(db_con);
db_con = NULL;
/* load the TM API */
if (load_tm_api(&tmb)!=0) {
LM_ERR("can't load TM API\n");
return -1;
}
if(ms_from!=NULL)
{
ms_from_sp = (void**)pkg_malloc(sizeof(void*));
if(ms_from_sp==NULL)
{
LM_ERR("no more pkg\n");
return -1;
}
*ms_from_sp = (void*)ms_from;
if(fixup_spve_null(ms_from_sp, 1)!=0)
{
LM_ERR("bad contact parameter\n");
return -1;
}
}
if(ms_contact!=NULL)
{
ms_contact_sp = (void**)pkg_malloc(sizeof(void*));
if(ms_contact_sp==NULL)
{
LM_ERR("no more pkg\n");
return -1;
}
*ms_contact_sp = (void*)ms_contact;
if(fixup_spve_null(ms_contact_sp, 1)!=0)
{
LM_ERR("bad contact parameter\n");
return -1;
}
}
if(ms_content_type!=NULL)
{
ms_content_type_sp = (void**)pkg_malloc(sizeof(void*));
if(ms_content_type_sp==NULL)
{
LM_ERR("no more pkg\n");
return -1;
}
*ms_content_type_sp = (void*)ms_content_type;
if(fixup_spve_null(ms_content_type_sp, 1)!=0)
{
LM_ERR("bad content_type parameter\n");
return -1;
}
}
if(ms_offline_message!=NULL)
{
ms_offline_message_sp = (void**)pkg_malloc(sizeof(void*));
if(ms_offline_message_sp==NULL)
{
LM_ERR("no more pkg\n");
return -1;
}
*ms_offline_message_sp = (void*)ms_offline_message;
if(fixup_spve_null(ms_offline_message_sp, 1)!=0)
{
LM_ERR("bad offline_message parameter\n");
return -1;
}
}
if(ms_offline_message!=NULL && ms_content_type==NULL)
{
LM_ERR("content_type parameter must be set\n");
return -1;
}
ml = msg_list_init();
if(ml==NULL)
{
LM_ERR("can't initialize msg list\n");
return -1;
}
rl = retry_list_init();
if(rl==NULL)
{
LM_ERR("can't initialize retry list\n");
return -1;
}
if(ms_check_time<0)
{
LM_ERR("bad check time value\n");
return -1;
}
register_timer( "msilo-clean", m_clean_silo, 0, ms_check_time);
if(ms_send_time>0 && ms_reminder.s!=NULL)
register_timer( "msilo-reminder", m_send_ontimer, 0, ms_send_time);
if(ms_reminder.s!=NULL)
ms_reminder.len = strlen(ms_reminder.s);
if(ms_outbound_proxy.s!=NULL)
ms_outbound_proxy.len = strlen(ms_outbound_proxy.s);
if (ms_amqp_enabled)
{
#ifdef MS_AMQP
if (ms_amqp_user == NULL || ms_amqp_pass == NULL || ms_amqp_queue == NULL)
{
LM_ERR("AMQP configuration is invalid, disabling AMQP");
amqp_cfg_ok = 0;
ms_amqp_enabled = 0;
}
else
{
amqp_cfg_ok = 1;
}
#else
LM_CRIT("Your configuration enables AMQP integration while module was compiled without AMQP support.");
amqp_cfg_ok = 0;
ms_amqp_enabled = 0;
#endif
}
// Sender thread startup.
return init_sender_worker_env() == 0 ? 0 : -1;
}
/**
* Initialize children
*/
static int child_init(int rank)
{
LM_DBG("rank #%d / pid <%d>\n", rank, getpid());
if (msilo_dbf.init==0)
{
LM_CRIT("database not bound\n");
return -1;
}
db_con = msilo_dbf.init(&ms_db_url);
if (!db_con)
{
LM_ERR("child %d: failed to connect database\n", rank);
return -1;
}
else
{
if (msilo_dbf.use_table(db_con, &ms_db_table) < 0) {
LM_ERR("child %d: failed in use_table\n", rank);
return -1;
}
LM_DBG("#%d database connection opened successfully\n", rank);
}
#ifdef MS_AMQP
if (amqp_cfg_ok && ms_amqp_enabled)
{
int amqp_init = msilo_amqp_init(&ms_amqp,
ms_amqp_host,
ms_amqp_port,
ms_amqp_vhost,
ms_amqp_user,
ms_amqp_pass);
if (amqp_init != 0)
{
LM_CRIT("AMQP initialization failed: %d\n", amqp_init);
ms_amqp_enabled = 0;
}
else
{
LM_INFO("AMQP initialized successfully: %s:%d vhost:%s queue:%s",
ms_amqp_host, ms_amqp_port, ms_amqp_vhost, ms_amqp_queue);
}
}
#endif
return 0;
// Sender threads.
//return init_child_sender_threads() == 0 ? 0 : -1;
}
/**
* store message
* mode = "0" -- look for outgoing URI starting with new_uri
* = "1" -- look for outgoing URI starting with r-uri
* = "2" -- look for outgoing URI only at to header
*/
static int m_store(struct sip_msg* msg, char* owner, char* s2)
{
str body, str_hdr, ctaddr;
struct to_body *pto, *pfrom;
struct sip_uri puri;
str duri, owner_s;
db_key_t db_keys[NR_KEYS-1];
db_val_t db_vals[NR_KEYS-1];
db_key_t db_cols[1];
db_res_t* res = NULL;
int nr_keys = 0;
long val;
long lexpire=0;
content_type_t ctype;
#define MS_BUF1_SIZE MSG_BODY_BUFF_LEN
#define MS_MSG_TYPE_SIZE 64
static char ms_buf1[MS_BUF1_SIZE];
static char ms_msg_type[MS_MSG_TYPE_SIZE];
int mime;
str notify_from;
str notify_body;
str notify_ctype;
str notify_contact;
str msg_type_value = {NULL, 0};
long msg_time = 0;
int_str avp_value;
struct usr_avp *avp;
LM_DBG("------------ start ------------\n");
/* get message body - after that whole SIP MESSAGE is parsed */
if ( get_body( msg, &body)!=0 || body.len==0)
{
LM_ERR("cannot extract body from msg\n");
goto error;
}
/* get TO URI */
if(!msg->to || !msg->to->body.s)
{
LM_ERR("cannot find 'to' header!\n");
goto error;
}
pto = get_to(msg);
if (pto == NULL || pto->error != PARSE_OK) {
LM_ERR("failed to parse TO header\n");
goto error;
}
/* get the owner */
memset(&puri, 0, sizeof(struct sip_uri));
if(owner)
{
if(fixup_get_svalue(msg, (gparam_p)owner, &owner_s)!=0)
{
LM_ERR("invalid owner uri parameter");
return -1;
}
if(parse_uri(owner_s.s, owner_s.len, &puri)!=0)
{
LM_ERR("bad owner SIP address!\n");
goto error;
} else {
LM_DBG("using user id [%.*s]\n", owner_s.len, owner_s.s);
}
} else { /* get it from R-URI */
if(msg->new_uri.len <= 0)
{
if(msg->first_line.u.request.uri.len <= 0)
{
LM_ERR("bad dst URI!\n");
goto error;
}
duri = msg->first_line.u.request.uri;
} else {
duri = msg->new_uri;
}
LM_DBG("NEW R-URI found - check if is AoR!\n");
if(parse_uri(duri.s, duri.len, &puri)!=0)
{
LM_ERR("bad dst R-URI!!\n");
goto error;
}
}
if(puri.user.len<=0)
{
LM_ERR("no username for owner\n");
goto error;
}
db_keys[nr_keys] = &sc_uri_user;
db_vals[nr_keys].type = DB_STR;
db_vals[nr_keys].nul = 0;
db_vals[nr_keys].val.str_val.s = puri.user.s;
db_vals[nr_keys].val.str_val.len = puri.user.len;
nr_keys++;
db_keys[nr_keys] = &sc_uri_host;
db_vals[nr_keys].type = DB_STR;
db_vals[nr_keys].nul = 0;
db_vals[nr_keys].val.str_val.s = puri.host.s;
db_vals[nr_keys].val.str_val.len = puri.host.len;
nr_keys++;
if (msilo_dbf.use_table(db_con, &ms_db_table) < 0)
{
LM_ERR("failed to use_table\n");
goto error;
}
if (ms_max_messages > 0) {
db_cols[0] = &sc_inc_time;
if (msilo_dbf.query(db_con, db_keys, 0, db_vals, db_cols,
2, 1, 0, &res) < 0 ) {
LM_ERR("failed to query the database\n");
return -1;
}
if (RES_ROW_N(res) >= ms_max_messages) {
LM_ERR("too many messages for AoR '%.*s@%.*s'\n",
puri.user.len, puri.user.s, puri.host.len, puri.host.s);
msilo_dbf.free_result(db_con, res);
return -1;
}
msilo_dbf.free_result(db_con, res);
}
/* Set To key */
db_keys[nr_keys] = &sc_to;
db_vals[nr_keys].type = DB_STR;
db_vals[nr_keys].nul = 0;
db_vals[nr_keys].val.str_val.s = pto->uri.s;
db_vals[nr_keys].val.str_val.len = pto->uri.len;
nr_keys++;
/* check FROM URI */
if(!msg->from || !msg->from->body.s)
{
LM_ERR("cannot find 'from' header!\n");
goto error;
}
if(msg->from->parsed == NULL)
{
LM_DBG("'From' header not parsed\n");
/* parsing from header */
if ( parse_from_header( msg )<0 )
{
LM_ERR("cannot parse From header\n");
goto error;
}
}
pfrom = (struct to_body*)msg->from->parsed;
LM_DBG("'From' header: <%.*s>\n", pfrom->uri.len, pfrom->uri.s);
db_keys[nr_keys] = &sc_from;
db_vals[nr_keys].type = DB_STR;
db_vals[nr_keys].nul = 0;
db_vals[nr_keys].val.str_val.s = pfrom->uri.s;
db_vals[nr_keys].val.str_val.len = pfrom->uri.len;
nr_keys++;
/* add the message's body in SQL query */
db_keys[nr_keys] = &sc_body;
db_vals[nr_keys].type = DB_BLOB;
db_vals[nr_keys].nul = 0;
db_vals[nr_keys].val.blob_val.s = body.s;
db_vals[nr_keys].val.blob_val.len = body.len;
nr_keys++;
lexpire = ms_expire_time;
/* add 'content-type' -- parse the content-type header */
if ((mime=parse_content_type_hdr(msg))<1 )
{
LM_ERR("cannot parse Content-Type header\n");
goto error;
}
db_keys[nr_keys] = &sc_ctype;
db_vals[nr_keys].type = DB_STR;
db_vals[nr_keys].nul = 0;
db_vals[nr_keys].val.str_val.s = "text/plain";
db_vals[nr_keys].val.str_val.len = 10;
/** check the content-type value */
if( mime!=(TYPE_TEXT<<16)+SUBTYPE_PLAIN
&& mime!=(TYPE_MESSAGE<<16)+SUBTYPE_CPIM )
{
if(m_extract_content_type(msg->content_type->body.s,
msg->content_type->body.len, &ctype, CT_TYPE) != -1)
{
LM_DBG("'content-type' found\n");
db_vals[nr_keys].val.str_val.s = ctype.type.s;
db_vals[nr_keys].val.str_val.len = ctype.type.len;
}
}
nr_keys++;
/* check 'expires' -- no more parsing - already done by get_body() */
if(msg->expires && msg->expires->body.len > 0)
{
LM_DBG("'expires' found\n");
val = atoi(msg->expires->body.s);
if(val > 0)
lexpire = (ms_expire_time<=val)?ms_expire_time:val;
}
/* current time */
val = (long)time(NULL);
/* add expiration time */
db_keys[nr_keys] = &sc_exp_time;
db_vals[nr_keys].type = DB_BIGINT;
db_vals[nr_keys].nul = 0;
db_vals[nr_keys].val.bigint_val = (long long)val+lexpire;
nr_keys++;
/* add incoming time */
db_keys[nr_keys] = &sc_inc_time;
db_vals[nr_keys].type = DB_BIGINT;
db_vals[nr_keys].nul = 0;
db_vals[nr_keys].val.bigint_val = (long long)val;
msg_time = val;
nr_keys++;
/* add sending time */
db_keys[nr_keys] = &sc_snd_time;
db_vals[nr_keys].type = DB_BIGINT;
db_vals[nr_keys].nul = 0;
db_vals[nr_keys].val.bigint_val = 0ll;
if(ms_snd_time_avp_name >= 0)
{
avp = NULL;
avp=search_first_avp(ms_snd_time_avp_type, ms_snd_time_avp_name,
&avp_value, 0);
if(avp!=NULL && is_avp_str_val(avp))
{
if(ms_extract_time(&avp_value.s, &db_vals[nr_keys].val.bigint_val)!=0)
db_vals[nr_keys].val.bigint_val = 0;
}
}
nr_keys++;
// MSG-type parsing.
if (msg->headers != NULL)
{
struct hdr_field * p0 = msg->headers;
for(; p0 != NULL; p0 = p0->next)
{
if (p0->type != HDR_OTHER_T && p0->type != HDR_EOH_T)
{
continue;
}
if (p0->name.len < msg_hdr_type.len && strncmp(p0->name.s, msg_hdr_type.s, (size_t)msg_hdr_type.len) != 0)
{
continue;
}
int len_to_copy = MS_MSG_TYPE_SIZE <= p0->body.len ? MS_MSG_TYPE_SIZE-1 : p0->body.len;
strncpy(ms_msg_type, p0->body.s, len_to_copy);
msg_type_value.s = ms_msg_type;
msg_type_value.len = len_to_copy;
break;
}
}
else
{
LM_INFO("Headers not parser for message\n");
}
db_keys[nr_keys] = &sc_msg_type;
db_vals[nr_keys].type = DB_STR;
db_vals[nr_keys].nul = msg_type_value.len <= 0;
db_vals[nr_keys].val.str_val.s = msg_type_value.s;
db_vals[nr_keys].val.str_val.len = msg_type_value.len;
nr_keys++;
if(msilo_dbf.insert(db_con, db_keys, db_vals, nr_keys) < 0)
{
LM_ERR("failed to store message\n");
goto error;
}
LM_INFO("message stored. T:<%.*s> F:<%.*s>\n",
pto->uri.len, pto->uri.s, pfrom->uri.len, pfrom->uri.s);
#ifdef MS_AMQP
// Send AMQP event
if (ms_amqp_enabled && msilo_amqp_started(&ms_amqp))
{
char amqp_buff[AMQP_BUFF];
amqp_buff[AMQP_BUFF-1] = 0;
size_t amqp_size = 0;
snprintf(amqp_buff, AMQP_BUFF,
"{\"job\":\"offlineMessage\", \"data\":{\"from\":\"%.*s\",\"to\":\"%.*s\","
"\"timestampSeconds\":%ld,\"msgType\":\"%.*s\"}}",
pfrom->uri.len, pfrom->uri.s,
pto->uri.len, pto->uri.s,
msg_time,
msg_type_value.len, msg_type_value.s
);
amqp_size = strlen(amqp_buff);
LM_INFO("Sending AMQP message: %s to queue %s\n", amqp_buff, ms_amqp_queue);
msilo_amqp_send(&ms_amqp, ms_amqp_queue, amqp_buff, amqp_size);
}
#endif
#ifdef STATISTICS
update_stat(ms_stored_msgs, 1);
#endif
if(ms_from==NULL || ms_offline_message == NULL)
goto done;
LM_DBG("sending info message.\n");
if(fixup_get_svalue(msg, (gparam_p)*ms_from_sp, ¬ify_from)!=0
|| notify_from.len<=0)
{
LM_WARN("cannot get notification From address\n");
goto done;
}
if(fixup_get_svalue(msg, (gparam_p)*ms_offline_message_sp, ¬ify_body)!=0
|| notify_body.len<=0)
{
LM_WARN("cannot get notification body\n");
goto done;
}
if(fixup_get_svalue(msg, (gparam_p)*ms_content_type_sp, ¬ify_ctype)!=0
|| notify_ctype.len<=0)
{
LM_WARN("cannot get notification content type\n");
goto done;
}
if(ms_contact!=NULL && fixup_get_svalue(msg, (gparam_p)*ms_contact_sp,
¬ify_contact)==0 && notify_contact.len>0)
{
if(notify_contact.len+notify_ctype.len>=MS_BUF1_SIZE)
{
LM_WARN("insufficient buffer to build notification headers\n");
goto done;
}
memcpy(ms_buf1, notify_contact.s, notify_contact.len);
memcpy(ms_buf1+notify_contact.len, notify_ctype.s, notify_ctype.len);
str_hdr.s = ms_buf1;
str_hdr.len = notify_contact.len + notify_ctype.len;
} else {
str_hdr = notify_ctype;
}
/* look for Contact header -- must be parsed by now*/
ctaddr.s = NULL;
if(ms_use_contact && msg->contact!=NULL && msg->contact->body.s!=NULL
&& msg->contact->body.len > 0)
{
LM_DBG("contact header found\n");
if((msg->contact->parsed!=NULL
&& ((contact_body_t*)(msg->contact->parsed))->contacts!=NULL)
|| (parse_contact(msg->contact)==0
&& msg->contact->parsed!=NULL
&& ((contact_body_t*)(msg->contact->parsed))->contacts!=NULL))
{
LM_DBG("using contact header for info msg\n");
ctaddr.s =
((contact_body_t*)(msg->contact->parsed))->contacts->uri.s;
ctaddr.len =
((contact_body_t*)(msg->contact->parsed))->contacts->uri.len;
if(!ctaddr.s || ctaddr.len < 6 || strncasecmp(ctaddr.s, "sip:", 4)
|| ctaddr.s[4]==' ')
ctaddr.s = NULL;
else
LM_DBG("feedback contact [%.*s]\n", ctaddr.len,ctaddr.s);
}
}
tmb.t_request(&msg_type, /* Type of the message */
(ctaddr.s)?&ctaddr:&pfrom->uri, /* Request-URI */
&pfrom->uri, /* To */
¬ify_from, /* From */
&str_hdr, /* Optional headers including CRLF */
¬ify_body, /* Message body */
(ms_outbound_proxy.s)?&ms_outbound_proxy:0, /* outbound uri */