summaryrefslogtreecommitdiffstats
path: root/tinySIP/src/dialogs/tsip_dialog_layer.c
blob: 9a33fd39c64bd21807934acaa51a00a209bdaafd (plain)
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
/*
* Copyright (C) 2010-2011 Mamadou Diop.
*
* Contact: Mamadou Diop <diopmamadou(at)doubango[dot]org>
*	
* This file is part of Open Source Doubango Framework.
*
* DOUBANGO 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 3 of the License, or
* (at your option) any later version.
*	
* DOUBANGO 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 DOUBANGO.
*
*/

/**@file tsip_dialog_layer.c
 * @brief SIP dialog layer.
 *
 * @author Mamadou Diop <diopmamadou(at)doubango[dot]org>
 *

 */
#include "tinysip/dialogs/tsip_dialog_layer.h"

#include "tinysip/dialogs/tsip_dialog_invite.h"
#include "tinysip/dialogs/tsip_dialog_message.h"
#include "tinysip/dialogs/tsip_dialog_info.h"
#include "tinysip/dialogs/tsip_dialog_options.h"
#include "tinysip/dialogs/tsip_dialog_publish.h"
#include "tinysip/dialogs/tsip_dialog_register.h"
#include "tinysip/dialogs/tsip_dialog_subscribe.h"

#include "tinysip/transactions/tsip_transac_layer.h"
#include "tinysip/transports/tsip_transport_layer.h"

#include "tsk_debug.h"

extern tsip_ssession_handle_t *tsip_ssession_create_2(const tsip_stack_t* stack, const struct tsip_message_s* message);

/*== Predicate function to find dialog by type */
static int pred_find_dialog_by_type(const tsk_list_item_t *item, const void *type)
{
	if(item && item->data){
		tsip_dialog_t *dialog = item->data;
		return (dialog->type - *((tsip_dialog_type_t*)type));
	}
	return -1;
}

/*== Predicate function to find dialog by not type */
static int pred_find_dialog_by_not_type(const tsk_list_item_t *item, const void *type)
{
	if(item && item->data){
		tsip_dialog_t *dialog = item->data;
		if(dialog->type != *((tsip_dialog_type_t*)type)){
			return 0;
		}
	}
	return -1;
}

/*== Predicate function to find dialog by callid */
static int pred_find_dialog_by_callid(const tsk_list_item_t *item, const void *callid)
{
	if(item && item->data && callid){
		return tsk_strcmp(((tsip_dialog_t*)item->data)->callid, ((const char*)callid));
	}
	return -1;
}

tsip_dialog_layer_t* tsip_dialog_layer_create(tsip_stack_t* stack)
{
	return tsk_object_new(tsip_dialog_layer_def_t, stack);
}

// it's up to the caller to release the returned object
tsip_dialog_t* tsip_dialog_layer_find_by_ss(tsip_dialog_layer_t *self, const tsip_ssession_handle_t *ss)
{
	return tsip_dialog_layer_find_by_ssid(self, tsip_ssession_get_id(ss));
}

// it's up to the caller to release the returned object
tsip_dialog_t* tsip_dialog_layer_find_by_ssid(tsip_dialog_layer_t *self, tsip_ssession_id_t ssid)
{
	tsip_dialog_t *ret = 0;
	tsip_dialog_t *dialog;
	tsk_list_item_t *item;

	tsk_safeobj_lock(self);

	tsk_list_foreach(item, self->dialogs){
		dialog = item->data;
		if(tsip_ssession_get_id(dialog->ss) == ssid){
			ret = dialog;
			break;
		}
	}

	tsk_safeobj_unlock(self);

	return tsk_object_ref(ret);
}

// it's up to the caller to release the returned object
tsip_dialog_t* tsip_dialog_layer_find_by_callid(tsip_dialog_layer_t *self, const char* callid)
{
	if(!self || !callid){
		TSK_DEBUG_ERROR("Invalid parameter");
		return tsk_null;
	}
	else{
		tsip_dialog_t *dialog = tsk_null;
		tsk_list_item_t *item;
		//--tsk_safeobj_lock(self);
		tsk_list_foreach(item, self->dialogs){
			if(tsk_striequals(TSIP_DIALOG(item->data)->callid, callid)){
				dialog = tsk_object_ref(item->data);
				break;
			}
		}
		//--tsk_safeobj_unlock(self);
		return dialog;
	}
}

