summaryrefslogtreecommitdiffstats
path: root/bindings/_common/SipStack.cxx
blob: 5ec3680ce3993b468bef9e6abf6f4b53c7187aff (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
/*
* Copyright (C) 2010-2011 Mamadou Diop.
*
* Contact: Mamadou Diop <diopmamadou(at)doubango.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.
*
*/
#include "SipStack.h"

#include "SipSession.h"
#include "SipEvent.h"

#include "DDebug.h"

#include "Common.h"

bool SipStack::g_bInitialized = false;


/* === ANSI-C functions (local use) === */
static int stack_callback(const tsip_event_t *sipevent);
static int session_handle_event(const tsip_event_t *sipevent);

SipStack::SipStack(SipCallback* pCallback, const char* realm_uri, const char* impi_uri, const char* impu_uri)
:SafeObject()
{
	m_pDebugCallback = tsk_null;
	m_pCallback = pCallback;

	/* Initialize network and media layers */
	if(!SipStack::initialize()){
		return;// isValid() will be false
	}

	/* Creates stack handle */
	m_pHandle = tsip_stack_create(stack_callback, realm_uri, impi_uri, impu_uri,
			TSIP_STACK_SET_USERDATA(this), /* used as context (useful for server-initiated requests) */
			TSIP_STACK_SET_NULL());
}

SipStack::~SipStack()
{
	this->stop();

	/* Destroy stack handle */
	TSK_OBJECT_SAFE_FREE(m_pHandle);
}

bool SipStack::start()
{
	bool ret = (tsip_stack_start(m_pHandle) == 0);
	return ret;
}

bool SipStack::setDebugCallback(DDebugCallback* pCallback)
{
	if(this && pCallback){
		m_pDebugCallback = pCallback;
		tsk_debug_set_arg_data(this);
		tsk_debug_set_info_cb(DDebugCallback::debug_info_cb);
		tsk_debug_set_warn_cb(DDebugCallback::debug_warn_cb);
		tsk_debug_set_error_cb(DDebugCallback::debug_error_cb);
		tsk_debug_set_fatal_cb(DDebugCallback::debug_fatal_cb);
	}
	else if(this){
		m_pDebugCallback = tsk_null;
		tsk_debug_set_arg_data(tsk_null);
		tsk_debug_set_info_cb(tsk_null);
		tsk_debug_set_warn_cb(tsk_null);
		tsk_debug_set_error_cb(tsk_null);
		tsk_debug_set_fatal_cb(tsk_null);
	}

	return true;
}

bool SipStack::setDisplayName(const char* display_name)
{
	int ret = tsip_stack_set(m_pHandle,
		TSIP_STACK_SET_DISPLAY_NAME(display_name),
		TSIP_STACK_SET_NULL());
	return (ret == 0);
}

bool SipStack::setRealm(const char* realm_uri)
{
	int ret = tsip_stack_set(m_pHandle,
		TSIP_STACK_SET_REALM(realm_uri),
		TSIP_STACK_SET_NULL());
	return (ret == 0);
}

bool SipStack::setIMPI(const char* impi)
{
	return (tsip_stack_set(m_pHandle,
		TSIP_STACK_SET_IMPI(impi),
		TSIP_STACK_SET_NULL()) == 0);
}

bool SipStack::setIMPU(const char* impu_uri)
{
	return (tsip_stack_set(m_pHandle,
		TSIP_STACK_SET_IMPU(impu_uri),
		TSIP_STACK_SET_NULL()) == 0);
}

bool SipStack::setPassword(const char* password)
{
	return (tsip_stack_set(m_pHandle,
		TSIP_STACK_SET_PASSWORD(password),
		TSIP_STACK_SET_NULL()) == 0);
}

bool SipStack::setAMF(const char* amf)
{
	uint16_t _amf = (uint16_t)tsk_atox(amf);
	return (tsip_stack_set(m_pHandle,
			TSIP_STACK_SET_IMS_AKA_AMF(_amf),
			TSIP_STACK_SET_NULL()) == 0);
}

bool SipStack::setOperatorId(const char* opid)
{
	return (tsip_stack_set(m_pHandle,
			TSIP_STACK_SET_IMS_AKA_OPERATOR_ID(opid),
			TSIP_STACK_SET_NULL()) == 0); 
}

bool SipStack::setProxyCSCF(const char* fqdn, unsigned short port, const char* transport, const char* ipversion)
{
	unsigned _port = port;//promote
	return (tsip_stack_set(m_pHandle,
			TSIP_STACK_SET_PROXY_CSCF(fqdn, _port, transport, ipversion),
			TSIP_STACK_SET_NULL()) == 0);
}

bool SipStack::setLocalIP(const char* ip, const char* transport/*=tsk_null*/)
{
	return (tsip_stack_set(m_pHandle,
		TSIP_STACK_SET_LOCAL_IP_2(transport, ip),
		TSIP_STACK_SET_NULL()) == 0);
}

bool SipStack::setLocalPort(unsigned short port, const char* transport/*=tsk_null*/)
{
	unsigned _port = port;//promote
	return (tsip_stack_set(m_pHandle,
		TSIP_STACK_SET_LOCAL_PORT_2(transport, _port),
		TSIP_STACK_SET_NULL()) == 0);
}

bool SipStack::setEarlyIMS(bool enabled){
	return (tsip_stack_set(m_pHandle,
		TSIP_STACK_SET_EARLY_IMS(enabled? tsk_true : tsk_false),
		TSIP_STACK_SET_NULL()) == 0);
}

bool SipStack::addHeader(const char* name, const char* value)
{
	return (tsip_stack_set(m_pHandle,
		TSIP_STACK_SET_HEADER(name, value),
		TSIP_STACK_SET_NULL()) == 0);
}

bool SipStack::removeHeader(const char* name)
{
	return (tsip_stack_set(m_pHandle,
		TSIP_STACK_UNSET_HEADER(name),
		TSIP_STACK_SET_NULL()) == 0);
}

bool SipStack::addDnsServer(const char* ip)
{
	return (tsip_stack_set(m_pHandle,
		TSIP_STACK_SET_DNS_SERVER(ip),
		TSIP_STACK_SET_NULL()) == 0);
}

bool SipStack::setDnsDiscovery(bool enabled)
{
	tsk_bool_t _enabled = enabled;// 32bit/64bit workaround
	return (tsip_stack_set(m_pHandle,
		TSIP_STACK_SET_DISCOVERY_NAPTR(_enabled),
		TSIP_STACK_SET_NULL()) == 0);
}

bool SipStack::setAoR(const char* ip, int port)
{
	return (tsip_stack_set(m_pHandle,
		TSIP_STACK_SET_AOR(ip, port),
		TSIP_STACK_SET_NULL()) == 0);
}

bool SipStack::setMode(enum tsip_stack_mode_e mode)
{
	return (tsip_stack_set(m_pHandle,
		TSIP_STACK_SET_MODE(mode),
		TSIP_STACK_SET_NULL()) == 0); 
}

bool SipStack::setSigCompParams(unsigned dms, unsigned sms, unsigned cpb, bool enablePresDict)
{
	tsk_bool_t _enablePresDict= enablePresDict;// 32bit/64bit workaround
	return (tsip_stack_set(m_pHandle,
		TSIP_STACK_SET_SIGCOMP(dms, sms, cpb, _enablePresDict),
		TSIP_STACK_SET_NULL()) == 0);
}

bool SipStack::addSigCompCompartment(const char* compId)
{
	return (tsip_stack_set(m_pHandle,
		TSIP_STACK_SET_SIGCOMP_NEW_COMPARTMENT(compId),
		TSIP_STACK_SET_NULL()) == 0);
}

bool SipStack::removeSigCompCompartment(const char* compId)
{
	return (tsip_stack_set(m_pHandle,
		TSIP_STACK_UNSET_SIGCOMP_COMPARTMENT(compId),
		TSIP_STACK_SET_NULL()) == 0);
}

 // @deprecated
bool SipStack::setSTUNEnabledForICE(bool enabled)
{
#if 0
	tsk_bool_t _enabled = enabled ? tsk_true : tsk_false;
	return (tsip_stack_set(m_pHandle,
		TSIP_STACK_SET_ICE_STUN_ENABLED(_enabled),
		TSIP_STACK_SET_NULL()) == 0);
#else
	// set global value
	return (tmedia_defaults_set_icestun_enabled(enabled ? tsk_true : tsk_false) == 0);
	// to set the value per session, use "CallSession::setICEStun()"
#endif
}

 // @deprecated
bool SipStack::setSTUNServer(const char* hostname, unsigned short port)
{
#if 0
	unsigned _port = port;//promote
	return (tsip_stack_set(m_pHandle,
		TSIP_STACK_SET_STUN_SERVER(hostname, _port),
		TSIP_STACK_SET_NULL()) == 0);
#else
	// set global value
	return (tmedia_defaults_set_stun_server(hostname, port) == 0);
	// to set the value per session, use "CallSession::setSTUNServer()"
#endif
}

 // @deprecated
bool SipStack::setSTUNCred(const char* login, const char* password)
{
#if 0
	return (tsip_stack_set(m_pHandle,
		TSIP_STACK_SET_STUN_CRED(login, password),
		TSIP_STACK_SET_NULL()) == 0);
#else
	// set global value
	return (tmedia_defaults_set_stun_cred(login, password) == 0);
	// to set the value per session, use "CallSession::setSTUNCred()"
#endif
}

bool SipStack::setSTUNEnabled(bool enabled)
{
	tsk_bool_t _enabled = enabled ? tsk_true : tsk_false;
	return (tsip_stack_set(m_pHandle,
		TSIP_STACK_SET_STUN_ENABLED(_enabled),
		TSIP_STACK_SET_NULL()) == 0);
}

bool SipStack::setTLSSecAgree(bool enabled)
{
	tsk_bool_t _enable = enabled ? tsk_true : tsk_false;
	return (tsip_stack_set(m_pHandle,
		TSIP_STACK_SET_SECAGREE_TLS(_enable),
		TSIP_STACK_SET_NULL()) == 0);
}

/*@deprecated: typo  */
bool SipStack::setSSLCretificates(const char* privKey, const char* pubKey, const char* caKey, bool verify/* = false*/)
{
	return setSSLCertificates(privKey, pubKey, caKey, verify);
}

bool SipStack::setSSLCertificates(const char* privKey, const char* pubKey, const char* caKey, bool verify/* = false*/)
{
	return (tsip_stack_set(m_pHandle,
		TSIP_STACK_SET_TLS_CERTS_2(caKey, pubKey, privKey, (verify ? tsk_true : tsk_false)),
		TSIP_STACK_SET_NULL()) == 0);
}

bool SipStack::setIPSecSecAgree(bool enabled)
{
	tsk_bool_t _enable = enabled;
	return (tsip_stack_set(m_pHandle,
		TSIP_STACK_SET_SECAGREE_IPSEC(_enable),
		TSIP_STACK_SET_NULL()) == 0);
}

bool SipStack::setIPSecParameters(const char* algo, const char* ealgo, const char* mode, const char* proto)
{
	return (tsip_stack_set(m_pHandle,
		TSIP_STACK_SET_IPSEC_PARAMS(algo, ealgo, mode, proto),
		TSIP_STACK_SET_NULL()) == 0);
}

char* SipStack::dnsENUM(const char* service, const char* e164num, const char* domain)
{
	tnet_dns_ctx_t* dnsctx = tsip_stack_get_dnsctx(m_pHandle);
	char* uri = tsk_null;

	if(dnsctx){
		if(!(uri = tnet_dns_enum_2(dnsctx, service, e164num, domain))){
			TSK_DEBUG_ERROR("ENUM(%s) failed", e164num);
		}
		tsk_object_unref(dnsctx);
		return uri;
	}
	else{
		TSK_DEBUG_ERROR("No DNS Context could be found");
		return tsk_null;
	}
}

char* SipStack::dnsNaptrSrv(const char* domain, const char* service, unsigned short *OUTPUT)
{
	tnet_dns_ctx_t* dnsctx = tsip_stack_get_dnsctx(m_pHandle);
	char* ip = tsk_null;
	tnet_port_t port;
	*OUTPUT = 0;
	

	if(dnsctx){
		if(!tnet_dns_query_naptr_srv(dnsctx, domain, service, &ip, &port)){
			*OUTPUT = port;
		}
		tsk_object_unref(dnsctx);
		return ip;
	}
	else{
		TSK_DEBUG_ERROR("No DNS Context could be found");
		return tsk_null;
	}
}

char* SipStack::dnsSrv(const char* service, unsigned short* OUTPUT)
{
	tnet_dns_ctx_t* dnsctx = tsip_stack_get_dnsctx(m_pHandle);
	char* ip = tsk_null;
	tnet_port_t port = 0;
	*OUTPUT = 0;

	if(dnsctx){
		if(!tnet_dns_query_srv(dnsctx, service, &ip, &port)){
			*OUTPUT = port;
		}
		tsk_object_unref(dnsctx);
		return ip;
	}
	else{
		TSK_DEBUG_ERROR("No DNS Context could be found");
		return tsk_null;
	}
}

bool SipStack::setMaxFDs(unsigned max_fds)
{
	return (tsip_stack_set(m_pHandle,
		TSIP_STACK_SET_MAX_FDS(max_fds),
		TSIP_STACK_SET_NULL()) == 0);
}

char* SipStack::getLocalIPnPort(const char* protocol, unsigned short* OUTPUT)
{
	tnet_ip_t ip;
	tnet_port_t port;
	int ret;

	if(!OUTPUT || !protocol){
		TSK_DEBUG_ERROR("invalid parameter");
		return tsk_null;
	}

	if((ret = tsip_stack_get_local_ip_n_port(m_pHandle, protocol, &port, &ip))){
		TSK_DEBUG_ERROR("Failed to get local ip and port with error code=%d", ret);
		return tsk_null;
	}

	*OUTPUT = port;
	return tsk_strdup(ip); // See Swig %newobject
}

char* SipStack::getPreferredIdentity()
{
	tsip_uri_t* ppid = tsip_stack_get_preferred_id(m_pHandle);
	char* str_ppid = tsk_null;
	if(ppid){
		str_ppid = tsip_uri_tostring(ppid, tsk_false, tsk_false);
		TSK_OBJECT_SAFE_FREE(ppid);
	}
	return str_ppid;
}

bool SipStack::isValid()
{
	return (m_pHandle != tsk_null);
}

bool SipStack::stop()
{
	int ret = tsip_stack_stop(m_pHandle);
	return (ret == 0);
}

bool SipStack::initialize()
{
	if (!g_bInitialized) {
		int ret;
		
		if((ret = tnet_startup())){
			TSK_DEBUG_ERROR("tnet_startup failed with error code=%d", ret);
			return false;
		}
		if((ret = thttp_startup())){
			TSK_DEBUG_ERROR("thttp_startup failed with error code=%d", ret);
			return false;
		}
		if((ret = tdav_init())){
			TSK_DEBUG_ERROR("tdav_init failed with error code=%d", ret);
			return false;
		}
		g_bInitialized = true;
	}
	return true;
}

bool SipStack::deInitialize()
{
	if (SipStack::g_bInitialized) {
		tdav_deinit();
		thttp_cleanup();
		tnet_cleanup();
		SipStack::g_bInitialized = false;
	}
	return false;
}

void SipStack::setCodecs(tdav_codec_id_t codecs)
{
	tdav_set_codecs(codecs);
}

void SipStack::setCodecs_2(int64_t codecs) // For stupid languages
{
	SipStack::setCodecs((tdav_codec_id_t)codecs);
}

bool SipStack::setCodecPriority(tdav_codec_id_t codec_id, int priority)
{
	return tdav_codec_set_priority(codec_id, priority) == 0;
}

bool SipStack::setCodecPriority_2(int codec_id, int priority)// For stupid languages
{
	return SipStack::setCodecPriority((tdav_codec_id_t)codec_id, priority);
}

bool SipStack::isCodecSupported(tdav_codec_id_t codec_id)
{
	return tdav_codec_is_supported(codec_id) ? true : false;
}

bool SipStack::isIPSecSupported()
{
	return tdav_ipsec_is_supported() ? true : false;
}

static int stack_callback(const tsip_event_t *sipevent)
{
	int ret = 0;
	const SipStack* sipStack = tsk_null;
	SipEvent* e = tsk_null;

	if(!sipevent){ /* should never happen ...but who know? */
		TSK_DEBUG_WARN("Null SIP event.");
		return -1;
	}
	else {
		if(sipevent->type == tsip_event_stack && sipevent->userdata){
			/* sessionless event */
			sipStack = dyn_cast<const SipStack*>((const SipStack*)sipevent->userdata);
		}
		else {
			const void* userdata;
			/* gets the stack from the session */
			const tsip_stack_handle_t* stack_handle = tsip_ssession_get_stack(sipevent->ss);
			if(stack_handle && (userdata = tsip_stack_get_userdata(stack_handle))){
				sipStack = dyn_cast<const SipStack*>((const SipStack*)userdata);
			}
		}
	}

	if(!sipStack){
		TSK_DEBUG_WARN("Invalid SIP event (Stack is Null).");
		return -2;
	}

	sipStack->Lock();

	switch(sipevent->type){
		case tsip_event_register:
			{	/* REGISTER */
				if(sipStack->getCallback()){
					e = new RegistrationEvent(sipevent);
					sipStack->getCallback()->OnRegistrationEvent((const RegistrationEvent*)e);
				}
				break;
			}
		case tsip_event_invite:
			{	/* INVITE */
				if(sipStack->getCallback()){
					e = new InviteEvent(sipevent);
					sipStack->getCallback()->OnInviteEvent((const InviteEvent*)e);
				}
				break;
			}
		case tsip_event_message:
			{	/* MESSAGE */
				if(sipStack->getCallback()){
					e = new MessagingEvent(sipevent);
					sipStack->getCallback()->OnMessagingEvent((const MessagingEvent*)e);
				}
				break;
			}
		case tsip_event_info:
			{	/* INFO */
				if(sipStack->getCallback()){
					e = new InfoEvent(sipevent);
					sipStack->getCallback()->OnInfoEvent((const InfoEvent*)e);
				}
				break;
			}
		case tsip_event_options:
			{ /* OPTIONS */
				if(sipStack->getCallback()){
					e = new OptionsEvent(sipevent);
					sipStack->getCallback()->OnOptionsEvent((const OptionsEvent*)e);
				}
				break;
			}
		case tsip_event_publish:
			{ /* PUBLISH */
				if(sipStack->getCallback()){
					e = new PublicationEvent(sipevent);
					sipStack->getCallback()->OnPublicationEvent((const PublicationEvent*)e);
				}
				break;
			}
		case tsip_event_subscribe:
			{	/* SUBSCRIBE */
				if(sipStack->getCallback()){
					e = new SubscriptionEvent(sipevent);
					sipStack->getCallback()->OnSubscriptionEvent((const SubscriptionEvent*)e);
				}
				break;
			}

		case tsip_event_dialog:
			{	/* Common to all dialogs */
				if(sipStack->getCallback()){
					e = new DialogEvent(sipevent);
					sipStack->getCallback()->OnDialogEvent((const DialogEvent*)e);
				}
				break;
			}

		case tsip_event_stack:
			{	/* Stack event */
				if(sipStack->getCallback()){
					e = new StackEvent(sipevent);
					sipStack->getCallback()->OnStackEvent((const StackEvent*)e);
				}
				break;
			}

		default:
			{	/* Unsupported */
				TSK_DEBUG_WARN("%d not supported as SIP event.", sipevent->type);
				ret = -3;
				break;
			}
	}

	sipStack->UnLock();

	if(e){
		delete e;
	}

	return ret;
}

OpenPOWER on IntegriCloud