File: dlg_timer.c

package info (click to toggle)
opensips 2.2.2-3.1
  • links: PTS
  • area: main
  • in suites: sid
  • size: 31,160 kB
  • sloc: ansic: 334,348; xml: 91,231; perl: 6,659; sh: 5,148; sql: 4,175; makefile: 3,167; yacc: 2,499; python: 1,197; cpp: 611; php: 573
file content (1033 lines) | stat: -rw-r--r-- 24,567 bytes parent folder | download | duplicates (2)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
/*
 * Copyright (C) 2006 Voice System SRL
 *
 * 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:
 * --------
 * 2006-04-14  initial version (bogdan)
 * 2007-03-06  to avoid races, tests on timer links are done under locks
 *             (bogdan)
 */


#include "../../mem/shm_mem.h"
#include "../../timer.h"
#include "dlg_timer.h"
#include "dlg_hash.h"
#include "dlg_req_within.h"

struct dlg_timer *d_timer = 0;
dlg_timer_handler timer_hdl = 0;

struct dlg_ping_timer *ping_timer=0;
struct dlg_reinvite_ping_timer *reinvite_ping_timer=0;
str options_str=str_init("OPTIONS");
str invite_str=str_init("INVITE");

/* for the dlg timer, there are 3 possible states :
 * prev=next=0 -> dialog not in timer list
 * prev=0 -> dialog expired
 * otherwise - dialog still in timer list
 */
#define FAKE_DIALOG_TL ((struct dlg_tl*)-1)

int init_dlg_timer( dlg_timer_handler hdl )
{
	d_timer = (struct dlg_timer*)shm_malloc(sizeof(struct dlg_timer));
	if (d_timer==0) {
		LM_ERR("no more shm mem\n");
		return -1;
	}
	memset( d_timer, 0, sizeof(struct dlg_timer) );

	d_timer->first.next = d_timer->first.prev = &(d_timer->first);

	d_timer->lock = lock_alloc();
	if (d_timer->lock==0) {
		LM_ERR("failed to alloc lock\n");
		goto error0;
	}

	if (lock_init(d_timer->lock)==0) {
		LM_ERR("failed to init lock\n");
		goto error1;
	}

	timer_hdl = hdl;
	return 0;
error1:
	lock_dealloc(d_timer->lock);
error0:
	shm_free(d_timer);
	d_timer = 0;
	return -1;
}

#ifdef EXTRA_DEBUG
#define tl_get_dlg(_tl_)  ((struct dlg_cell*)((char *)(_tl_)- \
		(unsigned long)(&((struct dlg_cell*)0)->tl)))
void debug_detached_timer_list(struct dlg_tl *detached)
{
	struct dlg_cell *dlg;

	LM_DBG("Debugging detached timer list\n");

	/* check the detached list is not circular */
	while (detached != FAKE_DIALOG_TL) {
		if (detached->prev != NULL || detached->visited==1) {
			dlg = tl_get_dlg(detached);
			LM_ERR("Detected something wrong with dialog %p [%.*s]. Aborting. Visited = %d \n",
					dlg,dlg->callid.len,dlg->callid.s,detached->visited);
			abort();
		}
		detached->visited = 1;
		detached = detached->next;
	}

}

/* assumed to be always called under timer lock */
void debug_main_timer_list(void)
{
	struct dlg_tl *start,*finish;
	int visited=1;

	start = finish = &(d_timer->first);
	LM_DBG("testing forward loop with visited = %d\n",visited);

	/* check the main list is circular in both directions from start to end,
	 * with no loops in the middle */
	while (start) {
		start->visited=visited;
		start = start->next;

		if (start == finish)
			break;

		if (start == NULL || start->visited == visited) {
			LM_ERR("Detected something wrong with main timer list on forward linking for entry %p \n",start);
			abort();
		}
	}

	visited++;
	start = &(d_timer->first);

	LM_DBG("testing backward loop with visited = %d\n",visited);

	while (start) {
		start->visited=visited;
		start = start->prev;

		if (start == finish)
			break;

		if (start == NULL || start->visited == visited) {
			LM_ERR("Detected something wrong with main timer list on backward linking for entry %p \n",start);
			abort();
		}
	}
}

