/
dlg_replication.c
1608 lines (1338 loc) · 40.8 KB
/
dlg_replication.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
/*
* dialog module - basic support for dialog tracking
*
* Copyright (C) 2013 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* History:
* --------
* 2013-04-12 initial version (Liviu)
*/
#include "dlg_hash.h"
#include "dlg_db_handler.h"
#include "dlg_profile.h"
#include "dlg_replication.h"
#include "dlg_repl_profile.h"
#include "../../resolve.h"
#include "../../forward.h"
extern int active_dlgs_cnt;
extern int early_dlgs_cnt;
extern int dlg_enable_stats;
extern stat_var *active_dlgs;
extern stat_var *processed_dlgs;
extern stat_var *create_sent;
extern stat_var *update_sent;
extern stat_var *delete_sent;
extern stat_var *create_recv;
extern stat_var *update_recv;
extern stat_var *delete_recv;
struct clusterer_binds clusterer_api;
str shtag_dlg_val = str_init("dlgX_shtag");
struct dlg_sharing_tag **shtags_list;
rw_lock_t *shtags_lock;
static struct socket_info * fetch_socket_info(str *addr)
{
struct socket_info *sock;
int port, proto;
str host;
if (!addr->s || addr->s[0] == 0)
return NULL;
if (parse_phostport(addr->s, addr->len, &host.s, &host.len,
&port, &proto) != 0) {
LM_ERR("bad socket <%.*s>\n", addr->len, addr->s);
return NULL;
}
sock = grep_sock_info(&host, (unsigned short) port, (unsigned short) proto);
if (!sock) {
LM_WARN("non-local socket <%.*s>...ignoring\n", addr->len, addr->s);
}
return sock;
}
#define DLG_BIN_POP(_type, _p, _field, _label) \
do { \
if (bin_pop_ ## _type(_p, (str *)&_field) != 0) { \
LM_WARN("cannot find %s field in bin packet!\n", #_field); \
goto _label; \
} \
} while (0)
/* Binary Packet receiving functions */
/**
* replicates a confirmed dialog from another OpenSIPS instance
* by reading the relevant information using the Binary Packet Interface
*/
int dlg_replicated_create(bin_packet_t *packet, struct dlg_cell *cell, str *ftag,
str *ttag, int safe)
{
int h_entry;
unsigned int dir, dst_leg;
str callid = { NULL, 0 }, from_uri, to_uri, from_tag, to_tag;
str cseq1, cseq2, contact1, contact2, rroute1, rroute2, mangled_fu, mangled_tu;
str sock, vars, profiles;
struct dlg_cell *dlg = NULL;
struct socket_info *caller_sock, *callee_sock;
struct dlg_entry *d_entry;
LM_DBG("Received replicated dialog!\n");
if (!cell) {
DLG_BIN_POP(str, packet, callid, malformed);
DLG_BIN_POP(str, packet, from_tag, malformed);
DLG_BIN_POP(str, packet, to_tag, malformed);
DLG_BIN_POP(str, packet, from_uri, malformed);
DLG_BIN_POP(str, packet, to_uri, malformed);
dlg = get_dlg(&callid, &from_tag, &to_tag, &dir, &dst_leg);
h_entry = dlg_hash(&callid);
d_entry = &d_table->entries[h_entry];
if (safe)
dlg_lock(d_table, d_entry);
if (dlg) {
LM_DBG("Dialog with ci '%.*s' is already created\n",
callid.len, callid.s);
unref_dlg_unsafe(dlg, 1, d_entry);
/* unmark dlg as loaded from DB (otherwise it would have been
* dropped later when syncing from cluster is done) */
dlg->flags &= ~DLG_FLAG_FROM_DB;
dlg_unlock(d_table, d_entry);
return 0;
}
dlg = build_new_dlg(&callid, &from_uri, &to_uri, &from_tag);
if (!dlg) {
LM_ERR("Failed to create replicated dialog!\n");
goto pre_linking_error;
}
} else {
h_entry = dlg_hash(&cell->callid);
d_entry = &d_table->entries[h_entry];
if (safe)
dlg_lock(d_table, d_entry);
from_tag = *ftag;
to_tag = *ttag;
dlg = cell;
}
if_update_stat(dlg_enable_stats, processed_dlgs, 1);
DLG_BIN_POP(int, packet, dlg->h_id, pre_linking_error);
DLG_BIN_POP(int, packet, dlg->start_ts, pre_linking_error);
DLG_BIN_POP(int, packet, dlg->state, pre_linking_error);
/* next_id follows the max value of all replicated ids */
if (d_table->entries[dlg->h_entry].next_id <= dlg->h_id)
d_table->entries[dlg->h_entry].next_id = dlg->h_id + 1;
DLG_BIN_POP(str, packet, sock, pre_linking_error);
caller_sock = fetch_socket_info(&sock);
DLG_BIN_POP(str, packet, sock, pre_linking_error);
callee_sock = fetch_socket_info(&sock);
if (!caller_sock || !callee_sock) {
LM_ERR("Replicated dialog doesn't match any listening sockets\n");
goto pre_linking_error;
}
DLG_BIN_POP(str, packet, cseq1, pre_linking_error);
DLG_BIN_POP(str, packet, cseq2, pre_linking_error);
DLG_BIN_POP(str, packet, rroute1, pre_linking_error);
DLG_BIN_POP(str, packet, rroute2, pre_linking_error);
DLG_BIN_POP(str, packet, contact1, pre_linking_error);
DLG_BIN_POP(str, packet, contact2, pre_linking_error);
DLG_BIN_POP(str, packet, mangled_fu, pre_linking_error);
DLG_BIN_POP(str, packet, mangled_tu, pre_linking_error);
/* add the 2 legs */
/* TODO - sdp here */
if (dlg_update_leg_info(0, dlg, &from_tag, &rroute1, &contact1,
&cseq1, caller_sock, 0, 0,0) != 0 ||
dlg_update_leg_info(1, dlg, &to_tag, &rroute2, &contact2,
&cseq2, callee_sock, &mangled_fu, &mangled_tu,0) != 0) {
LM_ERR("dlg_set_leg_info failed\n");
goto pre_linking_error;
}
dlg->legs_no[DLG_LEG_200OK] = DLG_FIRST_CALLEE_LEG;
/* link the dialog into the hash */
if (!d_entry->first)
d_entry->first = d_entry->last = dlg;
else {
d_entry->last->next = dlg;
dlg->prev = d_entry->last;
d_entry->last = dlg;
}
dlg->ref++;
d_entry->cnt++;
DLG_BIN_POP(str, packet, vars, pre_linking_error);
DLG_BIN_POP(str, packet, profiles, pre_linking_error);
DLG_BIN_POP(int, packet, dlg->user_flags, pre_linking_error);
DLG_BIN_POP(int, packet, dlg->mod_flags, pre_linking_error);
DLG_BIN_POP(int, packet, dlg->flags, pre_linking_error);
/* also save the dialog into the DB on this instance */
dlg->flags |= DLG_FLAG_NEW;
DLG_BIN_POP(int, packet, dlg->tl.timeout, pre_linking_error);
DLG_BIN_POP(int, packet, dlg->legs[DLG_CALLER_LEG].last_gen_cseq, pre_linking_error);
DLG_BIN_POP(int, packet, dlg->legs[callee_idx(dlg)].last_gen_cseq, pre_linking_error);
if (dlg->tl.timeout <= (unsigned int) time(0))
dlg->tl.timeout = 0;
else
dlg->tl.timeout -= (unsigned int) time(0);
/* restore the timer values */
if (insert_dlg_timer(&dlg->tl, (int) dlg->tl.timeout) != 0) {
LM_CRIT("Unable to insert dlg %p [%u:%u] "
"with clid '%.*s' and tags '%.*s' '%.*s'\n",
dlg, dlg->h_entry, dlg->h_id,
dlg->callid.len, dlg->callid.s,
dlg->legs[DLG_CALLER_LEG].tag.len,
dlg->legs[DLG_CALLER_LEG].tag.s,
dlg->legs[callee_idx(dlg)].tag.len,
ZSW(dlg->legs[callee_idx(dlg)].tag.s));
goto error;
}
if (dlg->state == DLG_STATE_CONFIRMED_NA ||
dlg->state == DLG_STATE_CONFIRMED)
active_dlgs_cnt++;
/* reference the dialog as kept in the timer list */
ref_dlg_unsafe(dlg, 1);
LM_DBG("Received initial timeout of %d for dialog %.*s, safe = %d\n",
dlg->tl.timeout, callid.len, callid.s, safe);
dlg->lifetime = 0;
if (dlg->flags & DLG_FLAG_PING_CALLER || dlg->flags & DLG_FLAG_PING_CALLEE) {
if (insert_ping_timer(dlg) != 0)
LM_CRIT("Unable to insert dlg %p into ping timer\n",dlg);
else {
ref_dlg_unsafe(dlg, 1);
}
}
if (dlg_db_mode == DB_MODE_DELAYED) {
/* to be later removed by timer */
ref_dlg_unsafe(dlg, 1);
}
if (vars.s && vars.len != 0)
read_dialog_vars(vars.s, vars.len, dlg);
dlg_unlock(d_table, d_entry);
if (profiles.s && profiles.len != 0)
read_dialog_profiles(profiles.s, profiles.len, dlg, 0, 1);
if_update_stat(dlg_enable_stats, active_dlgs, 1);
run_load_callback_per_dlg(dlg);
return 0;
pre_linking_error:
dlg_unlock(d_table, d_entry);
if (dlg)
destroy_dlg(dlg);
return -1;
error:
dlg_unlock(d_table, d_entry);
if (dlg)
unref_dlg(dlg, 1);
malformed:
return -1;
}
/**
* replicates the remote update of an ongoing dialog locally
* by reading the relevant information using the Binary Packet Interface
*/
int dlg_replicated_update(bin_packet_t *packet)
{
struct dlg_cell *dlg;
str call_id, from_tag, to_tag, from_uri, to_uri, vars, profiles;
unsigned int dir, dst_leg;
int timeout, h_entry;
str st;
struct dlg_entry *d_entry;
int rcv_flags, save_new_flag;
bin_pop_str(packet, &call_id);
bin_pop_str(packet, &from_tag);
bin_pop_str(packet, &to_tag);
bin_pop_str(packet, &from_uri);
bin_pop_str(packet, &to_uri);
LM_DBG("replicated update for ['%.*s' '%.*s' '%.*s' '%.*s' '%.*s']\n",
call_id.len, call_id.s, from_tag.len, from_tag.s, to_tag.len, to_tag.s,
from_uri.len, from_uri.s, to_uri.len, to_uri.s);
dlg = get_dlg(&call_id, &from_tag, &to_tag, &dir, &dst_leg);
h_entry = dlg_hash(&call_id);
d_entry = &d_table->entries[h_entry];
dlg_lock(d_table, d_entry);
if (!dlg) {
LM_DBG("dialog not found, building new\n");
dlg = build_new_dlg(&call_id, &from_uri, &to_uri, &from_tag);
if (!dlg) {
LM_ERR("Failed to create replicated dialog!\n");
goto error;
}
return dlg_replicated_create(packet ,dlg, &from_tag, &to_tag, 0);
}
bin_skip_int(packet, 2);
bin_pop_int(packet, &dlg->state);
bin_skip_str(packet, 2);
bin_pop_str(packet, &st);
if (dlg_update_cseq(dlg, DLG_CALLER_LEG, &st, 0) != 0) {
LM_ERR("failed to update caller cseq\n");
goto error;
}
bin_pop_str(packet, &st);
if (dlg_update_cseq(dlg, callee_idx(dlg), &st, 0) != 0) {
LM_ERR("failed to update callee cseq\n");
goto error;
}
bin_skip_str(packet, 6);
bin_pop_str(packet, &vars);
bin_pop_str(packet, &profiles);
bin_pop_int(packet, &dlg->user_flags);
bin_pop_int(packet, &dlg->mod_flags);
bin_pop_int(packet, &rcv_flags);
/* make sure an update received immediately after a create can't
* incorrectly erase the DLG_FLAG_NEW before locally writing to DB */
save_new_flag = dlg->flags & DLG_FLAG_NEW;
dlg->flags = rcv_flags;
dlg->flags |= ((save_new_flag ? DLG_FLAG_NEW : 0) | DLG_FLAG_CHANGED);
bin_pop_int(packet, &timeout);
bin_skip_int(packet, 2);
timeout -= time(0);
LM_DBG("Received updated timeout of %d for dialog %.*s\n",
timeout, call_id.len, call_id.s);
if (dlg->lifetime != timeout) {
dlg->lifetime = timeout;
switch (update_dlg_timer(&dlg->tl, dlg->lifetime) ) {
case -1:
LM_ERR("failed to update dialog lifetime!\n");
/* continue */
case 0:
/* timeout value was updated */
break;
case 1:
/* dlg inserted in timer list with new expire (reference it)*/
ref_dlg(dlg,1);
}
}
unref_dlg_unsafe(dlg, 1, d_entry);
if (vars.s && vars.len != 0)
read_dialog_vars(vars.s, vars.len, dlg);
dlg->flags |= DLG_FLAG_VP_CHANGED;
dlg_unlock(d_table, d_entry);
if (profiles.s && profiles.len != 0)
read_dialog_profiles(profiles.s, profiles.len, dlg, 1, 1);
return 0;
error:
dlg_unlock(d_table, d_entry);
return -1;
}
/**
* replicates the remote deletion of a dialog locally
* by reading the relevant information using the Binary Packet Interface
*/
int dlg_replicated_delete(bin_packet_t *packet)
{
str call_id, from_tag, to_tag;
unsigned int dir, dst_leg;
struct dlg_cell *dlg;
int old_state, new_state, unref, ret;
DLG_BIN_POP(str, packet, call_id, malformed);
DLG_BIN_POP(str, packet, from_tag, malformed);
DLG_BIN_POP(str, packet, to_tag, malformed);
LM_DBG("Deleting dialog with callid: %.*s\n", call_id.len, call_id.s);
dlg = get_dlg(&call_id, &from_tag, &to_tag, &dir, &dst_leg);
if (!dlg) {
/* may be already deleted due to timeout */
LM_DBG("dialog not found (callid: |%.*s| ftag: |%.*s|\n",
call_id.len, call_id.s, from_tag.len, from_tag.s);
return 0;
}
destroy_linkers(dlg, 1);
remove_dlg_prof_table(dlg, 1);
/* simulate BYE received from caller */
next_state_dlg(dlg, DLG_EVENT_REQBYE, DLG_DIR_DOWNSTREAM, &old_state,
&new_state, &unref, dlg->legs_no[DLG_LEG_200OK], 0);
if (old_state == new_state) {
LM_ERR("duplicate dialog delete request (callid: |%.*s|"
"ftag: |%.*s|\n", call_id.len, call_id.s, from_tag.len, from_tag.s);
return -1;
}
ret = remove_dlg_timer(&dlg->tl);
if (ret < 0) {
LM_CRIT("unable to unlink the timer on dlg %p [%u:%u] "
"with clid '%.*s' and tags '%.*s' '%.*s'\n",
dlg, dlg->h_entry, dlg->h_id,
dlg->callid.len, dlg->callid.s,
dlg->legs[DLG_CALLER_LEG].tag.len,
dlg->legs[DLG_CALLER_LEG].tag.s,
dlg->legs[callee_idx(dlg)].tag.len,
ZSW(dlg->legs[callee_idx(dlg)].tag.s));
} else if (ret > 0) {
LM_DBG("dlg expired (not in timer list) on dlg %p [%u:%u] "
"with clid '%.*s' and tags '%.*s' '%.*s'\n",
dlg, dlg->h_entry, dlg->h_id,
dlg->callid.len, dlg->callid.s,
dlg->legs[DLG_CALLER_LEG].tag.len,
dlg->legs[DLG_CALLER_LEG].tag.s,
dlg->legs[callee_idx(dlg)].tag.len,
ZSW(dlg->legs[callee_idx(dlg)].tag.s));
} else {
/* dialog successfully removed from timer -> unref */
unref++;
}
unref_dlg(dlg, 1 + unref);
if_update_stat(dlg_enable_stats, active_dlgs, -1);
return 0;
malformed:
return -1;
}
#undef DLG_BIN_POP
void bin_push_dlg(bin_packet_t *packet, struct dlg_cell *dlg)
{
int callee_leg;
str *vars, *profiles;
callee_leg = callee_idx(dlg);
bin_push_str(packet, &dlg->callid);
bin_push_str(packet, &dlg->legs[DLG_CALLER_LEG].tag);
bin_push_str(packet, &dlg->legs[callee_leg].tag);
bin_push_str(packet, &dlg->from_uri);
bin_push_str(packet, &dlg->to_uri);
bin_push_int(packet, dlg->h_id);
bin_push_int(packet, dlg->start_ts);
bin_push_int(packet, dlg->state);
bin_push_str(packet, &dlg->legs[DLG_CALLER_LEG].bind_addr->sock_str);
if (dlg->legs[callee_leg].bind_addr)
bin_push_str(packet, &dlg->legs[callee_leg].bind_addr->sock_str);
else
bin_push_str(packet, NULL);
bin_push_str(packet, &dlg->legs[DLG_CALLER_LEG].r_cseq);
bin_push_str(packet, &dlg->legs[callee_leg].r_cseq);
bin_push_str(packet, &dlg->legs[DLG_CALLER_LEG].route_set);
bin_push_str(packet, &dlg->legs[callee_leg].route_set);
bin_push_str(packet, &dlg->legs[DLG_CALLER_LEG].contact);
bin_push_str(packet, &dlg->legs[callee_leg].contact);
bin_push_str(packet, &dlg->legs[callee_leg].from_uri);
bin_push_str(packet, &dlg->legs[callee_leg].to_uri);
/* give modules the chance to write values/profiles before replicating */
run_dlg_callbacks(DLGCB_WRITE_VP, dlg, NULL, DLG_DIR_NONE, NULL, 1, 1);
vars = write_dialog_vars(dlg->vals);
profiles = write_dialog_profiles(dlg->profile_links);
bin_push_str(packet, vars);
bin_push_str(packet, profiles);
bin_push_int(packet, dlg->user_flags);
bin_push_int(packet, dlg->mod_flags);
bin_push_int(packet, dlg->flags &
~(DLG_FLAG_NEW|DLG_FLAG_CHANGED|DLG_FLAG_VP_CHANGED|DLG_FLAG_FROM_DB));
bin_push_int(packet, (unsigned int)time(0) + dlg->tl.timeout - get_ticks());
bin_push_int(packet, dlg->legs[DLG_CALLER_LEG].last_gen_cseq);
bin_push_int(packet, dlg->legs[callee_leg].last_gen_cseq);
}
/* Binary Packet sending functions */
/**
* replicates a locally created dialog to all the destinations
* specified with the 'replicate_dialogs' modparam
*/
void replicate_dialog_created(struct dlg_cell *dlg)
{
int rc;
bin_packet_t packet;
dlg_lock_dlg(dlg);
if (dlg->state != DLG_STATE_CONFIRMED_NA && dlg->state != DLG_STATE_CONFIRMED) {
/* we don't need to replicate when in deleted state */
LM_WARN("not replicating dlg create message due to bad state %d (%.*s)\n",
dlg->state, dlg->callid.len, dlg->callid.s);
goto no_send;
}
if (dlg->replicated) {
/* already created - must be a retransmission */
LM_DBG("not replicating retransmission for %p (%.*s)\n",
dlg, dlg->callid.len, dlg->callid.s);
goto no_send;
}
if (bin_init(&packet, &dlg_repl_cap, REPLICATION_DLG_CREATED, BIN_VERSION, 0) != 0)
goto init_error;
bin_push_dlg(&packet, dlg);
dlg->replicated = 1;
dlg_unlock_dlg(dlg);
rc = clusterer_api.send_all(&packet, dialog_repl_cluster);
switch (rc) {
case CLUSTERER_CURR_DISABLED:
LM_INFO("Current node is disabled in cluster: %d\n", dialog_repl_cluster);
goto error;
case CLUSTERER_DEST_DOWN:
LM_INFO("All destinations in cluster: %d are down or probing\n",
dialog_repl_cluster);
goto error;
case CLUSTERER_SEND_ERR:
LM_ERR("Error sending in cluster: %d\n", dialog_repl_cluster);
goto error;
}
if_update_stat(dlg_enable_stats,create_sent,1);
bin_free_packet(&packet);
return;
error:
bin_free_packet(&packet);
LM_ERR("Failed to replicate created dialog\n");
return;
init_error:
LM_ERR("Failed to replicate created dialog\n");
no_send:
dlg_unlock_dlg(dlg);
return;
}
/**
* replicates a local dialog update in the cluster
*/
void replicate_dialog_updated(struct dlg_cell *dlg)
{
int rc;
bin_packet_t packet;
dlg_lock_dlg(dlg);
if (dlg->state < DLG_STATE_CONFIRMED_NA) {
LM_DBG("not replicating update in state %d (%.*s)\n", dlg->state,
dlg->callid.len, dlg->callid.s);
goto end;
}
if (dlg->state == DLG_STATE_DELETED) {
/* we no longer need to update anything */
LM_WARN("not replicating dlg update message due to bad state %d (%.*s)\n",
dlg->state, dlg->callid.len, dlg->callid.s);
goto end;
}
if (bin_init(&packet, &dlg_repl_cap, REPLICATION_DLG_UPDATED, BIN_VERSION, 0) != 0)
goto init_error;
bin_push_dlg(&packet, dlg);
dlg->replicated = 1;
dlg_unlock_dlg(dlg);
rc = clusterer_api.send_all(&packet, dialog_repl_cluster);
switch (rc) {
case CLUSTERER_CURR_DISABLED:
LM_INFO("Current node is disabled in cluster: %d\n", dialog_repl_cluster);
goto error;
case CLUSTERER_DEST_DOWN:
LM_ERR("All destinations in cluster: %d are down or probing\n",
dialog_repl_cluster);
goto error;
case CLUSTERER_SEND_ERR:
LM_ERR("Error sending in cluster: %d\n", dialog_repl_cluster);
goto error;
}
if_update_stat(dlg_enable_stats,update_sent,1);
bin_free_packet(&packet);
return;
error:
LM_ERR("Failed to replicate updated dialog\n");
bin_free_packet(&packet);
return;
init_error:
LM_ERR("Failed to replicate updated dialog\n");
end:
dlg_unlock_dlg(dlg);
return;
}
/**
* replicates a local dialog delete event to all the destinations
* specified with the 'replicate_dialogs' modparam
*/
void replicate_dialog_deleted(struct dlg_cell *dlg)
{
int rc;
bin_packet_t packet;
if (bin_init(&packet, &dlg_repl_cap, REPLICATION_DLG_DELETED, BIN_VERSION, 1024) != 0)
goto error;
bin_push_str(&packet, &dlg->callid);
bin_push_str(&packet, &dlg->legs[DLG_CALLER_LEG].tag);
bin_push_str(&packet, &dlg->legs[callee_idx(dlg)].tag);
rc = clusterer_api.send_all(&packet, dialog_repl_cluster);
switch (rc) {
case CLUSTERER_CURR_DISABLED:
LM_INFO("Current node is disabled in cluster: %d\n", dialog_repl_cluster);
goto error_free;
case CLUSTERER_DEST_DOWN:
LM_ERR("All destinations in cluster: %d are down or probing\n",
dialog_repl_cluster);
goto error_free;
case CLUSTERER_SEND_ERR:
LM_ERR("Error sending in cluster: %d\n", dialog_repl_cluster);
goto error_free;
}
if_update_stat(dlg_enable_stats, delete_sent, 1);
bin_free_packet(&packet);
return;
error_free:
bin_free_packet(&packet);
error:
LM_ERR("Failed to replicate deleted dialog\n");
}
struct dlg_sharing_tag *create_dlg_shtag(str *tag_name)
{
struct dlg_sharing_tag *new_tag;
new_tag = shm_malloc(sizeof *new_tag + tag_name->len);
if (!new_tag) {
LM_ERR("No more shm memory\n");
return NULL;
}
memset(new_tag, 0, sizeof *new_tag);
new_tag->name.s = (char *)(new_tag + 1);
new_tag->name.len = tag_name->len;
memcpy(new_tag->name.s, tag_name->s, tag_name->len);
new_tag->state = SHTAG_STATE_BACKUP;
new_tag->next = *shtags_list;
*shtags_list = new_tag;
return new_tag;
}
/* should be called under writing lock */
struct dlg_sharing_tag *get_shtag_unsafe(str *tag_name)
{
struct dlg_sharing_tag *tag;
for (tag = *shtags_list; tag && str_strcmp(&tag->name, tag_name);
tag = tag->next) ;
if (!tag && !(tag = create_dlg_shtag(tag_name))) {
LM_ERR("Failed to create replication tag\n");
return NULL;
}
return tag;
}
/* you must release the lock for switchable reading if @lock_stop_r = 0
* in case of error the lock is released by the function
*/
struct dlg_sharing_tag *get_shtag(str *tag_name, int lock_stop_r)
{
struct dlg_sharing_tag *tag;
int lock_old_flag;
lock_start_sw_read(shtags_lock);
for (tag = *shtags_list; tag && str_strcmp(&tag->name, tag_name);
tag = tag->next) ;
if (!tag) {
lock_switch_write(shtags_lock, lock_old_flag);
if ((tag = create_dlg_shtag(tag_name)) == NULL) {
LM_ERR("Failed to create sharing tag\n");
lock_switch_read(shtags_lock, lock_old_flag);
lock_stop_sw_read(shtags_lock);
return NULL;
}
lock_switch_read(shtags_lock, lock_old_flag);
}
if (lock_stop_r)
lock_stop_sw_read(shtags_lock);
return tag;
}
void free_active_msgs_info(struct dlg_sharing_tag *tag)
{
struct n_send_info *it, *tmp;
it = tag->active_msgs_sent;
while (it) {
tmp = it;
it = it->next;
shm_free(tmp);
}
tag->active_msgs_sent = NULL;
}
static int receive_shtag_active_msg(bin_packet_t *packet)
{
str tag_name;
struct dlg_sharing_tag *tag;
bin_pop_str(packet, &tag_name);
lock_start_write(shtags_lock);
if ((tag = get_shtag_unsafe(&tag_name)) == NULL) {
LM_ERR("Unable to fetch sharing tag\n");
lock_stop_write(shtags_lock);
return -1;
}
/* directly go to backup state when another
* node in the cluster is to active */
tag->state = SHTAG_STATE_BACKUP;
tag->send_active_msg = 0;
free_active_msgs_info(tag);
lock_stop_write(shtags_lock);
return 0;
}
void receive_dlg_repl(bin_packet_t *packet)
{
int rc = 0;
bin_packet_t *pkt;
for (pkt = packet; pkt; pkt = pkt->next) {
switch (pkt->type) {
case REPLICATION_DLG_CREATED:
rc = dlg_replicated_create(pkt, NULL, NULL, NULL, 1);
if_update_stat(dlg_enable_stats, create_recv, 1);
break;
case REPLICATION_DLG_UPDATED:
rc = dlg_replicated_update(pkt);
if_update_stat(dlg_enable_stats, update_recv, 1);
break;
case REPLICATION_DLG_DELETED:
rc = dlg_replicated_delete(pkt);
if_update_stat(dlg_enable_stats, delete_recv, 1);
break;
case DLG_SHARING_TAG_ACTIVE:
rc = receive_shtag_active_msg(pkt);
break;
case SYNC_PACKET_TYPE:
while (clusterer_api.sync_chunk_iter(pkt))
if (dlg_replicated_create(pkt, NULL, NULL, NULL, 1) < 0) {
LM_ERR("Failed to process sync packet\n");
return;
}
break;
default:
rc = -1;
LM_WARN("Invalid dialog binary packet command: %d "
"(from node: %d in cluster: %d)\n", pkt->type, pkt->src_id,
dialog_repl_cluster);
}
if (rc != 0)
LM_ERR("Failed to process a binary packet!\n");
}
}
static int receive_sync_request(int node_id)
{
int i;
struct dlg_cell *dlg;
bin_packet_t *sync_packet;
for (i = 0; i < d_table->size; i++) {
dlg_lock(d_table, &(d_table->entries[i]));
for (dlg = d_table->entries[i].first; dlg; dlg = dlg->next) {
sync_packet = clusterer_api.sync_chunk_start(&dlg_repl_cap,
dialog_repl_cluster, node_id);
if (!sync_packet)
goto error;
bin_push_dlg(sync_packet, dlg);
}
dlg_unlock(d_table, &(d_table->entries[i]));
}
return 0;
error:
dlg_unlock(d_table, &(d_table->entries[i]));
return -1;
}
int send_shtag_active_info(str *tag_name, int node_id)
{
bin_packet_t packet;
if (bin_init(&packet, &dlg_repl_cap, DLG_SHARING_TAG_ACTIVE, BIN_VERSION,
0) < 0) {
LM_ERR("Failed to init bin packet\n");
return -1;
}
bin_push_str(&packet, tag_name);
if (node_id) {
if (clusterer_api.send_to(&packet, dialog_repl_cluster, node_id) !=
CLUSTERER_SEND_SUCCES) {
bin_free_packet(&packet);
return -1;
}
} else
if (clusterer_api.send_all(&packet, dialog_repl_cluster) !=
CLUSTERER_SEND_SUCCES) {
bin_free_packet(&packet);
return -1;
}
bin_free_packet(&packet);
return 0;
}
void rcv_cluster_event(enum clusterer_event ev, int node_id)
{
struct dlg_sharing_tag *tag;
struct n_send_info *ni;
int lock_old_flag;
struct dlg_cell *dlg, *next_dlg;
int i;
int ret;
int unref;
if (ev == SYNC_REQ_RCV && receive_sync_request(node_id) < 0)
LM_ERR("Failed to reply to sync request from node: %d\n", node_id);
else if (ev == SYNC_DONE) {
if (dlg_db_mode == DB_MODE_NONE)
return;
/* drop dialogs loaded from DB which have not been reconfirmed
* through syncing or SIP(updates) */
for (i = 0; i < d_table->size; i++) {
dlg_lock(d_table, &d_table->entries[i]);
dlg = d_table->entries[i].first;
while (dlg) {
if (!(dlg->flags & DLG_FLAG_FROM_DB)) {
dlg = dlg->next;
continue;
}
LM_DBG("Drop DB loaded dialog ID=%lld\n",
((long long)dlg->h_entry << 32) | (dlg->h_id));
unref = 1; /* unref from hash */
dlg->state = DLG_STATE_DELETED;
destroy_linkers_unsafe(dlg, 0);
/* make sure dialog is not freed while we don't hold the lock */
ref_dlg_unsafe(dlg, unref);
dlg_unlock(d_table, &d_table->entries[i]);
remove_dlg_prof_table(dlg, 0);
dlg_lock(d_table, &d_table->entries[i]);
unref++;
/* remove from timer */
ret = remove_dlg_timer(&dlg->tl);
if (ret < 0) {
LM_ERR("unable to unlink the timer on dlg %p [%u:%u] "
"with clid '%.*s' and tags '%.*s' '%.*s'\n",
dlg, dlg->h_entry, dlg->h_id,
dlg->callid.len, dlg->callid.s,
dlg_leg_print_info(dlg, DLG_CALLER_LEG, tag),
dlg_leg_print_info(dlg, callee_idx(dlg), tag));
} else if (ret == 0)
/* successfully removed from timer list */
unref++;
if (dlg_db_mode != DB_MODE_SHUTDOWN) {
dlg->flags &= ~DLG_FLAG_NEW;
remove_dialog_from_db(dlg);
dlg->flags |= DLG_FLAG_DB_DELETED;
}
if (dlg_db_mode == DB_MODE_DELAYED)
unref++;
next_dlg = dlg->next;
unref_dlg_unsafe(dlg, unref, &d_table->entries[i]);
dlg = next_dlg;
}
dlg_unlock(d_table, &d_table->entries[i]);
}
} else if (ev == CLUSTER_NODE_UP) {
lock_start_sw_read(shtags_lock);
for (tag = *shtags_list; tag; tag = tag->next) {
if (!tag->send_active_msg)
continue;
/* send sharing tag active msg to nodes to which we didn't already */
for (ni = tag->active_msgs_sent; ni && ni->node_id != node_id;
ni = ni->next) ;
if (!ni) {
if (send_shtag_active_info(&tag->name, node_id) < 0) {
LM_ERR("Failed to send info about sharing tag\n");
continue;
}
ni = shm_malloc(sizeof *ni);
if (!ni) {
LM_ERR("No more shm memory!\n");
return;
}
ni->node_id = node_id;
ni->next = tag->active_msgs_sent;
lock_switch_write(shtags_lock, lock_old_flag);
tag->active_msgs_sent = ni;
lock_switch_read(shtags_lock, lock_old_flag);
}
}
lock_stop_sw_read(shtags_lock);
}
}
int repl_prof_buffer_th = DLG_REPL_PROF_BUF_THRESHOLD;
int repl_prof_utimer = DLG_REPL_PROF_TIMER;
int repl_prof_timer_check = DLG_REPL_PROF_TIMER;
int repl_prof_timer_expire = DLG_REPL_PROF_EXPIRE_TIMER;
static void broadcast_profiles(utime_t ticks, void *param);