tsk_bool_t tsip_dialog_layer_have_dialog_with_callid(const tsip_dialog_layer_t *self, const char* callid)
{
	tsk_bool_t found = tsk_false;
	if(self){
		tsk_safeobj_lock(self);
		if(tsk_list_find_item_by_pred(self->dialogs, pred_find_dialog_by_callid, callid) != tsk_null){
			found = tsk_true;
		}
		tsk_safeobj_unlock(self);
	}
	return found;
}

// it's up to the caller to release the returned object
tsip_dialog_t* tsip_dialog_layer_find(const tsip_dialog_layer_t *self, const char* callid, const char* to_tag, const char* from_tag, tsip_request_type_t type, tsk_bool_t *cid_matched)
{
	tsip_dialog_t *ret = tsk_null;
	tsip_dialog_t *dialog;
	tsk_list_item_t *item;

	*cid_matched = tsk_false;
	
	tsk_safeobj_lock(self);

	tsk_list_foreach(item, self->dialogs){
		dialog = item->data;
		if(tsk_strequals(dialog->callid, callid)){
			tsk_bool_t is_cancel = (type == tsip_CANCEL); // Incoming CANCEL
			tsk_bool_t is_register = (type == tsip_REGISTER); // Incoming REGISTER
			tsk_bool_t is_notify = (type == tsip_NOTIFY); // Incoming NOTIFY
			*cid_matched = tsk_true;
			/* CANCEL Request will have the same local tag than the INVITE request
				the remote tag could be null if the CANCEL request is received immediately after a 100 Trying
			*/
			if((is_cancel || tsk_strequals(dialog->tag_local, from_tag)) && (!dialog->tag_remote || tsk_strequals(dialog->tag_remote, to_tag))){
				ret = tsk_object_ref(dialog);
				break;
			}
			/* REGISTER is dialogless which means that each reREGISTER or unREGISTER will have empty to tag  */
			if(is_register /* Do not check tags */){
				ret = tsk_object_ref(dialog);
				break;
			}
			/*	NOTIFY could arrive before the 200 SUBSCRIBE => This is why we don't try to match both tags
			 
				RFC 3265 - 3.1.4.4. Confirmation of Subscription Creation
				Due to the potential for both out-of-order messages and forking, the
				subscriber MUST be prepared to receive NOTIFY messages before the
				SUBSCRIBE transaction has completed.
			 */
			if(is_notify /* Do not check tags */){
				ret = tsk_object_ref(dialog);
				break;
			}
		}
	}

	tsk_safeobj_unlock(self);

	return ret;
}

tsk_size_t tsip_dialog_layer_count_active_calls(tsip_dialog_layer_t *self)
{
	tsk_size_t count = 0;

	tsip_dialog_t *dialog;
	tsk_list_item_t *item;

	tsk_safeobj_lock(self);

	tsk_list_foreach(item, self->dialogs) {
		if ((dialog = item->data) && dialog->type == tsip_dialog_INVITE && dialog->state != tsip_initial && dialog->state != tsip_terminated) {
			++count;
		}
	}

	tsk_safeobj_unlock(self);

	return count;
}