#endif

int init_dlg_ping_timer(void)
{
	ping_timer = (struct dlg_ping_timer*)shm_malloc(sizeof(struct dlg_timer));
	if (ping_timer==0) {
		LM_ERR("no more shm mem\n");
		return -1;
	}

	memset(ping_timer,0,sizeof(struct dlg_ping_timer));
	ping_timer->lock = lock_alloc();
	if (ping_timer->lock == 0) {
		LM_ERR("failed to alloc lock\n");
		goto error0;
	}

	if (lock_init(ping_timer->lock) == 0) {
		LM_ERR("failed to init lock\n");
		goto error1;
	}

	return 0;

error1:
	lock_dealloc(ping_timer->lock);
error0:
	shm_free(ping_timer);
	ping_timer=0;
	return -1;
}

int init_dlg_reinvite_ping_timer(void)
{
	reinvite_ping_timer = (struct dlg_reinvite_ping_timer*)shm_malloc(sizeof(struct dlg_timer));
	if (reinvite_ping_timer==0) {
		LM_ERR("no more shm mem\n");
		return -1;
	}

	memset(reinvite_ping_timer,0,sizeof(struct dlg_reinvite_ping_timer));
	reinvite_ping_timer->lock = lock_alloc();
	if (reinvite_ping_timer->lock == 0) {
		LM_ERR("failed to alloc lock\n");
		goto error0;
	}

	if (lock_init(reinvite_ping_timer->lock) == 0) {
		LM_ERR("failed to init lock\n");
		goto error1;
	}

	return 0;

error1:
	lock_dealloc(reinvite_ping_timer->lock);
error0:
	shm_free(reinvite_ping_timer);
	reinvite_ping_timer=0;
	return -1;
}

void destroy_ping_timer(void)
{
	if (ping_timer ==0)
		return;

	lock_destroy(ping_timer->lock);
	lock_dealloc(ping_timer->lock);

	shm_free(ping_timer);
	ping_timer=0;
}


void destroy_dlg_timer(void)
{
	if (d_timer==0)
		return;

	lock_destroy(d_timer->lock);
	lock_dealloc(d_timer->lock);

	shm_free(d_timer);
	d_timer = 0;
}



static inline void insert_dlg_timer_unsafe(struct dlg_tl *tl)
{
	struct dlg_tl* ptr;

#ifdef EXTRA_DEBUG
	debug_main_timer_list();
#endif

	for(ptr = d_timer->first.prev; ptr != &d_timer->first ; ptr = ptr->prev) {
		if ( ptr->timeout <= tl->timeout )
			break;
	}

	LM_DBG("inserting %p for %d\n", tl,tl->timeout);
	tl->prev = ptr;
	tl->next = ptr->next;
	tl->prev->next = tl;
	tl->next->prev = tl;

#ifdef EXTRA_DEBUG
	debug_main_timer_list();
#endif
}

int insert_dlg_timer(struct dlg_tl *tl, int interval)
{
	lock_get( d_timer->lock);

	if (tl->next!=0 || tl->prev!=0) {
		lock_release( d_timer->lock);
		LM_CRIT("Trying to insert a bogus dlg tl=%p tl->next=%p tl->prev=%p\n",
			tl, tl->next, tl->prev);
		return -1;
	}
	tl->timeout = get_ticks()+interval;

	insert_dlg_timer_unsafe( tl );

	lock_release( d_timer->lock);

	return 0;
}

