-
Notifications
You must be signed in to change notification settings - Fork 0
/
db_kazoo-blf-reg.patch
4177 lines (4075 loc) · 133 KB
/
db_kazoo-blf-reg.patch
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
From e1e3f0cc1304968fe96ee0a9ef313ce38b51f6f1 Mon Sep 17 00:00:00 2001
From: karl anderson <[email protected]>
Date: Fri, 4 Oct 2013 15:03:40 -0400
Subject: [PATCH] current patch
tweak reconnect
---
modules/db_kazoo/Makefile | 32 +
modules/db_kazoo/blf.c | 1574 ++++++++++++++++++++
modules/db_kazoo/blf.h | 25 +
modules/db_kazoo/db_kazoo.c | 192 +++
modules/db_kazoo/dbase.c | 941 ++++++++++++
modules/db_kazoo/dbase.h | 100 ++
modules/dispatcher/dispatch.c | 8 +-
modules/dispatcher/dispatch.h | 1 +
modules/dispatcher/dispatcher.c | 10 +
modules/presence/bind_presence.c | 2 +
modules/presence/bind_presence.h | 3 +
modules/presence/hash.c | 4 +-
modules/presence/notify.c | 94 ++
modules/presence/notify.h | 3 +
modules/presence/presence.c | 20 +-
modules/presence/presence.h | 1 +
modules/presence/subscribe.c | 17 +-
modules/presence/subscribe.h | 4 +-
modules/presence_dialoginfo/bind_pres_dialoginfo.h | 28 +
modules/presence_dialoginfo/notify_body.c | 63 +-
modules/presence_dialoginfo/notify_body.h | 7 +
modules/presence_dialoginfo/presence_dialoginfo.c | 16 +
modules/pua/pua.c | 4 +-
modules/pua_dialoginfo/Makefile | 2 +
modules/pua_dialoginfo/bind_dialoginfo.h | 29 +
modules/pua_dialoginfo/dialog_publish.c | 104 ++-
modules/pua_dialoginfo/pua_dialoginfo.c | 78 +-
modules/pua_dialoginfo/pua_dialoginfo.h | 7 +
modules/rls/rls.c | 2 +
modules/rls/rls.h | 1 +
modules/rls/subscribe.c | 7 +-
modules/usrloc/dlist.c | 3 +-
modules/usrloc/ul_mod.c | 5 +-
33 files changed, 3317 insertions(+), 70 deletions(-)
create mode 100644 modules/db_kazoo/Makefile
create mode 100644 modules/db_kazoo/blf.c
create mode 100644 modules/db_kazoo/blf.h
create mode 100644 modules/db_kazoo/db_kazoo.c
create mode 100644 modules/db_kazoo/dbase.c
create mode 100644 modules/db_kazoo/dbase.h
create mode 100644 modules/presence_dialoginfo/bind_pres_dialoginfo.h
create mode 100644 modules/pua_dialoginfo/bind_dialoginfo.h
diff --git a/modules/db_kazoo/Makefile b/modules/db_kazoo/Makefile
new file mode 100644
index 0000000..668d68c
--- /dev/null
+++ b/modules/db_kazoo/Makefile
@@ -0,0 +1,32 @@
+# $Id$
+#
+# WARNING: do not run this directly, it should be run by the master Makefile
+
+include ../../Makefile.defs
+auto_gen=
+NAME=db_kazoo.so
+
+DEFS+=-I/usr/local/include -DKAMAILIO_MOD_INTERFACE
+LIBS=-L/usr/local/lib -lrabbitmq -ljson
+
+DEFS += -DSER_MOD_INTERFACE
+
+ifeq ($(CROSS_COMPILE),)
+XML2CFG=$(shell which xml2-config)
+endif
+
+ifneq ($(XML2CFG),)
+ DEFS += $(shell $(XML2CFG) --cflags )
+ LIBS += $(shell $(XML2CFG) --libs)
+else
+ DEFS+=-I$(LOCALBASE)/include/libxml2 \
+ -I$(LOCALBASE)/include
+ LIBS+=-L$(LOCALBASE)/lib -lxml2
+endif
+
+
+SERLIBPATH=../../lib
+SER_LIBS=$(SERLIBPATH)/srdb2/srdb2 $(SERLIBPATH)/srdb1/srdb1
+SER_LIBS+=$(SERLIBPATH)/kmi/kmi
+
+include ../../Makefile.modules
diff --git a/modules/db_kazoo/blf.c b/modules/db_kazoo/blf.c
new file mode 100644
index 0000000..8eba2f9
--- /dev/null
+++ b/modules/db_kazoo/blf.c
@@ -0,0 +1,1574 @@
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <amqp.h>
+#include <amqp_framing.h>
+#include <amqp_tcp_socket.h>
+#include <json/json.h>
+#include <libxml/parser.h>
+#include "../../parser/parse_to.h"
+#include "../../dprint.h"
+#include "../../mem/mem.h"
+#include "../../timer_proc.h"
+#include "../../sr_module.h"
+#include "../../lib/kmi/mi.h"
+#include "../presence/bind_presence.h"
+#include "../pua_dialoginfo/bind_dialoginfo.h"
+#include "../presence_dialoginfo/bind_pres_dialoginfo.h"
+#include "../../pvar.h"
+#include "blf.h"
+#include "dbase.h"
+
+static presence_api_t presence_api;
+static pua_dialoginfo_api_t pua_dialoginfo_api;
+static pres_dialoginfo_api_t pres_dialoginfo_api;
+
+int rmqp_pres_update_handle(char* req);
+void start_presence_timer_processes(void);
+void start_presence_rmqp_consumer_processes(struct db_id* id);
+void rmqp_consumer_loop(struct db_id* id);
+int dbk_initialize_pres_htable(void);
+
+#define BLF_MAX_DIALOGS 8
+#define BLF_JSON_FROM "From"
+#define BLF_JSON_TO "To"
+#define BLF_JSON_CALLID "Call-ID"
+#define BLF_JSON_TOTAG "To-Tag"
+#define BLF_JSON_FROMTAG "From-Tag"
+#define BLF_JSON_STATE "State"
+#define BLF_JSON_USER "User"
+#define BLF_JSON_FROM "From"
+#define BLF_JSON_QUEUE "Queue"
+#define BLF_JSON_EXPIRES "Expires"
+#define BLF_JSON_APP_NAME "App-Name"
+#define BLF_JSON_APP_VERSION "App-Version"
+#define BLF_JSON_NODE "Node"
+#define BLF_JSON_SERVERID "Server-ID"
+#define BLF_JSON_EVENT_CATEGORY "Event-Category"
+#define BLF_JSON_EVENT_NAME "Event-Name"
+#define BLF_JSON_TYPE "Type"
+#define BLF_JSON_MSG_ID "Msg-ID"
+#define BLF_JSON_DIRECTION "Direction"
+
+
+static char blf_queue_name_buffer[128];
+static amqp_bytes_t blf_queue;
+
+str sht_name = str_init("$sht(dbkp=>$ci)");
+pv_spec_t sht_spec;
+char node_name[128];
+
+int dbk_initialize_presence()
+{
+ str unique_string;
+
+ LM_DBG("dbk_initialize_presence\n");
+ /* bind to presence module */
+ bind_presence_t bind_presence= (bind_presence_t)find_export("bind_presence", 1,0);
+ if (!bind_presence) {
+ LM_ERR("Can't find presence module\n");
+ return -1;
+ }
+ if (bind_presence(&presence_api) < 0) {
+ LM_ERR("Can't bind to presence module api\n");
+ return -1;
+ }
+ /* bind to pua_dialoginfo module */
+ bind_pua_dialoginfo_t bind_pua_dialoginfo= (bind_pua_dialoginfo_t)find_export("bind_pua_dialoginfo", 1,0);
+ if (!bind_pua_dialoginfo) {
+ LM_ERR("Can't find pua_dialoginfo module\n");
+ return -1;
+ }
+ if (bind_pua_dialoginfo(&pua_dialoginfo_api) < 0) {
+ LM_ERR("Can't bind to pua_dialoginfo module api\n");
+ return -1;
+ }
+
+ /* bind to presence_dialoginfo module */
+ bind_pres_dialoginfo_t bind_pres_dialoginfo= (bind_pres_dialoginfo_t)find_export("bind_pres_dialoginfo", 1,0);
+ if (!bind_pres_dialoginfo) {
+ LM_ERR("Can't find pres_dialoginfo module\n");
+ return -1;
+ }
+
+ if (bind_pres_dialoginfo(&pres_dialoginfo_api) < 0) {
+ LM_ERR("Can't bind to pres_dialoginfo module api\n");
+ return -1;
+ }
+
+ if ( dbk_initialize_pres_htable() < 0) {
+ LM_ERR("Failed to initialize presence htable\n");
+ return -1;
+ }
+ LM_DBG("Initialized pres_htable\n");
+
+ if ( pv_parse_spec(&sht_name, &sht_spec) == NULL ) {
+ LM_ERR("Failed to parse sht spec\n");
+ return -1;
+ }
+
+ tmb.generate_callid(&unique_string);
+
+ blf_queue.bytes = blf_queue_name_buffer;
+ blf_queue.len = sprintf(blf_queue.bytes, "BLF-%.*s-%.*s",
+ dbk_node_hostname.len, dbk_node_hostname.s, unique_string.len, unique_string.s);
+
+ sprintf(node_name, "kamailio@%.*s", dbk_node_hostname.len, dbk_node_hostname.s);
+ return 0;
+}
+
+
+void dbk_start_presence_rmqp_consumer_processes(struct db_id* id)
+{
+ int i;
+ for (i= 0; i< DBK_PRES_WORKERS_NO; i++) {
+ int newpid = fork_process(PROC_NOCHLDINIT, "RMQP PRESENCE WORKER", 0);
+ if(newpid < 0) {
+ LM_ERR("Failed to start AMQP presence worker\n");
+ return;
+ } else if(newpid == 0) {
+ // child - this will loop forever
+ LM_INFO("Created dbk AMQP presence worker %d\n", newpid);
+ rmqp_consumer_loop(id);
+ } else {
+ LM_INFO("Created dbk AMQP presence worker %d\n", newpid);
+ }
+ }
+}
+
+void rmqp_consumer_loop(struct db_id* id)
+{
+ amqp_frame_t frame;
+ int result;
+ amqp_basic_deliver_t *d;
+ amqp_basic_properties_t *p;
+ size_t body_target;
+ size_t body_received = 0;
+ char body[2048];
+ rmq_conn_t * rmq;
+ int reconn_retries = 0;
+
+ rmq = dbk_dummy_db_conn(id);
+ if (rmq == NULL) {
+ LM_ERR("Failed to create AMQP connection\n");
+ return;
+ }
+
+ while (1) {
+ if (!rmq->conn) {
+ reconn_retries = 0;
+ while (1) {
+ reconn_retries++;
+ LM_DBG("Attempt %d to connect to AMQP\n", reconn_retries);
+ sleep(1);
+
+ if (rmqp_open_connection(rmq) < 0) {
+ LM_DBG("Failed to open AMQP connection\n");
+ continue;
+ }
+
+ amqp_queue_declare(rmq->conn, rmq->channel, blf_queue, 0, 0, 0, 1, amqp_empty_table);
+ if (rmq_error("Declaring queue", amqp_get_rpc_reply(rmq->conn))) {
+ LM_DBG("Failed to declare AMQP presence queue\n");
+ continue;
+ }
+ LM_DBG("Create presence AMQP queue %.*s\n", (int) blf_queue.len, (char *) blf_queue.bytes);
+
+ amqp_exchange_declare(rmq->conn, rmq->channel, amqp_cstring_bytes("dialoginfo"), amqp_cstring_bytes("direct"),
+ 0, 0, amqp_empty_table);
+ if (rmq_error("Declaring exchange", amqp_get_rpc_reply(rmq->conn))) {
+ LM_ERR("Failed to declare AMQP dialoginfo exchange\n");
+ continue;
+ }
+
+ static amqp_bytes_t exch = {10, "dialoginfo"};
+ amqp_queue_bind(rmq->conn, rmq->channel, blf_queue, exch, blf_queue, amqp_empty_table);
+ if (rmq_error("Binding queue", amqp_get_rpc_reply(rmq->conn))) {
+ LM_DBG("Unable to bind presence AMQP queue\n");
+ continue;
+ }
+
+ amqp_basic_consume(rmq->conn, rmq->channel, blf_queue, amqp_empty_bytes, 0, 1, 0, amqp_empty_table);
+ if (rmq_error("Consuming", amqp_get_rpc_reply(rmq->conn))) {
+ LM_DBG("Failed to start consuming from queue\n");
+ continue;
+ }
+
+ break;
+ }
+
+ LM_DBG("Connected to AMQP after %d attempts\n", reconn_retries);
+
+ continue;
+ }
+
+ while (1) {
+ LM_DBG("Wait for a packet\n");
+ body_received = 0;
+ amqp_maybe_release_buffers(rmq->conn);
+ result = amqp_simple_wait_frame(rmq->conn, &frame);
+ if (result < 0 ) {
+ LM_ERR("Lost AMQP connection\n");
+ rmq_close(rmq);
+ break;
+ }
+
+ LM_DBG("Frame type: %d channel: %d\n", frame.frame_type, frame.channel);
+ if (frame.frame_type != AMQP_FRAME_METHOD) {
+ continue;
+ }
+
+ LM_DBG("Method: %s\n", amqp_method_name(frame.payload.method.id));
+ if (frame.payload.method.id != AMQP_BASIC_DELIVER_METHOD) {
+ continue;
+ }
+
+ d = (amqp_basic_deliver_t *) frame.payload.method.decoded;
+ LM_DBG("Delivery: %u exchange: %.*s routingkey: %.*s\n",
+ (unsigned) d->delivery_tag,
+ (int) d->exchange.len, (char *) d->exchange.bytes,
+ (int) d->routing_key.len, (char *) d->routing_key.bytes);
+
+ result = amqp_simple_wait_frame(rmq->conn, &frame);
+ if (result < 0) {
+ LM_ERR("Lost AMQP connection\n");
+ rmq_close(rmq);
+ break;
+ }
+
+ if (frame.frame_type != AMQP_FRAME_HEADER) {
+ LM_ERR("amqp: Expected header!");
+ break;
+ }
+
+ p = (amqp_basic_properties_t *) frame.payload.properties.decoded;
+ if (p->_flags & AMQP_BASIC_CONTENT_TYPE_FLAG) {
+ LM_DBG("Content-type: %.*s\n",
+ (int) p->content_type.len, (char *) p->content_type.bytes);
+ }
+
+ body_target = frame.payload.properties.body_size;
+
+ while (body_received < body_target) {
+ result = amqp_simple_wait_frame(rmq->conn, &frame);
+ if (result < 0) {
+ LM_ERR("Lost AMQP connection\n");
+ rmq_close(rmq);
+ body_received = 0;
+ break;
+ }
+
+ if (frame.frame_type != AMQP_FRAME_BODY) {
+ LM_ERR("amqp: Expected header!");
+ body_received = 0;
+ break;
+ }
+
+ memcpy(body + body_received, frame.payload.body_fragment.bytes,
+ frame.payload.body_fragment.len);
+
+ body_received += frame.payload.body_fragment.len;
+
+ LM_INFO("%.*s\n", (int)frame.payload.body_fragment.len,
+ (char*)frame.payload.body_fragment.bytes);
+ }
+
+ if (body_received != body_target) {
+ LM_CRIT("Unexpected body size: recv %zu, expected %zu\n",
+ body_received, body_target);
+ continue;
+ }
+
+ /* everything was fine, we can quit now because we received the reply */
+ break;
+ }
+
+ if (body_received) {
+ body[body_received] = '\0';
+ LM_DBG("Received update %s\n", body);
+
+ if (rmqp_pres_update_handle(body) < 0) {
+ LM_ERR("Failed to add to the update queue\n");
+ }
+ }
+ }
+
+ LM_ERR("Presence consumer loop terminated\n");
+ rmq_close(rmq);
+}
+
+typedef struct dbk_pres_dialog {
+ int version;
+ str callid;
+ str localtag;
+ str remotetag;
+ int body_size_alloc;
+ str body;
+ struct dbk_pres_dialog* next;
+} dbk_pres_dialog_t;
+
+typedef struct dbk_pres_user {
+ str user;
+ dbk_pres_dialog_t * pd;
+ struct dbk_pres_user* next;
+} dbk_pres_user_t;
+
+typedef struct {
+ gen_lock_t lock;
+ dbk_pres_user_t* pu;
+}dbk_pres_htable_t;
+
+static dbk_pres_htable_t* dbk_phtable = NULL;
+unsigned int dbk_phtable_size = 256;
+
+int dbk_initialize_pres_htable(void)
+{
+ int i;
+
+ dbk_phtable = (dbk_pres_htable_t*)shm_malloc(dbk_phtable_size*sizeof(dbk_pres_htable_t));
+ if ( dbk_phtable == NULL ) {
+ LM_ERR("No more shared memory\n");
+ return -1;
+ }
+ memset(dbk_phtable, 0, dbk_phtable_size*sizeof(dbk_pres_htable_t));
+
+ for ( i = 0; i < dbk_phtable_size; i++ ) {
+ lock_init(&dbk_phtable[i].lock);
+ }
+
+ return 0;
+}
+
+void dbk_free_pd(dbk_pres_dialog_t* pd)
+{
+ shm_free(pd->body.s);
+ shm_free(pd);
+}
+
+void dbk_free_pu(dbk_pres_user_t* pu)
+{
+ dbk_pres_dialog_t *pd_next;
+ dbk_pres_dialog_t *pd = pu->pd;
+ while ( pd ) {
+ pd_next = pd->next;
+ dbk_free_pd(pd);
+ pd = pd_next;
+ }
+ shm_free(pu);
+}
+
+void dbk_destroy_presence(void)
+{
+ dbk_pres_user_t *pu, *pu_next;
+ int i;
+
+ if ( dbk_phtable == NULL ) {
+ return;
+ }
+
+ for ( i = 0; i < dbk_phtable_size; i++ ) {
+ lock_destroy(&dbk_phtable[i].lock);
+ pu = dbk_phtable[i].pu;
+ while ( pu ) {
+ pu_next = pu->next;
+ dbk_free_pu(pu);
+ pu = pu_next;
+ }
+
+ }
+ shm_free(dbk_phtable);
+}
+
+dbk_pres_user_t* dbk_pres_search_pu(unsigned int hash_code, str* user,
+ dbk_pres_user_t** pu_prev_ret, dbk_pres_user_t *prev_pu_addr)
+{
+ dbk_pres_user_t *pu, *pu_prev = NULL;
+
+ for (pu = dbk_phtable[hash_code].pu; pu; pu = pu->next) {
+ if (pu->user.len == user->len &&
+ memcmp(pu->user.s, user->s, user->len)==0) {
+ break;
+ }
+ pu_prev = pu;
+ }
+ if (pu_prev_ret) {
+ *pu_prev_ret = pu_prev;
+ }
+
+ return pu;
+}
+
+dbk_pres_dialog_t* dbk_new_pres_dialog(str* callid, str* localtag, str* remotetag, str* body)
+{
+ dbk_pres_dialog_t* pd = (dbk_pres_dialog_t*)shm_malloc(sizeof(dbk_pres_dialog_t) + callid->len+
+ localtag->len + remotetag->len);
+
+ if ( pd == NULL) {
+ LM_ERR("No more shared memory\n");
+ return NULL;
+ }
+ memset(pd, 0, sizeof(dbk_pres_dialog_t));
+ char* p = (char*)pd + sizeof(dbk_pres_dialog_t);
+ pd->callid.s = p;
+ memcpy(pd->callid.s, callid->s, callid->len);
+ pd->callid.len = callid->len;
+ p += callid->len;
+
+ if (localtag->len) {
+ pd->localtag.s = p;
+ memcpy(pd->localtag.s, localtag->s, localtag->len);
+ pd->localtag.len = localtag->len;
+ p += localtag->len;
+ }
+ if (remotetag->len) {
+ pd->remotetag.s = p;
+ memcpy(pd->remotetag.s, remotetag->s, remotetag->len);
+ pd->remotetag.len = remotetag->len;
+ p += remotetag->len;
+ }
+
+ pd->body_size_alloc = body->len;
+ pd->body.s = (char*)shm_malloc(pd->body_size_alloc);
+ if (pd->body.s == NULL) {
+ LM_ERR("No more shared memory\n");
+ shm_free(pd);
+ return NULL;
+ }
+ memcpy(pd->body.s, body->s, body->len);
+ pd->body.len = body->len;
+
+ return pd;
+}
+
+dbk_pres_user_t* dbk_new_pres_user(str* user, str* callid, str* localtag, str* remotetag, str* body)
+{
+ dbk_pres_user_t* pu = (dbk_pres_user_t*)shm_malloc(sizeof(dbk_pres_user_t) + user->len);
+
+ if ( pu == NULL) {
+ LM_ERR("No more shared memory\n");
+ return NULL;
+ }
+ memset(pu, 0, sizeof(dbk_pres_user_t));
+ pu->user.s = (char*)pu + sizeof(dbk_pres_user_t);
+ memcpy(pu->user.s, user->s, user->len);
+ pu->user.len = user->len;
+ pu->pd = dbk_new_pres_dialog(callid, localtag, remotetag, body);
+ if (pu->pd == NULL) {
+ LM_ERR("Failed to construct presence dialog\n");
+ shm_free(pu);
+ return NULL;
+ }
+ return pu;
+}
+
+int dbk_pres_insert_pu(str* user, str* callid, str* localtag, str* remotetag, str* body,
+ unsigned int hash_code)
+{
+ dbk_pres_user_t* pu = dbk_new_pres_user(user, callid, localtag, remotetag, body);
+
+ if (pu == NULL) {
+ LM_ERR("Failed to create new pres user\n");
+ return -1;
+ }
+
+ lock_get(&dbk_phtable[hash_code].lock);
+ pu->next = dbk_phtable[hash_code].pu;
+ dbk_phtable[hash_code].pu = pu;
+ lock_release(&dbk_phtable[hash_code].lock);
+ return 0;
+}
+
+dbk_pres_dialog_t* dbk_pres_pu_search_pd(dbk_pres_user_t* pu, str* callid, str* localtag,
+ str* remotetag, dbk_pres_dialog_t** pd_prev_ret, dbk_pres_dialog_t* prev_pd_addr)
+{
+ dbk_pres_dialog_t *pd, *pd_prev = NULL;
+
+ for (pd = pu->pd; pd; pd = pd->next) {
+ if (prev_pd_addr && pd != prev_pd_addr) {
+ pd_prev = pd;
+ continue;
+ }
+
+ if (pd->callid.len==callid->len && memcmp(pd->callid.s, callid->s, callid->len)==0 &&
+ (!localtag->len || !pd->localtag.len || (localtag->len==pd->localtag.len &&
+ memcmp(localtag->s, pd->localtag.s, localtag->len)==0)) &&
+ (!remotetag->len || !pd->remotetag.len || (remotetag->len==pd->remotetag.len &&
+ memcmp(remotetag->s, pd->remotetag.s, remotetag->len)==0)) ) {
+ break;
+ }
+
+ pd_prev = pd;
+ }
+
+ if (pd_prev_ret) *pd_prev_ret = pd_prev;
+
+ return pd;
+}
+
+
+void dbk_free_xml(str* body) {
+ if (body) {
+ if (body->s)
+ xmlFree(body->s);
+ pkg_free(body);
+ }
+}
+
+int dbk_blf_copy_bodies(dbk_pres_user_t* pu, dbk_pres_dialog_t* curr_pd,
+ str* dlg_bodies, int* dlg_no)
+{
+ dbk_pres_dialog_t* pd;
+ int i;
+
+ for (pd = pu->pd, i= *dlg_no; pd && i< BLF_MAX_DIALOGS; pd = pd->next) {
+ if ( pd == curr_pd) {
+ continue;
+ }
+ dlg_bodies[i].s = (char*) pkg_malloc(pd->body.len);
+ if (dlg_bodies[i].s == NULL) {
+ LM_ERR("No more memory\n");
+ goto error;
+ }
+ memcpy(dlg_bodies[i].s, pd->body.s, pd->body.len);
+ dlg_bodies[i].len = pd->body.len;
+ i++;
+ }
+ if (pd) {
+ LM_WARN("More dialogs than BLF_MAX_DIALOGS=%d for user [%.*s]\n",
+ BLF_MAX_DIALOGS, pu->user.len, pu->user.s);
+ }
+ *dlg_no = i;
+ return 0;
+
+ error:
+ while(--i>=*dlg_no) {
+ pkg_free(dlg_bodies[i].s);
+ }
+ return -1;
+}
+
+str* dbk_blf_aggregate_body(str* user, str* dlg_bodies, int dlg_no)
+{
+ str* dlg_bodies_p[BLF_MAX_DIALOGS];
+ struct sip_uri uri;
+ int i;
+
+ LM_DBG("Aggregate %d bodies\n", dlg_no);
+
+ if (parse_uri(user->s, user->len, &uri) < 0) {
+ LM_ERR("Failed to parse uri\n");
+ return NULL;
+ }
+
+ for (i = 0; i < dlg_no; i++) {
+ dlg_bodies_p[i] = &dlg_bodies[i];
+ }
+
+ return pres_dialoginfo_api.agg_dialoginfo(&uri.user, &uri.host, dlg_bodies_p, dlg_no);
+}
+
+str* dbk_phtable_update(str* local_user, str* remote_user, str* callid,
+ str* localtag, str* remotetag, str* state, int initiator)
+{
+ dbk_pres_user_t* pu, *pu_prev = NULL;
+ dbk_pres_dialog_t* pd, *pd_prev = NULL;
+ unsigned int hash_code = core_hash(local_user, 0, dbk_phtable_size);
+ str* body = NULL ;
+ int terminated = strcmp(state->s, "terminated")==0?1:0;
+ str dlg_bodies[BLF_MAX_DIALOGS];
+ int dlg_no = 1;
+ int i;
+
+ LM_DBG("Update dbk phtable: %.*s %.*s %.*s %.*s"
+ ,local_user->len, local_user->s
+ ,remote_user->len, remote_user->s
+ ,callid->len, callid->s
+ ,state->len, state->s);
+
+ if (terminated) {
+ LM_DBG("Updated to terminated state\n");
+ }
+
+ lock_get(&dbk_phtable[hash_code].lock);
+ pu = dbk_pres_search_pu(hash_code, local_user, &pu_prev, 0);
+
+ /** 1. No record for user */
+ if (pu == NULL) {
+ lock_release(&dbk_phtable[hash_code].lock);
+ if (!terminated) {
+ LM_INFO("No record found for %.*s/%.*s, add with initial state %.*s\n"
+ ,local_user->len, local_user->s
+ ,remote_user->len, remote_user->s
+ ,state->len, state->s);
+ if ( (body = pua_dialoginfo_api.build_dialoginfo(state->s, local_user, remote_user,
+ callid, initiator, localtag, remotetag, 0 , 0, 0))== NULL) {
+ LM_ERR("Failed to construct BLF XML doc\n");
+ return NULL;
+ }
+ LM_DBG("dialoginfo body [%.*s]\n", body->len, body->s);
+
+ if ( dbk_pres_insert_pu(local_user, callid, localtag, remotetag, body, hash_code) < 0) {
+ LM_ERR("Failed to insert new pres_user in htable\n");
+ }
+ return body;
+ }
+ LM_INFO("No record found for %.*s/%.*s and state terminated, do nothing\n"
+ ,local_user->len, local_user->s
+ ,remote_user->len, remote_user->s);
+ return pua_dialoginfo_api.build_dialoginfo(state->s, local_user, remote_user, callid,
+ initiator, localtag, remotetag, 0 , 0, 0);
+ }
+
+ pd = dbk_pres_pu_search_pd(pu, callid, localtag, remotetag, &pd_prev, 0);
+
+ /** 2. Existing record for user, no record for dialog */
+ if (pd == NULL) {
+
+ /* copy the other bodies to create the aggregated body */
+ if (pu->pd != NULL) {
+ dbk_blf_copy_bodies(pu, NULL, dlg_bodies, &dlg_no);
+ }
+ lock_release(&dbk_phtable[hash_code].lock);
+ if (!terminated) {
+ LM_INFO("Dialog %.*s not found for %.*s/%.*s, add with initial state %.*s\n"
+ ,callid->len, callid->s
+ ,local_user->len, local_user->s
+ ,remote_user->len, remote_user->s
+ ,state->len, state->s);
+ LM_DBG("Dialog record not found, insert a new one\n");
+ if ( (body = pua_dialoginfo_api.build_dialoginfo(state->s, local_user, remote_user,
+ callid, initiator, localtag, remotetag, 0 , 0, 0))== NULL) {
+ LM_ERR("Failed to construct BLF XML doc\n");
+ goto ret_current_dialog;
+ }
+ LM_DBG("dialoginfo body [%.*s]\n", body->len, body->s);
+
+ pd = dbk_new_pres_dialog(callid, localtag, remotetag, body);
+ if (pd == NULL) {
+ LM_ERR("Failed to create new pres dialog\n");
+ goto ret_agg_dialogs;
+ }
+ lock_get(&dbk_phtable[hash_code].lock);
+ /* search again the corresponding pu
+ * give it the prev addr to speed up the search */
+ pu = dbk_pres_search_pu(hash_code, local_user, 0, pu);
+ if (pu == NULL) { /* pu deleted in the meantime (another dialog ended) */
+ lock_release(&dbk_phtable[hash_code].lock);
+ shm_free(pd);
+ dbk_pres_insert_pu(local_user, callid, localtag, remotetag, body, hash_code);
+ goto ret_current_dialog;
+ }
+ pd->next = pu->pd;
+ pu->pd = pd;
+ lock_release(&dbk_phtable[hash_code].lock);
+ } else { /* terminated */
+ LM_INFO("Dialog %.*s not found for %.*s/%.*s, building terminated body\n"
+ ,callid->len, callid->s
+ ,local_user->len, local_user->s
+ ,remote_user->len, remote_user->s);
+ body = pua_dialoginfo_api.build_dialoginfo(state->s, local_user, remote_user, callid,
+ initiator, localtag, remotetag, 0 , 0, 0);
+ }
+ goto ret_agg_dialogs;
+ }
+
+ /** 3. Existing record for user and existing record for dialog */
+
+ /* If there are other dialogs, make a copy of the bodies */
+ if (pu->pd != pd || pd->next != NULL) {
+ dbk_blf_copy_bodies(pu, pd, dlg_bodies, &dlg_no);
+ }
+
+ if (!terminated) {
+ LM_INFO("Found record %.*s/%.*s and matching dialog %.*s, update the state %.*s\n"
+ ,local_user->len, local_user->s
+ ,remote_user->len, remote_user->s
+ ,callid->len, callid->s
+ ,state->len, state->s);
+ /* update only the body */
+ lock_release(&dbk_phtable[hash_code].lock);
+
+ if ( (body = pua_dialoginfo_api.build_dialoginfo(state->s, local_user, remote_user,
+ callid, initiator, localtag, remotetag, 0 , 0, 0))== NULL) {
+ LM_ERR("Failed to construct BLF XML doc\n");
+ goto ret_current_dialog;
+ }
+ LM_DBG("dialoginfo body [%.*s]\n", body->len, body->s);
+
+ lock_get(&dbk_phtable[hash_code].lock);
+ /* search again the corresponding pu;
+ * give it the prev addr to speed up the search */
+ pu = dbk_pres_search_pu(hash_code, local_user, 0, pu);
+ if (pu == NULL) { /* pres user deleted in the meantime (my dialog ended) */
+ lock_release(&dbk_phtable[hash_code].lock);
+ LM_INFO("Dialog %.*s deleted in the meantime (my dialog must have ended)\n"
+ ,callid->len, callid->s);
+ dbk_free_xml(body);
+ body = NULL;
+ goto ret_current_dialog; /* to free the dlg_bodies array if there is anything there */
+ }
+
+ pd = dbk_pres_pu_search_pd(pu, callid, localtag, remotetag, &pd_prev, pd);
+ if (pd == NULL) {
+ lock_release(&dbk_phtable[hash_code].lock);
+ LM_INFO("Dialog %.*s deleted in the meantime (my dialog must have ended)\n"
+ ,callid->len, callid->s);
+ dbk_free_xml(body);
+ body = NULL;
+ goto ret_current_dialog; /* to free the dlg_bodies array if there is anything there */
+ }
+
+ if (pd->body_size_alloc < body->len) {
+ pd->body_size_alloc = body->len * 2;
+ pd->body.s = (char*) shm_realloc(pd->body.s, pd->body_size_alloc);
+ if (pd->body.s == NULL) {
+ LM_ERR("No more shared memory\n");
+ lock_release(&dbk_phtable[hash_code].lock);
+ goto ret_agg_dialogs;
+ }
+ }
+ memcpy(pd->body.s, body->s, body->len);
+ pd->body.len = body->len;
+ if ( pu->pd != pd || pd->next!= NULL) {
+ lock_release(&dbk_phtable[hash_code].lock);
+ goto ret_current_dialog;
+ }
+ lock_release(&dbk_phtable[hash_code].lock);
+ } else {
+ LM_INFO("pd_prev: %p pd->next: %p\n", pd_prev, pd->next);
+ if (pd_prev == NULL && pd->next == NULL) { // there is no other dialog record
+ LM_INFO("Found record %.*s/%.*s and matching dialog %.*s, no remaining dialogs removing pu record\n"
+ ,local_user->len, local_user->s
+ ,remote_user->len, remote_user->s
+ ,callid->len, callid->s);
+ /* delete pu */
+ if (pu_prev) {
+ pu_prev->next = pu->next;
+ } else {
+ dbk_phtable[hash_code].pu = pu->next;
+ }
+ lock_release(&dbk_phtable[hash_code].lock);
+ dbk_free_pu(pu);
+
+ body = pua_dialoginfo_api.build_dialoginfo(state->s, local_user, remote_user, callid,
+ initiator, localtag, remotetag, 0 , 0, 0);
+ goto ret_current_dialog;
+ } else {
+ LM_INFO("Found record %.*s/%.*s and matching dialog %.*s, other dialogs remain\n"
+ ,local_user->len, local_user->s
+ ,remote_user->len, remote_user->s
+ ,callid->len, callid->s);
+ /* delete pd */
+ if (pd_prev) {
+ pd_prev->next = pd->next;
+ } else {
+ pu->pd = pd->next;
+ }
+ lock_release(&dbk_phtable[hash_code].lock);
+ dbk_free_pd(pd);
+ body = pua_dialoginfo_api.build_dialoginfo(state->s, local_user, remote_user, callid,
+ initiator, localtag, remotetag, 0 , 0, 0);
+ }
+ }
+
+ ret_agg_dialogs:
+ if (dlg_no > 1) {
+ str* body_copy = body;
+ dlg_bodies[0] = *body;
+ body = dbk_blf_aggregate_body(local_user, dlg_bodies, dlg_no);
+ if (body == NULL) {
+ LM_ERR("Failed to aggregate body\n");
+ body = body_copy;
+ } else {
+ dbk_free_xml(body_copy);
+ }
+ }
+
+ ret_current_dialog:
+ /* free the copied dialog bodies */
+ for(i =1; i < dlg_no; i++) {
+ pkg_free(dlg_bodies[i].s);
+ }
+
+ return body;
+}
+
+
+int dbk_pres_update_and_notify(str* local_user, str* remote_user, str* callid,
+ str* localtag, str* remotetag, str* state, int initiator) {
+
+ str event = str_init("dialog");
+ str* body;
+
+ /* update in htable */
+ if ((body = dbk_phtable_update(local_user, remote_user, callid, localtag,
+ remotetag, state, initiator)) == 0) {
+ LM_ERR("Failed to update in htable\n");
+ return -1;
+ }
+
+ /* call refresh_watchers from presence */
+ if (presence_api.notify_watchers(local_user, &event, body) < 0) {
+ LM_ERR("Failed to notify watchers\n");
+ goto error;
+ }
+ dbk_free_xml(body);
+ return 0;
+ error:
+ dbk_free_xml(body);
+ return -1;
+}
+#define json_extract_field(json_name, field) do { \
+ struct json_object* obj = json_object_object_get(json_obj, json_name); \
+ field.s = (char*)json_object_get_string(obj); \
+ if (field.s == NULL) { \
+ LM_DBG("Json-c error - failed to extract field [%s]\n", json_name); \
+ field.s = ""; \
+ } else { \
+ field.len = strlen(field.s); \
+ } \
+ LM_DBG("%s: [%s]\n", json_name, field.s?field.s:"Empty"); \
+ } while (0);
+
+
+int dbk_phtable_flush(int flush_all, str* user)
+{
+ dbk_pres_user_t* pu;
+
+ if (flush_all) {
+ int i;
+ dbk_pres_user_t* pu_next;
+ for (i = 0; i< dbk_phtable_size; i++) {
+ lock_get(&dbk_phtable[i].lock);
+ pu = dbk_phtable[i].pu;
+ dbk_phtable[i].pu = NULL;
+ lock_release(&dbk_phtable[i].lock);
+
+ for (; pu; pu = pu_next) {
+ pu_next = pu->next;
+ dbk_free_pu(pu);
+ }
+ }
+ } else {
+ int hash_code;
+ dbk_pres_user_t* pu_prev = NULL;
+ str* body;
+ static str event = str_init("dialog");
+
+ hash_code = core_hash(user, NULL, dbk_phtable_size);
+ lock_get(&dbk_phtable[hash_code].lock);
+ pu = dbk_pres_search_pu(hash_code, user, &pu_prev, 0);
+ if (pu == NULL) {
+ LM_INFO("FLUSH: No record found for user %.*s\n", user->len, user->s);
+ lock_release(&dbk_phtable[hash_code].lock);
+ } else {
+ LM_INFO("FLUSH: Delete record for user %.*s\n", user->len, user->s);
+ if (pu_prev) {
+ pu_prev->next = pu->next;
+ } else {
+ dbk_phtable[hash_code].pu = pu->next;
+ }
+ lock_release(&dbk_phtable[hash_code].lock);
+ dbk_free_pu(pu);
+ }
+
+
+ /* send a notify with no dialog to clear dialog state */
+ if ( (body = pua_dialoginfo_api.build_dialoginfo(0, user,
+ 0, 0, 0, 0, 0, 0, 0, 0)) == NULL ) {
+ LM_ERR("Failed to construct BLF XML doc\n");
+ return -1;
+ }
+ if (presence_api.notify_watchers(user, &event, body) < 0) {
+ LM_ERR("Failed to notify watchers\n");
+ dbk_free_xml(body);
+ return -1;
+ }
+ dbk_free_xml(body);
+ }
+ return 0;
+}
+
+int rmqp_pres_flush_handle(struct json_object* json_obj)
+{
+ str type={0, 0};
+ str user= {0, 0};
+ int flush_all = 0;
+
+ json_extract_field(BLF_JSON_TYPE, type);
+ if (type.len == 3 && strncmp(type.s, "all", 3) == 0) {
+ flush_all = 1;
+ } else {
+ json_extract_field(BLF_JSON_USER, user);
+ }
+
+ return dbk_phtable_flush(flush_all, &user);