/** Hangup all dialogs starting by non-REGISTER  */
int tsip_dialog_layer_shutdownAll(tsip_dialog_layer_t *self)
{
	if(self){
		tsk_bool_t wait = tsk_false;
		tsk_list_item_t *item;
		tsip_dialog_t *dialog;
		tsip_dialog_type_t regtype = tsip_dialog_REGISTER;

		if(!self->shutdown.inprogress){
			self->shutdown.inprogress = tsk_true;
			if (!self->shutdown.condwait) {
				self->shutdown.condwait = tsk_condwait_create();
			}
		}
		
		tsk_safeobj_lock(self);
		if(tsk_list_count(self->dialogs, pred_find_dialog_by_not_type, &regtype)){
			/* There are non-register dialogs ==> phase-1 */
			goto phase1;
		}
		else if(tsk_list_count(self->dialogs, pred_find_dialog_by_type, &regtype)){
			/* There are one or more register dialogs ==> phase-2 */
			goto phase2;
		}
		else{
			tsk_safeobj_unlock(self);
			goto done;
		}

phase1:
		/* Phase 1 - shutdown all except register and silent_hangup */
		TSK_DEBUG_INFO("== Shutting down - Phase-1 started ==");
phase1_loop:
		tsk_list_foreach(item, self->dialogs){
			dialog = item->data;
			if(dialog->type != tsip_dialog_REGISTER && !dialog->ss->silent_hangup){
				item = tsk_object_ref(item);
				if(!tsip_dialog_shutdown(dialog, tsk_null)){
					wait = tsk_true;
				}

				// if "tsip_dialog_shutdown()" remove the dialog, then
				// "self->dialogs" will be unsafe
				if(!(item = tsk_object_unref(item))){
					goto phase1_loop;
				}
			}
		}
		tsk_safeobj_unlock(self);
		
		/* wait until phase-1 is completed */
		if(wait){
			tsk_condwait_timedwait(self->shutdown.condwait, TSIP_DIALOG_SHUTDOWN_TIMEOUT);
		}
		
		/* lock and goto phase2 */
		tsk_safeobj_lock(self);
		wait = tsk_false;
		goto phase2;

phase2:
		/* Phase 2 - unregister */
		TSK_DEBUG_INFO("== Shutting down - Phase-2 started ==");
		self->shutdown.phase2 = tsk_true;
phase2_loop:
		tsk_list_foreach(item, self->dialogs){
			dialog = item->data;
			if(dialog->type == tsip_dialog_REGISTER){
				item = tsk_object_ref(item);
				if(!tsip_dialog_shutdown(dialog, tsk_null)){
					wait = tsk_true;
				}
				// if "tsip_dialog_shutdown()" remove the dialog, then
				// "self->dialogs" will be unsafe 
				if(!(item = tsk_object_unref(item))){
					goto phase2_loop;
				}
			}
		}
		tsk_safeobj_unlock(self);

		/* wait until phase-2 is completed */
		if(wait){
			tsk_condwait_timedwait(self->shutdown.condwait, TSIP_DIALOG_SHUTDOWN_TIMEOUT);
		}


		/* Phase 3 - silenthangup (dialogs will be terminated immediately) */
		TSK_DEBUG_INFO("== Shutting down - Phase-3 ==");
phase3_loop:
		tsk_list_foreach(item, self->dialogs){
			dialog = item->data;
			if(dialog->ss->silent_hangup){
				item = tsk_object_ref(item);
				tsip_dialog_shutdown(dialog, tsk_null);

				// if "tsip_dialog_shutdown()" remove the dialog, then
				// "self->dialogs" will became unsafe while looping
				if(!(item = tsk_object_unref(item))){
					goto phase3_loop;
				}
			}
		}

done:
		TSK_DEBUG_INFO("== Shutting down - Terminated ==");
		return 0;
	}
	return -1;
}

static void* TSK_STDCALL _tsip_dialog_signal_transport_error_async(void* dialog)
{
    tsip_dialog_signal_transport_error(TSIP_DIALOG(dialog));
    return tsk_null;
}

int tsip_dialog_layer_signal_stack_disconnected(tsip_dialog_layer_t *self)
{
	tsk_list_item_t *item;
    // use copy  for lock-free code and faster code. also fix issue 172 (https://code.google.com/p/idoubs/issues/detail?id=172)
    tsip_dialogs_L_t *dialogs_copy;
    tsip_dialog_t *dialog;
	if(!self){
		TSK_DEBUG_ERROR("Invalid parameter");
		return -1;
	}
    
    if (!(dialogs_copy = tsk_list_create())) {
        TSK_DEBUG_ERROR("Failed to create list");
		return -1;
    }

	tsk_safeobj_lock(self);
    tsk_list_pushback_list(dialogs_copy, self->dialogs);
    tsk_safeobj_unlock(self);
    
    tsk_list_foreach(item, dialogs_copy){
        if((dialog = TSIP_DIALOG(item->data))){
            tsip_dialog_signal_transport_error(dialog);
        }
    }
    TSK_OBJECT_SAFE_FREE(dialogs_copy);
	return 0;
}