int insert_ping_timer(struct dlg_cell* dlg)
{
	struct dlg_ping_list *node;

	node = shm_malloc(sizeof(struct dlg_ping_list));
	if (node == 0) {
		LM_ERR("no more shm mem\n");
		return -1;
	}

	node->dlg = dlg;
	node->next = 0;
	node->prev = 0;

	lock_get( ping_timer->lock );

	dlg->pl = node;

	if (ping_timer->first == 0)
		ping_timer->first = node;
	else {
		node->next = ping_timer->first;
		ping_timer->first->prev = node;
		ping_timer->first = node;
	}

	dlg->legs[DLG_CALLER_LEG].reply_received = 1;
	dlg->legs[callee_idx(dlg)].reply_received = 1;


	lock_release( ping_timer->lock);
	LM_DBG("Inserted dlg [%p] in ping timer list\n",dlg);

	return 0;
}

int insert_reinvite_ping_timer(struct dlg_cell* dlg)
{
	struct dlg_ping_list *node;

	node = shm_malloc(sizeof(struct dlg_ping_list));
	if (node == 0) {
		LM_ERR("no more shm mem\n");
		return -1;
	}

	node->dlg = dlg;
	node->next = 0;
	node->prev = 0;

	lock_get( reinvite_ping_timer->lock );

	dlg->reinvite_pl = node;

	if (reinvite_ping_timer->first == 0)
		reinvite_ping_timer->first = node;
	else {
		node->next = reinvite_ping_timer->first;
		reinvite_ping_timer->first->prev = node;
		reinvite_ping_timer->first = node;
	}

	dlg->legs[DLG_CALLER_LEG].reinvite_confirmed = 1;
	dlg->legs[callee_idx(dlg)].reinvite_confirmed = 1;

	lock_release( reinvite_ping_timer->lock);
	LM_DBG("Inserted dlg [%p] in reinvite ping timer list\n",dlg);

	return 0;
}

static inline void remove_dlg_timer_unsafe(struct dlg_tl *tl)
{
#ifdef EXTRA_DEBUG
	debug_main_timer_list();
#endif

	tl->prev->next = tl->next;
	tl->next->prev = tl->prev;

#ifdef EXTRA_DEBUG
	debug_main_timer_list();
#endif
}



/* returns:
      0 - dialog OK and removed from timer list
      1 - dialog OK but not found in timer list
     -1 - dialog not OK
 */
int remove_dlg_timer(struct dlg_tl *tl)
{
	lock_get( d_timer->lock);

	if (tl->prev==NULL && tl->timeout==0) {
		/* dialog is not in timer list; either it is completly removed
		   (prev=next=timeout=0), either is in process by timeout routine
		   (prev=timeout=0;next!=0) */
		lock_release( d_timer->lock);
		return 1;
	}

	if (tl->prev==NULL || tl->next==NULL || tl->next == FAKE_DIALOG_TL) {
		LM_CRIT("bogus tl=%p tl->prev=%p tl->next=%p\n",
			tl, tl->prev, tl->next);
		lock_release( d_timer->lock);
		return -1;
	}

	remove_dlg_timer_unsafe(tl);
	/* mark that this dialog was one a part of the timer list */
	tl->next = FAKE_DIALOG_TL;
	tl->prev = NULL;
	tl->timeout = 0;

	lock_release( d_timer->lock);
	return 0;
}

static inline void detach_ping_node_unsafe(struct dlg_ping_list *it,int reinvite)
{
	if (it->next && it->prev) {
		it->prev->next = it->next;
		it->next->prev = it->prev;
	}
	else if (it->next) {
		it->next->prev = 0;
		if (reinvite)
			reinvite_ping_timer->first = it->next;
		else
			ping_timer->first = it->next;
	} else if (it->prev) {
		it->prev->next = 0;
	} else {
		if (reinvite)
			reinvite_ping_timer->first = 0;
		else
			ping_timer->first = 0;
	}

	it->next = it->prev = 0;
}

/* returns :
     0 - dialog was inserted in timer list with the new timeout
    -1 - failure (dialog is expired, so it cannot be added again) */
int update_dlg_timer( struct dlg_tl *tl, int timeout )
{
	int ret;

	lock_get( d_timer->lock);

	if ( tl->next == FAKE_DIALOG_TL ) {
		/* previously removed from timer list - we will not add it again */
		lock_release( d_timer->lock);
		return 0;
	}

	if ( tl->next ) {
		if (tl->prev==0) {
			lock_release( d_timer->lock);
			return -1;
		}
		remove_dlg_timer_unsafe(tl);
		ret = 0;
	} else {
		ret = 1;
	}

	tl->timeout = get_ticks()+timeout;
	insert_dlg_timer_unsafe( tl );

	lock_release( d_timer->lock);
	return ret;
}

static inline struct dlg_tl* get_expired_dlgs(unsigned int time)
{
	struct dlg_tl *tl , *end, *ret;

	lock_get( d_timer->lock);

	if (d_timer->first.next==&(d_timer->first)
	|| d_timer->first.next->timeout > time ) {
		lock_release( d_timer->lock);
		return FAKE_DIALOG_TL;
	}

#ifdef EXTRA_DEBUG
	debug_main_timer_list();
#endif

	end = &d_timer->first;
	tl = d_timer->first.next;
	LM_DBG("start with tl=%p tl->prev=%p tl->next=%p (%d) at %d "
		"and end with end=%p end->prev=%p end->next=%p\n",
		tl,tl->prev,tl->next,tl->timeout,time,
		end,end->prev,end->next);
	while( tl!=end && tl->timeout <= time) {
		LM_DBG("getting tl=%p tl->prev=%p tl->next=%p with %d\n",
			tl,tl->prev,tl->next,tl->timeout);
		tl->prev = 0;
		tl->timeout = 0;
		tl=tl->next;
	}
	LM_DBG("end with tl=%p tl->prev=%p tl->next=%p and "
		"d_timer->first.next->prev=%p\n",
		tl,tl->prev,tl->next,d_timer->first.next->prev);

	if (tl==end && d_timer->first.next->prev) {
		LM_DBG("no dialog to return\n");
		ret = FAKE_DIALOG_TL;
	} else {
		ret = d_timer->first.next;
		tl->prev->next = FAKE_DIALOG_TL;
		d_timer->first.next = tl;
		tl->prev = &d_timer->first;
	}

#ifdef EXTRA_DEBUG
	debug_main_timer_list();
#endif

	lock_release( d_timer->lock);

#ifdef EXTRA_DEBUG
	debug_detached_timer_list(ret);
#endif
	return ret;
}

void dlg_timer_routine(unsigned int ticks , void * attr)
{
	struct dlg_tl *tl, *ctl;

	tl = get_expired_dlgs( ticks );

	while (tl != FAKE_DIALOG_TL) {
		ctl = tl;
		tl = tl->next;
		/* keep dialog as expired (next is still set) */
		ctl->next = FAKE_DIALOG_TL;
		LM_DBG("tl=%p next=%p\n", ctl, tl);
		timer_hdl( ctl );
	}
}

/* removes expired dlgs from main ping_timer list
 * and links them back into a new list */
void get_timeout_dlgs(struct dlg_ping_list **expired,
		struct dlg_ping_list **to_be_deleted,int reinvite)
{
	struct dlg_ping_list *exp = NULL,*del=NULL,*it=NULL,*next=NULL;
	struct dlg_cell *current;
	int detached;

	if (reinvite)
		lock_get(reinvite_ping_timer->lock);
	else
		lock_get(ping_timer->lock);