int tsip_dialog_layer_signal_peer_disconnected(tsip_dialog_layer_t *self, const struct tsip_transport_stream_peer_s* peer)
{
	tsip_dialog_t *dialog;
	const tsk_list_item_t *item;

	if(!self || !peer){
		TSK_DEBUG_ERROR("Invalid parameter");
		return -1;
	}
    
    //!\ must not lock the entire layer

	// tsk_safeobj_lock(self);

	tsk_list_lock(peer->dialogs_cids);
	tsk_list_foreach(item, peer->dialogs_cids){
		if((dialog = tsip_dialog_layer_find_by_callid(self, TSK_STRING_STR(item->data)))){
			tsip_dialog_signal_transport_error(dialog);
			TSK_OBJECT_SAFE_FREE(dialog);
		}
		else{
			// To avoid this WARN, you should call tsip_dialog_layer_have_dialog_with_callid() before adding a callid to a peer
			TSK_DEBUG_WARN("Stream peer holds call-id='%s' but the dialog layer doesn't know it", TSK_STRING_STR(item->data));
		}
	}
	tsk_list_unlock(peer->dialogs_cids);

	// tsk_safeobj_unlock(self);

	return 0;
}

int tsip_dialog_layer_remove_callid_from_stream_peers(tsip_dialog_layer_t *self, const char* callid)
{
	if(self){
		return tsip_transport_layer_remove_callid_from_stream_peers(self->stack->layer_transport, callid);
	}
	TSK_DEBUG_ERROR("Invalid parameter");
	return -1;
}

/* the caller of this function must unref() the returned object */
tsip_dialog_t* tsip_dialog_layer_new(tsip_dialog_layer_t *self, tsip_dialog_type_t type, const tsip_ssession_t *ss)
{
	tsip_dialog_t* ret = tsk_null;
	tsip_dialog_t* dialog;
	if(!self){
		TSK_DEBUG_ERROR("Invalid parameter");
		goto bail;
	}

	switch(type){
		case tsip_dialog_INVITE:
			{
				if((dialog = (tsip_dialog_t*)tsip_dialog_invite_create(ss, tsk_null))){
					ret = tsk_object_ref(dialog);
					tsk_list_push_back_data(self->dialogs, (void**)&dialog);
				}
				break;
			}
		case tsip_dialog_MESSAGE:
			{
				if((dialog = (tsip_dialog_t*)tsip_dialog_message_create(ss))){
					ret = tsk_object_ref(dialog);
					tsk_list_push_back_data(self->dialogs, (void**)&dialog);
				}
				break;
			}
		case tsip_dialog_INFO:
			{
				if((dialog = (tsip_dialog_t*)tsip_dialog_info_create(ss))){
					ret = tsk_object_ref(dialog);
					tsk_list_push_back_data(self->dialogs, (void**)&dialog);
				}
				break;
			}
		case tsip_dialog_OPTIONS:
			{
				if((dialog = (tsip_dialog_t*)tsip_dialog_options_create(ss))){
					ret = tsk_object_ref(dialog);
					tsk_list_push_back_data(self->dialogs, (void**)&dialog);
				}
				break;
			}
		case tsip_dialog_PUBLISH:
			{
				if((dialog = (tsip_dialog_t*)tsip_dialog_publish_create(ss))){
					ret = tsk_object_ref(dialog);
					tsk_list_push_back_data(self->dialogs, (void**)&dialog);
				}
				break;
			}
		case tsip_dialog_REGISTER:
			{
				if((dialog = (tsip_dialog_t*)tsip_dialog_register_create(ss, tsk_null))){
					ret = tsk_object_ref(dialog);
					tsk_list_push_back_data(self->dialogs, (void**)&dialog);
				}
				break;
			}
		case tsip_dialog_SUBSCRIBE:
			{
				if((dialog = (tsip_dialog_t*)tsip_dialog_subscribe_create(ss))){
					ret = tsk_object_ref(dialog);
					tsk_list_push_back_data(self->dialogs, (void**)&dialog);
				}
				break;
			}

		default:
			{
				TSK_DEBUG_ERROR("Dialog type not supported.");
				break;
			}
	}

bail:
	return ret;
}