	for (it=reinvite?reinvite_ping_timer->first:ping_timer->first;it;it=next) {
		current = it->dlg;
		next = it->next;
		detached = 0;

		if (current->state == DLG_STATE_DELETED) {
			/* the dialog has terminated - we remove it as well
			 * since we also have a ref */
			detach_ping_node_unsafe(it,reinvite);
			if (reinvite)
				it->dlg->reinvite_pl = 0;
			else
				it->dlg->pl = 0;

			if (del == NULL)
				del = it;
			else {
				it->next = del;
				del = it;
			}

			continue;
		}

		if (reinvite?(current->flags & DLG_FLAG_REINVITE_PING_CALLER):
		(current->flags & DLG_FLAG_PING_CALLER)) {
			if (reinvite?(current->legs[DLG_CALLER_LEG].reinvite_confirmed == 0):
			(current->legs[DLG_CALLER_LEG].reply_received == 0)) {
				detach_ping_node_unsafe(it,reinvite);
				detached=1;

				if (reinvite)
					it->dlg->reinvite_pl = 0;
				else
					it->dlg->pl = 0;

				if (exp == NULL)
					exp = it;
				else {
					it->next = exp;
					exp = it;
				}
			}
		}

		if (detached == 0) {
			if (reinvite?(current->flags & DLG_FLAG_REINVITE_PING_CALLEE):
			(current->flags & DLG_FLAG_PING_CALLEE)) {
				if (reinvite?(current->legs[callee_idx(current)].reinvite_confirmed == 0):
				current->legs[callee_idx(current)].reply_received == 0) {
					detach_ping_node_unsafe(it,reinvite);
					if (reinvite)
						it->dlg->reinvite_pl = 0;
					else
						it->dlg->pl = 0;


					if (exp == NULL)
						exp = it;
					else {
						it->next = exp;
						exp = it;
					}
				}
			}
		}
	}

	if (reinvite)
		lock_release(reinvite_ping_timer->lock);
	else
		lock_release(ping_timer->lock);

	*to_be_deleted = del;
	*expired = exp;
}

void reply_from_caller(struct cell* t, int type, struct tmcb_params* ps)
{
	struct sip_msg *rpl;
	int statuscode;
	struct dlg_cell *dlg;

	if(ps == NULL || ps->rpl == NULL)
	{
			LM_ERR("Wrong tmcb params\n");
			return;
	}
	if( ps->param== NULL )
	{
			LM_ERR("Null callback parameter\n");
			return;
	}

	rpl = ps->rpl;
	statuscode = ps->code;
	dlg = *(ps->param);

	LM_DBG("Status Code received =  [%d]\n", statuscode);

	if (rpl == FAKED_REPLY || statuscode == 408) {
		/* timeout occurred, nothing else to do now
		 * next time timer fires, it will detect ping reply was not received
		 */
		LM_INFO("terminating dialog ( due to timeout ) "
					"with callid = [%.*s] \n",dlg->callid.len,dlg->callid.s);
		return;
	}

	if (statuscode == 481)
	{
		/* call/transaction does not exist
		 * terminate the dialog */
		LM_INFO("terminating dialog ( due to 481 ) "
				"with callid = [%.*s] \n",dlg->callid.len,dlg->callid.s);

		return;
	}

	dlg->legs[DLG_CALLER_LEG].reply_received = 1;
}

void reinvite_reply_from_caller(struct cell* t, int type, struct tmcb_params* ps)
{
	struct sip_msg *rpl;
	int statuscode;
	struct dlg_cell *dlg;

	if(ps == NULL || ps->rpl == NULL)
	{
			LM_ERR("Wrong tmcb params\n");
			return;
	}
	if( ps->param== NULL )
	{
			LM_ERR("Null callback parameter\n");
			return;
	}

	rpl = ps->rpl;
	statuscode = ps->code;
	dlg = *(ps->param);

	LM_DBG("Status Code received =  [%d]\n", statuscode);

	if (rpl == FAKED_REPLY || statuscode == 408) {
		/* timeout occurred, nothing else to do now
		 * next time timer fires, it will detect ping reply was not received
		 */
		LM_INFO("terminating dialog ( due to timeout ) "
					"with callid = [%.*s] \n",dlg->callid.len,dlg->callid.s);
		return;
	}

	if (statuscode == 481)
	{
		/* call/transaction does not exist
		 * terminate the dialog */
		LM_INFO("terminating dialog ( due to 481 ) "
				"with callid = [%.*s] \n",dlg->callid.len,dlg->callid.s);

		return;
	}

	dlg->legs[DLG_CALLER_LEG].reinvite_confirmed = 1;
}

/* Duplicate code for the sake of quickly knowing where the reply came from,
 * without any further checks */
void reply_from_callee(struct cell* t, int type, struct tmcb_params* ps)
{
	struct sip_msg *rpl;
	int statuscode;
	struct dlg_cell *dlg;

	if(ps == NULL || ps->rpl == NULL)
	{
			LM_ERR("Wrong tmcb params\n");
			return;
	}
	if( ps->param== NULL )
	{
			LM_ERR("Null callback parameter\n");
			return;
	}

	rpl = ps->rpl;
	statuscode = ps->code;
	dlg = *(ps->param);

	LM_DBG("Status Code received =  [%d]\n", statuscode);

	if (rpl == FAKED_REPLY || statuscode == 408) {
		/* timeout occurred, nothing else to do now
		 * next time timer fires, it will detect ping reply was not received
		 */
		LM_INFO("terminating dialog ( due to timeout ) "
					"with callid = [%.*s] \n",dlg->callid.len,dlg->callid.s);
		return;
	}

	if (statuscode == 481)
	{
		/* call/transaction does not exist
		 * terminate the dialog */
		LM_INFO("terminating dialog ( due to 481 ) "
				"with callid = [%.*s] \n",dlg->callid.len,dlg->callid.s);
		return;
	}

	dlg->legs[callee_idx(dlg)].reply_received = 1;
}

/* Duplicate code for the sake of quickly knowing where the reply came from,
 * without any further checks */
void reinvite_reply_from_callee(struct cell* t, int type, struct tmcb_params* ps)
{
	struct sip_msg *rpl;
	int statuscode;
	struct dlg_cell *dlg;

	if(ps == NULL || ps->rpl == NULL)
	{
			LM_ERR("Wrong tmcb params\n");
			return;
	}
	if( ps->param== NULL )
	{
			LM_ERR("Null callback parameter\n");
			return;
	}

	rpl = ps->rpl;
	statuscode = ps->code;
	dlg = *(ps->param);

	LM_DBG("Status Code received =  [%d]\n", statuscode);

	if (rpl == FAKED_REPLY || statuscode == 408) {
		/* timeout occurred, nothing else to do now
		 * next time timer fires, it will detect ping reply was not received
		 */
		LM_INFO("terminating dialog ( due to timeout ) "
					"with callid = [%.*s] \n",dlg->callid.len,dlg->callid.s);
		return;
	}

	if (statuscode == 481)
	{
		/* call/transaction does not exist
		 * terminate the dialog */
		LM_INFO("terminating dialog ( due to 481 ) "
				"with callid = [%.*s] \n",dlg->callid.len,dlg->callid.s);
		return;
	}

	dlg->legs[callee_idx(dlg)].reinvite_confirmed = 1;
}

void unref_dlg_cb(void *dlg)
{
	unref_dlg_destroy_safe((struct dlg_cell*)dlg,1);
}

void dlg_options_routine(unsigned int ticks , void * attr)
{
	struct dlg_ping_list *expired,*to_be_deleted,*it,*curr;
	struct dlg_cell *dlg;

	get_timeout_dlgs(&expired,&to_be_deleted,0);

	it = expired;
	while (it) {
		dlg = it->dlg;
		LM_DBG("dialog %p-%.*s has expired\n",dlg,dlg->callid.len,dlg->callid.s);
		curr = it->next;
		shm_free(it);
		it = curr;

		init_dlg_term_reason(dlg,"Ping Timeout",sizeof("Ping Timeout")-1);
		/* FIXME - maybe better not to send BYE both ways as we know for
		 * sure one end in down . */
		dlg_end_dlg(dlg,0);

		/* no longer reffed in list */
		unref_dlg(dlg,1);
	}

	it = to_be_deleted;
	while (it) {
		dlg = it->dlg;
		LM_DBG("dialog %p-%.*s has terminated\n",dlg,dlg->callid.len,dlg->callid.s);
		curr = it->next;
		/* if marked as to be deleted, we let it go
		 * for the ping timer list as well */
		unref_dlg(dlg,1);
		shm_free(it);
		it = curr;
	}

	tcp_no_new_conn = 1;

	/* ping_timer->first now contains all active dialogs */
	it = ping_timer->first;
	while (it) {
		dlg = it->dlg;

		/* do not ping ended dialogs - we might have missed them earlier or
		 * might have terminated in the mean time - we'll clean them up on
		 * our next iteration */
		if (dlg->state != DLG_STATE_DELETED) {
			if (dlg->flags & DLG_FLAG_PING_CALLER) {
				ref_dlg(dlg,1);
				if (send_leg_msg(dlg,&options_str,callee_idx(dlg),
				DLG_CALLER_LEG,0,0,reply_from_caller,dlg,unref_dlg_cb,
				&dlg->legs[DLG_CALLER_LEG].reply_received) < 0) {
					LM_ERR("failed to ping caller\n");
					unref_dlg(dlg,1);
				}
			}

			if (dlg->flags & DLG_FLAG_PING_CALLEE) {
				ref_dlg(dlg,1);
				if (send_leg_msg(dlg,&options_str,DLG_CALLER_LEG,
				callee_idx(dlg),0,0,reply_from_callee,dlg,unref_dlg_cb,
				&dlg->legs[callee_idx(dlg)].reply_received) < 0) {
					LM_ERR("failed to ping callee\n");
					unref_dlg(dlg,1);
				}
			}
		}
		it = it->next;
	}

	tcp_no_new_conn = 0;
}