int tsip_dialog_layer_remove(tsip_dialog_layer_t *self, const tsip_dialog_t *dialog)
{
	if(dialog && self){
		tsip_dialog_type_t regtype = tsip_dialog_REGISTER;
		tsk_safeobj_lock(self);
		
		/* remove the dialog */
		tsk_list_remove_item_by_data(self->dialogs, dialog);
		
		/* whether shutting down? */
		if(self->shutdown.inprogress){
			if(self->shutdown.phase2){ /* Phase 2 (all non-REGISTER and silent dialogs have been removed) */
				if(tsk_list_count(self->dialogs, pred_find_dialog_by_type, &regtype) == 0){
					/* alert only if there is not REGISTER dialog (ignore silents) */
					TSK_DEBUG_INFO("== Shutting down - Phase-2 completed ==");
					tsk_condwait_broadcast(self->shutdown.condwait);
				}
			}
			else{ /* Phase 1 */
				if(tsk_list_count(self->dialogs, pred_find_dialog_by_not_type, &regtype) == 0){
					/* alert only if all dialogs except REGISTER have been removed */
					TSK_DEBUG_INFO("== Shutting down - Phase-1 completed ==");
					tsk_condwait_broadcast(self->shutdown.condwait);
				}
			}
		}

		tsk_safeobj_unlock(self);

		return 0;
	}

	return -1;
}