#define CONTACT_STR_START "Contact: <"
#define CONTACT_STR_START_LEN (sizeof(CONTACT_STR_START)-1)

#define HEADERS_STR_END_NOCRLF "Content-Type: application/sdp\r\n"
#define HEADERS_STR_END_NOCRLF_LEN (sizeof(HEADERS_STR_END_NOCRLF)-1)

#define HEADERS_STR_END ">\r\nContent-Type: application/sdp\r\n"
#define HEADERS_STR_END_LEN (sizeof(HEADERS_STR_END)-1)

void dlg_reinvite_routine(unsigned int ticks , void * attr)
{
	struct dlg_ping_list *expired,*to_be_deleted,*it,*curr;
	struct dlg_cell *dlg;
	str extra_headers;
	char *p;

	get_timeout_dlgs(&expired,&to_be_deleted,1);

	it = expired;
	while (it) {
		dlg = it->dlg;
		LM_DBG("dialog %p-%.*s has expired\n",dlg,dlg->callid.len,dlg->callid.s);
		curr = it->next;
		shm_free(it);
		it = curr;

		init_dlg_term_reason(dlg,"ReINVITE Ping Timeout",sizeof("ReINVITE Ping Timeout")-1);
		/* FIXME - maybe better not to send BYE both ways as we know for
		 * sure one end in down . */
		dlg_end_dlg(dlg,0);

		/* no longer reffed in list */
		unref_dlg(dlg,1);
	}

	it = to_be_deleted;
	while (it) {
		dlg = it->dlg;
		LM_DBG("dialog %p-%.*s has terminated\n",dlg,dlg->callid.len,dlg->callid.s);
		curr = it->next;
		/* if marked as to be deleted, we let it go
		 * for the ping timer list as well */
		unref_dlg(dlg,1);
		shm_free(it);
		it = curr;
	}

	tcp_no_new_conn = 1;

	/* reinvite_ping_timer->first now contains all active dialogs */
	it = reinvite_ping_timer->first;
	while (it) {
		dlg = it->dlg;

		/* do not ping ended dialogs - we might have missed them earlier or
		 * might have terminated in the mean time - we'll clean them up on
		 * our next iteration */
		if (dlg->state != DLG_STATE_DELETED) {
			if (dlg->flags & DLG_FLAG_REINVITE_PING_CALLER) {
				if (dlg->legs[callee_idx(dlg)].th_sent_contact.len)
					extra_headers.len = 
					dlg->legs[callee_idx(dlg)].th_sent_contact.len +
					HEADERS_STR_END_NOCRLF_LEN;
				else
					extra_headers.len = CONTACT_STR_START_LEN +
						dlg->legs[callee_idx(dlg)].contact.len +
						HEADERS_STR_END_LEN;
				extra_headers.s = pkg_malloc(extra_headers.len);
				if (!extra_headers.s) {
					LM_ERR("No more pkg for extra headers \n");
					it = it->next;
					return;
				}
						
				if (dlg->legs[callee_idx(dlg)].th_sent_contact.len) {
					p = extra_headers.s;
					memcpy(p,dlg->legs[callee_idx(dlg)].th_sent_contact.s,
					dlg->legs[callee_idx(dlg)].th_sent_contact.len);

					p+= dlg->legs[callee_idx(dlg)].th_sent_contact.len;
					memcpy(p,HEADERS_STR_END_NOCRLF,HEADERS_STR_END_NOCRLF_LEN);
				} else {
					p = extra_headers.s;
					memcpy(p,CONTACT_STR_START,CONTACT_STR_START_LEN);
					p += CONTACT_STR_START_LEN;
					memcpy(p,dlg->legs[callee_idx(dlg)].contact.s,
					dlg->legs[callee_idx(dlg)].contact.len);

					p += dlg->legs[callee_idx(dlg)].contact.len;
					memcpy(p,HEADERS_STR_END,HEADERS_STR_END_LEN);
				}
				
				ref_dlg(dlg,1);
				if (send_leg_msg(dlg,&invite_str,callee_idx(dlg),
				DLG_CALLER_LEG,&extra_headers,&dlg->legs[callee_idx(dlg)].sdp,
				reinvite_reply_from_caller,dlg,unref_dlg_cb,
				&dlg->legs[DLG_CALLER_LEG].reinvite_confirmed) < 0) {
					LM_ERR("failed to ping caller\n");
					unref_dlg(dlg,1);
				}

				pkg_free(extra_headers.s);
			}

			if (dlg->flags & DLG_FLAG_REINVITE_PING_CALLEE) {
				if (dlg->legs[DLG_CALLER_LEG].th_sent_contact.len)
					extra_headers.len = 
					dlg->legs[DLG_CALLER_LEG].th_sent_contact.len +
					HEADERS_STR_END_NOCRLF_LEN;
				else
					extra_headers.len = CONTACT_STR_START_LEN +
							dlg->legs[DLG_CALLER_LEG].contact.len +
							HEADERS_STR_END_LEN;
				extra_headers.s = pkg_malloc(extra_headers.len);
				if (!extra_headers.s) {
					LM_ERR("No more pkg for extra headers \n");
					it = it->next;
					return ;
				}
						
				if (dlg->legs[DLG_CALLER_LEG].th_sent_contact.len) {
					p = extra_headers.s;
					memcpy(extra_headers.s,
					dlg->legs[DLG_CALLER_LEG].th_sent_contact.s,
					dlg->legs[DLG_CALLER_LEG].th_sent_contact.len);

					p+= dlg->legs[DLG_CALLER_LEG].th_sent_contact.len;
					memcpy(p,HEADERS_STR_END_NOCRLF,HEADERS_STR_END_NOCRLF_LEN);
				} else {
					p = extra_headers.s;
					memcpy(p,CONTACT_STR_START,CONTACT_STR_START_LEN);
					p += CONTACT_STR_START_LEN;
					memcpy(p,dlg->legs[DLG_CALLER_LEG].contact.s,
					dlg->legs[DLG_CALLER_LEG].contact.len);

					p += dlg->legs[DLG_CALLER_LEG].contact.len;
					memcpy(p,HEADERS_STR_END,HEADERS_STR_END_LEN);
				}

				ref_dlg(dlg,1);
				if (send_leg_msg(dlg,&invite_str,DLG_CALLER_LEG,
				callee_idx(dlg),&extra_headers,&dlg->legs[DLG_CALLER_LEG].sdp,reinvite_reply_from_callee,
				dlg,unref_dlg_cb,&dlg->legs[callee_idx(dlg)].reinvite_confirmed) < 0) {
					LM_ERR("failed to ping callee\n");
					unref_dlg(dlg,1);
				}

				pkg_free(extra_headers.s);
			}
		}
		it = it->next;
	}

	tcp_no_new_conn = 0;
}