// this function is only called if no transaction match
// for responses, the transaction will always match
int tsip_dialog_layer_handle_incoming_msg(const tsip_dialog_layer_t *self, tsip_message_t* message)
{
	int ret = -1;
	tsk_bool_t cid_matched;
	tsip_dialog_t* dialog;
	tsip_transac_t* transac = tsk_null;
	const tsip_transac_layer_t *layer_transac = self->stack->layer_transac;

	if(!layer_transac){
		goto bail;
	}
	
	//tsk_safeobj_lock(self);
	dialog = tsip_dialog_layer_find(self, message->Call_ID->value, 
		TSIP_MESSAGE_IS_RESPONSE(message) ? message->To->tag : message->From->tag, 
		TSIP_MESSAGE_IS_RESPONSE(message) ? message->From->tag : message->To->tag, 
		TSIP_MESSAGE_IS_REQUEST(message) ? TSIP_MESSAGE_AS_REQUEST(message)->line.request.request_type : tsip_NONE, 
		&cid_matched);
	//tsk_safeobj_unlock(self);
	
	if(dialog){
		if(TSIP_REQUEST_IS_CANCEL(message) || TSIP_REQUEST_IS_ACK(message)){
			ret = dialog->callback(dialog, tsip_dialog_i_msg, message);
			tsk_object_unref(dialog);
			goto bail;
		}
		else{
			static tsk_bool_t isCT = tsk_false;
			tsip_transac_dst_t* dst = tsip_transac_dst_dialog_create(dialog);
			transac = tsip_transac_layer_new(
				layer_transac, 
				isCT,
				message, 
				dst
			);
			TSK_OBJECT_SAFE_FREE(dst);
			TSK_OBJECT_SAFE_FREE(dialog);
		}
	}
	else{
		/* MediaProxyMode : forward all non-INVITE messages */
		if(self->stack->network.mode == tsip_stack_mode_webrtc2sip){
			tsk_bool_t b2bua;
			
			if(TSIP_MESSAGE_IS_REQUEST(message)){
				// requests received over TCP/TLS/UDP must contain "ws-src-ip" and "ws-src-port" parameters
				if(!TNET_SOCKET_TYPE_IS_WS(message->src_net_type) && !TNET_SOCKET_TYPE_IS_WSS(message->src_net_type)){
					const char* ws_src_ip = tsk_params_get_param_value(message->line.request.uri->params, "ws-src-ip");
					const tnet_port_t ws_src_port = (tnet_port_t)tsk_params_get_param_value_as_int(message->line.request.uri->params, "ws-src-port");
					if(!tsip_transport_layer_have_stream_peer_with_remote_ip(self->stack->layer_transport, ws_src_ip, ws_src_port)){
						if(!TSIP_REQUEST_IS_ACK(message)){ // ACK do not expect response
#if 0 // code commented because when using mjserver, rejecting the forked INVITE terminate all dialogs: have to check if it's conform to RFC 3261 or not
							tsip_response_t* response = tsip_response_new(488, "WebSocket Peer not connected", message);
							ret = tsip_transport_layer_send(self->stack->layer_transport, "no-branch", response);
							TSK_OBJECT_SAFE_FREE(response);
							return ret;
#else
							TSK_DEBUG_INFO("Request for peer at %s:%d cannot be delivered", ws_src_ip, ws_src_port);
#endif
						}
						return 0;
					}
				}
			}

			// "rtcweb-breaker" parameter will be in the Contact header for outgoing request and in the request-uri for incoming requests
			b2bua = TSIP_REQUEST_IS_INVITE(message) && message->Contact && message->Contact->uri &&
					(tsk_striequals(tsk_params_get_param_value(message->Contact->uri->params, "rtcweb-breaker"), "yes")
					|| tsk_striequals(tsk_params_get_param_value(message->line.request.uri->params, "rtcweb-breaker"), "yes"));
			
			if(!b2bua){
				// forward the message
				static tsk_bool_t isCT = tsk_true;
				tsip_transac_dst_t* dst;
				tsip_transac_t* transac;

				TSIP_MESSAGE(message)->update = tsk_true; // update AoR and Via
				if((dst = tsip_transac_dst_net_create(TSIP_STACK(self->stack)))){
					if((transac = tsip_transac_layer_new(self->stack->layer_transac, isCT, message, dst))){
						ret = tsip_transac_start(transac, message);
						TSK_OBJECT_SAFE_FREE(transac);
					}
					TSK_OBJECT_SAFE_FREE(dst);
				}
				return ret;
			}
		}

		if(TSIP_MESSAGE_IS_REQUEST(message)){
			tsip_ssession_t* ss = tsk_null;
			tsip_dialog_t* newdialog = tsk_null;
			
			switch(message->line.request.request_type){
				case tsip_MESSAGE:
					{	/* Server incoming MESSAGE */
						if((ss = tsip_ssession_create_2(self->stack, message))){
							newdialog = (tsip_dialog_t*)tsip_dialog_message_create(ss);
						}
						break;
					}
				case tsip_INFO:
					{	/* Server incoming INFO */
						if((ss = tsip_ssession_create_2(self->stack, message))){
							newdialog = (tsip_dialog_t*)tsip_dialog_info_create(ss);
						}
						break;
					}
				case tsip_OPTIONS:
					{	/* Server incoming OPTIONS */
						if((ss = tsip_ssession_create_2(self->stack, message))){
							newdialog = (tsip_dialog_t*)tsip_dialog_options_create(ss);
						}
						break;
					}

				case tsip_REGISTER:
					{	/* incoming REGISTER */
						if((ss = tsip_ssession_create_2(self->stack, message))){
							newdialog = (tsip_dialog_t*)tsip_dialog_register_create(ss, message->Call_ID ? message->Call_ID->value : tsk_null);
						}
						break;
					}

				case tsip_INVITE:
					{	/* incoming INVITE */
						if((ss = tsip_ssession_create_2(self->stack, message))){
							newdialog = (tsip_dialog_t*)tsip_dialog_invite_create(ss, message->Call_ID ? message->Call_ID->value : tsk_null);
						}
						break;
					}

				default:
					{
						break;
					}
			}//switch

			// for new dialog, create a new transac and start it later
			if(newdialog){
				static const tsk_bool_t isCT = tsk_false;
				tsip_transac_dst_t* dst = tsip_transac_dst_dialog_create(newdialog);
				transac = tsip_transac_layer_new(
					layer_transac, 
					isCT,
					message, 
					dst
				);
				if(message->local_fd > 0 && TNET_SOCKET_TYPE_IS_STREAM(message->src_net_type)) {
					tsip_dialog_set_connected_fd(newdialog, message->local_fd);
				}
				tsk_list_push_back_data(self->dialogs, (void**)&newdialog); /* add new dialog to the layer */
				TSK_OBJECT_SAFE_FREE(dst);
			}

			/* The dialog will become the owner of the SIP session
			* => when destoyed => SIP session will be destroyed, unless the user-end takes ownership() */
			TSK_OBJECT_SAFE_FREE(ss);
		}
	}

	if(transac){
		ret = tsip_transac_start(transac, message);
		tsk_object_unref(transac);
	}
	/* - No transaction match for the SIP request
	   - ACK do not expect any response (http://code.google.com/p/imsdroid/issues/detail?id=420)
	*/
	else if(TSIP_MESSAGE_IS_REQUEST(message) && !TSIP_REQUEST_IS_ACK(message)){
		const tsip_transport_layer_t *layer;
		tsip_response_t* response = tsk_null;
		if(!dialog && cid_matched){
			dialog = tsip_dialog_layer_find_by_callid((tsip_dialog_layer_t *)self, message->Call_ID->value);
		}

		if((layer = self->stack->layer_transport)){	
			if(cid_matched){ /* We are receiving our own message. */
				response = tsip_response_new(482, "Loop Detected (Check your iFCs)", message);
				if(response && !response->To->tag){/* Early dialog? */
					response->To->tag = tsk_strdup("doubango");
				}
			}
			else{
				switch(message->line.request.request_type){
					case tsip_OPTIONS: // Hacked to work on Tiscali IMS networks
					case tsip_INFO:
						response = tsip_response_new(405, "Method Not Allowed", message);
						break;
					default:
						response = tsip_response_new(481, "Dialog/Transaction Does Not Exist", message);
						break;
				}
			}
			if(response){
				if(dialog && TSIP_DIALOG_GET_SS(dialog)){
					tsk_strupdate(&response->sigcomp_id, TSIP_DIALOG_GET_SS(dialog)->sigcomp_id);
				}
				ret = tsip_transport_layer_send(layer, response->firstVia ? response->firstVia->branch : "no-branch", response);
				TSK_OBJECT_SAFE_FREE(response);
			}
		}

		TSK_OBJECT_SAFE_FREE(dialog);
	}
	
bail:
	return ret;
}





//========================================================
//	Dialog layer object definition
//
static tsk_object_t* tsip_dialog_layer_ctor(tsk_object_t * self, va_list * app)
{
	tsip_dialog_layer_t *layer = self;
	if(layer){
		layer->stack = va_arg(*app, const tsip_stack_t *);
		layer->dialogs = tsk_list_create();

		tsk_safeobj_init(layer);
	}
	return self;
}

static tsk_object_t* tsip_dialog_layer_dtor(tsk_object_t * self)
{ 
	tsip_dialog_layer_t *layer = self;
	if(layer){
		TSK_OBJECT_SAFE_FREE(layer->dialogs);

		/* condwait */
		if(layer->shutdown.condwait){
			tsk_condwait_destroy(&layer->shutdown.condwait);
		}

		tsk_safeobj_deinit(layer);

		TSK_DEBUG_INFO("*** Dialog Layer destroyed ***");
	}
	return self;
}

static int tsip_dialog_layer_cmp(const tsk_object_t *obj1, const tsk_object_t *obj2)
{
	return -1;
}

static const tsk_object_def_t tsip_dialog_layer_def_s = 
{
	sizeof(tsip_dialog_layer_t),
	tsip_dialog_layer_ctor, 
	tsip_dialog_layer_dtor,
	tsip_dialog_layer_cmp, 
};
const tsk_object_def_t *tsip_dialog_layer_def_t = &tsip_dialog_layer_def_s;
OpenPOWER on IntegriCloud